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