2005-03-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / unwind-dw2-fde-glibc.c
blob7dd9a72d4626bfc14fe0b6ed7f39b8d7b2dad926
1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005 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 2, 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 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
28 /* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF
29 segment and dl_iterate_phdr to avoid register/deregister calls at
30 DSO load/unload. */
32 #ifndef _GNU_SOURCE
33 #define _GNU_SOURCE 1
34 #endif
36 #include "auto-host.h" /* For HAVE_LD_EH_FRAME_HDR. */
37 #include "tconfig.h"
38 #include "tsystem.h"
39 #ifndef inhibit_libc
40 #include <link.h>
41 #endif
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "dwarf2.h"
45 #include "unwind.h"
46 #define NO_BASE_OF_ENCODED_VALUE
47 #include "unwind-pe.h"
48 #include "unwind-dw2-fde.h"
49 #include "unwind-compat.h"
50 #include "gthr.h"
52 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
53 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
54 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
56 #ifndef __RELOC_POINTER
57 # define __RELOC_POINTER(ptr, base) ((ptr) + (base))
58 #endif
60 static const fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases);
62 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
63 #include "unwind-dw2-fde.c"
64 #undef _Unwind_Find_FDE
66 #ifndef PT_GNU_EH_FRAME
67 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
68 #endif
70 struct unw_eh_callback_data
72 _Unwind_Ptr pc;
73 void *tbase;
74 void *dbase;
75 void *func;
76 const fde *ret;
77 int check_cache;
80 struct unw_eh_frame_hdr
82 unsigned char version;
83 unsigned char eh_frame_ptr_enc;
84 unsigned char fde_count_enc;
85 unsigned char table_enc;
88 #define FRAME_HDR_CACHE_SIZE 8
90 static struct frame_hdr_cache_element
92 _Unwind_Ptr pc_low;
93 _Unwind_Ptr pc_high;
94 _Unwind_Ptr load_base;
95 const ElfW(Phdr) *p_eh_frame_hdr;
96 const ElfW(Phdr) *p_dynamic;
97 struct frame_hdr_cache_element *link;
98 } frame_hdr_cache[FRAME_HDR_CACHE_SIZE];
100 static struct frame_hdr_cache_element *frame_hdr_cache_head;
102 /* Like base_of_encoded_value, but take the base from a struct
103 unw_eh_callback_data instead of an _Unwind_Context. */
105 static _Unwind_Ptr
106 base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
108 if (encoding == DW_EH_PE_omit)
109 return 0;
111 switch (encoding & 0x70)
113 case DW_EH_PE_absptr:
114 case DW_EH_PE_pcrel:
115 case DW_EH_PE_aligned:
116 return 0;
118 case DW_EH_PE_textrel:
119 return (_Unwind_Ptr) data->tbase;
120 case DW_EH_PE_datarel:
121 return (_Unwind_Ptr) data->dbase;
123 abort ();
126 static int
127 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
129 struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
130 const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
131 long n, match;
132 #ifdef __FRV_FDPIC__
133 struct elf32_fdpic_loadaddr load_base;
134 #else
135 _Unwind_Ptr load_base;
136 #endif
137 const unsigned char *p;
138 const struct unw_eh_frame_hdr *hdr;
139 _Unwind_Ptr eh_frame;
140 struct object ob;
142 struct ext_dl_phdr_info
144 ElfW(Addr) dlpi_addr;
145 const char *dlpi_name;
146 const ElfW(Phdr) *dlpi_phdr;
147 ElfW(Half) dlpi_phnum;
148 unsigned long long int dlpi_adds;
149 unsigned long long int dlpi_subs;
152 match = 0;
153 phdr = info->dlpi_phdr;
154 load_base = info->dlpi_addr;
155 p_eh_frame_hdr = NULL;
156 p_dynamic = NULL;
158 struct frame_hdr_cache_element *prev_cache_entry = NULL,
159 *last_cache_entry = NULL;
161 if (data->check_cache && size >= sizeof (struct ext_dl_phdr_info))
163 static unsigned long long adds = -1ULL, subs;
164 struct ext_dl_phdr_info *einfo = (struct ext_dl_phdr_info *) info;
166 /* We use a least recently used cache replacement policy. Also,
167 the most recently used cache entries are placed at the head
168 of the search chain. */
170 if (einfo->dlpi_adds == adds && einfo->dlpi_subs == subs)
172 /* Find data->pc in shared library cache.
173 Set load_base, p_eh_frame_hdr and p_dynamic
174 plus match from the cache and goto
175 "Read .eh_frame_hdr header." below. */
177 struct frame_hdr_cache_element *cache_entry;
179 for (cache_entry = frame_hdr_cache_head;
180 cache_entry;
181 cache_entry = cache_entry->link)
183 if (data->pc >= cache_entry->pc_low
184 && data->pc < cache_entry->pc_high)
186 load_base = cache_entry->load_base;
187 p_eh_frame_hdr = cache_entry->p_eh_frame_hdr;
188 p_dynamic = cache_entry->p_dynamic;
190 /* And move the entry we're using to the head. */
191 if (cache_entry != frame_hdr_cache_head)
193 prev_cache_entry->link = cache_entry->link;
194 cache_entry->link = frame_hdr_cache_head;
195 frame_hdr_cache_head = cache_entry;
197 goto found;
200 last_cache_entry = cache_entry;
201 /* Exit early if we found an unused entry. */
202 if ((cache_entry->pc_low | cache_entry->pc_high) == 0)
203 break;
204 if (cache_entry->link != NULL)
205 prev_cache_entry = cache_entry;
208 else
210 adds = einfo->dlpi_adds;
211 subs = einfo->dlpi_subs;
212 /* Initialize the cache. Create a chain of cache entries,
213 with the final one terminated by a NULL link. */
214 int i;
215 for (i = 0; i < FRAME_HDR_CACHE_SIZE; i++)
217 frame_hdr_cache[i].pc_low = 0;
218 frame_hdr_cache[i].pc_high = 0;
219 frame_hdr_cache[i].link = &frame_hdr_cache[i+1];
221 frame_hdr_cache[i-1].link = NULL;
222 frame_hdr_cache_head = &frame_hdr_cache[0];
223 data->check_cache = 0;
227 /* Make sure struct dl_phdr_info is at least as big as we need. */
228 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
229 + sizeof (info->dlpi_phnum))
230 return -1;
232 _Unwind_Ptr pc_low = 0, pc_high = 0;
234 /* See if PC falls into one of the loaded segments. Find the eh_frame
235 segment at the same time. */
236 for (n = info->dlpi_phnum; --n >= 0; phdr++)
238 if (phdr->p_type == PT_LOAD)
240 _Unwind_Ptr vaddr = (_Unwind_Ptr)
241 __RELOC_POINTER (phdr->p_vaddr, load_base);
242 if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
244 match = 1;
245 pc_low = vaddr;
246 pc_high = vaddr + phdr->p_memsz;
249 else if (phdr->p_type == PT_GNU_EH_FRAME)
250 p_eh_frame_hdr = phdr;
251 else if (phdr->p_type == PT_DYNAMIC)
252 p_dynamic = phdr;
255 if (!match)
256 return 0;
258 if (size >= sizeof (struct ext_dl_phdr_info))
260 if (last_cache_entry != NULL)
262 prev_cache_entry->link = last_cache_entry->link;
263 last_cache_entry->link = frame_hdr_cache_head;
264 frame_hdr_cache_head = last_cache_entry;
267 frame_hdr_cache_head->load_base = load_base;
268 frame_hdr_cache_head->p_eh_frame_hdr = p_eh_frame_hdr;
269 frame_hdr_cache_head->p_dynamic = p_dynamic;
270 frame_hdr_cache_head->pc_low = pc_low;
271 frame_hdr_cache_head->pc_high = pc_high;
274 found:
276 if (!p_eh_frame_hdr)
277 return 0;
279 /* Read .eh_frame_hdr header. */
280 hdr = (const struct unw_eh_frame_hdr *)
281 __RELOC_POINTER (p_eh_frame_hdr->p_vaddr, load_base);
282 if (hdr->version != 1)
283 return 1;
285 #ifdef CRT_GET_RFIB_DATA
286 # ifdef __i386__
287 data->dbase = NULL;
288 if (p_dynamic)
290 /* For dynamically linked executables and shared libraries,
291 DT_PLTGOT is the gp value for that object. */
292 ElfW(Dyn) *dyn = (ElfW(Dyn) *)
293 __RELOC_POINTER (p_dynamic->p_vaddr, load_base);
294 for (; dyn->d_tag != DT_NULL ; dyn++)
295 if (dyn->d_tag == DT_PLTGOT)
297 /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */
298 data->dbase = (void *) dyn->d_un.d_ptr;
299 break;
302 # elif defined __FRV_FDPIC__ && defined __linux__
303 data->dbase = load_base.got_value;
304 # else
305 # error What is DW_EH_PE_datarel base on this platform?
306 # endif
307 #endif
309 p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
310 base_from_cb_data (hdr->eh_frame_ptr_enc,
311 data),
312 (const unsigned char *) (hdr + 1),
313 &eh_frame);
315 /* We require here specific table encoding to speed things up.
316 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
317 as base, not the processor specific DW_EH_PE_datarel. */
318 if (hdr->fde_count_enc != DW_EH_PE_omit
319 && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
321 _Unwind_Ptr fde_count;
323 p = read_encoded_value_with_base (hdr->fde_count_enc,
324 base_from_cb_data (hdr->fde_count_enc,
325 data),
326 p, &fde_count);
327 /* Shouldn't happen. */
328 if (fde_count == 0)
329 return 1;
330 if ((((_Unwind_Ptr) p) & 3) == 0)
332 struct fde_table {
333 signed initial_loc __attribute__ ((mode (SI)));
334 signed fde __attribute__ ((mode (SI)));
336 const struct fde_table *table = (const struct fde_table *) p;
337 size_t lo, hi, mid;
338 _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
339 fde *f;
340 unsigned int f_enc, f_enc_size;
341 _Unwind_Ptr range;
343 mid = fde_count - 1;
344 if (data->pc < table[0].initial_loc + data_base)
345 return 1;
346 else if (data->pc < table[mid].initial_loc + data_base)
348 lo = 0;
349 hi = mid;
351 while (lo < hi)
353 mid = (lo + hi) / 2;
354 if (data->pc < table[mid].initial_loc + data_base)
355 hi = mid;
356 else if (data->pc >= table[mid + 1].initial_loc + data_base)
357 lo = mid + 1;
358 else
359 break;
362 if (lo >= hi)
363 __gxx_abort ();
366 f = (fde *) (table[mid].fde + data_base);
367 f_enc = get_fde_encoding (f);
368 f_enc_size = size_of_encoded_value (f_enc);
369 read_encoded_value_with_base (f_enc & 0x0f, 0,
370 &f->pc_begin[f_enc_size], &range);
371 if (data->pc < table[mid].initial_loc + data_base + range)
372 data->ret = f;
373 data->func = (void *) (table[mid].initial_loc + data_base);
374 return 1;
378 /* We have no sorted search table, so need to go the slow way.
379 As soon as GLIBC will provide API so to notify that a library has been
380 removed, we could cache this (and thus use search_object). */
381 ob.pc_begin = NULL;
382 ob.tbase = data->tbase;
383 ob.dbase = data->dbase;
384 ob.u.single = (fde *) eh_frame;
385 ob.s.i = 0;
386 ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */
387 data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
388 if (data->ret != NULL)
390 unsigned int encoding = get_fde_encoding (data->ret);
391 read_encoded_value_with_base (encoding,
392 base_from_cb_data (encoding, data),
393 data->ret->pc_begin,
394 (_Unwind_Ptr *)&data->func);
396 return 1;
399 const fde *
400 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
402 struct unw_eh_callback_data data;
403 const fde *ret;
405 ret = _Unwind_Find_registered_FDE (pc, bases);
406 if (ret != NULL)
407 return ret;
409 data.pc = (_Unwind_Ptr) pc;
410 data.tbase = NULL;
411 data.dbase = NULL;
412 data.func = NULL;
413 data.ret = NULL;
414 data.check_cache = 1;
416 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
417 return NULL;
419 if (data.ret)
421 bases->tbase = data.tbase;
422 bases->dbase = data.dbase;
423 bases->func = data.func;
425 return data.ret;
428 #else
429 /* Prevent multiple include of header files. */
430 #define _Unwind_Find_FDE _Unwind_Find_FDE
431 #include "unwind-dw2-fde.c"
432 #endif
434 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
435 alias (_Unwind_Find_FDE);
436 #endif