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)
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
37 #ifdef DWARF2_UNWIND_INFO
38 #include "gansidecl.h"
43 /* Don't use `fancy_abort' here even if config.h says to use it. */
48 /* Some types used by the DWARF 2 spec. */
50 typedef int sword
__attribute__ ((mode (SI
)));
51 typedef unsigned int uword
__attribute__ ((mode (SI
)));
52 typedef unsigned int uaddr
__attribute__ ((mode (pointer
)));
53 typedef int saddr
__attribute__ ((mode (pointer
)));
54 typedef unsigned char ubyte
;
56 /* The first few fields of a CIE. The CIE_id field is 0xffffffff for a CIE,
57 to distinguish it from a valid FDE. FDEs are aligned to an addressing
58 unit boundary, but the fields within are unaligned. */
65 } __attribute__ ((packed
, aligned (__alignof__ (void *))));
67 /* The first few fields of an FDE. */
74 } __attribute__ ((packed
, aligned (__alignof__ (void *))));
76 typedef struct dwarf_fde fde
;
78 /* The representation for an "object" to be searched for frame unwind info.
79 For targets with named sections, one object is an executable or shared
80 library; for other targets, one object is one translation unit. */
91 static struct object
*objects
;
93 /* The information we care about from a CIE. */
103 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
105 struct frame_state_internal
107 struct frame_state s
;
108 struct frame_state_internal
*saved_state
;
111 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
112 by R, and return the new value of BUF. */
115 decode_uleb128 (unsigned char *buf
, unsigned *r
)
122 unsigned byte
= *buf
++;
123 result
|= (byte
& 0x7f) << shift
;
124 if ((byte
& 0x80) == 0)
132 /* Decode the signed LEB128 constant at BUF into the variable pointed to
133 by R, and return the new value of BUF. */
136 decode_sleb128 (unsigned char *buf
, int *r
)
145 result
|= (byte
& 0x7f) << shift
;
147 if ((byte
& 0x80) == 0)
150 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
151 result
|= - (1 << shift
);
157 /* Read unaligned data from the instruction buffer. */
161 unsigned b2
__attribute__ ((mode (HI
)));
162 unsigned b4
__attribute__ ((mode (SI
)));
163 unsigned b8
__attribute__ ((mode (DI
)));
164 } __attribute__ ((packed
));
166 read_pointer (void *p
)
167 { union unaligned
*up
= p
; return up
->p
; }
168 static inline unsigned
170 { return *(unsigned char *)p
; }
171 static inline unsigned
173 { union unaligned
*up
= p
; return up
->b2
; }
174 static inline unsigned
176 { union unaligned
*up
= p
; return up
->b4
; }
177 static inline unsigned long
179 { union unaligned
*up
= p
; return up
->b8
; }
181 /* Ordering function for FDEs. Functions can't overlap, so we just compare
182 their starting addresses. */
185 fde_compare (fde
*x
, fde
*y
)
187 return (saddr
)x
->pc_begin
- (saddr
)y
->pc_begin
;
190 /* Return the address of the FDE after P. */
195 return (fde
*)(((char *)p
) + p
->length
+ sizeof (p
->length
));
198 /* One iteration of an insertion sort, for adding new FDEs to the array.
199 Usually the new FDE will go in at the end, so we can expect close to
200 O(n) performance. If this turns out to be overly optimistic, we can have
201 the linker sort the FDEs so we don't have to do it at run time. */
204 fde_insert (fde
**array
, size_t i
, fde
*this_fde
)
208 for (; i
> 0 && fde_compare (array
[i
], array
[i
-1]) < 0; --i
)
211 array
[i
] = array
[i
-1];
212 array
[i
-1] = this_fde
;
217 count_fdes (fde
*this_fde
)
221 for (count
= 0; this_fde
->length
!= 0; this_fde
= next_fde (this_fde
))
223 /* Skip CIEs and linked once FDE entries. */
224 if (this_fde
->CIE_delta
== 0 || this_fde
->pc_begin
== 0)
234 add_fdes (fde
*this_fde
, fde
**array
, size_t *i_ptr
,
235 void **beg_ptr
, void **end_ptr
)
238 void *pc_begin
= *beg_ptr
;
239 void *pc_end
= *end_ptr
;
241 for (; this_fde
->length
!= 0; this_fde
= next_fde (this_fde
))
243 /* Skip CIEs and linked once FDE entries. */
244 if (this_fde
->CIE_delta
== 0 || this_fde
->pc_begin
== 0)
247 fde_insert (array
, i
++, this_fde
);
249 if (this_fde
->pc_begin
< pc_begin
)
250 pc_begin
= this_fde
->pc_begin
;
251 if (this_fde
->pc_begin
+ this_fde
->pc_range
> pc_end
)
252 pc_end
= this_fde
->pc_begin
+ this_fde
->pc_range
;
260 /* Set up a sorted array of pointers to FDEs for a loaded object. We
261 count up the entries before allocating the array because it's likely to
265 frame_init (struct object
* ob
)
270 void *pc_begin
, *pc_end
;
274 fde
**p
= ob
->fde_array
;
275 for (count
= 0; *p
; ++p
)
276 count
+= count_fdes (*p
);
279 count
= count_fdes (ob
->fde_begin
);
282 array
= (fde
**) malloc (sizeof (fde
*) * count
);
284 pc_begin
= (void*)(uaddr
)-1;
290 fde
**p
= ob
->fde_array
;
292 add_fdes (*p
, array
, &count
, &pc_begin
, &pc_end
);
295 add_fdes (ob
->fde_begin
, array
, &count
, &pc_begin
, &pc_end
);
297 ob
->fde_array
= array
;
298 ob
->pc_begin
= pc_begin
;
302 /* Return a pointer to the FDE for the function containing PC. */
310 for (ob
= objects
; ob
; ob
= ob
->next
)
312 if (ob
->pc_begin
== 0)
314 if (pc
>= ob
->pc_begin
&& pc
< ob
->pc_end
)
321 /* Standard binary search algorithm. */
322 for (lo
= 0, hi
= ob
->count
; lo
< hi
; )
324 size_t i
= (lo
+ hi
) / 2;
325 fde
*f
= ob
->fde_array
[i
];
327 if (pc
< f
->pc_begin
)
329 else if (pc
> f
->pc_begin
+ f
->pc_range
)
338 static inline struct dwarf_cie
*
341 return ((void *)&f
->CIE_delta
) - f
->CIE_delta
;
344 /* Extract any interesting information from the CIE for the translation
345 unit F belongs to. */
348 extract_cie_info (fde
*f
, struct cie_info
*c
)
353 c
->augmentation
= get_cie (f
)->augmentation
;
355 if (strcmp (c
->augmentation
, "") != 0
356 && strcmp (c
->augmentation
, "eh") != 0
357 && c
->augmentation
[0] != 'z')
360 p
= c
->augmentation
+ strlen (c
->augmentation
) + 1;
362 if (strcmp (c
->augmentation
, "eh") == 0)
364 c
->eh_ptr
= read_pointer (p
);
365 p
+= sizeof (void *);
370 p
= decode_uleb128 (p
, &c
->code_align
);
371 p
= decode_sleb128 (p
, &c
->data_align
);
372 c
->ra_regno
= *(unsigned char *)p
++;
374 /* If the augmentation starts with 'z', we now see the length of the
375 augmentation fields. */
376 if (c
->augmentation
[0] == 'z')
378 p
= decode_uleb128 (p
, &i
);
385 /* Decode one instruction's worth of of DWARF 2 call frame information.
386 Used by __frame_state_for. Takes pointers P to the instruction to
387 decode, STATE to the current register unwind information, INFO to the
388 current CIE information, and PC to the current PC value. Returns a
389 pointer to the next instruction. */
392 execute_cfa_insn (void *p
, struct frame_state_internal
*state
,
393 struct cie_info
*info
, void **pc
)
395 unsigned insn
= *(unsigned char *)p
++;
399 if (insn
& DW_CFA_advance_loc
)
400 *pc
+= ((insn
& 0x3f) * info
->code_align
);
401 else if (insn
& DW_CFA_offset
)
404 p
= decode_uleb128 (p
, &offset
);
405 offset
*= info
->data_align
;
406 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
407 state
->s
.reg_or_offset
[reg
] = offset
;
409 else if (insn
& DW_CFA_restore
)
412 state
->s
.saved
[reg
] = REG_UNSAVED
;
417 *pc
= read_pointer (p
);
418 p
+= sizeof (void *);
420 case DW_CFA_advance_loc1
:
421 *pc
+= read_1byte (p
);
424 case DW_CFA_advance_loc2
:
425 *pc
+= read_2byte (p
);
428 case DW_CFA_advance_loc4
:
429 *pc
+= read_4byte (p
);
433 case DW_CFA_offset_extended
:
434 p
= decode_uleb128 (p
, ®
);
435 p
= decode_uleb128 (p
, &offset
);
436 offset
*= info
->data_align
;
437 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
438 state
->s
.reg_or_offset
[reg
] = offset
;
440 case DW_CFA_restore_extended
:
441 p
= decode_uleb128 (p
, ®
);
442 state
->s
.saved
[reg
] = REG_UNSAVED
;
445 case DW_CFA_undefined
:
446 case DW_CFA_same_value
:
450 case DW_CFA_register
:
453 p
= decode_uleb128 (p
, ®
);
454 p
= decode_uleb128 (p
, ®2
);
455 state
->s
.saved
[reg
] = REG_SAVED_REG
;
456 state
->s
.reg_or_offset
[reg
] = reg2
;
461 p
= decode_uleb128 (p
, ®
);
462 p
= decode_uleb128 (p
, &offset
);
463 state
->s
.cfa_reg
= reg
;
464 state
->s
.cfa_offset
= offset
;
466 case DW_CFA_def_cfa_register
:
467 p
= decode_uleb128 (p
, ®
);
468 state
->s
.cfa_reg
= reg
;
470 case DW_CFA_def_cfa_offset
:
471 p
= decode_uleb128 (p
, &offset
);
472 state
->s
.cfa_offset
= offset
;
475 case DW_CFA_remember_state
:
477 struct frame_state_internal
*save
=
478 (struct frame_state_internal
*)
479 malloc (sizeof (struct frame_state_internal
));
480 memcpy (save
, state
, sizeof (struct frame_state_internal
));
481 state
->saved_state
= save
;
484 case DW_CFA_restore_state
:
486 struct frame_state_internal
*save
= state
->saved_state
;
487 memcpy (state
, save
, sizeof (struct frame_state_internal
));
492 /* FIXME: Hardcoded for SPARC register window configuration. */
493 case DW_CFA_GNU_window_save
:
494 for (reg
= 16; reg
< 32; ++reg
)
496 state
->s
.saved
[reg
] = REG_SAVED_OFFSET
;
497 state
->s
.reg_or_offset
[reg
] = (reg
- 16) * sizeof (void *);
501 case DW_CFA_GNU_args_size
:
502 p
= decode_uleb128 (p
, &offset
);
503 state
->s
.args_size
= offset
;
512 /* Called from crtbegin.o to register the unwind info for an object. */
515 __register_frame (void *begin
)
517 struct object
*ob
= (struct object
*) malloc (sizeof (struct object
));
519 ob
->fde_begin
= begin
;
521 ob
->pc_begin
= ob
->pc_end
= 0;
529 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
530 for different translation units. Called from the file generated by
534 __register_frame_table (void *begin
)
536 struct object
*ob
= (struct object
*) malloc (sizeof (struct object
));
538 ob
->fde_begin
= begin
;
539 ob
->fde_array
= begin
;
541 ob
->pc_begin
= ob
->pc_end
= 0;
548 /* Called from crtend.o to deregister the unwind info for an object. */
551 __deregister_frame (void *begin
)
553 struct object
**p
= &objects
;
557 if ((*p
)->fde_begin
== begin
)
559 struct object
*ob
= *p
;
562 /* If we've run init_frame for this object, free the FDE array. */
564 free (ob
->fde_array
);
574 /* Called from __throw to find the registers to restore for a given
575 PC_TARGET. The caller should allocate a local variable of `struct
576 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
579 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
582 void *insn
, *end
, *pc
;
583 struct cie_info info
;
584 struct frame_state_internal state
;
586 f
= find_fde (pc_target
);
590 insn
= extract_cie_info (f
, &info
);
594 memset (&state
, 0, sizeof (state
));
595 state
.s
.retaddr_column
= info
.ra_regno
;
596 state
.s
.eh_ptr
= info
.eh_ptr
;
598 /* First decode all the insns in the CIE. */
599 end
= next_fde ((fde
*) get_cie (f
));
601 insn
= execute_cfa_insn (insn
, &state
, &info
, 0);
603 insn
= ((fde
*)f
) + 1;
605 if (info
.augmentation
[0] == 'z')
608 insn
= decode_uleb128 (insn
, &i
);
612 /* Then the insns in the FDE up to our target PC. */
615 while (insn
< end
&& pc
<= pc_target
)
616 insn
= execute_cfa_insn (insn
, &state
, &info
, &pc
);
618 memcpy (state_in
, &state
.s
, sizeof (state
.s
));
621 #endif /* DWARF2_UNWIND_INFO */