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
38 #include "gansidecl.h"
44 #ifdef __GTHREAD_MUTEX_INIT
45 static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
47 static __gthread_mutex_t object_mutex;
50 /* Don't use `fancy_abort' here even if config.h says to use it. */
55 /* Some types used by the DWARF 2 spec. */
57 typedef int sword __attribute__ ((mode (SI)));
58 typedef unsigned int uword __attribute__ ((mode (SI)));
59 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
60 typedef int saddr __attribute__ ((mode (pointer)));
61 typedef unsigned char ubyte;
63 /* The first few fields of a CIE. The CIE_id field is 0xffffffff for a CIE,
64 to distinguish it from a valid FDE. FDEs are aligned to an addressing
65 unit boundary, but the fields within are unaligned. */
72 } __attribute__ ((packed, aligned (__alignof__ (void *))));
74 /* The first few fields of an FDE. */
81 } __attribute__ ((packed, aligned (__alignof__ (void *))));
83 typedef struct dwarf_fde fde;
85 /* Objects to be searched for frame unwind info. */
87 static struct object *objects;
89 /* The information we care about from a CIE. */
99 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
101 struct frame_state_internal
103 struct frame_state s;
104 struct frame_state_internal *saved_state;
107 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
108 by R, and return the new value of BUF. */
111 decode_uleb128 (unsigned char *buf, unsigned *r)
118 unsigned byte = *buf++;
119 result |= (byte & 0x7f) << shift;
120 if ((byte & 0x80) == 0)
128 /* Decode the signed LEB128 constant at BUF into the variable pointed to
129 by R, and return the new value of BUF. */
132 decode_sleb128 (unsigned char *buf, int *r)
141 result |= (byte & 0x7f) << shift;
143 if ((byte & 0x80) == 0)
146 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
147 result |= - (1 << shift);
153 /* Read unaligned data from the instruction buffer. */
157 unsigned b2 __attribute__ ((mode (HI)));
158 unsigned b4 __attribute__ ((mode (SI)));
159 unsigned b8 __attribute__ ((mode (DI)));
160 } __attribute__ ((packed));
162 read_pointer (void *p)
163 { union unaligned *up = p; return up->p; }
164 static inline unsigned
166 { return *(unsigned char *)p; }
167 static inline unsigned
169 { union unaligned *up = p; return up->b2; }
170 static inline unsigned
172 { union unaligned *up = p; return up->b4; }
173 static inline unsigned long
175 { union unaligned *up = p; return up->b8; }
177 /* Ordering function for FDEs. Functions can't overlap, so we just compare
178 their starting addresses. */
181 fde_compare (fde *x, fde *y)
183 return (saddr)x->pc_begin - (saddr)y->pc_begin;
186 /* Return the address of the FDE after P. */
191 return (fde *)(((char *)p) + p->length + sizeof (p->length));
194 /* One iteration of an insertion sort, for adding new FDEs to the array.
195 Usually the new FDE will go in at the end, so we can expect close to
196 O(n) performance. If this turns out to be overly optimistic, we can have
197 the linker sort the FDEs so we don't have to do it at run time. */
200 fde_insert (fde **array, size_t i, fde *this_fde)
204 for (; i > 0 && fde_compare (array[i], array[i-1]) < 0; --i)
207 array[i] = array[i-1];
208 array[i-1] = this_fde;
213 count_fdes (fde *this_fde)
217 for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
219 /* Skip CIEs and linked once FDE entries. */
220 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
230 add_fdes (fde *this_fde, fde **array, size_t *i_ptr,
231 void **beg_ptr, void **end_ptr)
234 void *pc_begin = *beg_ptr;
235 void *pc_end = *end_ptr;
237 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
239 /* Skip CIEs and linked once FDE entries. */
240 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
243 fde_insert (array, i++, this_fde);
245 if (this_fde->pc_begin < pc_begin)
246 pc_begin = this_fde->pc_begin;
247 if (this_fde->pc_begin + this_fde->pc_range > pc_end)
248 pc_end = this_fde->pc_begin + this_fde->pc_range;
256 /* Set up a sorted array of pointers to FDEs for a loaded object. We
257 count up the entries before allocating the array because it's likely to
261 frame_init (struct object* ob)
266 void *pc_begin, *pc_end;
270 fde **p = ob->fde_array;
271 for (count = 0; *p; ++p)
272 count += count_fdes (*p);
275 count = count_fdes (ob->fde_begin);
278 array = (fde **) malloc (sizeof (fde *) * count);
280 pc_begin = (void*)(uaddr)-1;
286 fde **p = ob->fde_array;
288 add_fdes (*p, array, &count, &pc_begin, &pc_end);
291 add_fdes (ob->fde_begin, array, &count, &pc_begin, &pc_end);
293 ob->fde_array = array;
294 ob->pc_begin = pc_begin;
298 /* Return a pointer to the FDE for the function containing PC. */
306 __gthread_mutex_lock (&object_mutex);
308 for (ob = objects; ob; ob = ob->next)
310 if (ob->pc_begin == 0)
312 if (pc >= ob->pc_begin && pc < ob->pc_end)
316 __gthread_mutex_unlock (&object_mutex);
321 /* Standard binary search algorithm. */
322 for (lo = 0, hi = ob->count; lo < hi; )
324 size_t i = (lo + hi) / 2;
325 fde *f = ob->fde_array[i];
327 if (pc < f->pc_begin)
329 else if (pc >= f->pc_begin + f->pc_range)
338 static inline struct dwarf_cie *
341 return ((void *)&f->CIE_delta) - f->CIE_delta;
344 /* Extract any interesting information from the CIE for the translation
345 unit F belongs to. */
348 extract_cie_info (fde *f, struct cie_info *c)
353 c->augmentation = get_cie (f)->augmentation;
355 if (strcmp (c->augmentation, "") != 0
356 && strcmp (c->augmentation, "eh") != 0
357 && c->augmentation[0] != 'z')
360 p = c->augmentation + strlen (c->augmentation) + 1;
362 if (strcmp (c->augmentation, "eh") == 0)
364 c->eh_ptr = read_pointer (p);
365 p += sizeof (void *);
370 p = decode_uleb128 (p, &c->code_align);
371 p = decode_sleb128 (p, &c->data_align);
372 c->ra_regno = *(unsigned char *)p++;
374 /* If the augmentation starts with 'z', we now see the length of the
375 augmentation fields. */
376 if (c->augmentation[0] == 'z')
378 p = decode_uleb128 (p, &i);
385 /* Decode one instruction's worth of of DWARF 2 call frame information.
386 Used by __frame_state_for. Takes pointers P to the instruction to
387 decode, STATE to the current register unwind information, INFO to the
388 current CIE information, and PC to the current PC value. Returns a
389 pointer to the next instruction. */
392 execute_cfa_insn (void *p, struct frame_state_internal *state,
393 struct cie_info *info, void **pc)
395 unsigned insn = *(unsigned char *)p++;
399 if (insn & DW_CFA_advance_loc)
400 *pc += ((insn & 0x3f) * info->code_align);
401 else if (insn & DW_CFA_offset)
404 p = decode_uleb128 (p, &offset);
405 offset *= info->data_align;
406 state->s.saved[reg] = REG_SAVED_OFFSET;
407 state->s.reg_or_offset[reg] = offset;
409 else if (insn & DW_CFA_restore)
412 state->s.saved[reg] = REG_UNSAVED;
417 *pc = read_pointer (p);
418 p += sizeof (void *);
420 case DW_CFA_advance_loc1:
421 *pc += read_1byte (p);
424 case DW_CFA_advance_loc2:
425 *pc += read_2byte (p);
428 case DW_CFA_advance_loc4:
429 *pc += read_4byte (p);
433 case DW_CFA_offset_extended:
434 p = decode_uleb128 (p, ®);
435 p = decode_uleb128 (p, &offset);
436 offset *= info->data_align;
437 state->s.saved[reg] = REG_SAVED_OFFSET;
438 state->s.reg_or_offset[reg] = offset;
440 case DW_CFA_restore_extended:
441 p = decode_uleb128 (p, ®);
442 state->s.saved[reg] = REG_UNSAVED;
445 case DW_CFA_undefined:
446 case DW_CFA_same_value:
450 case DW_CFA_register:
453 p = decode_uleb128 (p, ®);
454 p = decode_uleb128 (p, ®2);
455 state->s.saved[reg] = REG_SAVED_REG;
456 state->s.reg_or_offset[reg] = reg2;
461 p = decode_uleb128 (p, ®);
462 p = decode_uleb128 (p, &offset);
463 state->s.cfa_reg = reg;
464 state->s.cfa_offset = offset;
466 case DW_CFA_def_cfa_register:
467 p = decode_uleb128 (p, ®);
468 state->s.cfa_reg = reg;
470 case DW_CFA_def_cfa_offset:
471 p = decode_uleb128 (p, &offset);
472 state->s.cfa_offset = offset;
475 case DW_CFA_remember_state:
477 struct frame_state_internal *save =
478 (struct frame_state_internal *)
479 malloc (sizeof (struct frame_state_internal));
480 memcpy (save, state, sizeof (struct frame_state_internal));
481 state->saved_state = save;
484 case DW_CFA_restore_state:
486 struct frame_state_internal *save = state->saved_state;
487 memcpy (state, save, sizeof (struct frame_state_internal));
492 /* FIXME: Hardcoded for SPARC register window configuration. */
493 case DW_CFA_GNU_window_save:
494 for (reg = 16; reg < 32; ++reg)
496 state->s.saved[reg] = REG_SAVED_OFFSET;
497 state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
501 case DW_CFA_GNU_args_size:
502 p = decode_uleb128 (p, &offset);
503 state->s.args_size = offset;
512 /* Called from crtbegin.o to register the unwind info for an object. */
515 __register_frame_info (void *begin, struct object *ob)
517 ob->fde_begin = begin;
519 ob->pc_begin = ob->pc_end = 0;
523 __gthread_mutex_lock (&object_mutex);
528 __gthread_mutex_unlock (&object_mutex);
532 __register_frame (void *begin)
534 struct object *ob = (struct object *) malloc (sizeof (struct object));
535 __register_frame_info (begin, ob);
538 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
539 for different translation units. Called from the file generated by
543 __register_frame_info_table (void *begin, struct object *ob)
545 ob->fde_begin = begin;
546 ob->fde_array = begin;
548 ob->pc_begin = ob->pc_end = 0;
551 __gthread_mutex_lock (&object_mutex);
556 __gthread_mutex_unlock (&object_mutex);
560 __register_frame_table (void *begin)
562 struct object *ob = (struct object *) malloc (sizeof (struct object));
563 __register_frame_info_table (begin, ob);
566 /* Called from crtend.o to deregister the unwind info for an object. */
569 __deregister_frame_info (void *begin)
573 __gthread_mutex_lock (&object_mutex);
578 if ((*p)->fde_begin == begin)
580 struct object *ob = *p;
583 /* If we've run init_frame for this object, free the FDE array. */
585 free (ob->fde_array);
587 __gthread_mutex_unlock (&object_mutex);
593 __gthread_mutex_unlock (&object_mutex);
598 __deregister_frame (void *begin)
600 free (__deregister_frame_info (begin));
603 /* Called from __throw to find the registers to restore for a given
604 PC_TARGET. The caller should allocate a local variable of `struct
605 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
608 __frame_state_for (void *pc_target, struct frame_state *state_in)
611 void *insn, *end, *pc;
612 struct cie_info info;
613 struct frame_state_internal state;
615 f = find_fde (pc_target);
619 insn = extract_cie_info (f, &info);
623 memset (&state, 0, sizeof (state));
624 state.s.retaddr_column = info.ra_regno;
625 state.s.eh_ptr = info.eh_ptr;
627 /* First decode all the insns in the CIE. */
628 end = next_fde ((fde*) get_cie (f));
630 insn = execute_cfa_insn (insn, &state, &info, 0);
632 insn = ((fde *)f) + 1;
634 if (info.augmentation[0] == 'z')
637 insn = decode_uleb128 (insn, &i);
641 /* Then the insns in the FDE up to our target PC. */
644 while (insn < end && pc <= pc_target)
645 insn = execute_cfa_insn (insn, &state, &info, &pc);
647 memcpy (state_in, &state.s, sizeof (state.s));
650 #endif /* DWARF2_UNWIND_INFO */