vfs: check userland buffers before reading them.
[haiku.git] / src / libs / libunwind / dwarf / Gfind_unwind_table.c
blobd54a370b275c3233fb6fb2b0ffd88bffa17f3847
1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2003-2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of libunwind.
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26 #include <fcntl.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include <sys/mman.h>
32 #include "libunwind_i.h"
33 #include "dwarf-eh.h"
34 #include "dwarf_i.h"
36 int
37 dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
38 char *path, unw_word_t segbase, unw_word_t mapoff,
39 unw_word_t ip)
41 Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
42 unw_word_t addr, eh_frame_start, fde_count, load_base;
43 unw_word_t max_load_addr = 0;
44 unw_word_t start_ip = (unw_word_t) -1;
45 unw_word_t end_ip = 0;
46 struct dwarf_eh_frame_hdr *hdr;
47 unw_accessors_t *a;
48 Elf_W(Ehdr) *ehdr;
49 #if UNW_TARGET_ARM
50 const Elf_W(Phdr) *parm_exidx = NULL;
51 #endif
52 int i, ret, found = 0;
54 /* XXX: Much of this code is Linux/LSB-specific. */
56 if (!elf_w(valid_object) (&edi->ei))
57 return -UNW_ENOINFO;
59 ehdr = edi->ei.image;
60 phdr = (Elf_W(Phdr) *) ((char *) edi->ei.image + ehdr->e_phoff);
62 for (i = 0; i < ehdr->e_phnum; ++i)
64 switch (phdr[i].p_type)
66 case PT_LOAD:
67 if (phdr[i].p_vaddr < start_ip)
68 start_ip = phdr[i].p_vaddr;
70 if (phdr[i].p_vaddr + phdr[i].p_memsz > end_ip)
71 end_ip = phdr[i].p_vaddr + phdr[i].p_memsz;
73 if (phdr[i].p_offset == mapoff)
74 ptxt = phdr + i;
75 if ((uintptr_t) edi->ei.image + phdr->p_filesz > max_load_addr)
76 max_load_addr = (uintptr_t) edi->ei.image + phdr->p_filesz;
77 break;
79 case PT_GNU_EH_FRAME:
80 peh_hdr = phdr + i;
81 break;
83 case PT_DYNAMIC:
84 pdyn = phdr + i;
85 break;
87 #if UNW_TARGET_ARM
88 case PT_ARM_EXIDX:
89 parm_exidx = phdr + i;
90 break;
91 #endif
93 default:
94 break;
98 if (!ptxt)
99 return 0;
101 load_base = segbase - ptxt->p_vaddr;
102 start_ip += load_base;
103 end_ip += load_base;
105 if (peh_hdr)
107 if (pdyn)
109 /* For dynamicly linked executables and shared libraries,
110 DT_PLTGOT is the value that data-relative addresses are
111 relative to for that object. We call this the "gp". */
112 Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(pdyn->p_offset
113 + (char *) edi->ei.image);
114 for (; dyn->d_tag != DT_NULL; ++dyn)
115 if (dyn->d_tag == DT_PLTGOT)
117 /* Assume that _DYNAMIC is writable and GLIBC has
118 relocated it (true for x86 at least). */
119 edi->di_cache.gp = dyn->d_un.d_ptr;
120 break;
123 else
124 /* Otherwise this is a static executable with no _DYNAMIC. Assume
125 that data-relative addresses are relative to 0, i.e.,
126 absolute. */
127 edi->di_cache.gp = 0;
129 hdr = (struct dwarf_eh_frame_hdr *) (peh_hdr->p_offset
130 + (char *) edi->ei.image);
131 if (hdr->version != DW_EH_VERSION)
133 Debug (1, "table `%s' has unexpected version %d\n",
134 path, hdr->version);
135 return -UNW_ENOINFO;
138 a = unw_get_accessors (unw_local_addr_space);
139 addr = (unw_word_t) (hdr + 1);
141 /* (Optionally) read eh_frame_ptr: */
142 if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
143 &addr, hdr->eh_frame_ptr_enc, edi->di_cache.gp, 0,
144 &eh_frame_start, NULL)) < 0)
145 return -UNW_ENOINFO;
147 /* (Optionally) read fde_count: */
148 if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
149 &addr, hdr->fde_count_enc, edi->di_cache.gp, 0,
150 &fde_count, NULL)) < 0)
151 return -UNW_ENOINFO;
153 if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
155 #if 1
156 abort ();
157 #else
158 unw_word_t eh_frame_end;
160 /* If there is no search table or it has an unsupported
161 encoding, fall back on linear search. */
162 if (hdr->table_enc == DW_EH_PE_omit)
163 Debug (4, "EH lacks search table; doing linear search\n");
164 else
165 Debug (4, "EH table has encoding 0x%x; doing linear search\n",
166 hdr->table_enc);
168 eh_frame_end = max_load_addr; /* XXX can we do better? */
170 if (hdr->fde_count_enc == DW_EH_PE_omit)
171 fde_count = ~0UL;
172 if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
173 abort ();
175 return linear_search (unw_local_addr_space, ip,
176 eh_frame_start, eh_frame_end, fde_count,
177 pi, need_unwind_info, NULL);
178 #endif
181 edi->di_cache.start_ip = start_ip;
182 edi->di_cache.end_ip = end_ip;
183 edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
184 edi->di_cache.u.rti.name_ptr = 0;
185 /* two 32-bit values (ip_offset/fde_offset) per table-entry: */
186 edi->di_cache.u.rti.table_len = (fde_count * 8) / sizeof (unw_word_t);
187 edi->di_cache.u.rti.table_data = ((load_base + peh_hdr->p_vaddr)
188 + (addr - (unw_word_t) edi->ei.image
189 - peh_hdr->p_offset));
191 /* For the binary-search table in the eh_frame_hdr, data-relative
192 means relative to the start of that section... */
193 edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
194 + ((unw_word_t) hdr - (unw_word_t) edi->ei.image
195 - peh_hdr->p_offset));
196 found = 1;
199 #if UNW_TARGET_ARM
200 if (parm_exidx)
202 edi->di_arm.format = UNW_INFO_FORMAT_ARM_EXIDX;
203 edi->di_arm.start_ip = start_ip;
204 edi->di_arm.end_ip = end_ip;
205 edi->di_arm.u.rti.name_ptr = (unw_word_t) path;
206 edi->di_arm.u.rti.table_data = load_base + parm_exidx->p_vaddr;
207 edi->di_arm.u.rti.table_len = parm_exidx->p_memsz;
208 found = 1;
210 #endif
212 #ifdef CONFIG_DEBUG_FRAME
213 /* Try .debug_frame. */
214 found = dwarf_find_debug_frame (found, &edi->di_debug, ip, load_base, path,
215 start_ip, end_ip);
216 #endif
218 return found;