1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, 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;
77 /* Byte size of every register managed by these routines. */
78 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
81 /* Read unaligned data from the instruction buffer. */
86 unsigned u2 __attribute__ ((mode (HI)));
87 unsigned u4 __attribute__ ((mode (SI)));
88 unsigned u8 __attribute__ ((mode (DI)));
89 signed s2 __attribute__ ((mode (HI)));
90 signed s4 __attribute__ ((mode (SI)));
91 signed s8 __attribute__ ((mode (DI)));
92 } __attribute__ ((packed));
95 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
98 read_1u (const void *p) { return *(const unsigned char *) p; }
101 read_1s (const void *p) { return *(const signed char *) p; }
104 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
107 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
109 static inline unsigned int
110 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
113 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
115 static inline unsigned long
116 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
118 static inline unsigned long
119 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
121 /* Get the value of register REG as saved in CONTEXT. */
124 _Unwind_GetGR (struct _Unwind_Context *context, int index)
129 #ifdef DWARF_ZERO_REG
130 if (index == DWARF_ZERO_REG)
134 index = DWARF_REG_TO_UNWIND_COLUMN (index);
135 gcc_assert (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;
144 gcc_assert (size == sizeof(_Unwind_Word));
145 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 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
173 size = dwarf_reg_size_table[index];
174 ptr = context->reg[index];
176 if (size == sizeof(_Unwind_Ptr))
177 * (_Unwind_Ptr *) ptr = val;
180 gcc_assert (size == sizeof(_Unwind_Word));
181 * (_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 /* Retrieve the return address and flag whether that IP is before
212 or after first not yet fully executed instruction. */
215 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
217 *ip_before_insn = context->signal_frame != 0;
218 return (_Unwind_Ptr) context->ra;
221 /* Overwrite the return address for CONTEXT with VAL. */
224 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
226 context->ra = (void *) val;
230 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
232 return context->lsda;
236 _Unwind_GetRegionStart (struct _Unwind_Context *context)
238 return (_Unwind_Ptr) context->bases.func;
242 _Unwind_FindEnclosingFunction (void *pc)
244 struct dwarf_eh_bases bases;
245 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
254 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
256 return (_Unwind_Ptr) context->bases.dbase;
260 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
262 return (_Unwind_Ptr) context->bases.tbase;
266 #ifdef MD_UNWIND_SUPPORT
267 #include MD_UNWIND_SUPPORT
270 /* Extract any interesting information from the CIE for the translation
271 unit F belongs to. Return a pointer to the byte after the augmentation,
272 or NULL if we encountered an undecipherable augmentation. */
274 static const unsigned char *
275 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
276 _Unwind_FrameState *fs)
278 const unsigned char *aug = cie->augmentation;
279 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
280 const unsigned char *ret = NULL;
283 /* g++ v2 "eh" has pointer immediately following augmentation string,
284 so it must be handled first. */
285 if (aug[0] == 'e' && aug[1] == 'h')
287 fs->eh_ptr = read_pointer (p);
288 p += sizeof (void *);
292 /* Immediately following the augmentation are the code and
293 data alignment and return address column. */
294 p = read_uleb128 (p, &fs->code_align);
295 p = read_sleb128 (p, &fs->data_align);
296 if (cie->version == 1)
297 fs->retaddr_column = *p++;
299 p = read_uleb128 (p, &fs->retaddr_column);
300 fs->lsda_encoding = DW_EH_PE_omit;
302 /* If the augmentation starts with 'z', then a uleb128 immediately
303 follows containing the length of the augmentation field following
307 p = read_uleb128 (p, &utmp);
314 /* Iterate over recognized augmentation subsequences. */
317 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
320 fs->lsda_encoding = *p++;
324 /* "R" indicates a byte indicating how FDE addresses are encoded. */
325 else if (aug[0] == 'R')
327 fs->fde_encoding = *p++;
331 /* "P" indicates a personality routine in the CIE augmentation. */
332 else if (aug[0] == 'P')
334 _Unwind_Ptr personality;
336 p = read_encoded_value (context, *p, p + 1, &personality);
337 fs->personality = (_Unwind_Personality_Fn) personality;
341 /* "S" indicates a signal frame. */
342 else if (aug[0] == 'S')
344 fs->signal_frame = 1;
348 /* Otherwise we have an unknown augmentation string.
349 Bail unless we saw a 'z' prefix. */
354 return ret ? ret : p;
358 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
359 onto the stack to start. */
362 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
363 struct _Unwind_Context *context, _Unwind_Word initial)
365 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
371 while (op_ptr < op_end)
373 enum dwarf_location_atom op = *op_ptr++;
374 _Unwind_Word result, reg, utmp;
375 _Unwind_Sword offset, stmp;
411 result = op - DW_OP_lit0;
415 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
416 op_ptr += sizeof (void *);
420 result = read_1u (op_ptr);
424 result = read_1s (op_ptr);
428 result = read_2u (op_ptr);
432 result = read_2s (op_ptr);
436 result = read_4u (op_ptr);
440 result = read_4s (op_ptr);
444 result = read_8u (op_ptr);
448 result = read_8s (op_ptr);
452 op_ptr = read_uleb128 (op_ptr, &result);
455 op_ptr = read_sleb128 (op_ptr, &stmp);
491 result = _Unwind_GetGR (context, op - DW_OP_reg0);
494 op_ptr = read_uleb128 (op_ptr, ®);
495 result = _Unwind_GetGR (context, reg);
530 op_ptr = read_sleb128 (op_ptr, &offset);
531 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
534 op_ptr = read_uleb128 (op_ptr, ®);
535 op_ptr = read_sleb128 (op_ptr, &offset);
536 result = _Unwind_GetGR (context, reg) + offset;
540 gcc_assert (stack_elt);
541 result = stack[stack_elt - 1];
545 gcc_assert (stack_elt);
551 gcc_assert (offset < stack_elt - 1);
552 result = stack[stack_elt - 1 - offset];
556 gcc_assert (stack_elt >= 2);
557 result = stack[stack_elt - 2];
562 _Unwind_Word t1, t2, t3;
564 gcc_assert (stack_elt >= 3);
565 t1 = stack[stack_elt - 1];
566 t2 = stack[stack_elt - 2];
567 t3 = stack[stack_elt - 3];
568 stack[stack_elt - 1] = t2;
569 stack[stack_elt - 2] = t3;
570 stack[stack_elt - 3] = t1;
575 case DW_OP_deref_size:
579 case DW_OP_plus_uconst:
580 /* Unary operations. */
581 gcc_assert (stack_elt);
584 result = stack[stack_elt];
590 void *ptr = (void *) (_Unwind_Ptr) result;
591 result = (_Unwind_Ptr) read_pointer (ptr);
595 case DW_OP_deref_size:
597 void *ptr = (void *) (_Unwind_Ptr) result;
601 result = read_1u (ptr);
604 result = read_2u (ptr);
607 result = read_4u (ptr);
610 result = read_8u (ptr);
619 if ((_Unwind_Sword) result < 0)
628 case DW_OP_plus_uconst:
629 op_ptr = read_uleb128 (op_ptr, &utmp);
656 /* Binary operations. */
657 _Unwind_Word first, second;
658 gcc_assert (stack_elt >= 2);
661 second = stack[stack_elt];
662 first = stack[stack_elt + 1];
667 result = second & first;
670 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
673 result = second - first;
676 result = (_Unwind_Sword) second % (_Unwind_Sword) first;
679 result = second * first;
682 result = second | first;
685 result = second + first;
688 result = second << first;
691 result = second >> first;
694 result = (_Unwind_Sword) second >> first;
697 result = second ^ first;
700 result = (_Unwind_Sword) first <= (_Unwind_Sword) second;
703 result = (_Unwind_Sword) first >= (_Unwind_Sword) second;
706 result = (_Unwind_Sword) first == (_Unwind_Sword) second;
709 result = (_Unwind_Sword) first < (_Unwind_Sword) second;
712 result = (_Unwind_Sword) first > (_Unwind_Sword) second;
715 result = (_Unwind_Sword) first != (_Unwind_Sword) second;
725 offset = read_2s (op_ptr);
731 gcc_assert (stack_elt);
734 offset = read_2s (op_ptr);
736 if (stack[stack_elt] != 0)
747 /* Most things push a result value. */
748 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
749 stack[stack_elt++] = result;
753 /* We were executing this program to get a value. It should be
755 gcc_assert (stack_elt);
757 return stack[stack_elt];
761 /* Decode DWARF 2 call frame information. Takes pointers the
762 instruction sequence to decode, current register information and
763 CIE info, and the PC range to evaluate. */
766 execute_cfa_program (const unsigned char *insn_ptr,
767 const unsigned char *insn_end,
768 struct _Unwind_Context *context,
769 _Unwind_FrameState *fs)
771 struct frame_state_reg_info *unused_rs = NULL;
773 /* Don't allow remember/restore between CIE and FDE programs. */
774 fs->regs.prev = NULL;
776 /* The comparison with the return address uses < rather than <= because
777 we are only interested in the effects of code before the call; for a
778 noreturn function, the return address may point to unrelated code with
779 a different stack configuration that we are not interested in. We
780 assume that the call itself is unwind info-neutral; if not, or if
781 there are delay instructions that adjust the stack, these must be
782 reflected at the point immediately before the call insn.
783 In signal frames, return address is after last completed instruction,
784 so we add 1 to return address to make the comparison <=. */
785 while (insn_ptr < insn_end && fs->pc < context->ra + context->signal_frame)
787 unsigned char insn = *insn_ptr++;
788 _Unwind_Word reg, utmp;
789 _Unwind_Sword offset, stmp;
791 if ((insn & 0xc0) == DW_CFA_advance_loc)
792 fs->pc += (insn & 0x3f) * fs->code_align;
793 else if ((insn & 0xc0) == DW_CFA_offset)
796 insn_ptr = read_uleb128 (insn_ptr, &utmp);
797 offset = (_Unwind_Sword) utmp * fs->data_align;
798 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
800 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
802 else if ((insn & 0xc0) == DW_CFA_restore)
805 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
813 insn_ptr = read_encoded_value (context, fs->fde_encoding,
815 fs->pc = (void *) pc;
819 case DW_CFA_advance_loc1:
820 fs->pc += read_1u (insn_ptr) * fs->code_align;
823 case DW_CFA_advance_loc2:
824 fs->pc += read_2u (insn_ptr) * fs->code_align;
827 case DW_CFA_advance_loc4:
828 fs->pc += read_4u (insn_ptr) * fs->code_align;
832 case DW_CFA_offset_extended:
833 insn_ptr = read_uleb128 (insn_ptr, ®);
834 insn_ptr = read_uleb128 (insn_ptr, &utmp);
835 offset = (_Unwind_Sword) utmp * fs->data_align;
836 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
838 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
841 case DW_CFA_restore_extended:
842 insn_ptr = read_uleb128 (insn_ptr, ®);
843 /* FIXME, this is wrong; the CIE might have said that the
844 register was saved somewhere. */
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, ®);
851 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
857 case DW_CFA_register:
860 insn_ptr = read_uleb128 (insn_ptr, ®);
861 insn_ptr = read_uleb128 (insn_ptr, ®2);
862 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
863 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg = reg2;
867 case DW_CFA_remember_state:
869 struct frame_state_reg_info *new_rs;
873 unused_rs = unused_rs->prev;
876 new_rs = alloca (sizeof (struct frame_state_reg_info));
879 fs->regs.prev = new_rs;
883 case DW_CFA_restore_state:
885 struct frame_state_reg_info *old_rs = fs->regs.prev;
887 old_rs->prev = unused_rs;
893 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
894 insn_ptr = read_uleb128 (insn_ptr, &utmp);
895 fs->cfa_offset = utmp;
896 fs->cfa_how = CFA_REG_OFFSET;
899 case DW_CFA_def_cfa_register:
900 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
901 fs->cfa_how = CFA_REG_OFFSET;
904 case DW_CFA_def_cfa_offset:
905 insn_ptr = read_uleb128 (insn_ptr, &utmp);
906 fs->cfa_offset = utmp;
907 /* cfa_how deliberately not set. */
910 case DW_CFA_def_cfa_expression:
911 fs->cfa_exp = insn_ptr;
912 fs->cfa_how = CFA_EXP;
913 insn_ptr = read_uleb128 (insn_ptr, &utmp);
917 case DW_CFA_expression:
918 insn_ptr = read_uleb128 (insn_ptr, ®);
919 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
920 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
921 insn_ptr = read_uleb128 (insn_ptr, &utmp);
925 /* From the dwarf3 draft. */
926 case DW_CFA_offset_extended_sf:
927 insn_ptr = read_uleb128 (insn_ptr, ®);
928 insn_ptr = read_sleb128 (insn_ptr, &stmp);
929 offset = stmp * fs->data_align;
930 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
932 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
935 case DW_CFA_def_cfa_sf:
936 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
937 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
938 fs->cfa_how = CFA_REG_OFFSET;
939 fs->cfa_offset *= fs->data_align;
942 case DW_CFA_def_cfa_offset_sf:
943 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
944 fs->cfa_offset *= fs->data_align;
945 /* cfa_how deliberately not set. */
948 case DW_CFA_GNU_window_save:
949 /* ??? Hardcoded for SPARC register window configuration. */
950 for (reg = 16; reg < 32; ++reg)
952 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
953 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
957 case DW_CFA_GNU_args_size:
958 insn_ptr = read_uleb128 (insn_ptr, &context->args_size);
961 case DW_CFA_GNU_negative_offset_extended:
962 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
963 older PowerPC code. */
964 insn_ptr = read_uleb128 (insn_ptr, ®);
965 insn_ptr = read_uleb128 (insn_ptr, &utmp);
966 offset = (_Unwind_Word) utmp * fs->data_align;
967 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
969 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
978 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
979 its caller and decode it into FS. This function also sets the
980 args_size and lsda members of CONTEXT, as they are really information
981 about the caller's frame. */
983 static _Unwind_Reason_Code
984 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
986 const struct dwarf_fde *fde;
987 const struct dwarf_cie *cie;
988 const unsigned char *aug, *insn, *end;
990 memset (fs, 0, sizeof (*fs));
991 context->args_size = 0;
994 if (context->ra == 0)
995 return _URC_END_OF_STACK;
997 fde = _Unwind_Find_FDE (context->ra + context->signal_frame - 1,
1001 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1002 /* Couldn't find frame unwind info for this function. Try a
1003 target-specific fallback mechanism. This will necessarily
1004 not provide a personality routine or LSDA. */
1005 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1007 return _URC_END_OF_STACK;
1011 fs->pc = context->bases.func;
1013 cie = get_cie (fde);
1014 insn = extract_cie_info (cie, context, fs);
1016 /* CIE contained unknown augmentation. */
1017 return _URC_FATAL_PHASE1_ERROR;
1019 /* First decode all the insns in the CIE. */
1020 end = (unsigned char *) next_fde ((struct dwarf_fde *) cie);
1021 execute_cfa_program (insn, end, context, fs);
1023 /* Locate augmentation for the fde. */
1024 aug = (unsigned char *) fde + sizeof (*fde);
1025 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1030 aug = read_uleb128 (aug, &i);
1033 if (fs->lsda_encoding != DW_EH_PE_omit)
1037 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1038 context->lsda = (void *) lsda;
1041 /* Then the insns in the FDE up to our target PC. */
1044 end = (unsigned char *) next_fde (fde);
1045 execute_cfa_program (insn, end, context, fs);
1047 return _URC_NO_REASON;
1050 typedef struct frame_state
1056 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1057 unsigned short cfa_reg;
1058 unsigned short retaddr_column;
1059 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1062 struct frame_state * __frame_state_for (void *, struct frame_state *);
1064 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1065 a given PC_TARGET. The caller should allocate a local variable of
1066 `struct frame_state' and pass its address to STATE_IN. */
1068 struct frame_state *
1069 __frame_state_for (void *pc_target, struct frame_state *state_in)
1071 struct _Unwind_Context context;
1072 _Unwind_FrameState fs;
1075 memset (&context, 0, sizeof (struct _Unwind_Context));
1076 context.ra = pc_target + 1;
1078 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1081 /* We have no way to pass a location expression for the CFA to our
1082 caller. It wouldn't understand it anyway. */
1083 if (fs.cfa_how == CFA_EXP)
1086 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1088 state_in->saved[reg] = fs.regs.reg[reg].how;
1089 switch (state_in->saved[reg])
1092 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1094 case REG_SAVED_OFFSET:
1095 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1098 state_in->reg_or_offset[reg] = 0;
1103 state_in->cfa_offset = fs.cfa_offset;
1104 state_in->cfa_reg = fs.cfa_reg;
1105 state_in->retaddr_column = fs.retaddr_column;
1106 state_in->args_size = context.args_size;
1107 state_in->eh_ptr = fs.eh_ptr;
1112 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1115 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1116 _Unwind_SpTmp *tmp_sp)
1118 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1120 if (size == sizeof(_Unwind_Ptr))
1121 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1124 gcc_assert (size == sizeof(_Unwind_Word));
1125 tmp_sp->word = (_Unwind_Ptr) cfa;
1127 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1131 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1133 struct _Unwind_Context orig_context = *context;
1137 #ifdef EH_RETURN_STACKADJ_RTX
1138 /* Special handling here: Many machines do not use a frame pointer,
1139 and track the CFA only through offsets from the stack pointer from
1140 one frame to the next. In this case, the stack pointer is never
1141 stored, so it has no saved address in the context. What we do
1142 have is the CFA from the previous stack frame.
1144 In very special situations (such as unwind info for signal return),
1145 there may be location expressions that use the stack pointer as well.
1147 Do this conditionally for one frame. This allows the unwind info
1148 for one frame to save a copy of the stack pointer from the previous
1149 frame, and be able to use much easier CFA mechanisms to do it.
1150 Always zap the saved stack pointer value for the next frame; carrying
1151 the value over from one frame to another doesn't make sense. */
1153 _Unwind_SpTmp tmp_sp;
1155 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1156 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1157 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1160 /* Compute this frame's CFA. */
1161 switch (fs->cfa_how)
1163 case CFA_REG_OFFSET:
1164 cfa = _Unwind_GetPtr (&orig_context, fs->cfa_reg);
1165 cfa += fs->cfa_offset;
1170 const unsigned char *exp = fs->cfa_exp;
1173 exp = read_uleb128 (exp, &len);
1174 cfa = (void *) (_Unwind_Ptr)
1175 execute_stack_op (exp, exp + len, &orig_context, 0);
1184 /* Compute the addresses of all registers saved in this frame. */
1185 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1186 switch (fs->regs.reg[i].how)
1191 case REG_SAVED_OFFSET:
1192 _Unwind_SetGRPtr (context, i,
1193 (void *) (cfa + fs->regs.reg[i].loc.offset));
1199 _Unwind_GetGRPtr (&orig_context, fs->regs.reg[i].loc.reg));
1204 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1208 exp = read_uleb128 (exp, &len);
1209 val = execute_stack_op (exp, exp + len, &orig_context,
1211 _Unwind_SetGRPtr (context, i, (void *) val);
1216 context->signal_frame = fs->signal_frame;
1218 #ifdef MD_FROB_UPDATE_CONTEXT
1219 MD_FROB_UPDATE_CONTEXT (context, fs);
1223 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1224 of its caller. Update CONTEXT to refer to the caller as well. Note
1225 that the args_size and lsda members are not updated here, but later in
1226 uw_frame_state_for. */
1229 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1231 uw_update_context_1 (context, fs);
1233 /* Compute the return address now, since the return address column
1234 can change from frame to frame. */
1235 context->ra = __builtin_extract_return_addr
1236 (_Unwind_GetPtr (context, fs->retaddr_column));
1240 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1242 uw_update_context (context, fs);
1245 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1246 level will be the return address and the CFA. */
1248 #define uw_init_context(CONTEXT) \
1251 /* Do any necessary initialization to access arbitrary stack frames. \
1252 On the SPARC, this means flushing the register windows. */ \
1253 __builtin_unwind_init (); \
1254 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1255 __builtin_return_address (0)); \
1260 init_dwarf_reg_size_table (void)
1262 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1266 uw_init_context_1 (struct _Unwind_Context *context,
1267 void *outer_cfa, void *outer_ra)
1269 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1270 _Unwind_FrameState fs;
1271 _Unwind_SpTmp sp_slot;
1272 _Unwind_Reason_Code code;
1274 memset (context, 0, sizeof (struct _Unwind_Context));
1277 code = uw_frame_state_for (context, &fs);
1278 gcc_assert (code == _URC_NO_REASON);
1282 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1283 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1284 || dwarf_reg_size_table[0] == 0)
1285 init_dwarf_reg_size_table ();
1288 if (dwarf_reg_size_table[0] == 0)
1289 init_dwarf_reg_size_table ();
1292 /* Force the frame state to use the known cfa value. */
1293 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1294 fs.cfa_how = CFA_REG_OFFSET;
1295 fs.cfa_reg = __builtin_dwarf_sp_column ();
1298 uw_update_context_1 (context, &fs);
1300 /* If the return address column was saved in a register in the
1301 initialization context, then we can't see it in the given
1302 call frame data. So have the initialization context tell us. */
1303 context->ra = __builtin_extract_return_addr (outer_ra);
1307 /* Install TARGET into CURRENT so that we can return to it. This is a
1308 macro because __builtin_eh_return must be invoked in the context of
1311 #define uw_install_context(CURRENT, TARGET) \
1314 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1315 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1316 __builtin_eh_return (offset, handler); \
1321 uw_install_context_1 (struct _Unwind_Context *current,
1322 struct _Unwind_Context *target)
1325 _Unwind_SpTmp sp_slot;
1327 /* If the target frame does not have a saved stack pointer,
1328 then set up the target's CFA. */
1329 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1330 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1332 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1334 void *c = current->reg[i];
1335 void *t = target->reg[i];
1337 if (t && c && t != c)
1338 memcpy (c, t, dwarf_reg_size_table[i]);
1341 /* If the current frame doesn't have a saved stack pointer, then we
1342 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1343 pointer value reloaded. */
1344 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1348 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1350 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1351 if (STACK_GROWS_DOWNWARD)
1352 return target_cfa - current->cfa + target->args_size;
1354 return current->cfa - target_cfa - target->args_size;
1359 static inline _Unwind_Ptr
1360 uw_identify_context (struct _Unwind_Context *context)
1362 return _Unwind_GetIP (context);
1366 #include "unwind.inc"
1368 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1369 alias (_Unwind_Backtrace);
1370 alias (_Unwind_DeleteException);
1371 alias (_Unwind_FindEnclosingFunction);
1372 alias (_Unwind_ForcedUnwind);
1373 alias (_Unwind_GetDataRelBase);
1374 alias (_Unwind_GetTextRelBase);
1375 alias (_Unwind_GetCFA);
1376 alias (_Unwind_GetGR);
1377 alias (_Unwind_GetIP);
1378 alias (_Unwind_GetLanguageSpecificData);
1379 alias (_Unwind_GetRegionStart);
1380 alias (_Unwind_RaiseException);
1381 alias (_Unwind_Resume);
1382 alias (_Unwind_Resume_or_Rethrow);
1383 alias (_Unwind_SetGR);
1384 alias (_Unwind_SetIP);
1387 #endif /* !USING_SJLJ_EXCEPTIONS */