ARM support for unordered FP operations.
[official-gcc.git] / gcc / frame-dwarf2.c
blob52db16f577e3bad0d062b11ac01510c7f8695e22
1 /* Subroutines needed for unwinding DWARF 2 format stack frame info
2 for exception handling. */
3 /* Compile this one with gcc. */
4 /* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
5 Contributed by Jason Merrill <jason@cygnus.com>.
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
23 GNU CC is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with GNU CC; see the file COPYING. If not, write to
30 the Free Software Foundation, 59 Temple Place - Suite 330,
31 Boston, MA 02111-1307, USA. */
33 /* It is incorrect to include config.h here, because this file is being
34 compiled for the target, and hence definitions concerning only the host
35 do not apply. */
37 #include "tconfig.h"
38 #include "tsystem.h"
40 #include "defaults.h"
42 #ifdef DWARF2_UNWIND_INFO
43 #include "dwarf2.h"
44 #include "frame.h"
45 #include "gthr.h"
47 #ifdef __GTHREAD_MUTEX_INIT
48 static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
49 #else
50 static __gthread_mutex_t object_mutex;
51 #endif
53 /* Don't use `fancy_abort' here even if config.h says to use it. */
54 #ifdef abort
55 #undef abort
56 #endif
58 /* Some types used by the DWARF 2 spec. */
60 typedef int sword __attribute__ ((mode (SI)));
61 typedef unsigned int uword __attribute__ ((mode (SI)));
62 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
63 typedef int saddr __attribute__ ((mode (pointer)));
64 typedef unsigned char ubyte;
66 /* Terminology:
67 CIE - Common Information Element
68 FDE - Frame Descriptor Element
70 There is one per function, and it describes where the function code
71 is located, and what the register lifetimes and stack layout are
72 within the function.
74 The data structures are defined in the DWARF specfication, although
75 not in a very readable way (see LITERATURE).
77 Every time an exception is thrown, the code needs to locate the FDE
78 for the current function, and starts to look for exception regions
79 from that FDE. This works in a two-level search:
80 a) in a linear search, find the shared image (i.e. DLL) containing
81 the PC
82 b) using the FDE table for that shared object, locate the FDE using
83 binary search (which requires the sorting). */
85 /* The first few fields of a CIE. The CIE_id field is 0 for a CIE,
86 to distinguish it from a valid FDE. FDEs are aligned to an addressing
87 unit boundary, but the fields within are unaligned. */
89 struct dwarf_cie {
90 uword length;
91 sword CIE_id;
92 ubyte version;
93 char augmentation[0];
94 } __attribute__ ((packed, aligned (__alignof__ (void *))));
96 /* The first few fields of an FDE. */
98 struct dwarf_fde {
99 uword length;
100 sword CIE_delta;
101 void* pc_begin;
102 uaddr pc_range;
103 } __attribute__ ((packed, aligned (__alignof__ (void *))));
105 typedef struct dwarf_fde fde;
107 /* Objects to be searched for frame unwind info. */
109 static struct object *objects;
111 /* The information we care about from a CIE. */
113 struct cie_info {
114 char *augmentation;
115 void *eh_ptr;
116 int code_align;
117 int data_align;
118 unsigned ra_regno;
121 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
123 struct frame_state_internal
125 struct frame_state s;
126 struct frame_state_internal *saved_state;
129 /* This is undefined below if we need it to be an actual function. */
130 #define init_object_mutex_once()
132 #if __GTHREADS
133 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
135 /* Helper for init_object_mutex_once. */
137 static void
138 init_object_mutex (void)
140 __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
143 /* Call this to arrange to initialize the object mutex. */
145 #undef init_object_mutex_once
146 static void
147 init_object_mutex_once (void)
149 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
150 __gthread_once (&once, init_object_mutex);
153 #endif /* __GTHREAD_MUTEX_INIT_FUNCTION */
154 #endif /* __GTHREADS */
156 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
157 by R, and return the new value of BUF. */
159 static void *
160 decode_uleb128 (unsigned char *buf, unsigned *r)
162 unsigned shift = 0;
163 unsigned result = 0;
165 while (1)
167 unsigned byte = *buf++;
168 result |= (byte & 0x7f) << shift;
169 if ((byte & 0x80) == 0)
170 break;
171 shift += 7;
173 *r = result;
174 return buf;
177 /* Decode the signed LEB128 constant at BUF into the variable pointed to
178 by R, and return the new value of BUF. */
180 static void *
181 decode_sleb128 (unsigned char *buf, int *r)
183 unsigned shift = 0;
184 unsigned result = 0;
185 unsigned byte;
187 while (1)
189 byte = *buf++;
190 result |= (byte & 0x7f) << shift;
191 shift += 7;
192 if ((byte & 0x80) == 0)
193 break;
195 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
196 result |= - (1 << shift);
198 *r = result;
199 return buf;
202 /* Read unaligned data from the instruction buffer. */
204 union unaligned {
205 void *p;
206 unsigned b2 __attribute__ ((mode (HI)));
207 unsigned b4 __attribute__ ((mode (SI)));
208 unsigned b8 __attribute__ ((mode (DI)));
209 } __attribute__ ((packed));
210 static inline void *
211 read_pointer (void *p)
212 { union unaligned *up = p; return up->p; }
213 static inline unsigned
214 read_1byte (void *p)
215 { return *(unsigned char *)p; }
216 static inline unsigned
217 read_2byte (void *p)
218 { union unaligned *up = p; return up->b2; }
219 static inline unsigned
220 read_4byte (void *p)
221 { union unaligned *up = p; return up->b4; }
222 static inline unsigned long
223 read_8byte (void *p)
224 { union unaligned *up = p; return up->b8; }
226 /* Ordering function for FDEs. Functions can't overlap, so we just compare
227 their starting addresses. */
229 static inline saddr
230 fde_compare (fde *x, fde *y)
232 return (saddr)x->pc_begin - (saddr)y->pc_begin;
235 /* Return the address of the FDE after P. */
237 static inline fde *
238 next_fde (fde *p)
240 return (fde *)(((char *)p) + p->length + sizeof (p->length));
243 #include "frame.c"
245 static size_t
246 count_fdes (fde *this_fde)
248 size_t count;
250 for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
252 /* Skip CIEs and linked once FDE entries. */
253 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
254 continue;
256 ++count;
259 return count;
262 static void
263 add_fdes (fde *this_fde, fde_accumulator *accu, void **beg_ptr, void **end_ptr)
265 void *pc_begin = *beg_ptr;
266 void *pc_end = *end_ptr;
268 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
270 /* Skip CIEs and linked once FDE entries. */
271 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
272 continue;
274 fde_insert (accu, this_fde);
276 if (this_fde->pc_begin < pc_begin)
277 pc_begin = this_fde->pc_begin;
278 if (this_fde->pc_begin + this_fde->pc_range > pc_end)
279 pc_end = this_fde->pc_begin + this_fde->pc_range;
282 *beg_ptr = pc_begin;
283 *end_ptr = pc_end;
286 /* search this fde table for the one containing the pc */
287 static fde *
288 search_fdes (fde *this_fde, void *pc)
290 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
292 /* Skip CIEs and linked once FDE entries. */
293 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
294 continue;
296 if ((uaddr)((char *)pc - (char *)this_fde->pc_begin) < this_fde->pc_range)
297 return this_fde;
299 return NULL;
302 /* Set up a sorted array of pointers to FDEs for a loaded object. We
303 count up the entries before allocating the array because it's likely to
304 be faster. We can be called multiple times, should we have failed to
305 allocate a sorted fde array on a previous occasion. */
307 static void
308 frame_init (struct object* ob)
310 size_t count;
311 fde_accumulator accu;
312 void *pc_begin, *pc_end;
313 fde **array;
315 if (ob->pc_begin)
316 count = ob->count;
317 else if (ob->fde_array)
319 fde **p = ob->fde_array;
320 for (count = 0; *p; ++p)
321 count += count_fdes (*p);
323 else
324 count = count_fdes (ob->fde_begin);
325 ob->count = count;
327 if (!start_fde_sort (&accu, count) && ob->pc_begin)
328 return;
330 pc_begin = (void*)(uaddr)-1;
331 pc_end = 0;
333 if (ob->fde_array)
335 fde **p = ob->fde_array;
336 for (; *p; ++p)
337 add_fdes (*p, &accu, &pc_begin, &pc_end);
339 else
340 add_fdes (ob->fde_begin, &accu, &pc_begin, &pc_end);
342 array = end_fde_sort (&accu, count);
343 if (array)
344 ob->fde_array = array;
345 ob->pc_begin = pc_begin;
346 ob->pc_end = pc_end;
349 /* Return a pointer to the FDE for the function containing PC. */
351 static fde *
352 find_fde (void *pc)
354 struct object *ob;
355 size_t lo, hi;
357 init_object_mutex_once ();
358 __gthread_mutex_lock (&object_mutex);
360 /* Linear search through the objects, to find the one containing the pc. */
361 for (ob = objects; ob; ob = ob->next)
363 if (ob->pc_begin == 0)
364 frame_init (ob);
365 if (pc >= ob->pc_begin && pc < ob->pc_end)
366 break;
369 if (ob == 0)
371 __gthread_mutex_unlock (&object_mutex);
372 return 0;
375 if (!ob->fde_array || (void *)ob->fde_array == (void *)ob->fde_begin)
376 frame_init (ob);
378 if (ob->fde_array && (void *)ob->fde_array != (void *)ob->fde_begin)
380 __gthread_mutex_unlock (&object_mutex);
382 /* Standard binary search algorithm. */
383 for (lo = 0, hi = ob->count; lo < hi; )
385 size_t i = (lo + hi) / 2;
386 fde *f = ob->fde_array[i];
388 if (pc < f->pc_begin)
389 hi = i;
390 else if (pc >= f->pc_begin + f->pc_range)
391 lo = i + 1;
392 else
393 return f;
396 else
398 /* Long slow labourious linear search, cos we've no memory. */
399 fde *f;
401 if (ob->fde_array)
403 fde **p = ob->fde_array;
407 f = search_fdes (*p, pc);
408 if (f)
409 break;
410 p++;
412 while (*p);
414 else
415 f = search_fdes (ob->fde_begin, pc);
416 __gthread_mutex_unlock (&object_mutex);
417 return f;
419 return 0;
422 static inline struct dwarf_cie *
423 get_cie (fde *f)
425 return ((void *)&f->CIE_delta) - f->CIE_delta;
428 /* Extract any interesting information from the CIE for the translation
429 unit F belongs to. */
431 static void *
432 extract_cie_info (fde *f, struct cie_info *c)
434 void *p;
435 int i;
437 c->augmentation = get_cie (f)->augmentation;
439 if (strcmp (c->augmentation, "") != 0
440 && strcmp (c->augmentation, "eh") != 0
441 && c->augmentation[0] != 'z')
442 return 0;
444 p = c->augmentation + strlen (c->augmentation) + 1;
446 if (strcmp (c->augmentation, "eh") == 0)
448 c->eh_ptr = read_pointer (p);
449 p += sizeof (void *);
451 else
452 c->eh_ptr = 0;
454 p = decode_uleb128 (p, &c->code_align);
455 p = decode_sleb128 (p, &c->data_align);
456 c->ra_regno = *(unsigned char *)p++;
458 /* If the augmentation starts with 'z', we now see the length of the
459 augmentation fields. */
460 if (c->augmentation[0] == 'z')
462 p = decode_uleb128 (p, &i);
463 p += i;
466 return p;
469 /* Decode a DW_OP stack operation. */
471 static void *
472 decode_stack_op (unsigned char *buf, struct frame_state *state)
474 enum dwarf_location_atom op;
475 int offset;
477 op = *buf++;
478 switch (op)
480 case DW_OP_reg0:
481 case DW_OP_reg1:
482 case DW_OP_reg2:
483 case DW_OP_reg3:
484 case DW_OP_reg4:
485 case DW_OP_reg5:
486 case DW_OP_reg6:
487 case DW_OP_reg7:
488 case DW_OP_reg8:
489 case DW_OP_reg9:
490 case DW_OP_reg10:
491 case DW_OP_reg11:
492 case DW_OP_reg12:
493 case DW_OP_reg13:
494 case DW_OP_reg14:
495 case DW_OP_reg15:
496 case DW_OP_reg16:
497 case DW_OP_reg17:
498 case DW_OP_reg18:
499 case DW_OP_reg19:
500 case DW_OP_reg20:
501 case DW_OP_reg21:
502 case DW_OP_reg22:
503 case DW_OP_reg23:
504 case DW_OP_reg24:
505 case DW_OP_reg25:
506 case DW_OP_reg26:
507 case DW_OP_reg27:
508 case DW_OP_reg28:
509 case DW_OP_reg29:
510 case DW_OP_reg30:
511 case DW_OP_reg31:
512 state->cfa_reg = op - DW_OP_reg0;
513 break;
514 case DW_OP_regx:
515 buf = decode_sleb128 (buf, &offset);
516 state->cfa_reg = offset;
517 case DW_OP_breg0:
518 case DW_OP_breg1:
519 case DW_OP_breg2:
520 case DW_OP_breg3:
521 case DW_OP_breg4:
522 case DW_OP_breg5:
523 case DW_OP_breg6:
524 case DW_OP_breg7:
525 case DW_OP_breg8:
526 case DW_OP_breg9:
527 case DW_OP_breg10:
528 case DW_OP_breg11:
529 case DW_OP_breg12:
530 case DW_OP_breg13:
531 case DW_OP_breg14:
532 case DW_OP_breg15:
533 case DW_OP_breg16:
534 case DW_OP_breg17:
535 case DW_OP_breg18:
536 case DW_OP_breg19:
537 case DW_OP_breg20:
538 case DW_OP_breg21:
539 case DW_OP_breg22:
540 case DW_OP_breg23:
541 case DW_OP_breg24:
542 case DW_OP_breg25:
543 case DW_OP_breg26:
544 case DW_OP_breg27:
545 case DW_OP_breg28:
546 case DW_OP_breg29:
547 case DW_OP_breg30:
548 case DW_OP_breg31:
549 state->cfa_reg = op - DW_OP_breg0;
550 buf = decode_sleb128 (buf, &offset);
551 state->base_offset = offset;
552 break;
553 case DW_OP_bregx:
554 buf = decode_sleb128 (buf, &offset);
555 state->cfa_reg = offset;
556 buf = decode_sleb128 (buf, &offset);
557 state->base_offset = offset;
558 break;
559 case DW_OP_deref:
560 state->indirect = 1;
561 break;
562 case DW_OP_plus_uconst:
563 buf = decode_uleb128 (buf, &offset);
564 state->cfa_offset = offset;
565 break;
566 default:
567 abort ();
569 return buf;
571 /* Decode one instruction's worth of DWARF 2 call frame information.
572 Used by __frame_state_for. Takes pointers P to the instruction to
573 decode, STATE to the current register unwind information, INFO to the
574 current CIE information, and PC to the current PC value. Returns a
575 pointer to the next instruction. */
577 static void *
578 execute_cfa_insn (void *p, struct frame_state_internal *state,
579 struct cie_info *info, void **pc)
581 unsigned insn = *(unsigned char *)p++;
582 unsigned reg;
583 int offset;
585 if (insn & DW_CFA_advance_loc)
586 *pc += ((insn & 0x3f) * info->code_align);
587 else if (insn & DW_CFA_offset)
589 reg = (insn & 0x3f);
590 p = decode_uleb128 (p, &offset);
591 if (reg == state->s.cfa_reg)
592 /* Don't record anything about this register; it's only used to
593 reload SP in the epilogue. We don't want to copy in SP
594 values for outer frames; we handle restoring SP specially. */;
595 else
597 offset *= info->data_align;
598 state->s.saved[reg] = REG_SAVED_OFFSET;
599 state->s.reg_or_offset[reg] = offset;
602 else if (insn & DW_CFA_restore)
604 reg = (insn & 0x3f);
605 state->s.saved[reg] = REG_UNSAVED;
607 else switch (insn)
609 case DW_CFA_set_loc:
610 *pc = read_pointer (p);
611 p += sizeof (void *);
612 break;
613 case DW_CFA_advance_loc1:
614 *pc += read_1byte (p);
615 p += 1;
616 break;
617 case DW_CFA_advance_loc2:
618 *pc += read_2byte (p);
619 p += 2;
620 break;
621 case DW_CFA_advance_loc4:
622 *pc += read_4byte (p);
623 p += 4;
624 break;
626 case DW_CFA_offset_extended:
627 p = decode_uleb128 (p, &reg);
628 p = decode_uleb128 (p, &offset);
629 if (reg == state->s.cfa_reg)
630 /* Don't record anything; see above. */;
631 else
633 offset *= info->data_align;
634 state->s.saved[reg] = REG_SAVED_OFFSET;
635 state->s.reg_or_offset[reg] = offset;
637 break;
638 case DW_CFA_restore_extended:
639 p = decode_uleb128 (p, &reg);
640 state->s.saved[reg] = REG_UNSAVED;
641 break;
643 case DW_CFA_undefined:
644 case DW_CFA_same_value:
645 case DW_CFA_nop:
646 break;
648 case DW_CFA_register:
650 unsigned reg2;
651 p = decode_uleb128 (p, &reg);
652 p = decode_uleb128 (p, &reg2);
653 state->s.saved[reg] = REG_SAVED_REG;
654 state->s.reg_or_offset[reg] = reg2;
656 break;
658 case DW_CFA_def_cfa:
659 p = decode_uleb128 (p, &reg);
660 p = decode_uleb128 (p, &offset);
661 state->s.cfa_reg = reg;
662 state->s.cfa_offset = offset;
663 break;
664 case DW_CFA_def_cfa_register:
665 p = decode_uleb128 (p, &reg);
666 state->s.cfa_reg = reg;
667 break;
668 case DW_CFA_def_cfa_offset:
669 p = decode_uleb128 (p, &offset);
670 state->s.cfa_offset = offset;
671 break;
672 case DW_CFA_def_cfa_expression:
674 void *end;
675 state->s.cfa_reg = 0;
676 state->s.cfa_offset = 0;
677 state->s.base_offset = 0;
678 state->s.indirect = 0;
680 p = decode_uleb128 (p, &offset);
681 end = p + offset;
682 while (p < end)
683 p = decode_stack_op (p, &(state->s));
684 break;
687 case DW_CFA_remember_state:
689 struct frame_state_internal *save =
690 (struct frame_state_internal *)
691 malloc (sizeof (struct frame_state_internal));
692 memcpy (save, state, sizeof (struct frame_state_internal));
693 state->saved_state = save;
695 break;
696 case DW_CFA_restore_state:
698 struct frame_state_internal *save = state->saved_state;
699 memcpy (state, save, sizeof (struct frame_state_internal));
700 free (save);
702 break;
704 /* FIXME: Hardcoded for SPARC register window configuration. */
705 case DW_CFA_GNU_window_save:
706 for (reg = 16; reg < 32; ++reg)
708 state->s.saved[reg] = REG_SAVED_OFFSET;
709 state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
711 break;
713 case DW_CFA_GNU_args_size:
714 p = decode_uleb128 (p, &offset);
715 state->s.args_size = offset;
716 break;
718 case DW_CFA_GNU_negative_offset_extended:
719 p = decode_uleb128 (p, &reg);
720 p = decode_uleb128 (p, &offset);
721 offset *= info->data_align;
722 state->s.saved[reg] = REG_SAVED_OFFSET;
723 state->s.reg_or_offset[reg] = -offset;
724 break;
726 default:
727 abort ();
729 return p;
732 /* Called from __throw to find the registers to restore for a given
733 PC_TARGET. The caller should allocate a local variable of `struct
734 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
736 struct frame_state *
737 __frame_state_for (void *pc_target, struct frame_state *state_in)
739 fde *f;
740 void *insn, *end, *pc;
741 struct cie_info info;
742 struct frame_state_internal state;
744 f = find_fde (pc_target);
745 if (f == 0)
746 return 0;
748 insn = extract_cie_info (f, &info);
749 if (insn == 0)
750 return 0;
752 memset (&state, 0, sizeof (state));
753 state.s.retaddr_column = info.ra_regno;
754 state.s.eh_ptr = info.eh_ptr;
756 /* First decode all the insns in the CIE. */
757 end = next_fde ((fde*) get_cie (f));
758 while (insn < end)
759 insn = execute_cfa_insn (insn, &state, &info, 0);
761 insn = ((fde *)f) + 1;
763 if (info.augmentation[0] == 'z')
765 int i;
766 insn = decode_uleb128 (insn, &i);
767 insn += i;
770 /* Then the insns in the FDE up to our target PC. */
771 end = next_fde (f);
772 pc = f->pc_begin;
773 while (insn < end && pc <= pc_target)
774 insn = execute_cfa_insn (insn, &state, &info, &pc);
776 memcpy (state_in, &state.s, sizeof (state.s));
777 return state_in;
779 #endif /* DWARF2_UNWIND_INFO */