1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
28 #include <unwind-pe.h>
29 #include <unwind-dw2-fde.h>
35 #include "unwind-pe.h"
36 #include "unwind-dw2-fde.h"
42 #ifndef STACK_GROWS_DOWNWARD
43 #define STACK_GROWS_DOWNWARD 0
45 #undef STACK_GROWS_DOWNWARD
46 #define STACK_GROWS_DOWNWARD 1
49 /* A target can override (perhaps for backward compatibility) how
50 many dwarf2 columns are unwound. */
51 #ifndef DWARF_FRAME_REGISTERS
52 #define DWARF_FRAME_REGISTERS FIRST_PSEUDO_REGISTER
55 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
56 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
57 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
60 /* This is the register and unwind state for a particular frame. This
61 provides the information necessary to unwind up past a frame and return
63 struct _Unwind_Context
65 void *reg
[DWARF_FRAME_REGISTERS
+1];
69 struct dwarf_eh_bases bases
;
70 _Unwind_Word args_size
;
74 /* Byte size of every register managed by these routines. */
75 static unsigned char dwarf_reg_size_table
[DWARF_FRAME_REGISTERS
];
79 /* The result of interpreting the frame unwind info for a frame.
80 This is all symbolic at this point, as none of the values can
81 be resolved until the target pc is located. */
84 /* Each register save state can be described in terms of a CFA slot,
85 another register, or a location expression. */
86 struct frame_state_reg_info
92 const unsigned char *exp
;
100 } reg
[DWARF_FRAME_REGISTERS
+1];
102 /* Used to implement DW_CFA_remember_state. */
103 struct frame_state_reg_info
*prev
;
106 /* The CFA can be described in terms of a reg+offset or a
107 location expression. */
108 _Unwind_Sword cfa_offset
;
109 _Unwind_Word cfa_reg
;
110 const unsigned char *cfa_exp
;
117 /* The PC described by the current frame state. */
120 /* The information we care about from the CIE/FDE. */
121 _Unwind_Personality_Fn personality
;
122 _Unwind_Sword data_align
;
123 _Unwind_Word code_align
;
124 unsigned char retaddr_column
;
125 unsigned char fde_encoding
;
126 unsigned char lsda_encoding
;
129 } _Unwind_FrameState
;
131 /* Read unaligned data from the instruction buffer. */
136 unsigned u2
__attribute__ ((mode (HI
)));
137 unsigned u4
__attribute__ ((mode (SI
)));
138 unsigned u8
__attribute__ ((mode (DI
)));
139 signed s2
__attribute__ ((mode (HI
)));
140 signed s4
__attribute__ ((mode (SI
)));
141 signed s8
__attribute__ ((mode (DI
)));
142 } __attribute__ ((packed
));
145 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
148 read_1u (const void *p
) { return *(const unsigned char *) p
; }
151 read_1s (const void *p
) { return *(const signed char *) p
; }
154 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
157 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
159 static inline unsigned int
160 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
163 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
165 static inline unsigned long
166 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
168 static inline unsigned long
169 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
171 /* Get the value of register REG as saved in CONTEXT. */
174 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
176 /* This will segfault if the register hasn't been saved. */
177 return * (_Unwind_Word
*) context
->reg
[index
];
180 /* Get the value of the CFA as saved in CONTEXT. */
183 _Unwind_GetCFA (struct _Unwind_Context
*context
)
185 return (_Unwind_Ptr
) context
->cfa
;
188 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
191 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
193 * (_Unwind_Word
*) context
->reg
[index
] = val
;
196 /* Retrieve the return address for CONTEXT. */
199 _Unwind_GetIP (struct _Unwind_Context
*context
)
201 return (_Unwind_Ptr
) context
->ra
;
204 /* Overwrite the return address for CONTEXT with VAL. */
207 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
209 context
->ra
= (void *) val
;
213 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
215 return context
->lsda
;
219 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
221 return (_Unwind_Ptr
) context
->bases
.func
;
225 _Unwind_FindEnclosingFunction (void *pc
)
227 struct dwarf_eh_bases bases
;
228 struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
237 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
239 return (_Unwind_Ptr
) context
->bases
.dbase
;
243 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
245 return (_Unwind_Ptr
) context
->bases
.tbase
;
249 /* Extract any interesting information from the CIE for the translation
250 unit F belongs to. Return a pointer to the byte after the augmentation,
251 or NULL if we encountered an undecipherable augmentation. */
253 static const unsigned char *
254 extract_cie_info (struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
255 _Unwind_FrameState
*fs
)
257 const unsigned char *aug
= cie
->augmentation
;
258 const unsigned char *p
= aug
+ strlen ((const char *) aug
) + 1;
259 const unsigned char *ret
= NULL
;
262 /* g++ v2 "eh" has pointer immediately following augmentation string,
263 so it must be handled first. */
264 if (aug
[0] == 'e' && aug
[1] == 'h')
266 fs
->eh_ptr
= read_pointer (p
);
267 p
+= sizeof (void *);
271 /* Immediately following the augmentation are the code and
272 data alignment and return address column. */
273 p
= read_uleb128 (p
, &fs
->code_align
);
274 p
= read_sleb128 (p
, &fs
->data_align
);
275 fs
->retaddr_column
= *p
++;
276 fs
->lsda_encoding
= DW_EH_PE_omit
;
278 /* If the augmentation starts with 'z', then a uleb128 immediately
279 follows containing the length of the augmentation field following
283 p
= read_uleb128 (p
, &utmp
);
290 /* Iterate over recognized augmentation subsequences. */
293 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
296 fs
->lsda_encoding
= *p
++;
300 /* "R" indicates a byte indicating how FDE addresses are encoded. */
301 else if (aug
[0] == 'R')
303 fs
->fde_encoding
= *p
++;
307 /* "P" indicates a personality routine in the CIE augmentation. */
308 else if (aug
[0] == 'P')
310 _Unwind_Ptr personality
;
311 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
312 fs
->personality
= (_Unwind_Personality_Fn
) 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
;
775 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
777 fs
->pc
= (void *) pc
;
781 case DW_CFA_advance_loc1
:
782 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
785 case DW_CFA_advance_loc2
:
786 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
789 case DW_CFA_advance_loc4
:
790 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
794 case DW_CFA_offset_extended
:
795 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
796 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
797 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
798 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
799 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
802 case DW_CFA_restore_extended
:
803 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
804 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
807 case DW_CFA_undefined
:
808 case DW_CFA_same_value
:
809 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
815 case DW_CFA_register
:
818 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
819 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
820 fs
->regs
.reg
[reg
].how
= REG_SAVED_REG
;
821 fs
->regs
.reg
[reg
].loc
.reg
= reg2
;
825 case DW_CFA_remember_state
:
827 struct frame_state_reg_info
*new_rs
;
831 unused_rs
= unused_rs
->prev
;
834 new_rs
= __builtin_alloca (sizeof (struct frame_state_reg_info
));
837 fs
->regs
.prev
= new_rs
;
841 case DW_CFA_restore_state
:
843 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
846 __libc_fatal ("invalid DWARF unwind data");
851 old_rs
->prev
= unused_rs
;
858 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
859 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
860 fs
->cfa_offset
= utmp
;
861 fs
->cfa_how
= CFA_REG_OFFSET
;
864 case DW_CFA_def_cfa_register
:
865 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
866 fs
->cfa_how
= CFA_REG_OFFSET
;
869 case DW_CFA_def_cfa_offset
:
870 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
871 fs
->cfa_offset
= utmp
;
872 /* cfa_how deliberately not set. */
875 case DW_CFA_def_cfa_expression
:
876 fs
->cfa_exp
= insn_ptr
;
877 fs
->cfa_how
= CFA_EXP
;
878 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
882 case DW_CFA_expression
:
883 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
884 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
885 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
886 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
890 /* From the 2.1 draft. */
891 case DW_CFA_offset_extended_sf
:
892 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
893 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
894 offset
= stmp
* fs
->data_align
;
895 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
896 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
899 case DW_CFA_def_cfa_sf
:
900 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
901 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
902 fs
->cfa_how
= CFA_REG_OFFSET
;
905 case DW_CFA_def_cfa_offset_sf
:
906 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
907 /* cfa_how deliberately not set. */
910 case DW_CFA_GNU_window_save
:
911 /* ??? Hardcoded for SPARC register window configuration.
912 At least do not do anything for archs which explicitly
913 define a lower register number. */
914 #if DWARF_FRAME_REGISTERS >= 32
915 for (reg
= 16; reg
< 32; ++reg
)
917 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
918 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
923 case DW_CFA_GNU_args_size
:
924 insn_ptr
= read_uleb128 (insn_ptr
, &context
->args_size
);
927 case DW_CFA_GNU_negative_offset_extended
:
928 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
929 older PowerPC code. */
930 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
931 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
932 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
933 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
934 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
943 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
944 its caller and decode it into FS. This function also sets the
945 args_size and lsda members of CONTEXT, as they are really information
946 about the caller's frame. */
948 static _Unwind_Reason_Code
949 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
951 struct dwarf_fde
*fde
;
952 struct dwarf_cie
*cie
;
953 const unsigned char *aug
, *insn
, *end
;
955 memset (fs
, 0, sizeof (*fs
));
956 context
->args_size
= 0;
959 fde
= _Unwind_Find_FDE (context
->ra
- 1, &context
->bases
);
962 /* Couldn't find frame unwind info for this function. Try a
963 target-specific fallback mechanism. This will necessarily
964 not provide a personality routine or LSDA. */
965 #ifdef MD_FALLBACK_FRAME_STATE_FOR
966 MD_FALLBACK_FRAME_STATE_FOR (context
, fs
, success
);
967 return _URC_END_OF_STACK
;
969 return _URC_NO_REASON
;
971 return _URC_END_OF_STACK
;
975 fs
->pc
= context
->bases
.func
;
978 insn
= extract_cie_info (cie
, context
, fs
);
980 /* CIE contained unknown augmentation. */
981 return _URC_FATAL_PHASE1_ERROR
;
983 /* First decode all the insns in the CIE. */
984 end
= (unsigned char *) next_fde ((struct dwarf_fde
*) cie
);
985 execute_cfa_program (insn
, end
, context
, fs
);
987 /* Locate augmentation for the fde. */
988 aug
= (unsigned char *) fde
+ sizeof (*fde
);
989 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
994 aug
= read_uleb128 (aug
, &i
);
997 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1000 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1001 context
->lsda
= (void *) lsda
;
1004 /* Then the insns in the FDE up to our target PC. */
1007 end
= (unsigned char *) next_fde (fde
);
1008 execute_cfa_program (insn
, end
, context
, fs
);
1010 return _URC_NO_REASON
;
1013 typedef struct frame_state
1019 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1020 unsigned short cfa_reg
;
1021 unsigned short retaddr_column
;
1022 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1030 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1032 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1033 a given PC_TARGET. The caller should allocate a local variable of
1034 `struct frame_state' and pass its address to STATE_IN. */
1037 struct frame_state
*
1038 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1040 struct _Unwind_Context context
;
1041 _Unwind_FrameState fs
;
1044 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1045 context
.ra
= pc_target
+ 1;
1047 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1050 /* We have no way to pass a location expression for the CFA to our
1051 caller. It wouldn't understand it anyway. */
1052 if (fs
.cfa_how
== CFA_EXP
)
1055 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1057 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1058 switch (state_in
->saved
[reg
])
1061 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1063 case REG_SAVED_OFFSET
:
1064 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1067 state_in
->reg_or_offset
[reg
] = 0;
1072 state_in
->cfa_offset
= fs
.cfa_offset
;
1073 state_in
->cfa_reg
= fs
.cfa_reg
;
1074 state_in
->retaddr_column
= fs
.retaddr_column
;
1075 state_in
->args_size
= context
.args_size
;
1076 state_in
->eh_ptr
= fs
.eh_ptr
;
1084 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1086 struct _Unwind_Context orig_context
= *context
;
1090 #ifdef EH_RETURN_STACKADJ_RTX
1091 /* Special handling here: Many machines do not use a frame pointer,
1092 and track the CFA only through offsets from the stack pointer from
1093 one frame to the next. In this case, the stack pointer is never
1094 stored, so it has no saved address in the context. What we do
1095 have is the CFA from the previous stack frame.
1097 In very special situations (such as unwind info for signal return),
1098 there may be location expressions that use the stack pointer as well.
1100 Do this conditionally for one frame. This allows the unwind info
1101 for one frame to save a copy of the stack pointer from the previous
1102 frame, and be able to use much easier CFA mechanisms to do it.
1103 Always zap the saved stack pointer value for the next frame; carrying
1104 the value over from one frame to another doesn't make sense. */
1106 _Unwind_Word tmp_sp
;
1108 if (!orig_context
.reg
[__builtin_dwarf_sp_column ()])
1110 tmp_sp
= (_Unwind_Ptr
) context
->cfa
;
1111 orig_context
.reg
[__builtin_dwarf_sp_column ()] = &tmp_sp
;
1113 context
->reg
[__builtin_dwarf_sp_column ()] = NULL
;
1116 /* Compute this frame's CFA. */
1117 switch (fs
->cfa_how
)
1119 case CFA_REG_OFFSET
:
1120 cfa
= (void *) (_Unwind_Ptr
) _Unwind_GetGR (&orig_context
, fs
->cfa_reg
);
1121 cfa
+= fs
->cfa_offset
;
1126 const unsigned char *exp
= fs
->cfa_exp
;
1129 exp
= read_uleb128 (exp
, &len
);
1130 cfa
= (void *) (_Unwind_Ptr
)
1131 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1140 /* Compute the addresses of all registers saved in this frame. */
1141 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1142 switch (fs
->regs
.reg
[i
].how
)
1147 case REG_SAVED_OFFSET
:
1148 context
->reg
[i
] = cfa
+ fs
->regs
.reg
[i
].loc
.offset
;
1152 context
->reg
[i
] = orig_context
.reg
[fs
->regs
.reg
[i
].loc
.reg
];
1157 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1161 exp
= read_uleb128 (exp
, &len
);
1162 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1164 context
->reg
[i
] = (void *) val
;
1170 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1171 of its caller. Update CONTEXT to refer to the caller as well. Note
1172 that the args_size and lsda members are not updated here, but later in
1173 uw_frame_state_for. */
1176 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1178 uw_update_context_1 (context
, fs
);
1180 /* Compute the return address now, since the return address column
1181 can change from frame to frame. */
1182 context
->ra
= __builtin_extract_return_addr
1183 ((void *) (_Unwind_Ptr
) _Unwind_GetGR (context
, fs
->retaddr_column
));
1186 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1187 level will be the return address and the CFA. */
1189 #define uw_init_context(CONTEXT) \
1192 /* Do any necessary initialization to access arbitrary stack frames. \
1193 On the SPARC, this means flushing the register windows. */ \
1194 __builtin_unwind_init (); \
1195 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1196 __builtin_return_address (0)); \
1201 uw_init_context_1 (struct _Unwind_Context
*context
,
1202 void *outer_cfa
, void *outer_ra
)
1204 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1205 _Unwind_FrameState fs
;
1206 _Unwind_Word sp_slot
;
1208 memset (context
, 0, sizeof (struct _Unwind_Context
));
1211 if (uw_frame_state_for (context
, &fs
) != _URC_NO_REASON
)
1214 /* Force the frame state to use the known cfa value. */
1215 sp_slot
= (_Unwind_Ptr
) outer_cfa
;
1216 context
->reg
[__builtin_dwarf_sp_column ()] = &sp_slot
;
1217 fs
.cfa_how
= CFA_REG_OFFSET
;
1218 fs
.cfa_reg
= __builtin_dwarf_sp_column ();
1221 uw_update_context_1 (context
, &fs
);
1223 /* If the return address column was saved in a register in the
1224 initialization context, then we can't see it in the given
1225 call frame data. So have the initialization context tell us. */
1226 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1230 /* Install TARGET into CURRENT so that we can return to it. This is a
1231 macro because __builtin_eh_return must be invoked in the context of
1234 #define uw_install_context(CURRENT, TARGET) \
1237 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1238 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1239 __builtin_eh_return (offset, handler); \
1244 init_dwarf_reg_size_table (void)
1246 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1250 uw_install_context_1 (struct _Unwind_Context
*current
,
1251 struct _Unwind_Context
*target
)
1257 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1258 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1259 || dwarf_reg_size_table
[0] == 0)
1260 init_dwarf_reg_size_table ();
1263 if (dwarf_reg_size_table
[0] == 0)
1264 init_dwarf_reg_size_table ();
1267 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1269 void *c
= current
->reg
[i
];
1270 void *t
= target
->reg
[i
];
1271 if (t
&& c
&& t
!= c
)
1272 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1275 #ifdef EH_RETURN_STACKADJ_RTX
1279 /* If the last frame records a saved stack pointer, use it. */
1280 if (target
->reg
[__builtin_dwarf_sp_column ()])
1281 target_cfa
= (void *)(_Unwind_Ptr
)
1282 _Unwind_GetGR (target
, __builtin_dwarf_sp_column ());
1284 target_cfa
= target
->cfa
;
1286 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1287 if (STACK_GROWS_DOWNWARD
)
1288 return target_cfa
- current
->cfa
+ target
->args_size
;
1290 return current
->cfa
- target_cfa
- target
->args_size
;
1297 static inline _Unwind_Ptr
1298 uw_identify_context (struct _Unwind_Context
*context
)
1300 return _Unwind_GetIP (context
);
1304 #include "unwind.inc"