1 /* Subroutines needed for unwinding stack frames for exception handling. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997 Free Software Foundation, Inc.
4 Contributed by Jason Merrill <jason@cygnus.com>.
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* As a special exception, if you link this library with other files,
24 some of which are compiled with GCC, to produce an executable,
25 this library does not by itself cause the resulting executable
26 to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 /* It is incorrect to include config.h here, because this file is being
31 compiled for the target, and hence definitions concerning only the host
37 #ifdef DWARF2_UNWIND_INFO
42 /* Don't use `fancy_abort' here even if config.h says to use it. */
47 /* Some types used by the DWARF 2 spec. */
49 typedef int sword __attribute__ ((mode (SI)));
50 typedef unsigned int uword __attribute__ ((mode (SI)));
51 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
52 typedef int saddr __attribute__ ((mode (pointer)));
53 typedef unsigned char ubyte;
55 /* The first few fields of a CIE. The CIE_id field is 0xffffffff for a CIE,
56 to distinguish it from a valid FDE. FDEs are aligned to an addressing
57 unit boundary, but the fields within are unaligned. */
64 } __attribute__ ((packed, aligned (__alignof__ (void *))));
66 /* The first few fields of an FDE. */
73 } __attribute__ ((packed, aligned (__alignof__ (void *))));
75 typedef struct dwarf_fde fde;
77 /* The representation for an "object" to be searched for frame unwind info.
78 For targets with named sections, one object is an executable or shared
79 library; for other targets, one object is one translation unit. */
90 static struct object *objects;
92 /* The information we care about from a CIE. */
102 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
104 struct frame_state_internal
106 struct frame_state s;
107 struct frame_state_internal *saved_state;
110 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
111 by R, and return the new value of BUF. */
114 decode_uleb128 (unsigned char *buf, unsigned *r)
121 unsigned byte = *buf++;
122 result |= (byte & 0x7f) << shift;
123 if ((byte & 0x80) == 0)
131 /* Decode the signed LEB128 constant at BUF into the variable pointed to
132 by R, and return the new value of BUF. */
135 decode_sleb128 (unsigned char *buf, int *r)
144 result |= (byte & 0x7f) << shift;
146 if ((byte & 0x80) == 0)
149 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
150 result |= - (1 << shift);
156 /* Read unaligned data from the instruction buffer. */
160 unsigned b2 __attribute__ ((mode (HI)));
161 unsigned b4 __attribute__ ((mode (SI)));
162 unsigned b8 __attribute__ ((mode (DI)));
163 } __attribute__ ((packed));
165 read_pointer (void *p)
166 { union unaligned *up = p; return up->p; }
167 static inline unsigned
169 { return *(unsigned char *)p; }
170 static inline unsigned
172 { union unaligned *up = p; return up->b2; }
173 static inline unsigned
175 { union unaligned *up = p; return up->b4; }
176 static inline unsigned long
178 { union unaligned *up = p; return up->b8; }
180 /* Ordering function for FDEs. Functions can't overlap, so we just compare
181 their starting addresses. */
184 fde_compare (fde *x, fde *y)
186 return (saddr)x->pc_begin - (saddr)y->pc_begin;
189 /* Return the address of the FDE after P. */
194 return (fde *)(((char *)p) + p->length + sizeof (p->length));
197 /* One iteration of an insertion sort, for adding new FDEs to the array.
198 Usually the new FDE will go in at the end, so we can expect close to
199 O(n) performance. If this turns out to be overly optimistic, we can have
200 the linker sort the FDEs so we don't have to do it at run time. */
203 fde_insert (fde **array, size_t i, fde *this_fde)
207 for (; i > 0 && fde_compare (array[i], array[i-1]) < 0; --i)
210 array[i] = array[i-1];
211 array[i-1] = this_fde;
216 count_fdes (fde *this_fde)
220 for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
222 /* Skip CIEs and linked once FDE entries. */
223 if (this_fde->CIE_delta == 0 || this_fde->pc_range == 0)
233 add_fdes (fde *this_fde, fde **array, size_t *i_ptr,
234 void **beg_ptr, void **end_ptr)
237 void *pc_begin = *beg_ptr;
238 void *pc_end = *end_ptr;
240 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
242 /* Skip CIEs and linked once FDE entries. */
243 if (this_fde->CIE_delta == 0 || this_fde->pc_range == 0)
246 fde_insert (array, i++, this_fde);
248 if (this_fde->pc_begin < pc_begin)
249 pc_begin = this_fde->pc_begin;
250 if (this_fde->pc_begin + this_fde->pc_range > pc_end)
251 pc_end = this_fde->pc_begin + this_fde->pc_range;
259 /* Set up a sorted array of pointers to FDEs for a loaded object. We
260 count up the entries before allocating the array because it's likely to
264 frame_init (struct object* ob)
269 void *pc_begin, *pc_end;
273 fde **p = ob->fde_array;
274 for (count = 0; *p; ++p)
275 count += count_fdes (*p);
278 count = count_fdes (ob->fde_begin);
281 array = (fde **) malloc (sizeof (fde *) * count);
283 pc_begin = (void*)(uaddr)-1;
289 fde **p = ob->fde_array;
291 add_fdes (*p, array, &count, &pc_begin, &pc_end);
294 add_fdes (ob->fde_begin, array, &count, &pc_begin, &pc_end);
296 ob->fde_array = array;
297 ob->pc_begin = pc_begin;
301 /* Return a pointer to the FDE for the function containing PC. */
309 for (ob = objects; ob; ob = ob->next)
311 if (ob->pc_begin == 0)
313 if (pc >= ob->pc_begin && pc < ob->pc_end)
320 /* Standard binary search algorithm. */
321 for (lo = 0, hi = ob->count; lo < hi; )
323 size_t i = (lo + hi) / 2;
324 fde *f = ob->fde_array[i];
326 if (pc < f->pc_begin)
328 else if (pc > f->pc_begin + f->pc_range)
337 static inline struct dwarf_cie *
340 return ((void *)&f->CIE_delta) - f->CIE_delta;
343 /* Extract any interesting information from the CIE for the translation
344 unit F belongs to. */
347 extract_cie_info (fde *f, struct cie_info *c)
352 c->augmentation = get_cie (f)->augmentation;
354 if (strcmp (c->augmentation, "") != 0
355 && strcmp (c->augmentation, "eh") != 0
356 && c->augmentation[0] != 'z')
359 p = c->augmentation + strlen (c->augmentation) + 1;
361 if (strcmp (c->augmentation, "eh") == 0)
363 c->eh_ptr = read_pointer (p);
364 p += sizeof (void *);
369 p = decode_uleb128 (p, &c->code_align);
370 p = decode_sleb128 (p, &c->data_align);
371 c->ra_regno = *(unsigned char *)p++;
373 /* If the augmentation starts with 'z', we now see the length of the
374 augmentation fields. */
375 if (c->augmentation[0] == 'z')
377 p = decode_uleb128 (p, &i);
384 /* Decode one instruction's worth of of DWARF 2 call frame information.
385 Used by __frame_state_for. Takes pointers P to the instruction to
386 decode, STATE to the current register unwind information, INFO to the
387 current CIE information, and PC to the current PC value. Returns a
388 pointer to the next instruction. */
391 execute_cfa_insn (void *p, struct frame_state_internal *state,
392 struct cie_info *info, void **pc)
394 unsigned insn = *(unsigned char *)p++;
398 if (insn & DW_CFA_advance_loc)
399 *pc += ((insn & 0x3f) * info->code_align);
400 else if (insn & DW_CFA_offset)
403 p = decode_uleb128 (p, &offset);
404 offset *= info->data_align;
405 state->s.saved[reg] = REG_SAVED_OFFSET;
406 state->s.reg_or_offset[reg] = offset;
408 else if (insn & DW_CFA_restore)
411 state->s.saved[reg] = REG_UNSAVED;
416 *pc = read_pointer (p);
417 p += sizeof (void *);
419 case DW_CFA_advance_loc1:
420 *pc += read_1byte (p);
423 case DW_CFA_advance_loc2:
424 *pc += read_2byte (p);
427 case DW_CFA_advance_loc4:
428 *pc += read_4byte (p);
432 case DW_CFA_offset_extended:
433 p = decode_uleb128 (p, ®);
434 p = decode_uleb128 (p, &offset);
435 offset *= info->data_align;
436 state->s.saved[reg] = REG_SAVED_OFFSET;
437 state->s.reg_or_offset[reg] = offset;
439 case DW_CFA_restore_extended:
440 p = decode_uleb128 (p, ®);
441 state->s.saved[reg] = REG_UNSAVED;
444 case DW_CFA_undefined:
445 case DW_CFA_same_value:
449 case DW_CFA_register:
452 p = decode_uleb128 (p, ®);
453 p = decode_uleb128 (p, ®2);
454 state->s.saved[reg] = REG_SAVED_REG;
455 state->s.reg_or_offset[reg] = reg2;
460 p = decode_uleb128 (p, ®);
461 p = decode_uleb128 (p, &offset);
462 state->s.cfa_reg = reg;
463 state->s.cfa_offset = offset;
465 case DW_CFA_def_cfa_register:
466 p = decode_uleb128 (p, ®);
467 state->s.cfa_reg = reg;
469 case DW_CFA_def_cfa_offset:
470 p = decode_uleb128 (p, &offset);
471 state->s.cfa_offset = offset;
474 case DW_CFA_remember_state:
476 struct frame_state_internal *save =
477 (struct frame_state_internal *)
478 malloc (sizeof (struct frame_state_internal));
479 memcpy (save, state, sizeof (struct frame_state_internal));
480 state->saved_state = save;
483 case DW_CFA_restore_state:
485 struct frame_state_internal *save = state->saved_state;
486 memcpy (state, save, sizeof (struct frame_state_internal));
491 /* FIXME: Hardcoded for SPARC register window configuration. */
492 case DW_CFA_GNU_window_save:
493 for (reg = 16; reg < 32; ++reg)
495 state->s.saved[reg] = REG_SAVED_OFFSET;
496 state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
500 case DW_CFA_GNU_args_size:
501 p = decode_uleb128 (p, &offset);
502 state->s.args_size = offset;
511 /* Called from crtbegin.o to register the unwind info for an object. */
514 __register_frame (void *begin)
516 struct object *ob = (struct object *) malloc (sizeof (struct object));
518 ob->fde_begin = begin;
520 ob->pc_begin = ob->pc_end = 0;
528 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
529 for different translation units. Called from the file generated by
533 __register_frame_table (void *begin)
535 struct object *ob = (struct object *) malloc (sizeof (struct object));
537 ob->fde_begin = begin;
538 ob->fde_array = begin;
540 ob->pc_begin = ob->pc_end = 0;
547 /* Called from crtend.o to deregister the unwind info for an object. */
550 __deregister_frame (void *begin)
552 struct object **p = &objects;
556 if ((*p)->fde_begin == begin)
558 struct object *ob = *p;
561 /* If we've run init_frame for this object, free the FDE array. */
563 free (ob->fde_array);
573 /* Called from __throw to find the registers to restore for a given
574 PC_TARGET. The caller should allocate a local variable of `struct
575 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
578 __frame_state_for (void *pc_target, struct frame_state *state_in)
581 void *insn, *end, *pc;
582 struct cie_info info;
583 struct frame_state_internal state;
585 f = find_fde (pc_target);
589 insn = extract_cie_info (f, &info);
593 memset (&state, 0, sizeof (state));
594 state.s.retaddr_column = info.ra_regno;
595 state.s.eh_ptr = info.eh_ptr;
597 /* First decode all the insns in the CIE. */
598 end = next_fde ((fde*) get_cie (f));
600 insn = execute_cfa_insn (insn, &state, &info, 0);
602 insn = ((fde *)f) + 1;
604 if (info.augmentation[0] == 'z')
607 insn = decode_uleb128 (insn, &i);
611 /* Then the insns in the FDE up to our target PC. */
614 while (insn < end && pc <= pc_target)
615 insn = execute_cfa_insn (insn, &state, &info, &pc);
617 memcpy (state_in, &state.s, sizeof (state.s));
620 #endif /* DWARF2_UNWIND_INFO */