Move <stddef.h> include before "frame.h"
[official-gcc.git] / gcc / frame.c
blob72eb8273b8ce596866372711deec7a9534ba63d7
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 <stddef.h>
40 #include "frame.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 /* Objects to be searched for frame unwind info. */
79 static struct object *objects;
81 /* The information we care about from a CIE. */
83 struct cie_info {
84 char *augmentation;
85 void *eh_ptr;
86 int code_align;
87 int data_align;
88 unsigned ra_regno;
91 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
93 struct frame_state_internal
95 struct frame_state s;
96 struct frame_state_internal *saved_state;
98 \f
99 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
100 by R, and return the new value of BUF. */
102 static void *
103 decode_uleb128 (unsigned char *buf, unsigned *r)
105 unsigned shift = 0;
106 unsigned result = 0;
108 while (1)
110 unsigned byte = *buf++;
111 result |= (byte & 0x7f) << shift;
112 if ((byte & 0x80) == 0)
113 break;
114 shift += 7;
116 *r = result;
117 return buf;
120 /* Decode the signed LEB128 constant at BUF into the variable pointed to
121 by R, and return the new value of BUF. */
123 static void *
124 decode_sleb128 (unsigned char *buf, int *r)
126 unsigned shift = 0;
127 unsigned result = 0;
128 unsigned byte;
130 while (1)
132 byte = *buf++;
133 result |= (byte & 0x7f) << shift;
134 shift += 7;
135 if ((byte & 0x80) == 0)
136 break;
138 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
139 result |= - (1 << shift);
141 *r = result;
142 return buf;
145 /* Read unaligned data from the instruction buffer. */
147 union unaligned {
148 void *p;
149 unsigned b2 __attribute__ ((mode (HI)));
150 unsigned b4 __attribute__ ((mode (SI)));
151 unsigned b8 __attribute__ ((mode (DI)));
152 } __attribute__ ((packed));
153 static inline void *
154 read_pointer (void *p)
155 { union unaligned *up = p; return up->p; }
156 static inline unsigned
157 read_1byte (void *p)
158 { return *(unsigned char *)p; }
159 static inline unsigned
160 read_2byte (void *p)
161 { union unaligned *up = p; return up->b2; }
162 static inline unsigned
163 read_4byte (void *p)
164 { union unaligned *up = p; return up->b4; }
165 static inline unsigned long
166 read_8byte (void *p)
167 { union unaligned *up = p; return up->b8; }
169 /* Ordering function for FDEs. Functions can't overlap, so we just compare
170 their starting addresses. */
172 static inline saddr
173 fde_compare (fde *x, fde *y)
175 return (saddr)x->pc_begin - (saddr)y->pc_begin;
178 /* Return the address of the FDE after P. */
180 static inline fde *
181 next_fde (fde *p)
183 return (fde *)(((char *)p) + p->length + sizeof (p->length));
186 /* One iteration of an insertion sort, for adding new FDEs to the array.
187 Usually the new FDE will go in at the end, so we can expect close to
188 O(n) performance. If this turns out to be overly optimistic, we can have
189 the linker sort the FDEs so we don't have to do it at run time. */
191 static void
192 fde_insert (fde **array, size_t i, fde *this_fde)
194 array[i] = this_fde;
196 for (; i > 0 && fde_compare (array[i], array[i-1]) < 0; --i)
198 this_fde = array[i];
199 array[i] = array[i-1];
200 array[i-1] = this_fde;
204 static size_t
205 count_fdes (fde *this_fde)
207 size_t count;
209 for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
211 /* Skip CIEs and linked once FDE entries. */
212 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
213 continue;
215 ++count;
218 return count;
221 static void
222 add_fdes (fde *this_fde, fde **array, size_t *i_ptr,
223 void **beg_ptr, void **end_ptr)
225 size_t i = *i_ptr;
226 void *pc_begin = *beg_ptr;
227 void *pc_end = *end_ptr;
229 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
231 /* Skip CIEs and linked once FDE entries. */
232 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
233 continue;
235 fde_insert (array, i++, this_fde);
237 if (this_fde->pc_begin < pc_begin)
238 pc_begin = this_fde->pc_begin;
239 if (this_fde->pc_begin + this_fde->pc_range > pc_end)
240 pc_end = this_fde->pc_begin + this_fde->pc_range;
243 *i_ptr = i;
244 *beg_ptr = pc_begin;
245 *end_ptr = pc_end;
248 /* Set up a sorted array of pointers to FDEs for a loaded object. We
249 count up the entries before allocating the array because it's likely to
250 be faster. */
252 static void
253 frame_init (struct object* ob)
255 fde *this_fde;
256 size_t count;
257 fde **array;
258 void *pc_begin, *pc_end;
260 if (ob->fde_array)
262 fde **p = ob->fde_array;
263 for (count = 0; *p; ++p)
264 count += count_fdes (*p);
266 else
267 count = count_fdes (ob->fde_begin);
269 ob->count = count;
270 array = (fde **) malloc (sizeof (fde *) * count);
272 pc_begin = (void*)(uaddr)-1;
273 pc_end = 0;
274 count = 0;
276 if (ob->fde_array)
278 fde **p = ob->fde_array;
279 for (; *p; ++p)
280 add_fdes (*p, array, &count, &pc_begin, &pc_end);
282 else
283 add_fdes (ob->fde_begin, array, &count, &pc_begin, &pc_end);
285 ob->fde_array = array;
286 ob->pc_begin = pc_begin;
287 ob->pc_end = pc_end;
290 /* Return a pointer to the FDE for the function containing PC. */
292 static fde *
293 find_fde (void *pc)
295 struct object *ob;
296 size_t lo, hi;
298 for (ob = objects; ob; ob = ob->next)
300 if (ob->pc_begin == 0)
301 frame_init (ob);
302 if (pc >= ob->pc_begin && pc < ob->pc_end)
303 break;
306 if (ob == 0)
307 return 0;
309 /* Standard binary search algorithm. */
310 for (lo = 0, hi = ob->count; lo < hi; )
312 size_t i = (lo + hi) / 2;
313 fde *f = ob->fde_array[i];
315 if (pc < f->pc_begin)
316 hi = i;
317 else if (pc > f->pc_begin + f->pc_range)
318 lo = i + 1;
319 else
320 return f;
323 return 0;
326 static inline struct dwarf_cie *
327 get_cie (fde *f)
329 return ((void *)&f->CIE_delta) - f->CIE_delta;
332 /* Extract any interesting information from the CIE for the translation
333 unit F belongs to. */
335 static void *
336 extract_cie_info (fde *f, struct cie_info *c)
338 void *p;
339 int i;
341 c->augmentation = get_cie (f)->augmentation;
343 if (strcmp (c->augmentation, "") != 0
344 && strcmp (c->augmentation, "eh") != 0
345 && c->augmentation[0] != 'z')
346 return 0;
348 p = c->augmentation + strlen (c->augmentation) + 1;
350 if (strcmp (c->augmentation, "eh") == 0)
352 c->eh_ptr = read_pointer (p);
353 p += sizeof (void *);
355 else
356 c->eh_ptr = 0;
358 p = decode_uleb128 (p, &c->code_align);
359 p = decode_sleb128 (p, &c->data_align);
360 c->ra_regno = *(unsigned char *)p++;
362 /* If the augmentation starts with 'z', we now see the length of the
363 augmentation fields. */
364 if (c->augmentation[0] == 'z')
366 p = decode_uleb128 (p, &i);
367 p += i;
370 return p;
373 /* Decode one instruction's worth of of DWARF 2 call frame information.
374 Used by __frame_state_for. Takes pointers P to the instruction to
375 decode, STATE to the current register unwind information, INFO to the
376 current CIE information, and PC to the current PC value. Returns a
377 pointer to the next instruction. */
379 static void *
380 execute_cfa_insn (void *p, struct frame_state_internal *state,
381 struct cie_info *info, void **pc)
383 unsigned insn = *(unsigned char *)p++;
384 unsigned reg;
385 int offset;
387 if (insn & DW_CFA_advance_loc)
388 *pc += ((insn & 0x3f) * info->code_align);
389 else if (insn & DW_CFA_offset)
391 reg = (insn & 0x3f);
392 p = decode_uleb128 (p, &offset);
393 offset *= info->data_align;
394 state->s.saved[reg] = REG_SAVED_OFFSET;
395 state->s.reg_or_offset[reg] = offset;
397 else if (insn & DW_CFA_restore)
399 reg = (insn & 0x3f);
400 state->s.saved[reg] = REG_UNSAVED;
402 else switch (insn)
404 case DW_CFA_set_loc:
405 *pc = read_pointer (p);
406 p += sizeof (void *);
407 break;
408 case DW_CFA_advance_loc1:
409 *pc += read_1byte (p);
410 p += 1;
411 break;
412 case DW_CFA_advance_loc2:
413 *pc += read_2byte (p);
414 p += 2;
415 break;
416 case DW_CFA_advance_loc4:
417 *pc += read_4byte (p);
418 p += 4;
419 break;
421 case DW_CFA_offset_extended:
422 p = decode_uleb128 (p, &reg);
423 p = decode_uleb128 (p, &offset);
424 offset *= info->data_align;
425 state->s.saved[reg] = REG_SAVED_OFFSET;
426 state->s.reg_or_offset[reg] = offset;
427 break;
428 case DW_CFA_restore_extended:
429 p = decode_uleb128 (p, &reg);
430 state->s.saved[reg] = REG_UNSAVED;
431 break;
433 case DW_CFA_undefined:
434 case DW_CFA_same_value:
435 case DW_CFA_nop:
436 break;
438 case DW_CFA_register:
440 unsigned reg2;
441 p = decode_uleb128 (p, &reg);
442 p = decode_uleb128 (p, &reg2);
443 state->s.saved[reg] = REG_SAVED_REG;
444 state->s.reg_or_offset[reg] = reg2;
446 break;
448 case DW_CFA_def_cfa:
449 p = decode_uleb128 (p, &reg);
450 p = decode_uleb128 (p, &offset);
451 state->s.cfa_reg = reg;
452 state->s.cfa_offset = offset;
453 break;
454 case DW_CFA_def_cfa_register:
455 p = decode_uleb128 (p, &reg);
456 state->s.cfa_reg = reg;
457 break;
458 case DW_CFA_def_cfa_offset:
459 p = decode_uleb128 (p, &offset);
460 state->s.cfa_offset = offset;
461 break;
463 case DW_CFA_remember_state:
465 struct frame_state_internal *save =
466 (struct frame_state_internal *)
467 malloc (sizeof (struct frame_state_internal));
468 memcpy (save, state, sizeof (struct frame_state_internal));
469 state->saved_state = save;
471 break;
472 case DW_CFA_restore_state:
474 struct frame_state_internal *save = state->saved_state;
475 memcpy (state, save, sizeof (struct frame_state_internal));
476 free (save);
478 break;
480 /* FIXME: Hardcoded for SPARC register window configuration. */
481 case DW_CFA_GNU_window_save:
482 for (reg = 16; reg < 32; ++reg)
484 state->s.saved[reg] = REG_SAVED_OFFSET;
485 state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
487 break;
489 case DW_CFA_GNU_args_size:
490 p = decode_uleb128 (p, &offset);
491 state->s.args_size = offset;
492 break;
494 default:
495 abort ();
497 return p;
500 /* Called from crtbegin.o to register the unwind info for an object. */
502 void
503 __register_frame_info (void *begin, struct object *ob)
505 ob->fde_begin = begin;
507 ob->pc_begin = ob->pc_end = 0;
508 ob->fde_array = 0;
509 ob->count = 0;
511 ob->next = objects;
512 objects = ob;
515 void
516 __register_frame (void *begin)
518 struct object *ob = (struct object *) malloc (sizeof (struct object));
519 __register_frame_info (begin, ob);
522 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
523 for different translation units. Called from the file generated by
524 collect2. */
526 void
527 __register_frame_info_table (void *begin, struct object *ob)
529 ob->fde_begin = begin;
530 ob->fde_array = begin;
532 ob->pc_begin = ob->pc_end = 0;
533 ob->count = 0;
535 ob->next = objects;
536 objects = ob;
539 void
540 __register_frame_table (void *begin)
542 struct object *ob = (struct object *) malloc (sizeof (struct object));
543 __register_frame_info_table (begin, ob);
546 /* Called from crtend.o to deregister the unwind info for an object. */
548 void *
549 __deregister_frame_info (void *begin)
551 struct object **p;
553 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);
565 return (void *) ob;
567 p = &((*p)->next);
570 abort ();
573 void
574 __deregister_frame (void *begin)
576 free (__deregister_frame_info (begin));
579 /* Called from __throw to find the registers to restore for a given
580 PC_TARGET. The caller should allocate a local variable of `struct
581 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
583 struct frame_state *
584 __frame_state_for (void *pc_target, struct frame_state *state_in)
586 fde *f;
587 void *insn, *end, *pc;
588 struct cie_info info;
589 struct frame_state_internal state;
591 f = find_fde (pc_target);
592 if (f == 0)
593 return 0;
595 insn = extract_cie_info (f, &info);
596 if (insn == 0)
597 return 0;
599 memset (&state, 0, sizeof (state));
600 state.s.retaddr_column = info.ra_regno;
601 state.s.eh_ptr = info.eh_ptr;
603 /* First decode all the insns in the CIE. */
604 end = next_fde ((fde*) get_cie (f));
605 while (insn < end)
606 insn = execute_cfa_insn (insn, &state, &info, 0);
608 insn = ((fde *)f) + 1;
610 if (info.augmentation[0] == 'z')
612 int i;
613 insn = decode_uleb128 (insn, &i);
614 insn += i;
617 /* Then the insns in the FDE up to our target PC. */
618 end = next_fde (f);
619 pc = f->pc_begin;
620 while (insn < end && pc <= pc_target)
621 insn = execute_cfa_insn (insn, &state, &info, &pc);
623 memcpy (state_in, &state.s, sizeof (state.s));
624 return state_in;
626 #endif /* DWARF2_UNWIND_INFO */