Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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