unwind-dw2-fde-dip.c 15 KB

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