1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997-2022 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)
140 /* Bit reserved on AArch64, return address has been signed with A or B
142 #define RA_SIGNED_BIT ((~(_Unwind_Word) 0 >> 3) + 1)
144 /* 0 for now, can be increased when further fields are added to
145 struct _Unwind_Context. */
146 _Unwind_Word version
;
147 _Unwind_Word args_size
;
148 char by_value
[__LIBGCC_DWARF_FRAME_REGISTERS__
+1];
151 /* Byte size of every register managed by these routines. */
152 static unsigned char dwarf_reg_size_table
[__LIBGCC_DWARF_FRAME_REGISTERS__
+1];
155 /* Read unaligned data from the instruction buffer. */
160 unsigned u2
__attribute__ ((mode (HI
)));
161 unsigned u4
__attribute__ ((mode (SI
)));
162 unsigned u8
__attribute__ ((mode (DI
)));
163 signed s2
__attribute__ ((mode (HI
)));
164 signed s4
__attribute__ ((mode (SI
)));
165 signed s8
__attribute__ ((mode (DI
)));
166 } __attribute__ ((packed
));
168 static void uw_update_context (struct _Unwind_Context
*, _Unwind_FrameState
*);
169 static _Unwind_Reason_Code
uw_frame_state_for (struct _Unwind_Context
*,
170 _Unwind_FrameState
*);
173 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
176 read_1u (const void *p
) { return *(const unsigned char *) p
; }
179 read_1s (const void *p
) { return *(const signed char *) p
; }
182 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
185 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
187 static inline unsigned int
188 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
191 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
193 static inline unsigned long
194 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
196 static inline unsigned long
197 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
199 static inline _Unwind_Word
200 _Unwind_IsSignalFrame (struct _Unwind_Context
*context
)
202 return (context
->flags
& SIGNAL_FRAME_BIT
) ? 1 : 0;
206 _Unwind_SetSignalFrame (struct _Unwind_Context
*context
, int val
)
209 context
->flags
|= SIGNAL_FRAME_BIT
;
211 context
->flags
&= ~SIGNAL_FRAME_BIT
;
214 static inline _Unwind_Word
215 _Unwind_IsExtendedContext (struct _Unwind_Context
*context
)
217 return (ASSUME_EXTENDED_UNWIND_CONTEXT
218 || (context
->flags
& EXTENDED_CONTEXT_BIT
));
221 /* Get the value of register REGNO as saved in CONTEXT. */
224 _Unwind_GetGR (struct _Unwind_Context
*context
, int regno
)
227 _Unwind_Context_Reg_Val val
;
229 #ifdef DWARF_ZERO_REG
230 if (regno
== DWARF_ZERO_REG
)
234 index
= DWARF_REG_TO_UNWIND_COLUMN (regno
);
235 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
236 size
= dwarf_reg_size_table
[index
];
237 val
= context
->reg
[index
];
239 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
240 return _Unwind_Get_Unwind_Word (val
);
242 #ifdef DWARF_LAZY_REGISTER_VALUE
245 if (DWARF_LAZY_REGISTER_VALUE (regno
, &value
))
250 /* This will segfault if the register hasn't been saved. */
251 if (size
== sizeof(_Unwind_Ptr
))
252 return * (_Unwind_Ptr
*) (_Unwind_Internal_Ptr
) val
;
255 gcc_assert (size
== sizeof(_Unwind_Word
));
256 return * (_Unwind_Word
*) (_Unwind_Internal_Ptr
) val
;
261 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
263 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
266 /* Get the value of the CFA as saved in CONTEXT. */
269 _Unwind_GetCFA (struct _Unwind_Context
*context
)
271 return (_Unwind_Ptr
) context
->cfa
;
274 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
277 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
282 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
283 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
284 size
= dwarf_reg_size_table
[index
];
286 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
288 context
->reg
[index
] = _Unwind_Get_Unwind_Context_Reg_Val (val
);
292 ptr
= (void *) (_Unwind_Internal_Ptr
) context
->reg
[index
];
294 if (size
== sizeof(_Unwind_Ptr
))
295 * (_Unwind_Ptr
*) ptr
= val
;
298 gcc_assert (size
== sizeof(_Unwind_Word
));
299 * (_Unwind_Word
*) ptr
= val
;
303 /* Get the pointer to a register INDEX as saved in CONTEXT. */
306 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
308 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
309 if (_Unwind_IsExtendedContext (context
) && context
->by_value
[index
])
310 return &context
->reg
[index
];
311 return (void *) (_Unwind_Internal_Ptr
) context
->reg
[index
];
314 /* Set the pointer to a register INDEX as saved in CONTEXT. */
317 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
319 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
320 if (_Unwind_IsExtendedContext (context
))
321 context
->by_value
[index
] = 0;
322 context
->reg
[index
] = (_Unwind_Context_Reg_Val
) (_Unwind_Internal_Ptr
) p
;
325 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
328 _Unwind_SetGRValue (struct _Unwind_Context
*context
, int index
,
331 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
332 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
333 /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
334 gcc_assert (dwarf_reg_size_table
[index
] <= sizeof (_Unwind_Context_Reg_Val
));
336 context
->by_value
[index
] = 1;
337 context
->reg
[index
] = _Unwind_Get_Unwind_Context_Reg_Val (val
);
340 /* Return nonzero if register INDEX is stored by value rather than
344 _Unwind_GRByValue (struct _Unwind_Context
*context
, int index
)
346 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
347 return context
->by_value
[index
];
350 /* Retrieve the return address for CONTEXT. */
353 _Unwind_GetIP (struct _Unwind_Context
*context
)
355 return (_Unwind_Ptr
) context
->ra
;
358 /* Retrieve the return address and flag whether that IP is before
359 or after first not yet fully executed instruction. */
362 _Unwind_GetIPInfo (struct _Unwind_Context
*context
, int *ip_before_insn
)
364 *ip_before_insn
= _Unwind_IsSignalFrame (context
);
365 return (_Unwind_Ptr
) context
->ra
;
368 /* Overwrite the return address for CONTEXT with VAL. */
371 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
373 context
->ra
= (void *) val
;
377 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
379 return context
->lsda
;
383 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
385 return (_Unwind_Ptr
) context
->bases
.func
;
389 _Unwind_FindEnclosingFunction (void *pc
)
391 struct dwarf_eh_bases bases
;
392 const struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
401 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
403 return (_Unwind_Ptr
) context
->bases
.dbase
;
407 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
409 return (_Unwind_Ptr
) context
->bases
.tbase
;
413 #include "md-unwind-support.h"
415 /* Extract any interesting information from the CIE for the translation
416 unit F belongs to. Return a pointer to the byte after the augmentation,
417 or NULL if we encountered an undecipherable augmentation. */
419 static const unsigned char *
420 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
421 _Unwind_FrameState
*fs
)
423 const unsigned char *aug
= cie
->augmentation
;
424 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
425 const unsigned char *ret
= NULL
;
429 /* g++ v2 "eh" has pointer immediately following augmentation string,
430 so it must be handled first. */
431 if (aug
[0] == 'e' && aug
[1] == 'h')
433 fs
->eh_ptr
= read_pointer (p
);
434 p
+= sizeof (void *);
438 /* After the augmentation resp. pointer for "eh" augmentation
439 follows for CIE version >= 4 address size byte and
440 segment size byte. */
441 if (__builtin_expect (cie
->version
>= 4, 0))
443 if (p
[0] != sizeof (void *) || p
[1] != 0)
447 /* Immediately following this are the code and
448 data alignment and return address column. */
449 p
= read_uleb128 (p
, &utmp
);
450 fs
->code_align
= (_Unwind_Word
)utmp
;
451 p
= read_sleb128 (p
, &stmp
);
452 fs
->data_align
= (_Unwind_Sword
)stmp
;
453 if (cie
->version
== 1)
454 fs
->retaddr_column
= *p
++;
457 p
= read_uleb128 (p
, &utmp
);
458 fs
->retaddr_column
= (_Unwind_Word
)utmp
;
460 fs
->lsda_encoding
= DW_EH_PE_omit
;
462 /* If the augmentation starts with 'z', then a uleb128 immediately
463 follows containing the length of the augmentation field following
467 p
= read_uleb128 (p
, &utmp
);
474 /* Iterate over recognized augmentation subsequences. */
477 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
480 fs
->lsda_encoding
= *p
++;
484 /* "R" indicates a byte indicating how FDE addresses are encoded. */
485 else if (aug
[0] == 'R')
487 fs
->fde_encoding
= *p
++;
491 /* "P" indicates a personality routine in the CIE augmentation. */
492 else if (aug
[0] == 'P')
494 _Unwind_Ptr personality
;
496 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
497 fs
->personality
= (_Unwind_Personality_Fn
) personality
;
501 /* "S" indicates a signal frame. */
502 else if (aug
[0] == 'S')
504 fs
->signal_frame
= 1;
507 /* aarch64 B-key pointer authentication. */
508 else if (aug
[0] == 'B')
513 /* Otherwise we have an unknown augmentation string.
514 Bail unless we saw a 'z' prefix. */
519 return ret
? ret
: p
;
523 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
524 onto the stack to start. */
527 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
528 struct _Unwind_Context
*context
, _Unwind_Word initial
)
530 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
536 while (op_ptr
< op_end
)
538 enum dwarf_location_atom op
= *op_ptr
++;
540 _uleb128_t reg
, utmp
;
541 _sleb128_t offset
, stmp
;
577 result
= op
- DW_OP_lit0
;
581 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
582 op_ptr
+= sizeof (void *);
585 case DW_OP_GNU_encoded_addr
:
588 op_ptr
= read_encoded_value (context
, *op_ptr
, op_ptr
+1, &presult
);
594 result
= read_1u (op_ptr
);
598 result
= read_1s (op_ptr
);
602 result
= read_2u (op_ptr
);
606 result
= read_2s (op_ptr
);
610 result
= read_4u (op_ptr
);
614 result
= read_4s (op_ptr
);
618 result
= read_8u (op_ptr
);
622 result
= read_8s (op_ptr
);
626 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
627 result
= (_Unwind_Word
)utmp
;
630 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
631 result
= (_Unwind_Sword
)stmp
;
666 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
669 op_ptr
= read_uleb128 (op_ptr
, ®
);
670 result
= _Unwind_GetGR (context
, reg
);
705 op_ptr
= read_sleb128 (op_ptr
, &offset
);
706 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
709 op_ptr
= read_uleb128 (op_ptr
, ®
);
710 op_ptr
= read_sleb128 (op_ptr
, &offset
);
711 result
= _Unwind_GetGR (context
, reg
) + (_Unwind_Word
)offset
;
715 gcc_assert (stack_elt
);
716 result
= stack
[stack_elt
- 1];
720 gcc_assert (stack_elt
);
726 gcc_assert (offset
< stack_elt
- 1);
727 result
= stack
[stack_elt
- 1 - offset
];
731 gcc_assert (stack_elt
>= 2);
732 result
= stack
[stack_elt
- 2];
738 gcc_assert (stack_elt
>= 2);
739 t
= stack
[stack_elt
- 1];
740 stack
[stack_elt
- 1] = stack
[stack_elt
- 2];
741 stack
[stack_elt
- 2] = t
;
747 _Unwind_Word t1
, t2
, t3
;
749 gcc_assert (stack_elt
>= 3);
750 t1
= stack
[stack_elt
- 1];
751 t2
= stack
[stack_elt
- 2];
752 t3
= stack
[stack_elt
- 3];
753 stack
[stack_elt
- 1] = t2
;
754 stack
[stack_elt
- 2] = t3
;
755 stack
[stack_elt
- 3] = t1
;
760 case DW_OP_deref_size
:
764 case DW_OP_plus_uconst
:
765 /* Unary operations. */
766 gcc_assert (stack_elt
);
769 result
= stack
[stack_elt
];
775 void *ptr
= (void *) (_Unwind_Ptr
) result
;
776 result
= (_Unwind_Ptr
) read_pointer (ptr
);
780 case DW_OP_deref_size
:
782 void *ptr
= (void *) (_Unwind_Ptr
) result
;
786 result
= read_1u (ptr
);
789 result
= read_2u (ptr
);
792 result
= read_4u (ptr
);
795 result
= read_8u (ptr
);
804 if ((_Unwind_Sword
) result
< 0)
813 case DW_OP_plus_uconst
:
814 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
815 result
+= (_Unwind_Word
)utmp
;
841 /* Binary operations. */
842 _Unwind_Word first
, second
;
843 gcc_assert (stack_elt
>= 2);
846 second
= stack
[stack_elt
];
847 first
= stack
[stack_elt
+ 1];
852 result
= second
& first
;
855 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) 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
= second
>> first
;
879 result
= (_Unwind_Sword
) second
>> first
;
882 result
= second
^ 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
;
900 result
= (_Unwind_Sword
) second
!= (_Unwind_Sword
) first
;
910 offset
= read_2s (op_ptr
);
916 gcc_assert (stack_elt
);
919 offset
= read_2s (op_ptr
);
921 if (stack
[stack_elt
] != 0)
932 /* Most things push a result value. */
933 gcc_assert ((size_t) stack_elt
< sizeof(stack
)/sizeof(*stack
));
934 stack
[stack_elt
++] = result
;
938 /* We were executing this program to get a value. It should be
940 gcc_assert (stack_elt
);
942 return stack
[stack_elt
];
946 /* Decode DWARF 2 call frame information. Takes pointers the
947 instruction sequence to decode, current register information and
948 CIE info, and the PC range to evaluate. */
950 static void __attribute__ ((__noinline__
))
951 execute_cfa_program_generic (const unsigned char *insn_ptr
,
952 const unsigned char *insn_end
,
953 struct _Unwind_Context
*context
,
954 _Unwind_FrameState
*fs
)
956 #define DATA_ALIGN fs->data_align
957 #define CODE_ALIGN fs->code_align
958 #include "unwind-dw2-execute_cfa.h"
962 execute_cfa_program_specialized (const unsigned char *insn_ptr
,
963 const unsigned char *insn_end
,
964 struct _Unwind_Context
*context
,
965 _Unwind_FrameState
*fs
)
967 #define DATA_ALIGN __LIBGCC_DWARF_CIE_DATA_ALIGNMENT__
968 /* GCC always uses 1 even on architectures with a fixed instruction
971 #include "unwind-dw2-execute_cfa.h"
975 execute_cfa_program (const unsigned char *insn_ptr
,
976 const unsigned char *insn_end
,
977 struct _Unwind_Context
*context
,
978 _Unwind_FrameState
*fs
)
980 if (fs
->data_align
== __LIBGCC_DWARF_CIE_DATA_ALIGNMENT__
981 && fs
->code_align
== 1)
982 execute_cfa_program_specialized (insn_ptr
, insn_end
, context
, fs
);
984 execute_cfa_program_generic (insn_ptr
, insn_end
, context
, fs
);
988 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
989 its caller and decode it into FS. This function also sets the
990 args_size and lsda members of CONTEXT, as they are really information
991 about the caller's frame. */
993 static _Unwind_Reason_Code
994 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
996 const struct dwarf_fde
*fde
;
997 const struct dwarf_cie
*cie
;
998 const unsigned char *aug
, *insn
, *end
;
1000 memset (&fs
->regs
.how
[0], 0,
1001 sizeof (*fs
) - offsetof (_Unwind_FrameState
, regs
.how
[0]));
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
])
1205 case REG_SAVED_OFFSET
:
1206 _Unwind_SetGRPtr (context
, i
,
1207 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1211 if (_Unwind_GRByValue (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
))
1212 _Unwind_SetGRValue (context
, i
,
1213 _Unwind_GetGR (&orig_context
,
1214 fs
->regs
.reg
[i
].loc
.reg
));
1216 _Unwind_SetGRPtr (context
, i
,
1217 _Unwind_GetGRPtr (&orig_context
,
1218 fs
->regs
.reg
[i
].loc
.reg
));
1223 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1227 exp
= read_uleb128 (exp
, &len
);
1228 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1230 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1234 case REG_SAVED_VAL_OFFSET
:
1235 _Unwind_SetGRValue (context
, i
,
1236 (_Unwind_Internal_Ptr
)
1237 (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1240 case REG_SAVED_VAL_EXP
:
1242 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1246 exp
= read_uleb128 (exp
, &len
);
1247 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1249 _Unwind_SetGRValue (context
, i
, val
);
1254 _Unwind_SetSignalFrame (context
, fs
->signal_frame
);
1256 #ifdef MD_FROB_UPDATE_CONTEXT
1257 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1261 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1262 of its caller. Update CONTEXT to refer to the caller as well. Note
1263 that the args_size and lsda members are not updated here, but later in
1264 uw_frame_state_for. */
1267 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1269 uw_update_context_1 (context
, fs
);
1271 /* In general this unwinder doesn't make any distinction between
1272 undefined and same_value rule. Call-saved registers are assumed
1273 to have same_value rule by default and explicit undefined
1274 rule is handled like same_value. The only exception is
1275 DW_CFA_undefined on retaddr_column which is supposed to
1276 mark outermost frame in DWARF 3. */
1277 if (fs
->regs
.how
[DWARF_REG_TO_UNWIND_COLUMN (fs
->retaddr_column
)]
1279 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1284 /* Compute the return address now, since the return address column
1285 can change from frame to frame. */
1287 #ifdef MD_DEMANGLE_RETURN_ADDR
1288 _Unwind_Word ra
= _Unwind_GetGR (context
, fs
->retaddr_column
);
1289 ret_addr
= MD_DEMANGLE_RETURN_ADDR (context
, fs
, ra
);
1291 ret_addr
= _Unwind_GetPtr (context
, fs
->retaddr_column
);
1293 context
->ra
= __builtin_extract_return_addr (ret_addr
);
1298 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1300 uw_update_context (context
, fs
);
1303 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1304 level will be the return address and the CFA. */
1306 #define uw_init_context(CONTEXT) \
1309 /* Do any necessary initialization to access arbitrary stack frames. \
1310 On the SPARC, this means flushing the register windows. */ \
1311 __builtin_unwind_init (); \
1312 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1313 __builtin_return_address (0)); \
1318 init_dwarf_reg_size_table (void)
1320 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1323 static void __attribute__((noinline
))
1324 uw_init_context_1 (struct _Unwind_Context
*context
,
1325 void *outer_cfa
, void *outer_ra
)
1327 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1328 _Unwind_FrameState fs
;
1329 _Unwind_SpTmp sp_slot
;
1330 _Unwind_Reason_Code code
;
1332 memset (context
, 0, sizeof (struct _Unwind_Context
));
1334 if (!ASSUME_EXTENDED_UNWIND_CONTEXT
)
1335 context
->flags
= EXTENDED_CONTEXT_BIT
;
1337 code
= uw_frame_state_for (context
, &fs
);
1338 gcc_assert (code
== _URC_NO_REASON
);
1342 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1343 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1344 && dwarf_reg_size_table
[0] == 0)
1345 init_dwarf_reg_size_table ();
1348 if (dwarf_reg_size_table
[0] == 0)
1349 init_dwarf_reg_size_table ();
1352 /* Force the frame state to use the known cfa value. */
1353 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1354 fs
.regs
.cfa_how
= CFA_REG_OFFSET
;
1355 fs
.regs
.cfa_reg
= __builtin_dwarf_sp_column ();
1356 fs
.regs
.cfa_offset
= 0;
1358 uw_update_context_1 (context
, &fs
);
1360 /* If the return address column was saved in a register in the
1361 initialization context, then we can't see it in the given
1362 call frame data. So have the initialization context tell us. */
1363 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1366 static void _Unwind_DebugHook (void *, void *)
1367 __attribute__ ((__noinline__
, __used__
, __noclone__
));
1369 /* This function is called during unwinding. It is intended as a hook
1370 for a debugger to intercept exceptions. CFA is the CFA of the
1371 target frame. HANDLER is the PC to which control will be
1374 _Unwind_DebugHook (void *cfa
__attribute__ ((__unused__
)),
1375 void *handler
__attribute__ ((__unused__
)))
1377 /* We only want to use stap probes starting with v3. Earlier
1378 versions added too much startup cost. */
1379 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1380 STAP_PROBE2 (libgcc
, unwind
, cfa
, handler
);
1386 /* Install TARGET into CURRENT so that we can return to it. This is a
1387 macro because __builtin_eh_return must be invoked in the context of
1388 our caller. FRAMES is a number of frames to be unwind.
1389 _Unwind_Frames_Extra is a macro to do additional work during unwinding
1390 if needed, for example shadow stack pointer adjustment for Intel CET
1393 #define uw_install_context(CURRENT, TARGET, FRAMES) \
1396 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1397 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1398 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1399 _Unwind_Frames_Extra (FRAMES); \
1400 __builtin_eh_return (offset, handler); \
1405 uw_install_context_1 (struct _Unwind_Context
*current
,
1406 struct _Unwind_Context
*target
)
1409 _Unwind_SpTmp sp_slot
;
1411 /* If the target frame does not have a saved stack pointer,
1412 then set up the target's CFA. */
1413 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1414 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1416 for (i
= 0; i
< __LIBGCC_DWARF_FRAME_REGISTERS__
; ++i
)
1418 void *c
= (void *) (_Unwind_Internal_Ptr
) current
->reg
[i
];
1419 void *t
= (void *) (_Unwind_Internal_Ptr
)target
->reg
[i
];
1421 gcc_assert (current
->by_value
[i
] == 0);
1422 if (target
->by_value
[i
] && c
)
1426 if (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Word
))
1428 w
= (_Unwind_Internal_Ptr
) t
;
1429 memcpy (c
, &w
, sizeof (_Unwind_Word
));
1433 gcc_assert (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Ptr
));
1434 p
= (_Unwind_Internal_Ptr
) t
;
1435 memcpy (c
, &p
, sizeof (_Unwind_Ptr
));
1438 else if (t
&& c
&& t
!= c
)
1439 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1442 /* If the current frame doesn't have a saved stack pointer, then we
1443 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1444 pointer value reloaded. */
1445 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1449 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1451 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1452 if (__LIBGCC_STACK_GROWS_DOWNWARD__
)
1453 return target_cfa
- current
->cfa
+ target
->args_size
;
1455 return current
->cfa
- target_cfa
- target
->args_size
;
1460 static inline _Unwind_Ptr
1461 uw_identify_context (struct _Unwind_Context
*context
)
1463 /* The CFA is not sufficient to disambiguate the context of a function
1464 interrupted by a signal before establishing its frame and the context
1465 of the signal itself. */
1466 if (__LIBGCC_STACK_GROWS_DOWNWARD__
)
1467 return _Unwind_GetCFA (context
) - _Unwind_IsSignalFrame (context
);
1469 return _Unwind_GetCFA (context
) + _Unwind_IsSignalFrame (context
);
1473 #include "unwind.inc"
1475 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1476 alias (_Unwind_Backtrace
);
1477 alias (_Unwind_DeleteException
);
1478 alias (_Unwind_FindEnclosingFunction
);
1479 alias (_Unwind_ForcedUnwind
);
1480 alias (_Unwind_GetDataRelBase
);
1481 alias (_Unwind_GetTextRelBase
);
1482 alias (_Unwind_GetCFA
);
1483 alias (_Unwind_GetGR
);
1484 alias (_Unwind_GetIP
);
1485 alias (_Unwind_GetLanguageSpecificData
);
1486 alias (_Unwind_GetRegionStart
);
1487 alias (_Unwind_RaiseException
);
1488 alias (_Unwind_Resume
);
1489 alias (_Unwind_Resume_or_Rethrow
);
1490 alias (_Unwind_SetGR
);
1491 alias (_Unwind_SetIP
);
1494 #endif /* !USING_SJLJ_EXCEPTIONS */