1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997,1998,1999,2000,2001,2002,2003,2005
3 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include <unwind-pe.h>
30 #include <unwind-dw2-fde.h>
36 #include "unwind-pe.h"
37 #include "unwind-dw2-fde.h"
43 #ifndef STACK_GROWS_DOWNWARD
44 #define STACK_GROWS_DOWNWARD 0
46 #undef STACK_GROWS_DOWNWARD
47 #define STACK_GROWS_DOWNWARD 1
50 /* A target can override (perhaps for backward compatibility) how
51 many dwarf2 columns are unwound. */
52 #ifndef DWARF_FRAME_REGISTERS
53 #define DWARF_FRAME_REGISTERS FIRST_PSEUDO_REGISTER
56 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
57 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
58 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
61 /* This is the register and unwind state for a particular frame. This
62 provides the information necessary to unwind up past a frame and return
64 struct _Unwind_Context
66 void *reg
[DWARF_FRAME_REGISTERS
+1];
70 struct dwarf_eh_bases bases
;
71 _Unwind_Word args_size
;
75 /* Byte size of every register managed by these routines. */
76 static unsigned char dwarf_reg_size_table
[DWARF_FRAME_REGISTERS
];
80 /* The result of interpreting the frame unwind info for a frame.
81 This is all symbolic at this point, as none of the values can
82 be resolved until the target pc is located. */
85 /* Each register save state can be described in terms of a CFA slot,
86 another register, or a location expression. */
87 struct frame_state_reg_info
93 const unsigned char *exp
;
101 } reg
[DWARF_FRAME_REGISTERS
+1];
103 /* Used to implement DW_CFA_remember_state. */
104 struct frame_state_reg_info
*prev
;
107 /* The CFA can be described in terms of a reg+offset or a
108 location expression. */
109 _Unwind_Sword cfa_offset
;
110 _Unwind_Word cfa_reg
;
111 const unsigned char *cfa_exp
;
118 /* The PC described by the current frame state. */
121 /* The information we care about from the CIE/FDE. */
122 _Unwind_Personality_Fn personality
;
123 _Unwind_Sword data_align
;
124 _Unwind_Word code_align
;
125 unsigned char retaddr_column
;
126 unsigned char fde_encoding
;
127 unsigned char lsda_encoding
;
130 } _Unwind_FrameState
;
132 /* Read unaligned data from the instruction buffer. */
137 unsigned u2
__attribute__ ((mode (HI
)));
138 unsigned u4
__attribute__ ((mode (SI
)));
139 unsigned u8
__attribute__ ((mode (DI
)));
140 signed s2
__attribute__ ((mode (HI
)));
141 signed s4
__attribute__ ((mode (SI
)));
142 signed s8
__attribute__ ((mode (DI
)));
143 } __attribute__ ((packed
));
146 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
149 read_1u (const void *p
) { return *(const unsigned char *) p
; }
152 read_1s (const void *p
) { return *(const signed char *) p
; }
155 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
158 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
160 static inline unsigned int
161 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
164 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
166 static inline unsigned long
167 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
169 static inline unsigned long
170 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
172 /* Get the value of register REG as saved in CONTEXT. */
175 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
177 /* This will segfault if the register hasn't been saved. */
178 return * (_Unwind_Word
*) context
->reg
[index
];
181 /* Get the value of the CFA as saved in CONTEXT. */
184 _Unwind_GetCFA (struct _Unwind_Context
*context
)
186 return (_Unwind_Ptr
) context
->cfa
;
189 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
192 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
194 * (_Unwind_Word
*) context
->reg
[index
] = val
;
197 /* Retrieve the return address for CONTEXT. */
200 _Unwind_GetIP (struct _Unwind_Context
*context
)
202 return (_Unwind_Ptr
) context
->ra
;
205 /* Overwrite the return address for CONTEXT with VAL. */
208 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
210 context
->ra
= (void *) val
;
214 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
216 return context
->lsda
;
220 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
222 return (_Unwind_Ptr
) context
->bases
.func
;
226 _Unwind_FindEnclosingFunction (void *pc
)
228 struct dwarf_eh_bases bases
;
229 struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
238 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
240 return (_Unwind_Ptr
) context
->bases
.dbase
;
244 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
246 return (_Unwind_Ptr
) context
->bases
.tbase
;
250 /* Extract any interesting information from the CIE for the translation
251 unit F belongs to. Return a pointer to the byte after the augmentation,
252 or NULL if we encountered an undecipherable augmentation. */
254 static const unsigned char *
255 extract_cie_info (struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
256 _Unwind_FrameState
*fs
)
258 const unsigned char *aug
= cie
->augmentation
;
259 const unsigned char *p
= aug
+ strlen ((const char *) aug
) + 1;
260 const unsigned char *ret
= NULL
;
263 /* g++ v2 "eh" has pointer immediately following augmentation string,
264 so it must be handled first. */
265 if (aug
[0] == 'e' && aug
[1] == 'h')
267 fs
->eh_ptr
= read_pointer (p
);
268 p
+= sizeof (void *);
272 /* Immediately following the augmentation are the code and
273 data alignment and return address column. */
274 p
= read_uleb128 (p
, &fs
->code_align
);
275 p
= read_sleb128 (p
, &fs
->data_align
);
276 fs
->retaddr_column
= *p
++;
277 fs
->lsda_encoding
= DW_EH_PE_omit
;
279 /* If the augmentation starts with 'z', then a uleb128 immediately
280 follows containing the length of the augmentation field following
284 p
= read_uleb128 (p
, &utmp
);
291 /* Iterate over recognized augmentation subsequences. */
294 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
297 fs
->lsda_encoding
= *p
++;
301 /* "R" indicates a byte indicating how FDE addresses are encoded. */
302 else if (aug
[0] == 'R')
304 fs
->fde_encoding
= *p
++;
308 /* "P" indicates a personality routine in the CIE augmentation. */
309 else if (aug
[0] == 'P')
311 p
= read_encoded_value (context
, *p
, p
+ 1,
312 (_Unwind_Ptr
*) &fs
->personality
);
316 /* Otherwise we have an unknown augmentation string.
317 Bail unless we saw a 'z' prefix. */
322 return ret
? ret
: p
;
326 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
327 onto the stack to start. */
330 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
331 struct _Unwind_Context
*context
, _Unwind_Word initial
)
333 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
339 while (op_ptr
< op_end
)
341 enum dwarf_location_atom op
= *op_ptr
++;
342 _Unwind_Word result
, reg
, utmp
;
343 _Unwind_Sword offset
, stmp
;
379 result
= op
- DW_OP_lit0
;
383 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
384 op_ptr
+= sizeof (void *);
388 result
= read_1u (op_ptr
);
392 result
= read_1s (op_ptr
);
396 result
= read_2u (op_ptr
);
400 result
= read_2s (op_ptr
);
404 result
= read_4u (op_ptr
);
408 result
= read_4s (op_ptr
);
412 result
= read_8u (op_ptr
);
416 result
= read_8s (op_ptr
);
420 op_ptr
= read_uleb128 (op_ptr
, &result
);
423 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
459 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
462 op_ptr
= read_uleb128 (op_ptr
, ®
);
463 result
= _Unwind_GetGR (context
, reg
);
498 op_ptr
= read_sleb128 (op_ptr
, &offset
);
499 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
502 op_ptr
= read_uleb128 (op_ptr
, ®
);
503 op_ptr
= read_sleb128 (op_ptr
, &offset
);
504 result
= _Unwind_GetGR (context
, reg
) + offset
;
510 result
= stack
[stack_elt
- 1];
520 if (offset
>= stack_elt
- 1)
522 result
= stack
[stack_elt
- 1 - offset
];
528 result
= stack
[stack_elt
- 2];
533 _Unwind_Word t1
, t2
, t3
;
537 t1
= stack
[stack_elt
- 1];
538 t2
= stack
[stack_elt
- 2];
539 t3
= stack
[stack_elt
- 3];
540 stack
[stack_elt
- 1] = t2
;
541 stack
[stack_elt
- 2] = t3
;
542 stack
[stack_elt
- 3] = t1
;
547 case DW_OP_deref_size
:
551 case DW_OP_plus_uconst
:
552 /* Unary operations. */
555 result
= stack
[stack_elt
];
561 void *ptr
= (void *) (_Unwind_Ptr
) result
;
562 result
= (_Unwind_Ptr
) read_pointer (ptr
);
566 case DW_OP_deref_size
:
568 void *ptr
= (void *) (_Unwind_Ptr
) result
;
572 result
= read_1u (ptr
);
575 result
= read_2u (ptr
);
578 result
= read_4u (ptr
);
581 result
= read_8u (ptr
);
590 if ((_Unwind_Sword
) result
< 0)
599 case DW_OP_plus_uconst
:
600 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
623 /* Binary operations. */
624 _Unwind_Word first
, second
;
625 if ((stack_elt
-= 2) < 0)
627 second
= stack
[stack_elt
];
628 first
= stack
[stack_elt
+ 1];
633 result
= second
& first
;
636 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
639 result
= second
- first
;
642 result
= (_Unwind_Sword
) second
% (_Unwind_Sword
) first
;
645 result
= second
* first
;
648 result
= second
| first
;
651 result
= second
+ first
;
654 result
= second
<< first
;
657 result
= second
>> first
;
660 result
= (_Unwind_Sword
) second
>> first
;
663 result
= second
^ first
;
666 result
= (_Unwind_Sword
) first
<= (_Unwind_Sword
) second
;
669 result
= (_Unwind_Sword
) first
>= (_Unwind_Sword
) second
;
672 result
= (_Unwind_Sword
) first
== (_Unwind_Sword
) second
;
675 result
= (_Unwind_Sword
) first
< (_Unwind_Sword
) second
;
678 result
= (_Unwind_Sword
) first
> (_Unwind_Sword
) second
;
681 result
= (_Unwind_Sword
) first
!= (_Unwind_Sword
) second
;
691 offset
= read_2s (op_ptr
);
699 offset
= read_2s (op_ptr
);
701 if (stack
[stack_elt
] != 0)
712 /* Most things push a result value. */
713 if ((size_t) stack_elt
>= sizeof(stack
)/sizeof(*stack
))
715 stack
[stack_elt
++] = result
;
719 /* We were executing this program to get a value. It should be
723 return stack
[stack_elt
];
727 /* Decode DWARF 2 call frame information. Takes pointers the
728 instruction sequence to decode, current register information and
729 CIE info, and the PC range to evaluate. */
732 execute_cfa_program (const unsigned char *insn_ptr
,
733 const unsigned char *insn_end
,
734 struct _Unwind_Context
*context
,
735 _Unwind_FrameState
*fs
)
737 struct frame_state_reg_info
*unused_rs
= NULL
;
739 /* Don't allow remember/restore between CIE and FDE programs. */
740 fs
->regs
.prev
= NULL
;
742 /* The comparison with the return address uses < rather than <= because
743 we are only interested in the effects of code before the call; for a
744 noreturn function, the return address may point to unrelated code with
745 a different stack configuration that we are not interested in. We
746 assume that the call itself is unwind info-neutral; if not, or if
747 there are delay instructions that adjust the stack, these must be
748 reflected at the point immediately before the call insn. */
749 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
)
751 unsigned char insn
= *insn_ptr
++;
752 _Unwind_Word reg
, utmp
;
753 _Unwind_Sword offset
, stmp
;
755 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
756 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
757 else if ((insn
& 0xc0) == DW_CFA_offset
)
760 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
761 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
762 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
763 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
765 else if ((insn
& 0xc0) == DW_CFA_restore
)
768 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
773 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
774 insn_ptr
, (_Unwind_Ptr
*) &fs
->pc
);
777 case DW_CFA_advance_loc1
:
778 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
781 case DW_CFA_advance_loc2
:
782 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
785 case DW_CFA_advance_loc4
:
786 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
790 case DW_CFA_offset_extended
:
791 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
792 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
793 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
794 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
795 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
798 case DW_CFA_restore_extended
:
799 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
800 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
803 case DW_CFA_undefined
:
804 case DW_CFA_same_value
:
805 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
811 case DW_CFA_register
:
814 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
815 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
816 fs
->regs
.reg
[reg
].how
= REG_SAVED_REG
;
817 fs
->regs
.reg
[reg
].loc
.reg
= reg2
;
821 case DW_CFA_remember_state
:
823 struct frame_state_reg_info
*new_rs
;
827 unused_rs
= unused_rs
->prev
;
830 new_rs
= __builtin_alloca (sizeof (struct frame_state_reg_info
));
833 fs
->regs
.prev
= new_rs
;
837 case DW_CFA_restore_state
:
839 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
841 old_rs
->prev
= unused_rs
;
847 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
848 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
849 fs
->cfa_offset
= utmp
;
850 fs
->cfa_how
= CFA_REG_OFFSET
;
853 case DW_CFA_def_cfa_register
:
854 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
855 fs
->cfa_how
= CFA_REG_OFFSET
;
858 case DW_CFA_def_cfa_offset
:
859 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
860 fs
->cfa_offset
= utmp
;
861 /* cfa_how deliberately not set. */
864 case DW_CFA_def_cfa_expression
:
865 fs
->cfa_exp
= insn_ptr
;
866 fs
->cfa_how
= CFA_EXP
;
867 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
871 case DW_CFA_expression
:
872 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
873 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
874 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
875 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
879 /* From the 2.1 draft. */
880 case DW_CFA_offset_extended_sf
:
881 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
882 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
883 offset
= stmp
* fs
->data_align
;
884 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
885 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
888 case DW_CFA_def_cfa_sf
:
889 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
890 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
891 fs
->cfa_how
= CFA_REG_OFFSET
;
894 case DW_CFA_def_cfa_offset_sf
:
895 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
896 /* cfa_how deliberately not set. */
899 case DW_CFA_GNU_window_save
:
900 /* ??? Hardcoded for SPARC register window configuration. */
901 for (reg
= 16; reg
< 32; ++reg
)
903 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
904 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
908 case DW_CFA_GNU_args_size
:
909 insn_ptr
= read_uleb128 (insn_ptr
, &context
->args_size
);
912 case DW_CFA_GNU_negative_offset_extended
:
913 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
914 older PowerPC code. */
915 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
916 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
917 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
918 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
919 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
928 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
929 its caller and decode it into FS. This function also sets the
930 args_size and lsda members of CONTEXT, as they are really information
931 about the caller's frame. */
933 static _Unwind_Reason_Code
934 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
936 struct dwarf_fde
*fde
;
937 struct dwarf_cie
*cie
;
938 const unsigned char *aug
, *insn
, *end
;
940 memset (fs
, 0, sizeof (*fs
));
941 context
->args_size
= 0;
944 fde
= _Unwind_Find_FDE (context
->ra
- 1, &context
->bases
);
947 /* Couldn't find frame unwind info for this function. Try a
948 target-specific fallback mechanism. This will necessarily
949 not provide a personality routine or LSDA. */
950 #ifdef MD_FALLBACK_FRAME_STATE_FOR
951 MD_FALLBACK_FRAME_STATE_FOR (context
, fs
, success
);
952 return _URC_END_OF_STACK
;
954 return _URC_NO_REASON
;
956 return _URC_END_OF_STACK
;
960 fs
->pc
= context
->bases
.func
;
963 insn
= extract_cie_info (cie
, context
, fs
);
965 /* CIE contained unknown augmentation. */
966 return _URC_FATAL_PHASE1_ERROR
;
968 /* First decode all the insns in the CIE. */
969 end
= (unsigned char *) next_fde ((struct dwarf_fde
*) cie
);
970 execute_cfa_program (insn
, end
, context
, fs
);
972 /* Locate augmentation for the fde. */
973 aug
= (unsigned char *) fde
+ sizeof (*fde
);
974 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
979 aug
= read_uleb128 (aug
, &i
);
982 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
983 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
,
984 (_Unwind_Ptr
*) &context
->lsda
);
986 /* Then the insns in the FDE up to our target PC. */
989 end
= (unsigned char *) next_fde (fde
);
990 execute_cfa_program (insn
, end
, context
, fs
);
992 return _URC_NO_REASON
;
995 typedef struct frame_state
1001 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1002 unsigned short cfa_reg
;
1003 unsigned short retaddr_column
;
1004 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1012 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1014 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1015 a given PC_TARGET. The caller should allocate a local variable of
1016 `struct frame_state' and pass its address to STATE_IN. */
1019 struct frame_state
*
1020 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1022 struct _Unwind_Context context
;
1023 _Unwind_FrameState fs
;
1026 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1027 context
.ra
= pc_target
+ 1;
1029 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1032 /* We have no way to pass a location expression for the CFA to our
1033 caller. It wouldn't understand it anyway. */
1034 if (fs
.cfa_how
== CFA_EXP
)
1037 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1039 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1040 switch (state_in
->saved
[reg
])
1043 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1045 case REG_SAVED_OFFSET
:
1046 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1049 state_in
->reg_or_offset
[reg
] = 0;
1054 state_in
->cfa_offset
= fs
.cfa_offset
;
1055 state_in
->cfa_reg
= fs
.cfa_reg
;
1056 state_in
->retaddr_column
= fs
.retaddr_column
;
1057 state_in
->args_size
= context
.args_size
;
1058 state_in
->eh_ptr
= fs
.eh_ptr
;
1066 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1068 struct _Unwind_Context orig_context
= *context
;
1072 #ifdef EH_RETURN_STACKADJ_RTX
1073 /* Special handling here: Many machines do not use a frame pointer,
1074 and track the CFA only through offsets from the stack pointer from
1075 one frame to the next. In this case, the stack pointer is never
1076 stored, so it has no saved address in the context. What we do
1077 have is the CFA from the previous stack frame.
1079 In very special situations (such as unwind info for signal return),
1080 there may be location expressions that use the stack pointer as well.
1082 Do this conditionally for one frame. This allows the unwind info
1083 for one frame to save a copy of the stack pointer from the previous
1084 frame, and be able to use much easier CFA mechanisms to do it.
1085 Always zap the saved stack pointer value for the next frame; carrying
1086 the value over from one frame to another doesn't make sense. */
1088 _Unwind_Word tmp_sp
;
1090 if (!orig_context
.reg
[__builtin_dwarf_sp_column ()])
1092 tmp_sp
= (_Unwind_Ptr
) context
->cfa
;
1093 orig_context
.reg
[__builtin_dwarf_sp_column ()] = &tmp_sp
;
1095 context
->reg
[__builtin_dwarf_sp_column ()] = NULL
;
1098 /* Compute this frame's CFA. */
1099 switch (fs
->cfa_how
)
1101 case CFA_REG_OFFSET
:
1102 cfa
= (void *) (_Unwind_Ptr
) _Unwind_GetGR (&orig_context
, fs
->cfa_reg
);
1103 cfa
+= fs
->cfa_offset
;
1108 const unsigned char *exp
= fs
->cfa_exp
;
1111 exp
= read_uleb128 (exp
, &len
);
1112 cfa
= (void *) (_Unwind_Ptr
)
1113 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1122 /* Compute the addresses of all registers saved in this frame. */
1123 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1124 switch (fs
->regs
.reg
[i
].how
)
1129 case REG_SAVED_OFFSET
:
1130 context
->reg
[i
] = cfa
+ fs
->regs
.reg
[i
].loc
.offset
;
1134 context
->reg
[i
] = orig_context
.reg
[fs
->regs
.reg
[i
].loc
.reg
];
1139 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1143 exp
= read_uleb128 (exp
, &len
);
1144 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1146 context
->reg
[i
] = (void *) val
;
1152 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1153 of its caller. Update CONTEXT to refer to the caller as well. Note
1154 that the args_size and lsda members are not updated here, but later in
1155 uw_frame_state_for. */
1158 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1160 uw_update_context_1 (context
, fs
);
1162 /* Compute the return address now, since the return address column
1163 can change from frame to frame. */
1164 context
->ra
= __builtin_extract_return_addr
1165 ((void *) (_Unwind_Ptr
) _Unwind_GetGR (context
, fs
->retaddr_column
));
1168 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1169 level will be the return address and the CFA. */
1171 #define uw_init_context(CONTEXT) \
1174 /* Do any necessary initialization to access arbitrary stack frames. \
1175 On the SPARC, this means flushing the register windows. */ \
1176 __builtin_unwind_init (); \
1177 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1178 __builtin_return_address (0)); \
1183 uw_init_context_1 (struct _Unwind_Context
*context
,
1184 void *outer_cfa
, void *outer_ra
)
1186 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1187 _Unwind_FrameState fs
;
1188 _Unwind_Word sp_slot
;
1190 memset (context
, 0, sizeof (struct _Unwind_Context
));
1193 if (uw_frame_state_for (context
, &fs
) != _URC_NO_REASON
)
1196 /* Force the frame state to use the known cfa value. */
1197 sp_slot
= (_Unwind_Ptr
) outer_cfa
;
1198 context
->reg
[__builtin_dwarf_sp_column ()] = &sp_slot
;
1199 fs
.cfa_how
= CFA_REG_OFFSET
;
1200 fs
.cfa_reg
= __builtin_dwarf_sp_column ();
1203 uw_update_context_1 (context
, &fs
);
1205 /* If the return address column was saved in a register in the
1206 initialization context, then we can't see it in the given
1207 call frame data. So have the initialization context tell us. */
1208 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1212 /* Install TARGET into CURRENT so that we can return to it. This is a
1213 macro because __builtin_eh_return must be invoked in the context of
1216 #define uw_install_context(CURRENT, TARGET) \
1219 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1220 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1221 __builtin_eh_return (offset, handler); \
1226 init_dwarf_reg_size_table (void)
1228 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1232 uw_install_context_1 (struct _Unwind_Context
*current
,
1233 struct _Unwind_Context
*target
)
1239 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1240 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1241 || dwarf_reg_size_table
[0] == 0)
1242 init_dwarf_reg_size_table ();
1245 if (dwarf_reg_size_table
[0] == 0)
1246 init_dwarf_reg_size_table ();
1249 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1251 void *c
= current
->reg
[i
];
1252 void *t
= target
->reg
[i
];
1253 if (t
&& c
&& t
!= c
)
1254 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1257 #ifdef EH_RETURN_STACKADJ_RTX
1261 /* If the last frame records a saved stack pointer, use it. */
1262 if (target
->reg
[__builtin_dwarf_sp_column ()])
1263 target_cfa
= (void *)(_Unwind_Ptr
)
1264 _Unwind_GetGR (target
, __builtin_dwarf_sp_column ());
1266 target_cfa
= target
->cfa
;
1268 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1269 if (STACK_GROWS_DOWNWARD
)
1270 return target_cfa
- current
->cfa
+ target
->args_size
;
1272 return current
->cfa
- target_cfa
- target
->args_size
;
1279 static inline _Unwind_Ptr
1280 uw_identify_context (struct _Unwind_Context
*context
)
1282 return _Unwind_GetIP (context
);
1286 #include "unwind.inc"