1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009 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)
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
38 #include "coretypes.h"
42 #define NO_BASE_OF_ENCODED_VALUE
43 #include "unwind-pe.h"
44 #include "unwind-dw2-fde.h"
45 #include "unwind-compat.h"
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
54 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
55 && defined(__FreeBSD__) && __FreeBSD__ >= 7
57 # define USE_PT_GNU_EH_FRAME
60 #if defined(USE_PT_GNU_EH_FRAME)
62 #ifndef __RELOC_POINTER
63 # define __RELOC_POINTER(ptr, base) ((ptr) + (base))
66 static const fde
* _Unwind_Find_registered_FDE (void *pc
, struct dwarf_eh_bases
*bases
);
68 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
69 #include "unwind-dw2-fde.c"
70 #undef _Unwind_Find_FDE
72 #ifndef PT_GNU_EH_FRAME
73 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
76 struct unw_eh_callback_data
86 struct unw_eh_frame_hdr
88 unsigned char version
;
89 unsigned char eh_frame_ptr_enc
;
90 unsigned char fde_count_enc
;
91 unsigned char table_enc
;
94 #define FRAME_HDR_CACHE_SIZE 8
96 static struct frame_hdr_cache_element
100 _Unwind_Ptr load_base
;
101 const ElfW(Phdr
) *p_eh_frame_hdr
;
102 const ElfW(Phdr
) *p_dynamic
;
103 struct frame_hdr_cache_element
*link
;
104 } frame_hdr_cache
[FRAME_HDR_CACHE_SIZE
];
106 static struct frame_hdr_cache_element
*frame_hdr_cache_head
;
108 /* Like base_of_encoded_value, but take the base from a struct
109 unw_eh_callback_data instead of an _Unwind_Context. */
112 base_from_cb_data (unsigned char encoding
, struct unw_eh_callback_data
*data
)
114 if (encoding
== DW_EH_PE_omit
)
117 switch (encoding
& 0x70)
119 case DW_EH_PE_absptr
:
121 case DW_EH_PE_aligned
:
124 case DW_EH_PE_textrel
:
125 return (_Unwind_Ptr
) data
->tbase
;
126 case DW_EH_PE_datarel
:
127 return (_Unwind_Ptr
) data
->dbase
;
134 _Unwind_IteratePhdrCallback (struct dl_phdr_info
*info
, size_t size
, void *ptr
)
136 struct unw_eh_callback_data
*data
= (struct unw_eh_callback_data
*) ptr
;
137 const ElfW(Phdr
) *phdr
, *p_eh_frame_hdr
, *p_dynamic
;
140 struct elf32_fdpic_loadaddr load_base
;
142 _Unwind_Ptr load_base
;
144 const unsigned char *p
;
145 const struct unw_eh_frame_hdr
*hdr
;
146 _Unwind_Ptr eh_frame
;
148 _Unwind_Ptr pc_low
= 0, pc_high
= 0;
150 struct ext_dl_phdr_info
152 ElfW(Addr
) dlpi_addr
;
153 const char *dlpi_name
;
154 const ElfW(Phdr
) *dlpi_phdr
;
155 ElfW(Half
) dlpi_phnum
;
156 unsigned long long int dlpi_adds
;
157 unsigned long long int dlpi_subs
;
161 phdr
= info
->dlpi_phdr
;
162 load_base
= info
->dlpi_addr
;
163 p_eh_frame_hdr
= NULL
;
166 struct frame_hdr_cache_element
*prev_cache_entry
= NULL
,
167 *last_cache_entry
= NULL
;
169 if (data
->check_cache
&& size
>= sizeof (struct ext_dl_phdr_info
))
171 static unsigned long long adds
= -1ULL, subs
;
172 struct ext_dl_phdr_info
*einfo
= (struct ext_dl_phdr_info
*) info
;
174 /* We use a least recently used cache replacement policy. Also,
175 the most recently used cache entries are placed at the head
176 of the search chain. */
178 if (einfo
->dlpi_adds
== adds
&& einfo
->dlpi_subs
== subs
)
180 /* Find data->pc in shared library cache.
181 Set load_base, p_eh_frame_hdr and p_dynamic
182 plus match from the cache and goto
183 "Read .eh_frame_hdr header." below. */
185 struct frame_hdr_cache_element
*cache_entry
;
187 for (cache_entry
= frame_hdr_cache_head
;
189 cache_entry
= cache_entry
->link
)
191 if (data
->pc
>= cache_entry
->pc_low
192 && data
->pc
< cache_entry
->pc_high
)
194 load_base
= cache_entry
->load_base
;
195 p_eh_frame_hdr
= cache_entry
->p_eh_frame_hdr
;
196 p_dynamic
= cache_entry
->p_dynamic
;
198 /* And move the entry we're using to the head. */
199 if (cache_entry
!= frame_hdr_cache_head
)
201 prev_cache_entry
->link
= cache_entry
->link
;
202 cache_entry
->link
= frame_hdr_cache_head
;
203 frame_hdr_cache_head
= cache_entry
;
208 last_cache_entry
= cache_entry
;
209 /* Exit early if we found an unused entry. */
210 if ((cache_entry
->pc_low
| cache_entry
->pc_high
) == 0)
212 if (cache_entry
->link
!= NULL
)
213 prev_cache_entry
= cache_entry
;
218 adds
= einfo
->dlpi_adds
;
219 subs
= einfo
->dlpi_subs
;
220 /* Initialize the cache. Create a chain of cache entries,
221 with the final one terminated by a NULL link. */
223 for (i
= 0; i
< FRAME_HDR_CACHE_SIZE
; i
++)
225 frame_hdr_cache
[i
].pc_low
= 0;
226 frame_hdr_cache
[i
].pc_high
= 0;
227 frame_hdr_cache
[i
].link
= &frame_hdr_cache
[i
+1];
229 frame_hdr_cache
[i
-1].link
= NULL
;
230 frame_hdr_cache_head
= &frame_hdr_cache
[0];
231 data
->check_cache
= 0;
235 /* Make sure struct dl_phdr_info is at least as big as we need. */
236 if (size
< offsetof (struct dl_phdr_info
, dlpi_phnum
)
237 + sizeof (info
->dlpi_phnum
))
240 /* See if PC falls into one of the loaded segments. Find the eh_frame
241 segment at the same time. */
242 for (n
= info
->dlpi_phnum
; --n
>= 0; phdr
++)
244 if (phdr
->p_type
== PT_LOAD
)
246 _Unwind_Ptr vaddr
= (_Unwind_Ptr
)
247 __RELOC_POINTER (phdr
->p_vaddr
, load_base
);
248 if (data
->pc
>= vaddr
&& data
->pc
< vaddr
+ phdr
->p_memsz
)
252 pc_high
= vaddr
+ phdr
->p_memsz
;
255 else if (phdr
->p_type
== PT_GNU_EH_FRAME
)
256 p_eh_frame_hdr
= phdr
;
257 else if (phdr
->p_type
== PT_DYNAMIC
)
264 if (size
>= sizeof (struct ext_dl_phdr_info
))
266 /* Move the cache entry we're about to overwrite to the head of
267 the list. If either last_cache_entry or prev_cache_entry are
268 NULL, that cache entry is already at the head. */
269 if (last_cache_entry
!= NULL
&& prev_cache_entry
!= NULL
)
271 prev_cache_entry
->link
= last_cache_entry
->link
;
272 last_cache_entry
->link
= frame_hdr_cache_head
;
273 frame_hdr_cache_head
= last_cache_entry
;
276 frame_hdr_cache_head
->load_base
= load_base
;
277 frame_hdr_cache_head
->p_eh_frame_hdr
= p_eh_frame_hdr
;
278 frame_hdr_cache_head
->p_dynamic
= p_dynamic
;
279 frame_hdr_cache_head
->pc_low
= pc_low
;
280 frame_hdr_cache_head
->pc_high
= pc_high
;
288 /* Read .eh_frame_hdr header. */
289 hdr
= (const struct unw_eh_frame_hdr
*)
290 __RELOC_POINTER (p_eh_frame_hdr
->p_vaddr
, load_base
);
291 if (hdr
->version
!= 1)
294 #ifdef CRT_GET_RFIB_DATA
299 /* For dynamically linked executables and shared libraries,
300 DT_PLTGOT is the gp value for that object. */
301 ElfW(Dyn
) *dyn
= (ElfW(Dyn
) *)
302 __RELOC_POINTER (p_dynamic
->p_vaddr
, load_base
);
303 for (; dyn
->d_tag
!= DT_NULL
; dyn
++)
304 if (dyn
->d_tag
== DT_PLTGOT
)
306 /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */
307 data
->dbase
= (void *) dyn
->d_un
.d_ptr
;
311 # elif defined __FRV_FDPIC__ && defined __linux__
312 data
->dbase
= load_base
.got_value
;
314 # error What is DW_EH_PE_datarel base on this platform?
318 p
= read_encoded_value_with_base (hdr
->eh_frame_ptr_enc
,
319 base_from_cb_data (hdr
->eh_frame_ptr_enc
,
321 (const unsigned char *) (hdr
+ 1),
324 /* We require here specific table encoding to speed things up.
325 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
326 as base, not the processor specific DW_EH_PE_datarel. */
327 if (hdr
->fde_count_enc
!= DW_EH_PE_omit
328 && hdr
->table_enc
== (DW_EH_PE_datarel
| DW_EH_PE_sdata4
))
330 _Unwind_Ptr fde_count
;
332 p
= read_encoded_value_with_base (hdr
->fde_count_enc
,
333 base_from_cb_data (hdr
->fde_count_enc
,
336 /* Shouldn't happen. */
339 if ((((_Unwind_Ptr
) p
) & 3) == 0)
342 signed initial_loc
__attribute__ ((mode (SI
)));
343 signed fde
__attribute__ ((mode (SI
)));
345 const struct fde_table
*table
= (const struct fde_table
*) p
;
347 _Unwind_Ptr data_base
= (_Unwind_Ptr
) hdr
;
349 unsigned int f_enc
, f_enc_size
;
353 if (data
->pc
< table
[0].initial_loc
+ data_base
)
355 else if (data
->pc
< table
[mid
].initial_loc
+ data_base
)
363 if (data
->pc
< table
[mid
].initial_loc
+ data_base
)
365 else if (data
->pc
>= table
[mid
+ 1].initial_loc
+ data_base
)
371 gcc_assert (lo
< hi
);
374 f
= (fde
*) (table
[mid
].fde
+ data_base
);
375 f_enc
= get_fde_encoding (f
);
376 f_enc_size
= size_of_encoded_value (f_enc
);
377 read_encoded_value_with_base (f_enc
& 0x0f, 0,
378 &f
->pc_begin
[f_enc_size
], &range
);
379 if (data
->pc
< table
[mid
].initial_loc
+ data_base
+ range
)
381 data
->func
= (void *) (table
[mid
].initial_loc
+ data_base
);
386 /* We have no sorted search table, so need to go the slow way.
387 As soon as GLIBC will provide API so to notify that a library has been
388 removed, we could cache this (and thus use search_object). */
390 ob
.tbase
= data
->tbase
;
391 ob
.dbase
= data
->dbase
;
392 ob
.u
.single
= (fde
*) eh_frame
;
394 ob
.s
.b
.mixed_encoding
= 1; /* Need to assume worst case. */
395 data
->ret
= linear_search_fdes (&ob
, (fde
*) eh_frame
, (void *) data
->pc
);
396 if (data
->ret
!= NULL
)
399 unsigned int encoding
= get_fde_encoding (data
->ret
);
401 read_encoded_value_with_base (encoding
,
402 base_from_cb_data (encoding
, data
),
403 data
->ret
->pc_begin
, &func
);
404 data
->func
= (void *) func
;
410 _Unwind_Find_FDE (void *pc
, struct dwarf_eh_bases
*bases
)
412 struct unw_eh_callback_data data
;
415 ret
= _Unwind_Find_registered_FDE (pc
, bases
);
419 data
.pc
= (_Unwind_Ptr
) pc
;
424 data
.check_cache
= 1;
426 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback
, &data
) < 0)
431 bases
->tbase
= data
.tbase
;
432 bases
->dbase
= data
.dbase
;
433 bases
->func
= data
.func
;
439 /* Prevent multiple include of header files. */
440 #define _Unwind_Find_FDE _Unwind_Find_FDE
441 #include "unwind-dw2-fde.c"
444 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
445 alias (_Unwind_Find_FDE
);