1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2008, 2009, 2010 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 3, 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 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
28 #include "coretypes.h"
32 #ifdef __USING_SJLJ_EXCEPTIONS__
33 # define NO_SIZE_OF_ENCODED_VALUE
35 #include "unwind-pe.h"
36 #include "unwind-dw2-fde.h"
38 #include "unwind-dw2.h"
40 #ifndef __USING_SJLJ_EXCEPTIONS__
42 #ifndef STACK_GROWS_DOWNWARD
43 #define STACK_GROWS_DOWNWARD 0
45 #undef STACK_GROWS_DOWNWARD
46 #define STACK_GROWS_DOWNWARD 1
49 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
50 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
51 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
54 #ifndef DWARF_REG_TO_UNWIND_COLUMN
55 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
58 /* This is the register and unwind state for a particular frame. This
59 provides the information necessary to unwind up past a frame and return
61 struct _Unwind_Context
63 void *reg
[DWARF_FRAME_REGISTERS
+1];
67 struct dwarf_eh_bases bases
;
68 /* Signal frame context. */
69 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
70 /* Context which has version/args_size/by_value fields. */
71 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
73 /* 0 for now, can be increased when further fields are added to
74 struct _Unwind_Context. */
76 _Unwind_Word args_size
;
77 char by_value
[DWARF_FRAME_REGISTERS
+1];
80 /* Byte size of every register managed by these routines. */
81 static unsigned char dwarf_reg_size_table
[DWARF_FRAME_REGISTERS
+1];
84 /* Read unaligned data from the instruction buffer. */
89 unsigned u2
__attribute__ ((mode (HI
)));
90 unsigned u4
__attribute__ ((mode (SI
)));
91 unsigned u8
__attribute__ ((mode (DI
)));
92 signed s2
__attribute__ ((mode (HI
)));
93 signed s4
__attribute__ ((mode (SI
)));
94 signed s8
__attribute__ ((mode (DI
)));
95 } __attribute__ ((packed
));
97 static void uw_update_context (struct _Unwind_Context
*, _Unwind_FrameState
*);
98 static _Unwind_Reason_Code
uw_frame_state_for (struct _Unwind_Context
*,
99 _Unwind_FrameState
*);
102 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
105 read_1u (const void *p
) { return *(const unsigned char *) p
; }
108 read_1s (const void *p
) { return *(const signed char *) p
; }
111 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
114 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
116 static inline unsigned int
117 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
120 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
122 static inline unsigned long
123 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
125 static inline unsigned long
126 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
128 static inline _Unwind_Word
129 _Unwind_IsSignalFrame (struct _Unwind_Context
*context
)
131 return (context
->flags
& SIGNAL_FRAME_BIT
) ? 1 : 0;
135 _Unwind_SetSignalFrame (struct _Unwind_Context
*context
, int val
)
138 context
->flags
|= SIGNAL_FRAME_BIT
;
140 context
->flags
&= ~SIGNAL_FRAME_BIT
;
143 static inline _Unwind_Word
144 _Unwind_IsExtendedContext (struct _Unwind_Context
*context
)
146 return context
->flags
& EXTENDED_CONTEXT_BIT
;
149 /* Get the value of register INDEX as saved in CONTEXT. */
152 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
157 #ifdef DWARF_ZERO_REG
158 if (index
== DWARF_ZERO_REG
)
162 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
163 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
164 size
= dwarf_reg_size_table
[index
];
165 ptr
= context
->reg
[index
];
167 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
168 return (_Unwind_Word
) (_Unwind_Internal_Ptr
) ptr
;
170 /* This will segfault if the register hasn't been saved. */
171 if (size
== sizeof(_Unwind_Ptr
))
172 return * (_Unwind_Ptr
*) ptr
;
175 gcc_assert (size
== sizeof(_Unwind_Word
));
176 return * (_Unwind_Word
*) ptr
;
181 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
183 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
186 /* Get the value of the CFA as saved in CONTEXT. */
189 _Unwind_GetCFA (struct _Unwind_Context
*context
)
191 return (_Unwind_Ptr
) context
->cfa
;
194 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
197 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
202 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
203 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
204 size
= dwarf_reg_size_table
[index
];
206 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
208 context
->reg
[index
] = (void *) (_Unwind_Internal_Ptr
) val
;
212 ptr
= context
->reg
[index
];
214 if (size
== sizeof(_Unwind_Ptr
))
215 * (_Unwind_Ptr
*) ptr
= val
;
218 gcc_assert (size
== sizeof(_Unwind_Word
));
219 * (_Unwind_Word
*) ptr
= val
;
223 /* Get the pointer to a register INDEX as saved in CONTEXT. */
226 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
228 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
229 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
230 return &context
->reg
[index
];
231 return context
->reg
[index
];
234 /* Set the pointer to a register INDEX as saved in CONTEXT. */
237 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
239 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
240 if (_Unwind_IsExtendedContext (context
))
241 context
->by_value
[index
] = 0;
242 context
->reg
[index
] = p
;
245 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
248 _Unwind_SetGRValue (struct _Unwind_Context
*context
, int index
,
251 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
252 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
253 gcc_assert (dwarf_reg_size_table
[index
] == sizeof (_Unwind_Ptr
));
255 context
->by_value
[index
] = 1;
256 context
->reg
[index
] = (void *) (_Unwind_Internal_Ptr
) val
;
259 /* Return nonzero if register INDEX is stored by value rather than
263 _Unwind_GRByValue (struct _Unwind_Context
*context
, int index
)
265 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
266 return context
->by_value
[index
];
269 /* Retrieve the return address for CONTEXT. */
272 _Unwind_GetIP (struct _Unwind_Context
*context
)
274 return (_Unwind_Ptr
) context
->ra
;
277 /* Retrieve the return address and flag whether that IP is before
278 or after first not yet fully executed instruction. */
281 _Unwind_GetIPInfo (struct _Unwind_Context
*context
, int *ip_before_insn
)
283 *ip_before_insn
= _Unwind_IsSignalFrame (context
);
284 return (_Unwind_Ptr
) context
->ra
;
287 /* Overwrite the return address for CONTEXT with VAL. */
290 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
292 context
->ra
= (void *) val
;
296 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
298 return context
->lsda
;
302 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
304 return (_Unwind_Ptr
) context
->bases
.func
;
308 _Unwind_FindEnclosingFunction (void *pc
)
310 struct dwarf_eh_bases bases
;
311 const struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
320 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
322 return (_Unwind_Ptr
) context
->bases
.dbase
;
326 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
328 return (_Unwind_Ptr
) context
->bases
.tbase
;
332 #ifdef MD_UNWIND_SUPPORT
333 #include MD_UNWIND_SUPPORT
336 /* Extract any interesting information from the CIE for the translation
337 unit F belongs to. Return a pointer to the byte after the augmentation,
338 or NULL if we encountered an undecipherable augmentation. */
340 static const unsigned char *
341 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
342 _Unwind_FrameState
*fs
)
344 const unsigned char *aug
= cie
->augmentation
;
345 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
346 const unsigned char *ret
= NULL
;
350 /* g++ v2 "eh" has pointer immediately following augmentation string,
351 so it must be handled first. */
352 if (aug
[0] == 'e' && aug
[1] == 'h')
354 fs
->eh_ptr
= read_pointer (p
);
355 p
+= sizeof (void *);
359 /* After the augmentation resp. pointer for "eh" augmentation
360 follows for CIE version >= 4 address size byte and
361 segment size byte. */
362 if (__builtin_expect (cie
->version
>= 4, 0))
364 if (p
[0] != sizeof (void *) || p
[1] != 0)
368 /* Immediately following this are the code and
369 data alignment and return address column. */
370 p
= read_uleb128 (p
, &utmp
);
371 fs
->code_align
= (_Unwind_Word
)utmp
;
372 p
= read_sleb128 (p
, &stmp
);
373 fs
->data_align
= (_Unwind_Sword
)stmp
;
374 if (cie
->version
== 1)
375 fs
->retaddr_column
= *p
++;
378 p
= read_uleb128 (p
, &utmp
);
379 fs
->retaddr_column
= (_Unwind_Word
)utmp
;
381 fs
->lsda_encoding
= DW_EH_PE_omit
;
383 /* If the augmentation starts with 'z', then a uleb128 immediately
384 follows containing the length of the augmentation field following
388 p
= read_uleb128 (p
, &utmp
);
395 /* Iterate over recognized augmentation subsequences. */
398 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
401 fs
->lsda_encoding
= *p
++;
405 /* "R" indicates a byte indicating how FDE addresses are encoded. */
406 else if (aug
[0] == 'R')
408 fs
->fde_encoding
= *p
++;
412 /* "P" indicates a personality routine in the CIE augmentation. */
413 else if (aug
[0] == 'P')
415 _Unwind_Ptr personality
;
417 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
418 fs
->personality
= (_Unwind_Personality_Fn
) personality
;
422 /* "S" indicates a signal frame. */
423 else if (aug
[0] == 'S')
425 fs
->signal_frame
= 1;
429 /* Otherwise we have an unknown augmentation string.
430 Bail unless we saw a 'z' prefix. */
435 return ret
? ret
: p
;
439 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
440 onto the stack to start. */
443 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
444 struct _Unwind_Context
*context
, _Unwind_Word initial
)
446 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
452 while (op_ptr
< op_end
)
454 enum dwarf_location_atom op
= *op_ptr
++;
456 _uleb128_t reg
, utmp
;
457 _sleb128_t offset
, stmp
;
493 result
= op
- DW_OP_lit0
;
497 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
498 op_ptr
+= sizeof (void *);
501 case DW_OP_GNU_encoded_addr
:
504 op_ptr
= read_encoded_value (context
, *op_ptr
, op_ptr
+1, &presult
);
510 result
= read_1u (op_ptr
);
514 result
= read_1s (op_ptr
);
518 result
= read_2u (op_ptr
);
522 result
= read_2s (op_ptr
);
526 result
= read_4u (op_ptr
);
530 result
= read_4s (op_ptr
);
534 result
= read_8u (op_ptr
);
538 result
= read_8s (op_ptr
);
542 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
543 result
= (_Unwind_Word
)utmp
;
546 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
547 result
= (_Unwind_Sword
)stmp
;
582 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
585 op_ptr
= read_uleb128 (op_ptr
, ®
);
586 result
= _Unwind_GetGR (context
, reg
);
621 op_ptr
= read_sleb128 (op_ptr
, &offset
);
622 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
625 op_ptr
= read_uleb128 (op_ptr
, ®
);
626 op_ptr
= read_sleb128 (op_ptr
, &offset
);
627 result
= _Unwind_GetGR (context
, reg
) + (_Unwind_Word
)offset
;
631 gcc_assert (stack_elt
);
632 result
= stack
[stack_elt
- 1];
636 gcc_assert (stack_elt
);
642 gcc_assert (offset
< stack_elt
- 1);
643 result
= stack
[stack_elt
- 1 - offset
];
647 gcc_assert (stack_elt
>= 2);
648 result
= stack
[stack_elt
- 2];
654 gcc_assert (stack_elt
>= 2);
655 t
= stack
[stack_elt
- 1];
656 stack
[stack_elt
- 1] = stack
[stack_elt
- 2];
657 stack
[stack_elt
- 2] = t
;
663 _Unwind_Word t1
, t2
, t3
;
665 gcc_assert (stack_elt
>= 3);
666 t1
= stack
[stack_elt
- 1];
667 t2
= stack
[stack_elt
- 2];
668 t3
= stack
[stack_elt
- 3];
669 stack
[stack_elt
- 1] = t2
;
670 stack
[stack_elt
- 2] = t3
;
671 stack
[stack_elt
- 3] = t1
;
676 case DW_OP_deref_size
:
680 case DW_OP_plus_uconst
:
681 /* Unary operations. */
682 gcc_assert (stack_elt
);
685 result
= stack
[stack_elt
];
691 void *ptr
= (void *) (_Unwind_Ptr
) result
;
692 result
= (_Unwind_Ptr
) read_pointer (ptr
);
696 case DW_OP_deref_size
:
698 void *ptr
= (void *) (_Unwind_Ptr
) result
;
702 result
= read_1u (ptr
);
705 result
= read_2u (ptr
);
708 result
= read_4u (ptr
);
711 result
= read_8u (ptr
);
720 if ((_Unwind_Sword
) result
< 0)
729 case DW_OP_plus_uconst
:
730 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
731 result
+= (_Unwind_Word
)utmp
;
757 /* Binary operations. */
758 _Unwind_Word first
, second
;
759 gcc_assert (stack_elt
>= 2);
762 second
= stack
[stack_elt
];
763 first
= stack
[stack_elt
+ 1];
768 result
= second
& first
;
771 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
774 result
= second
- first
;
777 result
= second
% first
;
780 result
= second
* first
;
783 result
= second
| first
;
786 result
= second
+ first
;
789 result
= second
<< first
;
792 result
= second
>> first
;
795 result
= (_Unwind_Sword
) second
>> first
;
798 result
= second
^ first
;
801 result
= (_Unwind_Sword
) second
<= (_Unwind_Sword
) first
;
804 result
= (_Unwind_Sword
) second
>= (_Unwind_Sword
) first
;
807 result
= (_Unwind_Sword
) second
== (_Unwind_Sword
) first
;
810 result
= (_Unwind_Sword
) second
< (_Unwind_Sword
) first
;
813 result
= (_Unwind_Sword
) second
> (_Unwind_Sword
) first
;
816 result
= (_Unwind_Sword
) second
!= (_Unwind_Sword
) first
;
826 offset
= read_2s (op_ptr
);
832 gcc_assert (stack_elt
);
835 offset
= read_2s (op_ptr
);
837 if (stack
[stack_elt
] != 0)
848 /* Most things push a result value. */
849 gcc_assert ((size_t) stack_elt
< sizeof(stack
)/sizeof(*stack
));
850 stack
[stack_elt
++] = result
;
854 /* We were executing this program to get a value. It should be
856 gcc_assert (stack_elt
);
858 return stack
[stack_elt
];
862 /* Decode DWARF 2 call frame information. Takes pointers the
863 instruction sequence to decode, current register information and
864 CIE info, and the PC range to evaluate. */
867 execute_cfa_program (const unsigned char *insn_ptr
,
868 const unsigned char *insn_end
,
869 struct _Unwind_Context
*context
,
870 _Unwind_FrameState
*fs
)
872 struct frame_state_reg_info
*unused_rs
= NULL
;
874 /* Don't allow remember/restore between CIE and FDE programs. */
875 fs
->regs
.prev
= NULL
;
877 /* The comparison with the return address uses < rather than <= because
878 we are only interested in the effects of code before the call; for a
879 noreturn function, the return address may point to unrelated code with
880 a different stack configuration that we are not interested in. We
881 assume that the call itself is unwind info-neutral; if not, or if
882 there are delay instructions that adjust the stack, these must be
883 reflected at the point immediately before the call insn.
884 In signal frames, return address is after last completed instruction,
885 so we add 1 to return address to make the comparison <=. */
886 while (insn_ptr
< insn_end
887 && fs
->pc
< context
->ra
+ _Unwind_IsSignalFrame (context
))
889 unsigned char insn
= *insn_ptr
++;
890 _uleb128_t reg
, utmp
;
891 _sleb128_t offset
, stmp
;
893 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
894 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
895 else if ((insn
& 0xc0) == DW_CFA_offset
)
898 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
899 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
900 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
902 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
904 else if ((insn
& 0xc0) == DW_CFA_restore
)
907 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_UNSAVED
;
915 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
917 fs
->pc
= (void *) pc
;
921 case DW_CFA_advance_loc1
:
922 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
925 case DW_CFA_advance_loc2
:
926 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
929 case DW_CFA_advance_loc4
:
930 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
934 case DW_CFA_offset_extended
:
935 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
936 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
937 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
938 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
940 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
943 case DW_CFA_restore_extended
:
944 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
945 /* FIXME, this is wrong; the CIE might have said that the
946 register was saved somewhere. */
947 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
950 case DW_CFA_same_value
:
951 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
952 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
955 case DW_CFA_undefined
:
956 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
957 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNDEFINED
;
963 case DW_CFA_register
:
966 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
967 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
968 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_REG
;
969 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.reg
=
974 case DW_CFA_remember_state
:
976 struct frame_state_reg_info
*new_rs
;
980 unused_rs
= unused_rs
->prev
;
983 new_rs
= alloca (sizeof (struct frame_state_reg_info
));
986 fs
->regs
.prev
= new_rs
;
990 case DW_CFA_restore_state
:
992 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
994 old_rs
->prev
= unused_rs
;
1000 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1001 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1002 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1003 fs
->regs
.cfa_offset
= (_Unwind_Word
)utmp
;
1004 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1007 case DW_CFA_def_cfa_register
:
1008 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1009 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1010 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1013 case DW_CFA_def_cfa_offset
:
1014 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1015 fs
->regs
.cfa_offset
= utmp
;
1016 /* cfa_how deliberately not set. */
1019 case DW_CFA_def_cfa_expression
:
1020 fs
->regs
.cfa_exp
= insn_ptr
;
1021 fs
->regs
.cfa_how
= CFA_EXP
;
1022 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1026 case DW_CFA_expression
:
1027 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1028 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_EXP
;
1029 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
1030 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1035 case DW_CFA_offset_extended_sf
:
1036 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1037 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1038 offset
= stmp
* fs
->data_align
;
1039 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1041 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1044 case DW_CFA_def_cfa_sf
:
1045 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1046 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1047 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1048 fs
->regs
.cfa_offset
= (_Unwind_Sword
)stmp
;
1049 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1050 fs
->regs
.cfa_offset
*= fs
->data_align
;
1053 case DW_CFA_def_cfa_offset_sf
:
1054 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1055 fs
->regs
.cfa_offset
= (_Unwind_Sword
)stmp
;
1056 fs
->regs
.cfa_offset
*= fs
->data_align
;
1057 /* cfa_how deliberately not set. */
1060 case DW_CFA_val_offset
:
1061 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1062 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1063 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
1064 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1065 = REG_SAVED_VAL_OFFSET
;
1066 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1069 case DW_CFA_val_offset_sf
:
1070 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1071 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1072 offset
= stmp
* fs
->data_align
;
1073 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1074 = REG_SAVED_VAL_OFFSET
;
1075 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1078 case DW_CFA_val_expression
:
1079 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1080 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1081 = REG_SAVED_VAL_EXP
;
1082 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
1083 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1087 case DW_CFA_GNU_window_save
:
1088 /* ??? Hardcoded for SPARC register window configuration. */
1089 for (reg
= 16; reg
< 32; ++reg
)
1091 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
1092 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
1096 case DW_CFA_GNU_args_size
:
1097 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1098 context
->args_size
= (_Unwind_Word
)utmp
;
1101 case DW_CFA_GNU_negative_offset_extended
:
1102 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1103 older PowerPC code. */
1104 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1105 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1106 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
1107 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1109 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= -offset
;
1118 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1119 its caller and decode it into FS. This function also sets the
1120 args_size and lsda members of CONTEXT, as they are really information
1121 about the caller's frame. */
1123 static _Unwind_Reason_Code
1124 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1126 const struct dwarf_fde
*fde
;
1127 const struct dwarf_cie
*cie
;
1128 const unsigned char *aug
, *insn
, *end
;
1130 memset (fs
, 0, sizeof (*fs
));
1131 context
->args_size
= 0;
1134 if (context
->ra
== 0)
1135 return _URC_END_OF_STACK
;
1137 fde
= _Unwind_Find_FDE (context
->ra
+ _Unwind_IsSignalFrame (context
) - 1,
1141 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1142 /* Couldn't find frame unwind info for this function. Try a
1143 target-specific fallback mechanism. This will necessarily
1144 not provide a personality routine or LSDA. */
1145 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
1147 return _URC_END_OF_STACK
;
1151 fs
->pc
= context
->bases
.func
;
1153 cie
= get_cie (fde
);
1154 insn
= extract_cie_info (cie
, context
, fs
);
1156 /* CIE contained unknown augmentation. */
1157 return _URC_FATAL_PHASE1_ERROR
;
1159 /* First decode all the insns in the CIE. */
1160 end
= (const unsigned char *) next_fde ((const struct dwarf_fde
*) cie
);
1161 execute_cfa_program (insn
, end
, context
, fs
);
1163 /* Locate augmentation for the fde. */
1164 aug
= (const unsigned char *) fde
+ sizeof (*fde
);
1165 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1170 aug
= read_uleb128 (aug
, &i
);
1173 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1177 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1178 context
->lsda
= (void *) lsda
;
1181 /* Then the insns in the FDE up to our target PC. */
1184 end
= (const unsigned char *) next_fde (fde
);
1185 execute_cfa_program (insn
, end
, context
, fs
);
1187 return _URC_NO_REASON
;
1190 typedef struct frame_state
1196 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1197 unsigned short cfa_reg
;
1198 unsigned short retaddr_column
;
1199 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1202 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1204 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1205 a given PC_TARGET. The caller should allocate a local variable of
1206 `struct frame_state' and pass its address to STATE_IN. */
1208 struct frame_state
*
1209 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1211 struct _Unwind_Context context
;
1212 _Unwind_FrameState fs
;
1215 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1216 context
.flags
= EXTENDED_CONTEXT_BIT
;
1217 context
.ra
= pc_target
+ 1;
1219 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1222 /* We have no way to pass a location expression for the CFA to our
1223 caller. It wouldn't understand it anyway. */
1224 if (fs
.regs
.cfa_how
== CFA_EXP
)
1227 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1229 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1230 switch (state_in
->saved
[reg
])
1233 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1235 case REG_SAVED_OFFSET
:
1236 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1239 state_in
->reg_or_offset
[reg
] = 0;
1244 state_in
->cfa_offset
= fs
.regs
.cfa_offset
;
1245 state_in
->cfa_reg
= fs
.regs
.cfa_reg
;
1246 state_in
->retaddr_column
= fs
.retaddr_column
;
1247 state_in
->args_size
= context
.args_size
;
1248 state_in
->eh_ptr
= fs
.eh_ptr
;
1253 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1256 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1257 _Unwind_SpTmp
*tmp_sp
)
1259 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1261 if (size
== sizeof(_Unwind_Ptr
))
1262 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1265 gcc_assert (size
== sizeof(_Unwind_Word
));
1266 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1268 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1272 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1274 struct _Unwind_Context orig_context
= *context
;
1278 #ifdef EH_RETURN_STACKADJ_RTX
1279 /* Special handling here: Many machines do not use a frame pointer,
1280 and track the CFA only through offsets from the stack pointer from
1281 one frame to the next. In this case, the stack pointer is never
1282 stored, so it has no saved address in the context. What we do
1283 have is the CFA from the previous stack frame.
1285 In very special situations (such as unwind info for signal return),
1286 there may be location expressions that use the stack pointer as well.
1288 Do this conditionally for one frame. This allows the unwind info
1289 for one frame to save a copy of the stack pointer from the previous
1290 frame, and be able to use much easier CFA mechanisms to do it.
1291 Always zap the saved stack pointer value for the next frame; carrying
1292 the value over from one frame to another doesn't make sense. */
1294 _Unwind_SpTmp tmp_sp
;
1296 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1297 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1298 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1301 /* Compute this frame's CFA. */
1302 switch (fs
->regs
.cfa_how
)
1304 case CFA_REG_OFFSET
:
1305 cfa
= _Unwind_GetPtr (&orig_context
, fs
->regs
.cfa_reg
);
1306 cfa
+= fs
->regs
.cfa_offset
;
1311 const unsigned char *exp
= fs
->regs
.cfa_exp
;
1314 exp
= read_uleb128 (exp
, &len
);
1315 cfa
= (void *) (_Unwind_Ptr
)
1316 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1325 /* Compute the addresses of all registers saved in this frame. */
1326 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1327 switch (fs
->regs
.reg
[i
].how
)
1333 case REG_SAVED_OFFSET
:
1334 _Unwind_SetGRPtr (context
, i
,
1335 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1339 if (_Unwind_GRByValue (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
))
1340 _Unwind_SetGRValue (context
, i
,
1341 _Unwind_GetGR (&orig_context
,
1342 fs
->regs
.reg
[i
].loc
.reg
));
1344 _Unwind_SetGRPtr (context
, i
,
1345 _Unwind_GetGRPtr (&orig_context
,
1346 fs
->regs
.reg
[i
].loc
.reg
));
1351 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1355 exp
= read_uleb128 (exp
, &len
);
1356 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1358 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1362 case REG_SAVED_VAL_OFFSET
:
1363 _Unwind_SetGRValue (context
, i
,
1364 (_Unwind_Internal_Ptr
)
1365 (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1368 case REG_SAVED_VAL_EXP
:
1370 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1374 exp
= read_uleb128 (exp
, &len
);
1375 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1377 _Unwind_SetGRValue (context
, i
, val
);
1382 _Unwind_SetSignalFrame (context
, fs
->signal_frame
);
1384 #ifdef MD_FROB_UPDATE_CONTEXT
1385 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1389 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1390 of its caller. Update CONTEXT to refer to the caller as well. Note
1391 that the args_size and lsda members are not updated here, but later in
1392 uw_frame_state_for. */
1395 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1397 uw_update_context_1 (context
, fs
);
1399 /* In general this unwinder doesn't make any distinction between
1400 undefined and same_value rule. Call-saved registers are assumed
1401 to have same_value rule by default and explicit undefined
1402 rule is handled like same_value. The only exception is
1403 DW_CFA_undefined on retaddr_column which is supposed to
1404 mark outermost frame in DWARF 3. */
1405 if (fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (fs
->retaddr_column
)].how
1407 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1411 /* Compute the return address now, since the return address column
1412 can change from frame to frame. */
1413 context
->ra
= __builtin_extract_return_addr
1414 (_Unwind_GetPtr (context
, fs
->retaddr_column
));
1418 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1420 uw_update_context (context
, fs
);
1423 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1424 level will be the return address and the CFA. */
1426 #define uw_init_context(CONTEXT) \
1429 /* Do any necessary initialization to access arbitrary stack frames. \
1430 On the SPARC, this means flushing the register windows. */ \
1431 __builtin_unwind_init (); \
1432 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1433 __builtin_return_address (0)); \
1438 init_dwarf_reg_size_table (void)
1440 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1443 static void __attribute__((noinline
))
1444 uw_init_context_1 (struct _Unwind_Context
*context
,
1445 void *outer_cfa
, void *outer_ra
)
1447 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1448 _Unwind_FrameState fs
;
1449 _Unwind_SpTmp sp_slot
;
1450 _Unwind_Reason_Code code
;
1452 memset (context
, 0, sizeof (struct _Unwind_Context
));
1454 context
->flags
= EXTENDED_CONTEXT_BIT
;
1456 code
= uw_frame_state_for (context
, &fs
);
1457 gcc_assert (code
== _URC_NO_REASON
);
1461 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1462 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1463 && dwarf_reg_size_table
[0] == 0)
1464 init_dwarf_reg_size_table ();
1467 if (dwarf_reg_size_table
[0] == 0)
1468 init_dwarf_reg_size_table ();
1471 /* Force the frame state to use the known cfa value. */
1472 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1473 fs
.regs
.cfa_how
= CFA_REG_OFFSET
;
1474 fs
.regs
.cfa_reg
= __builtin_dwarf_sp_column ();
1475 fs
.regs
.cfa_offset
= 0;
1477 uw_update_context_1 (context
, &fs
);
1479 /* If the return address column was saved in a register in the
1480 initialization context, then we can't see it in the given
1481 call frame data. So have the initialization context tell us. */
1482 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1485 static void _Unwind_DebugHook (void *, void *)
1486 __attribute__ ((__noinline__
, __used__
, __noclone__
));
1488 /* This function is called during unwinding. It is intended as a hook
1489 for a debugger to intercept exceptions. CFA is the CFA of the
1490 target frame. HANDLER is the PC to which control will be
1493 _Unwind_DebugHook (void *cfa
__attribute__ ((__unused__
)),
1494 void *handler
__attribute__ ((__unused__
)))
1499 /* Install TARGET into CURRENT so that we can return to it. This is a
1500 macro because __builtin_eh_return must be invoked in the context of
1503 #define uw_install_context(CURRENT, TARGET) \
1506 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1507 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1508 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1509 __builtin_eh_return (offset, handler); \
1514 uw_install_context_1 (struct _Unwind_Context
*current
,
1515 struct _Unwind_Context
*target
)
1518 _Unwind_SpTmp sp_slot
;
1520 /* If the target frame does not have a saved stack pointer,
1521 then set up the target's CFA. */
1522 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1523 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1525 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1527 void *c
= current
->reg
[i
];
1528 void *t
= target
->reg
[i
];
1530 gcc_assert (current
->by_value
[i
] == 0);
1531 if (target
->by_value
[i
] && c
)
1535 if (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Word
))
1537 w
= (_Unwind_Internal_Ptr
) t
;
1538 memcpy (c
, &w
, sizeof (_Unwind_Word
));
1542 gcc_assert (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Ptr
));
1543 p
= (_Unwind_Internal_Ptr
) t
;
1544 memcpy (c
, &p
, sizeof (_Unwind_Ptr
));
1547 else if (t
&& c
&& t
!= c
)
1548 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1551 /* If the current frame doesn't have a saved stack pointer, then we
1552 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1553 pointer value reloaded. */
1554 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1558 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1560 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1561 if (STACK_GROWS_DOWNWARD
)
1562 return target_cfa
- current
->cfa
+ target
->args_size
;
1564 return current
->cfa
- target_cfa
- target
->args_size
;
1569 static inline _Unwind_Ptr
1570 uw_identify_context (struct _Unwind_Context
*context
)
1572 /* The CFA is not sufficient to disambiguate the context of a function
1573 interrupted by a signal before establishing its frame and the context
1574 of the signal itself. */
1575 if (STACK_GROWS_DOWNWARD
)
1576 return _Unwind_GetCFA (context
) - _Unwind_IsSignalFrame (context
);
1578 return _Unwind_GetCFA (context
) + _Unwind_IsSignalFrame (context
);
1582 #include "unwind.inc"
1584 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1585 alias (_Unwind_Backtrace
);
1586 alias (_Unwind_DeleteException
);
1587 alias (_Unwind_FindEnclosingFunction
);
1588 alias (_Unwind_ForcedUnwind
);
1589 alias (_Unwind_GetDataRelBase
);
1590 alias (_Unwind_GetTextRelBase
);
1591 alias (_Unwind_GetCFA
);
1592 alias (_Unwind_GetGR
);
1593 alias (_Unwind_GetIP
);
1594 alias (_Unwind_GetLanguageSpecificData
);
1595 alias (_Unwind_GetRegionStart
);
1596 alias (_Unwind_RaiseException
);
1597 alias (_Unwind_Resume
);
1598 alias (_Unwind_Resume_or_Rethrow
);
1599 alias (_Unwind_SetGR
);
1600 alias (_Unwind_SetIP
);
1603 #endif /* !USING_SJLJ_EXCEPTIONS */