1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 GCC is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 License for more details.
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 #include "coretypes.h"
37 #ifdef __USING_SJLJ_EXCEPTIONS__
38 # define NO_SIZE_OF_ENCODED_VALUE
40 #include "unwind-pe.h"
41 #include "unwind-dw2-fde.h"
43 #include "unwind-dw2.h"
45 #ifndef __USING_SJLJ_EXCEPTIONS__
47 #ifndef STACK_GROWS_DOWNWARD
48 #define STACK_GROWS_DOWNWARD 0
50 #undef STACK_GROWS_DOWNWARD
51 #define STACK_GROWS_DOWNWARD 1
54 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
55 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
56 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
59 #ifndef DWARF_REG_TO_UNWIND_COLUMN
60 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
63 /* This is the register and unwind state for a particular frame. This
64 provides the information necessary to unwind up past a frame and return
66 struct _Unwind_Context
68 void *reg
[DWARF_FRAME_REGISTERS
+1];
72 struct dwarf_eh_bases bases
;
73 _Unwind_Word args_size
;
76 /* Byte size of every register managed by these routines. */
77 static unsigned char dwarf_reg_size_table
[DWARF_FRAME_REGISTERS
+1];
80 /* Read unaligned data from the instruction buffer. */
85 unsigned u2
__attribute__ ((mode (HI
)));
86 unsigned u4
__attribute__ ((mode (SI
)));
87 unsigned u8
__attribute__ ((mode (DI
)));
88 signed s2
__attribute__ ((mode (HI
)));
89 signed s4
__attribute__ ((mode (SI
)));
90 signed s8
__attribute__ ((mode (DI
)));
91 } __attribute__ ((packed
));
94 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
97 read_1u (const void *p
) { return *(const unsigned char *) p
; }
100 read_1s (const void *p
) { return *(const signed char *) p
; }
103 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
106 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
108 static inline unsigned int
109 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
112 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
114 static inline unsigned long
115 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
117 static inline unsigned long
118 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
120 /* Get the value of register REG as saved in CONTEXT. */
123 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
128 #ifdef DWARF_ZERO_REG
129 if (index
== DWARF_ZERO_REG
)
133 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
134 if (index
>= (int) sizeof(dwarf_reg_size_table
))
136 size
= dwarf_reg_size_table
[index
];
137 ptr
= context
->reg
[index
];
139 /* This will segfault if the register hasn't been saved. */
140 if (size
== sizeof(_Unwind_Ptr
))
141 return * (_Unwind_Ptr
*) ptr
;
143 if (size
== sizeof(_Unwind_Word
))
144 return * (_Unwind_Word
*) ptr
;
150 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
152 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
155 /* Get the value of the CFA as saved in CONTEXT. */
158 _Unwind_GetCFA (struct _Unwind_Context
*context
)
160 return (_Unwind_Ptr
) context
->cfa
;
163 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
166 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
171 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
172 if (index
>= (int) sizeof(dwarf_reg_size_table
))
174 size
= dwarf_reg_size_table
[index
];
175 ptr
= context
->reg
[index
];
177 if (size
== sizeof(_Unwind_Ptr
))
178 * (_Unwind_Ptr
*) ptr
= val
;
179 else if (size
== sizeof(_Unwind_Word
))
180 * (_Unwind_Word
*) ptr
= val
;
185 /* Get the pointer to a register INDEX as saved in CONTEXT. */
188 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
190 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
191 return context
->reg
[index
];
194 /* Set the pointer to a register INDEX as saved in CONTEXT. */
197 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
199 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
200 context
->reg
[index
] = p
;
203 /* Retrieve the return address for CONTEXT. */
206 _Unwind_GetIP (struct _Unwind_Context
*context
)
208 return (_Unwind_Ptr
) context
->ra
;
211 /* Overwrite the return address for CONTEXT with VAL. */
214 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
216 context
->ra
= (void *) val
;
220 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
222 return context
->lsda
;
226 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
228 return (_Unwind_Ptr
) context
->bases
.func
;
232 _Unwind_FindEnclosingFunction (void *pc
)
234 struct dwarf_eh_bases bases
;
235 const struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
244 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
246 return (_Unwind_Ptr
) context
->bases
.dbase
;
250 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
252 return (_Unwind_Ptr
) context
->bases
.tbase
;
256 #ifdef MD_UNWIND_SUPPORT
257 #include MD_UNWIND_SUPPORT
260 /* Extract any interesting information from the CIE for the translation
261 unit F belongs to. Return a pointer to the byte after the augmentation,
262 or NULL if we encountered an undecipherable augmentation. */
264 static const unsigned char *
265 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
266 _Unwind_FrameState
*fs
)
268 const unsigned char *aug
= cie
->augmentation
;
269 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
270 const unsigned char *ret
= NULL
;
273 /* g++ v2 "eh" has pointer immediately following augmentation string,
274 so it must be handled first. */
275 if (aug
[0] == 'e' && aug
[1] == 'h')
277 fs
->eh_ptr
= read_pointer (p
);
278 p
+= sizeof (void *);
282 /* Immediately following the augmentation are the code and
283 data alignment and return address column. */
284 p
= read_uleb128 (p
, &fs
->code_align
);
285 p
= read_sleb128 (p
, &fs
->data_align
);
286 if (cie
->version
== 1)
287 fs
->retaddr_column
= *p
++;
289 p
= read_uleb128 (p
, &fs
->retaddr_column
);
290 fs
->lsda_encoding
= DW_EH_PE_omit
;
292 /* If the augmentation starts with 'z', then a uleb128 immediately
293 follows containing the length of the augmentation field following
297 p
= read_uleb128 (p
, &utmp
);
304 /* Iterate over recognized augmentation subsequences. */
307 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
310 fs
->lsda_encoding
= *p
++;
314 /* "R" indicates a byte indicating how FDE addresses are encoded. */
315 else if (aug
[0] == 'R')
317 fs
->fde_encoding
= *p
++;
321 /* "P" indicates a personality routine in the CIE augmentation. */
322 else if (aug
[0] == 'P')
324 p
= read_encoded_value (context
, *p
, p
+ 1,
325 (_Unwind_Ptr
*) &fs
->personality
);
329 /* Otherwise we have an unknown augmentation string.
330 Bail unless we saw a 'z' prefix. */
335 return ret
? ret
: p
;
339 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
340 onto the stack to start. */
343 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
344 struct _Unwind_Context
*context
, _Unwind_Word initial
)
346 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
352 while (op_ptr
< op_end
)
354 enum dwarf_location_atom op
= *op_ptr
++;
355 _Unwind_Word result
, reg
, utmp
;
356 _Unwind_Sword offset
, stmp
;
392 result
= op
- DW_OP_lit0
;
396 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
397 op_ptr
+= sizeof (void *);
401 result
= read_1u (op_ptr
);
405 result
= read_1s (op_ptr
);
409 result
= read_2u (op_ptr
);
413 result
= read_2s (op_ptr
);
417 result
= read_4u (op_ptr
);
421 result
= read_4s (op_ptr
);
425 result
= read_8u (op_ptr
);
429 result
= read_8s (op_ptr
);
433 op_ptr
= read_uleb128 (op_ptr
, &result
);
436 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
472 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
475 op_ptr
= read_uleb128 (op_ptr
, ®
);
476 result
= _Unwind_GetGR (context
, reg
);
511 op_ptr
= read_sleb128 (op_ptr
, &offset
);
512 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
515 op_ptr
= read_uleb128 (op_ptr
, ®
);
516 op_ptr
= read_sleb128 (op_ptr
, &offset
);
517 result
= _Unwind_GetGR (context
, reg
) + offset
;
523 result
= stack
[stack_elt
- 1];
533 if (offset
>= stack_elt
- 1)
535 result
= stack
[stack_elt
- 1 - offset
];
541 result
= stack
[stack_elt
- 2];
546 _Unwind_Word t1
, t2
, t3
;
550 t1
= stack
[stack_elt
- 1];
551 t2
= stack
[stack_elt
- 2];
552 t3
= stack
[stack_elt
- 3];
553 stack
[stack_elt
- 1] = t2
;
554 stack
[stack_elt
- 2] = t3
;
555 stack
[stack_elt
- 3] = t1
;
560 case DW_OP_deref_size
:
564 case DW_OP_plus_uconst
:
565 /* Unary operations. */
568 result
= stack
[stack_elt
];
574 void *ptr
= (void *) (_Unwind_Ptr
) result
;
575 result
= (_Unwind_Ptr
) read_pointer (ptr
);
579 case DW_OP_deref_size
:
581 void *ptr
= (void *) (_Unwind_Ptr
) result
;
585 result
= read_1u (ptr
);
588 result
= read_2u (ptr
);
591 result
= read_4u (ptr
);
594 result
= read_8u (ptr
);
603 if ((_Unwind_Sword
) result
< 0)
612 case DW_OP_plus_uconst
:
613 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
636 /* Binary operations. */
637 _Unwind_Word first
, second
;
638 if ((stack_elt
-= 2) < 0)
640 second
= stack
[stack_elt
];
641 first
= stack
[stack_elt
+ 1];
646 result
= second
& first
;
649 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
652 result
= second
- first
;
655 result
= (_Unwind_Sword
) second
% (_Unwind_Sword
) first
;
658 result
= second
* first
;
661 result
= second
| first
;
664 result
= second
+ first
;
667 result
= second
<< first
;
670 result
= second
>> first
;
673 result
= (_Unwind_Sword
) second
>> first
;
676 result
= second
^ first
;
679 result
= (_Unwind_Sword
) first
<= (_Unwind_Sword
) second
;
682 result
= (_Unwind_Sword
) first
>= (_Unwind_Sword
) second
;
685 result
= (_Unwind_Sword
) first
== (_Unwind_Sword
) second
;
688 result
= (_Unwind_Sword
) first
< (_Unwind_Sword
) second
;
691 result
= (_Unwind_Sword
) first
> (_Unwind_Sword
) second
;
694 result
= (_Unwind_Sword
) first
!= (_Unwind_Sword
) second
;
704 offset
= read_2s (op_ptr
);
712 offset
= read_2s (op_ptr
);
714 if (stack
[stack_elt
] != 0)
725 /* Most things push a result value. */
726 if ((size_t) stack_elt
>= sizeof(stack
)/sizeof(*stack
))
728 stack
[stack_elt
++] = result
;
732 /* We were executing this program to get a value. It should be
736 return stack
[stack_elt
];
740 /* Decode DWARF 2 call frame information. Takes pointers the
741 instruction sequence to decode, current register information and
742 CIE info, and the PC range to evaluate. */
745 execute_cfa_program (const unsigned char *insn_ptr
,
746 const unsigned char *insn_end
,
747 struct _Unwind_Context
*context
,
748 _Unwind_FrameState
*fs
)
750 struct frame_state_reg_info
*unused_rs
= NULL
;
752 /* Don't allow remember/restore between CIE and FDE programs. */
753 fs
->regs
.prev
= NULL
;
755 /* The comparison with the return address uses < rather than <= because
756 we are only interested in the effects of code before the call; for a
757 noreturn function, the return address may point to unrelated code with
758 a different stack configuration that we are not interested in. We
759 assume that the call itself is unwind info-neutral; if not, or if
760 there are delay instructions that adjust the stack, these must be
761 reflected at the point immediately before the call insn. */
762 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
)
764 unsigned char insn
= *insn_ptr
++;
765 _Unwind_Word reg
, utmp
;
766 _Unwind_Sword offset
, stmp
;
768 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
769 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
770 else if ((insn
& 0xc0) == DW_CFA_offset
)
773 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
774 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
775 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
777 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
779 else if ((insn
& 0xc0) == DW_CFA_restore
)
782 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_UNSAVED
;
787 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
788 insn_ptr
, (_Unwind_Ptr
*) &fs
->pc
);
791 case DW_CFA_advance_loc1
:
792 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
795 case DW_CFA_advance_loc2
:
796 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
799 case DW_CFA_advance_loc4
:
800 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
804 case DW_CFA_offset_extended
:
805 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
806 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
807 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
808 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
810 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
813 case DW_CFA_restore_extended
:
814 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
815 /* FIXME, this is wrong; the CIE might have said that the
816 register was saved somewhere. */
817 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
820 case DW_CFA_undefined
:
821 case DW_CFA_same_value
:
822 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
823 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
829 case DW_CFA_register
:
832 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
833 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
834 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_REG
;
835 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.reg
= reg2
;
839 case DW_CFA_remember_state
:
841 struct frame_state_reg_info
*new_rs
;
845 unused_rs
= unused_rs
->prev
;
848 new_rs
= alloca (sizeof (struct frame_state_reg_info
));
851 fs
->regs
.prev
= new_rs
;
855 case DW_CFA_restore_state
:
857 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
859 old_rs
->prev
= unused_rs
;
865 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
866 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
867 fs
->cfa_offset
= utmp
;
868 fs
->cfa_how
= CFA_REG_OFFSET
;
871 case DW_CFA_def_cfa_register
:
872 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
873 fs
->cfa_how
= CFA_REG_OFFSET
;
876 case DW_CFA_def_cfa_offset
:
877 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
878 fs
->cfa_offset
= utmp
;
879 /* cfa_how deliberately not set. */
882 case DW_CFA_def_cfa_expression
:
883 fs
->cfa_exp
= insn_ptr
;
884 fs
->cfa_how
= CFA_EXP
;
885 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
889 case DW_CFA_expression
:
890 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
891 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_EXP
;
892 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
893 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
897 /* From the 2.1 draft. */
898 case DW_CFA_offset_extended_sf
:
899 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
900 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
901 offset
= stmp
* fs
->data_align
;
902 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
904 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
907 case DW_CFA_def_cfa_sf
:
908 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
909 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
910 fs
->cfa_how
= CFA_REG_OFFSET
;
913 case DW_CFA_def_cfa_offset_sf
:
914 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
915 /* cfa_how deliberately not set. */
918 case DW_CFA_GNU_window_save
:
919 /* ??? Hardcoded for SPARC register window configuration. */
920 for (reg
= 16; reg
< 32; ++reg
)
922 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
923 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
927 case DW_CFA_GNU_args_size
:
928 insn_ptr
= read_uleb128 (insn_ptr
, &context
->args_size
);
931 case DW_CFA_GNU_negative_offset_extended
:
932 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
933 older PowerPC code. */
934 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
935 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
936 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
937 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
939 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= -offset
;
948 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
949 its caller and decode it into FS. This function also sets the
950 args_size and lsda members of CONTEXT, as they are really information
951 about the caller's frame. */
953 static _Unwind_Reason_Code
954 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
956 const struct dwarf_fde
*fde
;
957 const struct dwarf_cie
*cie
;
958 const unsigned char *aug
, *insn
, *end
;
960 memset (fs
, 0, sizeof (*fs
));
961 context
->args_size
= 0;
964 if (context
->ra
== 0)
965 return _URC_END_OF_STACK
;
967 fde
= _Unwind_Find_FDE (context
->ra
- 1, &context
->bases
);
970 #ifdef MD_FALLBACK_FRAME_STATE_FOR
971 /* Couldn't find frame unwind info for this function. Try a
972 target-specific fallback mechanism. This will necessarily
973 not provide a personality routine or LSDA. */
974 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
976 return _URC_END_OF_STACK
;
980 fs
->pc
= context
->bases
.func
;
983 insn
= extract_cie_info (cie
, context
, fs
);
985 /* CIE contained unknown augmentation. */
986 return _URC_FATAL_PHASE1_ERROR
;
988 /* First decode all the insns in the CIE. */
989 end
= (unsigned char *) next_fde ((struct dwarf_fde
*) cie
);
990 execute_cfa_program (insn
, end
, context
, fs
);
992 /* Locate augmentation for the fde. */
993 aug
= (unsigned char *) fde
+ sizeof (*fde
);
994 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
999 aug
= read_uleb128 (aug
, &i
);
1002 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1003 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
,
1004 (_Unwind_Ptr
*) &context
->lsda
);
1006 /* Then the insns in the FDE up to our target PC. */
1009 end
= (unsigned char *) next_fde (fde
);
1010 execute_cfa_program (insn
, end
, context
, fs
);
1012 return _URC_NO_REASON
;
1015 typedef struct frame_state
1021 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1022 unsigned short cfa_reg
;
1023 unsigned short retaddr_column
;
1024 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1027 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1029 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1030 a given PC_TARGET. The caller should allocate a local variable of
1031 `struct frame_state' and pass its address to STATE_IN. */
1033 struct frame_state
*
1034 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1036 struct _Unwind_Context context
;
1037 _Unwind_FrameState fs
;
1040 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1041 context
.ra
= pc_target
+ 1;
1043 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1046 /* We have no way to pass a location expression for the CFA to our
1047 caller. It wouldn't understand it anyway. */
1048 if (fs
.cfa_how
== CFA_EXP
)
1051 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1053 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1054 switch (state_in
->saved
[reg
])
1057 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1059 case REG_SAVED_OFFSET
:
1060 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1063 state_in
->reg_or_offset
[reg
] = 0;
1068 state_in
->cfa_offset
= fs
.cfa_offset
;
1069 state_in
->cfa_reg
= fs
.cfa_reg
;
1070 state_in
->retaddr_column
= fs
.retaddr_column
;
1071 state_in
->args_size
= context
.args_size
;
1072 state_in
->eh_ptr
= fs
.eh_ptr
;
1077 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1080 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1081 _Unwind_SpTmp
*tmp_sp
)
1083 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1085 if (size
== sizeof(_Unwind_Ptr
))
1086 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1087 else if (size
== sizeof(_Unwind_Word
))
1088 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1091 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1095 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1097 struct _Unwind_Context orig_context
= *context
;
1101 #ifdef EH_RETURN_STACKADJ_RTX
1102 /* Special handling here: Many machines do not use a frame pointer,
1103 and track the CFA only through offsets from the stack pointer from
1104 one frame to the next. In this case, the stack pointer is never
1105 stored, so it has no saved address in the context. What we do
1106 have is the CFA from the previous stack frame.
1108 In very special situations (such as unwind info for signal return),
1109 there may be location expressions that use the stack pointer as well.
1111 Do this conditionally for one frame. This allows the unwind info
1112 for one frame to save a copy of the stack pointer from the previous
1113 frame, and be able to use much easier CFA mechanisms to do it.
1114 Always zap the saved stack pointer value for the next frame; carrying
1115 the value over from one frame to another doesn't make sense. */
1117 _Unwind_SpTmp tmp_sp
;
1119 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1120 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1121 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1124 /* Compute this frame's CFA. */
1125 switch (fs
->cfa_how
)
1127 case CFA_REG_OFFSET
:
1128 cfa
= _Unwind_GetPtr (&orig_context
, fs
->cfa_reg
);
1129 cfa
+= fs
->cfa_offset
;
1134 const unsigned char *exp
= fs
->cfa_exp
;
1137 exp
= read_uleb128 (exp
, &len
);
1138 cfa
= (void *) (_Unwind_Ptr
)
1139 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1148 /* Compute the addresses of all registers saved in this frame. */
1149 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1150 switch (fs
->regs
.reg
[i
].how
)
1155 case REG_SAVED_OFFSET
:
1156 _Unwind_SetGRPtr (context
, i
,
1157 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1163 _Unwind_GetGRPtr (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
));
1168 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1172 exp
= read_uleb128 (exp
, &len
);
1173 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1175 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1180 #ifdef MD_FROB_UPDATE_CONTEXT
1181 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1185 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1186 of its caller. Update CONTEXT to refer to the caller as well. Note
1187 that the args_size and lsda members are not updated here, but later in
1188 uw_frame_state_for. */
1191 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1193 uw_update_context_1 (context
, fs
);
1195 /* Compute the return address now, since the return address column
1196 can change from frame to frame. */
1197 context
->ra
= __builtin_extract_return_addr
1198 (_Unwind_GetPtr (context
, fs
->retaddr_column
));
1201 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1202 level will be the return address and the CFA. */
1204 #define uw_init_context(CONTEXT) \
1207 /* Do any necessary initialization to access arbitrary stack frames. \
1208 On the SPARC, this means flushing the register windows. */ \
1209 __builtin_unwind_init (); \
1210 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1211 __builtin_return_address (0)); \
1216 init_dwarf_reg_size_table (void)
1218 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1222 uw_init_context_1 (struct _Unwind_Context
*context
,
1223 void *outer_cfa
, void *outer_ra
)
1225 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1226 _Unwind_FrameState fs
;
1227 _Unwind_SpTmp sp_slot
;
1229 memset (context
, 0, sizeof (struct _Unwind_Context
));
1232 if (uw_frame_state_for (context
, &fs
) != _URC_NO_REASON
)
1237 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1238 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1239 || dwarf_reg_size_table
[0] == 0)
1240 init_dwarf_reg_size_table ();
1243 if (dwarf_reg_size_table
[0] == 0)
1244 init_dwarf_reg_size_table ();
1247 /* Force the frame state to use the known cfa value. */
1248 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1249 fs
.cfa_how
= CFA_REG_OFFSET
;
1250 fs
.cfa_reg
= __builtin_dwarf_sp_column ();
1253 uw_update_context_1 (context
, &fs
);
1255 /* If the return address column was saved in a register in the
1256 initialization context, then we can't see it in the given
1257 call frame data. So have the initialization context tell us. */
1258 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1262 /* Install TARGET into CURRENT so that we can return to it. This is a
1263 macro because __builtin_eh_return must be invoked in the context of
1266 #define uw_install_context(CURRENT, TARGET) \
1269 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1270 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1271 __builtin_eh_return (offset, handler); \
1276 uw_install_context_1 (struct _Unwind_Context
*current
,
1277 struct _Unwind_Context
*target
)
1280 _Unwind_SpTmp sp_slot
;
1282 /* If the target frame does not have a saved stack pointer,
1283 then set up the target's CFA. */
1284 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1285 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1287 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1289 void *c
= current
->reg
[i
];
1290 void *t
= target
->reg
[i
];
1292 if (t
&& c
&& t
!= c
)
1293 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1296 /* If the current frame doesn't have a saved stack pointer, then we
1297 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1298 pointer value reloaded. */
1299 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1303 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1305 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1306 if (STACK_GROWS_DOWNWARD
)
1307 return target_cfa
- current
->cfa
+ target
->args_size
;
1309 return current
->cfa
- target_cfa
- target
->args_size
;
1314 static inline _Unwind_Ptr
1315 uw_identify_context (struct _Unwind_Context
*context
)
1317 return _Unwind_GetIP (context
);
1321 #include "unwind.inc"
1323 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1324 alias (_Unwind_Backtrace
);
1325 alias (_Unwind_DeleteException
);
1326 alias (_Unwind_FindEnclosingFunction
);
1327 alias (_Unwind_FindTableEntry
);
1328 alias (_Unwind_ForcedUnwind
);
1329 alias (_Unwind_GetDataRelBase
);
1330 alias (_Unwind_GetTextRelBase
);
1331 alias (_Unwind_GetCFA
);
1332 alias (_Unwind_GetGR
);
1333 alias (_Unwind_GetIP
);
1334 alias (_Unwind_GetLanguageSpecificData
);
1335 alias (_Unwind_GetRegionStart
);
1336 alias (_Unwind_RaiseException
);
1337 alias (_Unwind_Resume
);
1338 alias (_Unwind_Resume_or_Rethrow
);
1339 alias (_Unwind_SetGR
);
1340 alias (_Unwind_SetIP
);
1343 #endif /* !USING_SJLJ_EXCEPTIONS */