1 /* Copyright (C) 2001-2021 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(TARGET_DL_ITERATE_PHDR) \
75 && (defined(__OpenBSD__) || defined(__NetBSD__))
76 # define ElfW(type) Elf_##type
77 # define USE_PT_GNU_EH_FRAME
80 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
81 && defined(TARGET_DL_ITERATE_PHDR) \
82 && defined(__sun__) && defined(__svr4__)
83 # define USE_PT_GNU_EH_FRAME
86 #if defined(USE_PT_GNU_EH_FRAME)
90 #ifndef __RELOC_POINTER
91 # define __RELOC_POINTER(ptr, base) ((ptr) + (base))
94 static const fde
* _Unwind_Find_registered_FDE (void *pc
, struct dwarf_eh_bases
*bases
);
96 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
97 #include "unwind-dw2-fde.c"
98 #undef _Unwind_Find_FDE
100 #ifndef PT_GNU_EH_FRAME
101 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
104 #ifdef CRT_GET_RFIB_DATA
105 #define NEED_DBASE_MEMBER 1
107 #define NEED_DBASE_MEMBER 0
110 struct unw_eh_callback_data
113 #if NEED_DBASE_MEMBER
116 const struct unw_eh_frame_hdr
*hdr
;
120 /* Returns DATA->dbase if available, else NULL. */
121 static inline _Unwind_Ptr
122 unw_eh_callback_data_dbase (const struct unw_eh_callback_data
*data
123 __attribute__ ((unused
)))
125 #if NEED_DBASE_MEMBER
126 return (_Unwind_Ptr
) data
->dbase
;
132 struct unw_eh_frame_hdr
134 unsigned char version
;
135 unsigned char eh_frame_ptr_enc
;
136 unsigned char fde_count_enc
;
137 unsigned char table_enc
;
140 #define FRAME_HDR_CACHE_SIZE 8
142 static struct frame_hdr_cache_element
146 #if defined __FRV_FDPIC__ || defined __BFIN_FDPIC__
147 struct elf32_fdpic_loadaddr load_base
;
149 _Unwind_Ptr load_base
;
151 const ElfW(Phdr
) *p_eh_frame_hdr
;
152 const ElfW(Phdr
) *p_dynamic
;
153 struct frame_hdr_cache_element
*link
;
154 } frame_hdr_cache
[FRAME_HDR_CACHE_SIZE
];
156 static struct frame_hdr_cache_element
*frame_hdr_cache_head
;
158 /* Like base_of_encoded_value, but take the base from a struct
159 unw_eh_callback_data instead of an _Unwind_Context. */
161 static inline _Unwind_Ptr
162 base_from_cb_data (unsigned char encoding
__attribute__ ((unused
)),
163 _Unwind_Ptr dbase
__attribute__ ((unused
)))
165 #if NEED_DBASE_MEMBER
166 if (encoding
== DW_EH_PE_omit
)
169 switch (encoding
& 0x70)
171 case DW_EH_PE_absptr
:
173 case DW_EH_PE_aligned
:
176 case DW_EH_PE_textrel
:
178 case DW_EH_PE_datarel
:
183 #else /* !NEED_DBASE_MEMBER */
189 _Unwind_IteratePhdrCallback (struct dl_phdr_info
*info
, size_t size
, void *ptr
)
191 struct unw_eh_callback_data
*data
= (struct unw_eh_callback_data
*) ptr
;
192 const ElfW(Phdr
) *phdr
, *p_eh_frame_hdr
, *p_dynamic
;
194 #if defined __FRV_FDPIC__ || defined __BFIN_FDPIC__
195 struct elf32_fdpic_loadaddr load_base
;
197 _Unwind_Ptr load_base
;
199 _Unwind_Ptr pc_low
= 0, pc_high
= 0;
201 struct ext_dl_phdr_info
203 ElfW(Addr
) dlpi_addr
;
204 const char *dlpi_name
;
205 const ElfW(Phdr
) *dlpi_phdr
;
206 ElfW(Half
) dlpi_phnum
;
207 unsigned long long int dlpi_adds
;
208 unsigned long long int dlpi_subs
;
212 phdr
= info
->dlpi_phdr
;
213 load_base
= info
->dlpi_addr
;
214 p_eh_frame_hdr
= NULL
;
217 struct frame_hdr_cache_element
*prev_cache_entry
= NULL
,
218 *last_cache_entry
= NULL
;
220 if (data
->check_cache
&& size
>= sizeof (struct ext_dl_phdr_info
))
222 static unsigned long long adds
= -1ULL, subs
;
223 struct ext_dl_phdr_info
*einfo
= (struct ext_dl_phdr_info
*) info
;
225 /* We use a least recently used cache replacement policy. Also,
226 the most recently used cache entries are placed at the head
227 of the search chain. */
229 if (einfo
->dlpi_adds
== adds
&& einfo
->dlpi_subs
== subs
)
231 /* Find data->pc in shared library cache.
232 Set load_base, p_eh_frame_hdr and p_dynamic
233 plus match from the cache and goto
234 "Read .eh_frame_hdr header." below. */
236 struct frame_hdr_cache_element
*cache_entry
;
238 for (cache_entry
= frame_hdr_cache_head
;
240 cache_entry
= cache_entry
->link
)
242 if (data
->pc
>= cache_entry
->pc_low
243 && data
->pc
< cache_entry
->pc_high
)
245 load_base
= cache_entry
->load_base
;
246 p_eh_frame_hdr
= cache_entry
->p_eh_frame_hdr
;
247 p_dynamic
= cache_entry
->p_dynamic
;
249 /* And move the entry we're using to the head. */
250 if (cache_entry
!= frame_hdr_cache_head
)
252 prev_cache_entry
->link
= cache_entry
->link
;
253 cache_entry
->link
= frame_hdr_cache_head
;
254 frame_hdr_cache_head
= cache_entry
;
259 last_cache_entry
= cache_entry
;
260 /* Exit early if we found an unused entry. */
261 if ((cache_entry
->pc_low
| cache_entry
->pc_high
) == 0)
263 if (cache_entry
->link
!= NULL
)
264 prev_cache_entry
= cache_entry
;
269 adds
= einfo
->dlpi_adds
;
270 subs
= einfo
->dlpi_subs
;
271 /* Initialize the cache. Create a chain of cache entries,
272 with the final one terminated by a NULL link. */
274 for (i
= 0; i
< FRAME_HDR_CACHE_SIZE
; i
++)
276 frame_hdr_cache
[i
].pc_low
= 0;
277 frame_hdr_cache
[i
].pc_high
= 0;
278 frame_hdr_cache
[i
].link
= &frame_hdr_cache
[i
+1];
280 frame_hdr_cache
[i
-1].link
= NULL
;
281 frame_hdr_cache_head
= &frame_hdr_cache
[0];
282 data
->check_cache
= 0;
286 /* Make sure struct dl_phdr_info is at least as big as we need. */
287 if (size
< offsetof (struct dl_phdr_info
, dlpi_phnum
)
288 + sizeof (info
->dlpi_phnum
))
291 /* See if PC falls into one of the loaded segments. Find the eh_frame
292 segment at the same time. */
293 for (n
= info
->dlpi_phnum
; --n
>= 0; phdr
++)
295 if (phdr
->p_type
== PT_LOAD
)
297 _Unwind_Ptr vaddr
= (_Unwind_Ptr
)
298 __RELOC_POINTER (phdr
->p_vaddr
, load_base
);
299 if (data
->pc
>= vaddr
&& data
->pc
< vaddr
+ phdr
->p_memsz
)
303 pc_high
= vaddr
+ phdr
->p_memsz
;
306 else if (phdr
->p_type
== PT_GNU_EH_FRAME
)
307 p_eh_frame_hdr
= phdr
;
308 #ifdef PT_SUNW_UNWIND
309 /* Sun ld emits PT_SUNW_UNWIND .eh_frame_hdr sections instead of
310 PT_SUNW_EH_FRAME/PT_GNU_EH_FRAME, so accept them as well. */
311 else if (phdr
->p_type
== PT_SUNW_UNWIND
)
312 p_eh_frame_hdr
= phdr
;
314 else if (phdr
->p_type
== PT_DYNAMIC
)
321 if (size
>= sizeof (struct ext_dl_phdr_info
))
323 /* Move the cache entry we're about to overwrite to the head of
324 the list. If either last_cache_entry or prev_cache_entry are
325 NULL, that cache entry is already at the head. */
326 if (last_cache_entry
!= NULL
&& prev_cache_entry
!= NULL
)
328 prev_cache_entry
->link
= last_cache_entry
->link
;
329 last_cache_entry
->link
= frame_hdr_cache_head
;
330 frame_hdr_cache_head
= last_cache_entry
;
333 frame_hdr_cache_head
->load_base
= load_base
;
334 frame_hdr_cache_head
->p_eh_frame_hdr
= p_eh_frame_hdr
;
335 frame_hdr_cache_head
->p_dynamic
= p_dynamic
;
336 frame_hdr_cache_head
->pc_low
= pc_low
;
337 frame_hdr_cache_head
->pc_high
= pc_high
;
345 /* Read .eh_frame_hdr header. */
346 data
->hdr
= (const struct unw_eh_frame_hdr
*)
347 __RELOC_POINTER (p_eh_frame_hdr
->p_vaddr
, load_base
);
349 #ifdef CRT_GET_RFIB_DATA
350 # if defined __i386__ || defined __nios2__
354 /* For dynamically linked executables and shared libraries,
355 DT_PLTGOT is the gp value for that object. */
356 ElfW(Dyn
) *dyn
= (ElfW(Dyn
) *)
357 __RELOC_POINTER (p_dynamic
->p_vaddr
, load_base
);
358 for (; dyn
->d_tag
!= DT_NULL
; dyn
++)
359 if (dyn
->d_tag
== DT_PLTGOT
)
361 data
->dbase
= (void *) dyn
->d_un
.d_ptr
;
362 #if defined __linux__
363 /* On IA-32 Linux, _DYNAMIC is writable and GLIBC has
365 #elif defined __sun__ && defined __svr4__
366 /* On Solaris 2/x86, we need to do this ourselves. */
367 data
->dbase
+= load_base
;
372 # elif (defined __FRV_FDPIC__ || defined __BFIN_FDPIC__) && defined __linux__
373 data
->dbase
= load_base
.got_value
;
375 # error What is DW_EH_PE_datarel base on this platform?
382 /* Find the FDE for the program counter PC, in a previously located
383 PT_GNU_EH_FRAME data region. *BASES is updated if an FDE to return is
387 find_fde_tail (_Unwind_Ptr pc
,
388 const struct unw_eh_frame_hdr
*hdr
,
390 struct dwarf_eh_bases
*bases
)
392 const unsigned char *p
= (const unsigned char *) (hdr
+ 1);
393 _Unwind_Ptr eh_frame
;
396 if (hdr
->version
!= 1)
399 p
= read_encoded_value_with_base (hdr
->eh_frame_ptr_enc
,
400 base_from_cb_data (hdr
->eh_frame_ptr_enc
,
404 /* We require here specific table encoding to speed things up.
405 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
406 as base, not the processor specific DW_EH_PE_datarel. */
407 if (hdr
->fde_count_enc
!= DW_EH_PE_omit
408 && hdr
->table_enc
== (DW_EH_PE_datarel
| DW_EH_PE_sdata4
))
410 _Unwind_Ptr fde_count
;
412 p
= read_encoded_value_with_base (hdr
->fde_count_enc
,
413 base_from_cb_data (hdr
->fde_count_enc
,
416 /* Shouldn't happen. */
419 if ((((_Unwind_Ptr
) p
) & 3) == 0)
422 signed initial_loc
__attribute__ ((mode (SI
)));
423 signed fde
__attribute__ ((mode (SI
)));
425 const struct fde_table
*table
= (const struct fde_table
*) p
;
427 _Unwind_Ptr data_base
= (_Unwind_Ptr
) hdr
;
429 unsigned int f_enc
, f_enc_size
;
433 if (pc
< table
[0].initial_loc
+ data_base
)
435 else if (pc
< table
[mid
].initial_loc
+ data_base
)
443 if (pc
< table
[mid
].initial_loc
+ data_base
)
445 else if (pc
>= table
[mid
+ 1].initial_loc
+ data_base
)
451 gcc_assert (lo
< hi
);
454 f
= (fde
*) (table
[mid
].fde
+ data_base
);
455 f_enc
= get_fde_encoding (f
);
456 f_enc_size
= size_of_encoded_value (f_enc
);
457 read_encoded_value_with_base (f_enc
& 0x0f, 0,
458 &f
->pc_begin
[f_enc_size
], &range
);
459 _Unwind_Ptr func
= table
[mid
].initial_loc
+ data_base
;
460 if (pc
< table
[mid
].initial_loc
+ data_base
+ range
)
463 bases
->dbase
= (void *) dbase
;
464 bases
->func
= (void *) func
;
472 /* We have no sorted search table, so need to go the slow way.
473 As soon as GLIBC will provide API so to notify that a library has been
474 removed, we could cache this (and thus use search_object). */
477 ob
.dbase
= (void *) dbase
;
478 ob
.u
.single
= (fde
*) eh_frame
;
480 ob
.s
.b
.mixed_encoding
= 1; /* Need to assume worst case. */
481 const fde
*entry
= linear_search_fdes (&ob
, (fde
*) eh_frame
, (void *) pc
);
485 unsigned int encoding
= get_fde_encoding (entry
);
487 read_encoded_value_with_base (encoding
,
488 base_from_cb_data (encoding
, dbase
),
489 entry
->pc_begin
, &func
);
491 bases
->dbase
= (void *) dbase
;
492 bases
->func
= (void *) func
;
498 _Unwind_Find_FDE (void *pc
, struct dwarf_eh_bases
*bases
)
500 struct unw_eh_callback_data data
;
503 ret
= _Unwind_Find_registered_FDE (pc
, bases
);
507 data
.pc
= (_Unwind_Ptr
) pc
;
508 #if NEED_DBASE_MEMBER
511 data
.check_cache
= 1;
513 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback
, &data
) <= 0)
516 _Unwind_Ptr dbase
= unw_eh_callback_data_dbase (&data
);
517 return find_fde_tail ((_Unwind_Ptr
) pc
, data
.hdr
, dbase
, bases
);
521 /* Prevent multiple include of header files. */
522 #define _Unwind_Find_FDE _Unwind_Find_FDE
523 #include "unwind-dw2-fde.c"
526 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
527 alias (_Unwind_Find_FDE
);