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, 2011 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"
44 #ifndef __USING_SJLJ_EXCEPTIONS__
46 #ifndef STACK_GROWS_DOWNWARD
47 #define STACK_GROWS_DOWNWARD 0
49 #undef STACK_GROWS_DOWNWARD
50 #define STACK_GROWS_DOWNWARD 1
53 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
54 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
55 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
58 #ifndef DWARF_REG_TO_UNWIND_COLUMN
59 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
62 /* This is the register and unwind state for a particular frame. This
63 provides the information necessary to unwind up past a frame and return
65 struct _Unwind_Context
67 void *reg
[DWARF_FRAME_REGISTERS
+1];
71 struct dwarf_eh_bases bases
;
72 /* Signal frame context. */
73 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
74 /* Context which has version/args_size/by_value fields. */
75 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
77 /* 0 for now, can be increased when further fields are added to
78 struct _Unwind_Context. */
80 _Unwind_Word args_size
;
81 char by_value
[DWARF_FRAME_REGISTERS
+1];
84 /* Byte size of every register managed by these routines. */
85 static unsigned char dwarf_reg_size_table
[DWARF_FRAME_REGISTERS
+1];
88 /* Read unaligned data from the instruction buffer. */
93 unsigned u2
__attribute__ ((mode (HI
)));
94 unsigned u4
__attribute__ ((mode (SI
)));
95 unsigned u8
__attribute__ ((mode (DI
)));
96 signed s2
__attribute__ ((mode (HI
)));
97 signed s4
__attribute__ ((mode (SI
)));
98 signed s8
__attribute__ ((mode (DI
)));
99 } __attribute__ ((packed
));
101 static void uw_update_context (struct _Unwind_Context
*, _Unwind_FrameState
*);
102 static _Unwind_Reason_Code
uw_frame_state_for (struct _Unwind_Context
*,
103 _Unwind_FrameState
*);
106 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
109 read_1u (const void *p
) { return *(const unsigned char *) p
; }
112 read_1s (const void *p
) { return *(const signed char *) p
; }
115 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
118 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
120 static inline unsigned int
121 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
124 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
126 static inline unsigned long
127 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
129 static inline unsigned long
130 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
132 static inline _Unwind_Word
133 _Unwind_IsSignalFrame (struct _Unwind_Context
*context
)
135 return (context
->flags
& SIGNAL_FRAME_BIT
) ? 1 : 0;
139 _Unwind_SetSignalFrame (struct _Unwind_Context
*context
, int val
)
142 context
->flags
|= SIGNAL_FRAME_BIT
;
144 context
->flags
&= ~SIGNAL_FRAME_BIT
;
147 static inline _Unwind_Word
148 _Unwind_IsExtendedContext (struct _Unwind_Context
*context
)
150 return context
->flags
& EXTENDED_CONTEXT_BIT
;
153 /* Get the value of register INDEX as saved in CONTEXT. */
156 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
161 #ifdef DWARF_ZERO_REG
162 if (index
== DWARF_ZERO_REG
)
166 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
167 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
168 size
= dwarf_reg_size_table
[index
];
169 ptr
= context
->reg
[index
];
171 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
172 return (_Unwind_Word
) (_Unwind_Internal_Ptr
) ptr
;
174 /* This will segfault if the register hasn't been saved. */
175 if (size
== sizeof(_Unwind_Ptr
))
176 return * (_Unwind_Ptr
*) ptr
;
179 gcc_assert (size
== sizeof(_Unwind_Word
));
180 return * (_Unwind_Word
*) ptr
;
185 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
187 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
190 /* Get the value of the CFA as saved in CONTEXT. */
193 _Unwind_GetCFA (struct _Unwind_Context
*context
)
195 return (_Unwind_Ptr
) context
->cfa
;
198 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
201 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
206 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
207 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
208 size
= dwarf_reg_size_table
[index
];
210 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
212 context
->reg
[index
] = (void *) (_Unwind_Internal_Ptr
) val
;
216 ptr
= context
->reg
[index
];
218 if (size
== sizeof(_Unwind_Ptr
))
219 * (_Unwind_Ptr
*) ptr
= val
;
222 gcc_assert (size
== sizeof(_Unwind_Word
));
223 * (_Unwind_Word
*) ptr
= val
;
227 /* Get the pointer to a register INDEX as saved in CONTEXT. */
230 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
232 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
233 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
234 return &context
->reg
[index
];
235 return context
->reg
[index
];
238 /* Set the pointer to a register INDEX as saved in CONTEXT. */
241 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
243 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
244 if (_Unwind_IsExtendedContext (context
))
245 context
->by_value
[index
] = 0;
246 context
->reg
[index
] = p
;
249 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
252 _Unwind_SetGRValue (struct _Unwind_Context
*context
, int index
,
255 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
256 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
257 gcc_assert (dwarf_reg_size_table
[index
] == sizeof (_Unwind_Ptr
));
259 context
->by_value
[index
] = 1;
260 context
->reg
[index
] = (void *) (_Unwind_Internal_Ptr
) val
;
263 /* Return nonzero if register INDEX is stored by value rather than
267 _Unwind_GRByValue (struct _Unwind_Context
*context
, int index
)
269 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
270 return context
->by_value
[index
];
273 /* Retrieve the return address for CONTEXT. */
276 _Unwind_GetIP (struct _Unwind_Context
*context
)
278 return (_Unwind_Ptr
) context
->ra
;
281 /* Retrieve the return address and flag whether that IP is before
282 or after first not yet fully executed instruction. */
285 _Unwind_GetIPInfo (struct _Unwind_Context
*context
, int *ip_before_insn
)
287 *ip_before_insn
= _Unwind_IsSignalFrame (context
);
288 return (_Unwind_Ptr
) context
->ra
;
291 /* Overwrite the return address for CONTEXT with VAL. */
294 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
296 context
->ra
= (void *) val
;
300 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
302 return context
->lsda
;
306 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
308 return (_Unwind_Ptr
) context
->bases
.func
;
312 _Unwind_FindEnclosingFunction (void *pc
)
314 struct dwarf_eh_bases bases
;
315 const struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
324 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
326 return (_Unwind_Ptr
) context
->bases
.dbase
;
330 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
332 return (_Unwind_Ptr
) context
->bases
.tbase
;
336 #ifdef MD_UNWIND_SUPPORT
337 #include MD_UNWIND_SUPPORT
340 /* Extract any interesting information from the CIE for the translation
341 unit F belongs to. Return a pointer to the byte after the augmentation,
342 or NULL if we encountered an undecipherable augmentation. */
344 static const unsigned char *
345 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
346 _Unwind_FrameState
*fs
)
348 const unsigned char *aug
= cie
->augmentation
;
349 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
350 const unsigned char *ret
= NULL
;
354 /* g++ v2 "eh" has pointer immediately following augmentation string,
355 so it must be handled first. */
356 if (aug
[0] == 'e' && aug
[1] == 'h')
358 fs
->eh_ptr
= read_pointer (p
);
359 p
+= sizeof (void *);
363 /* After the augmentation resp. pointer for "eh" augmentation
364 follows for CIE version >= 4 address size byte and
365 segment size byte. */
366 if (__builtin_expect (cie
->version
>= 4, 0))
368 if (p
[0] != sizeof (void *) || p
[1] != 0)
372 /* Immediately following this are the code and
373 data alignment and return address column. */
374 p
= read_uleb128 (p
, &utmp
);
375 fs
->code_align
= (_Unwind_Word
)utmp
;
376 p
= read_sleb128 (p
, &stmp
);
377 fs
->data_align
= (_Unwind_Sword
)stmp
;
378 if (cie
->version
== 1)
379 fs
->retaddr_column
= *p
++;
382 p
= read_uleb128 (p
, &utmp
);
383 fs
->retaddr_column
= (_Unwind_Word
)utmp
;
385 fs
->lsda_encoding
= DW_EH_PE_omit
;
387 /* If the augmentation starts with 'z', then a uleb128 immediately
388 follows containing the length of the augmentation field following
392 p
= read_uleb128 (p
, &utmp
);
399 /* Iterate over recognized augmentation subsequences. */
402 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
405 fs
->lsda_encoding
= *p
++;
409 /* "R" indicates a byte indicating how FDE addresses are encoded. */
410 else if (aug
[0] == 'R')
412 fs
->fde_encoding
= *p
++;
416 /* "P" indicates a personality routine in the CIE augmentation. */
417 else if (aug
[0] == 'P')
419 _Unwind_Ptr personality
;
421 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
422 fs
->personality
= (_Unwind_Personality_Fn
) personality
;
426 /* "S" indicates a signal frame. */
427 else if (aug
[0] == 'S')
429 fs
->signal_frame
= 1;
433 /* Otherwise we have an unknown augmentation string.
434 Bail unless we saw a 'z' prefix. */
439 return ret
? ret
: p
;
443 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
444 onto the stack to start. */
447 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
448 struct _Unwind_Context
*context
, _Unwind_Word initial
)
450 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
456 while (op_ptr
< op_end
)
458 enum dwarf_location_atom op
= *op_ptr
++;
460 _uleb128_t reg
, utmp
;
461 _sleb128_t offset
, stmp
;
497 result
= op
- DW_OP_lit0
;
501 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
502 op_ptr
+= sizeof (void *);
505 case DW_OP_GNU_encoded_addr
:
508 op_ptr
= read_encoded_value (context
, *op_ptr
, op_ptr
+1, &presult
);
514 result
= read_1u (op_ptr
);
518 result
= read_1s (op_ptr
);
522 result
= read_2u (op_ptr
);
526 result
= read_2s (op_ptr
);
530 result
= read_4u (op_ptr
);
534 result
= read_4s (op_ptr
);
538 result
= read_8u (op_ptr
);
542 result
= read_8s (op_ptr
);
546 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
547 result
= (_Unwind_Word
)utmp
;
550 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
551 result
= (_Unwind_Sword
)stmp
;
586 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
589 op_ptr
= read_uleb128 (op_ptr
, ®
);
590 result
= _Unwind_GetGR (context
, reg
);
625 op_ptr
= read_sleb128 (op_ptr
, &offset
);
626 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
629 op_ptr
= read_uleb128 (op_ptr
, ®
);
630 op_ptr
= read_sleb128 (op_ptr
, &offset
);
631 result
= _Unwind_GetGR (context
, reg
) + (_Unwind_Word
)offset
;
635 gcc_assert (stack_elt
);
636 result
= stack
[stack_elt
- 1];
640 gcc_assert (stack_elt
);
646 gcc_assert (offset
< stack_elt
- 1);
647 result
= stack
[stack_elt
- 1 - offset
];
651 gcc_assert (stack_elt
>= 2);
652 result
= stack
[stack_elt
- 2];
658 gcc_assert (stack_elt
>= 2);
659 t
= stack
[stack_elt
- 1];
660 stack
[stack_elt
- 1] = stack
[stack_elt
- 2];
661 stack
[stack_elt
- 2] = t
;
667 _Unwind_Word t1
, t2
, t3
;
669 gcc_assert (stack_elt
>= 3);
670 t1
= stack
[stack_elt
- 1];
671 t2
= stack
[stack_elt
- 2];
672 t3
= stack
[stack_elt
- 3];
673 stack
[stack_elt
- 1] = t2
;
674 stack
[stack_elt
- 2] = t3
;
675 stack
[stack_elt
- 3] = t1
;
680 case DW_OP_deref_size
:
684 case DW_OP_plus_uconst
:
685 /* Unary operations. */
686 gcc_assert (stack_elt
);
689 result
= stack
[stack_elt
];
695 void *ptr
= (void *) (_Unwind_Ptr
) result
;
696 result
= (_Unwind_Ptr
) read_pointer (ptr
);
700 case DW_OP_deref_size
:
702 void *ptr
= (void *) (_Unwind_Ptr
) result
;
706 result
= read_1u (ptr
);
709 result
= read_2u (ptr
);
712 result
= read_4u (ptr
);
715 result
= read_8u (ptr
);
724 if ((_Unwind_Sword
) result
< 0)
733 case DW_OP_plus_uconst
:
734 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
735 result
+= (_Unwind_Word
)utmp
;
761 /* Binary operations. */
762 _Unwind_Word first
, second
;
763 gcc_assert (stack_elt
>= 2);
766 second
= stack
[stack_elt
];
767 first
= stack
[stack_elt
+ 1];
772 result
= second
& first
;
775 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
778 result
= second
- first
;
781 result
= second
% first
;
784 result
= second
* first
;
787 result
= second
| first
;
790 result
= second
+ first
;
793 result
= second
<< first
;
796 result
= second
>> first
;
799 result
= (_Unwind_Sword
) second
>> first
;
802 result
= second
^ first
;
805 result
= (_Unwind_Sword
) second
<= (_Unwind_Sword
) first
;
808 result
= (_Unwind_Sword
) second
>= (_Unwind_Sword
) first
;
811 result
= (_Unwind_Sword
) second
== (_Unwind_Sword
) first
;
814 result
= (_Unwind_Sword
) second
< (_Unwind_Sword
) first
;
817 result
= (_Unwind_Sword
) second
> (_Unwind_Sword
) first
;
820 result
= (_Unwind_Sword
) second
!= (_Unwind_Sword
) first
;
830 offset
= read_2s (op_ptr
);
836 gcc_assert (stack_elt
);
839 offset
= read_2s (op_ptr
);
841 if (stack
[stack_elt
] != 0)
852 /* Most things push a result value. */
853 gcc_assert ((size_t) stack_elt
< sizeof(stack
)/sizeof(*stack
));
854 stack
[stack_elt
++] = result
;
858 /* We were executing this program to get a value. It should be
860 gcc_assert (stack_elt
);
862 return stack
[stack_elt
];
866 /* Decode DWARF 2 call frame information. Takes pointers the
867 instruction sequence to decode, current register information and
868 CIE info, and the PC range to evaluate. */
871 execute_cfa_program (const unsigned char *insn_ptr
,
872 const unsigned char *insn_end
,
873 struct _Unwind_Context
*context
,
874 _Unwind_FrameState
*fs
)
876 struct frame_state_reg_info
*unused_rs
= NULL
;
878 /* Don't allow remember/restore between CIE and FDE programs. */
879 fs
->regs
.prev
= NULL
;
881 /* The comparison with the return address uses < rather than <= because
882 we are only interested in the effects of code before the call; for a
883 noreturn function, the return address may point to unrelated code with
884 a different stack configuration that we are not interested in. We
885 assume that the call itself is unwind info-neutral; if not, or if
886 there are delay instructions that adjust the stack, these must be
887 reflected at the point immediately before the call insn.
888 In signal frames, return address is after last completed instruction,
889 so we add 1 to return address to make the comparison <=. */
890 while (insn_ptr
< insn_end
891 && fs
->pc
< context
->ra
+ _Unwind_IsSignalFrame (context
))
893 unsigned char insn
= *insn_ptr
++;
894 _uleb128_t reg
, utmp
;
895 _sleb128_t offset
, stmp
;
897 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
898 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
899 else if ((insn
& 0xc0) == DW_CFA_offset
)
902 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
903 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
904 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
906 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
908 else if ((insn
& 0xc0) == DW_CFA_restore
)
911 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_UNSAVED
;
919 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
921 fs
->pc
= (void *) pc
;
925 case DW_CFA_advance_loc1
:
926 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
929 case DW_CFA_advance_loc2
:
930 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
933 case DW_CFA_advance_loc4
:
934 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
938 case DW_CFA_offset_extended
:
939 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
940 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
941 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
942 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
944 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
947 case DW_CFA_restore_extended
:
948 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
949 /* FIXME, this is wrong; the CIE might have said that the
950 register was saved somewhere. */
951 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
954 case DW_CFA_same_value
:
955 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
956 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
959 case DW_CFA_undefined
:
960 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
961 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNDEFINED
;
967 case DW_CFA_register
:
970 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
971 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
972 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_REG
;
973 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.reg
=
978 case DW_CFA_remember_state
:
980 struct frame_state_reg_info
*new_rs
;
984 unused_rs
= unused_rs
->prev
;
987 new_rs
= alloca (sizeof (struct frame_state_reg_info
));
990 fs
->regs
.prev
= new_rs
;
994 case DW_CFA_restore_state
:
996 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
998 old_rs
->prev
= unused_rs
;
1003 case DW_CFA_def_cfa
:
1004 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1005 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1006 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1007 fs
->regs
.cfa_offset
= (_Unwind_Word
)utmp
;
1008 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1011 case DW_CFA_def_cfa_register
:
1012 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1013 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1014 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1017 case DW_CFA_def_cfa_offset
:
1018 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1019 fs
->regs
.cfa_offset
= utmp
;
1020 /* cfa_how deliberately not set. */
1023 case DW_CFA_def_cfa_expression
:
1024 fs
->regs
.cfa_exp
= insn_ptr
;
1025 fs
->regs
.cfa_how
= CFA_EXP
;
1026 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1030 case DW_CFA_expression
:
1031 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1032 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_EXP
;
1033 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
1034 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1039 case DW_CFA_offset_extended_sf
:
1040 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1041 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1042 offset
= stmp
* fs
->data_align
;
1043 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1045 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1048 case DW_CFA_def_cfa_sf
:
1049 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1050 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1051 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1052 fs
->regs
.cfa_offset
= (_Unwind_Sword
)stmp
;
1053 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1054 fs
->regs
.cfa_offset
*= fs
->data_align
;
1057 case DW_CFA_def_cfa_offset_sf
:
1058 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1059 fs
->regs
.cfa_offset
= (_Unwind_Sword
)stmp
;
1060 fs
->regs
.cfa_offset
*= fs
->data_align
;
1061 /* cfa_how deliberately not set. */
1064 case DW_CFA_val_offset
:
1065 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1066 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1067 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
1068 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1069 = REG_SAVED_VAL_OFFSET
;
1070 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1073 case DW_CFA_val_offset_sf
:
1074 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1075 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1076 offset
= stmp
* fs
->data_align
;
1077 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1078 = REG_SAVED_VAL_OFFSET
;
1079 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1082 case DW_CFA_val_expression
:
1083 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1084 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1085 = REG_SAVED_VAL_EXP
;
1086 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
1087 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1091 case DW_CFA_GNU_window_save
:
1092 /* ??? Hardcoded for SPARC register window configuration. */
1093 for (reg
= 16; reg
< 32; ++reg
)
1095 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
1096 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
1100 case DW_CFA_GNU_args_size
:
1101 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1102 context
->args_size
= (_Unwind_Word
)utmp
;
1105 case DW_CFA_GNU_negative_offset_extended
:
1106 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1107 older PowerPC code. */
1108 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1109 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1110 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
1111 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1113 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= -offset
;
1122 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1123 its caller and decode it into FS. This function also sets the
1124 args_size and lsda members of CONTEXT, as they are really information
1125 about the caller's frame. */
1127 static _Unwind_Reason_Code
1128 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1130 const struct dwarf_fde
*fde
;
1131 const struct dwarf_cie
*cie
;
1132 const unsigned char *aug
, *insn
, *end
;
1134 memset (fs
, 0, sizeof (*fs
));
1135 context
->args_size
= 0;
1138 if (context
->ra
== 0)
1139 return _URC_END_OF_STACK
;
1141 fde
= _Unwind_Find_FDE (context
->ra
+ _Unwind_IsSignalFrame (context
) - 1,
1145 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1146 /* Couldn't find frame unwind info for this function. Try a
1147 target-specific fallback mechanism. This will necessarily
1148 not provide a personality routine or LSDA. */
1149 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
1151 return _URC_END_OF_STACK
;
1155 fs
->pc
= context
->bases
.func
;
1157 cie
= get_cie (fde
);
1158 insn
= extract_cie_info (cie
, context
, fs
);
1160 /* CIE contained unknown augmentation. */
1161 return _URC_FATAL_PHASE1_ERROR
;
1163 /* First decode all the insns in the CIE. */
1164 end
= (const unsigned char *) next_fde ((const struct dwarf_fde
*) cie
);
1165 execute_cfa_program (insn
, end
, context
, fs
);
1167 /* Locate augmentation for the fde. */
1168 aug
= (const unsigned char *) fde
+ sizeof (*fde
);
1169 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1174 aug
= read_uleb128 (aug
, &i
);
1177 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1181 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1182 context
->lsda
= (void *) lsda
;
1185 /* Then the insns in the FDE up to our target PC. */
1188 end
= (const unsigned char *) next_fde (fde
);
1189 execute_cfa_program (insn
, end
, context
, fs
);
1191 return _URC_NO_REASON
;
1194 typedef struct frame_state
1200 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1201 unsigned short cfa_reg
;
1202 unsigned short retaddr_column
;
1203 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1206 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1208 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1209 a given PC_TARGET. The caller should allocate a local variable of
1210 `struct frame_state' and pass its address to STATE_IN. */
1212 struct frame_state
*
1213 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1215 struct _Unwind_Context context
;
1216 _Unwind_FrameState fs
;
1219 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1220 context
.flags
= EXTENDED_CONTEXT_BIT
;
1221 context
.ra
= pc_target
+ 1;
1223 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1226 /* We have no way to pass a location expression for the CFA to our
1227 caller. It wouldn't understand it anyway. */
1228 if (fs
.regs
.cfa_how
== CFA_EXP
)
1231 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1233 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1234 switch (state_in
->saved
[reg
])
1237 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1239 case REG_SAVED_OFFSET
:
1240 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1243 state_in
->reg_or_offset
[reg
] = 0;
1248 state_in
->cfa_offset
= fs
.regs
.cfa_offset
;
1249 state_in
->cfa_reg
= fs
.regs
.cfa_reg
;
1250 state_in
->retaddr_column
= fs
.retaddr_column
;
1251 state_in
->args_size
= context
.args_size
;
1252 state_in
->eh_ptr
= fs
.eh_ptr
;
1257 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1260 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1261 _Unwind_SpTmp
*tmp_sp
)
1263 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1265 if (size
== sizeof(_Unwind_Ptr
))
1266 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1269 gcc_assert (size
== sizeof(_Unwind_Word
));
1270 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1272 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1276 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1278 struct _Unwind_Context orig_context
= *context
;
1282 #ifdef EH_RETURN_STACKADJ_RTX
1283 /* Special handling here: Many machines do not use a frame pointer,
1284 and track the CFA only through offsets from the stack pointer from
1285 one frame to the next. In this case, the stack pointer is never
1286 stored, so it has no saved address in the context. What we do
1287 have is the CFA from the previous stack frame.
1289 In very special situations (such as unwind info for signal return),
1290 there may be location expressions that use the stack pointer as well.
1292 Do this conditionally for one frame. This allows the unwind info
1293 for one frame to save a copy of the stack pointer from the previous
1294 frame, and be able to use much easier CFA mechanisms to do it.
1295 Always zap the saved stack pointer value for the next frame; carrying
1296 the value over from one frame to another doesn't make sense. */
1298 _Unwind_SpTmp tmp_sp
;
1300 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1301 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1302 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1305 /* Compute this frame's CFA. */
1306 switch (fs
->regs
.cfa_how
)
1308 case CFA_REG_OFFSET
:
1309 cfa
= _Unwind_GetPtr (&orig_context
, fs
->regs
.cfa_reg
);
1310 cfa
+= fs
->regs
.cfa_offset
;
1315 const unsigned char *exp
= fs
->regs
.cfa_exp
;
1318 exp
= read_uleb128 (exp
, &len
);
1319 cfa
= (void *) (_Unwind_Ptr
)
1320 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1329 /* Compute the addresses of all registers saved in this frame. */
1330 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1331 switch (fs
->regs
.reg
[i
].how
)
1337 case REG_SAVED_OFFSET
:
1338 _Unwind_SetGRPtr (context
, i
,
1339 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1343 if (_Unwind_GRByValue (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
))
1344 _Unwind_SetGRValue (context
, i
,
1345 _Unwind_GetGR (&orig_context
,
1346 fs
->regs
.reg
[i
].loc
.reg
));
1348 _Unwind_SetGRPtr (context
, i
,
1349 _Unwind_GetGRPtr (&orig_context
,
1350 fs
->regs
.reg
[i
].loc
.reg
));
1355 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1359 exp
= read_uleb128 (exp
, &len
);
1360 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1362 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1366 case REG_SAVED_VAL_OFFSET
:
1367 _Unwind_SetGRValue (context
, i
,
1368 (_Unwind_Internal_Ptr
)
1369 (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1372 case REG_SAVED_VAL_EXP
:
1374 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1378 exp
= read_uleb128 (exp
, &len
);
1379 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1381 _Unwind_SetGRValue (context
, i
, val
);
1386 _Unwind_SetSignalFrame (context
, fs
->signal_frame
);
1388 #ifdef MD_FROB_UPDATE_CONTEXT
1389 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1393 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1394 of its caller. Update CONTEXT to refer to the caller as well. Note
1395 that the args_size and lsda members are not updated here, but later in
1396 uw_frame_state_for. */
1399 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1401 uw_update_context_1 (context
, fs
);
1403 /* In general this unwinder doesn't make any distinction between
1404 undefined and same_value rule. Call-saved registers are assumed
1405 to have same_value rule by default and explicit undefined
1406 rule is handled like same_value. The only exception is
1407 DW_CFA_undefined on retaddr_column which is supposed to
1408 mark outermost frame in DWARF 3. */
1409 if (fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (fs
->retaddr_column
)].how
1411 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1415 /* Compute the return address now, since the return address column
1416 can change from frame to frame. */
1417 context
->ra
= __builtin_extract_return_addr
1418 (_Unwind_GetPtr (context
, fs
->retaddr_column
));
1422 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1424 uw_update_context (context
, fs
);
1427 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1428 level will be the return address and the CFA. */
1430 #define uw_init_context(CONTEXT) \
1433 /* Do any necessary initialization to access arbitrary stack frames. \
1434 On the SPARC, this means flushing the register windows. */ \
1435 __builtin_unwind_init (); \
1436 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1437 __builtin_return_address (0)); \
1442 init_dwarf_reg_size_table (void)
1444 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1447 static void __attribute__((noinline
))
1448 uw_init_context_1 (struct _Unwind_Context
*context
,
1449 void *outer_cfa
, void *outer_ra
)
1451 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1452 _Unwind_FrameState fs
;
1453 _Unwind_SpTmp sp_slot
;
1454 _Unwind_Reason_Code code
;
1456 memset (context
, 0, sizeof (struct _Unwind_Context
));
1458 context
->flags
= EXTENDED_CONTEXT_BIT
;
1460 code
= uw_frame_state_for (context
, &fs
);
1461 gcc_assert (code
== _URC_NO_REASON
);
1465 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1466 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1467 && dwarf_reg_size_table
[0] == 0)
1468 init_dwarf_reg_size_table ();
1471 if (dwarf_reg_size_table
[0] == 0)
1472 init_dwarf_reg_size_table ();
1475 /* Force the frame state to use the known cfa value. */
1476 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1477 fs
.regs
.cfa_how
= CFA_REG_OFFSET
;
1478 fs
.regs
.cfa_reg
= __builtin_dwarf_sp_column ();
1479 fs
.regs
.cfa_offset
= 0;
1481 uw_update_context_1 (context
, &fs
);
1483 /* If the return address column was saved in a register in the
1484 initialization context, then we can't see it in the given
1485 call frame data. So have the initialization context tell us. */
1486 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1489 static void _Unwind_DebugHook (void *, void *)
1490 __attribute__ ((__noinline__
, __used__
, __noclone__
));
1492 /* This function is called during unwinding. It is intended as a hook
1493 for a debugger to intercept exceptions. CFA is the CFA of the
1494 target frame. HANDLER is the PC to which control will be
1497 _Unwind_DebugHook (void *cfa
__attribute__ ((__unused__
)),
1498 void *handler
__attribute__ ((__unused__
)))
1500 /* We only want to use stap probes starting with v3. Earlier
1501 versions added too much startup cost. */
1502 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1503 STAP_PROBE2 (libgcc
, unwind
, cfa
, handler
);
1509 /* Install TARGET into CURRENT so that we can return to it. This is a
1510 macro because __builtin_eh_return must be invoked in the context of
1513 #define uw_install_context(CURRENT, TARGET) \
1516 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1517 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1518 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1519 __builtin_eh_return (offset, handler); \
1524 uw_install_context_1 (struct _Unwind_Context
*current
,
1525 struct _Unwind_Context
*target
)
1528 _Unwind_SpTmp sp_slot
;
1530 /* If the target frame does not have a saved stack pointer,
1531 then set up the target's CFA. */
1532 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1533 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1535 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1537 void *c
= current
->reg
[i
];
1538 void *t
= target
->reg
[i
];
1540 gcc_assert (current
->by_value
[i
] == 0);
1541 if (target
->by_value
[i
] && c
)
1545 if (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Word
))
1547 w
= (_Unwind_Internal_Ptr
) t
;
1548 memcpy (c
, &w
, sizeof (_Unwind_Word
));
1552 gcc_assert (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Ptr
));
1553 p
= (_Unwind_Internal_Ptr
) t
;
1554 memcpy (c
, &p
, sizeof (_Unwind_Ptr
));
1557 else if (t
&& c
&& t
!= c
)
1558 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1561 /* If the current frame doesn't have a saved stack pointer, then we
1562 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1563 pointer value reloaded. */
1564 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1568 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1570 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1571 if (STACK_GROWS_DOWNWARD
)
1572 return target_cfa
- current
->cfa
+ target
->args_size
;
1574 return current
->cfa
- target_cfa
- target
->args_size
;
1579 static inline _Unwind_Ptr
1580 uw_identify_context (struct _Unwind_Context
*context
)
1582 /* The CFA is not sufficient to disambiguate the context of a function
1583 interrupted by a signal before establishing its frame and the context
1584 of the signal itself. */
1585 if (STACK_GROWS_DOWNWARD
)
1586 return _Unwind_GetCFA (context
) - _Unwind_IsSignalFrame (context
);
1588 return _Unwind_GetCFA (context
) + _Unwind_IsSignalFrame (context
);
1592 #include "unwind.inc"
1594 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1595 alias (_Unwind_Backtrace
);
1596 alias (_Unwind_DeleteException
);
1597 alias (_Unwind_FindEnclosingFunction
);
1598 alias (_Unwind_ForcedUnwind
);
1599 alias (_Unwind_GetDataRelBase
);
1600 alias (_Unwind_GetTextRelBase
);
1601 alias (_Unwind_GetCFA
);
1602 alias (_Unwind_GetGR
);
1603 alias (_Unwind_GetIP
);
1604 alias (_Unwind_GetLanguageSpecificData
);
1605 alias (_Unwind_GetRegionStart
);
1606 alias (_Unwind_RaiseException
);
1607 alias (_Unwind_Resume
);
1608 alias (_Unwind_Resume_or_Rethrow
);
1609 alias (_Unwind_SetGR
);
1610 alias (_Unwind_SetIP
);
1613 #endif /* !USING_SJLJ_EXCEPTIONS */