Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / frame.c
blob2ab4aac93b422da8ae60c18321166ad4733544f2
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)
11 any later version.
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
32 do not apply. */
34 #include "tconfig.h"
35 #include "defaults.h"
37 #ifdef DWARF2_UNWIND_INFO
38 #include "gansidecl.h"
39 #include "dwarf2.h"
40 #include <stddef.h>
41 #include "frame.h"
43 /* Don't use `fancy_abort' here even if config.h says to use it. */
44 #ifdef abort
45 #undef abort
46 #endif
48 /* Some types used by the DWARF 2 spec. */
50 typedef int sword __attribute__ ((mode (SI)));
51 typedef unsigned int uword __attribute__ ((mode (SI)));
52 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
53 typedef int saddr __attribute__ ((mode (pointer)));
54 typedef unsigned char ubyte;
56 /* The first few fields of a CIE. The CIE_id field is 0xffffffff for a CIE,
57 to distinguish it from a valid FDE. FDEs are aligned to an addressing
58 unit boundary, but the fields within are unaligned. */
60 struct dwarf_cie {
61 uword length;
62 sword CIE_id;
63 ubyte version;
64 char augmentation[0];
65 } __attribute__ ((packed, aligned (__alignof__ (void *))));
67 /* The first few fields of an FDE. */
69 struct dwarf_fde {
70 uword length;
71 sword CIE_delta;
72 void* pc_begin;
73 uaddr pc_range;
74 } __attribute__ ((packed, aligned (__alignof__ (void *))));
76 typedef struct dwarf_fde fde;
78 /* Objects to be searched for frame unwind info. */
80 static struct object *objects;
82 /* The information we care about from a CIE. */
84 struct cie_info {
85 char *augmentation;
86 void *eh_ptr;
87 int code_align;
88 int data_align;
89 unsigned ra_regno;
92 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
94 struct frame_state_internal
96 struct frame_state s;
97 struct frame_state_internal *saved_state;
99 \f
100 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
101 by R, and return the new value of BUF. */
103 static void *
104 decode_uleb128 (unsigned char *buf, unsigned *r)
106 unsigned shift = 0;
107 unsigned result = 0;
109 while (1)
111 unsigned byte = *buf++;
112 result |= (byte & 0x7f) << shift;
113 if ((byte & 0x80) == 0)
114 break;
115 shift += 7;
117 *r = result;
118 return buf;
121 /* Decode the signed LEB128 constant at BUF into the variable pointed to
122 by R, and return the new value of BUF. */
124 static void *
125 decode_sleb128 (unsigned char *buf, int *r)
127 unsigned shift = 0;
128 unsigned result = 0;
129 unsigned byte;
131 while (1)
133 byte = *buf++;
134 result |= (byte & 0x7f) << shift;
135 shift += 7;
136 if ((byte & 0x80) == 0)
137 break;
139 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
140 result |= - (1 << shift);
142 *r = result;
143 return buf;
146 /* Read unaligned data from the instruction buffer. */
148 union unaligned {
149 void *p;
150 unsigned b2 __attribute__ ((mode (HI)));
151 unsigned b4 __attribute__ ((mode (SI)));
152 unsigned b8 __attribute__ ((mode (DI)));
153 } __attribute__ ((packed));
154 static inline void *
155 read_pointer (void *p)
156 { union unaligned *up = p; return up->p; }
157 static inline unsigned
158 read_1byte (void *p)
159 { return *(unsigned char *)p; }
160 static inline unsigned
161 read_2byte (void *p)
162 { union unaligned *up = p; return up->b2; }
163 static inline unsigned
164 read_4byte (void *p)
165 { union unaligned *up = p; return up->b4; }
166 static inline unsigned long
167 read_8byte (void *p)
168 { union unaligned *up = p; return up->b8; }
170 /* Ordering function for FDEs. Functions can't overlap, so we just compare
171 their starting addresses. */
173 static inline saddr
174 fde_compare (fde *x, fde *y)
176 return (saddr)x->pc_begin - (saddr)y->pc_begin;
179 /* Return the address of the FDE after P. */
181 static inline fde *
182 next_fde (fde *p)
184 return (fde *)(((char *)p) + p->length + sizeof (p->length));
187 /* One iteration of an insertion sort, for adding new FDEs to the array.
188 Usually the new FDE will go in at the end, so we can expect close to
189 O(n) performance. If this turns out to be overly optimistic, we can have
190 the linker sort the FDEs so we don't have to do it at run time. */
192 static void
193 fde_insert (fde **array, size_t i, fde *this_fde)
195 array[i] = this_fde;
197 for (; i > 0 && fde_compare (array[i], array[i-1]) < 0; --i)
199 this_fde = array[i];
200 array[i] = array[i-1];
201 array[i-1] = this_fde;
205 static size_t
206 count_fdes (fde *this_fde)
208 size_t count;
210 for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
212 /* Skip CIEs and linked once FDE entries. */
213 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
214 continue;
216 ++count;
219 return count;
222 static void
223 add_fdes (fde *this_fde, fde **array, size_t *i_ptr,
224 void **beg_ptr, void **end_ptr)
226 size_t i = *i_ptr;
227 void *pc_begin = *beg_ptr;
228 void *pc_end = *end_ptr;
230 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
232 /* Skip CIEs and linked once FDE entries. */
233 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
234 continue;
236 fde_insert (array, i++, this_fde);
238 if (this_fde->pc_begin < pc_begin)
239 pc_begin = this_fde->pc_begin;
240 if (this_fde->pc_begin + this_fde->pc_range > pc_end)
241 pc_end = this_fde->pc_begin + this_fde->pc_range;
244 *i_ptr = i;
245 *beg_ptr = pc_begin;
246 *end_ptr = pc_end;
249 /* Set up a sorted array of pointers to FDEs for a loaded object. We
250 count up the entries before allocating the array because it's likely to
251 be faster. */
253 static void
254 frame_init (struct object* ob)
256 fde *this_fde;
257 size_t count;
258 fde **array;
259 void *pc_begin, *pc_end;
261 if (ob->fde_array)
263 fde **p = ob->fde_array;
264 for (count = 0; *p; ++p)
265 count += count_fdes (*p);
267 else
268 count = count_fdes (ob->fde_begin);
270 ob->count = count;
271 array = (fde **) malloc (sizeof (fde *) * count);
273 pc_begin = (void*)(uaddr)-1;
274 pc_end = 0;
275 count = 0;
277 if (ob->fde_array)
279 fde **p = ob->fde_array;
280 for (; *p; ++p)
281 add_fdes (*p, array, &count, &pc_begin, &pc_end);
283 else
284 add_fdes (ob->fde_begin, array, &count, &pc_begin, &pc_end);
286 ob->fde_array = array;
287 ob->pc_begin = pc_begin;
288 ob->pc_end = pc_end;
291 /* Return a pointer to the FDE for the function containing PC. */
293 static fde *
294 find_fde (void *pc)
296 struct object *ob;
297 size_t lo, hi;
299 for (ob = objects; ob; ob = ob->next)
301 if (ob->pc_begin == 0)
302 frame_init (ob);
303 if (pc >= ob->pc_begin && pc < ob->pc_end)
304 break;
307 if (ob == 0)
308 return 0;
310 /* Standard binary search algorithm. */
311 for (lo = 0, hi = ob->count; lo < hi; )
313 size_t i = (lo + hi) / 2;
314 fde *f = ob->fde_array[i];
316 if (pc < f->pc_begin)
317 hi = i;
318 else if (pc > f->pc_begin + f->pc_range)
319 lo = i + 1;
320 else
321 return f;
324 return 0;
327 static inline struct dwarf_cie *
328 get_cie (fde *f)
330 return ((void *)&f->CIE_delta) - f->CIE_delta;
333 /* Extract any interesting information from the CIE for the translation
334 unit F belongs to. */
336 static void *
337 extract_cie_info (fde *f, struct cie_info *c)
339 void *p;
340 int i;
342 c->augmentation = get_cie (f)->augmentation;
344 if (strcmp (c->augmentation, "") != 0
345 && strcmp (c->augmentation, "eh") != 0
346 && c->augmentation[0] != 'z')
347 return 0;
349 p = c->augmentation + strlen (c->augmentation) + 1;
351 if (strcmp (c->augmentation, "eh") == 0)
353 c->eh_ptr = read_pointer (p);
354 p += sizeof (void *);
356 else
357 c->eh_ptr = 0;
359 p = decode_uleb128 (p, &c->code_align);
360 p = decode_sleb128 (p, &c->data_align);
361 c->ra_regno = *(unsigned char *)p++;
363 /* If the augmentation starts with 'z', we now see the length of the
364 augmentation fields. */
365 if (c->augmentation[0] == 'z')
367 p = decode_uleb128 (p, &i);
368 p += i;
371 return p;
374 /* Decode one instruction's worth of of DWARF 2 call frame information.
375 Used by __frame_state_for. Takes pointers P to the instruction to
376 decode, STATE to the current register unwind information, INFO to the
377 current CIE information, and PC to the current PC value. Returns a
378 pointer to the next instruction. */
380 static void *
381 execute_cfa_insn (void *p, struct frame_state_internal *state,
382 struct cie_info *info, void **pc)
384 unsigned insn = *(unsigned char *)p++;
385 unsigned reg;
386 int offset;
388 if (insn & DW_CFA_advance_loc)
389 *pc += ((insn & 0x3f) * info->code_align);
390 else if (insn & DW_CFA_offset)
392 reg = (insn & 0x3f);
393 p = decode_uleb128 (p, &offset);
394 offset *= info->data_align;
395 state->s.saved[reg] = REG_SAVED_OFFSET;
396 state->s.reg_or_offset[reg] = offset;
398 else if (insn & DW_CFA_restore)
400 reg = (insn & 0x3f);
401 state->s.saved[reg] = REG_UNSAVED;
403 else switch (insn)
405 case DW_CFA_set_loc:
406 *pc = read_pointer (p);
407 p += sizeof (void *);
408 break;
409 case DW_CFA_advance_loc1:
410 *pc += read_1byte (p);
411 p += 1;
412 break;
413 case DW_CFA_advance_loc2:
414 *pc += read_2byte (p);
415 p += 2;
416 break;
417 case DW_CFA_advance_loc4:
418 *pc += read_4byte (p);
419 p += 4;
420 break;
422 case DW_CFA_offset_extended:
423 p = decode_uleb128 (p, &reg);
424 p = decode_uleb128 (p, &offset);
425 offset *= info->data_align;
426 state->s.saved[reg] = REG_SAVED_OFFSET;
427 state->s.reg_or_offset[reg] = offset;
428 break;
429 case DW_CFA_restore_extended:
430 p = decode_uleb128 (p, &reg);
431 state->s.saved[reg] = REG_UNSAVED;
432 break;
434 case DW_CFA_undefined:
435 case DW_CFA_same_value:
436 case DW_CFA_nop:
437 break;
439 case DW_CFA_register:
441 unsigned reg2;
442 p = decode_uleb128 (p, &reg);
443 p = decode_uleb128 (p, &reg2);
444 state->s.saved[reg] = REG_SAVED_REG;
445 state->s.reg_or_offset[reg] = reg2;
447 break;
449 case DW_CFA_def_cfa:
450 p = decode_uleb128 (p, &reg);
451 p = decode_uleb128 (p, &offset);
452 state->s.cfa_reg = reg;
453 state->s.cfa_offset = offset;
454 break;
455 case DW_CFA_def_cfa_register:
456 p = decode_uleb128 (p, &reg);
457 state->s.cfa_reg = reg;
458 break;
459 case DW_CFA_def_cfa_offset:
460 p = decode_uleb128 (p, &offset);
461 state->s.cfa_offset = offset;
462 break;
464 case DW_CFA_remember_state:
466 struct frame_state_internal *save =
467 (struct frame_state_internal *)
468 malloc (sizeof (struct frame_state_internal));
469 memcpy (save, state, sizeof (struct frame_state_internal));
470 state->saved_state = save;
472 break;
473 case DW_CFA_restore_state:
475 struct frame_state_internal *save = state->saved_state;
476 memcpy (state, save, sizeof (struct frame_state_internal));
477 free (save);
479 break;
481 /* FIXME: Hardcoded for SPARC register window configuration. */
482 case DW_CFA_GNU_window_save:
483 for (reg = 16; reg < 32; ++reg)
485 state->s.saved[reg] = REG_SAVED_OFFSET;
486 state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
488 break;
490 case DW_CFA_GNU_args_size:
491 p = decode_uleb128 (p, &offset);
492 state->s.args_size = offset;
493 break;
495 default:
496 abort ();
498 return p;
501 /* Called from crtbegin.o to register the unwind info for an object. */
503 void
504 __register_frame_info (void *begin, struct object *ob)
506 ob->fde_begin = begin;
508 ob->pc_begin = ob->pc_end = 0;
509 ob->fde_array = 0;
510 ob->count = 0;
512 ob->next = objects;
513 objects = ob;
516 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
517 for different translation units. Called from the file generated by
518 collect2. */
520 void
521 __register_frame_info_table (void *begin, struct object *ob)
523 ob->fde_begin = begin;
524 ob->fde_array = begin;
526 ob->pc_begin = ob->pc_end = 0;
527 ob->count = 0;
529 ob->next = objects;
530 objects = ob;
533 /* Called from crtend.o to deregister the unwind info for an object. */
535 void
536 __deregister_frame_info (void *begin)
538 struct object **p = &objects;
540 while (*p)
542 if ((*p)->fde_begin == begin)
544 struct object *ob = *p;
545 *p = (*p)->next;
547 /* If we've run init_frame for this object, free the FDE array. */
548 if (ob->pc_begin)
549 free (ob->fde_array);
551 return;
553 p = &((*p)->next);
555 abort ();
558 /* Called from __throw to find the registers to restore for a given
559 PC_TARGET. The caller should allocate a local variable of `struct
560 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
562 struct frame_state *
563 __frame_state_for (void *pc_target, struct frame_state *state_in)
565 fde *f;
566 void *insn, *end, *pc;
567 struct cie_info info;
568 struct frame_state_internal state;
570 f = find_fde (pc_target);
571 if (f == 0)
572 return 0;
574 insn = extract_cie_info (f, &info);
575 if (insn == 0)
576 return 0;
578 memset (&state, 0, sizeof (state));
579 state.s.retaddr_column = info.ra_regno;
580 state.s.eh_ptr = info.eh_ptr;
582 /* First decode all the insns in the CIE. */
583 end = next_fde ((fde*) get_cie (f));
584 while (insn < end)
585 insn = execute_cfa_insn (insn, &state, &info, 0);
587 insn = ((fde *)f) + 1;
589 if (info.augmentation[0] == 'z')
591 int i;
592 insn = decode_uleb128 (insn, &i);
593 insn += i;
596 /* Then the insns in the FDE up to our target PC. */
597 end = next_fde (f);
598 pc = f->pc_begin;
599 while (insn < end && pc <= pc_target)
600 insn = execute_cfa_insn (insn, &state, &info, &pc);
602 memcpy (state_in, &state.s, sizeof (state.s));
603 return state_in;
605 #endif /* DWARF2_UNWIND_INFO */