Subversion Repositories Kolibri OS

Rev

Rev 4383 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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