* except.c (expand_start_all_catch): One more do_pending_stack_adjust.
[official-gcc.git] / gcc / frame.c
blob118329bbfd5ef08df45110931e6e6c5a76407001
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 "dwarf2.h"
39 #include "frame.h"
40 #include <stddef.h>
42 /* Don't use `fancy_abort' here even if config.h says to use it. */
43 #ifdef abort
44 #undef abort
45 #endif
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. */
59 struct dwarf_cie {
60 uword length;
61 sword CIE_id;
62 ubyte version;
63 char augmentation[0];
64 } __attribute__ ((packed, aligned (__alignof__ (void *))));
66 /* The first few fields of an FDE. */
68 struct dwarf_fde {
69 uword length;
70 sword CIE_delta;
71 void* pc_begin;
72 uaddr pc_range;
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. */
81 struct object {
82 void *pc_begin;
83 void *pc_end;
84 fde *fde_begin;
85 fde ** fde_array;
86 size_t count;
87 struct object *next;
90 static struct object *objects;
92 /* The information we care about from a CIE. */
94 struct cie_info {
95 char *augmentation;
96 void *eh_ptr;
97 int code_align;
98 int data_align;
99 unsigned ra_regno;
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. */
113 static void *
114 decode_uleb128 (unsigned char *buf, unsigned *r)
116 unsigned shift = 0;
117 unsigned result = 0;
119 while (1)
121 unsigned byte = *buf++;
122 result |= (byte & 0x7f) << shift;
123 if ((byte & 0x80) == 0)
124 break;
125 shift += 7;
127 *r = result;
128 return buf;
131 /* Decode the signed LEB128 constant at BUF into the variable pointed to
132 by R, and return the new value of BUF. */
134 static void *
135 decode_sleb128 (unsigned char *buf, int *r)
137 unsigned shift = 0;
138 unsigned result = 0;
139 unsigned byte;
141 while (1)
143 byte = *buf++;
144 result |= (byte & 0x7f) << shift;
145 shift += 7;
146 if ((byte & 0x80) == 0)
147 break;
149 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
150 result |= - (1 << shift);
152 *r = result;
153 return buf;
156 /* Read unaligned data from the instruction buffer. */
158 union unaligned {
159 void *p;
160 unsigned b2 __attribute__ ((mode (HI)));
161 unsigned b4 __attribute__ ((mode (SI)));
162 unsigned b8 __attribute__ ((mode (DI)));
163 } __attribute__ ((packed));
164 static inline void *
165 read_pointer (void *p)
166 { union unaligned *up = p; return up->p; }
167 static inline unsigned
168 read_1byte (void *p)
169 { return *(unsigned char *)p; }
170 static inline unsigned
171 read_2byte (void *p)
172 { union unaligned *up = p; return up->b2; }
173 static inline unsigned
174 read_4byte (void *p)
175 { union unaligned *up = p; return up->b4; }
176 static inline unsigned long
177 read_8byte (void *p)
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. */
183 static inline saddr
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. */
191 static inline fde *
192 next_fde (fde *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. */
202 static void
203 fde_insert (fde **array, size_t i, fde *this_fde)
205 array[i] = this_fde;
207 for (; i > 0 && fde_compare (array[i], array[i-1]) < 0; --i)
209 this_fde = array[i];
210 array[i] = array[i-1];
211 array[i-1] = this_fde;
215 static size_t
216 count_fdes (fde *this_fde)
218 size_t count;
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_begin == 0)
224 continue;
226 ++count;
229 return count;
232 static void
233 add_fdes (fde *this_fde, fde **array, size_t *i_ptr,
234 void **beg_ptr, void **end_ptr)
236 size_t i = *i_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_begin == 0)
244 continue;
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;
254 *i_ptr = i;
255 *beg_ptr = pc_begin;
256 *end_ptr = pc_end;
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
261 be faster. */
263 static void
264 frame_init (struct object* ob)
266 fde *this_fde;
267 size_t count;
268 fde **array;
269 void *pc_begin, *pc_end;
271 if (ob->fde_array)
273 fde **p = ob->fde_array;
274 for (count = 0; *p; ++p)
275 count += count_fdes (*p);
277 else
278 count = count_fdes (ob->fde_begin);
280 ob->count = count;
281 array = (fde **) malloc (sizeof (fde *) * count);
283 pc_begin = (void*)(uaddr)-1;
284 pc_end = 0;
285 count = 0;
287 if (ob->fde_array)
289 fde **p = ob->fde_array;
290 for (; *p; ++p)
291 add_fdes (*p, array, &count, &pc_begin, &pc_end);
293 else
294 add_fdes (ob->fde_begin, array, &count, &pc_begin, &pc_end);
296 ob->fde_array = array;
297 ob->pc_begin = pc_begin;
298 ob->pc_end = pc_end;
301 /* Return a pointer to the FDE for the function containing PC. */
303 static fde *
304 find_fde (void *pc)
306 struct object *ob;
307 size_t lo, hi;
309 for (ob = objects; ob; ob = ob->next)
311 if (ob->pc_begin == 0)
312 frame_init (ob);
313 if (pc >= ob->pc_begin && pc < ob->pc_end)
314 break;
317 if (ob == 0)
318 return 0;
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)
327 hi = i;
328 else if (pc > f->pc_begin + f->pc_range)
329 lo = i + 1;
330 else
331 return f;
334 return 0;
337 static inline struct dwarf_cie *
338 get_cie (fde *f)
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. */
346 static void *
347 extract_cie_info (fde *f, struct cie_info *c)
349 void *p;
350 int i;
352 c->augmentation = get_cie (f)->augmentation;
354 if (strcmp (c->augmentation, "") != 0
355 && strcmp (c->augmentation, "eh") != 0
356 && c->augmentation[0] != 'z')
357 return 0;
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 *);
366 else
367 c->eh_ptr = 0;
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);
378 p += i;
381 return p;
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. */
390 static void *
391 execute_cfa_insn (void *p, struct frame_state_internal *state,
392 struct cie_info *info, void **pc)
394 unsigned insn = *(unsigned char *)p++;
395 unsigned reg;
396 int offset;
398 if (insn & DW_CFA_advance_loc)
399 *pc += ((insn & 0x3f) * info->code_align);
400 else if (insn & DW_CFA_offset)
402 reg = (insn & 0x3f);
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)
410 reg = (insn & 0x3f);
411 state->s.saved[reg] = REG_UNSAVED;
413 else switch (insn)
415 case DW_CFA_set_loc:
416 *pc = read_pointer (p);
417 p += sizeof (void *);
418 break;
419 case DW_CFA_advance_loc1:
420 *pc += read_1byte (p);
421 p += 1;
422 break;
423 case DW_CFA_advance_loc2:
424 *pc += read_2byte (p);
425 p += 2;
426 break;
427 case DW_CFA_advance_loc4:
428 *pc += read_4byte (p);
429 p += 4;
430 break;
432 case DW_CFA_offset_extended:
433 p = decode_uleb128 (p, &reg);
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;
438 break;
439 case DW_CFA_restore_extended:
440 p = decode_uleb128 (p, &reg);
441 state->s.saved[reg] = REG_UNSAVED;
442 break;
444 case DW_CFA_undefined:
445 case DW_CFA_same_value:
446 case DW_CFA_nop:
447 break;
449 case DW_CFA_register:
451 unsigned reg2;
452 p = decode_uleb128 (p, &reg);
453 p = decode_uleb128 (p, &reg2);
454 state->s.saved[reg] = REG_SAVED_REG;
455 state->s.reg_or_offset[reg] = reg2;
457 break;
459 case DW_CFA_def_cfa:
460 p = decode_uleb128 (p, &reg);
461 p = decode_uleb128 (p, &offset);
462 state->s.cfa_reg = reg;
463 state->s.cfa_offset = offset;
464 break;
465 case DW_CFA_def_cfa_register:
466 p = decode_uleb128 (p, &reg);
467 state->s.cfa_reg = reg;
468 break;
469 case DW_CFA_def_cfa_offset:
470 p = decode_uleb128 (p, &offset);
471 state->s.cfa_offset = offset;
472 break;
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;
482 break;
483 case DW_CFA_restore_state:
485 struct frame_state_internal *save = state->saved_state;
486 memcpy (state, save, sizeof (struct frame_state_internal));
487 free (save);
489 break;
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 *);
498 break;
500 case DW_CFA_GNU_args_size:
501 p = decode_uleb128 (p, &offset);
502 state->s.args_size = offset;
503 break;
505 default:
506 abort ();
508 return p;
511 /* Called from crtbegin.o to register the unwind info for an object. */
513 void
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;
521 ob->fde_array = 0;
522 ob->count = 0;
524 ob->next = objects;
525 objects = ob;
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
530 collect2. */
532 void
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;
541 ob->count = 0;
543 ob->next = objects;
544 objects = ob;
547 /* Called from crtend.o to deregister the unwind info for an object. */
549 void
550 __deregister_frame (void *begin)
552 struct object **p = &objects;
554 while (*p)
556 if ((*p)->fde_begin == begin)
558 struct object *ob = *p;
559 *p = (*p)->next;
561 /* If we've run init_frame for this object, free the FDE array. */
562 if (ob->pc_begin)
563 free (ob->fde_array);
564 free (ob);
566 return;
568 p = &((*p)->next);
570 abort ();
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. */
577 struct frame_state *
578 __frame_state_for (void *pc_target, struct frame_state *state_in)
580 fde *f;
581 void *insn, *end, *pc;
582 struct cie_info info;
583 struct frame_state_internal state;
585 f = find_fde (pc_target);
586 if (f == 0)
587 return 0;
589 insn = extract_cie_info (f, &info);
590 if (insn == 0)
591 return 0;
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));
599 while (insn < end)
600 insn = execute_cfa_insn (insn, &state, &info, 0);
602 insn = ((fde *)f) + 1;
604 if (info.augmentation[0] == 'z')
606 int i;
607 insn = decode_uleb128 (insn, &i);
608 insn += i;
611 /* Then the insns in the FDE up to our target PC. */
612 end = next_fde (f);
613 pc = f->pc_begin;
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));
618 return state_in;
620 #endif /* DWARF2_UNWIND_INFO */