1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
27 #include "coretypes.h"
29 #include "libgcc_tm.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"
45 #ifndef __USING_SJLJ_EXCEPTIONS__
47 #ifndef __LIBGCC_STACK_GROWS_DOWNWARD__
48 #define __LIBGCC_STACK_GROWS_DOWNWARD__ 0
50 #undef __LIBGCC_STACK_GROWS_DOWNWARD__
51 #define __LIBGCC_STACK_GROWS_DOWNWARD__ 1
54 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
55 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
56 #define PRE_GCC3_DWARF_FRAME_REGISTERS __LIBGCC_DWARF_FRAME_REGISTERS__
59 /* ??? For the public function interfaces, we tend to gcc_assert that the
60 column numbers are in range. For the dwarf2 unwind info this does happen,
61 although so far in a case that doesn't actually matter.
63 See PR49146, in which a call from x86_64 ms abi to x86_64 unix abi stores
64 the call-saved xmm registers and annotates them. We havn't bothered
65 providing support for the xmm registers for the x86_64 port primarily
66 because the 64-bit windows targets don't use dwarf2 unwind, using sjlj or
67 SEH instead. Adding the support for unix targets would generally be a
68 waste. However, some runtime libraries supplied with ICC do contain such
69 an unorthodox transition, as well as the unwind info to match. This loss
70 of register restoration doesn't matter in practice, because the exception
71 is caught in the native unix abi, where all of the xmm registers are
74 Ideally, we'd record some bit to notice when we're failing to restore some
75 register recorded in the unwind info, but to do that we need annotation on
76 the unix->ms abi edge, so that we know when the register data may be
77 discarded. And since this edge is also within the ICC library, we're
78 unlikely to be able to get the new annotation.
80 Barring a magic solution to restore the ms abi defined 128-bit xmm registers
81 (as distictly opposed to the full runtime width) without causing extra
82 overhead for normal unix abis, the best solution seems to be to simply
83 ignore unwind data for unknown columns. */
85 #define UNWIND_COLUMN_IN_RANGE(x) \
86 __builtin_expect((x) <= __LIBGCC_DWARF_FRAME_REGISTERS__, 1)
88 #ifdef REG_VALUE_IN_UNWIND_CONTEXT
89 typedef _Unwind_Word _Unwind_Context_Reg_Val
;
91 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
92 #define ASSUME_EXTENDED_UNWIND_CONTEXT 1
95 static inline _Unwind_Word
96 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val
)
101 static inline _Unwind_Context_Reg_Val
102 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val
)
107 typedef void *_Unwind_Context_Reg_Val
;
109 static inline _Unwind_Word
110 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val
)
112 return (_Unwind_Word
) (_Unwind_Internal_Ptr
) val
;
115 static inline _Unwind_Context_Reg_Val
116 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val
)
118 return (_Unwind_Context_Reg_Val
) (_Unwind_Internal_Ptr
) val
;
122 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
123 #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
126 /* This is the register and unwind state for a particular frame. This
127 provides the information necessary to unwind up past a frame and return
129 struct _Unwind_Context
131 _Unwind_Context_Reg_Val reg
[__LIBGCC_DWARF_FRAME_REGISTERS__
+1];
135 struct dwarf_eh_bases bases
;
136 /* Signal frame context. */
137 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
138 /* Context which has version/args_size/by_value fields. */
139 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
141 /* 0 for now, can be increased when further fields are added to
142 struct _Unwind_Context. */
143 _Unwind_Word version
;
144 _Unwind_Word args_size
;
145 char by_value
[__LIBGCC_DWARF_FRAME_REGISTERS__
+1];
148 /* Byte size of every register managed by these routines. */
149 static unsigned char dwarf_reg_size_table
[__LIBGCC_DWARF_FRAME_REGISTERS__
+1];
152 /* Read unaligned data from the instruction buffer. */
157 unsigned u2
__attribute__ ((mode (HI
)));
158 unsigned u4
__attribute__ ((mode (SI
)));
159 unsigned u8
__attribute__ ((mode (DI
)));
160 signed s2
__attribute__ ((mode (HI
)));
161 signed s4
__attribute__ ((mode (SI
)));
162 signed s8
__attribute__ ((mode (DI
)));
163 } __attribute__ ((packed
));
165 static void uw_update_context (struct _Unwind_Context
*, _Unwind_FrameState
*);
166 static _Unwind_Reason_Code
uw_frame_state_for (struct _Unwind_Context
*,
167 _Unwind_FrameState
*);
170 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
173 read_1u (const void *p
) { return *(const unsigned char *) p
; }
176 read_1s (const void *p
) { return *(const signed char *) p
; }
179 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
182 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
184 static inline unsigned int
185 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
188 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
190 static inline unsigned long
191 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
193 static inline unsigned long
194 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
196 static inline _Unwind_Word
197 _Unwind_IsSignalFrame (struct _Unwind_Context
*context
)
199 return (context
->flags
& SIGNAL_FRAME_BIT
) ? 1 : 0;
203 _Unwind_SetSignalFrame (struct _Unwind_Context
*context
, int val
)
206 context
->flags
|= SIGNAL_FRAME_BIT
;
208 context
->flags
&= ~SIGNAL_FRAME_BIT
;
211 static inline _Unwind_Word
212 _Unwind_IsExtendedContext (struct _Unwind_Context
*context
)
214 return (ASSUME_EXTENDED_UNWIND_CONTEXT
215 || (context
->flags
& EXTENDED_CONTEXT_BIT
));
218 /* Get the value of register REGNO as saved in CONTEXT. */
221 _Unwind_GetGR (struct _Unwind_Context
*context
, int regno
)
224 _Unwind_Context_Reg_Val val
;
226 #ifdef DWARF_ZERO_REG
227 if (regno
== DWARF_ZERO_REG
)
231 index
= DWARF_REG_TO_UNWIND_COLUMN (regno
);
232 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
233 size
= dwarf_reg_size_table
[index
];
234 val
= context
->reg
[index
];
236 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
237 return _Unwind_Get_Unwind_Word (val
);
239 #ifdef DWARF_LAZY_REGISTER_VALUE
242 if (DWARF_LAZY_REGISTER_VALUE (regno
, &value
))
247 /* This will segfault if the register hasn't been saved. */
248 if (size
== sizeof(_Unwind_Ptr
))
249 return * (_Unwind_Ptr
*) (_Unwind_Internal_Ptr
) val
;
252 gcc_assert (size
== sizeof(_Unwind_Word
));
253 return * (_Unwind_Word
*) (_Unwind_Internal_Ptr
) val
;
258 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
260 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
263 /* Get the value of the CFA as saved in CONTEXT. */
266 _Unwind_GetCFA (struct _Unwind_Context
*context
)
268 return (_Unwind_Ptr
) context
->cfa
;
271 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
274 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
279 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
280 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
281 size
= dwarf_reg_size_table
[index
];
283 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
285 context
->reg
[index
] = _Unwind_Get_Unwind_Context_Reg_Val (val
);
289 ptr
= (void *) (_Unwind_Internal_Ptr
) context
->reg
[index
];
291 if (size
== sizeof(_Unwind_Ptr
))
292 * (_Unwind_Ptr
*) ptr
= val
;
295 gcc_assert (size
== sizeof(_Unwind_Word
));
296 * (_Unwind_Word
*) ptr
= val
;
300 /* Get the pointer to a register INDEX as saved in CONTEXT. */
303 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
305 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
306 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
307 return &context
->reg
[index
];
308 return (void *) (_Unwind_Internal_Ptr
) context
->reg
[index
];
311 /* Set the pointer to a register INDEX as saved in CONTEXT. */
314 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
316 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
317 if (_Unwind_IsExtendedContext (context
))
318 context
->by_value
[index
] = 0;
319 context
->reg
[index
] = (_Unwind_Context_Reg_Val
) (_Unwind_Internal_Ptr
) p
;
322 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
325 _Unwind_SetGRValue (struct _Unwind_Context
*context
, int index
,
328 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
329 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
330 /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
331 gcc_assert (dwarf_reg_size_table
[index
] <= sizeof (_Unwind_Context_Reg_Val
));
333 context
->by_value
[index
] = 1;
334 context
->reg
[index
] = _Unwind_Get_Unwind_Context_Reg_Val (val
);
337 /* Return nonzero if register INDEX is stored by value rather than
341 _Unwind_GRByValue (struct _Unwind_Context
*context
, int index
)
343 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
344 return context
->by_value
[index
];
347 /* Retrieve the return address for CONTEXT. */
350 _Unwind_GetIP (struct _Unwind_Context
*context
)
352 return (_Unwind_Ptr
) context
->ra
;
355 /* Retrieve the return address and flag whether that IP is before
356 or after first not yet fully executed instruction. */
359 _Unwind_GetIPInfo (struct _Unwind_Context
*context
, int *ip_before_insn
)
361 *ip_before_insn
= _Unwind_IsSignalFrame (context
);
362 return (_Unwind_Ptr
) context
->ra
;
365 /* Overwrite the return address for CONTEXT with VAL. */
368 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
370 context
->ra
= (void *) val
;
374 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
376 return context
->lsda
;
380 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
382 return (_Unwind_Ptr
) context
->bases
.func
;
386 _Unwind_FindEnclosingFunction (void *pc
)
388 struct dwarf_eh_bases bases
;
389 const struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
398 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
400 return (_Unwind_Ptr
) context
->bases
.dbase
;
404 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
406 return (_Unwind_Ptr
) context
->bases
.tbase
;
410 #include "md-unwind-support.h"
412 /* Extract any interesting information from the CIE for the translation
413 unit F belongs to. Return a pointer to the byte after the augmentation,
414 or NULL if we encountered an undecipherable augmentation. */
416 static const unsigned char *
417 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
418 _Unwind_FrameState
*fs
)
420 const unsigned char *aug
= cie
->augmentation
;
421 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
422 const unsigned char *ret
= NULL
;
426 /* g++ v2 "eh" has pointer immediately following augmentation string,
427 so it must be handled first. */
428 if (aug
[0] == 'e' && aug
[1] == 'h')
430 fs
->eh_ptr
= read_pointer (p
);
431 p
+= sizeof (void *);
435 /* After the augmentation resp. pointer for "eh" augmentation
436 follows for CIE version >= 4 address size byte and
437 segment size byte. */
438 if (__builtin_expect (cie
->version
>= 4, 0))
440 if (p
[0] != sizeof (void *) || p
[1] != 0)
444 /* Immediately following this are the code and
445 data alignment and return address column. */
446 p
= read_uleb128 (p
, &utmp
);
447 fs
->code_align
= (_Unwind_Word
)utmp
;
448 p
= read_sleb128 (p
, &stmp
);
449 fs
->data_align
= (_Unwind_Sword
)stmp
;
450 if (cie
->version
== 1)
451 fs
->retaddr_column
= *p
++;
454 p
= read_uleb128 (p
, &utmp
);
455 fs
->retaddr_column
= (_Unwind_Word
)utmp
;
457 fs
->lsda_encoding
= DW_EH_PE_omit
;
459 /* If the augmentation starts with 'z', then a uleb128 immediately
460 follows containing the length of the augmentation field following
464 p
= read_uleb128 (p
, &utmp
);
471 /* Iterate over recognized augmentation subsequences. */
474 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
477 fs
->lsda_encoding
= *p
++;
481 /* "R" indicates a byte indicating how FDE addresses are encoded. */
482 else if (aug
[0] == 'R')
484 fs
->fde_encoding
= *p
++;
488 /* "P" indicates a personality routine in the CIE augmentation. */
489 else if (aug
[0] == 'P')
491 _Unwind_Ptr personality
;
493 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
494 fs
->personality
= (_Unwind_Personality_Fn
) personality
;
498 /* "S" indicates a signal frame. */
499 else if (aug
[0] == 'S')
501 fs
->signal_frame
= 1;
504 /* aarch64 B-key pointer authentication. */
505 else if (aug
[0] == 'B')
510 /* Otherwise we have an unknown augmentation string.
511 Bail unless we saw a 'z' prefix. */
516 return ret
? ret
: p
;
520 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
521 onto the stack to start. */
524 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
525 struct _Unwind_Context
*context
, _Unwind_Word initial
)
527 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
533 while (op_ptr
< op_end
)
535 enum dwarf_location_atom op
= *op_ptr
++;
537 _uleb128_t reg
, utmp
;
538 _sleb128_t offset
, stmp
;
574 result
= op
- DW_OP_lit0
;
578 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
579 op_ptr
+= sizeof (void *);
582 case DW_OP_GNU_encoded_addr
:
585 op_ptr
= read_encoded_value (context
, *op_ptr
, op_ptr
+1, &presult
);
591 result
= read_1u (op_ptr
);
595 result
= read_1s (op_ptr
);
599 result
= read_2u (op_ptr
);
603 result
= read_2s (op_ptr
);
607 result
= read_4u (op_ptr
);
611 result
= read_4s (op_ptr
);
615 result
= read_8u (op_ptr
);
619 result
= read_8s (op_ptr
);
623 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
624 result
= (_Unwind_Word
)utmp
;
627 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
628 result
= (_Unwind_Sword
)stmp
;
663 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
666 op_ptr
= read_uleb128 (op_ptr
, ®
);
667 result
= _Unwind_GetGR (context
, reg
);
702 op_ptr
= read_sleb128 (op_ptr
, &offset
);
703 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
706 op_ptr
= read_uleb128 (op_ptr
, ®
);
707 op_ptr
= read_sleb128 (op_ptr
, &offset
);
708 result
= _Unwind_GetGR (context
, reg
) + (_Unwind_Word
)offset
;
712 gcc_assert (stack_elt
);
713 result
= stack
[stack_elt
- 1];
717 gcc_assert (stack_elt
);
723 gcc_assert (offset
< stack_elt
- 1);
724 result
= stack
[stack_elt
- 1 - offset
];
728 gcc_assert (stack_elt
>= 2);
729 result
= stack
[stack_elt
- 2];
735 gcc_assert (stack_elt
>= 2);
736 t
= stack
[stack_elt
- 1];
737 stack
[stack_elt
- 1] = stack
[stack_elt
- 2];
738 stack
[stack_elt
- 2] = t
;
744 _Unwind_Word t1
, t2
, t3
;
746 gcc_assert (stack_elt
>= 3);
747 t1
= stack
[stack_elt
- 1];
748 t2
= stack
[stack_elt
- 2];
749 t3
= stack
[stack_elt
- 3];
750 stack
[stack_elt
- 1] = t2
;
751 stack
[stack_elt
- 2] = t3
;
752 stack
[stack_elt
- 3] = t1
;
757 case DW_OP_deref_size
:
761 case DW_OP_plus_uconst
:
762 /* Unary operations. */
763 gcc_assert (stack_elt
);
766 result
= stack
[stack_elt
];
772 void *ptr
= (void *) (_Unwind_Ptr
) result
;
773 result
= (_Unwind_Ptr
) read_pointer (ptr
);
777 case DW_OP_deref_size
:
779 void *ptr
= (void *) (_Unwind_Ptr
) result
;
783 result
= read_1u (ptr
);
786 result
= read_2u (ptr
);
789 result
= read_4u (ptr
);
792 result
= read_8u (ptr
);
801 if ((_Unwind_Sword
) result
< 0)
810 case DW_OP_plus_uconst
:
811 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
812 result
+= (_Unwind_Word
)utmp
;
838 /* Binary operations. */
839 _Unwind_Word first
, second
;
840 gcc_assert (stack_elt
>= 2);
843 second
= stack
[stack_elt
];
844 first
= stack
[stack_elt
+ 1];
849 result
= second
& first
;
852 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
855 result
= second
- first
;
858 result
= second
% first
;
861 result
= second
* first
;
864 result
= second
| first
;
867 result
= second
+ first
;
870 result
= second
<< first
;
873 result
= second
>> first
;
876 result
= (_Unwind_Sword
) second
>> first
;
879 result
= second
^ first
;
882 result
= (_Unwind_Sword
) second
<= (_Unwind_Sword
) first
;
885 result
= (_Unwind_Sword
) second
>= (_Unwind_Sword
) first
;
888 result
= (_Unwind_Sword
) second
== (_Unwind_Sword
) first
;
891 result
= (_Unwind_Sword
) second
< (_Unwind_Sword
) first
;
894 result
= (_Unwind_Sword
) second
> (_Unwind_Sword
) first
;
897 result
= (_Unwind_Sword
) second
!= (_Unwind_Sword
) first
;
907 offset
= read_2s (op_ptr
);
913 gcc_assert (stack_elt
);
916 offset
= read_2s (op_ptr
);
918 if (stack
[stack_elt
] != 0)
929 /* Most things push a result value. */
930 gcc_assert ((size_t) stack_elt
< sizeof(stack
)/sizeof(*stack
));
931 stack
[stack_elt
++] = result
;
935 /* We were executing this program to get a value. It should be
937 gcc_assert (stack_elt
);
939 return stack
[stack_elt
];
943 /* Decode DWARF 2 call frame information. Takes pointers the
944 instruction sequence to decode, current register information and
945 CIE info, and the PC range to evaluate. */
947 static void __attribute__ ((__noinline__
))
948 execute_cfa_program_generic (const unsigned char *insn_ptr
,
949 const unsigned char *insn_end
,
950 struct _Unwind_Context
*context
,
951 _Unwind_FrameState
*fs
)
953 #define DATA_ALIGN fs->data_align
954 #define CODE_ALIGN fs->code_align
955 #include "unwind-dw2-execute_cfa.h"
959 execute_cfa_program_specialized (const unsigned char *insn_ptr
,
960 const unsigned char *insn_end
,
961 struct _Unwind_Context
*context
,
962 _Unwind_FrameState
*fs
)
964 #define DATA_ALIGN __LIBGCC_DWARF_CIE_DATA_ALIGNMENT__
965 /* GCC always uses 1 even on architectures with a fixed instruction
968 #include "unwind-dw2-execute_cfa.h"
972 execute_cfa_program (const unsigned char *insn_ptr
,
973 const unsigned char *insn_end
,
974 struct _Unwind_Context
*context
,
975 _Unwind_FrameState
*fs
)
977 if (fs
->data_align
== __LIBGCC_DWARF_CIE_DATA_ALIGNMENT__
978 && fs
->code_align
== 1)
979 execute_cfa_program_specialized (insn_ptr
, insn_end
, context
, fs
);
981 execute_cfa_program_generic (insn_ptr
, insn_end
, context
, fs
);
985 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
986 its caller and decode it into FS. This function also sets the
987 args_size and lsda members of CONTEXT, as they are really information
988 about the caller's frame. */
990 static _Unwind_Reason_Code
991 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
993 const struct dwarf_fde
*fde
;
994 const struct dwarf_cie
*cie
;
995 const unsigned char *aug
, *insn
, *end
;
997 memset (&fs
->regs
.how
[0], 0,
998 sizeof (*fs
) - offsetof (_Unwind_FrameState
, regs
.how
[0]));
999 context
->args_size
= 0;
1002 if (context
->ra
== 0)
1003 return _URC_END_OF_STACK
;
1005 fde
= _Unwind_Find_FDE (context
->ra
+ _Unwind_IsSignalFrame (context
) - 1,
1009 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1010 /* Couldn't find frame unwind info for this function. Try a
1011 target-specific fallback mechanism. This will necessarily
1012 not provide a personality routine or LSDA. */
1013 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
1015 return _URC_END_OF_STACK
;
1019 fs
->pc
= context
->bases
.func
;
1021 cie
= get_cie (fde
);
1022 insn
= extract_cie_info (cie
, context
, fs
);
1024 /* CIE contained unknown augmentation. */
1025 return _URC_FATAL_PHASE1_ERROR
;
1027 /* First decode all the insns in the CIE. */
1028 end
= (const unsigned char *) next_fde ((const struct dwarf_fde
*) cie
);
1029 execute_cfa_program (insn
, end
, context
, fs
);
1031 /* Locate augmentation for the fde. */
1032 aug
= (const unsigned char *) fde
+ sizeof (*fde
);
1033 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1038 aug
= read_uleb128 (aug
, &i
);
1041 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1045 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1046 context
->lsda
= (void *) lsda
;
1049 /* Then the insns in the FDE up to our target PC. */
1052 end
= (const unsigned char *) next_fde (fde
);
1053 execute_cfa_program (insn
, end
, context
, fs
);
1055 return _URC_NO_REASON
;
1058 typedef struct frame_state
1064 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1065 unsigned short cfa_reg
;
1066 unsigned short retaddr_column
;
1067 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1070 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1072 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1073 a given PC_TARGET. The caller should allocate a local variable of
1074 `struct frame_state' and pass its address to STATE_IN. */
1076 struct frame_state
*
1077 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1079 struct _Unwind_Context context
;
1080 _Unwind_FrameState fs
;
1083 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1084 if (!ASSUME_EXTENDED_UNWIND_CONTEXT
)
1085 context
.flags
= EXTENDED_CONTEXT_BIT
;
1086 context
.ra
= pc_target
+ 1;
1088 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1091 /* We have no way to pass a location expression for the CFA to our
1092 caller. It wouldn't understand it anyway. */
1093 if (fs
.regs
.cfa_how
== CFA_EXP
)
1096 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1098 state_in
->saved
[reg
] = fs
.regs
.how
[reg
];
1099 switch (state_in
->saved
[reg
])
1102 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1104 case REG_SAVED_OFFSET
:
1105 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1108 state_in
->reg_or_offset
[reg
] = 0;
1113 state_in
->cfa_offset
= fs
.regs
.cfa_offset
;
1114 state_in
->cfa_reg
= fs
.regs
.cfa_reg
;
1115 state_in
->retaddr_column
= fs
.retaddr_column
;
1116 state_in
->args_size
= context
.args_size
;
1117 state_in
->eh_ptr
= fs
.eh_ptr
;
1122 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1125 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1126 _Unwind_SpTmp
*tmp_sp
)
1128 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1130 if (size
== sizeof(_Unwind_Ptr
))
1131 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1134 gcc_assert (size
== sizeof(_Unwind_Word
));
1135 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1137 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1141 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1143 struct _Unwind_Context orig_context
= *context
;
1147 #ifdef __LIBGCC_EH_RETURN_STACKADJ_RTX__
1148 /* Special handling here: Many machines do not use a frame pointer,
1149 and track the CFA only through offsets from the stack pointer from
1150 one frame to the next. In this case, the stack pointer is never
1151 stored, so it has no saved address in the context. What we do
1152 have is the CFA from the previous stack frame.
1154 In very special situations (such as unwind info for signal return),
1155 there may be location expressions that use the stack pointer as well.
1157 Do this conditionally for one frame. This allows the unwind info
1158 for one frame to save a copy of the stack pointer from the previous
1159 frame, and be able to use much easier CFA mechanisms to do it.
1160 Always zap the saved stack pointer value for the next frame; carrying
1161 the value over from one frame to another doesn't make sense. */
1163 _Unwind_SpTmp tmp_sp
;
1165 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1166 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1167 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1170 /* Compute this frame's CFA. */
1171 switch (fs
->regs
.cfa_how
)
1173 case CFA_REG_OFFSET
:
1174 cfa
= _Unwind_GetPtr (&orig_context
, fs
->regs
.cfa_reg
);
1175 cfa
+= fs
->regs
.cfa_offset
;
1180 const unsigned char *exp
= fs
->regs
.cfa_exp
;
1183 exp
= read_uleb128 (exp
, &len
);
1184 cfa
= (void *) (_Unwind_Ptr
)
1185 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1194 /* Compute the addresses of all registers saved in this frame. */
1195 for (i
= 0; i
< __LIBGCC_DWARF_FRAME_REGISTERS__
+ 1; ++i
)
1196 switch (fs
->regs
.how
[i
])
1200 case REG_UNSAVED_ARCHEXT
:
1203 case REG_SAVED_OFFSET
:
1204 _Unwind_SetGRPtr (context
, i
,
1205 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1209 if (_Unwind_GRByValue (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
))
1210 _Unwind_SetGRValue (context
, i
,
1211 _Unwind_GetGR (&orig_context
,
1212 fs
->regs
.reg
[i
].loc
.reg
));
1214 _Unwind_SetGRPtr (context
, i
,
1215 _Unwind_GetGRPtr (&orig_context
,
1216 fs
->regs
.reg
[i
].loc
.reg
));
1221 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1225 exp
= read_uleb128 (exp
, &len
);
1226 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1228 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1232 case REG_SAVED_VAL_OFFSET
:
1233 _Unwind_SetGRValue (context
, i
,
1234 (_Unwind_Internal_Ptr
)
1235 (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1238 case REG_SAVED_VAL_EXP
:
1240 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1244 exp
= read_uleb128 (exp
, &len
);
1245 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1247 _Unwind_SetGRValue (context
, i
, val
);
1252 _Unwind_SetSignalFrame (context
, fs
->signal_frame
);
1254 #ifdef MD_FROB_UPDATE_CONTEXT
1255 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1259 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1260 of its caller. Update CONTEXT to refer to the caller as well. Note
1261 that the args_size and lsda members are not updated here, but later in
1262 uw_frame_state_for. */
1265 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1267 uw_update_context_1 (context
, fs
);
1269 /* In general this unwinder doesn't make any distinction between
1270 undefined and same_value rule. Call-saved registers are assumed
1271 to have same_value rule by default and explicit undefined
1272 rule is handled like same_value. The only exception is
1273 DW_CFA_undefined on retaddr_column which is supposed to
1274 mark outermost frame in DWARF 3. */
1275 if (fs
->regs
.how
[DWARF_REG_TO_UNWIND_COLUMN (fs
->retaddr_column
)]
1277 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1282 /* Compute the return address now, since the return address column
1283 can change from frame to frame. */
1285 #ifdef MD_DEMANGLE_RETURN_ADDR
1286 _Unwind_Word ra
= _Unwind_GetGR (context
, fs
->retaddr_column
);
1287 ret_addr
= MD_DEMANGLE_RETURN_ADDR (context
, fs
, ra
);
1289 ret_addr
= _Unwind_GetPtr (context
, fs
->retaddr_column
);
1291 context
->ra
= __builtin_extract_return_addr (ret_addr
);
1296 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1298 uw_update_context (context
, fs
);
1301 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1302 level will be the return address and the CFA. */
1304 #define uw_init_context(CONTEXT) \
1307 /* Do any necessary initialization to access arbitrary stack frames. \
1308 On the SPARC, this means flushing the register windows. */ \
1309 __builtin_unwind_init (); \
1310 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1311 __builtin_return_address (0)); \
1316 init_dwarf_reg_size_table (void)
1318 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1321 static void __attribute__((noinline
))
1322 uw_init_context_1 (struct _Unwind_Context
*context
,
1323 void *outer_cfa
, void *outer_ra
)
1325 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1326 _Unwind_FrameState fs
;
1327 _Unwind_SpTmp sp_slot
;
1328 _Unwind_Reason_Code code
;
1330 memset (context
, 0, sizeof (struct _Unwind_Context
));
1332 if (!ASSUME_EXTENDED_UNWIND_CONTEXT
)
1333 context
->flags
= EXTENDED_CONTEXT_BIT
;
1335 code
= uw_frame_state_for (context
, &fs
);
1336 gcc_assert (code
== _URC_NO_REASON
);
1340 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1341 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1342 && dwarf_reg_size_table
[0] == 0)
1343 init_dwarf_reg_size_table ();
1346 if (dwarf_reg_size_table
[0] == 0)
1347 init_dwarf_reg_size_table ();
1350 /* Force the frame state to use the known cfa value. */
1351 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1352 fs
.regs
.cfa_how
= CFA_REG_OFFSET
;
1353 fs
.regs
.cfa_reg
= __builtin_dwarf_sp_column ();
1354 fs
.regs
.cfa_offset
= 0;
1356 uw_update_context_1 (context
, &fs
);
1358 /* If the return address column was saved in a register in the
1359 initialization context, then we can't see it in the given
1360 call frame data. So have the initialization context tell us. */
1361 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1364 static void _Unwind_DebugHook (void *, void *)
1365 __attribute__ ((__noinline__
, __used__
, __noclone__
));
1367 /* This function is called during unwinding. It is intended as a hook
1368 for a debugger to intercept exceptions. CFA is the CFA of the
1369 target frame. HANDLER is the PC to which control will be
1372 _Unwind_DebugHook (void *cfa
__attribute__ ((__unused__
)),
1373 void *handler
__attribute__ ((__unused__
)))
1375 /* We only want to use stap probes starting with v3. Earlier
1376 versions added too much startup cost. */
1377 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1378 STAP_PROBE2 (libgcc
, unwind
, cfa
, handler
);
1384 /* Install TARGET into CURRENT so that we can return to it. This is a
1385 macro because __builtin_eh_return must be invoked in the context of
1386 our caller. FRAMES is a number of frames to be unwind.
1387 _Unwind_Frames_Extra is a macro to do additional work during unwinding
1388 if needed, for example shadow stack pointer adjustment for Intel CET
1391 #define uw_install_context(CURRENT, TARGET, FRAMES) \
1394 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1395 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1396 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1397 _Unwind_Frames_Extra (FRAMES); \
1398 __builtin_eh_return (offset, handler); \
1403 uw_install_context_1 (struct _Unwind_Context
*current
,
1404 struct _Unwind_Context
*target
)
1407 _Unwind_SpTmp sp_slot
;
1409 /* If the target frame does not have a saved stack pointer,
1410 then set up the target's CFA. */
1411 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1412 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1414 for (i
= 0; i
< __LIBGCC_DWARF_FRAME_REGISTERS__
; ++i
)
1416 void *c
= (void *) (_Unwind_Internal_Ptr
) current
->reg
[i
];
1417 void *t
= (void *) (_Unwind_Internal_Ptr
)target
->reg
[i
];
1419 gcc_assert (current
->by_value
[i
] == 0);
1420 if (target
->by_value
[i
] && c
)
1424 if (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Word
))
1426 w
= (_Unwind_Internal_Ptr
) t
;
1427 memcpy (c
, &w
, sizeof (_Unwind_Word
));
1431 gcc_assert (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Ptr
));
1432 p
= (_Unwind_Internal_Ptr
) t
;
1433 memcpy (c
, &p
, sizeof (_Unwind_Ptr
));
1436 else if (t
&& c
&& t
!= c
)
1437 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1440 /* If the current frame doesn't have a saved stack pointer, then we
1441 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1442 pointer value reloaded. */
1443 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1447 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1449 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1450 if (__LIBGCC_STACK_GROWS_DOWNWARD__
)
1451 return target_cfa
- current
->cfa
+ target
->args_size
;
1453 return current
->cfa
- target_cfa
- target
->args_size
;
1458 static inline _Unwind_Ptr
1459 uw_identify_context (struct _Unwind_Context
*context
)
1461 /* The CFA is not sufficient to disambiguate the context of a function
1462 interrupted by a signal before establishing its frame and the context
1463 of the signal itself. */
1464 if (__LIBGCC_STACK_GROWS_DOWNWARD__
)
1465 return _Unwind_GetCFA (context
) - _Unwind_IsSignalFrame (context
);
1467 return _Unwind_GetCFA (context
) + _Unwind_IsSignalFrame (context
);
1471 #include "unwind.inc"
1473 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1474 alias (_Unwind_Backtrace
);
1475 alias (_Unwind_DeleteException
);
1476 alias (_Unwind_FindEnclosingFunction
);
1477 alias (_Unwind_ForcedUnwind
);
1478 alias (_Unwind_GetDataRelBase
);
1479 alias (_Unwind_GetTextRelBase
);
1480 alias (_Unwind_GetCFA
);
1481 alias (_Unwind_GetGR
);
1482 alias (_Unwind_GetIP
);
1483 alias (_Unwind_GetLanguageSpecificData
);
1484 alias (_Unwind_GetRegionStart
);
1485 alias (_Unwind_RaiseException
);
1486 alias (_Unwind_Resume
);
1487 alias (_Unwind_Resume_or_Rethrow
);
1488 alias (_Unwind_SetGR
);
1489 alias (_Unwind_SetIP
);
1492 #endif /* !USING_SJLJ_EXCEPTIONS */