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 #include "md-unwind-support.h"
338 /* Extract any interesting information from the CIE for the translation
339 unit F belongs to. Return a pointer to the byte after the augmentation,
340 or NULL if we encountered an undecipherable augmentation. */
342 static const unsigned char *
343 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
344 _Unwind_FrameState
*fs
)
346 const unsigned char *aug
= cie
->augmentation
;
347 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
348 const unsigned char *ret
= NULL
;
352 /* g++ v2 "eh" has pointer immediately following augmentation string,
353 so it must be handled first. */
354 if (aug
[0] == 'e' && aug
[1] == 'h')
356 fs
->eh_ptr
= read_pointer (p
);
357 p
+= sizeof (void *);
361 /* After the augmentation resp. pointer for "eh" augmentation
362 follows for CIE version >= 4 address size byte and
363 segment size byte. */
364 if (__builtin_expect (cie
->version
>= 4, 0))
366 if (p
[0] != sizeof (void *) || p
[1] != 0)
370 /* Immediately following this are the code and
371 data alignment and return address column. */
372 p
= read_uleb128 (p
, &utmp
);
373 fs
->code_align
= (_Unwind_Word
)utmp
;
374 p
= read_sleb128 (p
, &stmp
);
375 fs
->data_align
= (_Unwind_Sword
)stmp
;
376 if (cie
->version
== 1)
377 fs
->retaddr_column
= *p
++;
380 p
= read_uleb128 (p
, &utmp
);
381 fs
->retaddr_column
= (_Unwind_Word
)utmp
;
383 fs
->lsda_encoding
= DW_EH_PE_omit
;
385 /* If the augmentation starts with 'z', then a uleb128 immediately
386 follows containing the length of the augmentation field following
390 p
= read_uleb128 (p
, &utmp
);
397 /* Iterate over recognized augmentation subsequences. */
400 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
403 fs
->lsda_encoding
= *p
++;
407 /* "R" indicates a byte indicating how FDE addresses are encoded. */
408 else if (aug
[0] == 'R')
410 fs
->fde_encoding
= *p
++;
414 /* "P" indicates a personality routine in the CIE augmentation. */
415 else if (aug
[0] == 'P')
417 _Unwind_Ptr personality
;
419 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
420 fs
->personality
= (_Unwind_Personality_Fn
) personality
;
424 /* "S" indicates a signal frame. */
425 else if (aug
[0] == 'S')
427 fs
->signal_frame
= 1;
431 /* Otherwise we have an unknown augmentation string.
432 Bail unless we saw a 'z' prefix. */
437 return ret
? ret
: p
;
441 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
442 onto the stack to start. */
445 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
446 struct _Unwind_Context
*context
, _Unwind_Word initial
)
448 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
454 while (op_ptr
< op_end
)
456 enum dwarf_location_atom op
= *op_ptr
++;
458 _uleb128_t reg
, utmp
;
459 _sleb128_t offset
, stmp
;
495 result
= op
- DW_OP_lit0
;
499 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
500 op_ptr
+= sizeof (void *);
503 case DW_OP_GNU_encoded_addr
:
506 op_ptr
= read_encoded_value (context
, *op_ptr
, op_ptr
+1, &presult
);
512 result
= read_1u (op_ptr
);
516 result
= read_1s (op_ptr
);
520 result
= read_2u (op_ptr
);
524 result
= read_2s (op_ptr
);
528 result
= read_4u (op_ptr
);
532 result
= read_4s (op_ptr
);
536 result
= read_8u (op_ptr
);
540 result
= read_8s (op_ptr
);
544 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
545 result
= (_Unwind_Word
)utmp
;
548 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
549 result
= (_Unwind_Sword
)stmp
;
584 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
587 op_ptr
= read_uleb128 (op_ptr
, ®
);
588 result
= _Unwind_GetGR (context
, reg
);
623 op_ptr
= read_sleb128 (op_ptr
, &offset
);
624 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
627 op_ptr
= read_uleb128 (op_ptr
, ®
);
628 op_ptr
= read_sleb128 (op_ptr
, &offset
);
629 result
= _Unwind_GetGR (context
, reg
) + (_Unwind_Word
)offset
;
633 gcc_assert (stack_elt
);
634 result
= stack
[stack_elt
- 1];
638 gcc_assert (stack_elt
);
644 gcc_assert (offset
< stack_elt
- 1);
645 result
= stack
[stack_elt
- 1 - offset
];
649 gcc_assert (stack_elt
>= 2);
650 result
= stack
[stack_elt
- 2];
656 gcc_assert (stack_elt
>= 2);
657 t
= stack
[stack_elt
- 1];
658 stack
[stack_elt
- 1] = stack
[stack_elt
- 2];
659 stack
[stack_elt
- 2] = t
;
665 _Unwind_Word t1
, t2
, t3
;
667 gcc_assert (stack_elt
>= 3);
668 t1
= stack
[stack_elt
- 1];
669 t2
= stack
[stack_elt
- 2];
670 t3
= stack
[stack_elt
- 3];
671 stack
[stack_elt
- 1] = t2
;
672 stack
[stack_elt
- 2] = t3
;
673 stack
[stack_elt
- 3] = t1
;
678 case DW_OP_deref_size
:
682 case DW_OP_plus_uconst
:
683 /* Unary operations. */
684 gcc_assert (stack_elt
);
687 result
= stack
[stack_elt
];
693 void *ptr
= (void *) (_Unwind_Ptr
) result
;
694 result
= (_Unwind_Ptr
) read_pointer (ptr
);
698 case DW_OP_deref_size
:
700 void *ptr
= (void *) (_Unwind_Ptr
) result
;
704 result
= read_1u (ptr
);
707 result
= read_2u (ptr
);
710 result
= read_4u (ptr
);
713 result
= read_8u (ptr
);
722 if ((_Unwind_Sword
) result
< 0)
731 case DW_OP_plus_uconst
:
732 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
733 result
+= (_Unwind_Word
)utmp
;
759 /* Binary operations. */
760 _Unwind_Word first
, second
;
761 gcc_assert (stack_elt
>= 2);
764 second
= stack
[stack_elt
];
765 first
= stack
[stack_elt
+ 1];
770 result
= second
& first
;
773 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
776 result
= second
- first
;
779 result
= second
% first
;
782 result
= second
* first
;
785 result
= second
| first
;
788 result
= second
+ first
;
791 result
= second
<< first
;
794 result
= second
>> first
;
797 result
= (_Unwind_Sword
) second
>> first
;
800 result
= second
^ first
;
803 result
= (_Unwind_Sword
) second
<= (_Unwind_Sword
) first
;
806 result
= (_Unwind_Sword
) second
>= (_Unwind_Sword
) first
;
809 result
= (_Unwind_Sword
) second
== (_Unwind_Sword
) first
;
812 result
= (_Unwind_Sword
) second
< (_Unwind_Sword
) first
;
815 result
= (_Unwind_Sword
) second
> (_Unwind_Sword
) first
;
818 result
= (_Unwind_Sword
) second
!= (_Unwind_Sword
) first
;
828 offset
= read_2s (op_ptr
);
834 gcc_assert (stack_elt
);
837 offset
= read_2s (op_ptr
);
839 if (stack
[stack_elt
] != 0)
850 /* Most things push a result value. */
851 gcc_assert ((size_t) stack_elt
< sizeof(stack
)/sizeof(*stack
));
852 stack
[stack_elt
++] = result
;
856 /* We were executing this program to get a value. It should be
858 gcc_assert (stack_elt
);
860 return stack
[stack_elt
];
864 /* Decode DWARF 2 call frame information. Takes pointers the
865 instruction sequence to decode, current register information and
866 CIE info, and the PC range to evaluate. */
869 execute_cfa_program (const unsigned char *insn_ptr
,
870 const unsigned char *insn_end
,
871 struct _Unwind_Context
*context
,
872 _Unwind_FrameState
*fs
)
874 struct frame_state_reg_info
*unused_rs
= NULL
;
876 /* Don't allow remember/restore between CIE and FDE programs. */
877 fs
->regs
.prev
= NULL
;
879 /* The comparison with the return address uses < rather than <= because
880 we are only interested in the effects of code before the call; for a
881 noreturn function, the return address may point to unrelated code with
882 a different stack configuration that we are not interested in. We
883 assume that the call itself is unwind info-neutral; if not, or if
884 there are delay instructions that adjust the stack, these must be
885 reflected at the point immediately before the call insn.
886 In signal frames, return address is after last completed instruction,
887 so we add 1 to return address to make the comparison <=. */
888 while (insn_ptr
< insn_end
889 && fs
->pc
< context
->ra
+ _Unwind_IsSignalFrame (context
))
891 unsigned char insn
= *insn_ptr
++;
892 _uleb128_t reg
, utmp
;
893 _sleb128_t offset
, stmp
;
895 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
896 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
897 else if ((insn
& 0xc0) == DW_CFA_offset
)
900 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
901 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
902 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
904 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
906 else if ((insn
& 0xc0) == DW_CFA_restore
)
909 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_UNSAVED
;
917 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
919 fs
->pc
= (void *) pc
;
923 case DW_CFA_advance_loc1
:
924 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
927 case DW_CFA_advance_loc2
:
928 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
931 case DW_CFA_advance_loc4
:
932 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
936 case DW_CFA_offset_extended
:
937 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
938 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
939 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
940 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
942 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
945 case DW_CFA_restore_extended
:
946 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
947 /* FIXME, this is wrong; the CIE might have said that the
948 register was saved somewhere. */
949 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
952 case DW_CFA_same_value
:
953 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
954 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
957 case DW_CFA_undefined
:
958 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
959 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNDEFINED
;
965 case DW_CFA_register
:
968 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
969 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
970 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_REG
;
971 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.reg
=
976 case DW_CFA_remember_state
:
978 struct frame_state_reg_info
*new_rs
;
982 unused_rs
= unused_rs
->prev
;
985 new_rs
= alloca (sizeof (struct frame_state_reg_info
));
988 fs
->regs
.prev
= new_rs
;
992 case DW_CFA_restore_state
:
994 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
996 old_rs
->prev
= unused_rs
;
1001 case DW_CFA_def_cfa
:
1002 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1003 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1004 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1005 fs
->regs
.cfa_offset
= (_Unwind_Word
)utmp
;
1006 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1009 case DW_CFA_def_cfa_register
:
1010 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1011 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1012 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1015 case DW_CFA_def_cfa_offset
:
1016 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1017 fs
->regs
.cfa_offset
= utmp
;
1018 /* cfa_how deliberately not set. */
1021 case DW_CFA_def_cfa_expression
:
1022 fs
->regs
.cfa_exp
= insn_ptr
;
1023 fs
->regs
.cfa_how
= CFA_EXP
;
1024 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1028 case DW_CFA_expression
:
1029 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1030 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_EXP
;
1031 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
1032 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1037 case DW_CFA_offset_extended_sf
:
1038 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1039 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1040 offset
= stmp
* fs
->data_align
;
1041 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1043 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1046 case DW_CFA_def_cfa_sf
:
1047 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1048 fs
->regs
.cfa_reg
= (_Unwind_Word
)utmp
;
1049 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1050 fs
->regs
.cfa_offset
= (_Unwind_Sword
)stmp
;
1051 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
1052 fs
->regs
.cfa_offset
*= fs
->data_align
;
1055 case DW_CFA_def_cfa_offset_sf
:
1056 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1057 fs
->regs
.cfa_offset
= (_Unwind_Sword
)stmp
;
1058 fs
->regs
.cfa_offset
*= fs
->data_align
;
1059 /* cfa_how deliberately not set. */
1062 case DW_CFA_val_offset
:
1063 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1064 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1065 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
1066 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1067 = REG_SAVED_VAL_OFFSET
;
1068 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1071 case DW_CFA_val_offset_sf
:
1072 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1073 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
1074 offset
= stmp
* fs
->data_align
;
1075 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1076 = REG_SAVED_VAL_OFFSET
;
1077 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1080 case DW_CFA_val_expression
:
1081 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1082 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1083 = REG_SAVED_VAL_EXP
;
1084 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
1085 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1089 case DW_CFA_GNU_window_save
:
1090 /* ??? Hardcoded for SPARC register window configuration. */
1091 for (reg
= 16; reg
< 32; ++reg
)
1093 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
1094 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
1098 case DW_CFA_GNU_args_size
:
1099 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1100 context
->args_size
= (_Unwind_Word
)utmp
;
1103 case DW_CFA_GNU_negative_offset_extended
:
1104 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1105 older PowerPC code. */
1106 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1107 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1108 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
1109 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1111 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= -offset
;
1120 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1121 its caller and decode it into FS. This function also sets the
1122 args_size and lsda members of CONTEXT, as they are really information
1123 about the caller's frame. */
1125 static _Unwind_Reason_Code
1126 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1128 const struct dwarf_fde
*fde
;
1129 const struct dwarf_cie
*cie
;
1130 const unsigned char *aug
, *insn
, *end
;
1132 memset (fs
, 0, sizeof (*fs
));
1133 context
->args_size
= 0;
1136 if (context
->ra
== 0)
1137 return _URC_END_OF_STACK
;
1139 fde
= _Unwind_Find_FDE (context
->ra
+ _Unwind_IsSignalFrame (context
) - 1,
1143 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1144 /* Couldn't find frame unwind info for this function. Try a
1145 target-specific fallback mechanism. This will necessarily
1146 not provide a personality routine or LSDA. */
1147 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
1149 return _URC_END_OF_STACK
;
1153 fs
->pc
= context
->bases
.func
;
1155 cie
= get_cie (fde
);
1156 insn
= extract_cie_info (cie
, context
, fs
);
1158 /* CIE contained unknown augmentation. */
1159 return _URC_FATAL_PHASE1_ERROR
;
1161 /* First decode all the insns in the CIE. */
1162 end
= (const unsigned char *) next_fde ((const struct dwarf_fde
*) cie
);
1163 execute_cfa_program (insn
, end
, context
, fs
);
1165 /* Locate augmentation for the fde. */
1166 aug
= (const unsigned char *) fde
+ sizeof (*fde
);
1167 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1172 aug
= read_uleb128 (aug
, &i
);
1175 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1179 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1180 context
->lsda
= (void *) lsda
;
1183 /* Then the insns in the FDE up to our target PC. */
1186 end
= (const unsigned char *) next_fde (fde
);
1187 execute_cfa_program (insn
, end
, context
, fs
);
1189 return _URC_NO_REASON
;
1192 typedef struct frame_state
1198 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1199 unsigned short cfa_reg
;
1200 unsigned short retaddr_column
;
1201 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1204 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1206 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1207 a given PC_TARGET. The caller should allocate a local variable of
1208 `struct frame_state' and pass its address to STATE_IN. */
1210 struct frame_state
*
1211 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1213 struct _Unwind_Context context
;
1214 _Unwind_FrameState fs
;
1217 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1218 context
.flags
= EXTENDED_CONTEXT_BIT
;
1219 context
.ra
= pc_target
+ 1;
1221 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1224 /* We have no way to pass a location expression for the CFA to our
1225 caller. It wouldn't understand it anyway. */
1226 if (fs
.regs
.cfa_how
== CFA_EXP
)
1229 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1231 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1232 switch (state_in
->saved
[reg
])
1235 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1237 case REG_SAVED_OFFSET
:
1238 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1241 state_in
->reg_or_offset
[reg
] = 0;
1246 state_in
->cfa_offset
= fs
.regs
.cfa_offset
;
1247 state_in
->cfa_reg
= fs
.regs
.cfa_reg
;
1248 state_in
->retaddr_column
= fs
.retaddr_column
;
1249 state_in
->args_size
= context
.args_size
;
1250 state_in
->eh_ptr
= fs
.eh_ptr
;
1255 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1258 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1259 _Unwind_SpTmp
*tmp_sp
)
1261 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1263 if (size
== sizeof(_Unwind_Ptr
))
1264 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1267 gcc_assert (size
== sizeof(_Unwind_Word
));
1268 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1270 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1274 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1276 struct _Unwind_Context orig_context
= *context
;
1280 #ifdef EH_RETURN_STACKADJ_RTX
1281 /* Special handling here: Many machines do not use a frame pointer,
1282 and track the CFA only through offsets from the stack pointer from
1283 one frame to the next. In this case, the stack pointer is never
1284 stored, so it has no saved address in the context. What we do
1285 have is the CFA from the previous stack frame.
1287 In very special situations (such as unwind info for signal return),
1288 there may be location expressions that use the stack pointer as well.
1290 Do this conditionally for one frame. This allows the unwind info
1291 for one frame to save a copy of the stack pointer from the previous
1292 frame, and be able to use much easier CFA mechanisms to do it.
1293 Always zap the saved stack pointer value for the next frame; carrying
1294 the value over from one frame to another doesn't make sense. */
1296 _Unwind_SpTmp tmp_sp
;
1298 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1299 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1300 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1303 /* Compute this frame's CFA. */
1304 switch (fs
->regs
.cfa_how
)
1306 case CFA_REG_OFFSET
:
1307 cfa
= _Unwind_GetPtr (&orig_context
, fs
->regs
.cfa_reg
);
1308 cfa
+= fs
->regs
.cfa_offset
;
1313 const unsigned char *exp
= fs
->regs
.cfa_exp
;
1316 exp
= read_uleb128 (exp
, &len
);
1317 cfa
= (void *) (_Unwind_Ptr
)
1318 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1327 /* Compute the addresses of all registers saved in this frame. */
1328 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1329 switch (fs
->regs
.reg
[i
].how
)
1335 case REG_SAVED_OFFSET
:
1336 _Unwind_SetGRPtr (context
, i
,
1337 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1341 if (_Unwind_GRByValue (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
))
1342 _Unwind_SetGRValue (context
, i
,
1343 _Unwind_GetGR (&orig_context
,
1344 fs
->regs
.reg
[i
].loc
.reg
));
1346 _Unwind_SetGRPtr (context
, i
,
1347 _Unwind_GetGRPtr (&orig_context
,
1348 fs
->regs
.reg
[i
].loc
.reg
));
1353 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1357 exp
= read_uleb128 (exp
, &len
);
1358 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1360 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1364 case REG_SAVED_VAL_OFFSET
:
1365 _Unwind_SetGRValue (context
, i
,
1366 (_Unwind_Internal_Ptr
)
1367 (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1370 case REG_SAVED_VAL_EXP
:
1372 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1376 exp
= read_uleb128 (exp
, &len
);
1377 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1379 _Unwind_SetGRValue (context
, i
, val
);
1384 _Unwind_SetSignalFrame (context
, fs
->signal_frame
);
1386 #ifdef MD_FROB_UPDATE_CONTEXT
1387 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1391 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1392 of its caller. Update CONTEXT to refer to the caller as well. Note
1393 that the args_size and lsda members are not updated here, but later in
1394 uw_frame_state_for. */
1397 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1399 uw_update_context_1 (context
, fs
);
1401 /* In general this unwinder doesn't make any distinction between
1402 undefined and same_value rule. Call-saved registers are assumed
1403 to have same_value rule by default and explicit undefined
1404 rule is handled like same_value. The only exception is
1405 DW_CFA_undefined on retaddr_column which is supposed to
1406 mark outermost frame in DWARF 3. */
1407 if (fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (fs
->retaddr_column
)].how
1409 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1413 /* Compute the return address now, since the return address column
1414 can change from frame to frame. */
1415 context
->ra
= __builtin_extract_return_addr
1416 (_Unwind_GetPtr (context
, fs
->retaddr_column
));
1420 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1422 uw_update_context (context
, fs
);
1425 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1426 level will be the return address and the CFA. */
1428 #define uw_init_context(CONTEXT) \
1431 /* Do any necessary initialization to access arbitrary stack frames. \
1432 On the SPARC, this means flushing the register windows. */ \
1433 __builtin_unwind_init (); \
1434 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1435 __builtin_return_address (0)); \
1440 init_dwarf_reg_size_table (void)
1442 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1445 static void __attribute__((noinline
))
1446 uw_init_context_1 (struct _Unwind_Context
*context
,
1447 void *outer_cfa
, void *outer_ra
)
1449 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1450 _Unwind_FrameState fs
;
1451 _Unwind_SpTmp sp_slot
;
1452 _Unwind_Reason_Code code
;
1454 memset (context
, 0, sizeof (struct _Unwind_Context
));
1456 context
->flags
= EXTENDED_CONTEXT_BIT
;
1458 code
= uw_frame_state_for (context
, &fs
);
1459 gcc_assert (code
== _URC_NO_REASON
);
1463 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1464 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1465 && dwarf_reg_size_table
[0] == 0)
1466 init_dwarf_reg_size_table ();
1469 if (dwarf_reg_size_table
[0] == 0)
1470 init_dwarf_reg_size_table ();
1473 /* Force the frame state to use the known cfa value. */
1474 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1475 fs
.regs
.cfa_how
= CFA_REG_OFFSET
;
1476 fs
.regs
.cfa_reg
= __builtin_dwarf_sp_column ();
1477 fs
.regs
.cfa_offset
= 0;
1479 uw_update_context_1 (context
, &fs
);
1481 /* If the return address column was saved in a register in the
1482 initialization context, then we can't see it in the given
1483 call frame data. So have the initialization context tell us. */
1484 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1487 static void _Unwind_DebugHook (void *, void *)
1488 __attribute__ ((__noinline__
, __used__
, __noclone__
));
1490 /* This function is called during unwinding. It is intended as a hook
1491 for a debugger to intercept exceptions. CFA is the CFA of the
1492 target frame. HANDLER is the PC to which control will be
1495 _Unwind_DebugHook (void *cfa
__attribute__ ((__unused__
)),
1496 void *handler
__attribute__ ((__unused__
)))
1498 /* We only want to use stap probes starting with v3. Earlier
1499 versions added too much startup cost. */
1500 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1501 STAP_PROBE2 (libgcc
, unwind
, cfa
, handler
);
1507 /* Install TARGET into CURRENT so that we can return to it. This is a
1508 macro because __builtin_eh_return must be invoked in the context of
1511 #define uw_install_context(CURRENT, TARGET) \
1514 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1515 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1516 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1517 __builtin_eh_return (offset, handler); \
1522 uw_install_context_1 (struct _Unwind_Context
*current
,
1523 struct _Unwind_Context
*target
)
1526 _Unwind_SpTmp sp_slot
;
1528 /* If the target frame does not have a saved stack pointer,
1529 then set up the target's CFA. */
1530 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1531 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1533 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1535 void *c
= current
->reg
[i
];
1536 void *t
= target
->reg
[i
];
1538 gcc_assert (current
->by_value
[i
] == 0);
1539 if (target
->by_value
[i
] && c
)
1543 if (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Word
))
1545 w
= (_Unwind_Internal_Ptr
) t
;
1546 memcpy (c
, &w
, sizeof (_Unwind_Word
));
1550 gcc_assert (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Ptr
));
1551 p
= (_Unwind_Internal_Ptr
) t
;
1552 memcpy (c
, &p
, sizeof (_Unwind_Ptr
));
1555 else if (t
&& c
&& t
!= c
)
1556 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1559 /* If the current frame doesn't have a saved stack pointer, then we
1560 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1561 pointer value reloaded. */
1562 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1566 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1568 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1569 if (STACK_GROWS_DOWNWARD
)
1570 return target_cfa
- current
->cfa
+ target
->args_size
;
1572 return current
->cfa
- target_cfa
- target
->args_size
;
1577 static inline _Unwind_Ptr
1578 uw_identify_context (struct _Unwind_Context
*context
)
1580 /* The CFA is not sufficient to disambiguate the context of a function
1581 interrupted by a signal before establishing its frame and the context
1582 of the signal itself. */
1583 if (STACK_GROWS_DOWNWARD
)
1584 return _Unwind_GetCFA (context
) - _Unwind_IsSignalFrame (context
);
1586 return _Unwind_GetCFA (context
) + _Unwind_IsSignalFrame (context
);
1590 #include "unwind.inc"
1592 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1593 alias (_Unwind_Backtrace
);
1594 alias (_Unwind_DeleteException
);
1595 alias (_Unwind_FindEnclosingFunction
);
1596 alias (_Unwind_ForcedUnwind
);
1597 alias (_Unwind_GetDataRelBase
);
1598 alias (_Unwind_GetTextRelBase
);
1599 alias (_Unwind_GetCFA
);
1600 alias (_Unwind_GetGR
);
1601 alias (_Unwind_GetIP
);
1602 alias (_Unwind_GetLanguageSpecificData
);
1603 alias (_Unwind_GetRegionStart
);
1604 alias (_Unwind_RaiseException
);
1605 alias (_Unwind_Resume
);
1606 alias (_Unwind_Resume_or_Rethrow
);
1607 alias (_Unwind_SetGR
);
1608 alias (_Unwind_SetIP
);
1611 #endif /* !USING_SJLJ_EXCEPTIONS */