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