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 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
28 #include "unwind-pe.h"
29 #include "unwind-dw2-fde.h"
33 #ifndef __USING_SJLJ_EXCEPTIONS__
35 #ifndef STACK_GROWS_DOWNWARD
36 #define STACK_GROWS_DOWNWARD 0
38 #undef STACK_GROWS_DOWNWARD
39 #define STACK_GROWS_DOWNWARD 1
42 /* A target can override (perhaps for backward compatibility) how
43 many dwarf2 columns are unwound. */
44 #ifndef DWARF_FRAME_REGISTERS
45 #define DWARF_FRAME_REGISTERS FIRST_PSEUDO_REGISTER
48 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
49 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
50 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
53 #ifndef DWARF_REG_TO_UNWIND_COLUMN
54 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
57 /* This is the register and unwind state for a particular frame. This
58 provides the information necessary to unwind up past a frame and return
60 struct _Unwind_Context
62 void *reg[DWARF_FRAME_REGISTERS+1];
66 struct dwarf_eh_bases bases;
67 _Unwind_Word args_size;
70 /* Byte size of every register managed by these routines. */
71 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS];
74 /* The result of interpreting the frame unwind info for a frame.
75 This is all symbolic at this point, as none of the values can
76 be resolved until the target pc is located. */
79 /* Each register save state can be described in terms of a CFA slot,
80 another register, or a location expression. */
81 struct frame_state_reg_info
87 const unsigned char *exp;
95 } reg[DWARF_FRAME_REGISTERS+1];
97 /* Used to implement DW_CFA_remember_state. */
98 struct frame_state_reg_info *prev;
101 /* The CFA can be described in terms of a reg+offset or a
102 location expression. */
103 _Unwind_Sword cfa_offset;
104 _Unwind_Word cfa_reg;
105 const unsigned char *cfa_exp;
112 /* The PC described by the current frame state. */
115 /* The information we care about from the CIE/FDE. */
116 _Unwind_Personality_Fn personality;
117 _Unwind_Sword data_align;
118 _Unwind_Word code_align;
119 unsigned char retaddr_column;
120 unsigned char fde_encoding;
121 unsigned char lsda_encoding;
124 } _Unwind_FrameState;
126 /* Read unaligned data from the instruction buffer. */
131 unsigned u2 __attribute__ ((mode (HI)));
132 unsigned u4 __attribute__ ((mode (SI)));
133 unsigned u8 __attribute__ ((mode (DI)));
134 signed s2 __attribute__ ((mode (HI)));
135 signed s4 __attribute__ ((mode (SI)));
136 signed s8 __attribute__ ((mode (DI)));
137 } __attribute__ ((packed));
140 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
143 read_1u (const void *p) { return *(const unsigned char *) p; }
146 read_1s (const void *p) { return *(const signed char *) p; }
149 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
152 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
154 static inline unsigned int
155 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
158 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
160 static inline unsigned long
161 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
163 static inline unsigned long
164 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
166 /* Get the value of register REG as saved in CONTEXT. */
169 _Unwind_GetGR (struct _Unwind_Context *context, int index)
174 index = DWARF_REG_TO_UNWIND_COLUMN (index);
175 size = dwarf_reg_size_table[index];
176 ptr = context->reg[index];
178 /* This will segfault if the register hasn't been saved. */
179 if (size == sizeof(_Unwind_Ptr))
180 return * (_Unwind_Ptr *) ptr;
182 if (size == sizeof(_Unwind_Word))
183 return * (_Unwind_Word *) ptr;
189 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
191 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
194 /* Get the value of the CFA as saved in CONTEXT. */
197 _Unwind_GetCFA (struct _Unwind_Context *context)
199 return (_Unwind_Ptr) context->cfa;
202 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
205 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
210 index = DWARF_REG_TO_UNWIND_COLUMN (index);
211 size = dwarf_reg_size_table[index];
212 ptr = context->reg[index];
214 if (size == sizeof(_Unwind_Ptr))
215 * (_Unwind_Ptr *) ptr = val;
216 else if (size == sizeof(_Unwind_Word))
217 * (_Unwind_Word *) ptr = val;
222 /* Get the pointer to a register INDEX as saved in CONTEXT. */
225 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
227 index = DWARF_REG_TO_UNWIND_COLUMN (index);
228 return context->reg[index];
231 /* Set the pointer to a register INDEX as saved in CONTEXT. */
234 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
236 index = DWARF_REG_TO_UNWIND_COLUMN (index);
237 context->reg[index] = p;
240 /* Retrieve the return address for CONTEXT. */
243 _Unwind_GetIP (struct _Unwind_Context *context)
245 return (_Unwind_Ptr) context->ra;
248 /* Overwrite the return address for CONTEXT with VAL. */
251 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
253 context->ra = (void *) val;
257 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
259 return context->lsda;
263 _Unwind_GetRegionStart (struct _Unwind_Context *context)
265 return (_Unwind_Ptr) context->bases.func;
269 _Unwind_FindEnclosingFunction (void *pc)
271 struct dwarf_eh_bases bases;
272 struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
281 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
283 return (_Unwind_Ptr) context->bases.dbase;
287 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
289 return (_Unwind_Ptr) context->bases.tbase;
293 /* Extract any interesting information from the CIE for the translation
294 unit F belongs to. Return a pointer to the byte after the augmentation,
295 or NULL if we encountered an undecipherable augmentation. */
297 static const unsigned char *
298 extract_cie_info (struct dwarf_cie *cie, struct _Unwind_Context *context,
299 _Unwind_FrameState *fs)
301 const unsigned char *aug = cie->augmentation;
302 const unsigned char *p = aug + strlen (aug) + 1;
303 const unsigned char *ret = NULL;
306 /* g++ v2 "eh" has pointer immediately following augmentation string,
307 so it must be handled first. */
308 if (aug[0] == 'e' && aug[1] == 'h')
310 fs->eh_ptr = read_pointer (p);
311 p += sizeof (void *);
315 /* Immediately following the augmentation are the code and
316 data alignment and return address column. */
317 p = read_uleb128 (p, &fs->code_align);
318 p = read_sleb128 (p, &fs->data_align);
319 fs->retaddr_column = *p++;
320 fs->lsda_encoding = DW_EH_PE_omit;
322 /* If the augmentation starts with 'z', then a uleb128 immediately
323 follows containing the length of the augmentation field following
327 p = read_uleb128 (p, &utmp);
334 /* Iterate over recognized augmentation subsequences. */
337 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
340 fs->lsda_encoding = *p++;
344 /* "R" indicates a byte indicating how FDE addresses are encoded. */
345 else if (aug[0] == 'R')
347 fs->fde_encoding = *p++;
351 /* "P" indicates a personality routine in the CIE augmentation. */
352 else if (aug[0] == 'P')
354 p = read_encoded_value (context, *p, p + 1,
355 (_Unwind_Ptr *) &fs->personality);
359 /* Otherwise we have an unknown augmentation string.
360 Bail unless we saw a 'z' prefix. */
365 return ret ? ret : p;
369 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
370 onto the stack to start. */
373 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
374 struct _Unwind_Context *context, _Unwind_Word initial)
376 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
382 while (op_ptr < op_end)
384 enum dwarf_location_atom op = *op_ptr++;
385 _Unwind_Word result, reg, utmp;
386 _Unwind_Sword offset, stmp;
422 result = op - DW_OP_lit0;
426 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
427 op_ptr += sizeof (void *);
431 result = read_1u (op_ptr);
435 result = read_1s (op_ptr);
439 result = read_2u (op_ptr);
443 result = read_2s (op_ptr);
447 result = read_4u (op_ptr);
451 result = read_4s (op_ptr);
455 result = read_8u (op_ptr);
459 result = read_8s (op_ptr);
463 op_ptr = read_uleb128 (op_ptr, &result);
466 op_ptr = read_sleb128 (op_ptr, &stmp);
502 result = _Unwind_GetGR (context, op - DW_OP_reg0);
505 op_ptr = read_uleb128 (op_ptr, ®);
506 result = _Unwind_GetGR (context, reg);
541 op_ptr = read_sleb128 (op_ptr, &offset);
542 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
545 op_ptr = read_uleb128 (op_ptr, ®);
546 op_ptr = read_sleb128 (op_ptr, &offset);
547 result = _Unwind_GetGR (context, reg) + offset;
553 result = stack[stack_elt - 1];
563 if (offset >= stack_elt - 1)
565 result = stack[stack_elt - 1 - offset];
571 result = stack[stack_elt - 2];
576 _Unwind_Word t1, t2, t3;
580 t1 = stack[stack_elt - 1];
581 t2 = stack[stack_elt - 2];
582 t3 = stack[stack_elt - 3];
583 stack[stack_elt - 1] = t2;
584 stack[stack_elt - 2] = t3;
585 stack[stack_elt - 3] = t1;
590 case DW_OP_deref_size:
594 case DW_OP_plus_uconst:
595 /* Unary operations. */
598 result = stack[stack_elt];
604 void *ptr = (void *) (_Unwind_Ptr) result;
605 result = (_Unwind_Ptr) read_pointer (ptr);
609 case DW_OP_deref_size:
611 void *ptr = (void *) (_Unwind_Ptr) result;
615 result = read_1u (ptr);
618 result = read_2u (ptr);
621 result = read_4u (ptr);
624 result = read_8u (ptr);
633 if ((_Unwind_Sword) result < 0)
642 case DW_OP_plus_uconst:
643 op_ptr = read_uleb128 (op_ptr, &utmp);
666 /* Binary operations. */
667 _Unwind_Word first, second;
668 if ((stack_elt -= 2) < 0)
670 second = stack[stack_elt];
671 first = stack[stack_elt + 1];
676 result = second & first;
679 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
682 result = second - first;
685 result = (_Unwind_Sword) second % (_Unwind_Sword) first;
688 result = second * first;
691 result = second | first;
694 result = second + first;
697 result = second << first;
700 result = second >> first;
703 result = (_Unwind_Sword) second >> first;
706 result = second ^ first;
709 result = (_Unwind_Sword) first <= (_Unwind_Sword) second;
712 result = (_Unwind_Sword) first >= (_Unwind_Sword) second;
715 result = (_Unwind_Sword) first == (_Unwind_Sword) second;
718 result = (_Unwind_Sword) first < (_Unwind_Sword) second;
721 result = (_Unwind_Sword) first > (_Unwind_Sword) second;
724 result = (_Unwind_Sword) first != (_Unwind_Sword) second;
734 offset = read_2s (op_ptr);
742 offset = read_2s (op_ptr);
744 if (stack[stack_elt] != 0)
755 /* Most things push a result value. */
756 if ((size_t) stack_elt >= sizeof(stack)/sizeof(*stack))
758 stack[stack_elt++] = result;
762 /* We were executing this program to get a value. It should be
766 return stack[stack_elt];
770 /* Decode DWARF 2 call frame information. Takes pointers the
771 instruction sequence to decode, current register information and
772 CIE info, and the PC range to evaluate. */
775 execute_cfa_program (const unsigned char *insn_ptr,
776 const unsigned char *insn_end,
777 struct _Unwind_Context *context,
778 _Unwind_FrameState *fs)
780 struct frame_state_reg_info *unused_rs = NULL;
782 /* Don't allow remember/restore between CIE and FDE programs. */
783 fs->regs.prev = NULL;
785 /* The comparison with the return address uses < rather than <= because
786 we are only interested in the effects of code before the call; for a
787 noreturn function, the return address may point to unrelated code with
788 a different stack configuration that we are not interested in. We
789 assume that the call itself is unwind info-neutral; if not, or if
790 there are delay instructions that adjust the stack, these must be
791 reflected at the point immediately before the call insn. */
792 while (insn_ptr < insn_end && fs->pc < context->ra)
794 unsigned char insn = *insn_ptr++;
795 _Unwind_Word reg, utmp;
796 _Unwind_Sword offset, stmp;
798 if ((insn & 0xc0) == DW_CFA_advance_loc)
799 fs->pc += (insn & 0x3f) * fs->code_align;
800 else if ((insn & 0xc0) == DW_CFA_offset)
803 insn_ptr = read_uleb128 (insn_ptr, &utmp);
804 offset = (_Unwind_Sword) utmp * fs->data_align;
805 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
807 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
809 else if ((insn & 0xc0) == DW_CFA_restore)
812 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
817 insn_ptr = read_encoded_value (context, fs->fde_encoding,
818 insn_ptr, (_Unwind_Ptr *) &fs->pc);
821 case DW_CFA_advance_loc1:
822 fs->pc += read_1u (insn_ptr) * fs->code_align;
825 case DW_CFA_advance_loc2:
826 fs->pc += read_2u (insn_ptr) * fs->code_align;
829 case DW_CFA_advance_loc4:
830 fs->pc += read_4u (insn_ptr) * fs->code_align;
834 case DW_CFA_offset_extended:
835 insn_ptr = read_uleb128 (insn_ptr, ®);
836 insn_ptr = read_uleb128 (insn_ptr, &utmp);
837 offset = (_Unwind_Sword) utmp * fs->data_align;
838 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
840 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
843 case DW_CFA_restore_extended:
844 insn_ptr = read_uleb128 (insn_ptr, ®);
845 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
848 case DW_CFA_undefined:
849 case DW_CFA_same_value:
850 insn_ptr = read_uleb128 (insn_ptr, ®);
856 case DW_CFA_register:
859 insn_ptr = read_uleb128 (insn_ptr, ®);
860 insn_ptr = read_uleb128 (insn_ptr, ®2);
861 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
862 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg = reg2;
866 case DW_CFA_remember_state:
868 struct frame_state_reg_info *new_rs;
872 unused_rs = unused_rs->prev;
875 new_rs = __builtin_alloca (sizeof (struct frame_state_reg_info));
878 fs->regs.prev = new_rs;
882 case DW_CFA_restore_state:
884 struct frame_state_reg_info *old_rs = fs->regs.prev;
886 old_rs->prev = unused_rs;
892 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
893 insn_ptr = read_uleb128 (insn_ptr, &utmp);
894 fs->cfa_offset = utmp;
895 fs->cfa_how = CFA_REG_OFFSET;
898 case DW_CFA_def_cfa_register:
899 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
900 fs->cfa_how = CFA_REG_OFFSET;
903 case DW_CFA_def_cfa_offset:
904 insn_ptr = read_uleb128 (insn_ptr, &utmp);
905 fs->cfa_offset = utmp;
906 /* cfa_how deliberately not set. */
909 case DW_CFA_def_cfa_expression:
910 fs->cfa_exp = insn_ptr;
911 fs->cfa_how = CFA_EXP;
912 insn_ptr = read_uleb128 (insn_ptr, &utmp);
916 case DW_CFA_expression:
917 insn_ptr = read_uleb128 (insn_ptr, ®);
918 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
919 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
920 insn_ptr = read_uleb128 (insn_ptr, &utmp);
924 /* From the 2.1 draft. */
925 case DW_CFA_offset_extended_sf:
926 insn_ptr = read_uleb128 (insn_ptr, ®);
927 insn_ptr = read_sleb128 (insn_ptr, &stmp);
928 offset = stmp * fs->data_align;
929 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
931 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
934 case DW_CFA_def_cfa_sf:
935 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
936 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
937 fs->cfa_how = CFA_REG_OFFSET;
940 case DW_CFA_def_cfa_offset_sf:
941 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
942 /* cfa_how deliberately not set. */
945 case DW_CFA_GNU_window_save:
946 /* ??? Hardcoded for SPARC register window configuration. */
947 for (reg = 16; reg < 32; ++reg)
949 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
950 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
954 case DW_CFA_GNU_args_size:
955 insn_ptr = read_uleb128 (insn_ptr, &context->args_size);
958 case DW_CFA_GNU_negative_offset_extended:
959 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
960 older PowerPC code. */
961 insn_ptr = read_uleb128 (insn_ptr, ®);
962 insn_ptr = read_uleb128 (insn_ptr, &utmp);
963 offset = (_Unwind_Word) utmp * fs->data_align;
964 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
966 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
975 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
976 its caller and decode it into FS. This function also sets the
977 args_size and lsda members of CONTEXT, as they are really information
978 about the caller's frame. */
980 static _Unwind_Reason_Code
981 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
983 struct dwarf_fde *fde;
984 struct dwarf_cie *cie;
985 const unsigned char *aug, *insn, *end;
987 memset (fs, 0, sizeof (*fs));
988 context->args_size = 0;
991 fde = _Unwind_Find_FDE (context->ra - 1, &context->bases);
994 /* Couldn't find frame unwind info for this function. Try a
995 target-specific fallback mechanism. This will necessarily
996 not provide a personality routine or LSDA. */
997 #ifdef MD_FALLBACK_FRAME_STATE_FOR
998 MD_FALLBACK_FRAME_STATE_FOR (context, fs, success);
999 return _URC_END_OF_STACK;
1001 return _URC_NO_REASON;
1003 return _URC_END_OF_STACK;
1007 fs->pc = context->bases.func;
1009 cie = get_cie (fde);
1010 insn = extract_cie_info (cie, context, fs);
1012 /* CIE contained unknown augmentation. */
1013 return _URC_FATAL_PHASE1_ERROR;
1015 /* First decode all the insns in the CIE. */
1016 end = (unsigned char *) next_fde ((struct dwarf_fde *) cie);
1017 execute_cfa_program (insn, end, context, fs);
1019 /* Locate augmentation for the fde. */
1020 aug = (unsigned char *) fde + sizeof (*fde);
1021 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1026 aug = read_uleb128 (aug, &i);
1029 if (fs->lsda_encoding != DW_EH_PE_omit)
1030 aug = read_encoded_value (context, fs->lsda_encoding, aug,
1031 (_Unwind_Ptr *) &context->lsda);
1033 /* Then the insns in the FDE up to our target PC. */
1036 end = (unsigned char *) next_fde (fde);
1037 execute_cfa_program (insn, end, context, fs);
1039 return _URC_NO_REASON;
1042 typedef struct frame_state
1048 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1049 unsigned short cfa_reg;
1050 unsigned short retaddr_column;
1051 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1054 struct frame_state * __frame_state_for (void *, struct frame_state *);
1056 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1057 a given PC_TARGET. The caller should allocate a local variable of
1058 `struct frame_state' and pass its address to STATE_IN. */
1060 struct frame_state *
1061 __frame_state_for (void *pc_target, struct frame_state *state_in)
1063 struct _Unwind_Context context;
1064 _Unwind_FrameState fs;
1067 memset (&context, 0, sizeof (struct _Unwind_Context));
1068 context.ra = pc_target + 1;
1070 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1073 /* We have no way to pass a location expression for the CFA to our
1074 caller. It wouldn't understand it anyway. */
1075 if (fs.cfa_how == CFA_EXP)
1078 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1080 state_in->saved[reg] = fs.regs.reg[reg].how;
1081 switch (state_in->saved[reg])
1084 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1086 case REG_SAVED_OFFSET:
1087 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1090 state_in->reg_or_offset[reg] = 0;
1095 state_in->cfa_offset = fs.cfa_offset;
1096 state_in->cfa_reg = fs.cfa_reg;
1097 state_in->retaddr_column = fs.retaddr_column;
1098 state_in->args_size = context.args_size;
1099 state_in->eh_ptr = fs.eh_ptr;
1104 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1107 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1108 _Unwind_SpTmp *tmp_sp)
1110 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1112 if (size == sizeof(_Unwind_Ptr))
1113 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1114 else if (size == sizeof(_Unwind_Word))
1115 tmp_sp->word = (_Unwind_Ptr) cfa;
1118 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1122 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1124 struct _Unwind_Context orig_context = *context;
1128 #ifdef EH_RETURN_STACKADJ_RTX
1129 /* Special handling here: Many machines do not use a frame pointer,
1130 and track the CFA only through offsets from the stack pointer from
1131 one frame to the next. In this case, the stack pointer is never
1132 stored, so it has no saved address in the context. What we do
1133 have is the CFA from the previous stack frame.
1135 In very special situations (such as unwind info for signal return),
1136 there may be location expressions that use the stack pointer as well.
1138 Do this conditionally for one frame. This allows the unwind info
1139 for one frame to save a copy of the stack pointer from the previous
1140 frame, and be able to use much easier CFA mechanisms to do it.
1141 Always zap the saved stack pointer value for the next frame; carrying
1142 the value over from one frame to another doesn't make sense. */
1144 _Unwind_SpTmp tmp_sp;
1146 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1147 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1148 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1151 /* Compute this frame's CFA. */
1152 switch (fs->cfa_how)
1154 case CFA_REG_OFFSET:
1155 cfa = _Unwind_GetPtr (&orig_context, fs->cfa_reg);
1156 cfa += fs->cfa_offset;
1161 const unsigned char *exp = fs->cfa_exp;
1164 exp = read_uleb128 (exp, &len);
1165 cfa = (void *) (_Unwind_Ptr)
1166 execute_stack_op (exp, exp + len, &orig_context, 0);
1175 /* Compute the addresses of all registers saved in this frame. */
1176 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1177 switch (fs->regs.reg[i].how)
1182 case REG_SAVED_OFFSET:
1183 _Unwind_SetGRPtr (context, i,
1184 (void *) (cfa + fs->regs.reg[i].loc.offset));
1190 _Unwind_GetGRPtr (&orig_context, fs->regs.reg[i].loc.reg));
1195 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1199 exp = read_uleb128 (exp, &len);
1200 val = execute_stack_op (exp, exp + len, &orig_context,
1202 _Unwind_SetGRPtr (context, i, (void *) val);
1208 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1209 of its caller. Update CONTEXT to refer to the caller as well. Note
1210 that the args_size and lsda members are not updated here, but later in
1211 uw_frame_state_for. */
1214 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1216 uw_update_context_1 (context, fs);
1218 /* Compute the return address now, since the return address column
1219 can change from frame to frame. */
1220 context->ra = __builtin_extract_return_addr
1221 (_Unwind_GetPtr (context, fs->retaddr_column));
1224 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1225 level will be the return address and the CFA. */
1227 #define uw_init_context(CONTEXT) \
1230 /* Do any necessary initialization to access arbitrary stack frames. \
1231 On the SPARC, this means flushing the register windows. */ \
1232 __builtin_unwind_init (); \
1233 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1234 __builtin_return_address (0)); \
1239 init_dwarf_reg_size_table (void)
1241 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1245 uw_init_context_1 (struct _Unwind_Context *context,
1246 void *outer_cfa, void *outer_ra)
1248 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1249 _Unwind_FrameState fs;
1250 _Unwind_SpTmp sp_slot;
1252 memset (context, 0, sizeof (struct _Unwind_Context));
1255 if (uw_frame_state_for (context, &fs) != _URC_NO_REASON)
1260 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1261 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1262 || dwarf_reg_size_table[0] == 0)
1263 init_dwarf_reg_size_table ();
1266 if (dwarf_reg_size_table[0] == 0)
1267 init_dwarf_reg_size_table ();
1270 /* Force the frame state to use the known cfa value. */
1271 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1272 fs.cfa_how = CFA_REG_OFFSET;
1273 fs.cfa_reg = __builtin_dwarf_sp_column ();
1276 uw_update_context_1 (context, &fs);
1278 /* If the return address column was saved in a register in the
1279 initialization context, then we can't see it in the given
1280 call frame data. So have the initialization context tell us. */
1281 context->ra = __builtin_extract_return_addr (outer_ra);
1285 /* Install TARGET into CURRENT so that we can return to it. This is a
1286 macro because __builtin_eh_return must be invoked in the context of
1289 #define uw_install_context(CURRENT, TARGET) \
1292 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1293 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1294 __builtin_eh_return (offset, handler); \
1299 uw_install_context_1 (struct _Unwind_Context *current,
1300 struct _Unwind_Context *target)
1304 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1306 void *c = current->reg[i];
1307 void *t = target->reg[i];
1309 if (t && c && t != c)
1310 memcpy (c, t, dwarf_reg_size_table[i]);
1313 #ifdef EH_RETURN_STACKADJ_RTX
1317 /* If the last frame records a saved stack pointer, use it. */
1318 if (_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1319 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1321 target_cfa = target->cfa;
1323 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1324 if (STACK_GROWS_DOWNWARD)
1325 return target_cfa - current->cfa + target->args_size;
1327 return current->cfa - target_cfa - target->args_size;
1334 static inline _Unwind_Ptr
1335 uw_identify_context (struct _Unwind_Context *context)
1337 return _Unwind_GetIP (context);
1341 #include "unwind.inc"
1343 #endif /* !USING_SJLJ_EXCEPTIONS */