/* Subroutines needed for unwinding stack frames for exception handling. */
/* Compile this one with gcc. */
-/* Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
Contributed by Jason Merrill <jason@cygnus.com>.
This file is part of GNU CC.
do not apply. */
#include "tconfig.h"
-
-/* We disable this when inhibit_libc, so that gcc can still be built without
- needing header files first. */
-/* ??? This is not a good solution, since prototypes may be required in
- some cases for correct code. See also libgcc2.c. */
-#ifndef inhibit_libc
-/* fixproto guarantees these system headers exist. */
-#include <stdlib.h>
-#include <unistd.h>
-
-#else
-#include <stddef.h>
-#ifndef malloc
-extern void *malloc (size_t);
-#endif
-#ifndef free
-extern void free (void *);
-#endif
-#endif
+#include "tsystem.h"
#include "defaults.h"
#ifdef DWARF2_UNWIND_INFO
#include "dwarf2.h"
-#include <stddef.h>
#include "frame.h"
#include "gthr.h"
fde_vector erratic;
} fde_accumulator;
-static inline void
+static inline int
start_fde_sort (fde_accumulator *accu, size_t count)
{
accu->linear.array = (fde **) malloc (sizeof (fde *) * count);
- accu->erratic.array = (fde **) malloc (sizeof (fde *) * count);
+ accu->erratic.array = accu->linear.array ?
+ (fde **) malloc (sizeof (fde *) * count) : NULL;
accu->linear.count = 0;
accu->erratic.count = 0;
+
+ return accu->linear.array != NULL;
}
static inline void
fde_insert (fde_accumulator *accu, fde *this_fde)
{
- accu->linear.array[accu->linear.count++] = this_fde;
+ if (accu->linear.array)
+ accu->linear.array[accu->linear.count++] = this_fde;
}
/* Split LINEAR into a linear sequence with low values and an erratic
static fde **
end_fde_sort (fde_accumulator *accu, size_t count)
{
- if (accu->linear.count != count)
- abort ();
- fde_split (&accu->linear, &accu->erratic);
- if (accu->linear.count + accu->erratic.count != count)
+ if (accu->linear.array && accu->linear.count != count)
abort ();
- frame_heapsort (&accu->erratic);
- fde_merge (&accu->linear, &accu->erratic);
- free (accu->erratic.array);
+
+ if (accu->erratic.array)
+ {
+ fde_split (&accu->linear, &accu->erratic);
+ if (accu->linear.count + accu->erratic.count != count)
+ abort ();
+ frame_heapsort (&accu->erratic);
+ fde_merge (&accu->linear, &accu->erratic);
+ if (accu->erratic.array)
+ free (accu->erratic.array);
+ }
+ else
+ {
+ /* We've not managed to malloc an erratic array, so heap sort in the
+ linear one. */
+ frame_heapsort (&accu->linear);
+ }
return accu->linear.array;
}
*end_ptr = pc_end;
}
+/* search this fde table for the one containing the pc */
+static fde *
+search_fdes (fde *this_fde, void *pc)
+{
+ for (; this_fde->length != 0; this_fde = next_fde (this_fde))
+ {
+ /* Skip CIEs and linked once FDE entries. */
+ if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
+ continue;
+
+ if ((uaddr)((char *)pc - (char *)this_fde->pc_begin) < this_fde->pc_range)
+ return this_fde;
+ }
+ return NULL;
+}
+
/* Set up a sorted array of pointers to FDEs for a loaded object. We
count up the entries before allocating the array because it's likely to
- be faster. */
+ be faster. We can be called multiple times, should we have failed to
+ allocate a sorted fde array on a previous occasion. */
static void
frame_init (struct object* ob)
size_t count;
fde_accumulator accu;
void *pc_begin, *pc_end;
+ fde **array;
- if (ob->fde_array)
+ if (ob->pc_begin)
+ count = ob->count;
+ else if (ob->fde_array)
{
fde **p = ob->fde_array;
for (count = 0; *p; ++p)
}
else
count = count_fdes (ob->fde_begin);
-
ob->count = count;
- start_fde_sort (&accu, count);
+ if (!start_fde_sort (&accu, count) && ob->pc_begin)
+ return;
+
pc_begin = (void*)(uaddr)-1;
pc_end = 0;
else
add_fdes (ob->fde_begin, &accu, &pc_begin, &pc_end);
- ob->fde_array = end_fde_sort (&accu, count);
+ array = end_fde_sort (&accu, count);
+ if (array)
+ ob->fde_array = array;
ob->pc_begin = pc_begin;
ob->pc_end = pc_end;
}
init_object_mutex_once ();
__gthread_mutex_lock (&object_mutex);
+ /* Linear search through the objects, to find the one containing the pc. */
for (ob = objects; ob; ob = ob->next)
{
if (ob->pc_begin == 0)
break;
}
- __gthread_mutex_unlock (&object_mutex);
-
if (ob == 0)
- return 0;
-
- /* Standard binary search algorithm. */
- for (lo = 0, hi = ob->count; lo < hi; )
{
- size_t i = (lo + hi) / 2;
- fde *f = ob->fde_array[i];
+ __gthread_mutex_unlock (&object_mutex);
+ return 0;
+ }
- if (pc < f->pc_begin)
- hi = i;
- else if (pc >= f->pc_begin + f->pc_range)
- lo = i + 1;
+ if (!ob->fde_array || (void *)ob->fde_array == (void *)ob->fde_begin)
+ frame_init (ob);
+
+ if (ob->fde_array && (void *)ob->fde_array != (void *)ob->fde_begin)
+ {
+ __gthread_mutex_unlock (&object_mutex);
+
+ /* Standard binary search algorithm. */
+ for (lo = 0, hi = ob->count; lo < hi; )
+ {
+ size_t i = (lo + hi) / 2;
+ fde *f = ob->fde_array[i];
+
+ if (pc < f->pc_begin)
+ hi = i;
+ else if (pc >= f->pc_begin + f->pc_range)
+ lo = i + 1;
+ else
+ return f;
+ }
+ }
+ else
+ {
+ /* Long slow labourious linear search, cos we've no memory. */
+ fde *f;
+
+ if (ob->fde_array)
+ {
+ fde **p = ob->fde_array;
+
+ do
+ {
+ f = search_fdes (*p, pc);
+ if (f)
+ break;
+ p++;
+ }
+ while (*p);
+ }
else
- return f;
+ f = search_fdes (ob->fde_begin, pc);
+ __gthread_mutex_unlock (&object_mutex);
+ return f;
}
-
return 0;
}
\f
{
reg = (insn & 0x3f);
p = decode_uleb128 (p, &offset);
- offset *= info->data_align;
- state->s.saved[reg] = REG_SAVED_OFFSET;
- state->s.reg_or_offset[reg] = offset;
+ if (reg == state->s.cfa_reg)
+ /* Don't record anything about this register; it's only used to
+ reload SP in the epilogue. We don't want to copy in SP
+ values for outer frames; we handle restoring SP specially. */;
+ else
+ {
+ offset *= info->data_align;
+ state->s.saved[reg] = REG_SAVED_OFFSET;
+ state->s.reg_or_offset[reg] = offset;
+ }
}
else if (insn & DW_CFA_restore)
{
case DW_CFA_offset_extended:
p = decode_uleb128 (p, ®);
p = decode_uleb128 (p, &offset);
- offset *= info->data_align;
- state->s.saved[reg] = REG_SAVED_OFFSET;
- state->s.reg_or_offset[reg] = offset;
+ if (reg == state->s.cfa_reg)
+ /* Don't record anything; see above. */;
+ else
+ {
+ offset *= info->data_align;
+ state->s.saved[reg] = REG_SAVED_OFFSET;
+ state->s.reg_or_offset[reg] = offset;
+ }
break;
case DW_CFA_restore_extended:
p = decode_uleb128 (p, ®);
state->s.args_size = offset;
break;
+ case DW_CFA_GNU_negative_offset_extended:
+ p = decode_uleb128 (p, ®);
+ p = decode_uleb128 (p, &offset);
+ offset *= info->data_align;
+ state->s.saved[reg] = REG_SAVED_OFFSET;
+ state->s.reg_or_offset[reg] = -offset;
+ break;
+
default:
abort ();
}
*p = (*p)->next;
/* If we've run init_frame for this object, free the FDE array. */
- if (ob->pc_begin)
+ if (ob->fde_array && ob->fde_array != begin)
free (ob->fde_array);
__gthread_mutex_unlock (&object_mutex);