Correct ChangeLog entry.
[official-gcc.git] / libgcc / unwind-dw2-fde-dip.c
blob47af7a34a6ffc37958b5b310bbc29e1e8dcb3667
1 /* Copyright (C) 2001-2015 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@redhat.com>.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF
26 segment and dl_iterate_phdr to avoid register/deregister calls at
27 DSO load/unload. */
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE 1
31 #endif
33 #include "tconfig.h"
34 #include "tsystem.h"
35 #if !defined(inhibit_libc) && !defined(__OpenBSD__)
36 #include <elf.h> /* Get DT_CONFIG. */
37 #endif
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "libgcc_tm.h"
41 #include "dwarf2.h"
42 #include "unwind.h"
43 #define NO_BASE_OF_ENCODED_VALUE
44 #include "unwind-pe.h"
45 #include "unwind-dw2-fde.h"
46 #include "unwind-compat.h"
47 #include "gthr.h"
49 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
50 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
51 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
52 # define USE_PT_GNU_EH_FRAME
53 #endif
55 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
56 && defined(__BIONIC__)
57 # define USE_PT_GNU_EH_FRAME
58 #endif
60 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
61 && defined(TARGET_DL_ITERATE_PHDR) \
62 && (defined(__DragonFly__) || defined(__FreeBSD__))
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(__OpenBSD__)
69 # define ElfW(type) Elf_##type
70 # define USE_PT_GNU_EH_FRAME
71 #endif
73 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
74 && defined(TARGET_DL_ITERATE_PHDR) \
75 && defined(__sun__) && defined(__svr4__)
76 # define USE_PT_GNU_EH_FRAME
77 #endif
79 #if defined(USE_PT_GNU_EH_FRAME)
81 #include <link.h>
83 #ifndef __RELOC_POINTER
84 # define __RELOC_POINTER(ptr, base) ((ptr) + (base))
85 #endif
87 static const fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases);
89 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
90 #include "unwind-dw2-fde.c"
91 #undef _Unwind_Find_FDE
93 #ifndef PT_GNU_EH_FRAME
94 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
95 #endif
97 struct unw_eh_callback_data
99 _Unwind_Ptr pc;
100 void *tbase;
101 void *dbase;
102 void *func;
103 const fde *ret;
104 int check_cache;
107 struct unw_eh_frame_hdr
109 unsigned char version;
110 unsigned char eh_frame_ptr_enc;
111 unsigned char fde_count_enc;
112 unsigned char table_enc;
115 #define FRAME_HDR_CACHE_SIZE 8
117 static struct frame_hdr_cache_element
119 _Unwind_Ptr pc_low;
120 _Unwind_Ptr pc_high;
121 _Unwind_Ptr load_base;
122 const ElfW(Phdr) *p_eh_frame_hdr;
123 const ElfW(Phdr) *p_dynamic;
124 struct frame_hdr_cache_element *link;
125 } frame_hdr_cache[FRAME_HDR_CACHE_SIZE];
127 static struct frame_hdr_cache_element *frame_hdr_cache_head;
129 /* Like base_of_encoded_value, but take the base from a struct
130 unw_eh_callback_data instead of an _Unwind_Context. */
132 static _Unwind_Ptr
133 base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
135 if (encoding == DW_EH_PE_omit)
136 return 0;
138 switch (encoding & 0x70)
140 case DW_EH_PE_absptr:
141 case DW_EH_PE_pcrel:
142 case DW_EH_PE_aligned:
143 return 0;
145 case DW_EH_PE_textrel:
146 return (_Unwind_Ptr) data->tbase;
147 case DW_EH_PE_datarel:
148 return (_Unwind_Ptr) data->dbase;
149 default:
150 gcc_unreachable ();
154 static int
155 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
157 struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
158 const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
159 long n, match;
160 #ifdef __FRV_FDPIC__
161 struct elf32_fdpic_loadaddr load_base;
162 #else
163 _Unwind_Ptr load_base;
164 #endif
165 const unsigned char *p;
166 const struct unw_eh_frame_hdr *hdr;
167 _Unwind_Ptr eh_frame;
168 struct object ob;
169 _Unwind_Ptr pc_low = 0, pc_high = 0;
171 struct ext_dl_phdr_info
173 ElfW(Addr) dlpi_addr;
174 const char *dlpi_name;
175 const ElfW(Phdr) *dlpi_phdr;
176 ElfW(Half) dlpi_phnum;
177 unsigned long long int dlpi_adds;
178 unsigned long long int dlpi_subs;
181 match = 0;
182 phdr = info->dlpi_phdr;
183 load_base = info->dlpi_addr;
184 p_eh_frame_hdr = NULL;
185 p_dynamic = NULL;
187 struct frame_hdr_cache_element *prev_cache_entry = NULL,
188 *last_cache_entry = NULL;
190 if (data->check_cache && size >= sizeof (struct ext_dl_phdr_info))
192 static unsigned long long adds = -1ULL, subs;
193 struct ext_dl_phdr_info *einfo = (struct ext_dl_phdr_info *) info;
195 /* We use a least recently used cache replacement policy. Also,
196 the most recently used cache entries are placed at the head
197 of the search chain. */
199 if (einfo->dlpi_adds == adds && einfo->dlpi_subs == subs)
201 /* Find data->pc in shared library cache.
202 Set load_base, p_eh_frame_hdr and p_dynamic
203 plus match from the cache and goto
204 "Read .eh_frame_hdr header." below. */
206 struct frame_hdr_cache_element *cache_entry;
208 for (cache_entry = frame_hdr_cache_head;
209 cache_entry;
210 cache_entry = cache_entry->link)
212 if (data->pc >= cache_entry->pc_low
213 && data->pc < cache_entry->pc_high)
215 load_base = cache_entry->load_base;
216 p_eh_frame_hdr = cache_entry->p_eh_frame_hdr;
217 p_dynamic = cache_entry->p_dynamic;
219 /* And move the entry we're using to the head. */
220 if (cache_entry != frame_hdr_cache_head)
222 prev_cache_entry->link = cache_entry->link;
223 cache_entry->link = frame_hdr_cache_head;
224 frame_hdr_cache_head = cache_entry;
226 goto found;
229 last_cache_entry = cache_entry;
230 /* Exit early if we found an unused entry. */
231 if ((cache_entry->pc_low | cache_entry->pc_high) == 0)
232 break;
233 if (cache_entry->link != NULL)
234 prev_cache_entry = cache_entry;
237 else
239 adds = einfo->dlpi_adds;
240 subs = einfo->dlpi_subs;
241 /* Initialize the cache. Create a chain of cache entries,
242 with the final one terminated by a NULL link. */
243 int i;
244 for (i = 0; i < FRAME_HDR_CACHE_SIZE; i++)
246 frame_hdr_cache[i].pc_low = 0;
247 frame_hdr_cache[i].pc_high = 0;
248 frame_hdr_cache[i].link = &frame_hdr_cache[i+1];
250 frame_hdr_cache[i-1].link = NULL;
251 frame_hdr_cache_head = &frame_hdr_cache[0];
252 data->check_cache = 0;
256 /* Make sure struct dl_phdr_info is at least as big as we need. */
257 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
258 + sizeof (info->dlpi_phnum))
259 return -1;
261 /* See if PC falls into one of the loaded segments. Find the eh_frame
262 segment at the same time. */
263 for (n = info->dlpi_phnum; --n >= 0; phdr++)
265 if (phdr->p_type == PT_LOAD)
267 _Unwind_Ptr vaddr = (_Unwind_Ptr)
268 __RELOC_POINTER (phdr->p_vaddr, load_base);
269 if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
271 match = 1;
272 pc_low = vaddr;
273 pc_high = vaddr + phdr->p_memsz;
276 else if (phdr->p_type == PT_GNU_EH_FRAME)
277 p_eh_frame_hdr = phdr;
278 #ifdef PT_SUNW_UNWIND
279 /* Sun ld emits PT_SUNW_UNWIND .eh_frame_hdr sections instead of
280 PT_SUNW_EH_FRAME/PT_GNU_EH_FRAME, so accept them as well. */
281 else if (phdr->p_type == PT_SUNW_UNWIND)
282 p_eh_frame_hdr = phdr;
283 #endif
284 else if (phdr->p_type == PT_DYNAMIC)
285 p_dynamic = phdr;
288 if (!match)
289 return 0;
291 if (size >= sizeof (struct ext_dl_phdr_info))
293 /* Move the cache entry we're about to overwrite to the head of
294 the list. If either last_cache_entry or prev_cache_entry are
295 NULL, that cache entry is already at the head. */
296 if (last_cache_entry != NULL && prev_cache_entry != NULL)
298 prev_cache_entry->link = last_cache_entry->link;
299 last_cache_entry->link = frame_hdr_cache_head;
300 frame_hdr_cache_head = last_cache_entry;
303 frame_hdr_cache_head->load_base = load_base;
304 frame_hdr_cache_head->p_eh_frame_hdr = p_eh_frame_hdr;
305 frame_hdr_cache_head->p_dynamic = p_dynamic;
306 frame_hdr_cache_head->pc_low = pc_low;
307 frame_hdr_cache_head->pc_high = pc_high;
310 found:
312 if (!p_eh_frame_hdr)
313 return 0;
315 /* Read .eh_frame_hdr header. */
316 hdr = (const struct unw_eh_frame_hdr *)
317 __RELOC_POINTER (p_eh_frame_hdr->p_vaddr, load_base);
318 if (hdr->version != 1)
319 return 1;
321 #ifdef CRT_GET_RFIB_DATA
322 # ifdef __i386__
323 data->dbase = NULL;
324 if (p_dynamic)
326 /* For dynamically linked executables and shared libraries,
327 DT_PLTGOT is the gp value for that object. */
328 ElfW(Dyn) *dyn = (ElfW(Dyn) *)
329 __RELOC_POINTER (p_dynamic->p_vaddr, load_base);
330 for (; dyn->d_tag != DT_NULL ; dyn++)
331 if (dyn->d_tag == DT_PLTGOT)
333 data->dbase = (void *) dyn->d_un.d_ptr;
334 #if defined __linux__
335 /* On IA-32 Linux, _DYNAMIC is writable and GLIBC has
336 relocated it. */
337 #elif defined __sun__ && defined __svr4__
338 /* On Solaris 2/x86, we need to do this ourselves. */
339 data->dbase += load_base;
340 #endif
341 break;
344 # elif defined __FRV_FDPIC__ && defined __linux__
345 data->dbase = load_base.got_value;
346 # elif defined __x86_64__ && defined __sun__ && defined __svr4__
347 /* While CRT_GET_RFIB_DATA is also defined for 64-bit Solaris 10+/x86, it
348 doesn't apply since it uses DW_EH_PE_pcrel encoding. */
349 # else
350 # error What is DW_EH_PE_datarel base on this platform?
351 # endif
352 #endif
354 p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
355 base_from_cb_data (hdr->eh_frame_ptr_enc,
356 data),
357 (const unsigned char *) (hdr + 1),
358 &eh_frame);
360 /* We require here specific table encoding to speed things up.
361 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
362 as base, not the processor specific DW_EH_PE_datarel. */
363 if (hdr->fde_count_enc != DW_EH_PE_omit
364 && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
366 _Unwind_Ptr fde_count;
368 p = read_encoded_value_with_base (hdr->fde_count_enc,
369 base_from_cb_data (hdr->fde_count_enc,
370 data),
371 p, &fde_count);
372 /* Shouldn't happen. */
373 if (fde_count == 0)
374 return 1;
375 if ((((_Unwind_Ptr) p) & 3) == 0)
377 struct fde_table {
378 signed initial_loc __attribute__ ((mode (SI)));
379 signed fde __attribute__ ((mode (SI)));
381 const struct fde_table *table = (const struct fde_table *) p;
382 size_t lo, hi, mid;
383 _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
384 fde *f;
385 unsigned int f_enc, f_enc_size;
386 _Unwind_Ptr range;
388 mid = fde_count - 1;
389 if (data->pc < table[0].initial_loc + data_base)
390 return 1;
391 else if (data->pc < table[mid].initial_loc + data_base)
393 lo = 0;
394 hi = mid;
396 while (lo < hi)
398 mid = (lo + hi) / 2;
399 if (data->pc < table[mid].initial_loc + data_base)
400 hi = mid;
401 else if (data->pc >= table[mid + 1].initial_loc + data_base)
402 lo = mid + 1;
403 else
404 break;
407 gcc_assert (lo < hi);
410 f = (fde *) (table[mid].fde + data_base);
411 f_enc = get_fde_encoding (f);
412 f_enc_size = size_of_encoded_value (f_enc);
413 read_encoded_value_with_base (f_enc & 0x0f, 0,
414 &f->pc_begin[f_enc_size], &range);
415 if (data->pc < table[mid].initial_loc + data_base + range)
416 data->ret = f;
417 data->func = (void *) (table[mid].initial_loc + data_base);
418 return 1;
422 /* We have no sorted search table, so need to go the slow way.
423 As soon as GLIBC will provide API so to notify that a library has been
424 removed, we could cache this (and thus use search_object). */
425 ob.pc_begin = NULL;
426 ob.tbase = data->tbase;
427 ob.dbase = data->dbase;
428 ob.u.single = (fde *) eh_frame;
429 ob.s.i = 0;
430 ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */
431 data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
432 if (data->ret != NULL)
434 _Unwind_Ptr func;
435 unsigned int encoding = get_fde_encoding (data->ret);
437 read_encoded_value_with_base (encoding,
438 base_from_cb_data (encoding, data),
439 data->ret->pc_begin, &func);
440 data->func = (void *) func;
442 return 1;
445 const fde *
446 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
448 struct unw_eh_callback_data data;
449 const fde *ret;
451 ret = _Unwind_Find_registered_FDE (pc, bases);
452 if (ret != NULL)
453 return ret;
455 data.pc = (_Unwind_Ptr) pc;
456 data.tbase = NULL;
457 data.dbase = NULL;
458 data.func = NULL;
459 data.ret = NULL;
460 data.check_cache = 1;
462 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
463 return NULL;
465 if (data.ret)
467 bases->tbase = data.tbase;
468 bases->dbase = data.dbase;
469 bases->func = data.func;
471 return data.ret;
474 #else
475 /* Prevent multiple include of header files. */
476 #define _Unwind_Find_FDE _Unwind_Find_FDE
477 #include "unwind-dw2-fde.c"
478 #endif
480 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
481 alias (_Unwind_Find_FDE);
482 #endif