1 /* Subroutines needed for unwinding stack frames for exception handling. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1998, 1999 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)
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
36 /* We disable this when inhibit_libc, so that gcc can still be built without
37 needing header files first. */
38 /* ??? This is not a good solution, since prototypes may be required in
39 some cases for correct code. See also libgcc2.c. */
41 /* fixproto guarantees these system headers exist. */
48 extern void *malloc (size_t);
51 extern void free (void *);
57 #ifdef DWARF2_UNWIND_INFO
63 #ifdef __GTHREAD_MUTEX_INIT
64 static __gthread_mutex_t object_mutex
= __GTHREAD_MUTEX_INIT
;
66 static __gthread_mutex_t object_mutex
;
69 /* Don't use `fancy_abort' here even if config.h says to use it. */
74 /* Some types used by the DWARF 2 spec. */
76 typedef int sword
__attribute__ ((mode (SI
)));
77 typedef unsigned int uword
__attribute__ ((mode (SI
)));
78 typedef unsigned int uaddr
__attribute__ ((mode (pointer
)));
79 typedef int saddr
__attribute__ ((mode (pointer
)));
80 typedef unsigned char ubyte
;
83 CIE - Common Information Element
84 FDE - Frame Descriptor Element
86 There is one per function, and it describes where the function code
87 is located, and what the register lifetimes and stack layout are
90 The data structures are defined in the DWARF specfication, although
91 not in a very readable way (see LITERATURE).
93 Every time an exception is thrown, the code needs to locate the FDE
94 for the current function, and starts to look for exception regions
95 from that FDE. This works in a two-level search:
96 a) in a linear search, find the shared image (i.e. DLL) containing
98 b) using the FDE table for that shared object, locate the FDE using
99 binary search (which requires the sorting). */
101 /* The first few fields of a CIE. The CIE_id field is 0 for a CIE,
102 to distinguish it from a valid FDE. FDEs are aligned to an addressing
103 unit boundary, but the fields within are unaligned. */
109 char augmentation
[0];
110 } __attribute__ ((packed
, aligned (__alignof__ (void *))));
112 /* The first few fields of an FDE. */
119 } __attribute__ ((packed
, aligned (__alignof__ (void *))));
121 typedef struct dwarf_fde fde
;
123 /* Objects to be searched for frame unwind info. */
125 static struct object
*objects
;
127 /* The information we care about from a CIE. */
137 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
139 struct frame_state_internal
141 struct frame_state s
;
142 struct frame_state_internal
*saved_state
;
145 /* This is undefined below if we need it to be an actual function. */
146 #define init_object_mutex_once()
149 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
151 /* Helper for init_object_mutex_once. */
154 init_object_mutex (void)
156 __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex
);
159 /* Call this to arrange to initialize the object mutex. */
161 #undef init_object_mutex_once
163 init_object_mutex_once (void)
165 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
166 __gthread_once (&once
, init_object_mutex
);
169 #endif /* __GTHREAD_MUTEX_INIT_FUNCTION */
170 #endif /* __GTHREADS */
172 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
173 by R, and return the new value of BUF. */
176 decode_uleb128 (unsigned char *buf
, unsigned *r
)
183 unsigned byte
= *buf
++;
184 result
|= (byte
& 0x7f) << shift
;
185 if ((byte
& 0x80) == 0)
193 /* Decode the signed LEB128 constant at BUF into the variable pointed to
194 by R, and return the new value of BUF. */
197 decode_sleb128 (unsigned char *buf
, int *r
)
206 result
|= (byte
& 0x7f) << shift
;
208 if ((byte
& 0x80) == 0)
211 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
212 result
|= - (1 << shift
);
218 /* Read unaligned data from the instruction buffer. */
222 unsigned b2
__attribute__ ((mode (HI
)));
223 unsigned b4
__attribute__ ((mode (SI
)));
224 unsigned b8
__attribute__ ((mode (DI
)));
225 } __attribute__ ((packed
));
227 read_pointer (void *p
)
228 { union unaligned
*up
= p
; return up
->p
; }
229 static inline unsigned
231 { return *(unsigned char *)p
; }
232 static inline unsigned
234 { union unaligned
*up
= p
; return up
->b2
; }
235 static inline unsigned
237 { union unaligned
*up
= p
; return up
->b4
; }
238 static inline unsigned long
240 { union unaligned
*up
= p
; return up
->b8
; }
242 /* Ordering function for FDEs. Functions can't overlap, so we just compare
243 their starting addresses. */
246 fde_compare (fde
*x
, fde
*y
)
248 return (saddr
)x
->pc_begin
- (saddr
)y
->pc_begin
;
251 /* Return the address of the FDE after P. */
256 return (fde
*)(((char *)p
) + p
->length
+ sizeof (p
->length
));
259 /* Sorting an array of FDEs by address.
260 (Ideally we would have the linker sort the FDEs so we don't have to do
261 it at run time. But the linkers are not yet prepared for this.) */
263 /* This is a special mix of insertion sort and heap sort, optimized for
264 the data sets that actually occur. They look like
265 101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130.
266 I.e. a linearly increasing sequence (coming from functions in the text
267 section), with additionally a few unordered elements (coming from functions
268 in gnu_linkonce sections) whose values are higher than the values in the
269 surrounding linear sequence (but not necessarily higher than the values
270 at the end of the linear sequence!).
271 The worst-case total run time is O(N) + O(n log (n)), where N is the
272 total number of FDEs and n is the number of erratic ones. */
274 typedef struct fde_vector
280 typedef struct fde_accumulator
287 start_fde_sort (fde_accumulator
*accu
, size_t count
)
289 accu
->linear
.array
= (fde
**) malloc (sizeof (fde
*) * count
);
290 accu
->erratic
.array
= (fde
**) malloc (sizeof (fde
*) * count
);
291 accu
->linear
.count
= 0;
292 accu
->erratic
.count
= 0;
296 fde_insert (fde_accumulator
*accu
, fde
*this_fde
)
298 accu
->linear
.array
[accu
->linear
.count
++] = this_fde
;
301 /* Split LINEAR into a linear sequence with low values and an erratic
302 sequence with high values, put the linear one (of longest possible
303 length) into LINEAR and the erratic one into ERRATIC. This is O(N). */
305 fde_split (fde_vector
*linear
, fde_vector
*erratic
)
307 size_t count
= linear
->count
;
308 size_t linear_max
= (size_t) -1;
309 size_t previous_max
[count
];
312 for (i
= 0; i
< count
; i
++)
316 && fde_compare (linear
->array
[i
], linear
->array
[j
]) < 0;
319 erratic
->array
[erratic
->count
++] = linear
->array
[j
];
320 linear
->array
[j
] = (fde
*) NULL
;
326 for (i
= 0, j
= 0; i
< count
; i
++)
327 if (linear
->array
[i
] != (fde
*) NULL
)
328 linear
->array
[j
++] = linear
->array
[i
];
332 /* This is O(n log(n)). BSD/OS defines heapsort in stdlib.h, so we must
333 use a name that does not conflict. */
335 frame_heapsort (fde_vector
*erratic
)
337 /* For a description of this algorithm, see:
338 Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
340 fde
** a
= erratic
->array
;
341 /* A portion of the array is called a "heap" if for all i>=0:
342 If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
343 If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */
344 #define SWAP(x,y) do { fde * tmp = x; x = y; y = tmp; } while (0)
345 size_t n
= erratic
->count
;
351 /* Invariant: a[m..n-1] is a heap. */
353 for (i
= m
; 2*i
+1 < n
; )
356 && fde_compare (a
[2*i
+2], a
[2*i
+1]) > 0
357 && fde_compare (a
[2*i
+2], a
[i
]) > 0)
359 SWAP (a
[i
], a
[2*i
+2]);
362 else if (fde_compare (a
[2*i
+1], a
[i
]) > 0)
364 SWAP (a
[i
], a
[2*i
+1]);
373 /* Invariant: a[0..n-1] is a heap. */
376 for (i
= 0; 2*i
+1 < n
; )
379 && fde_compare (a
[2*i
+2], a
[2*i
+1]) > 0
380 && fde_compare (a
[2*i
+2], a
[i
]) > 0)
382 SWAP (a
[i
], a
[2*i
+2]);
385 else if (fde_compare (a
[2*i
+1], a
[i
]) > 0)
387 SWAP (a
[i
], a
[2*i
+1]);
397 /* Merge V1 and V2, both sorted, and put the result into V1. */
399 fde_merge (fde_vector
*v1
, const fde_vector
*v2
)
410 fde2
= v2
->array
[i2
];
411 while (i1
> 0 && fde_compare (v1
->array
[i1
-1], fde2
) > 0)
413 v1
->array
[i1
+i2
] = v1
->array
[i1
-1];
416 v1
->array
[i1
+i2
] = fde2
;
418 v1
->count
+= v2
->count
;
423 end_fde_sort (fde_accumulator
*accu
, size_t count
)
425 if (accu
->linear
.count
!= count
)
427 fde_split (&accu
->linear
, &accu
->erratic
);
428 if (accu
->linear
.count
+ accu
->erratic
.count
!= count
)
430 frame_heapsort (&accu
->erratic
);
431 fde_merge (&accu
->linear
, &accu
->erratic
);
432 free (accu
->erratic
.array
);
433 return accu
->linear
.array
;
437 count_fdes (fde
*this_fde
)
441 for (count
= 0; this_fde
->length
!= 0; this_fde
= next_fde (this_fde
))
443 /* Skip CIEs and linked once FDE entries. */
444 if (this_fde
->CIE_delta
== 0 || this_fde
->pc_begin
== 0)
454 add_fdes (fde
*this_fde
, fde_accumulator
*accu
, void **beg_ptr
, void **end_ptr
)
456 void *pc_begin
= *beg_ptr
;
457 void *pc_end
= *end_ptr
;
459 for (; this_fde
->length
!= 0; this_fde
= next_fde (this_fde
))
461 /* Skip CIEs and linked once FDE entries. */
462 if (this_fde
->CIE_delta
== 0 || this_fde
->pc_begin
== 0)
465 fde_insert (accu
, this_fde
);
467 if (this_fde
->pc_begin
< pc_begin
)
468 pc_begin
= this_fde
->pc_begin
;
469 if (this_fde
->pc_begin
+ this_fde
->pc_range
> pc_end
)
470 pc_end
= this_fde
->pc_begin
+ this_fde
->pc_range
;
477 /* Set up a sorted array of pointers to FDEs for a loaded object. We
478 count up the entries before allocating the array because it's likely to
482 frame_init (struct object
* ob
)
485 fde_accumulator accu
;
486 void *pc_begin
, *pc_end
;
490 fde
**p
= ob
->fde_array
;
491 for (count
= 0; *p
; ++p
)
492 count
+= count_fdes (*p
);
495 count
= count_fdes (ob
->fde_begin
);
499 start_fde_sort (&accu
, count
);
500 pc_begin
= (void*)(uaddr
)-1;
505 fde
**p
= ob
->fde_array
;
507 add_fdes (*p
, &accu
, &pc_begin
, &pc_end
);
510 add_fdes (ob
->fde_begin
, &accu
, &pc_begin
, &pc_end
);
512 ob
->fde_array
= end_fde_sort (&accu
, count
);
513 ob
->pc_begin
= pc_begin
;
517 /* Return a pointer to the FDE for the function containing PC. */
525 init_object_mutex_once ();
526 __gthread_mutex_lock (&object_mutex
);
528 for (ob
= objects
; ob
; ob
= ob
->next
)
530 if (ob
->pc_begin
== 0)
532 if (pc
>= ob
->pc_begin
&& pc
< ob
->pc_end
)
536 __gthread_mutex_unlock (&object_mutex
);
541 /* Standard binary search algorithm. */
542 for (lo
= 0, hi
= ob
->count
; lo
< hi
; )
544 size_t i
= (lo
+ hi
) / 2;
545 fde
*f
= ob
->fde_array
[i
];
547 if (pc
< f
->pc_begin
)
549 else if (pc
>= f
->pc_begin
+ f
->pc_range
)
558 static inline struct dwarf_cie
*
561 return ((void *)&f
->CIE_delta
) - f
->CIE_delta
;
564 /* Extract any interesting information from the CIE for the translation
565 unit F belongs to. */
568 extract_cie_info (fde
*f
, struct cie_info
*c
)
573 c
->augmentation
= get_cie (f
)->augmentation
;
575 if (strcmp (c
->augmentation
, "") != 0
576 && strcmp (c
->augmentation
, "eh") != 0
577 && c
->augmentation
[0] != 'z')
580 p
= c
->augmentation
+ strlen (c
->augmentation
) + 1;
582 if (strcmp (c
->augmentation
, "eh") == 0)
584 c
->eh_ptr
= read_pointer (p
);
585 p
+= sizeof (void *);
590 p
= decode_uleb128 (p
, &c
->code_align
);
591 p
= decode_sleb128 (p
, &c
->data_align
);
592 c
->ra_regno
= *(unsigned char *)p
++;
594 /* If the augmentation starts with 'z', we now see the length of the
595 augmentation fields. */
596 if (c
->augmentation
[0] == 'z')
598 p
= decode_uleb128 (p
, &i
);
605 /* Decode one instruction's worth of DWARF 2 call frame information.
606 Used by __frame_state_for. Takes pointers P to the instruction to
607 decode, STATE to the current register unwind information, INFO to the
608 current CIE information, and PC to the current PC value. Returns a
609 pointer to the next instruction. */
612 execute_cfa_insn (void *p
, struct frame_state_internal
*state
,
613 struct cie_info
*info
, void **pc
)
615 unsigned insn
= *(unsigned char *)p
++;
619 if (insn
& DW_CFA_advance_loc
)
620 *pc
+= ((insn
& 0x3f) * info
->code_align
);
621 else if (insn
& DW_CFA_offset
)
624 p
= decode_uleb128 (p
, &offset
);
625 offset
*= info
->data_align
;
626 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
627 state
->s
.reg_or_offset
[reg
] = offset
;
629 else if (insn
& DW_CFA_restore
)
632 state
->s
.saved
[reg
] = REG_UNSAVED
;
637 *pc
= read_pointer (p
);
638 p
+= sizeof (void *);
640 case DW_CFA_advance_loc1
:
641 *pc
+= read_1byte (p
);
644 case DW_CFA_advance_loc2
:
645 *pc
+= read_2byte (p
);
648 case DW_CFA_advance_loc4
:
649 *pc
+= read_4byte (p
);
653 case DW_CFA_offset_extended
:
654 p
= decode_uleb128 (p
, ®
);
655 p
= decode_uleb128 (p
, &offset
);
656 offset
*= info
->data_align
;
657 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
658 state
->s
.reg_or_offset
[reg
] = offset
;
660 case DW_CFA_restore_extended
:
661 p
= decode_uleb128 (p
, ®
);
662 state
->s
.saved
[reg
] = REG_UNSAVED
;
665 case DW_CFA_undefined
:
666 case DW_CFA_same_value
:
670 case DW_CFA_register
:
673 p
= decode_uleb128 (p
, ®
);
674 p
= decode_uleb128 (p
, ®2
);
675 state
->s
.saved
[reg
] = REG_SAVED_REG
;
676 state
->s
.reg_or_offset
[reg
] = reg2
;
681 p
= decode_uleb128 (p
, ®
);
682 p
= decode_uleb128 (p
, &offset
);
683 state
->s
.cfa_reg
= reg
;
684 state
->s
.cfa_offset
= offset
;
686 case DW_CFA_def_cfa_register
:
687 p
= decode_uleb128 (p
, ®
);
688 state
->s
.cfa_reg
= reg
;
690 case DW_CFA_def_cfa_offset
:
691 p
= decode_uleb128 (p
, &offset
);
692 state
->s
.cfa_offset
= offset
;
695 case DW_CFA_remember_state
:
697 struct frame_state_internal
*save
=
698 (struct frame_state_internal
*)
699 malloc (sizeof (struct frame_state_internal
));
700 memcpy (save
, state
, sizeof (struct frame_state_internal
));
701 state
->saved_state
= save
;
704 case DW_CFA_restore_state
:
706 struct frame_state_internal
*save
= state
->saved_state
;
707 memcpy (state
, save
, sizeof (struct frame_state_internal
));
712 /* FIXME: Hardcoded for SPARC register window configuration. */
713 case DW_CFA_GNU_window_save
:
714 for (reg
= 16; reg
< 32; ++reg
)
716 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
717 state
->s
.reg_or_offset
[reg
] = (reg
- 16) * sizeof (void *);
721 case DW_CFA_GNU_args_size
:
722 p
= decode_uleb128 (p
, &offset
);
723 state
->s
.args_size
= offset
;
732 /* Called from crtbegin.o to register the unwind info for an object. */
735 __register_frame_info (void *begin
, struct object
*ob
)
737 ob
->fde_begin
= begin
;
739 ob
->pc_begin
= ob
->pc_end
= 0;
743 init_object_mutex_once ();
744 __gthread_mutex_lock (&object_mutex
);
749 __gthread_mutex_unlock (&object_mutex
);
753 __register_frame (void *begin
)
755 struct object
*ob
= (struct object
*) malloc (sizeof (struct object
));
756 __register_frame_info (begin
, ob
);
759 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
760 for different translation units. Called from the file generated by
764 __register_frame_info_table (void *begin
, struct object
*ob
)
766 ob
->fde_begin
= begin
;
767 ob
->fde_array
= begin
;
769 ob
->pc_begin
= ob
->pc_end
= 0;
772 init_object_mutex_once ();
773 __gthread_mutex_lock (&object_mutex
);
778 __gthread_mutex_unlock (&object_mutex
);
782 __register_frame_table (void *begin
)
784 struct object
*ob
= (struct object
*) malloc (sizeof (struct object
));
785 __register_frame_info_table (begin
, ob
);
788 /* Called from crtbegin.o to deregister the unwind info for an object. */
791 __deregister_frame_info (void *begin
)
795 init_object_mutex_once ();
796 __gthread_mutex_lock (&object_mutex
);
801 if ((*p
)->fde_begin
== begin
)
803 struct object
*ob
= *p
;
806 /* If we've run init_frame for this object, free the FDE array. */
808 free (ob
->fde_array
);
810 __gthread_mutex_unlock (&object_mutex
);
816 __gthread_mutex_unlock (&object_mutex
);
821 __deregister_frame (void *begin
)
823 free (__deregister_frame_info (begin
));
826 /* Called from __throw to find the registers to restore for a given
827 PC_TARGET. The caller should allocate a local variable of `struct
828 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
831 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
834 void *insn
, *end
, *pc
;
835 struct cie_info info
;
836 struct frame_state_internal state
;
838 f
= find_fde (pc_target
);
842 insn
= extract_cie_info (f
, &info
);
846 memset (&state
, 0, sizeof (state
));
847 state
.s
.retaddr_column
= info
.ra_regno
;
848 state
.s
.eh_ptr
= info
.eh_ptr
;
850 /* First decode all the insns in the CIE. */
851 end
= next_fde ((fde
*) get_cie (f
));
853 insn
= execute_cfa_insn (insn
, &state
, &info
, 0);
855 insn
= ((fde
*)f
) + 1;
857 if (info
.augmentation
[0] == 'z')
860 insn
= decode_uleb128 (insn
, &i
);
864 /* Then the insns in the FDE up to our target PC. */
867 while (insn
< end
&& pc
<= pc_target
)
868 insn
= execute_cfa_insn (insn
, &state
, &info
, &pc
);
870 memcpy (state_in
, &state
.s
, sizeof (state
.s
));
873 #endif /* DWARF2_UNWIND_INFO */