Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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