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 /* Signal frame context. */
74 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
75 /* Context which has version/args_size/by_value fields. */
76 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
78 /* 0 for now, can be increased when further fields are added to
79 struct _Unwind_Context. */
81 _Unwind_Word args_size;
82 char by_value[DWARF_FRAME_REGISTERS+1];
85 /* Byte size of every register managed by these routines. */
86 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
89 /* Read unaligned data from the instruction buffer. */
94 unsigned u2 __attribute__ ((mode (HI)));
95 unsigned u4 __attribute__ ((mode (SI)));
96 unsigned u8 __attribute__ ((mode (DI)));
97 signed s2 __attribute__ ((mode (HI)));
98 signed s4 __attribute__ ((mode (SI)));
99 signed s8 __attribute__ ((mode (DI)));
100 } __attribute__ ((packed));
102 static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
103 static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
104 _Unwind_FrameState *);
107 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
110 read_1u (const void *p) { return *(const unsigned char *) p; }
113 read_1s (const void *p) { return *(const signed char *) p; }
116 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
119 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
121 static inline unsigned int
122 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
125 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
127 static inline unsigned long
128 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
130 static inline unsigned long
131 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
133 static inline _Unwind_Word
134 _Unwind_IsSignalFrame (struct _Unwind_Context *context)
136 return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
140 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
143 context->flags |= SIGNAL_FRAME_BIT;
145 context->flags &= ~SIGNAL_FRAME_BIT;
148 static inline _Unwind_Word
149 _Unwind_IsExtendedContext (struct _Unwind_Context *context)
151 return context->flags & EXTENDED_CONTEXT_BIT;
154 /* Get the value of register INDEX as saved in CONTEXT. */
157 _Unwind_GetGR (struct _Unwind_Context *context, int index)
162 #ifdef DWARF_ZERO_REG
163 if (index == DWARF_ZERO_REG)
167 index = DWARF_REG_TO_UNWIND_COLUMN (index);
168 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
169 size = dwarf_reg_size_table[index];
170 ptr = context->reg[index];
172 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
173 return (_Unwind_Word) (_Unwind_Internal_Ptr) ptr;
175 /* This will segfault if the register hasn't been saved. */
176 if (size == sizeof(_Unwind_Ptr))
177 return * (_Unwind_Ptr *) ptr;
180 gcc_assert (size == sizeof(_Unwind_Word));
181 return * (_Unwind_Word *) ptr;
186 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
188 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
191 /* Get the value of the CFA as saved in CONTEXT. */
194 _Unwind_GetCFA (struct _Unwind_Context *context)
196 return (_Unwind_Ptr) context->cfa;
199 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
202 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
207 index = DWARF_REG_TO_UNWIND_COLUMN (index);
208 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
209 size = dwarf_reg_size_table[index];
211 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
213 context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
217 ptr = context->reg[index];
219 if (size == sizeof(_Unwind_Ptr))
220 * (_Unwind_Ptr *) ptr = val;
223 gcc_assert (size == sizeof(_Unwind_Word));
224 * (_Unwind_Word *) ptr = val;
228 /* Get the pointer to a register INDEX as saved in CONTEXT. */
231 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
233 index = DWARF_REG_TO_UNWIND_COLUMN (index);
234 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
235 return &context->reg[index];
236 return context->reg[index];
239 /* Set the pointer to a register INDEX as saved in CONTEXT. */
242 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
244 index = DWARF_REG_TO_UNWIND_COLUMN (index);
245 if (_Unwind_IsExtendedContext (context))
246 context->by_value[index] = 0;
247 context->reg[index] = p;
250 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
253 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
256 index = DWARF_REG_TO_UNWIND_COLUMN (index);
257 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
258 gcc_assert (dwarf_reg_size_table[index] == sizeof (_Unwind_Ptr));
260 context->by_value[index] = 1;
261 context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
264 /* Return nonzero if register INDEX is stored by value rather than
268 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
270 index = DWARF_REG_TO_UNWIND_COLUMN (index);
271 return context->by_value[index];
274 /* Retrieve the return address for CONTEXT. */
277 _Unwind_GetIP (struct _Unwind_Context *context)
279 return (_Unwind_Ptr) context->ra;
282 /* Retrieve the return address and flag whether that IP is before
283 or after first not yet fully executed instruction. */
286 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
288 *ip_before_insn = _Unwind_IsSignalFrame (context);
289 return (_Unwind_Ptr) context->ra;
292 /* Overwrite the return address for CONTEXT with VAL. */
295 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
297 context->ra = (void *) val;
301 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
303 return context->lsda;
307 _Unwind_GetRegionStart (struct _Unwind_Context *context)
309 return (_Unwind_Ptr) context->bases.func;
313 _Unwind_FindEnclosingFunction (void *pc)
315 struct dwarf_eh_bases bases;
316 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
325 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
327 return (_Unwind_Ptr) context->bases.dbase;
331 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
333 return (_Unwind_Ptr) context->bases.tbase;
337 #ifdef MD_UNWIND_SUPPORT
338 #include MD_UNWIND_SUPPORT
341 /* Extract any interesting information from the CIE for the translation
342 unit F belongs to. Return a pointer to the byte after the augmentation,
343 or NULL if we encountered an undecipherable augmentation. */
345 static const unsigned char *
346 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
347 _Unwind_FrameState *fs)
349 const unsigned char *aug = cie->augmentation;
350 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
351 const unsigned char *ret = NULL;
355 /* g++ v2 "eh" has pointer immediately following augmentation string,
356 so it must be handled first. */
357 if (aug[0] == 'e' && aug[1] == 'h')
359 fs->eh_ptr = read_pointer (p);
360 p += sizeof (void *);
364 /* Immediately following the augmentation are the code and
365 data alignment and return address column. */
366 p = read_uleb128 (p, &utmp);
367 fs->code_align = (_Unwind_Word)utmp;
368 p = read_sleb128 (p, &stmp);
369 fs->data_align = (_Unwind_Sword)stmp;
370 if (cie->version == 1)
371 fs->retaddr_column = *p++;
374 p = read_uleb128 (p, &utmp);
375 fs->retaddr_column = (_Unwind_Word)utmp;
377 fs->lsda_encoding = DW_EH_PE_omit;
379 /* If the augmentation starts with 'z', then a uleb128 immediately
380 follows containing the length of the augmentation field following
384 p = read_uleb128 (p, &utmp);
391 /* Iterate over recognized augmentation subsequences. */
394 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
397 fs->lsda_encoding = *p++;
401 /* "R" indicates a byte indicating how FDE addresses are encoded. */
402 else if (aug[0] == 'R')
404 fs->fde_encoding = *p++;
408 /* "P" indicates a personality routine in the CIE augmentation. */
409 else if (aug[0] == 'P')
411 _Unwind_Ptr personality;
413 p = read_encoded_value (context, *p, p + 1, &personality);
414 fs->personality = (_Unwind_Personality_Fn) personality;
418 /* "S" indicates a signal frame. */
419 else if (aug[0] == 'S')
421 fs->signal_frame = 1;
425 /* Otherwise we have an unknown augmentation string.
426 Bail unless we saw a 'z' prefix. */
431 return ret ? ret : p;
435 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
436 onto the stack to start. */
439 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
440 struct _Unwind_Context *context, _Unwind_Word initial)
442 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
448 while (op_ptr < op_end)
450 enum dwarf_location_atom op = *op_ptr++;
452 _uleb128_t reg, utmp;
453 _sleb128_t offset, stmp;
489 result = op - DW_OP_lit0;
493 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
494 op_ptr += sizeof (void *);
498 result = read_1u (op_ptr);
502 result = read_1s (op_ptr);
506 result = read_2u (op_ptr);
510 result = read_2s (op_ptr);
514 result = read_4u (op_ptr);
518 result = read_4s (op_ptr);
522 result = read_8u (op_ptr);
526 result = read_8s (op_ptr);
530 op_ptr = read_uleb128 (op_ptr, &utmp);
531 result = (_Unwind_Word)utmp;
534 op_ptr = read_sleb128 (op_ptr, &stmp);
535 result = (_Unwind_Sword)stmp;
570 result = _Unwind_GetGR (context, op - DW_OP_reg0);
573 op_ptr = read_uleb128 (op_ptr, ®);
574 result = _Unwind_GetGR (context, reg);
609 op_ptr = read_sleb128 (op_ptr, &offset);
610 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
613 op_ptr = read_uleb128 (op_ptr, ®);
614 op_ptr = read_sleb128 (op_ptr, &offset);
615 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
619 gcc_assert (stack_elt);
620 result = stack[stack_elt - 1];
624 gcc_assert (stack_elt);
630 gcc_assert (offset < stack_elt - 1);
631 result = stack[stack_elt - 1 - offset];
635 gcc_assert (stack_elt >= 2);
636 result = stack[stack_elt - 2];
641 _Unwind_Word t1, t2, t3;
643 gcc_assert (stack_elt >= 3);
644 t1 = stack[stack_elt - 1];
645 t2 = stack[stack_elt - 2];
646 t3 = stack[stack_elt - 3];
647 stack[stack_elt - 1] = t2;
648 stack[stack_elt - 2] = t3;
649 stack[stack_elt - 3] = t1;
654 case DW_OP_deref_size:
658 case DW_OP_plus_uconst:
659 /* Unary operations. */
660 gcc_assert (stack_elt);
663 result = stack[stack_elt];
669 void *ptr = (void *) (_Unwind_Ptr) result;
670 result = (_Unwind_Ptr) read_pointer (ptr);
674 case DW_OP_deref_size:
676 void *ptr = (void *) (_Unwind_Ptr) result;
680 result = read_1u (ptr);
683 result = read_2u (ptr);
686 result = read_4u (ptr);
689 result = read_8u (ptr);
698 if ((_Unwind_Sword) result < 0)
707 case DW_OP_plus_uconst:
708 op_ptr = read_uleb128 (op_ptr, &utmp);
709 result += (_Unwind_Word)utmp;
735 /* Binary operations. */
736 _Unwind_Word first, second;
737 gcc_assert (stack_elt >= 2);
740 second = stack[stack_elt];
741 first = stack[stack_elt + 1];
746 result = second & first;
749 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
752 result = second - first;
755 result = (_Unwind_Sword) second % (_Unwind_Sword) first;
758 result = second * first;
761 result = second | first;
764 result = second + first;
767 result = second << first;
770 result = second >> first;
773 result = (_Unwind_Sword) second >> first;
776 result = second ^ first;
779 result = (_Unwind_Sword) first <= (_Unwind_Sword) second;
782 result = (_Unwind_Sword) first >= (_Unwind_Sword) second;
785 result = (_Unwind_Sword) first == (_Unwind_Sword) second;
788 result = (_Unwind_Sword) first < (_Unwind_Sword) second;
791 result = (_Unwind_Sword) first > (_Unwind_Sword) second;
794 result = (_Unwind_Sword) first != (_Unwind_Sword) second;
804 offset = read_2s (op_ptr);
810 gcc_assert (stack_elt);
813 offset = read_2s (op_ptr);
815 if (stack[stack_elt] != 0)
826 /* Most things push a result value. */
827 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
828 stack[stack_elt++] = result;
832 /* We were executing this program to get a value. It should be
834 gcc_assert (stack_elt);
836 return stack[stack_elt];
840 /* Decode DWARF 2 call frame information. Takes pointers the
841 instruction sequence to decode, current register information and
842 CIE info, and the PC range to evaluate. */
845 execute_cfa_program (const unsigned char *insn_ptr,
846 const unsigned char *insn_end,
847 struct _Unwind_Context *context,
848 _Unwind_FrameState *fs)
850 struct frame_state_reg_info *unused_rs = NULL;
852 /* Don't allow remember/restore between CIE and FDE programs. */
853 fs->regs.prev = NULL;
855 /* The comparison with the return address uses < rather than <= because
856 we are only interested in the effects of code before the call; for a
857 noreturn function, the return address may point to unrelated code with
858 a different stack configuration that we are not interested in. We
859 assume that the call itself is unwind info-neutral; if not, or if
860 there are delay instructions that adjust the stack, these must be
861 reflected at the point immediately before the call insn.
862 In signal frames, return address is after last completed instruction,
863 so we add 1 to return address to make the comparison <=. */
864 while (insn_ptr < insn_end
865 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
867 unsigned char insn = *insn_ptr++;
868 _uleb128_t reg, utmp;
869 _sleb128_t offset, stmp;
871 if ((insn & 0xc0) == DW_CFA_advance_loc)
872 fs->pc += (insn & 0x3f) * fs->code_align;
873 else if ((insn & 0xc0) == DW_CFA_offset)
876 insn_ptr = read_uleb128 (insn_ptr, &utmp);
877 offset = (_Unwind_Sword) utmp * fs->data_align;
878 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
880 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
882 else if ((insn & 0xc0) == DW_CFA_restore)
885 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
893 insn_ptr = read_encoded_value (context, fs->fde_encoding,
895 fs->pc = (void *) pc;
899 case DW_CFA_advance_loc1:
900 fs->pc += read_1u (insn_ptr) * fs->code_align;
903 case DW_CFA_advance_loc2:
904 fs->pc += read_2u (insn_ptr) * fs->code_align;
907 case DW_CFA_advance_loc4:
908 fs->pc += read_4u (insn_ptr) * fs->code_align;
912 case DW_CFA_offset_extended:
913 insn_ptr = read_uleb128 (insn_ptr, ®);
914 insn_ptr = read_uleb128 (insn_ptr, &utmp);
915 offset = (_Unwind_Sword) utmp * fs->data_align;
916 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
918 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
921 case DW_CFA_restore_extended:
922 insn_ptr = read_uleb128 (insn_ptr, ®);
923 /* FIXME, this is wrong; the CIE might have said that the
924 register was saved somewhere. */
925 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
928 case DW_CFA_undefined:
929 case DW_CFA_same_value:
930 insn_ptr = read_uleb128 (insn_ptr, ®);
931 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
937 case DW_CFA_register:
940 insn_ptr = read_uleb128 (insn_ptr, ®);
941 insn_ptr = read_uleb128 (insn_ptr, ®2);
942 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
943 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg =
948 case DW_CFA_remember_state:
950 struct frame_state_reg_info *new_rs;
954 unused_rs = unused_rs->prev;
957 new_rs = alloca (sizeof (struct frame_state_reg_info));
960 fs->regs.prev = new_rs;
964 case DW_CFA_restore_state:
966 struct frame_state_reg_info *old_rs = fs->regs.prev;
968 old_rs->prev = unused_rs;
974 insn_ptr = read_uleb128 (insn_ptr, &utmp);
975 fs->regs.cfa_reg = (_Unwind_Word)utmp;
976 insn_ptr = read_uleb128 (insn_ptr, &utmp);
977 fs->regs.cfa_offset = (_Unwind_Word)utmp;
978 fs->regs.cfa_how = CFA_REG_OFFSET;
981 case DW_CFA_def_cfa_register:
982 insn_ptr = read_uleb128 (insn_ptr, &utmp);
983 fs->regs.cfa_reg = (_Unwind_Word)utmp;
984 fs->regs.cfa_how = CFA_REG_OFFSET;
987 case DW_CFA_def_cfa_offset:
988 insn_ptr = read_uleb128 (insn_ptr, &utmp);
989 fs->regs.cfa_offset = utmp;
990 /* cfa_how deliberately not set. */
993 case DW_CFA_def_cfa_expression:
994 fs->regs.cfa_exp = insn_ptr;
995 fs->regs.cfa_how = CFA_EXP;
996 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1000 case DW_CFA_expression:
1001 insn_ptr = read_uleb128 (insn_ptr, ®);
1002 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
1003 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1004 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1009 case DW_CFA_offset_extended_sf:
1010 insn_ptr = read_uleb128 (insn_ptr, ®);
1011 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1012 offset = stmp * fs->data_align;
1013 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1015 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1018 case DW_CFA_def_cfa_sf:
1019 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1020 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1021 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1022 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1023 fs->regs.cfa_how = CFA_REG_OFFSET;
1024 fs->regs.cfa_offset *= fs->data_align;
1027 case DW_CFA_def_cfa_offset_sf:
1028 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1029 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1030 fs->regs.cfa_offset *= fs->data_align;
1031 /* cfa_how deliberately not set. */
1034 case DW_CFA_val_offset:
1035 insn_ptr = read_uleb128 (insn_ptr, ®);
1036 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1037 offset = (_Unwind_Sword) utmp * fs->data_align;
1038 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1039 = REG_SAVED_VAL_OFFSET;
1040 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1043 case DW_CFA_val_offset_sf:
1044 insn_ptr = read_uleb128 (insn_ptr, ®);
1045 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1046 offset = stmp * fs->data_align;
1047 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1048 = REG_SAVED_VAL_OFFSET;
1049 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1052 case DW_CFA_val_expression:
1053 insn_ptr = read_uleb128 (insn_ptr, ®);
1054 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1055 = REG_SAVED_VAL_EXP;
1056 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1057 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1061 case DW_CFA_GNU_window_save:
1062 /* ??? Hardcoded for SPARC register window configuration. */
1063 for (reg = 16; reg < 32; ++reg)
1065 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1066 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1070 case DW_CFA_GNU_args_size:
1071 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1072 context->args_size = (_Unwind_Word)utmp;
1075 case DW_CFA_GNU_negative_offset_extended:
1076 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1077 older PowerPC code. */
1078 insn_ptr = read_uleb128 (insn_ptr, ®);
1079 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1080 offset = (_Unwind_Word) utmp * fs->data_align;
1081 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1083 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
1092 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1093 its caller and decode it into FS. This function also sets the
1094 args_size and lsda members of CONTEXT, as they are really information
1095 about the caller's frame. */
1097 static _Unwind_Reason_Code
1098 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1100 const struct dwarf_fde *fde;
1101 const struct dwarf_cie *cie;
1102 const unsigned char *aug, *insn, *end;
1104 memset (fs, 0, sizeof (*fs));
1105 context->args_size = 0;
1108 if (context->ra == 0)
1109 return _URC_END_OF_STACK;
1111 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1115 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1116 /* Couldn't find frame unwind info for this function. Try a
1117 target-specific fallback mechanism. This will necessarily
1118 not provide a personality routine or LSDA. */
1119 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1121 return _URC_END_OF_STACK;
1125 fs->pc = context->bases.func;
1127 cie = get_cie (fde);
1128 insn = extract_cie_info (cie, context, fs);
1130 /* CIE contained unknown augmentation. */
1131 return _URC_FATAL_PHASE1_ERROR;
1133 /* First decode all the insns in the CIE. */
1134 end = (unsigned char *) next_fde ((struct dwarf_fde *) cie);
1135 execute_cfa_program (insn, end, context, fs);
1137 /* Locate augmentation for the fde. */
1138 aug = (unsigned char *) fde + sizeof (*fde);
1139 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1144 aug = read_uleb128 (aug, &i);
1147 if (fs->lsda_encoding != DW_EH_PE_omit)
1151 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1152 context->lsda = (void *) lsda;
1155 /* Then the insns in the FDE up to our target PC. */
1158 end = (unsigned char *) next_fde (fde);
1159 execute_cfa_program (insn, end, context, fs);
1161 return _URC_NO_REASON;
1164 typedef struct frame_state
1170 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1171 unsigned short cfa_reg;
1172 unsigned short retaddr_column;
1173 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1176 struct frame_state * __frame_state_for (void *, struct frame_state *);
1178 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1179 a given PC_TARGET. The caller should allocate a local variable of
1180 `struct frame_state' and pass its address to STATE_IN. */
1182 struct frame_state *
1183 __frame_state_for (void *pc_target, struct frame_state *state_in)
1185 struct _Unwind_Context context;
1186 _Unwind_FrameState fs;
1189 memset (&context, 0, sizeof (struct _Unwind_Context));
1190 context.flags = EXTENDED_CONTEXT_BIT;
1191 context.ra = pc_target + 1;
1193 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1196 /* We have no way to pass a location expression for the CFA to our
1197 caller. It wouldn't understand it anyway. */
1198 if (fs.regs.cfa_how == CFA_EXP)
1201 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1203 state_in->saved[reg] = fs.regs.reg[reg].how;
1204 switch (state_in->saved[reg])
1207 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1209 case REG_SAVED_OFFSET:
1210 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1213 state_in->reg_or_offset[reg] = 0;
1218 state_in->cfa_offset = fs.regs.cfa_offset;
1219 state_in->cfa_reg = fs.regs.cfa_reg;
1220 state_in->retaddr_column = fs.retaddr_column;
1221 state_in->args_size = context.args_size;
1222 state_in->eh_ptr = fs.eh_ptr;
1227 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1230 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1231 _Unwind_SpTmp *tmp_sp)
1233 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1235 if (size == sizeof(_Unwind_Ptr))
1236 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1239 gcc_assert (size == sizeof(_Unwind_Word));
1240 tmp_sp->word = (_Unwind_Ptr) cfa;
1242 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1246 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1248 struct _Unwind_Context orig_context = *context;
1252 #ifdef EH_RETURN_STACKADJ_RTX
1253 /* Special handling here: Many machines do not use a frame pointer,
1254 and track the CFA only through offsets from the stack pointer from
1255 one frame to the next. In this case, the stack pointer is never
1256 stored, so it has no saved address in the context. What we do
1257 have is the CFA from the previous stack frame.
1259 In very special situations (such as unwind info for signal return),
1260 there may be location expressions that use the stack pointer as well.
1262 Do this conditionally for one frame. This allows the unwind info
1263 for one frame to save a copy of the stack pointer from the previous
1264 frame, and be able to use much easier CFA mechanisms to do it.
1265 Always zap the saved stack pointer value for the next frame; carrying
1266 the value over from one frame to another doesn't make sense. */
1268 _Unwind_SpTmp tmp_sp;
1270 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1271 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1272 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1275 /* Compute this frame's CFA. */
1276 switch (fs->regs.cfa_how)
1278 case CFA_REG_OFFSET:
1279 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1280 cfa += fs->regs.cfa_offset;
1285 const unsigned char *exp = fs->regs.cfa_exp;
1288 exp = read_uleb128 (exp, &len);
1289 cfa = (void *) (_Unwind_Ptr)
1290 execute_stack_op (exp, exp + len, &orig_context, 0);
1299 /* Compute the addresses of all registers saved in this frame. */
1300 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1301 switch (fs->regs.reg[i].how)
1306 case REG_SAVED_OFFSET:
1307 _Unwind_SetGRPtr (context, i,
1308 (void *) (cfa + fs->regs.reg[i].loc.offset));
1312 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1313 _Unwind_SetGRValue (context, i,
1314 _Unwind_GetGR (&orig_context,
1315 fs->regs.reg[i].loc.reg));
1317 _Unwind_SetGRPtr (context, i,
1318 _Unwind_GetGRPtr (&orig_context,
1319 fs->regs.reg[i].loc.reg));
1324 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1328 exp = read_uleb128 (exp, &len);
1329 val = execute_stack_op (exp, exp + len, &orig_context,
1331 _Unwind_SetGRPtr (context, i, (void *) val);
1335 case REG_SAVED_VAL_OFFSET:
1336 _Unwind_SetGRValue (context, i,
1337 (_Unwind_Internal_Ptr)
1338 (cfa + fs->regs.reg[i].loc.offset));
1341 case REG_SAVED_VAL_EXP:
1343 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1347 exp = read_uleb128 (exp, &len);
1348 val = execute_stack_op (exp, exp + len, &orig_context,
1350 _Unwind_SetGRValue (context, i, val);
1355 _Unwind_SetSignalFrame (context, fs->signal_frame);
1357 #ifdef MD_FROB_UPDATE_CONTEXT
1358 MD_FROB_UPDATE_CONTEXT (context, fs);
1362 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1363 of its caller. Update CONTEXT to refer to the caller as well. Note
1364 that the args_size and lsda members are not updated here, but later in
1365 uw_frame_state_for. */
1368 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1370 uw_update_context_1 (context, fs);
1372 /* Compute the return address now, since the return address column
1373 can change from frame to frame. */
1374 context->ra = __builtin_extract_return_addr
1375 (_Unwind_GetPtr (context, fs->retaddr_column));
1379 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1381 uw_update_context (context, fs);
1384 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1385 level will be the return address and the CFA. */
1387 #define uw_init_context(CONTEXT) \
1390 /* Do any necessary initialization to access arbitrary stack frames. \
1391 On the SPARC, this means flushing the register windows. */ \
1392 __builtin_unwind_init (); \
1393 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1394 __builtin_return_address (0)); \
1399 init_dwarf_reg_size_table (void)
1401 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1405 uw_init_context_1 (struct _Unwind_Context *context,
1406 void *outer_cfa, void *outer_ra)
1408 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1409 _Unwind_FrameState fs;
1410 _Unwind_SpTmp sp_slot;
1411 _Unwind_Reason_Code code;
1413 memset (context, 0, sizeof (struct _Unwind_Context));
1415 context->flags = EXTENDED_CONTEXT_BIT;
1417 code = uw_frame_state_for (context, &fs);
1418 gcc_assert (code == _URC_NO_REASON);
1422 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1423 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1424 && dwarf_reg_size_table[0] == 0)
1425 init_dwarf_reg_size_table ();
1428 if (dwarf_reg_size_table[0] == 0)
1429 init_dwarf_reg_size_table ();
1432 /* Force the frame state to use the known cfa value. */
1433 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1434 fs.regs.cfa_how = CFA_REG_OFFSET;
1435 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1436 fs.regs.cfa_offset = 0;
1438 uw_update_context_1 (context, &fs);
1440 /* If the return address column was saved in a register in the
1441 initialization context, then we can't see it in the given
1442 call frame data. So have the initialization context tell us. */
1443 context->ra = __builtin_extract_return_addr (outer_ra);
1447 /* Install TARGET into CURRENT so that we can return to it. This is a
1448 macro because __builtin_eh_return must be invoked in the context of
1451 #define uw_install_context(CURRENT, TARGET) \
1454 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1455 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1456 __builtin_eh_return (offset, handler); \
1461 uw_install_context_1 (struct _Unwind_Context *current,
1462 struct _Unwind_Context *target)
1465 _Unwind_SpTmp sp_slot;
1467 /* If the target frame does not have a saved stack pointer,
1468 then set up the target's CFA. */
1469 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1470 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1472 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1474 void *c = current->reg[i];
1475 void *t = target->reg[i];
1477 gcc_assert (current->by_value[i] == 0);
1478 if (target->by_value[i] && c)
1482 if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
1484 w = (_Unwind_Internal_Ptr) t;
1485 memcpy (c, &w, sizeof (_Unwind_Word));
1489 gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
1490 p = (_Unwind_Internal_Ptr) t;
1491 memcpy (c, &p, sizeof (_Unwind_Ptr));
1494 else if (t && c && t != c)
1495 memcpy (c, t, dwarf_reg_size_table[i]);
1498 /* If the current frame doesn't have a saved stack pointer, then we
1499 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1500 pointer value reloaded. */
1501 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1505 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1507 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1508 if (STACK_GROWS_DOWNWARD)
1509 return target_cfa - current->cfa + target->args_size;
1511 return current->cfa - target_cfa - target->args_size;
1516 static inline _Unwind_Ptr
1517 uw_identify_context (struct _Unwind_Context *context)
1519 return _Unwind_GetIP (context);
1523 #include "unwind.inc"
1525 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1526 alias (_Unwind_Backtrace);
1527 alias (_Unwind_DeleteException);
1528 alias (_Unwind_FindEnclosingFunction);
1529 alias (_Unwind_ForcedUnwind);
1530 alias (_Unwind_GetDataRelBase);
1531 alias (_Unwind_GetTextRelBase);
1532 alias (_Unwind_GetCFA);
1533 alias (_Unwind_GetGR);
1534 alias (_Unwind_GetIP);
1535 alias (_Unwind_GetLanguageSpecificData);
1536 alias (_Unwind_GetRegionStart);
1537 alias (_Unwind_RaiseException);
1538 alias (_Unwind_Resume);
1539 alias (_Unwind_Resume_or_Rethrow);
1540 alias (_Unwind_SetGR);
1541 alias (_Unwind_SetIP);
1544 #endif /* !USING_SJLJ_EXCEPTIONS */