* MAINTAINERS (Write After Approval): Add myself.
[official-gcc.git] / libgcc / unwind-dw2-fde-dip.c
blob92f8ab546449618a53fcf864924eac31308db5bf
1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009, 2010, 2011
2 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF
27 segment and dl_iterate_phdr to avoid register/deregister calls at
28 DSO load/unload. */
30 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE 1
32 #endif
34 #include "tconfig.h"
35 #include "tsystem.h"
36 #ifndef inhibit_libc
37 #include <elf.h> /* Get DT_CONFIG. */
38 #endif
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "libgcc_tm.h"
42 #include "dwarf2.h"
43 #include "unwind.h"
44 #define NO_BASE_OF_ENCODED_VALUE
45 #include "unwind-pe.h"
46 #include "unwind-dw2-fde.h"
47 #include "unwind-compat.h"
48 #include "gthr.h"
50 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
51 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
52 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
53 # define USE_PT_GNU_EH_FRAME
54 #endif
56 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
57 && defined(__BIONIC__)
58 # define USE_PT_GNU_EH_FRAME
59 #endif
61 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
62 && defined(__FreeBSD__) && __FreeBSD__ >= 7
63 # define ElfW __ElfN
64 # define USE_PT_GNU_EH_FRAME
65 #endif
67 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
68 && defined(TARGET_DL_ITERATE_PHDR) \
69 && defined(__sun__) && defined(__svr4__)
70 # define USE_PT_GNU_EH_FRAME
71 #endif
73 #if defined(USE_PT_GNU_EH_FRAME)
75 #include <link.h>
77 #ifndef __RELOC_POINTER
78 # define __RELOC_POINTER(ptr, base) ((ptr) + (base))
79 #endif
81 static const fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases);
83 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
84 #include "unwind-dw2-fde.c"
85 #undef _Unwind_Find_FDE
87 #ifndef PT_GNU_EH_FRAME
88 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
89 #endif
91 struct unw_eh_callback_data
93 _Unwind_Ptr pc;
94 void *tbase;
95 void *dbase;
96 void *func;
97 const fde *ret;
98 int check_cache;
101 struct unw_eh_frame_hdr
103 unsigned char version;
104 unsigned char eh_frame_ptr_enc;
105 unsigned char fde_count_enc;
106 unsigned char table_enc;
109 #define FRAME_HDR_CACHE_SIZE 8
111 static struct frame_hdr_cache_element
113 _Unwind_Ptr pc_low;
114 _Unwind_Ptr pc_high;
115 _Unwind_Ptr load_base;
116 const ElfW(Phdr) *p_eh_frame_hdr;
117 const ElfW(Phdr) *p_dynamic;
118 struct frame_hdr_cache_element *link;
119 } frame_hdr_cache[FRAME_HDR_CACHE_SIZE];
121 static struct frame_hdr_cache_element *frame_hdr_cache_head;
123 /* Like base_of_encoded_value, but take the base from a struct
124 unw_eh_callback_data instead of an _Unwind_Context. */
126 static _Unwind_Ptr
127 base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
129 if (encoding == DW_EH_PE_omit)
130 return 0;
132 switch (encoding & 0x70)
134 case DW_EH_PE_absptr:
135 case DW_EH_PE_pcrel:
136 case DW_EH_PE_aligned:
137 return 0;
139 case DW_EH_PE_textrel:
140 return (_Unwind_Ptr) data->tbase;
141 case DW_EH_PE_datarel:
142 return (_Unwind_Ptr) data->dbase;
143 default:
144 gcc_unreachable ();
148 static int
149 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
151 struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
152 const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
153 long n, match;
154 #ifdef __FRV_FDPIC__
155 struct elf32_fdpic_loadaddr load_base;
156 #else
157 _Unwind_Ptr load_base;
158 #endif
159 const unsigned char *p;
160 const struct unw_eh_frame_hdr *hdr;
161 _Unwind_Ptr eh_frame;
162 struct object ob;
163 _Unwind_Ptr pc_low = 0, pc_high = 0;
165 struct ext_dl_phdr_info
167 ElfW(Addr) dlpi_addr;
168 const char *dlpi_name;
169 const ElfW(Phdr) *dlpi_phdr;
170 ElfW(Half) dlpi_phnum;
171 unsigned long long int dlpi_adds;
172 unsigned long long int dlpi_subs;
175 match = 0;
176 phdr = info->dlpi_phdr;
177 load_base = info->dlpi_addr;
178 p_eh_frame_hdr = NULL;
179 p_dynamic = NULL;
181 struct frame_hdr_cache_element *prev_cache_entry = NULL,
182 *last_cache_entry = NULL;
184 if (data->check_cache && size >= sizeof (struct ext_dl_phdr_info))
186 static unsigned long long adds = -1ULL, subs;
187 struct ext_dl_phdr_info *einfo = (struct ext_dl_phdr_info *) info;
189 /* We use a least recently used cache replacement policy. Also,
190 the most recently used cache entries are placed at the head
191 of the search chain. */
193 if (einfo->dlpi_adds == adds && einfo->dlpi_subs == subs)
195 /* Find data->pc in shared library cache.
196 Set load_base, p_eh_frame_hdr and p_dynamic
197 plus match from the cache and goto
198 "Read .eh_frame_hdr header." below. */
200 struct frame_hdr_cache_element *cache_entry;
202 for (cache_entry = frame_hdr_cache_head;
203 cache_entry;
204 cache_entry = cache_entry->link)
206 if (data->pc >= cache_entry->pc_low
207 && data->pc < cache_entry->pc_high)
209 load_base = cache_entry->load_base;
210 p_eh_frame_hdr = cache_entry->p_eh_frame_hdr;
211 p_dynamic = cache_entry->p_dynamic;
213 /* And move the entry we're using to the head. */
214 if (cache_entry != frame_hdr_cache_head)
216 prev_cache_entry->link = cache_entry->link;
217 cache_entry->link = frame_hdr_cache_head;
218 frame_hdr_cache_head = cache_entry;
220 goto found;
223 last_cache_entry = cache_entry;
224 /* Exit early if we found an unused entry. */
225 if ((cache_entry->pc_low | cache_entry->pc_high) == 0)
226 break;
227 if (cache_entry->link != NULL)
228 prev_cache_entry = cache_entry;
231 else
233 adds = einfo->dlpi_adds;
234 subs = einfo->dlpi_subs;
235 /* Initialize the cache. Create a chain of cache entries,
236 with the final one terminated by a NULL link. */
237 int i;
238 for (i = 0; i < FRAME_HDR_CACHE_SIZE; i++)
240 frame_hdr_cache[i].pc_low = 0;
241 frame_hdr_cache[i].pc_high = 0;
242 frame_hdr_cache[i].link = &frame_hdr_cache[i+1];
244 frame_hdr_cache[i-1].link = NULL;
245 frame_hdr_cache_head = &frame_hdr_cache[0];
246 data->check_cache = 0;
250 /* Make sure struct dl_phdr_info is at least as big as we need. */
251 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
252 + sizeof (info->dlpi_phnum))
253 return -1;
255 /* See if PC falls into one of the loaded segments. Find the eh_frame
256 segment at the same time. */
257 for (n = info->dlpi_phnum; --n >= 0; phdr++)
259 if (phdr->p_type == PT_LOAD)
261 _Unwind_Ptr vaddr = (_Unwind_Ptr)
262 __RELOC_POINTER (phdr->p_vaddr, load_base);
263 if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
265 match = 1;
266 pc_low = vaddr;
267 pc_high = vaddr + phdr->p_memsz;
270 else if (phdr->p_type == PT_GNU_EH_FRAME)
271 p_eh_frame_hdr = phdr;
272 #ifdef PT_SUNW_UNWIND
273 /* Sun ld emits PT_SUNW_UNWIND .eh_frame_hdr sections instead of
274 PT_SUNW_EH_FRAME/PT_GNU_EH_FRAME, so accept them as well. */
275 else if (phdr->p_type == PT_SUNW_UNWIND)
276 p_eh_frame_hdr = phdr;
277 #endif
278 else if (phdr->p_type == PT_DYNAMIC)
279 p_dynamic = phdr;
282 if (!match)
283 return 0;
285 if (size >= sizeof (struct ext_dl_phdr_info))
287 /* Move the cache entry we're about to overwrite to the head of
288 the list. If either last_cache_entry or prev_cache_entry are
289 NULL, that cache entry is already at the head. */
290 if (last_cache_entry != NULL && prev_cache_entry != NULL)
292 prev_cache_entry->link = last_cache_entry->link;
293 last_cache_entry->link = frame_hdr_cache_head;
294 frame_hdr_cache_head = last_cache_entry;
297 frame_hdr_cache_head->load_base = load_base;
298 frame_hdr_cache_head->p_eh_frame_hdr = p_eh_frame_hdr;
299 frame_hdr_cache_head->p_dynamic = p_dynamic;
300 frame_hdr_cache_head->pc_low = pc_low;
301 frame_hdr_cache_head->pc_high = pc_high;
304 found:
306 if (!p_eh_frame_hdr)
307 return 0;
309 /* Read .eh_frame_hdr header. */
310 hdr = (const struct unw_eh_frame_hdr *)
311 __RELOC_POINTER (p_eh_frame_hdr->p_vaddr, load_base);
312 if (hdr->version != 1)
313 return 1;
315 #ifdef CRT_GET_RFIB_DATA
316 # ifdef __i386__
317 data->dbase = NULL;
318 if (p_dynamic)
320 /* For dynamically linked executables and shared libraries,
321 DT_PLTGOT is the gp value for that object. */
322 ElfW(Dyn) *dyn = (ElfW(Dyn) *)
323 __RELOC_POINTER (p_dynamic->p_vaddr, load_base);
324 for (; dyn->d_tag != DT_NULL ; dyn++)
325 if (dyn->d_tag == DT_PLTGOT)
327 data->dbase = (void *) dyn->d_un.d_ptr;
328 #if defined __linux__
329 /* On IA-32 Linux, _DYNAMIC is writable and GLIBC has
330 relocated it. */
331 #elif defined __sun__ && defined __svr4__
332 /* On Solaris 2/x86, we need to do this ourselves. */
333 data->dbase += load_base;
334 #endif
335 break;
338 # elif defined __FRV_FDPIC__ && defined __linux__
339 data->dbase = load_base.got_value;
340 # elif defined __x86_64__ && defined __sun__ && defined __svr4__
341 /* While CRT_GET_RFIB_DATA is also defined for 64-bit Solaris 10+/x86, it
342 doesn't apply since it uses DW_EH_PE_pcrel encoding. */
343 # else
344 # error What is DW_EH_PE_datarel base on this platform?
345 # endif
346 #endif
348 p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
349 base_from_cb_data (hdr->eh_frame_ptr_enc,
350 data),
351 (const unsigned char *) (hdr + 1),
352 &eh_frame);
354 /* We require here specific table encoding to speed things up.
355 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
356 as base, not the processor specific DW_EH_PE_datarel. */
357 if (hdr->fde_count_enc != DW_EH_PE_omit
358 && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
360 _Unwind_Ptr fde_count;
362 p = read_encoded_value_with_base (hdr->fde_count_enc,
363 base_from_cb_data (hdr->fde_count_enc,
364 data),
365 p, &fde_count);
366 /* Shouldn't happen. */
367 if (fde_count == 0)
368 return 1;
369 if ((((_Unwind_Ptr) p) & 3) == 0)
371 struct fde_table {
372 signed initial_loc __attribute__ ((mode (SI)));
373 signed fde __attribute__ ((mode (SI)));
375 const struct fde_table *table = (const struct fde_table *) p;
376 size_t lo, hi, mid;
377 _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
378 fde *f;
379 unsigned int f_enc, f_enc_size;
380 _Unwind_Ptr range;
382 mid = fde_count - 1;
383 if (data->pc < table[0].initial_loc + data_base)
384 return 1;
385 else if (data->pc < table[mid].initial_loc + data_base)
387 lo = 0;
388 hi = mid;
390 while (lo < hi)
392 mid = (lo + hi) / 2;
393 if (data->pc < table[mid].initial_loc + data_base)
394 hi = mid;
395 else if (data->pc >= table[mid + 1].initial_loc + data_base)
396 lo = mid + 1;
397 else
398 break;
401 gcc_assert (lo < hi);
404 f = (fde *) (table[mid].fde + data_base);
405 f_enc = get_fde_encoding (f);
406 f_enc_size = size_of_encoded_value (f_enc);
407 read_encoded_value_with_base (f_enc & 0x0f, 0,
408 &f->pc_begin[f_enc_size], &range);
409 if (data->pc < table[mid].initial_loc + data_base + range)
410 data->ret = f;
411 data->func = (void *) (table[mid].initial_loc + data_base);
412 return 1;
416 /* We have no sorted search table, so need to go the slow way.
417 As soon as GLIBC will provide API so to notify that a library has been
418 removed, we could cache this (and thus use search_object). */
419 ob.pc_begin = NULL;
420 ob.tbase = data->tbase;
421 ob.dbase = data->dbase;
422 ob.u.single = (fde *) eh_frame;
423 ob.s.i = 0;
424 ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */
425 data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
426 if (data->ret != NULL)
428 _Unwind_Ptr func;
429 unsigned int encoding = get_fde_encoding (data->ret);
431 read_encoded_value_with_base (encoding,
432 base_from_cb_data (encoding, data),
433 data->ret->pc_begin, &func);
434 data->func = (void *) func;
436 return 1;
439 const fde *
440 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
442 struct unw_eh_callback_data data;
443 const fde *ret;
445 ret = _Unwind_Find_registered_FDE (pc, bases);
446 if (ret != NULL)
447 return ret;
449 data.pc = (_Unwind_Ptr) pc;
450 data.tbase = NULL;
451 data.dbase = NULL;
452 data.func = NULL;
453 data.ret = NULL;
454 data.check_cache = 1;
456 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
457 return NULL;
459 if (data.ret)
461 bases->tbase = data.tbase;
462 bases->dbase = data.dbase;
463 bases->func = data.func;
465 return data.ret;
468 #else
469 /* Prevent multiple include of header files. */
470 #define _Unwind_Find_FDE _Unwind_Find_FDE
471 #include "unwind-dw2-fde.c"
472 #endif
474 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
475 alias (_Unwind_Find_FDE);
476 #endif