OSDN Git Service

2008-03-04 Uros Bizjak <ubizjak@gmail.com>
[pf3gnuchains/gcc-fork.git] / gcc / unwind-dw2-fde.c
index c8e6c5d..ac2e0d4 100644 (file)
@@ -1,5 +1,6 @@
 /* Subroutines needed for unwinding stack frames for exception handling.  */
-/* Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Free Software Foundation, Inc.
    Contributed by Jason Merrill <jason@cygnus.com>.
 
 This file is part of GCC.
@@ -25,21 +26,25 @@ for more details.
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.  */
+Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.  */
 
+#ifndef _Unwind_Find_FDE
 #include "tconfig.h"
 #include "tsystem.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "dwarf2.h"
 #include "unwind.h"
 #define NO_BASE_OF_ENCODED_VALUE
 #include "unwind-pe.h"
 #include "unwind-dw2-fde.h"
 #include "gthr.h"
+#endif
 
 /* The unseen_objects list contains objects that have been registered
    but not yet categorized in any way.  The seen_objects list has had
-   it's pc_begin and count fields initialized at minimum, and is sorted
+   its pc_begin and count fields initialized at minimum, and is sorted
    by decreasing value of pc_begin.  */
 static struct object *unseen_objects;
 static struct object *seen_objects;
@@ -51,7 +56,7 @@ static __gthread_mutex_t object_mutex;
 #endif
 
 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
-static void 
+static void
 init_object_mutex (void)
 {
   __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
@@ -70,11 +75,11 @@ init_object_mutex_once (void)
 /* Called from crtbegin.o to register the unwind info for an object.  */
 
 void
-__register_frame_info_bases (void *begin, struct object *ob,
+__register_frame_info_bases (const void *begin, struct object *ob,
                             void *tbase, void *dbase)
 {
   /* If .eh_frame is empty, don't register at all.  */
-  if (*(uword *)begin == 0)
+  if ((const uword *) begin == 0 || *(const uword *) begin == 0)
     return;
 
   ob->pc_begin = (void *)-1;
@@ -83,6 +88,9 @@ __register_frame_info_bases (void *begin, struct object *ob,
   ob->u.single = begin;
   ob->s.i = 0;
   ob->s.b.encoding = DW_EH_PE_omit;
+#ifdef DWARF2_OBJECT_END_PTR_EXTENSION
+  ob->fde_end = NULL;
+#endif
 
   init_object_mutex_once ();
   __gthread_mutex_lock (&object_mutex);
@@ -94,7 +102,7 @@ __register_frame_info_bases (void *begin, struct object *ob,
 }
 
 void
-__register_frame_info (void *begin, struct object *ob)
+__register_frame_info (const void *begin, struct object *ob)
 {
   __register_frame_info_bases (begin, ob, 0, 0);
 }
@@ -105,11 +113,11 @@ __register_frame (void *begin)
   struct object *ob;
 
   /* If .eh_frame is empty, don't register at all.  */
-  if (*(uword *)begin == 0)
+  if (*(uword *) begin == 0)
     return;
 
-  ob = (struct object *) malloc (sizeof (struct object));
-  __register_frame_info (begin, ob);                       
+  ob = malloc (sizeof (struct object));
+  __register_frame_info (begin, ob);
 }
 
 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
@@ -146,7 +154,7 @@ __register_frame_info_table (void *begin, struct object *ob)
 void
 __register_frame_table (void *begin)
 {
-  struct object *ob = (struct object *) malloc (sizeof (struct object));
+  struct object *ob = malloc (sizeof (struct object));
   __register_frame_info_table (begin, ob);
 }
 
@@ -156,21 +164,21 @@ __register_frame_table (void *begin)
    from crtbegin (wherein it is declared weak), and this object does
    not get pulled from libgcc.a for other reasons, then the
    invocation of __deregister_frame_info will be resolved from glibc.
-   Since the registration did not happen there, we'll abort.
+   Since the registration did not happen there, we'll die.
 
    Therefore, declare a new deregistration entry point that does the
-   exact same thing, but will resolve to the same library as 
+   exact same thing, but will resolve to the same library as
    implements __register_frame_info_bases.  */
 
 void *
-__deregister_frame_info_bases (void *begin)
+__deregister_frame_info_bases (const void *begin)
 {
   struct object **p;
   struct object *ob = 0;
 
   /* If .eh_frame is empty, we haven't registered.  */
-  if (*(uword *)begin == 0)
-    return;
+  if ((const uword *) begin == 0 || *(const uword *) begin == 0)
+    return ob;
 
   init_object_mutex_once ();
   __gthread_mutex_lock (&object_mutex);
@@ -204,16 +212,14 @@ __deregister_frame_info_bases (void *begin)
          }
       }
 
-  __gthread_mutex_unlock (&object_mutex);
-  abort ();
-
  out:
   __gthread_mutex_unlock (&object_mutex);
+  gcc_assert (ob);
   return (void *) ob;
 }
 
 void *
-__deregister_frame_info (void *begin)
+__deregister_frame_info (const void *begin)
 {
   return __deregister_frame_info_bases (begin);
 }
@@ -222,7 +228,7 @@ void
 __deregister_frame (void *begin)
 {
   /* If .eh_frame is empty, we haven't registered.  */
-  if (*(uword *)begin != 0)
+  if (*(uword *) begin != 0)
     free (__deregister_frame_info (begin));
 }
 
@@ -247,29 +253,33 @@ base_from_object (unsigned char encoding, struct object *ob)
       return (_Unwind_Ptr) ob->tbase;
     case DW_EH_PE_datarel:
       return (_Unwind_Ptr) ob->dbase;
+    default:
+      gcc_unreachable ();
     }
-  abort ();
 }
 
 /* Return the FDE pointer encoding from the CIE.  */
 /* ??? This is a subset of extract_cie_info from unwind-dw2.c.  */
 
 static int
-get_cie_encoding (struct dwarf_cie *cie)
+get_cie_encoding (const struct dwarf_cie *cie)
 {
   const unsigned char *aug, *p;
   _Unwind_Ptr dummy;
-  _Unwind_Word utmp;
-  _Unwind_Sword stmp;
+  _uleb128_t utmp;
+  _sleb128_t stmp;
 
   aug = cie->augmentation;
   if (aug[0] != 'z')
     return DW_EH_PE_absptr;
 
-  p = aug + strlen (aug) + 1;          /* Skip the augmentation string.  */
+  p = aug + strlen ((const char *)aug) + 1; /* Skip the augmentation string.  */
   p = read_uleb128 (p, &utmp);         /* Skip code alignment.  */
   p = read_sleb128 (p, &stmp);         /* Skip data alignment.  */
-  p++;                                 /* Skip return address column.  */
+  if (cie->version == 1)               /* Skip return address column.  */
+    p++;
+  else
+    p = read_uleb128 (p, &utmp);
 
   aug++;                               /* Skip 'z' */
   p = read_uleb128 (p, &utmp);         /* Skip augmentation length.  */
@@ -297,7 +307,7 @@ get_cie_encoding (struct dwarf_cie *cie)
 }
 
 static inline int
-get_fde_encoding (struct dwarf_fde *f)
+get_fde_encoding (const struct dwarf_fde *f)
 {
   return get_cie_encoding (get_cie (f));
 }
@@ -311,10 +321,10 @@ get_fde_encoding (struct dwarf_fde *f)
 
 static int
 fde_unencoded_compare (struct object *ob __attribute__((unused)),
-                      fde *x, fde *y)
+                      const fde *x, const fde *y)
 {
-  _Unwind_Ptr x_ptr = *(_Unwind_Ptr *) x->pc_begin;
-  _Unwind_Ptr y_ptr = *(_Unwind_Ptr *) y->pc_begin;
+  const _Unwind_Ptr x_ptr = *(const _Unwind_Ptr *) x->pc_begin;
+  const _Unwind_Ptr y_ptr = *(const _Unwind_Ptr *) y->pc_begin;
 
   if (x_ptr > y_ptr)
     return 1;
@@ -324,7 +334,7 @@ fde_unencoded_compare (struct object *ob __attribute__((unused)),
 }
 
 static int
-fde_single_encoding_compare (struct object *ob, fde *x, fde *y)
+fde_single_encoding_compare (struct object *ob, const fde *x, const fde *y)
 {
   _Unwind_Ptr base, x_ptr, y_ptr;
 
@@ -340,7 +350,7 @@ fde_single_encoding_compare (struct object *ob, fde *x, fde *y)
 }
 
 static int
-fde_mixed_encoding_compare (struct object *ob, fde *x, fde *y)
+fde_mixed_encoding_compare (struct object *ob, const fde *x, const fde *y)
 {
   int x_encoding, y_encoding;
   _Unwind_Ptr x_ptr, y_ptr;
@@ -360,7 +370,7 @@ fde_mixed_encoding_compare (struct object *ob, fde *x, fde *y)
   return 0;
 }
 
-typedef int (*fde_compare_t) (struct object *, fde *, fde *);
+typedef int (*fde_compare_t) (struct object *, const fde *, const fde *);
 
 
 /* This is a special mix of insertion sort and heap sort, optimized for
@@ -387,20 +397,20 @@ start_fde_sort (struct fde_accumulator *accu, size_t count)
   if (! count)
     return 0;
 
-  size = sizeof (struct fde_vector) + sizeof (fde *) * count;
-  if ((accu->linear = (struct fde_vector *) malloc (size)))
+  size = sizeof (struct fde_vector) + sizeof (const fde *) * count;
+  if ((accu->linear = malloc (size)))
     {
       accu->linear->count = 0;
-      if ((accu->erratic = (struct fde_vector *) malloc (size)))
+      if ((accu->erratic = malloc (size)))
        accu->erratic->count = 0;
       return 1;
     }
   else
-    return 0;  
+    return 0;
 }
 
 static inline void
-fde_insert (struct fde_accumulator *accu, fde *this_fde)
+fde_insert (struct fde_accumulator *accu, const fde *this_fde)
 {
   if (accu->linear)
     accu->linear->array[accu->linear->count++] = this_fde;
@@ -409,7 +419,7 @@ fde_insert (struct fde_accumulator *accu, fde *this_fde)
 /* Split LINEAR into a linear sequence with low values and an erratic
    sequence with high values, put the linear one (of longest possible
    length) into LINEAR and the erratic one into ERRATIC. This is O(N).
-   
+
    Because the longest linear sequence we are trying to locate within the
    incoming LINEAR array can be interspersed with (high valued) erratic
    entries.  We construct a chain indicating the sequenced entries.
@@ -422,29 +432,28 @@ static inline void
 fde_split (struct object *ob, fde_compare_t fde_compare,
           struct fde_vector *linear, struct fde_vector *erratic)
 {
-  static fde *marker;
+  static const fde *marker;
   size_t count = linear->count;
-  fde **chain_end = &marker;
+  const fde *const *chain_end = &marker;
   size_t i, j, k;
 
   /* This should optimize out, but it is wise to make sure this assumption
      is correct. Should these have different sizes, we cannot cast between
      them and the overlaying onto ERRATIC will not work.  */
-  if (sizeof (fde *) != sizeof (fde **))
-    abort ();
-  
+  gcc_assert (sizeof (const fde *) == sizeof (const fde **));
+
   for (i = 0; i < count; i++)
     {
-      fde **probe;
-      
+      const fde *const *probe;
+
       for (probe = chain_end;
-           probe != &marker && fde_compare (ob, linear->array[i], *probe) < 0;
-           probe = chain_end)
-        {
-          chain_end = (fde **)erratic->array[probe - linear->array];
-          erratic->array[probe - linear->array] = NULL;
-        }
-      erratic->array[i] = (fde *)chain_end;
+          probe != &marker && fde_compare (ob, linear->array[i], *probe) < 0;
+          probe = chain_end)
+       {
+         chain_end = (const fde *const*) erratic->array[probe - linear->array];
+         erratic->array[probe - linear->array] = NULL;
+       }
+      erratic->array[i] = (const fde *) chain_end;
       chain_end = &linear->array[i];
     }
 
@@ -460,6 +469,34 @@ fde_split (struct object *ob, fde_compare_t fde_compare,
   erratic->count = k;
 }
 
+#define SWAP(x,y) do { const fde * tmp = x; x = y; y = tmp; } while (0)
+
+/* Convert a semi-heap to a heap.  A semi-heap is a heap except possibly
+   for the first (root) node; push it down to its rightful place.  */
+
+static void
+frame_downheap (struct object *ob, fde_compare_t fde_compare, const fde **a,
+               int lo, int hi)
+{
+  int i, j;
+
+  for (i = lo, j = 2*i+1;
+       j < hi;
+       j = 2*i+1)
+    {
+      if (j+1 < hi && fde_compare (ob, a[j], a[j+1]) < 0)
+       ++j;
+
+      if (fde_compare (ob, a[i], a[j]) < 0)
+       {
+         SWAP (a[i], a[j]);
+         i = j;
+       }
+      else
+       break;
+    }
+}
+
 /* This is O(n log(n)).  BSD/OS defines heapsort in stdlib.h, so we must
    use a name that does not conflict.  */
 
@@ -470,59 +507,26 @@ frame_heapsort (struct object *ob, fde_compare_t fde_compare,
   /* For a description of this algorithm, see:
      Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
      p. 60-61.  */
-  fde ** a = erratic->array;
+  const fde ** a = erratic->array;
   /* A portion of the array is called a "heap" if for all i>=0:
      If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
      If i and 2i+2 are valid indices, then a[i] >= a[2i+2].  */
-#define SWAP(x,y) do { fde * tmp = x; x = y; y = tmp; } while (0)
   size_t n = erratic->count;
-  size_t m = n;
-  size_t i;
-
-  while (m > 0)
-    {
-      /* Invariant: a[m..n-1] is a heap.  */
-      m--;
-      for (i = m; 2*i+1 < n; )
-        {
-          if (2*i+2 < n
-              && fde_compare (ob, a[2*i+2], a[2*i+1]) > 0
-              && fde_compare (ob, a[2*i+2], a[i]) > 0)
-            {
-              SWAP (a[i], a[2*i+2]);
-              i = 2*i+2;
-            }
-          else if (fde_compare (ob, a[2*i+1], a[i]) > 0)
-            {
-              SWAP (a[i], a[2*i+1]);
-              i = 2*i+1;
-            }
-          else
-            break;
-        }
-    }
-  while (n > 1)
+  int m;
+
+  /* Expand our heap incrementally from the end of the array, heapifying
+     each resulting semi-heap as we go.  After each step, a[m] is the top
+     of a heap.  */
+  for (m = n/2-1; m >= 0; --m)
+    frame_downheap (ob, fde_compare, a, m, n);
+
+  /* Shrink our heap incrementally from the end of the array, first
+     swapping out the largest element a[0] and then re-heapifying the
+     resulting semi-heap.  After each step, a[0..m) is a heap.  */
+  for (m = n-1; m >= 1; --m)
     {
-      /* Invariant: a[0..n-1] is a heap.  */
-      n--;
-      SWAP (a[0], a[n]);
-      for (i = 0; 2*i+1 < n; )
-        {
-          if (2*i+2 < n
-              && fde_compare (ob, a[2*i+2], a[2*i+1]) > 0
-              && fde_compare (ob, a[2*i+2], a[i]) > 0)
-            {
-              SWAP (a[i], a[2*i+2]);
-              i = 2*i+2;
-            }
-          else if (fde_compare (ob, a[2*i+1], a[i]) > 0)
-            {
-              SWAP (a[i], a[2*i+1]);
-              i = 2*i+1;
-            }
-          else
-            break;
-        }
+      SWAP (a[0], a[m]);
+      frame_downheap (ob, fde_compare, a, 0, m);
     }
 #undef SWAP
 }
@@ -533,22 +537,24 @@ fde_merge (struct object *ob, fde_compare_t fde_compare,
           struct fde_vector *v1, struct fde_vector *v2)
 {
   size_t i1, i2;
-  fde * fde2;
+  const fde * fde2;
 
   i2 = v2->count;
   if (i2 > 0)
     {
       i1 = v1->count;
-      do {
-        i2--;
-        fde2 = v2->array[i2];
-        while (i1 > 0 && fde_compare (ob, v1->array[i1-1], fde2) > 0)
-          {
-            v1->array[i1+i2] = v1->array[i1-1];
-            i1--;
-          }
-        v1->array[i1+i2] = fde2;
-      } while (i2 > 0);
+      do
+       {
+         i2--;
+         fde2 = v2->array[i2];
+         while (i1 > 0 && fde_compare (ob, v1->array[i1-1], fde2) > 0)
+           {
+             v1->array[i1+i2] = v1->array[i1-1];
+             i1--;
+           }
+         v1->array[i1+i2] = fde2;
+       }
+      while (i2 > 0);
       v1->count += v2->count;
     }
 }
@@ -558,8 +564,7 @@ end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count)
 {
   fde_compare_t fde_compare;
 
-  if (accu->linear && accu->linear->count != count)
-    abort ();
+  gcc_assert (!accu->linear || accu->linear->count == count);
 
   if (ob->s.b.mixed_encoding)
     fde_compare = fde_mixed_encoding_compare;
@@ -571,8 +576,7 @@ end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count)
   if (accu->erratic)
     {
       fde_split (ob, fde_compare, accu->linear, accu->erratic);
-      if (accu->linear->count + accu->erratic->count != count)
-       abort ();
+      gcc_assert (accu->linear->count + accu->erratic->count == count);
       frame_heapsort (ob, fde_compare, accu->erratic);
       fde_merge (ob, fde_compare, accu->linear, accu->erratic);
       free (accu->erratic);
@@ -586,21 +590,21 @@ end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count)
 }
 
 \f
-/* Update encoding, mixed_encoding, and pc_begin for OB for the 
+/* Update encoding, mixed_encoding, and pc_begin for OB for the
    fde array beginning at THIS_FDE.  Return the number of fdes
    encountered along the way.  */
 
 static size_t
-classify_object_over_fdes (struct object *ob, fde *this_fde)
+classify_object_over_fdes (struct object *ob, const fde *this_fde)
 {
-  struct dwarf_cie *last_cie = 0;
+  const struct dwarf_cie *last_cie = 0;
   size_t count = 0;
   int encoding = DW_EH_PE_absptr;
   _Unwind_Ptr base = 0;
 
-  for (; this_fde->length != 0; this_fde = next_fde (this_fde))
+  for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
     {
-      struct dwarf_cie *this_cie;
+      const struct dwarf_cie *this_cie;
       _Unwind_Ptr mask, pc_begin;
 
       /* Skip CIEs.  */
@@ -638,23 +642,23 @@ classify_object_over_fdes (struct object *ob, fde *this_fde)
        continue;
 
       count += 1;
-      if ((void *)pc_begin < ob->pc_begin)
-       ob->pc_begin = (void *)pc_begin;
+      if ((void *) pc_begin < ob->pc_begin)
+       ob->pc_begin = (void *) pc_begin;
     }
 
   return count;
 }
 
 static void
-add_fdes (struct object *ob, struct fde_accumulator *accu, fde *this_fde)
+add_fdes (struct object *ob, struct fde_accumulator *accu, const fde *this_fde)
 {
-  struct dwarf_cie *last_cie = 0;
+  const struct dwarf_cie *last_cie = 0;
   int encoding = ob->s.b.encoding;
   _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
 
-  for (; this_fde->length != 0; this_fde = next_fde (this_fde))
+  for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
     {
-      struct dwarf_cie *this_cie;
+      const struct dwarf_cie *this_cie;
 
       /* Skip CIEs.  */
       if (this_fde->CIE_delta == 0)
@@ -675,7 +679,7 @@ add_fdes (struct object *ob, struct fde_accumulator *accu, fde *this_fde)
 
       if (encoding == DW_EH_PE_absptr)
        {
-         if (*(_Unwind_Ptr *)this_fde->pc_begin == 0)
+         if (*(const _Unwind_Ptr *) this_fde->pc_begin == 0)
            continue;
        }
       else
@@ -743,7 +747,7 @@ init_object (struct object* ob)
     {
       fde **p;
       for (p = ob->u.array; *p; ++p)
-        add_fdes (ob, &accu, *p);
+       add_fdes (ob, &accu, *p);
     }
   else
     add_fdes (ob, &accu, ob->u.single);
@@ -762,16 +766,16 @@ init_object (struct object* ob)
    used when there was insufficient memory to allocate and sort an
    array.  */
 
-static fde *
-linear_search_fdes (struct object *ob, fde *this_fde, void *pc)
+static const fde *
+linear_search_fdes (struct object *ob, const fde *this_fde, void *pc)
 {
-  struct dwarf_cie *last_cie = 0;
+  const struct dwarf_cie *last_cie = 0;
   int encoding = ob->s.b.encoding;
   _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
 
-  for (; this_fde->length != 0; this_fde = next_fde (this_fde))
+  for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
     {
-      struct dwarf_cie *this_cie;
+      const struct dwarf_cie *this_cie;
       _Unwind_Ptr pc_begin, pc_range;
 
       /* Skip CIEs.  */
@@ -793,15 +797,15 @@ linear_search_fdes (struct object *ob, fde *this_fde, void *pc)
 
       if (encoding == DW_EH_PE_absptr)
        {
-         pc_begin = ((_Unwind_Ptr *)this_fde->pc_begin)[0];
-         pc_range = ((_Unwind_Ptr *)this_fde->pc_begin)[1];
+         pc_begin = ((const _Unwind_Ptr *) this_fde->pc_begin)[0];
+         pc_range = ((const _Unwind_Ptr *) this_fde->pc_begin)[1];
          if (pc_begin == 0)
            continue;
        }
       else
        {
          _Unwind_Ptr mask;
-         const char *p;
+         const unsigned char *p;
 
          p = read_encoded_value_with_base (encoding, base,
                                            this_fde->pc_begin, &pc_begin);
@@ -821,8 +825,8 @@ linear_search_fdes (struct object *ob, fde *this_fde, void *pc)
            continue;
        }
 
-      if ((_Unwind_Ptr)pc - pc_begin < pc_range)
-        return this_fde;
+      if ((_Unwind_Ptr) pc - pc_begin < pc_range)
+       return this_fde;
     }
 
   return NULL;
@@ -831,21 +835,18 @@ linear_search_fdes (struct object *ob, fde *this_fde, void *pc)
 /* Binary search for an FDE containing the given PC.  Here are three
    implementations of increasing complexity.  */
 
-static inline fde *
+static inline const fde *
 binary_search_unencoded_fdes (struct object *ob, void *pc)
 {
   struct fde_vector *vec = ob->u.sort;
   size_t lo, hi;
-      
+
   for (lo = 0, hi = vec->count; lo < hi; )
     {
       size_t i = (lo + hi) / 2;
-      fde *f = vec->array[i];
-      void *pc_begin;
-      uaddr pc_range;
-
-      pc_begin = ((void **)f->pc_begin)[0];
-      pc_range = ((uaddr *)f->pc_begin)[1];
+      const fde *const f = vec->array[i];
+      const void *pc_begin = ((const void *const*) f->pc_begin)[0];
+      const uaddr pc_range = ((const uaddr *) f->pc_begin)[1];
 
       if (pc < pc_begin)
        hi = i;
@@ -858,28 +859,28 @@ binary_search_unencoded_fdes (struct object *ob, void *pc)
   return NULL;
 }
 
-static inline fde *
+static inline const fde *
 binary_search_single_encoding_fdes (struct object *ob, void *pc)
 {
   struct fde_vector *vec = ob->u.sort;
   int encoding = ob->s.b.encoding;
   _Unwind_Ptr base = base_from_object (encoding, ob);
   size_t lo, hi;
-      
+
   for (lo = 0, hi = vec->count; lo < hi; )
     {
       size_t i = (lo + hi) / 2;
-      fde *f = vec->array[i];
+      const fde *f = vec->array[i];
       _Unwind_Ptr pc_begin, pc_range;
-      const char *p;
+      const unsigned char *p;
 
       p = read_encoded_value_with_base (encoding, base, f->pc_begin,
                                        &pc_begin);
       read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
 
-      if ((_Unwind_Ptr)pc < pc_begin)
+      if ((_Unwind_Ptr) pc < pc_begin)
        hi = i;
-      else if ((_Unwind_Ptr)pc >= pc_begin + pc_range)
+      else if ((_Unwind_Ptr) pc >= pc_begin + pc_range)
        lo = i + 1;
       else
        return f;
@@ -888,18 +889,18 @@ binary_search_single_encoding_fdes (struct object *ob, void *pc)
   return NULL;
 }
 
-static inline fde *
+static inline const fde *
 binary_search_mixed_encoding_fdes (struct object *ob, void *pc)
 {
   struct fde_vector *vec = ob->u.sort;
   size_t lo, hi;
-      
+
   for (lo = 0, hi = vec->count; lo < hi; )
     {
       size_t i = (lo + hi) / 2;
-      fde *f = vec->array[i];
+      const fde *f = vec->array[i];
       _Unwind_Ptr pc_begin, pc_range;
-      const char *p;
+      const unsigned char *p;
       int encoding;
 
       encoding = get_fde_encoding (f);
@@ -908,9 +909,9 @@ binary_search_mixed_encoding_fdes (struct object *ob, void *pc)
                                        f->pc_begin, &pc_begin);
       read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
 
-      if ((_Unwind_Ptr)pc < pc_begin)
+      if ((_Unwind_Ptr) pc < pc_begin)
        hi = i;
-      else if ((_Unwind_Ptr)pc >= pc_begin + pc_range)
+      else if ((_Unwind_Ptr) pc >= pc_begin + pc_range)
        lo = i + 1;
       else
        return f;
@@ -919,7 +920,7 @@ binary_search_mixed_encoding_fdes (struct object *ob, void *pc)
   return NULL;
 }
 
-static fde *
+static const fde *
 search_object (struct object* ob, void *pc)
 {
   /* If the data hasn't been sorted, try to do this now.  We may have
@@ -948,14 +949,14 @@ search_object (struct object* ob, void *pc)
     {
       /* Long slow labourious linear search, cos we've no memory.  */
       if (ob->s.b.from_array)
-        {
-          fde **p;
+       {
+         fde **p;
          for (p = ob->u.array; *p ; p++)
            {
-             fde *f = linear_search_fdes (ob, *p, pc);
-              if (f)
+             const fde *f = linear_search_fdes (ob, *p, pc);
+             if (f)
                return f;
-            }
+           }
          return NULL;
        }
       else
@@ -963,11 +964,11 @@ search_object (struct object* ob, void *pc)
     }
 }
 
-fde *
+const fde *
 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
 {
   struct object *ob;
-  fde *f = NULL;
+  const fde *f = NULL;
 
   init_object_mutex_once ();
   __gthread_mutex_lock (&object_mutex);
@@ -1009,6 +1010,7 @@ _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
   if (f)
     {
       int encoding;
+      _Unwind_Ptr func;
 
       bases->tbase = ob->tbase;
       bases->dbase = ob->dbase;
@@ -1017,7 +1019,8 @@ _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
       if (ob->s.b.mixed_encoding)
        encoding = get_fde_encoding (f);
       read_encoded_value_with_base (encoding, base_from_object (encoding, ob),
-                                   f->pc_begin, (_Unwind_Ptr *)&bases->func);
+                                   f->pc_begin, &func);
+      bases->func = (void *) func;
     }
 
   return f;