1 /* Copyright (C) 2001-2017 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
35 #if !defined(inhibit_libc) && !defined(__OpenBSD__)
36 #include <elf.h> /* Get DT_CONFIG. */
38 #include "coretypes.h"
40 #include "libgcc_tm.h"
43 #define NO_BASE_OF_ENCODED_VALUE
44 #include "unwind-pe.h"
45 #include "unwind-dw2-fde.h"
46 #include "unwind-compat.h"
49 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
50 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
51 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
52 # define USE_PT_GNU_EH_FRAME
55 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
56 && defined(__BIONIC__)
57 # define USE_PT_GNU_EH_FRAME
60 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
61 && defined(TARGET_DL_ITERATE_PHDR) \
63 # define USE_PT_GNU_EH_FRAME
66 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
67 && defined(TARGET_DL_ITERATE_PHDR) \
68 && (defined(__DragonFly__) || defined(__FreeBSD__))
70 # define USE_PT_GNU_EH_FRAME
73 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
74 && (defined(__OpenBSD__) || defined(__NetBSD__))
75 # define ElfW(type) Elf_##type
76 # define USE_PT_GNU_EH_FRAME
79 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
80 && defined(TARGET_DL_ITERATE_PHDR) \
81 && defined(__sun__) && defined(__svr4__)
82 # define USE_PT_GNU_EH_FRAME
85 #if defined(USE_PT_GNU_EH_FRAME)
89 #ifndef __RELOC_POINTER
90 # define __RELOC_POINTER(ptr, base) ((ptr) + (base))
93 static const fde
* _Unwind_Find_registered_FDE (void *pc
, struct dwarf_eh_bases
*bases
);
95 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
96 #include "unwind-dw2-fde.c"
97 #undef _Unwind_Find_FDE
99 #ifndef PT_GNU_EH_FRAME
100 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
103 struct unw_eh_callback_data
113 struct unw_eh_frame_hdr
115 unsigned char version
;
116 unsigned char eh_frame_ptr_enc
;
117 unsigned char fde_count_enc
;
118 unsigned char table_enc
;
121 #define FRAME_HDR_CACHE_SIZE 8
123 static struct frame_hdr_cache_element
127 #if defined __FRV_FDPIC__ || defined __BFIN_FDPIC__
128 struct elf32_fdpic_loadaddr load_base
;
130 _Unwind_Ptr load_base
;
132 const ElfW(Phdr
) *p_eh_frame_hdr
;
133 const ElfW(Phdr
) *p_dynamic
;
134 struct frame_hdr_cache_element
*link
;
135 } frame_hdr_cache
[FRAME_HDR_CACHE_SIZE
];
137 static struct frame_hdr_cache_element
*frame_hdr_cache_head
;
139 /* Like base_of_encoded_value, but take the base from a struct
140 unw_eh_callback_data instead of an _Unwind_Context. */
143 base_from_cb_data (unsigned char encoding
, struct unw_eh_callback_data
*data
)
145 if (encoding
== DW_EH_PE_omit
)
148 switch (encoding
& 0x70)
150 case DW_EH_PE_absptr
:
152 case DW_EH_PE_aligned
:
155 case DW_EH_PE_textrel
:
156 return (_Unwind_Ptr
) data
->tbase
;
157 case DW_EH_PE_datarel
:
158 return (_Unwind_Ptr
) data
->dbase
;
165 _Unwind_IteratePhdrCallback (struct dl_phdr_info
*info
, size_t size
, void *ptr
)
167 struct unw_eh_callback_data
*data
= (struct unw_eh_callback_data
*) ptr
;
168 const ElfW(Phdr
) *phdr
, *p_eh_frame_hdr
, *p_dynamic
;
170 #if defined __FRV_FDPIC__ || defined __BFIN_FDPIC__
171 struct elf32_fdpic_loadaddr load_base
;
173 _Unwind_Ptr load_base
;
175 const unsigned char *p
;
176 const struct unw_eh_frame_hdr
*hdr
;
177 _Unwind_Ptr eh_frame
;
179 _Unwind_Ptr pc_low
= 0, pc_high
= 0;
181 struct ext_dl_phdr_info
183 ElfW(Addr
) dlpi_addr
;
184 const char *dlpi_name
;
185 const ElfW(Phdr
) *dlpi_phdr
;
186 ElfW(Half
) dlpi_phnum
;
187 unsigned long long int dlpi_adds
;
188 unsigned long long int dlpi_subs
;
192 phdr
= info
->dlpi_phdr
;
193 load_base
= info
->dlpi_addr
;
194 p_eh_frame_hdr
= NULL
;
197 struct frame_hdr_cache_element
*prev_cache_entry
= NULL
,
198 *last_cache_entry
= NULL
;
200 if (data
->check_cache
&& size
>= sizeof (struct ext_dl_phdr_info
))
202 static unsigned long long adds
= -1ULL, subs
;
203 struct ext_dl_phdr_info
*einfo
= (struct ext_dl_phdr_info
*) info
;
205 /* We use a least recently used cache replacement policy. Also,
206 the most recently used cache entries are placed at the head
207 of the search chain. */
209 if (einfo
->dlpi_adds
== adds
&& einfo
->dlpi_subs
== subs
)
211 /* Find data->pc in shared library cache.
212 Set load_base, p_eh_frame_hdr and p_dynamic
213 plus match from the cache and goto
214 "Read .eh_frame_hdr header." below. */
216 struct frame_hdr_cache_element
*cache_entry
;
218 for (cache_entry
= frame_hdr_cache_head
;
220 cache_entry
= cache_entry
->link
)
222 if (data
->pc
>= cache_entry
->pc_low
223 && data
->pc
< cache_entry
->pc_high
)
225 load_base
= cache_entry
->load_base
;
226 p_eh_frame_hdr
= cache_entry
->p_eh_frame_hdr
;
227 p_dynamic
= cache_entry
->p_dynamic
;
229 /* And move the entry we're using to the head. */
230 if (cache_entry
!= frame_hdr_cache_head
)
232 prev_cache_entry
->link
= cache_entry
->link
;
233 cache_entry
->link
= frame_hdr_cache_head
;
234 frame_hdr_cache_head
= cache_entry
;
239 last_cache_entry
= cache_entry
;
240 /* Exit early if we found an unused entry. */
241 if ((cache_entry
->pc_low
| cache_entry
->pc_high
) == 0)
243 if (cache_entry
->link
!= NULL
)
244 prev_cache_entry
= cache_entry
;
249 adds
= einfo
->dlpi_adds
;
250 subs
= einfo
->dlpi_subs
;
251 /* Initialize the cache. Create a chain of cache entries,
252 with the final one terminated by a NULL link. */
254 for (i
= 0; i
< FRAME_HDR_CACHE_SIZE
; i
++)
256 frame_hdr_cache
[i
].pc_low
= 0;
257 frame_hdr_cache
[i
].pc_high
= 0;
258 frame_hdr_cache
[i
].link
= &frame_hdr_cache
[i
+1];
260 frame_hdr_cache
[i
-1].link
= NULL
;
261 frame_hdr_cache_head
= &frame_hdr_cache
[0];
262 data
->check_cache
= 0;
266 /* Make sure struct dl_phdr_info is at least as big as we need. */
267 if (size
< offsetof (struct dl_phdr_info
, dlpi_phnum
)
268 + sizeof (info
->dlpi_phnum
))
271 /* See if PC falls into one of the loaded segments. Find the eh_frame
272 segment at the same time. */
273 for (n
= info
->dlpi_phnum
; --n
>= 0; phdr
++)
275 if (phdr
->p_type
== PT_LOAD
)
277 _Unwind_Ptr vaddr
= (_Unwind_Ptr
)
278 __RELOC_POINTER (phdr
->p_vaddr
, load_base
);
279 if (data
->pc
>= vaddr
&& data
->pc
< vaddr
+ phdr
->p_memsz
)
283 pc_high
= vaddr
+ phdr
->p_memsz
;
286 else if (phdr
->p_type
== PT_GNU_EH_FRAME
)
287 p_eh_frame_hdr
= phdr
;
288 #ifdef PT_SUNW_UNWIND
289 /* Sun ld emits PT_SUNW_UNWIND .eh_frame_hdr sections instead of
290 PT_SUNW_EH_FRAME/PT_GNU_EH_FRAME, so accept them as well. */
291 else if (phdr
->p_type
== PT_SUNW_UNWIND
)
292 p_eh_frame_hdr
= phdr
;
294 else if (phdr
->p_type
== PT_DYNAMIC
)
301 if (size
>= sizeof (struct ext_dl_phdr_info
))
303 /* Move the cache entry we're about to overwrite to the head of
304 the list. If either last_cache_entry or prev_cache_entry are
305 NULL, that cache entry is already at the head. */
306 if (last_cache_entry
!= NULL
&& prev_cache_entry
!= NULL
)
308 prev_cache_entry
->link
= last_cache_entry
->link
;
309 last_cache_entry
->link
= frame_hdr_cache_head
;
310 frame_hdr_cache_head
= last_cache_entry
;
313 frame_hdr_cache_head
->load_base
= load_base
;
314 frame_hdr_cache_head
->p_eh_frame_hdr
= p_eh_frame_hdr
;
315 frame_hdr_cache_head
->p_dynamic
= p_dynamic
;
316 frame_hdr_cache_head
->pc_low
= pc_low
;
317 frame_hdr_cache_head
->pc_high
= pc_high
;
325 /* Read .eh_frame_hdr header. */
326 hdr
= (const struct unw_eh_frame_hdr
*)
327 __RELOC_POINTER (p_eh_frame_hdr
->p_vaddr
, load_base
);
328 if (hdr
->version
!= 1)
331 #ifdef CRT_GET_RFIB_DATA
336 /* For dynamically linked executables and shared libraries,
337 DT_PLTGOT is the gp value for that object. */
338 ElfW(Dyn
) *dyn
= (ElfW(Dyn
) *)
339 __RELOC_POINTER (p_dynamic
->p_vaddr
, load_base
);
340 for (; dyn
->d_tag
!= DT_NULL
; dyn
++)
341 if (dyn
->d_tag
== DT_PLTGOT
)
343 data
->dbase
= (void *) dyn
->d_un
.d_ptr
;
344 #if defined __linux__
345 /* On IA-32 Linux, _DYNAMIC is writable and GLIBC has
347 #elif defined __sun__ && defined __svr4__
348 /* On Solaris 2/x86, we need to do this ourselves. */
349 data
->dbase
+= load_base
;
354 # elif (defined __FRV_FDPIC__ || defined __BFIN_FDPIC__) && defined __linux__
355 data
->dbase
= load_base
.got_value
;
357 # error What is DW_EH_PE_datarel base on this platform?
361 p
= read_encoded_value_with_base (hdr
->eh_frame_ptr_enc
,
362 base_from_cb_data (hdr
->eh_frame_ptr_enc
,
364 (const unsigned char *) (hdr
+ 1),
367 /* We require here specific table encoding to speed things up.
368 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
369 as base, not the processor specific DW_EH_PE_datarel. */
370 if (hdr
->fde_count_enc
!= DW_EH_PE_omit
371 && hdr
->table_enc
== (DW_EH_PE_datarel
| DW_EH_PE_sdata4
))
373 _Unwind_Ptr fde_count
;
375 p
= read_encoded_value_with_base (hdr
->fde_count_enc
,
376 base_from_cb_data (hdr
->fde_count_enc
,
379 /* Shouldn't happen. */
382 if ((((_Unwind_Ptr
) p
) & 3) == 0)
385 signed initial_loc
__attribute__ ((mode (SI
)));
386 signed fde
__attribute__ ((mode (SI
)));
388 const struct fde_table
*table
= (const struct fde_table
*) p
;
390 _Unwind_Ptr data_base
= (_Unwind_Ptr
) hdr
;
392 unsigned int f_enc
, f_enc_size
;
396 if (data
->pc
< table
[0].initial_loc
+ data_base
)
398 else if (data
->pc
< table
[mid
].initial_loc
+ data_base
)
406 if (data
->pc
< table
[mid
].initial_loc
+ data_base
)
408 else if (data
->pc
>= table
[mid
+ 1].initial_loc
+ data_base
)
414 gcc_assert (lo
< hi
);
417 f
= (fde
*) (table
[mid
].fde
+ data_base
);
418 f_enc
= get_fde_encoding (f
);
419 f_enc_size
= size_of_encoded_value (f_enc
);
420 read_encoded_value_with_base (f_enc
& 0x0f, 0,
421 &f
->pc_begin
[f_enc_size
], &range
);
422 if (data
->pc
< table
[mid
].initial_loc
+ data_base
+ range
)
424 data
->func
= (void *) (table
[mid
].initial_loc
+ data_base
);
429 /* We have no sorted search table, so need to go the slow way.
430 As soon as GLIBC will provide API so to notify that a library has been
431 removed, we could cache this (and thus use search_object). */
433 ob
.tbase
= data
->tbase
;
434 ob
.dbase
= data
->dbase
;
435 ob
.u
.single
= (fde
*) eh_frame
;
437 ob
.s
.b
.mixed_encoding
= 1; /* Need to assume worst case. */
438 data
->ret
= linear_search_fdes (&ob
, (fde
*) eh_frame
, (void *) data
->pc
);
439 if (data
->ret
!= NULL
)
442 unsigned int encoding
= get_fde_encoding (data
->ret
);
444 read_encoded_value_with_base (encoding
,
445 base_from_cb_data (encoding
, data
),
446 data
->ret
->pc_begin
, &func
);
447 data
->func
= (void *) func
;
453 _Unwind_Find_FDE (void *pc
, struct dwarf_eh_bases
*bases
)
455 struct unw_eh_callback_data data
;
458 ret
= _Unwind_Find_registered_FDE (pc
, bases
);
462 data
.pc
= (_Unwind_Ptr
) pc
;
467 data
.check_cache
= 1;
469 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback
, &data
) < 0)
474 bases
->tbase
= data
.tbase
;
475 bases
->dbase
= data
.dbase
;
476 bases
->func
= data
.func
;
482 /* Prevent multiple include of header files. */
483 #define _Unwind_Find_FDE _Unwind_Find_FDE
484 #include "unwind-dw2-fde.c"
487 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
488 alias (_Unwind_Find_FDE
);