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)
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
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
42 #ifdef DWARF2_UNWIND_INFO
47 #ifdef __GTHREAD_MUTEX_INIT
48 static __gthread_mutex_t object_mutex
= __GTHREAD_MUTEX_INIT
;
50 static __gthread_mutex_t object_mutex
;
53 /* Don't use `fancy_abort' here even if config.h says to use it. */
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
;
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
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
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. */
94 } __attribute__ ((packed
, aligned (__alignof__ (void *))));
96 /* The first few fields of an FDE. */
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. */
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()
133 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
135 /* Helper for init_object_mutex_once. */
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
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. */
160 decode_uleb128 (unsigned char *buf
, unsigned *r
)
167 unsigned byte
= *buf
++;
168 result
|= (byte
& 0x7f) << shift
;
169 if ((byte
& 0x80) == 0)
177 /* Decode the signed LEB128 constant at BUF into the variable pointed to
178 by R, and return the new value of BUF. */
181 decode_sleb128 (unsigned char *buf
, int *r
)
190 result
|= (byte
& 0x7f) << shift
;
192 if ((byte
& 0x80) == 0)
195 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
196 result
|= - (1 << shift
);
202 /* Read unaligned data from the instruction buffer. */
206 unsigned b2
__attribute__ ((mode (HI
)));
207 unsigned b4
__attribute__ ((mode (SI
)));
208 unsigned b8
__attribute__ ((mode (DI
)));
209 } __attribute__ ((packed
));
211 read_pointer (void *p
)
212 { union unaligned
*up
= p
; return up
->p
; }
213 static inline unsigned
215 { return *(unsigned char *)p
; }
216 static inline unsigned
218 { union unaligned
*up
= p
; return up
->b2
; }
219 static inline unsigned
221 { union unaligned
*up
= p
; return up
->b4
; }
222 static inline unsigned long
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. */
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. */
240 return (fde
*)(((char *)p
) + p
->length
+ sizeof (p
->length
));
246 count_fdes (fde
*this_fde
)
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)
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)
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
;
286 /* search this fde table for the one containing the pc */
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)
296 if ((uaddr
)((char *)pc
- (char *)this_fde
->pc_begin
) < this_fde
->pc_range
)
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. */
308 frame_init (struct object
* ob
)
311 fde_accumulator accu
;
312 void *pc_begin
, *pc_end
;
317 else if (ob
->fde_array
)
319 fde
**p
= ob
->fde_array
;
320 for (count
= 0; *p
; ++p
)
321 count
+= count_fdes (*p
);
324 count
= count_fdes (ob
->fde_begin
);
327 if (!start_fde_sort (&accu
, count
) && ob
->pc_begin
)
330 pc_begin
= (void*)(uaddr
)-1;
335 fde
**p
= ob
->fde_array
;
337 add_fdes (*p
, &accu
, &pc_begin
, &pc_end
);
340 add_fdes (ob
->fde_begin
, &accu
, &pc_begin
, &pc_end
);
342 array
= end_fde_sort (&accu
, count
);
344 ob
->fde_array
= array
;
345 ob
->pc_begin
= pc_begin
;
349 /* Return a pointer to the FDE for the function containing PC. */
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)
365 if (pc
>= ob
->pc_begin
&& pc
< ob
->pc_end
)
371 __gthread_mutex_unlock (&object_mutex
);
375 if (!ob
->fde_array
|| (void *)ob
->fde_array
== (void *)ob
->fde_begin
)
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
)
390 else if (pc
>= f
->pc_begin
+ f
->pc_range
)
398 /* Long slow labourious linear search, cos we've no memory. */
403 fde
**p
= ob
->fde_array
;
407 f
= search_fdes (*p
, pc
);
415 f
= search_fdes (ob
->fde_begin
, pc
);
416 __gthread_mutex_unlock (&object_mutex
);
422 static inline struct dwarf_cie
*
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. */
432 extract_cie_info (fde
*f
, struct cie_info
*c
)
437 c
->augmentation
= get_cie (f
)->augmentation
;
439 if (strcmp (c
->augmentation
, "") != 0
440 && strcmp (c
->augmentation
, "eh") != 0
441 && c
->augmentation
[0] != 'z')
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 *);
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
);
469 /* Decode a DW_OP stack operation. */
472 decode_stack_op (unsigned char *buf
, struct frame_state
*state
)
474 enum dwarf_location_atom op
;
512 state
->cfa_reg
= op
- DW_OP_reg0
;
515 buf
= decode_sleb128 (buf
, &offset
);
516 state
->cfa_reg
= offset
;
550 state
->cfa_reg
= op
- DW_OP_breg0
;
551 buf
= decode_sleb128 (buf
, &offset
);
552 state
->base_offset
= offset
;
555 buf
= decode_sleb128 (buf
, &offset
);
556 state
->cfa_reg
= offset
;
557 buf
= decode_sleb128 (buf
, &offset
);
558 state
->base_offset
= offset
;
563 case DW_OP_plus_uconst
:
564 buf
= decode_uleb128 (buf
, &offset
);
565 state
->cfa_offset
= offset
;
572 /* Decode one instruction's worth of DWARF 2 call frame information.
573 Used by __frame_state_for. Takes pointers P to the instruction to
574 decode, STATE to the current register unwind information, INFO to the
575 current CIE information, and PC to the current PC value. Returns a
576 pointer to the next instruction. */
579 execute_cfa_insn (void *p
, struct frame_state_internal
*state
,
580 struct cie_info
*info
, void **pc
)
582 unsigned insn
= *(unsigned char *)p
++;
586 if (insn
& DW_CFA_advance_loc
)
587 *pc
+= ((insn
& 0x3f) * info
->code_align
);
588 else if (insn
& DW_CFA_offset
)
591 p
= decode_uleb128 (p
, &offset
);
592 if (reg
== state
->s
.cfa_reg
)
593 /* Don't record anything about this register; it's only used to
594 reload SP in the epilogue. We don't want to copy in SP
595 values for outer frames; we handle restoring SP specially. */;
598 offset
*= info
->data_align
;
599 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
600 state
->s
.reg_or_offset
[reg
] = offset
;
603 else if (insn
& DW_CFA_restore
)
606 state
->s
.saved
[reg
] = REG_UNSAVED
;
611 *pc
= read_pointer (p
);
612 p
+= sizeof (void *);
614 case DW_CFA_advance_loc1
:
615 *pc
+= read_1byte (p
);
618 case DW_CFA_advance_loc2
:
619 *pc
+= read_2byte (p
);
622 case DW_CFA_advance_loc4
:
623 *pc
+= read_4byte (p
);
627 case DW_CFA_offset_extended
:
628 p
= decode_uleb128 (p
, ®
);
629 p
= decode_uleb128 (p
, &offset
);
630 if (reg
== state
->s
.cfa_reg
)
631 /* Don't record anything; see above. */;
634 offset
*= info
->data_align
;
635 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
636 state
->s
.reg_or_offset
[reg
] = offset
;
639 case DW_CFA_restore_extended
:
640 p
= decode_uleb128 (p
, ®
);
641 state
->s
.saved
[reg
] = REG_UNSAVED
;
644 case DW_CFA_undefined
:
645 case DW_CFA_same_value
:
649 case DW_CFA_register
:
652 p
= decode_uleb128 (p
, ®
);
653 p
= decode_uleb128 (p
, ®2
);
654 state
->s
.saved
[reg
] = REG_SAVED_REG
;
655 state
->s
.reg_or_offset
[reg
] = reg2
;
660 p
= decode_uleb128 (p
, ®
);
661 p
= decode_uleb128 (p
, &offset
);
662 state
->s
.cfa_reg
= reg
;
663 state
->s
.cfa_offset
= offset
;
665 case DW_CFA_def_cfa_register
:
666 p
= decode_uleb128 (p
, ®
);
667 state
->s
.cfa_reg
= reg
;
669 case DW_CFA_def_cfa_offset
:
670 p
= decode_uleb128 (p
, &offset
);
671 state
->s
.cfa_offset
= offset
;
673 case DW_CFA_def_cfa_expression
:
676 state
->s
.cfa_reg
= 0;
677 state
->s
.cfa_offset
= 0;
678 state
->s
.base_offset
= 0;
679 state
->s
.indirect
= 0;
681 p
= decode_uleb128 (p
, &offset
);
684 p
= decode_stack_op (p
, &(state
->s
));
688 case DW_CFA_remember_state
:
690 struct frame_state_internal
*save
=
691 (struct frame_state_internal
*)
692 malloc (sizeof (struct frame_state_internal
));
693 memcpy (save
, state
, sizeof (struct frame_state_internal
));
694 state
->saved_state
= save
;
697 case DW_CFA_restore_state
:
699 struct frame_state_internal
*save
= state
->saved_state
;
700 memcpy (state
, save
, sizeof (struct frame_state_internal
));
705 /* FIXME: Hardcoded for SPARC register window configuration. */
706 case DW_CFA_GNU_window_save
:
707 for (reg
= 16; reg
< 32; ++reg
)
709 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
710 state
->s
.reg_or_offset
[reg
] = (reg
- 16) * sizeof (void *);
714 case DW_CFA_GNU_args_size
:
715 p
= decode_uleb128 (p
, &offset
);
716 state
->s
.args_size
= offset
;
719 case DW_CFA_GNU_negative_offset_extended
:
720 p
= decode_uleb128 (p
, ®
);
721 p
= decode_uleb128 (p
, &offset
);
722 offset
*= info
->data_align
;
723 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
724 state
->s
.reg_or_offset
[reg
] = -offset
;
733 /* Called from __throw to find the registers to restore for a given
734 PC_TARGET. The caller should allocate a local variable of `struct
735 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
738 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
741 void *insn
, *end
, *pc
;
742 struct cie_info info
;
743 struct frame_state_internal state
;
745 f
= find_fde (pc_target
);
749 insn
= extract_cie_info (f
, &info
);
753 memset (&state
, 0, sizeof (state
));
754 state
.s
.retaddr_column
= info
.ra_regno
;
755 state
.s
.eh_ptr
= info
.eh_ptr
;
757 /* First decode all the insns in the CIE. */
758 end
= next_fde ((fde
*) get_cie (f
));
760 insn
= execute_cfa_insn (insn
, &state
, &info
, 0);
762 insn
= ((fde
*)f
) + 1;
764 if (info
.augmentation
[0] == 'z')
767 insn
= decode_uleb128 (insn
, &i
);
771 /* Then the insns in the FDE up to our target PC. */
774 while (insn
< end
&& pc
<= pc_target
)
775 insn
= execute_cfa_insn (insn
, &state
, &info
, &pc
);
777 memcpy (state_in
, &state
.s
, sizeof (state
.s
));
780 #endif /* DWARF2_UNWIND_INFO */