PR target/16286
[official-gcc.git] / gcc / unwind-dw2-fde-glibc.c
blob96d7946291a074fe0b6b39eaf3f56d2da6e10eec
1 /* Copyright (C) 2001, 2002, 2003, 2004 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;
79 struct unw_eh_frame_hdr
81 unsigned char version;
82 unsigned char eh_frame_ptr_enc;
83 unsigned char fde_count_enc;
84 unsigned char table_enc;
87 /* Like base_of_encoded_value, but take the base from a struct
88 unw_eh_callback_data instead of an _Unwind_Context. */
90 static _Unwind_Ptr
91 base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
93 if (encoding == DW_EH_PE_omit)
94 return 0;
96 switch (encoding & 0x70)
98 case DW_EH_PE_absptr:
99 case DW_EH_PE_pcrel:
100 case DW_EH_PE_aligned:
101 return 0;
103 case DW_EH_PE_textrel:
104 return (_Unwind_Ptr) data->tbase;
105 case DW_EH_PE_datarel:
106 return (_Unwind_Ptr) data->dbase;
108 abort ();
111 static int
112 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
114 struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
115 const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
116 long n, match;
117 #ifdef __FRV_FDPIC__
118 struct elf32_fdpic_loadaddr load_base;
119 #else
120 _Unwind_Ptr load_base;
121 #endif
122 const unsigned char *p;
123 const struct unw_eh_frame_hdr *hdr;
124 _Unwind_Ptr eh_frame;
125 struct object ob;
127 /* Make sure struct dl_phdr_info is at least as big as we need. */
128 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
129 + sizeof (info->dlpi_phnum))
130 return -1;
132 match = 0;
133 phdr = info->dlpi_phdr;
134 load_base = info->dlpi_addr;
135 p_eh_frame_hdr = NULL;
136 p_dynamic = NULL;
138 /* See if PC falls into one of the loaded segments. Find the eh_frame
139 segment at the same time. */
140 for (n = info->dlpi_phnum; --n >= 0; phdr++)
142 if (phdr->p_type == PT_LOAD)
144 _Unwind_Ptr vaddr = (_Unwind_Ptr)
145 __RELOC_POINTER (phdr->p_vaddr, load_base);
146 if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
147 match = 1;
149 else if (phdr->p_type == PT_GNU_EH_FRAME)
150 p_eh_frame_hdr = phdr;
151 else if (phdr->p_type == PT_DYNAMIC)
152 p_dynamic = phdr;
154 if (!match || !p_eh_frame_hdr)
155 return 0;
157 /* Read .eh_frame_hdr header. */
158 hdr = (const struct unw_eh_frame_hdr *)
159 __RELOC_POINTER (p_eh_frame_hdr->p_vaddr, load_base);
160 if (hdr->version != 1)
161 return 1;
163 #ifdef CRT_GET_RFIB_DATA
164 # ifdef __i386__
165 data->dbase = NULL;
166 if (p_dynamic)
168 /* For dynamically linked executables and shared libraries,
169 DT_PLTGOT is the gp value for that object. */
170 ElfW(Dyn) *dyn = (ElfW(Dyn) *)
171 __RELOC_POINTER (p_dynamic->p_vaddr, load_base);
172 for (; dyn->d_tag != DT_NULL ; dyn++)
173 if (dyn->d_tag == DT_PLTGOT)
175 /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */
176 data->dbase = (void *) dyn->d_un.d_ptr;
177 break;
180 # elif defined __FRV_FDPIC__ && defined __linux__
181 data->dbase = load_base.got_value;
182 # else
183 # error What is DW_EH_PE_datarel base on this platform?
184 # endif
185 #endif
187 p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
188 base_from_cb_data (hdr->eh_frame_ptr_enc,
189 data),
190 (const unsigned char *) (hdr + 1),
191 &eh_frame);
193 /* We require here specific table encoding to speed things up.
194 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
195 as base, not the processor specific DW_EH_PE_datarel. */
196 if (hdr->fde_count_enc != DW_EH_PE_omit
197 && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
199 _Unwind_Ptr fde_count;
201 p = read_encoded_value_with_base (hdr->fde_count_enc,
202 base_from_cb_data (hdr->fde_count_enc,
203 data),
204 p, &fde_count);
205 /* Shouldn't happen. */
206 if (fde_count == 0)
207 return 1;
208 if ((((_Unwind_Ptr) p) & 3) == 0)
210 struct fde_table {
211 signed initial_loc __attribute__ ((mode (SI)));
212 signed fde __attribute__ ((mode (SI)));
214 const struct fde_table *table = (const struct fde_table *) p;
215 size_t lo, hi, mid;
216 _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
217 fde *f;
218 unsigned int f_enc, f_enc_size;
219 _Unwind_Ptr range;
221 mid = fde_count - 1;
222 if (data->pc < table[0].initial_loc + data_base)
223 return 1;
224 else if (data->pc < table[mid].initial_loc + data_base)
226 lo = 0;
227 hi = mid;
229 while (lo < hi)
231 mid = (lo + hi) / 2;
232 if (data->pc < table[mid].initial_loc + data_base)
233 hi = mid;
234 else if (data->pc >= table[mid + 1].initial_loc + data_base)
235 lo = mid + 1;
236 else
237 break;
240 if (lo >= hi)
241 __gxx_abort ();
244 f = (fde *) (table[mid].fde + data_base);
245 f_enc = get_fde_encoding (f);
246 f_enc_size = size_of_encoded_value (f_enc);
247 read_encoded_value_with_base (f_enc & 0x0f, 0,
248 &f->pc_begin[f_enc_size], &range);
249 if (data->pc < table[mid].initial_loc + data_base + range)
250 data->ret = f;
251 data->func = (void *) (table[mid].initial_loc + data_base);
252 return 1;
256 /* We have no sorted search table, so need to go the slow way.
257 As soon as GLIBC will provide API so to notify that a library has been
258 removed, we could cache this (and thus use search_object). */
259 ob.pc_begin = NULL;
260 ob.tbase = data->tbase;
261 ob.dbase = data->dbase;
262 ob.u.single = (fde *) eh_frame;
263 ob.s.i = 0;
264 ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */
265 data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
266 if (data->ret != NULL)
268 unsigned int encoding = get_fde_encoding (data->ret);
269 read_encoded_value_with_base (encoding,
270 base_from_cb_data (encoding, data),
271 data->ret->pc_begin,
272 (_Unwind_Ptr *)&data->func);
274 return 1;
277 const fde *
278 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
280 struct unw_eh_callback_data data;
281 const fde *ret;
283 ret = _Unwind_Find_registered_FDE (pc, bases);
284 if (ret != NULL)
285 return ret;
287 data.pc = (_Unwind_Ptr) pc;
288 data.tbase = NULL;
289 data.dbase = NULL;
290 data.func = NULL;
291 data.ret = NULL;
293 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
294 return NULL;
296 if (data.ret)
298 bases->tbase = data.tbase;
299 bases->dbase = data.dbase;
300 bases->func = data.func;
302 return data.ret;
305 #else
306 /* Prevent multiple include of header files. */
307 #define _Unwind_Find_FDE _Unwind_Find_FDE
308 #include "unwind-dw2-fde.c"
309 #endif
311 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
312 alias (_Unwind_Find_FDE);
313 #endif