unwind-dw2-fde.c 28 KB

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