OSDN Git Service

PR middle-end/46844
[pf3gnuchains/gcc-fork.git] / gcc / unwind-dw2-fde-glibc.c
1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
2    Contributed by Jakub Jelinek <jakub@redhat.com>.
3
4    This file is part of GCC.
5
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.
10
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.
15
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.
19
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/>.  */
24
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.  */
28
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE 1
31 #endif
32
33 #include "tconfig.h"
34 #include "tsystem.h"
35 #ifndef inhibit_libc
36 #include <elf.h>                /* Get DT_CONFIG.  */
37 #endif
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "dwarf2.h"
41 #include "unwind.h"
42 #define NO_BASE_OF_ENCODED_VALUE
43 #include "unwind-pe.h"
44 #include "unwind-dw2-fde.h"
45 #include "unwind-compat.h"
46 #include "gthr.h"
47
48 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
49     && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
50         || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
51 # define USE_PT_GNU_EH_FRAME
52 #endif
53
54 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
55     && defined(__FreeBSD__) && __FreeBSD__ >= 7
56 # define ElfW __ElfN
57 # define USE_PT_GNU_EH_FRAME
58 #endif
59
60 #if defined(USE_PT_GNU_EH_FRAME)
61
62 #include <link.h>
63
64 #ifndef __RELOC_POINTER
65 # define __RELOC_POINTER(ptr, base) ((ptr) + (base))
66 #endif
67
68 static const fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases);
69
70 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
71 #include "unwind-dw2-fde.c"
72 #undef _Unwind_Find_FDE
73
74 #ifndef PT_GNU_EH_FRAME
75 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
76 #endif
77
78 struct unw_eh_callback_data
79 {
80   _Unwind_Ptr pc;
81   void *tbase;
82   void *dbase;
83   void *func;
84   const fde *ret;
85   int check_cache;
86 };
87
88 struct unw_eh_frame_hdr
89 {
90   unsigned char version;
91   unsigned char eh_frame_ptr_enc;
92   unsigned char fde_count_enc;
93   unsigned char table_enc;
94 };
95
96 #define FRAME_HDR_CACHE_SIZE 8
97
98 static struct frame_hdr_cache_element
99 {
100   _Unwind_Ptr pc_low;
101   _Unwind_Ptr pc_high;
102   _Unwind_Ptr load_base;
103   const ElfW(Phdr) *p_eh_frame_hdr;
104   const ElfW(Phdr) *p_dynamic;
105   struct frame_hdr_cache_element *link;
106 } frame_hdr_cache[FRAME_HDR_CACHE_SIZE];
107
108 static struct frame_hdr_cache_element *frame_hdr_cache_head;
109
110 /* Like base_of_encoded_value, but take the base from a struct
111    unw_eh_callback_data instead of an _Unwind_Context.  */
112
113 static _Unwind_Ptr
114 base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
115 {
116   if (encoding == DW_EH_PE_omit)
117     return 0;
118
119   switch (encoding & 0x70)
120     {
121     case DW_EH_PE_absptr:
122     case DW_EH_PE_pcrel:
123     case DW_EH_PE_aligned:
124       return 0;
125
126     case DW_EH_PE_textrel:
127       return (_Unwind_Ptr) data->tbase;
128     case DW_EH_PE_datarel:
129       return (_Unwind_Ptr) data->dbase;
130     default:
131       gcc_unreachable ();
132     }
133 }
134
135 static int
136 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
137 {
138   struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
139   const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
140   long n, match;
141 #ifdef __FRV_FDPIC__
142   struct elf32_fdpic_loadaddr load_base;
143 #else
144   _Unwind_Ptr load_base;
145 #endif
146   const unsigned char *p;
147   const struct unw_eh_frame_hdr *hdr;
148   _Unwind_Ptr eh_frame;
149   struct object ob;
150   _Unwind_Ptr pc_low = 0, pc_high = 0;
151
152   struct ext_dl_phdr_info
153     {
154       ElfW(Addr) dlpi_addr;
155       const char *dlpi_name;
156       const ElfW(Phdr) *dlpi_phdr;
157       ElfW(Half) dlpi_phnum;
158       unsigned long long int dlpi_adds;
159       unsigned long long int dlpi_subs;
160     };
161
162   match = 0;
163   phdr = info->dlpi_phdr;
164   load_base = info->dlpi_addr;
165   p_eh_frame_hdr = NULL;
166   p_dynamic = NULL;
167
168   struct frame_hdr_cache_element *prev_cache_entry = NULL,
169     *last_cache_entry = NULL;
170
171   if (data->check_cache && size >= sizeof (struct ext_dl_phdr_info))
172     {
173       static unsigned long long adds = -1ULL, subs;
174       struct ext_dl_phdr_info *einfo = (struct ext_dl_phdr_info *) info;
175
176       /* We use a least recently used cache replacement policy.  Also,
177          the most recently used cache entries are placed at the head
178          of the search chain.  */
179
180       if (einfo->dlpi_adds == adds && einfo->dlpi_subs == subs)
181         {
182           /* Find data->pc in shared library cache.
183              Set load_base, p_eh_frame_hdr and p_dynamic
184              plus match from the cache and goto
185              "Read .eh_frame_hdr header." below.  */
186
187           struct frame_hdr_cache_element *cache_entry;
188
189           for (cache_entry = frame_hdr_cache_head;
190                cache_entry;
191                cache_entry = cache_entry->link)
192             {
193               if (data->pc >= cache_entry->pc_low
194                   && data->pc < cache_entry->pc_high)
195                 {
196                   load_base = cache_entry->load_base;
197                   p_eh_frame_hdr = cache_entry->p_eh_frame_hdr;
198                   p_dynamic = cache_entry->p_dynamic;
199
200                   /* And move the entry we're using to the head.  */
201                   if (cache_entry != frame_hdr_cache_head)
202                     {
203                       prev_cache_entry->link = cache_entry->link;
204                       cache_entry->link = frame_hdr_cache_head;
205                       frame_hdr_cache_head = cache_entry;
206                     }
207                   goto found;
208                 }
209
210               last_cache_entry = cache_entry;
211               /* Exit early if we found an unused entry.  */
212               if ((cache_entry->pc_low | cache_entry->pc_high) == 0)
213                 break;
214               if (cache_entry->link != NULL)
215                 prev_cache_entry = cache_entry;
216             }
217         }
218       else
219         {
220           adds = einfo->dlpi_adds;
221           subs = einfo->dlpi_subs;
222           /* Initialize the cache.  Create a chain of cache entries,
223              with the final one terminated by a NULL link.  */
224           int i;
225           for (i = 0; i < FRAME_HDR_CACHE_SIZE; i++)
226             {
227               frame_hdr_cache[i].pc_low = 0;
228               frame_hdr_cache[i].pc_high = 0;
229               frame_hdr_cache[i].link = &frame_hdr_cache[i+1];
230             }
231           frame_hdr_cache[i-1].link = NULL;
232           frame_hdr_cache_head = &frame_hdr_cache[0];
233           data->check_cache = 0;
234         }
235     }
236
237   /* Make sure struct dl_phdr_info is at least as big as we need.  */
238   if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
239              + sizeof (info->dlpi_phnum))
240     return -1;
241
242   /* See if PC falls into one of the loaded segments.  Find the eh_frame
243      segment at the same time.  */
244   for (n = info->dlpi_phnum; --n >= 0; phdr++)
245     {
246       if (phdr->p_type == PT_LOAD)
247         {
248           _Unwind_Ptr vaddr = (_Unwind_Ptr)
249             __RELOC_POINTER (phdr->p_vaddr, load_base);
250           if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
251             {
252               match = 1;
253               pc_low = vaddr;
254               pc_high =  vaddr + phdr->p_memsz;
255             }
256         }
257       else if (phdr->p_type == PT_GNU_EH_FRAME)
258         p_eh_frame_hdr = phdr;
259       else if (phdr->p_type == PT_DYNAMIC)
260         p_dynamic = phdr;
261     }
262
263   if (!match)
264     return 0;
265
266   if (size >= sizeof (struct ext_dl_phdr_info))
267     {
268       /* Move the cache entry we're about to overwrite to the head of
269          the list.  If either last_cache_entry or prev_cache_entry are
270          NULL, that cache entry is already at the head.  */
271       if (last_cache_entry != NULL && prev_cache_entry != NULL)
272         {
273           prev_cache_entry->link = last_cache_entry->link;
274           last_cache_entry->link = frame_hdr_cache_head;
275           frame_hdr_cache_head = last_cache_entry;
276         }
277
278       frame_hdr_cache_head->load_base = load_base;
279       frame_hdr_cache_head->p_eh_frame_hdr = p_eh_frame_hdr;
280       frame_hdr_cache_head->p_dynamic = p_dynamic;
281       frame_hdr_cache_head->pc_low = pc_low;
282       frame_hdr_cache_head->pc_high = pc_high;
283     }
284
285  found:
286
287   if (!p_eh_frame_hdr)
288     return 0;
289
290   /* Read .eh_frame_hdr header.  */
291   hdr = (const struct unw_eh_frame_hdr *)
292     __RELOC_POINTER (p_eh_frame_hdr->p_vaddr, load_base);
293   if (hdr->version != 1)
294     return 1;
295
296 #ifdef CRT_GET_RFIB_DATA
297 # ifdef __i386__
298   data->dbase = NULL;
299   if (p_dynamic)
300     {
301       /* For dynamically linked executables and shared libraries,
302          DT_PLTGOT is the gp value for that object.  */
303       ElfW(Dyn) *dyn = (ElfW(Dyn) *)
304         __RELOC_POINTER (p_dynamic->p_vaddr, load_base);
305       for (; dyn->d_tag != DT_NULL ; dyn++)
306         if (dyn->d_tag == DT_PLTGOT)
307           {
308             /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it.  */
309             data->dbase = (void *) dyn->d_un.d_ptr;
310             break;
311           }
312     }
313 # elif defined __FRV_FDPIC__ && defined __linux__
314   data->dbase = load_base.got_value;
315 # else
316 #  error What is DW_EH_PE_datarel base on this platform?
317 # endif
318 #endif
319
320   p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
321                                     base_from_cb_data (hdr->eh_frame_ptr_enc,
322                                                        data),
323                                     (const unsigned char *) (hdr + 1),
324                                     &eh_frame);
325
326   /* We require here specific table encoding to speed things up.
327      Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
328      as base, not the processor specific DW_EH_PE_datarel.  */
329   if (hdr->fde_count_enc != DW_EH_PE_omit
330       && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
331     {
332       _Unwind_Ptr fde_count;
333
334       p = read_encoded_value_with_base (hdr->fde_count_enc,
335                                         base_from_cb_data (hdr->fde_count_enc,
336                                                            data),
337                                         p, &fde_count);
338       /* Shouldn't happen.  */
339       if (fde_count == 0)
340         return 1;
341       if ((((_Unwind_Ptr) p) & 3) == 0)
342         {
343           struct fde_table {
344             signed initial_loc __attribute__ ((mode (SI)));
345             signed fde __attribute__ ((mode (SI)));
346           };
347           const struct fde_table *table = (const struct fde_table *) p;
348           size_t lo, hi, mid;
349           _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
350           fde *f;
351           unsigned int f_enc, f_enc_size;
352           _Unwind_Ptr range;
353
354           mid = fde_count - 1;
355           if (data->pc < table[0].initial_loc + data_base)
356             return 1;
357           else if (data->pc < table[mid].initial_loc + data_base)
358             {
359               lo = 0;
360               hi = mid;
361
362               while (lo < hi)
363                 {
364                   mid = (lo + hi) / 2;
365                   if (data->pc < table[mid].initial_loc + data_base)
366                     hi = mid;
367                   else if (data->pc >= table[mid + 1].initial_loc + data_base)
368                     lo = mid + 1;
369                   else
370                     break;
371                 }
372
373               gcc_assert (lo < hi);
374             }
375
376           f = (fde *) (table[mid].fde + data_base);
377           f_enc = get_fde_encoding (f);
378           f_enc_size = size_of_encoded_value (f_enc);
379           read_encoded_value_with_base (f_enc & 0x0f, 0,
380                                         &f->pc_begin[f_enc_size], &range);
381           if (data->pc < table[mid].initial_loc + data_base + range)
382             data->ret = f;
383           data->func = (void *) (table[mid].initial_loc + data_base);
384           return 1;
385         }
386     }
387
388   /* We have no sorted search table, so need to go the slow way.
389      As soon as GLIBC will provide API so to notify that a library has been
390      removed, we could cache this (and thus use search_object).  */
391   ob.pc_begin = NULL;
392   ob.tbase = data->tbase;
393   ob.dbase = data->dbase;
394   ob.u.single = (fde *) eh_frame;
395   ob.s.i = 0;
396   ob.s.b.mixed_encoding = 1;  /* Need to assume worst case.  */
397   data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
398   if (data->ret != NULL)
399     {
400       _Unwind_Ptr func;
401       unsigned int encoding = get_fde_encoding (data->ret);
402
403       read_encoded_value_with_base (encoding,
404                                     base_from_cb_data (encoding, data),
405                                     data->ret->pc_begin, &func);
406       data->func = (void *) func;
407     }
408   return 1;
409 }
410
411 const fde *
412 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
413 {
414   struct unw_eh_callback_data data;
415   const fde *ret;
416
417   ret = _Unwind_Find_registered_FDE (pc, bases);
418   if (ret != NULL)
419     return ret;
420
421   data.pc = (_Unwind_Ptr) pc;
422   data.tbase = NULL;
423   data.dbase = NULL;
424   data.func = NULL;
425   data.ret = NULL;
426   data.check_cache = 1;
427
428   if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
429     return NULL;
430
431   if (data.ret)
432     {
433       bases->tbase = data.tbase;
434       bases->dbase = data.dbase;
435       bases->func = data.func;
436     }
437   return data.ret;
438 }
439
440 #else
441 /* Prevent multiple include of header files.  */
442 #define _Unwind_Find_FDE _Unwind_Find_FDE
443 #include "unwind-dw2-fde.c"
444 #endif
445
446 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
447 alias (_Unwind_Find_FDE);
448 #endif