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 unsigned int uword __attribute__ ((mode (SI)));
50 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
51 typedef int saddr __attribute__ ((mode (pointer)));
52 typedef unsigned char ubyte;
54 /* The first few fields of a CIE. The CIE_id field is 0xffffffff for a CIE,
55 to distinguish it from a valid FDE. FDEs are aligned to an addressing
56 unit boundary, but the fields within are unaligned. */
63 } __attribute__ ((packed, aligned (__alignof__ (void *))));
65 /* The first few fields of an FDE. */
69 struct dwarf_cie* CIE_pointer;
72 } __attribute__ ((packed, aligned (__alignof__ (void *))));
74 typedef struct dwarf_fde fde;
76 /* The representation for an "object" to be searched for frame unwind info.
77 For targets with named sections, one object is an executable or shared
78 library; for other targets, one object is one translation unit. */
89 static struct object *objects;
91 /* The information we care about from a CIE. */
100 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
102 struct frame_state_internal
104 struct frame_state s;
105 struct frame_state_internal *saved_state;
108 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
109 by R, and return the new value of BUF. */
112 decode_uleb128 (unsigned char *buf, unsigned *r)
119 unsigned byte = *buf++;
120 result |= (byte & 0x7f) << shift;
121 if ((byte & 0x80) == 0)
129 /* Decode the signed LEB128 constant at BUF into the variable pointed to
130 by R, and return the new value of BUF. */
133 decode_sleb128 (unsigned char *buf, int *r)
142 result |= (byte & 0x7f) << shift;
144 if ((byte & 0x80) == 0)
147 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
148 result |= - (1 << shift);
154 /* Read unaligned data from the instruction buffer. */
158 unsigned b2 __attribute__ ((mode (HI)));
159 unsigned b4 __attribute__ ((mode (SI)));
160 unsigned b8 __attribute__ ((mode (DI)));
161 } __attribute__ ((packed));
163 read_pointer (void *p)
164 { union unaligned *up = p; return up->p; }
165 static inline unsigned
167 { return *(unsigned char *)p; }
168 static inline unsigned
170 { union unaligned *up = p; return up->b2; }
171 static inline unsigned
173 { union unaligned *up = p; return up->b4; }
174 static inline unsigned long
176 { union unaligned *up = p; return up->b8; }
178 /* Ordering function for FDEs. Functions can't overlap, so we just compare
179 their starting addresses. */
182 fde_compare (fde *x, fde *y)
184 return (saddr)x->pc_begin - (saddr)y->pc_begin;
187 /* Return the address of the FDE after P. */
192 return (fde *)(((char *)p) + p->length + sizeof (p->length));
195 /* One iteration of an insertion sort, for adding new FDEs to the array.
196 Usually the new FDE will go in at the end, so we can expect close to
197 O(n) performance. If this turns out to be overly optimistic, we can have
198 the linker sort the FDEs so we don't have to do it at run time. */
201 fde_insert (fde **array, size_t i, fde *this_fde)
205 for (; i > 0 && fde_compare (array[i], array[i-1]) < 0; --i)
208 array[i] = array[i-1];
209 array[i-1] = this_fde;
214 count_fdes (fde *this_fde)
218 for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
221 if ((uaddr)(this_fde->CIE_pointer) == (uaddr)-1)
231 add_fdes (fde *this_fde, fde **array, size_t *i_ptr,
232 void **beg_ptr, void **end_ptr)
235 void *pc_begin = *beg_ptr;
236 void *pc_end = *end_ptr;
238 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
241 if ((uaddr)(this_fde->CIE_pointer) == (uaddr)-1)
244 fde_insert (array, i++, this_fde);
246 if (this_fde->pc_begin < pc_begin)
247 pc_begin = this_fde->pc_begin;
248 if (this_fde->pc_begin + this_fde->pc_range > pc_end)
249 pc_end = this_fde->pc_begin + this_fde->pc_range;
257 /* Set up a sorted array of pointers to FDEs for a loaded object. We
258 count up the entries before allocating the array because it's likely to
262 frame_init (struct object* ob)
267 void *pc_begin, *pc_end;
271 fde **p = ob->fde_array;
272 for (count = 0; *p; ++p)
273 count += count_fdes (*p);
276 count = count_fdes (ob->fde_begin);
279 array = (fde **) malloc (sizeof (fde *) * count);
281 pc_begin = (void*)(uaddr)-1;
287 fde **p = ob->fde_array;
289 add_fdes (*p, array, &count, &pc_begin, &pc_end);
292 add_fdes (ob->fde_begin, array, &count, &pc_begin, &pc_end);
294 ob->fde_array = array;
295 ob->pc_begin = pc_begin;
299 /* Return a pointer to the FDE for the function containing PC. */
307 for (ob = objects; ob; ob = ob->next)
309 if (ob->pc_begin == 0)
311 if (pc >= ob->pc_begin && pc < ob->pc_end)
318 /* Standard binary search algorithm. */
319 for (lo = 0, hi = ob->count; lo < hi; )
321 size_t i = (lo + hi) / 2;
322 fde *f = ob->fde_array[i];
324 if (pc < f->pc_begin)
326 else if (pc > f->pc_begin + f->pc_range)
335 /* Extract any interesting information from the CIE for the translation
336 unit F belongs to. */
339 extract_cie_info (fde *f, struct cie_info *c)
344 c->augmentation = f->CIE_pointer->augmentation;
346 if (strcmp (c->augmentation, "") != 0
347 && strcmp (c->augmentation, "e") != 0
348 && c->augmentation[0] != 'z')
351 p = c->augmentation + strlen (c->augmentation) + 1;
353 p = decode_uleb128 (p, &c->code_align);
354 p = decode_sleb128 (p, &c->data_align);
355 c->ra_regno = *(unsigned char *)p++;
357 /* If the augmentation starts with 'z', we now see the length of the
358 augmentation fields. */
359 if (c->augmentation[0] == 'z')
361 p = decode_uleb128 (p, &i);
368 /* Decode one instruction's worth of of DWARF 2 call frame information.
369 Used by __frame_state_for. Takes pointers P to the instruction to
370 decode, STATE to the current register unwind information, INFO to the
371 current CIE information, and PC to the current PC value. Returns a
372 pointer to the next instruction. */
375 execute_cfa_insn (void *p, struct frame_state_internal *state,
376 struct cie_info *info, void **pc)
378 unsigned insn = *(unsigned char *)p++;
382 if (insn & DW_CFA_advance_loc)
383 *pc += ((insn & 0x3f) * info->code_align);
384 else if (insn & DW_CFA_offset)
387 p = decode_uleb128 (p, &offset);
388 offset *= info->data_align;
389 state->s.saved[reg] = REG_SAVED_OFFSET;
390 state->s.reg_or_offset[reg] = offset;
392 else if (insn & DW_CFA_restore)
395 state->s.saved[reg] = REG_UNSAVED;
400 *pc = read_pointer (p);
401 p += sizeof (void *);
403 case DW_CFA_advance_loc1:
404 *pc += read_1byte (p);
407 case DW_CFA_advance_loc2:
408 *pc += read_2byte (p);
411 case DW_CFA_advance_loc4:
412 *pc += read_4byte (p);
416 case DW_CFA_offset_extended:
417 p = decode_uleb128 (p, ®);
418 p = decode_uleb128 (p, &offset);
419 offset *= info->data_align;
420 state->s.saved[reg] = REG_SAVED_OFFSET;
421 state->s.reg_or_offset[reg] = offset;
423 case DW_CFA_restore_extended:
424 p = decode_uleb128 (p, ®);
425 state->s.saved[reg] = REG_UNSAVED;
428 case DW_CFA_undefined:
429 case DW_CFA_same_value:
433 case DW_CFA_register:
436 p = decode_uleb128 (p, ®);
437 p = decode_uleb128 (p, ®2);
438 state->s.saved[reg] = REG_SAVED_REG;
439 state->s.reg_or_offset[reg] = reg2;
444 p = decode_uleb128 (p, ®);
445 p = decode_uleb128 (p, &offset);
446 state->s.cfa_reg = reg;
447 state->s.cfa_offset = offset;
449 case DW_CFA_def_cfa_register:
450 p = decode_uleb128 (p, ®);
451 state->s.cfa_reg = reg;
453 case DW_CFA_def_cfa_offset:
454 p = decode_uleb128 (p, &offset);
455 state->s.cfa_offset = offset;
458 case DW_CFA_remember_state:
460 struct frame_state_internal *save =
461 (struct frame_state_internal *)
462 malloc (sizeof (struct frame_state_internal));
463 memcpy (save, state, sizeof (struct frame_state_internal));
464 state->saved_state = save;
467 case DW_CFA_restore_state:
469 struct frame_state_internal *save = state->saved_state;
470 memcpy (state, save, sizeof (struct frame_state_internal));
475 /* FIXME: Hardcoded for SPARC register window configuration. */
476 case DW_CFA_GNU_window_save:
477 for (reg = 16; reg < 32; ++reg)
479 state->s.saved[reg] = REG_SAVED_OFFSET;
480 state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
484 case DW_CFA_GNU_args_size:
485 p = decode_uleb128 (p, &offset);
486 state->s.args_size = offset;
495 /* Called from crtbegin.o to register the unwind info for an object. */
498 __register_frame (void *begin)
500 struct object *ob = (struct object *) malloc (sizeof (struct object));
502 ob->fde_begin = begin;
504 ob->pc_begin = ob->pc_end = 0;
512 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
513 for different translation units. Called from the file generated by
517 __register_frame_table (void *begin)
519 struct object *ob = (struct object *) malloc (sizeof (struct object));
521 ob->fde_begin = begin;
522 ob->fde_array = begin;
524 ob->pc_begin = ob->pc_end = 0;
531 /* Called from crtend.o to deregister the unwind info for an object. */
534 __deregister_frame (void *begin)
536 struct object **p = &objects;
540 if ((*p)->fde_begin == begin)
542 struct object *ob = *p;
545 /* If we've run init_frame for this object, free the FDE array. */
547 free (ob->fde_array);
557 /* Called from __throw to find the registers to restore for a given
558 PC_TARGET. The caller should allocate a local variable of `struct
559 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
562 __frame_state_for (void *pc_target, struct frame_state *state_in)
565 void *insn, *end, *pc;
566 struct cie_info info;
567 struct frame_state_internal state;
569 f = find_fde (pc_target);
573 insn = extract_cie_info (f, &info);
577 memset (&state, 0, sizeof (state));
578 state.s.retaddr_column = info.ra_regno;
580 /* First decode all the insns in the CIE. */
581 end = next_fde ((fde*) f->CIE_pointer);
583 insn = execute_cfa_insn (insn, &state, &info, 0);
585 insn = ((fde *)f) + 1;
587 if (info.augmentation[0] == 'z')
590 insn = decode_uleb128 (insn, &i);
593 else if (strcmp (info.augmentation, "e") == 0)
595 state.s.eh_ptr = read_pointer (insn);
596 insn += sizeof (void *);
599 /* Then the insns in the FDE up to our target PC. */
602 while (insn < end && pc < pc_target)
603 insn = execute_cfa_insn (insn, &state, &info, &pc);
605 memcpy (state_in, &state.s, sizeof (state.s));
608 #endif /* DWARF2_UNWIND_INFO */