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;
505 #if defined(MD_ARCH_EXTENSION_CIE_AUG_HANDLER)
506 else if (MD_ARCH_EXTENSION_CIE_AUG_HANDLER (fs
, aug
[0]))
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 #if defined(MD_ARCH_EXTENSION_FRAME_INIT)
1000 MD_ARCH_EXTENSION_FRAME_INIT (context
, fs
);
1002 context
->args_size
= 0;
1005 if (context
->ra
== 0)
1006 return _URC_END_OF_STACK
;
1008 fde
= _Unwind_Find_FDE (context
->ra
+ _Unwind_IsSignalFrame (context
) - 1,
1012 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1013 /* Couldn't find frame unwind info for this function. Try a
1014 target-specific fallback mechanism. This will necessarily
1015 not provide a personality routine or LSDA. */
1016 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
1018 return _URC_END_OF_STACK
;
1022 fs
->pc
= context
->bases
.func
;
1024 cie
= get_cie (fde
);
1025 insn
= extract_cie_info (cie
, context
, fs
);
1027 /* CIE contained unknown augmentation. */
1028 return _URC_FATAL_PHASE1_ERROR
;
1030 /* First decode all the insns in the CIE. */
1031 end
= (const unsigned char *) next_fde ((const struct dwarf_fde
*) cie
);
1032 execute_cfa_program (insn
, end
, context
, fs
);
1034 /* Locate augmentation for the fde. */
1035 aug
= (const unsigned char *) fde
+ sizeof (*fde
);
1036 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1041 aug
= read_uleb128 (aug
, &i
);
1044 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1048 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1049 context
->lsda
= (void *) lsda
;
1052 /* Then the insns in the FDE up to our target PC. */
1055 end
= (const unsigned char *) next_fde (fde
);
1056 execute_cfa_program (insn
, end
, context
, fs
);
1058 return _URC_NO_REASON
;
1061 typedef struct frame_state
1067 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1068 unsigned short cfa_reg
;
1069 unsigned short retaddr_column
;
1070 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1073 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1075 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1076 a given PC_TARGET. The caller should allocate a local variable of
1077 `struct frame_state' and pass its address to STATE_IN. */
1079 struct frame_state
*
1080 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1082 struct _Unwind_Context context
;
1083 _Unwind_FrameState fs
;
1086 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1087 if (!ASSUME_EXTENDED_UNWIND_CONTEXT
)
1088 context
.flags
= EXTENDED_CONTEXT_BIT
;
1089 context
.ra
= pc_target
+ 1;
1091 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1094 /* We have no way to pass a location expression for the CFA to our
1095 caller. It wouldn't understand it anyway. */
1096 if (fs
.regs
.cfa_how
== CFA_EXP
)
1099 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1101 state_in
->saved
[reg
] = fs
.regs
.how
[reg
];
1102 switch (state_in
->saved
[reg
])
1105 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1107 case REG_SAVED_OFFSET
:
1108 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1111 state_in
->reg_or_offset
[reg
] = 0;
1116 state_in
->cfa_offset
= fs
.regs
.cfa_offset
;
1117 state_in
->cfa_reg
= fs
.regs
.cfa_reg
;
1118 state_in
->retaddr_column
= fs
.retaddr_column
;
1119 state_in
->args_size
= context
.args_size
;
1120 state_in
->eh_ptr
= fs
.eh_ptr
;
1125 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1128 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1129 _Unwind_SpTmp
*tmp_sp
)
1131 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1133 if (size
== sizeof(_Unwind_Ptr
))
1134 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1137 gcc_assert (size
== sizeof(_Unwind_Word
));
1138 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1140 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1144 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1146 struct _Unwind_Context orig_context
= *context
;
1150 #ifdef __LIBGCC_EH_RETURN_STACKADJ_RTX__
1151 /* Special handling here: Many machines do not use a frame pointer,
1152 and track the CFA only through offsets from the stack pointer from
1153 one frame to the next. In this case, the stack pointer is never
1154 stored, so it has no saved address in the context. What we do
1155 have is the CFA from the previous stack frame.
1157 In very special situations (such as unwind info for signal return),
1158 there may be location expressions that use the stack pointer as well.
1160 Do this conditionally for one frame. This allows the unwind info
1161 for one frame to save a copy of the stack pointer from the previous
1162 frame, and be able to use much easier CFA mechanisms to do it.
1163 Always zap the saved stack pointer value for the next frame; carrying
1164 the value over from one frame to another doesn't make sense. */
1166 _Unwind_SpTmp tmp_sp
;
1168 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1169 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1170 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1173 /* Compute this frame's CFA. */
1174 switch (fs
->regs
.cfa_how
)
1176 case CFA_REG_OFFSET
:
1177 cfa
= _Unwind_GetPtr (&orig_context
, fs
->regs
.cfa_reg
);
1178 cfa
+= fs
->regs
.cfa_offset
;
1183 const unsigned char *exp
= fs
->regs
.cfa_exp
;
1186 exp
= read_uleb128 (exp
, &len
);
1187 cfa
= (void *) (_Unwind_Ptr
)
1188 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1197 /* Compute the addresses of all registers saved in this frame. */
1198 for (i
= 0; i
< __LIBGCC_DWARF_FRAME_REGISTERS__
+ 1; ++i
)
1199 switch (fs
->regs
.how
[i
])
1203 /* If the value depends on an augmenter, then there is no processing
1204 to do here, and the value computation should be delayed until the
1205 architecture handler computes the value correctly based on the
1206 augmenter information. */
1210 case REG_SAVED_OFFSET
:
1211 _Unwind_SetGRPtr (context
, i
,
1212 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1216 if (_Unwind_GRByValue (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
))
1217 _Unwind_SetGRValue (context
, i
,
1218 _Unwind_GetGR (&orig_context
,
1219 fs
->regs
.reg
[i
].loc
.reg
));
1221 _Unwind_SetGRPtr (context
, i
,
1222 _Unwind_GetGRPtr (&orig_context
,
1223 fs
->regs
.reg
[i
].loc
.reg
));
1228 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1232 exp
= read_uleb128 (exp
, &len
);
1233 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1235 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1239 case REG_SAVED_VAL_OFFSET
:
1240 _Unwind_SetGRValue (context
, i
,
1241 (_Unwind_Internal_Ptr
)
1242 (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1245 case REG_SAVED_VAL_EXP
:
1247 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1251 exp
= read_uleb128 (exp
, &len
);
1252 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1254 _Unwind_SetGRValue (context
, i
, val
);
1259 _Unwind_SetSignalFrame (context
, fs
->signal_frame
);
1261 #ifdef MD_FROB_UPDATE_CONTEXT
1262 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1266 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1267 of its caller. Update CONTEXT to refer to the caller as well. Note
1268 that the args_size and lsda members are not updated here, but later in
1269 uw_frame_state_for. */
1272 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1274 uw_update_context_1 (context
, fs
);
1276 /* In general this unwinder doesn't make any distinction between
1277 undefined and same_value rule. Call-saved registers are assumed
1278 to have same_value rule by default and explicit undefined
1279 rule is handled like same_value. The only exception is
1280 DW_CFA_undefined on retaddr_column which is supposed to
1281 mark outermost frame in DWARF 3. */
1282 if (fs
->regs
.how
[DWARF_REG_TO_UNWIND_COLUMN (fs
->retaddr_column
)]
1284 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1289 /* Compute the return address now, since the return address column
1290 can change from frame to frame. */
1292 #ifdef MD_DEMANGLE_RETURN_ADDR
1293 _Unwind_Word ra
= _Unwind_GetGR (context
, fs
->retaddr_column
);
1294 ret_addr
= MD_DEMANGLE_RETURN_ADDR (context
, fs
, ra
);
1296 ret_addr
= _Unwind_GetPtr (context
, fs
->retaddr_column
);
1298 context
->ra
= __builtin_extract_return_addr (ret_addr
);
1303 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1305 uw_update_context (context
, fs
);
1308 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1309 level will be the return address and the CFA. */
1311 #define uw_init_context(CONTEXT) \
1314 /* Do any necessary initialization to access arbitrary stack frames. \
1315 On the SPARC, this means flushing the register windows. */ \
1316 __builtin_unwind_init (); \
1317 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1318 __builtin_return_address (0)); \
1323 init_dwarf_reg_size_table (void)
1325 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1328 static void __attribute__((noinline
))
1329 uw_init_context_1 (struct _Unwind_Context
*context
,
1330 void *outer_cfa
, void *outer_ra
)
1332 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1333 _Unwind_FrameState fs
;
1334 _Unwind_SpTmp sp_slot
;
1335 _Unwind_Reason_Code code
;
1337 memset (context
, 0, sizeof (struct _Unwind_Context
));
1339 if (!ASSUME_EXTENDED_UNWIND_CONTEXT
)
1340 context
->flags
= EXTENDED_CONTEXT_BIT
;
1342 code
= uw_frame_state_for (context
, &fs
);
1343 gcc_assert (code
== _URC_NO_REASON
);
1347 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1348 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1349 && dwarf_reg_size_table
[0] == 0)
1350 init_dwarf_reg_size_table ();
1353 if (dwarf_reg_size_table
[0] == 0)
1354 init_dwarf_reg_size_table ();
1357 /* Force the frame state to use the known cfa value. */
1358 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1359 fs
.regs
.cfa_how
= CFA_REG_OFFSET
;
1360 fs
.regs
.cfa_reg
= __builtin_dwarf_sp_column ();
1361 fs
.regs
.cfa_offset
= 0;
1363 uw_update_context_1 (context
, &fs
);
1365 /* If the return address column was saved in a register in the
1366 initialization context, then we can't see it in the given
1367 call frame data. So have the initialization context tell us. */
1368 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1371 static void _Unwind_DebugHook (void *, void *)
1372 __attribute__ ((__noinline__
, __used__
, __noclone__
));
1374 /* This function is called during unwinding. It is intended as a hook
1375 for a debugger to intercept exceptions. CFA is the CFA of the
1376 target frame. HANDLER is the PC to which control will be
1379 _Unwind_DebugHook (void *cfa
__attribute__ ((__unused__
)),
1380 void *handler
__attribute__ ((__unused__
)))
1382 /* We only want to use stap probes starting with v3. Earlier
1383 versions added too much startup cost. */
1384 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1385 STAP_PROBE2 (libgcc
, unwind
, cfa
, handler
);
1391 /* Install TARGET into CURRENT so that we can return to it. This is a
1392 macro because __builtin_eh_return must be invoked in the context of
1393 our caller. FRAMES is a number of frames to be unwind.
1394 _Unwind_Frames_Extra is a macro to do additional work during unwinding
1395 if needed, for example shadow stack pointer adjustment for Intel CET
1398 #define uw_install_context(CURRENT, TARGET, FRAMES) \
1401 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1402 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1403 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1404 _Unwind_Frames_Extra (FRAMES); \
1405 __builtin_eh_return (offset, handler); \
1410 uw_install_context_1 (struct _Unwind_Context
*current
,
1411 struct _Unwind_Context
*target
)
1414 _Unwind_SpTmp sp_slot
;
1416 /* If the target frame does not have a saved stack pointer,
1417 then set up the target's CFA. */
1418 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1419 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1421 for (i
= 0; i
< __LIBGCC_DWARF_FRAME_REGISTERS__
; ++i
)
1423 void *c
= (void *) (_Unwind_Internal_Ptr
) current
->reg
[i
];
1424 void *t
= (void *) (_Unwind_Internal_Ptr
)target
->reg
[i
];
1426 #ifdef MD_FRAME_LOCAL_REGISTER_P
1427 if (MD_FRAME_LOCAL_REGISTER_P (i
))
1431 gcc_assert (current
->by_value
[i
] == 0);
1432 if (target
->by_value
[i
] && c
)
1436 if (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Word
))
1438 w
= (_Unwind_Internal_Ptr
) t
;
1439 memcpy (c
, &w
, sizeof (_Unwind_Word
));
1443 gcc_assert (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Ptr
));
1444 p
= (_Unwind_Internal_Ptr
) t
;
1445 memcpy (c
, &p
, sizeof (_Unwind_Ptr
));
1448 else if (t
&& c
&& t
!= c
)
1449 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1452 /* If the current frame doesn't have a saved stack pointer, then we
1453 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1454 pointer value reloaded. */
1455 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1459 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1461 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1462 if (__LIBGCC_STACK_GROWS_DOWNWARD__
)
1463 return target_cfa
- current
->cfa
+ target
->args_size
;
1465 return current
->cfa
- target_cfa
- target
->args_size
;
1470 static inline _Unwind_Ptr
1471 uw_identify_context (struct _Unwind_Context
*context
)
1473 /* The CFA is not sufficient to disambiguate the context of a function
1474 interrupted by a signal before establishing its frame and the context
1475 of the signal itself. */
1476 if (__LIBGCC_STACK_GROWS_DOWNWARD__
)
1477 return _Unwind_GetCFA (context
) - _Unwind_IsSignalFrame (context
);
1479 return _Unwind_GetCFA (context
) + _Unwind_IsSignalFrame (context
);
1483 #include "unwind.inc"
1485 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1486 alias (_Unwind_Backtrace
);
1487 alias (_Unwind_DeleteException
);
1488 alias (_Unwind_FindEnclosingFunction
);
1489 alias (_Unwind_ForcedUnwind
);
1490 alias (_Unwind_GetDataRelBase
);
1491 alias (_Unwind_GetTextRelBase
);
1492 alias (_Unwind_GetCFA
);
1493 alias (_Unwind_GetGR
);
1494 alias (_Unwind_GetIP
);
1495 alias (_Unwind_GetLanguageSpecificData
);
1496 alias (_Unwind_GetRegionStart
);
1497 alias (_Unwind_RaiseException
);
1498 alias (_Unwind_Resume
);
1499 alias (_Unwind_Resume_or_Rethrow
);
1500 alias (_Unwind_SetGR
);
1501 alias (_Unwind_SetIP
);
1504 #endif /* !USING_SJLJ_EXCEPTIONS */