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
42 /* Don't use `fancy_abort' here even if config.h says to use it. */
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. */
64 } __attribute__ ((packed
, aligned (__alignof__ (void *))));
66 /* The first few fields of an FDE. */
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. */
91 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
93 struct frame_state_internal
96 struct frame_state_internal
*saved_state
;
99 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
100 by R, and return the new value of BUF. */
103 decode_uleb128 (unsigned char *buf
, unsigned *r
)
110 unsigned byte
= *buf
++;
111 result
|= (byte
& 0x7f) << shift
;
112 if ((byte
& 0x80) == 0)
120 /* Decode the signed LEB128 constant at BUF into the variable pointed to
121 by R, and return the new value of BUF. */
124 decode_sleb128 (unsigned char *buf
, int *r
)
133 result
|= (byte
& 0x7f) << shift
;
135 if ((byte
& 0x80) == 0)
138 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
139 result
|= - (1 << shift
);
145 /* Read unaligned data from the instruction buffer. */
149 unsigned b2
__attribute__ ((mode (HI
)));
150 unsigned b4
__attribute__ ((mode (SI
)));
151 unsigned b8
__attribute__ ((mode (DI
)));
152 } __attribute__ ((packed
));
154 read_pointer (void *p
)
155 { union unaligned
*up
= p
; return up
->p
; }
156 static inline unsigned
158 { return *(unsigned char *)p
; }
159 static inline unsigned
161 { union unaligned
*up
= p
; return up
->b2
; }
162 static inline unsigned
164 { union unaligned
*up
= p
; return up
->b4
; }
165 static inline unsigned long
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. */
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. */
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. */
192 fde_insert (fde
**array
, size_t i
, fde
*this_fde
)
196 for (; i
> 0 && fde_compare (array
[i
], array
[i
-1]) < 0; --i
)
199 array
[i
] = array
[i
-1];
200 array
[i
-1] = this_fde
;
205 count_fdes (fde
*this_fde
)
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)
222 add_fdes (fde
*this_fde
, fde
**array
, size_t *i_ptr
,
223 void **beg_ptr
, void **end_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)
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
;
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
253 frame_init (struct object
* ob
)
258 void *pc_begin
, *pc_end
;
262 fde
**p
= ob
->fde_array
;
263 for (count
= 0; *p
; ++p
)
264 count
+= count_fdes (*p
);
267 count
= count_fdes (ob
->fde_begin
);
270 array
= (fde
**) malloc (sizeof (fde
*) * count
);
272 pc_begin
= (void*)(uaddr
)-1;
278 fde
**p
= ob
->fde_array
;
280 add_fdes (*p
, array
, &count
, &pc_begin
, &pc_end
);
283 add_fdes (ob
->fde_begin
, array
, &count
, &pc_begin
, &pc_end
);
285 ob
->fde_array
= array
;
286 ob
->pc_begin
= pc_begin
;
290 /* Return a pointer to the FDE for the function containing PC. */
298 for (ob
= objects
; ob
; ob
= ob
->next
)
300 if (ob
->pc_begin
== 0)
302 if (pc
>= ob
->pc_begin
&& pc
< ob
->pc_end
)
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
)
317 else if (pc
> f
->pc_begin
+ f
->pc_range
)
326 static inline struct dwarf_cie
*
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. */
336 extract_cie_info (fde
*f
, struct cie_info
*c
)
341 c
->augmentation
= get_cie (f
)->augmentation
;
343 if (strcmp (c
->augmentation
, "") != 0
344 && strcmp (c
->augmentation
, "eh") != 0
345 && c
->augmentation
[0] != 'z')
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 *);
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
);
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. */
380 execute_cfa_insn (void *p
, struct frame_state_internal
*state
,
381 struct cie_info
*info
, void **pc
)
383 unsigned insn
= *(unsigned char *)p
++;
387 if (insn
& DW_CFA_advance_loc
)
388 *pc
+= ((insn
& 0x3f) * info
->code_align
);
389 else if (insn
& DW_CFA_offset
)
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
)
400 state
->s
.saved
[reg
] = REG_UNSAVED
;
405 *pc
= read_pointer (p
);
406 p
+= sizeof (void *);
408 case DW_CFA_advance_loc1
:
409 *pc
+= read_1byte (p
);
412 case DW_CFA_advance_loc2
:
413 *pc
+= read_2byte (p
);
416 case DW_CFA_advance_loc4
:
417 *pc
+= read_4byte (p
);
421 case DW_CFA_offset_extended
:
422 p
= decode_uleb128 (p
, ®
);
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
;
428 case DW_CFA_restore_extended
:
429 p
= decode_uleb128 (p
, ®
);
430 state
->s
.saved
[reg
] = REG_UNSAVED
;
433 case DW_CFA_undefined
:
434 case DW_CFA_same_value
:
438 case DW_CFA_register
:
441 p
= decode_uleb128 (p
, ®
);
442 p
= decode_uleb128 (p
, ®2
);
443 state
->s
.saved
[reg
] = REG_SAVED_REG
;
444 state
->s
.reg_or_offset
[reg
] = reg2
;
449 p
= decode_uleb128 (p
, ®
);
450 p
= decode_uleb128 (p
, &offset
);
451 state
->s
.cfa_reg
= reg
;
452 state
->s
.cfa_offset
= offset
;
454 case DW_CFA_def_cfa_register
:
455 p
= decode_uleb128 (p
, ®
);
456 state
->s
.cfa_reg
= reg
;
458 case DW_CFA_def_cfa_offset
:
459 p
= decode_uleb128 (p
, &offset
);
460 state
->s
.cfa_offset
= offset
;
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
;
472 case DW_CFA_restore_state
:
474 struct frame_state_internal
*save
= state
->saved_state
;
475 memcpy (state
, save
, sizeof (struct frame_state_internal
));
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 *);
489 case DW_CFA_GNU_args_size
:
490 p
= decode_uleb128 (p
, &offset
);
491 state
->s
.args_size
= offset
;
500 /* Called from crtbegin.o to register the unwind info for an object. */
503 __register_frame_info (void *begin
, struct object
*ob
)
505 ob
->fde_begin
= begin
;
507 ob
->pc_begin
= ob
->pc_end
= 0;
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
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;
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. */
549 __deregister_frame_info (void *begin
)
556 if ((*p
)->fde_begin
== begin
)
558 struct object
*ob
= *p
;
561 /* If we've run init_frame for this object, free the FDE array. */
563 free (ob
->fde_array
);
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. */
584 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
587 void *insn
, *end
, *pc
;
588 struct cie_info info
;
589 struct frame_state_internal state
;
591 f
= find_fde (pc_target
);
595 insn
= extract_cie_info (f
, &info
);
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
));
606 insn
= execute_cfa_insn (insn
, &state
, &info
, 0);
608 insn
= ((fde
*)f
) + 1;
610 if (info
.augmentation
[0] == 'z')
613 insn
= decode_uleb128 (insn
, &i
);
617 /* Then the insns in the FDE up to our target PC. */
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
));
626 #endif /* DWARF2_UNWIND_INFO */