unwind-dw2-fde.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /* Subroutines needed for unwinding stack frames for exception handling. */
  2. /* Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Contributed by Jason Merrill <jason@cygnus.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #ifndef _Unwind_Find_FDE
  21. #include "tconfig.h"
  22. #include "tsystem.h"
  23. #include "coretypes.h"
  24. #include "tm.h"
  25. #include "libgcc_tm.h"
  26. #include "dwarf2.h"
  27. #include "unwind.h"
  28. #define NO_BASE_OF_ENCODED_VALUE
  29. #include "unwind-pe.h"
  30. #include "unwind-dw2-fde.h"
  31. #include "gthr.h"
  32. #else
  33. #if (defined(__GTHREAD_MUTEX_INIT) || defined(__GTHREAD_MUTEX_INIT_FUNCTION)) \
  34. && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  35. #define ATOMIC_FDE_FAST_PATH 1
  36. #endif
  37. #endif
  38. /* The unseen_objects list contains objects that have been registered
  39. but not yet categorized in any way. The seen_objects list has had
  40. its pc_begin and count fields initialized at minimum, and is sorted
  41. by decreasing value of pc_begin. */
  42. static struct object *unseen_objects;
  43. static struct object *seen_objects;
  44. #ifdef ATOMIC_FDE_FAST_PATH
  45. static int any_objects_registered;
  46. #endif
  47. #ifdef __GTHREAD_MUTEX_INIT
  48. static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
  49. #define init_object_mutex_once()
  50. #else
  51. #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
  52. static __gthread_mutex_t object_mutex;
  53. static void
  54. init_object_mutex (void)
  55. {
  56. __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
  57. }
  58. static void
  59. init_object_mutex_once (void)
  60. {
  61. static __gthread_once_t once = __GTHREAD_ONCE_INIT;
  62. __gthread_once (&once, init_object_mutex);
  63. }
  64. #else
  65. /* ??? Several targets include this file with stubbing parts of gthr.h
  66. and expect no locking to be done. */
  67. #define init_object_mutex_once()
  68. static __gthread_mutex_t object_mutex;
  69. #endif
  70. #endif
  71. /* Called from crtbegin.o to register the unwind info for an object. */
  72. void
  73. __register_frame_info_bases (const void *begin, struct object *ob,
  74. void *tbase, void *dbase)
  75. {
  76. /* If .eh_frame is empty, don't register at all. */
  77. if ((const uword *) begin == 0 || *(const uword *) begin == 0)
  78. return;
  79. ob->pc_begin = (void *)-1;
  80. ob->tbase = tbase;
  81. ob->dbase = dbase;
  82. ob->u.single = begin;
  83. ob->s.i = 0;
  84. ob->s.b.encoding = DW_EH_PE_omit;
  85. #ifdef DWARF2_OBJECT_END_PTR_EXTENSION
  86. ob->fde_end = NULL;
  87. #endif
  88. init_object_mutex_once ();
  89. __gthread_mutex_lock (&object_mutex);
  90. ob->next = unseen_objects;
  91. unseen_objects = ob;
  92. #ifdef ATOMIC_FDE_FAST_PATH
  93. /* Set flag that at least one library has registered FDEs.
  94. Use relaxed MO here, it is up to the app to ensure that the library
  95. loading/initialization happens-before using that library in other
  96. threads (in particular unwinding with that library's functions
  97. appearing in the backtraces). Calling that library's functions
  98. without waiting for the library to initialize would be racy. */
  99. if (!any_objects_registered)
  100. __atomic_store_n (&any_objects_registered, 1, __ATOMIC_RELAXED);
  101. #endif
  102. __gthread_mutex_unlock (&object_mutex);
  103. }
  104. void
  105. __register_frame_info (const void *begin, struct object *ob)
  106. {
  107. __register_frame_info_bases (begin, ob, 0, 0);
  108. }
  109. void
  110. __register_frame (void *begin)
  111. {
  112. struct object *ob;
  113. /* If .eh_frame is empty, don't register at all. */
  114. if (*(uword *) begin == 0)
  115. return;
  116. ob = malloc (sizeof (struct object));
  117. __register_frame_info (begin, ob);
  118. }
  119. /* Similar, but BEGIN is actually a pointer to a table of unwind entries
  120. for different translation units. Called from the file generated by
  121. collect2. */
  122. void
  123. __register_frame_info_table_bases (void *begin, struct object *ob,
  124. void *tbase, void *dbase)
  125. {
  126. ob->pc_begin = (void *)-1;
  127. ob->tbase = tbase;
  128. ob->dbase = dbase;
  129. ob->u.array = begin;
  130. ob->s.i = 0;
  131. ob->s.b.from_array = 1;
  132. ob->s.b.encoding = DW_EH_PE_omit;
  133. init_object_mutex_once ();
  134. __gthread_mutex_lock (&object_mutex);
  135. ob->next = unseen_objects;
  136. unseen_objects = ob;
  137. #ifdef ATOMIC_FDE_FAST_PATH
  138. /* Set flag that at least one library has registered FDEs.
  139. Use relaxed MO here, it is up to the app to ensure that the library
  140. loading/initialization happens-before using that library in other
  141. threads (in particular unwinding with that library's functions
  142. appearing in the backtraces). Calling that library's functions
  143. without waiting for the library to initialize would be racy. */
  144. if (!any_objects_registered)
  145. __atomic_store_n (&any_objects_registered, 1, __ATOMIC_RELAXED);
  146. #endif
  147. __gthread_mutex_unlock (&object_mutex);
  148. }
  149. void
  150. __register_frame_info_table (void *begin, struct object *ob)
  151. {
  152. __register_frame_info_table_bases (begin, ob, 0, 0);
  153. }
  154. void
  155. __register_frame_table (void *begin)
  156. {
  157. struct object *ob = malloc (sizeof (struct object));
  158. __register_frame_info_table (begin, ob);
  159. }
  160. /* Called from crtbegin.o to deregister the unwind info for an object. */
  161. /* ??? Glibc has for a while now exported __register_frame_info and
  162. __deregister_frame_info. If we call __register_frame_info_bases
  163. from crtbegin (wherein it is declared weak), and this object does
  164. not get pulled from libgcc.a for other reasons, then the
  165. invocation of __deregister_frame_info will be resolved from glibc.
  166. Since the registration did not happen there, we'll die.
  167. Therefore, declare a new deregistration entry point that does the
  168. exact same thing, but will resolve to the same library as
  169. implements __register_frame_info_bases. */
  170. void *
  171. __deregister_frame_info_bases (const void *begin)
  172. {
  173. struct object **p;
  174. struct object *ob = 0;
  175. /* If .eh_frame is empty, we haven't registered. */
  176. if ((const uword *) begin == 0 || *(const uword *) begin == 0)
  177. return ob;
  178. init_object_mutex_once ();
  179. __gthread_mutex_lock (&object_mutex);
  180. for (p = &unseen_objects; *p ; p = &(*p)->next)
  181. if ((*p)->u.single == begin)
  182. {
  183. ob = *p;
  184. *p = ob->next;
  185. goto out;
  186. }
  187. for (p = &seen_objects; *p ; p = &(*p)->next)
  188. if ((*p)->s.b.sorted)
  189. {
  190. if ((*p)->u.sort->orig_data == begin)
  191. {
  192. ob = *p;
  193. *p = ob->next;
  194. free (ob->u.sort);
  195. goto out;
  196. }
  197. }
  198. else
  199. {
  200. if ((*p)->u.single == begin)
  201. {
  202. ob = *p;
  203. *p = ob->next;
  204. goto out;
  205. }
  206. }
  207. out:
  208. __gthread_mutex_unlock (&object_mutex);
  209. gcc_assert (ob);
  210. return (void *) ob;
  211. }
  212. void *
  213. __deregister_frame_info (const void *begin)
  214. {
  215. return __deregister_frame_info_bases (begin);
  216. }
  217. void
  218. __deregister_frame (void *begin)
  219. {
  220. /* If .eh_frame is empty, we haven't registered. */
  221. if (*(uword *) begin != 0)
  222. free (__deregister_frame_info (begin));
  223. }
  224. /* Like base_of_encoded_value, but take the base from a struct object
  225. instead of an _Unwind_Context. */
  226. static _Unwind_Ptr
  227. base_from_object (unsigned char encoding, struct object *ob)
  228. {
  229. if (encoding == DW_EH_PE_omit)
  230. return 0;
  231. switch (encoding & 0x70)
  232. {
  233. case DW_EH_PE_absptr:
  234. case DW_EH_PE_pcrel:
  235. case DW_EH_PE_aligned:
  236. return 0;
  237. case DW_EH_PE_textrel:
  238. return (_Unwind_Ptr) ob->tbase;
  239. case DW_EH_PE_datarel:
  240. return (_Unwind_Ptr) ob->dbase;
  241. default:
  242. gcc_unreachable ();
  243. }
  244. }
  245. /* Return the FDE pointer encoding from the CIE. */
  246. /* ??? This is a subset of extract_cie_info from unwind-dw2.c. */
  247. static int
  248. get_cie_encoding (const struct dwarf_cie *cie)
  249. {
  250. const unsigned char *aug, *p;
  251. _Unwind_Ptr dummy;
  252. _uleb128_t utmp;
  253. _sleb128_t stmp;
  254. aug = cie->augmentation;
  255. p = aug + strlen ((const char *)aug) + 1; /* Skip the augmentation string. */
  256. if (__builtin_expect (cie->version >= 4, 0))
  257. {
  258. if (p[0] != sizeof (void *) || p[1] != 0)
  259. return DW_EH_PE_omit; /* We are not prepared to handle unexpected
  260. address sizes or segment selectors. */
  261. p += 2; /* Skip address size and segment size. */
  262. }
  263. if (aug[0] != 'z')
  264. return DW_EH_PE_absptr;
  265. p = read_uleb128 (p, &utmp); /* Skip code alignment. */
  266. p = read_sleb128 (p, &stmp); /* Skip data alignment. */
  267. if (cie->version == 1) /* Skip return address column. */
  268. p++;
  269. else
  270. p = read_uleb128 (p, &utmp);
  271. aug++; /* Skip 'z' */
  272. p = read_uleb128 (p, &utmp); /* Skip augmentation length. */
  273. while (1)
  274. {
  275. /* This is what we're looking for. */
  276. if (*aug == 'R')
  277. return *p;
  278. /* Personality encoding and pointer. */
  279. else if (*aug == 'P')
  280. {
  281. /* ??? Avoid dereferencing indirect pointers, since we're
  282. faking the base address. Gotta keep DW_EH_PE_aligned
  283. intact, however. */
  284. p = read_encoded_value_with_base (*p & 0x7F, 0, p + 1, &dummy);
  285. }
  286. /* LSDA encoding. */
  287. else if (*aug == 'L')
  288. p++;
  289. /* aarch64 b-key pointer authentication. */
  290. else if (*aug == 'B')
  291. p++;
  292. /* Otherwise end of string, or unknown augmentation. */
  293. else
  294. return DW_EH_PE_absptr;
  295. aug++;
  296. }
  297. }
  298. static inline int
  299. get_fde_encoding (const struct dwarf_fde *f)
  300. {
  301. return get_cie_encoding (get_cie (f));
  302. }
  303. /* Sorting an array of FDEs by address.
  304. (Ideally we would have the linker sort the FDEs so we don't have to do
  305. it at run time. But the linkers are not yet prepared for this.) */
  306. /* Comparison routines. Three variants of increasing complexity. */
  307. static int
  308. fde_unencoded_compare (struct object *ob __attribute__((unused)),
  309. const fde *x, const fde *y)
  310. {
  311. _Unwind_Ptr x_ptr, y_ptr;
  312. memcpy (&x_ptr, x->pc_begin, sizeof (_Unwind_Ptr));
  313. memcpy (&y_ptr, y->pc_begin, sizeof (_Unwind_Ptr));
  314. if (x_ptr > y_ptr)
  315. return 1;
  316. if (x_ptr < y_ptr)
  317. return -1;
  318. return 0;
  319. }
  320. static int
  321. fde_single_encoding_compare (struct object *ob, const fde *x, const fde *y)
  322. {
  323. _Unwind_Ptr base, x_ptr, y_ptr;
  324. base = base_from_object (ob->s.b.encoding, ob);
  325. read_encoded_value_with_base (ob->s.b.encoding, base, x->pc_begin, &x_ptr);
  326. read_encoded_value_with_base (ob->s.b.encoding, base, y->pc_begin, &y_ptr);
  327. if (x_ptr > y_ptr)
  328. return 1;
  329. if (x_ptr < y_ptr)
  330. return -1;
  331. return 0;
  332. }
  333. static int
  334. fde_mixed_encoding_compare (struct object *ob, const fde *x, const fde *y)
  335. {
  336. int x_encoding, y_encoding;
  337. _Unwind_Ptr x_ptr, y_ptr;
  338. x_encoding = get_fde_encoding (x);
  339. read_encoded_value_with_base (x_encoding, base_from_object (x_encoding, ob),
  340. x->pc_begin, &x_ptr);
  341. y_encoding = get_fde_encoding (y);
  342. read_encoded_value_with_base (y_encoding, base_from_object (y_encoding, ob),
  343. y->pc_begin, &y_ptr);
  344. if (x_ptr > y_ptr)
  345. return 1;
  346. if (x_ptr < y_ptr)
  347. return -1;
  348. return 0;
  349. }
  350. typedef int (*fde_compare_t) (struct object *, const fde *, const fde *);
  351. /* This is a special mix of insertion sort and heap sort, optimized for
  352. the data sets that actually occur. They look like
  353. 101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130.
  354. I.e. a linearly increasing sequence (coming from functions in the text
  355. section), with additionally a few unordered elements (coming from functions
  356. in gnu_linkonce sections) whose values are higher than the values in the
  357. surrounding linear sequence (but not necessarily higher than the values
  358. at the end of the linear sequence!).
  359. The worst-case total run time is O(N) + O(n log (n)), where N is the
  360. total number of FDEs and n is the number of erratic ones. */
  361. struct fde_accumulator
  362. {
  363. struct fde_vector *linear;
  364. struct fde_vector *erratic;
  365. };
  366. static inline int
  367. start_fde_sort (struct fde_accumulator *accu, size_t count)
  368. {
  369. size_t size;
  370. if (! count)
  371. return 0;
  372. size = sizeof (struct fde_vector) + sizeof (const fde *) * count;
  373. if ((accu->linear = malloc (size)))
  374. {
  375. accu->linear->count = 0;
  376. if ((accu->erratic = malloc (size)))
  377. accu->erratic->count = 0;
  378. return 1;
  379. }
  380. else
  381. return 0;
  382. }
  383. static inline void
  384. fde_insert (struct fde_accumulator *accu, const fde *this_fde)
  385. {
  386. if (accu->linear)
  387. accu->linear->array[accu->linear->count++] = this_fde;
  388. }
  389. /* Split LINEAR into a linear sequence with low values and an erratic
  390. sequence with high values, put the linear one (of longest possible
  391. length) into LINEAR and the erratic one into ERRATIC. This is O(N).
  392. Because the longest linear sequence we are trying to locate within the
  393. incoming LINEAR array can be interspersed with (high valued) erratic
  394. entries. We construct a chain indicating the sequenced entries.
  395. To avoid having to allocate this chain, we overlay it onto the space of
  396. the ERRATIC array during construction. A final pass iterates over the
  397. chain to determine what should be placed in the ERRATIC array, and
  398. what is the linear sequence. This overlay is safe from aliasing. */
  399. static inline void
  400. fde_split (struct object *ob, fde_compare_t fde_compare,
  401. struct fde_vector *linear, struct fde_vector *erratic)
  402. {
  403. static const fde *marker;
  404. size_t count = linear->count;
  405. const fde *const *chain_end = &marker;
  406. size_t i, j, k;
  407. /* This should optimize out, but it is wise to make sure this assumption
  408. is correct. Should these have different sizes, we cannot cast between
  409. them and the overlaying onto ERRATIC will not work. */
  410. gcc_assert (sizeof (const fde *) == sizeof (const fde **));
  411. for (i = 0; i < count; i++)
  412. {
  413. const fde *const *probe;
  414. for (probe = chain_end;
  415. probe != &marker && fde_compare (ob, linear->array[i], *probe) < 0;
  416. probe = chain_end)
  417. {
  418. chain_end = (const fde *const*) erratic->array[probe - linear->array];
  419. erratic->array[probe - linear->array] = NULL;
  420. }
  421. erratic->array[i] = (const fde *) chain_end;
  422. chain_end = &linear->array[i];
  423. }
  424. /* Each entry in LINEAR which is part of the linear sequence we have
  425. discovered will correspond to a non-NULL entry in the chain we built in
  426. the ERRATIC array. */
  427. for (i = j = k = 0; i < count; i++)
  428. if (erratic->array[i])
  429. linear->array[j++] = linear->array[i];
  430. else
  431. erratic->array[k++] = linear->array[i];
  432. linear->count = j;
  433. erratic->count = k;
  434. }
  435. #define SWAP(x,y) do { const fde * tmp = x; x = y; y = tmp; } while (0)
  436. /* Convert a semi-heap to a heap. A semi-heap is a heap except possibly
  437. for the first (root) node; push it down to its rightful place. */
  438. static void
  439. frame_downheap (struct object *ob, fde_compare_t fde_compare, const fde **a,
  440. int lo, int hi)
  441. {
  442. int i, j;
  443. for (i = lo, j = 2*i+1;
  444. j < hi;
  445. j = 2*i+1)
  446. {
  447. if (j+1 < hi && fde_compare (ob, a[j], a[j+1]) < 0)
  448. ++j;
  449. if (fde_compare (ob, a[i], a[j]) < 0)
  450. {
  451. SWAP (a[i], a[j]);
  452. i = j;
  453. }
  454. else
  455. break;
  456. }
  457. }
  458. /* This is O(n log(n)). BSD/OS defines heapsort in stdlib.h, so we must
  459. use a name that does not conflict. */
  460. static void
  461. frame_heapsort (struct object *ob, fde_compare_t fde_compare,
  462. struct fde_vector *erratic)
  463. {
  464. /* For a description of this algorithm, see:
  465. Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
  466. p. 60-61. */
  467. const fde ** a = erratic->array;
  468. /* A portion of the array is called a "heap" if for all i>=0:
  469. If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
  470. If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */
  471. size_t n = erratic->count;
  472. int m;
  473. /* Expand our heap incrementally from the end of the array, heapifying
  474. each resulting semi-heap as we go. After each step, a[m] is the top
  475. of a heap. */
  476. for (m = n/2-1; m >= 0; --m)
  477. frame_downheap (ob, fde_compare, a, m, n);
  478. /* Shrink our heap incrementally from the end of the array, first
  479. swapping out the largest element a[0] and then re-heapifying the
  480. resulting semi-heap. After each step, a[0..m) is a heap. */
  481. for (m = n-1; m >= 1; --m)
  482. {
  483. SWAP (a[0], a[m]);
  484. frame_downheap (ob, fde_compare, a, 0, m);
  485. }
  486. #undef SWAP
  487. }
  488. /* Merge V1 and V2, both sorted, and put the result into V1. */
  489. static inline void
  490. fde_merge (struct object *ob, fde_compare_t fde_compare,
  491. struct fde_vector *v1, struct fde_vector *v2)
  492. {
  493. size_t i1, i2;
  494. const fde * fde2;
  495. i2 = v2->count;
  496. if (i2 > 0)
  497. {
  498. i1 = v1->count;
  499. do
  500. {
  501. i2--;
  502. fde2 = v2->array[i2];
  503. while (i1 > 0 && fde_compare (ob, v1->array[i1-1], fde2) > 0)
  504. {
  505. v1->array[i1+i2] = v1->array[i1-1];
  506. i1--;
  507. }
  508. v1->array[i1+i2] = fde2;
  509. }
  510. while (i2 > 0);
  511. v1->count += v2->count;
  512. }
  513. }
  514. static inline void
  515. end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count)
  516. {
  517. fde_compare_t fde_compare;
  518. gcc_assert (!accu->linear || accu->linear->count == count);
  519. if (ob->s.b.mixed_encoding)
  520. fde_compare = fde_mixed_encoding_compare;
  521. else if (ob->s.b.encoding == DW_EH_PE_absptr)
  522. fde_compare = fde_unencoded_compare;
  523. else
  524. fde_compare = fde_single_encoding_compare;
  525. if (accu->erratic)
  526. {
  527. fde_split (ob, fde_compare, accu->linear, accu->erratic);
  528. gcc_assert (accu->linear->count + accu->erratic->count == count);
  529. frame_heapsort (ob, fde_compare, accu->erratic);
  530. fde_merge (ob, fde_compare, accu->linear, accu->erratic);
  531. free (accu->erratic);
  532. }
  533. else
  534. {
  535. /* We've not managed to malloc an erratic array,
  536. so heap sort in the linear one. */
  537. frame_heapsort (ob, fde_compare, accu->linear);
  538. }
  539. }
  540. /* Update encoding, mixed_encoding, and pc_begin for OB for the
  541. fde array beginning at THIS_FDE. Return the number of fdes
  542. encountered along the way. */
  543. static size_t
  544. classify_object_over_fdes (struct object *ob, const fde *this_fde)
  545. {
  546. const struct dwarf_cie *last_cie = 0;
  547. size_t count = 0;
  548. int encoding = DW_EH_PE_absptr;
  549. _Unwind_Ptr base = 0;
  550. for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
  551. {
  552. const struct dwarf_cie *this_cie;
  553. _Unwind_Ptr mask, pc_begin;
  554. /* Skip CIEs. */
  555. if (this_fde->CIE_delta == 0)
  556. continue;
  557. /* Determine the encoding for this FDE. Note mixed encoded
  558. objects for later. */
  559. this_cie = get_cie (this_fde);
  560. if (this_cie != last_cie)
  561. {
  562. last_cie = this_cie;
  563. encoding = get_cie_encoding (this_cie);
  564. if (encoding == DW_EH_PE_omit)
  565. return -1;
  566. base = base_from_object (encoding, ob);
  567. if (ob->s.b.encoding == DW_EH_PE_omit)
  568. ob->s.b.encoding = encoding;
  569. else if (ob->s.b.encoding != encoding)
  570. ob->s.b.mixed_encoding = 1;
  571. }
  572. read_encoded_value_with_base (encoding, base, this_fde->pc_begin,
  573. &pc_begin);
  574. /* Take care to ignore link-once functions that were removed.
  575. In these cases, the function address will be NULL, but if
  576. the encoding is smaller than a pointer a true NULL may not
  577. be representable. Assume 0 in the representable bits is NULL. */
  578. mask = size_of_encoded_value (encoding);
  579. if (mask < sizeof (void *))
  580. mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1;
  581. else
  582. mask = -1;
  583. if ((pc_begin & mask) == 0)
  584. continue;
  585. count += 1;
  586. if ((void *) pc_begin < ob->pc_begin)
  587. ob->pc_begin = (void *) pc_begin;
  588. }
  589. return count;
  590. }
  591. static void
  592. add_fdes (struct object *ob, struct fde_accumulator *accu, const fde *this_fde)
  593. {
  594. const struct dwarf_cie *last_cie = 0;
  595. int encoding = ob->s.b.encoding;
  596. _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
  597. for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
  598. {
  599. const struct dwarf_cie *this_cie;
  600. /* Skip CIEs. */
  601. if (this_fde->CIE_delta == 0)
  602. continue;
  603. if (ob->s.b.mixed_encoding)
  604. {
  605. /* Determine the encoding for this FDE. Note mixed encoded
  606. objects for later. */
  607. this_cie = get_cie (this_fde);
  608. if (this_cie != last_cie)
  609. {
  610. last_cie = this_cie;
  611. encoding = get_cie_encoding (this_cie);
  612. base = base_from_object (encoding, ob);
  613. }
  614. }
  615. if (encoding == DW_EH_PE_absptr)
  616. {
  617. _Unwind_Ptr ptr;
  618. memcpy (&ptr, this_fde->pc_begin, sizeof (_Unwind_Ptr));
  619. if (ptr == 0)
  620. continue;
  621. }
  622. else
  623. {
  624. _Unwind_Ptr pc_begin, mask;
  625. read_encoded_value_with_base (encoding, base, this_fde->pc_begin,
  626. &pc_begin);
  627. /* Take care to ignore link-once functions that were removed.
  628. In these cases, the function address will be NULL, but if
  629. the encoding is smaller than a pointer a true NULL may not
  630. be representable. Assume 0 in the representable bits is NULL. */
  631. mask = size_of_encoded_value (encoding);
  632. if (mask < sizeof (void *))
  633. mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1;
  634. else
  635. mask = -1;
  636. if ((pc_begin & mask) == 0)
  637. continue;
  638. }
  639. fde_insert (accu, this_fde);
  640. }
  641. }
  642. /* Set up a sorted array of pointers to FDEs for a loaded object. We
  643. count up the entries before allocating the array because it's likely to
  644. be faster. We can be called multiple times, should we have failed to
  645. allocate a sorted fde array on a previous occasion. */
  646. static inline void
  647. init_object (struct object* ob)
  648. {
  649. struct fde_accumulator accu;
  650. size_t count;
  651. count = ob->s.b.count;
  652. if (count == 0)
  653. {
  654. if (ob->s.b.from_array)
  655. {
  656. fde **p = ob->u.array;
  657. for (count = 0; *p; ++p)
  658. {
  659. size_t cur_count = classify_object_over_fdes (ob, *p);
  660. if (cur_count == (size_t) -1)
  661. goto unhandled_fdes;
  662. count += cur_count;
  663. }
  664. }
  665. else
  666. {
  667. count = classify_object_over_fdes (ob, ob->u.single);
  668. if (count == (size_t) -1)
  669. {
  670. static const fde terminator;
  671. unhandled_fdes:
  672. ob->s.i = 0;
  673. ob->s.b.encoding = DW_EH_PE_omit;
  674. ob->u.single = &terminator;
  675. return;
  676. }
  677. }
  678. /* The count field we have in the main struct object is somewhat
  679. limited, but should suffice for virtually all cases. If the
  680. counted value doesn't fit, re-write a zero. The worst that
  681. happens is that we re-count next time -- admittedly non-trivial
  682. in that this implies some 2M fdes, but at least we function. */
  683. ob->s.b.count = count;
  684. if (ob->s.b.count != count)
  685. ob->s.b.count = 0;
  686. }
  687. if (!start_fde_sort (&accu, count))
  688. return;
  689. if (ob->s.b.from_array)
  690. {
  691. fde **p;
  692. for (p = ob->u.array; *p; ++p)
  693. add_fdes (ob, &accu, *p);
  694. }
  695. else
  696. add_fdes (ob, &accu, ob->u.single);
  697. end_fde_sort (ob, &accu, count);
  698. /* Save the original fde pointer, since this is the key by which the
  699. DSO will deregister the object. */
  700. accu.linear->orig_data = ob->u.single;
  701. ob->u.sort = accu.linear;
  702. ob->s.b.sorted = 1;
  703. }
  704. /* A linear search through a set of FDEs for the given PC. This is
  705. used when there was insufficient memory to allocate and sort an
  706. array. */
  707. static const fde *
  708. linear_search_fdes (struct object *ob, const fde *this_fde, void *pc)
  709. {
  710. const struct dwarf_cie *last_cie = 0;
  711. int encoding = ob->s.b.encoding;
  712. _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
  713. for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
  714. {
  715. const struct dwarf_cie *this_cie;
  716. _Unwind_Ptr pc_begin, pc_range;
  717. /* Skip CIEs. */
  718. if (this_fde->CIE_delta == 0)
  719. continue;
  720. if (ob->s.b.mixed_encoding)
  721. {
  722. /* Determine the encoding for this FDE. Note mixed encoded
  723. objects for later. */
  724. this_cie = get_cie (this_fde);
  725. if (this_cie != last_cie)
  726. {
  727. last_cie = this_cie;
  728. encoding = get_cie_encoding (this_cie);
  729. base = base_from_object (encoding, ob);
  730. }
  731. }
  732. if (encoding == DW_EH_PE_absptr)
  733. {
  734. const _Unwind_Ptr *pc_array = (const _Unwind_Ptr *) this_fde->pc_begin;
  735. pc_begin = pc_array[0];
  736. pc_range = pc_array[1];
  737. if (pc_begin == 0)
  738. continue;
  739. }
  740. else
  741. {
  742. _Unwind_Ptr mask;
  743. const unsigned char *p;
  744. p = read_encoded_value_with_base (encoding, base,
  745. this_fde->pc_begin, &pc_begin);
  746. read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
  747. /* Take care to ignore link-once functions that were removed.
  748. In these cases, the function address will be NULL, but if
  749. the encoding is smaller than a pointer a true NULL may not
  750. be representable. Assume 0 in the representable bits is NULL. */
  751. mask = size_of_encoded_value (encoding);
  752. if (mask < sizeof (void *))
  753. mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1;
  754. else
  755. mask = -1;
  756. if ((pc_begin & mask) == 0)
  757. continue;
  758. }
  759. if ((_Unwind_Ptr) pc - pc_begin < pc_range)
  760. return this_fde;
  761. }
  762. return NULL;
  763. }
  764. /* Binary search for an FDE containing the given PC. Here are three
  765. implementations of increasing complexity. */
  766. static inline const fde *
  767. binary_search_unencoded_fdes (struct object *ob, void *pc)
  768. {
  769. struct fde_vector *vec = ob->u.sort;
  770. size_t lo, hi;
  771. for (lo = 0, hi = vec->count; lo < hi; )
  772. {
  773. size_t i = (lo + hi) / 2;
  774. const fde *const f = vec->array[i];
  775. void *pc_begin;
  776. uaddr pc_range;
  777. memcpy (&pc_begin, (const void * const *) f->pc_begin, sizeof (void *));
  778. memcpy (&pc_range, (const uaddr *) f->pc_begin + 1, sizeof (uaddr));
  779. if (pc < pc_begin)
  780. hi = i;
  781. else if (pc >= pc_begin + pc_range)
  782. lo = i + 1;
  783. else
  784. return f;
  785. }
  786. return NULL;
  787. }
  788. static inline const fde *
  789. binary_search_single_encoding_fdes (struct object *ob, void *pc)
  790. {
  791. struct fde_vector *vec = ob->u.sort;
  792. int encoding = ob->s.b.encoding;
  793. _Unwind_Ptr base = base_from_object (encoding, ob);
  794. size_t lo, hi;
  795. for (lo = 0, hi = vec->count; lo < hi; )
  796. {
  797. size_t i = (lo + hi) / 2;
  798. const fde *f = vec->array[i];
  799. _Unwind_Ptr pc_begin, pc_range;
  800. const unsigned char *p;
  801. p = read_encoded_value_with_base (encoding, base, f->pc_begin,
  802. &pc_begin);
  803. read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
  804. if ((_Unwind_Ptr) pc < pc_begin)
  805. hi = i;
  806. else if ((_Unwind_Ptr) pc >= pc_begin + pc_range)
  807. lo = i + 1;
  808. else
  809. return f;
  810. }
  811. return NULL;
  812. }
  813. static inline const fde *
  814. binary_search_mixed_encoding_fdes (struct object *ob, void *pc)
  815. {
  816. struct fde_vector *vec = ob->u.sort;
  817. size_t lo, hi;
  818. for (lo = 0, hi = vec->count; lo < hi; )
  819. {
  820. size_t i = (lo + hi) / 2;
  821. const fde *f = vec->array[i];
  822. _Unwind_Ptr pc_begin, pc_range;
  823. const unsigned char *p;
  824. int encoding;
  825. encoding = get_fde_encoding (f);
  826. p = read_encoded_value_with_base (encoding,
  827. base_from_object (encoding, ob),
  828. f->pc_begin, &pc_begin);
  829. read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
  830. if ((_Unwind_Ptr) pc < pc_begin)
  831. hi = i;
  832. else if ((_Unwind_Ptr) pc >= pc_begin + pc_range)
  833. lo = i + 1;
  834. else
  835. return f;
  836. }
  837. return NULL;
  838. }
  839. static const fde *
  840. search_object (struct object* ob, void *pc)
  841. {
  842. /* If the data hasn't been sorted, try to do this now. We may have
  843. more memory available than last time we tried. */
  844. if (! ob->s.b.sorted)
  845. {
  846. init_object (ob);
  847. /* Despite the above comment, the normal reason to get here is
  848. that we've not processed this object before. A quick range
  849. check is in order. */
  850. if (pc < ob->pc_begin)
  851. return NULL;
  852. }
  853. if (ob->s.b.sorted)
  854. {
  855. if (ob->s.b.mixed_encoding)
  856. return binary_search_mixed_encoding_fdes (ob, pc);
  857. else if (ob->s.b.encoding == DW_EH_PE_absptr)
  858. return binary_search_unencoded_fdes (ob, pc);
  859. else
  860. return binary_search_single_encoding_fdes (ob, pc);
  861. }
  862. else
  863. {
  864. /* Long slow laborious linear search, cos we've no memory. */
  865. if (ob->s.b.from_array)
  866. {
  867. fde **p;
  868. for (p = ob->u.array; *p ; p++)
  869. {
  870. const fde *f = linear_search_fdes (ob, *p, pc);
  871. if (f)
  872. return f;
  873. }
  874. return NULL;
  875. }
  876. else
  877. return linear_search_fdes (ob, ob->u.single, pc);
  878. }
  879. }
  880. const fde *
  881. _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
  882. {
  883. struct object *ob;
  884. const fde *f = NULL;
  885. #ifdef ATOMIC_FDE_FAST_PATH
  886. /* For targets where unwind info is usually not registered through these
  887. APIs anymore, avoid taking a global lock.
  888. Use relaxed MO here, it is up to the app to ensure that the library
  889. loading/initialization happens-before using that library in other
  890. threads (in particular unwinding with that library's functions
  891. appearing in the backtraces). Calling that library's functions
  892. without waiting for the library to initialize would be racy. */
  893. if (__builtin_expect (!__atomic_load_n (&any_objects_registered,
  894. __ATOMIC_RELAXED), 1))
  895. return NULL;
  896. #endif
  897. init_object_mutex_once ();
  898. __gthread_mutex_lock (&object_mutex);
  899. /* Linear search through the classified objects, to find the one
  900. containing the pc. Note that pc_begin is sorted descending, and
  901. we expect objects to be non-overlapping. */
  902. for (ob = seen_objects; ob; ob = ob->next)
  903. if (pc >= ob->pc_begin)
  904. {
  905. f = search_object (ob, pc);
  906. if (f)
  907. goto fini;
  908. break;
  909. }
  910. /* Classify and search the objects we've not yet processed. */
  911. while ((ob = unseen_objects))
  912. {
  913. struct object **p;
  914. unseen_objects = ob->next;
  915. f = search_object (ob, pc);
  916. /* Insert the object into the classified list. */
  917. for (p = &seen_objects; *p ; p = &(*p)->next)
  918. if ((*p)->pc_begin < ob->pc_begin)
  919. break;
  920. ob->next = *p;
  921. *p = ob;
  922. if (f)
  923. goto fini;
  924. }
  925. fini:
  926. __gthread_mutex_unlock (&object_mutex);
  927. if (f)
  928. {
  929. int encoding;
  930. _Unwind_Ptr func;
  931. bases->tbase = ob->tbase;
  932. bases->dbase = ob->dbase;
  933. encoding = ob->s.b.encoding;
  934. if (ob->s.b.mixed_encoding)
  935. encoding = get_fde_encoding (f);
  936. read_encoded_value_with_base (encoding, base_from_object (encoding, ob),
  937. f->pc_begin, &func);
  938. bases->func = (void *) func;
  939. }
  940. return f;
  941. }