Define __LIBGCC_DWARF_REG_SIZES_CONSTANT__ if DWARF register size is constant
[official-gcc.git] / libgcc / unwind-dw2.c
blobc370121bb2945e0f05dfff03cd3f9fdb765e84c4
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)
9 any later version.
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/>. */
25 #include "tconfig.h"
26 #include "tsystem.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "libgcc_tm.h"
30 #include "dwarf2.h"
31 #include "unwind.h"
32 #ifdef __USING_SJLJ_EXCEPTIONS__
33 # define NO_SIZE_OF_ENCODED_VALUE
34 #endif
35 #include "unwind-pe.h"
36 #include "unwind-dw2-fde.h"
37 #include "gthr.h"
38 #include "unwind-dw2.h"
39 #include <stddef.h>
41 #ifdef HAVE_SYS_SDT_H
42 #include <sys/sdt.h>
43 #endif
45 #ifndef __USING_SJLJ_EXCEPTIONS__
47 #ifndef __LIBGCC_STACK_GROWS_DOWNWARD__
48 #define __LIBGCC_STACK_GROWS_DOWNWARD__ 0
49 #else
50 #undef __LIBGCC_STACK_GROWS_DOWNWARD__
51 #define __LIBGCC_STACK_GROWS_DOWNWARD__ 1
52 #endif
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__
57 #endif
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
72 call clobbered.
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
93 #endif
95 static inline _Unwind_Word
96 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
98 return val;
101 static inline _Unwind_Context_Reg_Val
102 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
104 return val;
106 #else
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;
120 #endif
122 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
123 #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
124 #endif
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
128 to its caller. */
129 struct _Unwind_Context
131 _Unwind_Context_Reg_Val reg[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
132 void *cfa;
133 void *ra;
134 void *lsda;
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
141 key. */
142 #define RA_SIGNED_BIT ((~(_Unwind_Word) 0 >> 3) + 1)
143 _Unwind_Word flags;
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 #ifdef __LIBGCC_DWARF_REG_SIZES_CONSTANT__
152 static inline unsigned char
153 dwarf_reg_size (int index __attribute__ ((__unused__)))
155 return __LIBGCC_DWARF_REG_SIZES_CONSTANT__;
157 #else
158 /* Byte size of every register managed by these routines. */
159 static unsigned char dwarf_reg_size_table[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
162 static inline unsigned char
163 dwarf_reg_size (unsigned index)
165 gcc_assert (index < sizeof (dwarf_reg_size_table));
166 return dwarf_reg_size_table[index];
168 #endif
171 /* Read unaligned data from the instruction buffer. */
173 union unaligned
175 void *p;
176 unsigned u2 __attribute__ ((mode (HI)));
177 unsigned u4 __attribute__ ((mode (SI)));
178 unsigned u8 __attribute__ ((mode (DI)));
179 signed s2 __attribute__ ((mode (HI)));
180 signed s4 __attribute__ ((mode (SI)));
181 signed s8 __attribute__ ((mode (DI)));
182 } __attribute__ ((packed));
184 static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
185 static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
186 _Unwind_FrameState *);
188 static inline void *
189 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
191 static inline int
192 read_1u (const void *p) { return *(const unsigned char *) p; }
194 static inline int
195 read_1s (const void *p) { return *(const signed char *) p; }
197 static inline int
198 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
200 static inline int
201 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
203 static inline unsigned int
204 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
206 static inline int
207 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
209 static inline unsigned long
210 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
212 static inline unsigned long
213 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
215 static inline _Unwind_Word
216 _Unwind_IsSignalFrame (struct _Unwind_Context *context)
218 return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
221 static inline void
222 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
224 if (val)
225 context->flags |= SIGNAL_FRAME_BIT;
226 else
227 context->flags &= ~SIGNAL_FRAME_BIT;
230 static inline _Unwind_Word
231 _Unwind_IsExtendedContext (struct _Unwind_Context *context)
233 return (ASSUME_EXTENDED_UNWIND_CONTEXT
234 || (context->flags & EXTENDED_CONTEXT_BIT));
237 /* Get the value of register REGNO as saved in CONTEXT. */
239 inline _Unwind_Word
240 _Unwind_GetGR (struct _Unwind_Context *context, int regno)
242 int size, index;
243 _Unwind_Context_Reg_Val val;
245 #ifdef DWARF_ZERO_REG
246 if (regno == DWARF_ZERO_REG)
247 return 0;
248 #endif
250 index = DWARF_REG_TO_UNWIND_COLUMN (regno);
251 size = dwarf_reg_size (index);
252 val = context->reg[index];
254 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
255 return _Unwind_Get_Unwind_Word (val);
257 #ifdef DWARF_LAZY_REGISTER_VALUE
259 _Unwind_Word value;
260 if (DWARF_LAZY_REGISTER_VALUE (regno, &value))
261 return value;
263 #endif
265 /* This will segfault if the register hasn't been saved. */
266 if (size == sizeof(_Unwind_Ptr))
267 return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
268 else
270 gcc_assert (size == sizeof(_Unwind_Word));
271 return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
275 static inline void *
276 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
278 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
281 /* Get the value of the CFA as saved in CONTEXT. */
283 _Unwind_Word
284 _Unwind_GetCFA (struct _Unwind_Context *context)
286 return (_Unwind_Ptr) context->cfa;
289 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
291 inline void
292 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
294 int size;
295 void *ptr;
297 index = DWARF_REG_TO_UNWIND_COLUMN (index);
298 size = dwarf_reg_size (index);
300 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
302 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
303 return;
306 ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
308 if (size == sizeof(_Unwind_Ptr))
309 * (_Unwind_Ptr *) ptr = val;
310 else
312 gcc_assert (size == sizeof(_Unwind_Word));
313 * (_Unwind_Word *) ptr = val;
317 /* Get the pointer to a register INDEX as saved in CONTEXT. */
319 static inline void *
320 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
322 index = DWARF_REG_TO_UNWIND_COLUMN (index);
323 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
324 return &context->reg[index];
325 return (void *) (_Unwind_Internal_Ptr) context->reg[index];
328 /* Set the pointer to a register INDEX as saved in CONTEXT. */
330 static inline void
331 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
333 index = DWARF_REG_TO_UNWIND_COLUMN (index);
334 if (_Unwind_IsExtendedContext (context))
335 context->by_value[index] = 0;
336 context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
339 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
341 static inline void
342 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
343 _Unwind_Word val)
345 index = DWARF_REG_TO_UNWIND_COLUMN (index);
346 /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
347 gcc_assert (dwarf_reg_size (index) <= sizeof (_Unwind_Context_Reg_Val));
349 context->by_value[index] = 1;
350 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
353 /* Return nonzero if register INDEX is stored by value rather than
354 by reference. */
356 static inline int
357 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
359 index = DWARF_REG_TO_UNWIND_COLUMN (index);
360 return context->by_value[index];
363 /* Retrieve the return address for CONTEXT. */
365 inline _Unwind_Ptr
366 _Unwind_GetIP (struct _Unwind_Context *context)
368 return (_Unwind_Ptr) context->ra;
371 /* Retrieve the return address and flag whether that IP is before
372 or after first not yet fully executed instruction. */
374 inline _Unwind_Ptr
375 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
377 *ip_before_insn = _Unwind_IsSignalFrame (context);
378 return (_Unwind_Ptr) context->ra;
381 /* Overwrite the return address for CONTEXT with VAL. */
383 inline void
384 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
386 context->ra = (void *) val;
389 void *
390 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
392 return context->lsda;
395 _Unwind_Ptr
396 _Unwind_GetRegionStart (struct _Unwind_Context *context)
398 return (_Unwind_Ptr) context->bases.func;
401 void *
402 _Unwind_FindEnclosingFunction (void *pc)
404 struct dwarf_eh_bases bases;
405 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
406 if (fde)
407 return bases.func;
408 else
409 return NULL;
412 #ifndef __ia64__
413 _Unwind_Ptr
414 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
416 return (_Unwind_Ptr) context->bases.dbase;
419 _Unwind_Ptr
420 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
422 return (_Unwind_Ptr) context->bases.tbase;
424 #endif
426 #include "md-unwind-support.h"
428 /* Extract any interesting information from the CIE for the translation
429 unit F belongs to. Return a pointer to the byte after the augmentation,
430 or NULL if we encountered an undecipherable augmentation. */
432 static const unsigned char *
433 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
434 _Unwind_FrameState *fs)
436 const unsigned char *aug = cie->augmentation;
437 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
438 const unsigned char *ret = NULL;
439 _uleb128_t utmp;
440 _sleb128_t stmp;
442 /* g++ v2 "eh" has pointer immediately following augmentation string,
443 so it must be handled first. */
444 if (aug[0] == 'e' && aug[1] == 'h')
446 fs->eh_ptr = read_pointer (p);
447 p += sizeof (void *);
448 aug += 2;
451 /* After the augmentation resp. pointer for "eh" augmentation
452 follows for CIE version >= 4 address size byte and
453 segment size byte. */
454 if (__builtin_expect (cie->version >= 4, 0))
456 if (p[0] != sizeof (void *) || p[1] != 0)
457 return NULL;
458 p += 2;
460 /* Immediately following this are the code and
461 data alignment and return address column. */
462 p = read_uleb128 (p, &utmp);
463 fs->code_align = (_Unwind_Word)utmp;
464 p = read_sleb128 (p, &stmp);
465 fs->data_align = (_Unwind_Sword)stmp;
466 if (cie->version == 1)
467 fs->retaddr_column = *p++;
468 else
470 p = read_uleb128 (p, &utmp);
471 fs->retaddr_column = (_Unwind_Word)utmp;
473 fs->lsda_encoding = DW_EH_PE_omit;
475 /* If the augmentation starts with 'z', then a uleb128 immediately
476 follows containing the length of the augmentation field following
477 the size. */
478 if (*aug == 'z')
480 p = read_uleb128 (p, &utmp);
481 ret = p + utmp;
483 fs->saw_z = 1;
484 ++aug;
487 /* Iterate over recognized augmentation subsequences. */
488 while (*aug != '\0')
490 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
491 if (aug[0] == 'L')
493 fs->lsda_encoding = *p++;
494 aug += 1;
497 /* "R" indicates a byte indicating how FDE addresses are encoded. */
498 else if (aug[0] == 'R')
500 fs->fde_encoding = *p++;
501 aug += 1;
504 /* "P" indicates a personality routine in the CIE augmentation. */
505 else if (aug[0] == 'P')
507 _Unwind_Ptr personality;
509 p = read_encoded_value (context, *p, p + 1, &personality);
510 fs->personality = (_Unwind_Personality_Fn) personality;
511 aug += 1;
514 /* "S" indicates a signal frame. */
515 else if (aug[0] == 'S')
517 fs->signal_frame = 1;
518 aug += 1;
520 /* aarch64 B-key pointer authentication. */
521 else if (aug[0] == 'B')
523 aug += 1;
526 /* Otherwise we have an unknown augmentation string.
527 Bail unless we saw a 'z' prefix. */
528 else
529 return ret;
532 return ret ? ret : p;
536 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
537 onto the stack to start. */
539 static _Unwind_Word
540 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
541 struct _Unwind_Context *context, _Unwind_Word initial)
543 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
544 int stack_elt;
546 stack[0] = initial;
547 stack_elt = 1;
549 while (op_ptr < op_end)
551 enum dwarf_location_atom op = *op_ptr++;
552 _Unwind_Word result;
553 _uleb128_t reg, utmp;
554 _sleb128_t offset, stmp;
556 switch (op)
558 case DW_OP_lit0:
559 case DW_OP_lit1:
560 case DW_OP_lit2:
561 case DW_OP_lit3:
562 case DW_OP_lit4:
563 case DW_OP_lit5:
564 case DW_OP_lit6:
565 case DW_OP_lit7:
566 case DW_OP_lit8:
567 case DW_OP_lit9:
568 case DW_OP_lit10:
569 case DW_OP_lit11:
570 case DW_OP_lit12:
571 case DW_OP_lit13:
572 case DW_OP_lit14:
573 case DW_OP_lit15:
574 case DW_OP_lit16:
575 case DW_OP_lit17:
576 case DW_OP_lit18:
577 case DW_OP_lit19:
578 case DW_OP_lit20:
579 case DW_OP_lit21:
580 case DW_OP_lit22:
581 case DW_OP_lit23:
582 case DW_OP_lit24:
583 case DW_OP_lit25:
584 case DW_OP_lit26:
585 case DW_OP_lit27:
586 case DW_OP_lit28:
587 case DW_OP_lit29:
588 case DW_OP_lit30:
589 case DW_OP_lit31:
590 result = op - DW_OP_lit0;
591 break;
593 case DW_OP_addr:
594 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
595 op_ptr += sizeof (void *);
596 break;
598 case DW_OP_GNU_encoded_addr:
600 _Unwind_Ptr presult;
601 op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
602 result = presult;
604 break;
606 case DW_OP_const1u:
607 result = read_1u (op_ptr);
608 op_ptr += 1;
609 break;
610 case DW_OP_const1s:
611 result = read_1s (op_ptr);
612 op_ptr += 1;
613 break;
614 case DW_OP_const2u:
615 result = read_2u (op_ptr);
616 op_ptr += 2;
617 break;
618 case DW_OP_const2s:
619 result = read_2s (op_ptr);
620 op_ptr += 2;
621 break;
622 case DW_OP_const4u:
623 result = read_4u (op_ptr);
624 op_ptr += 4;
625 break;
626 case DW_OP_const4s:
627 result = read_4s (op_ptr);
628 op_ptr += 4;
629 break;
630 case DW_OP_const8u:
631 result = read_8u (op_ptr);
632 op_ptr += 8;
633 break;
634 case DW_OP_const8s:
635 result = read_8s (op_ptr);
636 op_ptr += 8;
637 break;
638 case DW_OP_constu:
639 op_ptr = read_uleb128 (op_ptr, &utmp);
640 result = (_Unwind_Word)utmp;
641 break;
642 case DW_OP_consts:
643 op_ptr = read_sleb128 (op_ptr, &stmp);
644 result = (_Unwind_Sword)stmp;
645 break;
647 case DW_OP_reg0:
648 case DW_OP_reg1:
649 case DW_OP_reg2:
650 case DW_OP_reg3:
651 case DW_OP_reg4:
652 case DW_OP_reg5:
653 case DW_OP_reg6:
654 case DW_OP_reg7:
655 case DW_OP_reg8:
656 case DW_OP_reg9:
657 case DW_OP_reg10:
658 case DW_OP_reg11:
659 case DW_OP_reg12:
660 case DW_OP_reg13:
661 case DW_OP_reg14:
662 case DW_OP_reg15:
663 case DW_OP_reg16:
664 case DW_OP_reg17:
665 case DW_OP_reg18:
666 case DW_OP_reg19:
667 case DW_OP_reg20:
668 case DW_OP_reg21:
669 case DW_OP_reg22:
670 case DW_OP_reg23:
671 case DW_OP_reg24:
672 case DW_OP_reg25:
673 case DW_OP_reg26:
674 case DW_OP_reg27:
675 case DW_OP_reg28:
676 case DW_OP_reg29:
677 case DW_OP_reg30:
678 case DW_OP_reg31:
679 result = _Unwind_GetGR (context, op - DW_OP_reg0);
680 break;
681 case DW_OP_regx:
682 op_ptr = read_uleb128 (op_ptr, &reg);
683 result = _Unwind_GetGR (context, reg);
684 break;
686 case DW_OP_breg0:
687 case DW_OP_breg1:
688 case DW_OP_breg2:
689 case DW_OP_breg3:
690 case DW_OP_breg4:
691 case DW_OP_breg5:
692 case DW_OP_breg6:
693 case DW_OP_breg7:
694 case DW_OP_breg8:
695 case DW_OP_breg9:
696 case DW_OP_breg10:
697 case DW_OP_breg11:
698 case DW_OP_breg12:
699 case DW_OP_breg13:
700 case DW_OP_breg14:
701 case DW_OP_breg15:
702 case DW_OP_breg16:
703 case DW_OP_breg17:
704 case DW_OP_breg18:
705 case DW_OP_breg19:
706 case DW_OP_breg20:
707 case DW_OP_breg21:
708 case DW_OP_breg22:
709 case DW_OP_breg23:
710 case DW_OP_breg24:
711 case DW_OP_breg25:
712 case DW_OP_breg26:
713 case DW_OP_breg27:
714 case DW_OP_breg28:
715 case DW_OP_breg29:
716 case DW_OP_breg30:
717 case DW_OP_breg31:
718 op_ptr = read_sleb128 (op_ptr, &offset);
719 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
720 break;
721 case DW_OP_bregx:
722 op_ptr = read_uleb128 (op_ptr, &reg);
723 op_ptr = read_sleb128 (op_ptr, &offset);
724 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
725 break;
727 case DW_OP_dup:
728 gcc_assert (stack_elt);
729 result = stack[stack_elt - 1];
730 break;
732 case DW_OP_drop:
733 gcc_assert (stack_elt);
734 stack_elt -= 1;
735 goto no_push;
737 case DW_OP_pick:
738 offset = *op_ptr++;
739 gcc_assert (offset < stack_elt - 1);
740 result = stack[stack_elt - 1 - offset];
741 break;
743 case DW_OP_over:
744 gcc_assert (stack_elt >= 2);
745 result = stack[stack_elt - 2];
746 break;
748 case DW_OP_swap:
750 _Unwind_Word t;
751 gcc_assert (stack_elt >= 2);
752 t = stack[stack_elt - 1];
753 stack[stack_elt - 1] = stack[stack_elt - 2];
754 stack[stack_elt - 2] = t;
755 goto no_push;
758 case DW_OP_rot:
760 _Unwind_Word t1, t2, t3;
762 gcc_assert (stack_elt >= 3);
763 t1 = stack[stack_elt - 1];
764 t2 = stack[stack_elt - 2];
765 t3 = stack[stack_elt - 3];
766 stack[stack_elt - 1] = t2;
767 stack[stack_elt - 2] = t3;
768 stack[stack_elt - 3] = t1;
769 goto no_push;
772 case DW_OP_deref:
773 case DW_OP_deref_size:
774 case DW_OP_abs:
775 case DW_OP_neg:
776 case DW_OP_not:
777 case DW_OP_plus_uconst:
778 /* Unary operations. */
779 gcc_assert (stack_elt);
780 stack_elt -= 1;
782 result = stack[stack_elt];
784 switch (op)
786 case DW_OP_deref:
788 void *ptr = (void *) (_Unwind_Ptr) result;
789 result = (_Unwind_Ptr) read_pointer (ptr);
791 break;
793 case DW_OP_deref_size:
795 void *ptr = (void *) (_Unwind_Ptr) result;
796 switch (*op_ptr++)
798 case 1:
799 result = read_1u (ptr);
800 break;
801 case 2:
802 result = read_2u (ptr);
803 break;
804 case 4:
805 result = read_4u (ptr);
806 break;
807 case 8:
808 result = read_8u (ptr);
809 break;
810 default:
811 gcc_unreachable ();
814 break;
816 case DW_OP_abs:
817 if ((_Unwind_Sword) result < 0)
818 result = -result;
819 break;
820 case DW_OP_neg:
821 result = -result;
822 break;
823 case DW_OP_not:
824 result = ~result;
825 break;
826 case DW_OP_plus_uconst:
827 op_ptr = read_uleb128 (op_ptr, &utmp);
828 result += (_Unwind_Word)utmp;
829 break;
831 default:
832 gcc_unreachable ();
834 break;
836 case DW_OP_and:
837 case DW_OP_div:
838 case DW_OP_minus:
839 case DW_OP_mod:
840 case DW_OP_mul:
841 case DW_OP_or:
842 case DW_OP_plus:
843 case DW_OP_shl:
844 case DW_OP_shr:
845 case DW_OP_shra:
846 case DW_OP_xor:
847 case DW_OP_le:
848 case DW_OP_ge:
849 case DW_OP_eq:
850 case DW_OP_lt:
851 case DW_OP_gt:
852 case DW_OP_ne:
854 /* Binary operations. */
855 _Unwind_Word first, second;
856 gcc_assert (stack_elt >= 2);
857 stack_elt -= 2;
859 second = stack[stack_elt];
860 first = stack[stack_elt + 1];
862 switch (op)
864 case DW_OP_and:
865 result = second & first;
866 break;
867 case DW_OP_div:
868 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
869 break;
870 case DW_OP_minus:
871 result = second - first;
872 break;
873 case DW_OP_mod:
874 result = second % first;
875 break;
876 case DW_OP_mul:
877 result = second * first;
878 break;
879 case DW_OP_or:
880 result = second | first;
881 break;
882 case DW_OP_plus:
883 result = second + first;
884 break;
885 case DW_OP_shl:
886 result = second << first;
887 break;
888 case DW_OP_shr:
889 result = second >> first;
890 break;
891 case DW_OP_shra:
892 result = (_Unwind_Sword) second >> first;
893 break;
894 case DW_OP_xor:
895 result = second ^ first;
896 break;
897 case DW_OP_le:
898 result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
899 break;
900 case DW_OP_ge:
901 result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
902 break;
903 case DW_OP_eq:
904 result = (_Unwind_Sword) second == (_Unwind_Sword) first;
905 break;
906 case DW_OP_lt:
907 result = (_Unwind_Sword) second < (_Unwind_Sword) first;
908 break;
909 case DW_OP_gt:
910 result = (_Unwind_Sword) second > (_Unwind_Sword) first;
911 break;
912 case DW_OP_ne:
913 result = (_Unwind_Sword) second != (_Unwind_Sword) first;
914 break;
916 default:
917 gcc_unreachable ();
920 break;
922 case DW_OP_skip:
923 offset = read_2s (op_ptr);
924 op_ptr += 2;
925 op_ptr += offset;
926 goto no_push;
928 case DW_OP_bra:
929 gcc_assert (stack_elt);
930 stack_elt -= 1;
932 offset = read_2s (op_ptr);
933 op_ptr += 2;
934 if (stack[stack_elt] != 0)
935 op_ptr += offset;
936 goto no_push;
938 case DW_OP_nop:
939 goto no_push;
941 default:
942 gcc_unreachable ();
945 /* Most things push a result value. */
946 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
947 stack[stack_elt++] = result;
948 no_push:;
951 /* We were executing this program to get a value. It should be
952 at top of stack. */
953 gcc_assert (stack_elt);
954 stack_elt -= 1;
955 return stack[stack_elt];
959 /* Decode DWARF 2 call frame information. Takes pointers the
960 instruction sequence to decode, current register information and
961 CIE info, and the PC range to evaluate. */
963 static void
964 execute_cfa_program (const unsigned char *insn_ptr,
965 const unsigned char *insn_end,
966 struct _Unwind_Context *context,
967 _Unwind_FrameState *fs)
969 struct frame_state_reg_info *unused_rs = NULL;
971 /* Don't allow remember/restore between CIE and FDE programs. */
972 fs->regs.prev = NULL;
974 /* The comparison with the return address uses < rather than <= because
975 we are only interested in the effects of code before the call; for a
976 noreturn function, the return address may point to unrelated code with
977 a different stack configuration that we are not interested in. We
978 assume that the call itself is unwind info-neutral; if not, or if
979 there are delay instructions that adjust the stack, these must be
980 reflected at the point immediately before the call insn.
981 In signal frames, return address is after last completed instruction,
982 so we add 1 to return address to make the comparison <=. */
983 while (insn_ptr < insn_end
984 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
986 unsigned char insn = *insn_ptr++;
987 _uleb128_t reg, utmp;
988 _sleb128_t offset, stmp;
990 if ((insn & 0xc0) == DW_CFA_advance_loc)
991 fs->pc += (insn & 0x3f) * fs->code_align;
992 else if ((insn & 0xc0) == DW_CFA_offset)
994 reg = insn & 0x3f;
995 insn_ptr = read_uleb128 (insn_ptr, &utmp);
996 offset = (_Unwind_Sword) utmp * fs->data_align;
997 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
998 if (UNWIND_COLUMN_IN_RANGE (reg))
1000 fs->regs.how[reg] = REG_SAVED_OFFSET;
1001 fs->regs.reg[reg].loc.offset = offset;
1004 else if ((insn & 0xc0) == DW_CFA_restore)
1006 reg = insn & 0x3f;
1007 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1008 if (UNWIND_COLUMN_IN_RANGE (reg))
1009 fs->regs.how[reg] = REG_UNSAVED;
1011 else switch (insn)
1013 case DW_CFA_set_loc:
1015 _Unwind_Ptr pc;
1017 insn_ptr = read_encoded_value (context, fs->fde_encoding,
1018 insn_ptr, &pc);
1019 fs->pc = (void *) pc;
1021 break;
1023 case DW_CFA_advance_loc1:
1024 fs->pc += read_1u (insn_ptr) * fs->code_align;
1025 insn_ptr += 1;
1026 break;
1027 case DW_CFA_advance_loc2:
1028 fs->pc += read_2u (insn_ptr) * fs->code_align;
1029 insn_ptr += 2;
1030 break;
1031 case DW_CFA_advance_loc4:
1032 fs->pc += read_4u (insn_ptr) * fs->code_align;
1033 insn_ptr += 4;
1034 break;
1036 case DW_CFA_offset_extended:
1037 insn_ptr = read_uleb128 (insn_ptr, &reg);
1038 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1039 offset = (_Unwind_Sword) utmp * fs->data_align;
1040 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1041 if (UNWIND_COLUMN_IN_RANGE (reg))
1043 fs->regs.how[reg] = REG_SAVED_OFFSET;
1044 fs->regs.reg[reg].loc.offset = offset;
1046 break;
1048 case DW_CFA_restore_extended:
1049 insn_ptr = read_uleb128 (insn_ptr, &reg);
1050 /* FIXME, this is wrong; the CIE might have said that the
1051 register was saved somewhere. */
1052 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1053 if (UNWIND_COLUMN_IN_RANGE (reg))
1054 fs->regs.how[reg] = REG_UNSAVED;
1055 break;
1057 case DW_CFA_same_value:
1058 insn_ptr = read_uleb128 (insn_ptr, &reg);
1059 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1060 if (UNWIND_COLUMN_IN_RANGE (reg))
1061 fs->regs.how[reg] = REG_UNSAVED;
1062 break;
1064 case DW_CFA_undefined:
1065 insn_ptr = read_uleb128 (insn_ptr, &reg);
1066 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1067 if (UNWIND_COLUMN_IN_RANGE (reg))
1068 fs->regs.how[reg] = REG_UNDEFINED;
1069 break;
1071 case DW_CFA_nop:
1072 break;
1074 case DW_CFA_register:
1076 _uleb128_t reg2;
1077 insn_ptr = read_uleb128 (insn_ptr, &reg);
1078 insn_ptr = read_uleb128 (insn_ptr, &reg2);
1079 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1080 if (UNWIND_COLUMN_IN_RANGE (reg))
1082 fs->regs.how[reg] = REG_SAVED_REG;
1083 fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2;
1086 break;
1088 case DW_CFA_remember_state:
1090 struct frame_state_reg_info *new_rs;
1091 if (unused_rs)
1093 new_rs = unused_rs;
1094 unused_rs = unused_rs->prev;
1096 else
1097 new_rs = alloca (sizeof (struct frame_state_reg_info));
1099 *new_rs = fs->regs;
1100 fs->regs.prev = new_rs;
1102 break;
1104 case DW_CFA_restore_state:
1106 struct frame_state_reg_info *old_rs = fs->regs.prev;
1107 fs->regs = *old_rs;
1108 old_rs->prev = unused_rs;
1109 unused_rs = old_rs;
1111 break;
1113 case DW_CFA_def_cfa:
1114 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1115 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1116 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1117 fs->regs.cfa_offset = (_Unwind_Word)utmp;
1118 fs->regs.cfa_how = CFA_REG_OFFSET;
1119 break;
1121 case DW_CFA_def_cfa_register:
1122 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1123 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1124 fs->regs.cfa_how = CFA_REG_OFFSET;
1125 break;
1127 case DW_CFA_def_cfa_offset:
1128 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1129 fs->regs.cfa_offset = utmp;
1130 /* cfa_how deliberately not set. */
1131 break;
1133 case DW_CFA_def_cfa_expression:
1134 fs->regs.cfa_exp = insn_ptr;
1135 fs->regs.cfa_how = CFA_EXP;
1136 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1137 insn_ptr += utmp;
1138 break;
1140 case DW_CFA_expression:
1141 insn_ptr = read_uleb128 (insn_ptr, &reg);
1142 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1143 if (UNWIND_COLUMN_IN_RANGE (reg))
1145 fs->regs.how[reg] = REG_SAVED_EXP;
1146 fs->regs.reg[reg].loc.exp = insn_ptr;
1148 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1149 insn_ptr += utmp;
1150 break;
1152 /* Dwarf3. */
1153 case DW_CFA_offset_extended_sf:
1154 insn_ptr = read_uleb128 (insn_ptr, &reg);
1155 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1156 offset = stmp * fs->data_align;
1157 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1158 if (UNWIND_COLUMN_IN_RANGE (reg))
1160 fs->regs.how[reg] = REG_SAVED_OFFSET;
1161 fs->regs.reg[reg].loc.offset = offset;
1163 break;
1165 case DW_CFA_def_cfa_sf:
1166 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1167 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1168 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1169 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1170 fs->regs.cfa_how = CFA_REG_OFFSET;
1171 fs->regs.cfa_offset *= fs->data_align;
1172 break;
1174 case DW_CFA_def_cfa_offset_sf:
1175 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1176 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1177 fs->regs.cfa_offset *= fs->data_align;
1178 /* cfa_how deliberately not set. */
1179 break;
1181 case DW_CFA_val_offset:
1182 insn_ptr = read_uleb128 (insn_ptr, &reg);
1183 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1184 offset = (_Unwind_Sword) utmp * fs->data_align;
1185 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1186 if (UNWIND_COLUMN_IN_RANGE (reg))
1188 fs->regs.how[reg] = REG_SAVED_VAL_OFFSET;
1189 fs->regs.reg[reg].loc.offset = offset;
1191 break;
1193 case DW_CFA_val_offset_sf:
1194 insn_ptr = read_uleb128 (insn_ptr, &reg);
1195 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1196 offset = stmp * fs->data_align;
1197 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1198 if (UNWIND_COLUMN_IN_RANGE (reg))
1200 fs->regs.how[reg] = REG_SAVED_VAL_OFFSET;
1201 fs->regs.reg[reg].loc.offset = offset;
1203 break;
1205 case DW_CFA_val_expression:
1206 insn_ptr = read_uleb128 (insn_ptr, &reg);
1207 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1208 if (UNWIND_COLUMN_IN_RANGE (reg))
1210 fs->regs.how[reg] = REG_SAVED_VAL_EXP;
1211 fs->regs.reg[reg].loc.exp = insn_ptr;
1213 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1214 insn_ptr += utmp;
1215 break;
1217 case DW_CFA_GNU_window_save:
1218 #if defined (__aarch64__) && !defined (__ILP32__)
1219 /* This CFA is multiplexed with Sparc. On AArch64 it's used to toggle
1220 return address signing status. */
1221 reg = DWARF_REGNUM_AARCH64_RA_STATE;
1222 gcc_assert (fs->regs.how[reg] == REG_UNSAVED);
1223 fs->regs.reg[reg].loc.offset ^= 1;
1224 #else
1225 /* ??? Hardcoded for SPARC register window configuration. */
1226 if (__LIBGCC_DWARF_FRAME_REGISTERS__ >= 32)
1227 for (reg = 16; reg < 32; ++reg)
1229 fs->regs.how[reg] = REG_SAVED_OFFSET;
1230 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1232 #endif
1233 break;
1235 case DW_CFA_GNU_args_size:
1236 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1237 context->args_size = (_Unwind_Word)utmp;
1238 break;
1240 case DW_CFA_GNU_negative_offset_extended:
1241 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1242 older PowerPC code. */
1243 insn_ptr = read_uleb128 (insn_ptr, &reg);
1244 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1245 offset = (_Unwind_Word) utmp * fs->data_align;
1246 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1247 if (UNWIND_COLUMN_IN_RANGE (reg))
1249 fs->regs.how[reg] = REG_SAVED_OFFSET;
1250 fs->regs.reg[reg].loc.offset = -offset;
1252 break;
1254 default:
1255 gcc_unreachable ();
1260 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1261 its caller and decode it into FS. This function also sets the
1262 args_size and lsda members of CONTEXT, as they are really information
1263 about the caller's frame. */
1265 static _Unwind_Reason_Code
1266 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1268 const struct dwarf_fde *fde;
1269 const struct dwarf_cie *cie;
1270 const unsigned char *aug, *insn, *end;
1272 memset (&fs->regs.how[0], 0,
1273 sizeof (*fs) - offsetof (_Unwind_FrameState, regs.how[0]));
1274 context->args_size = 0;
1275 context->lsda = 0;
1277 if (context->ra == 0)
1278 return _URC_END_OF_STACK;
1280 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1281 &context->bases);
1282 if (fde == NULL)
1284 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1285 /* Couldn't find frame unwind info for this function. Try a
1286 target-specific fallback mechanism. This will necessarily
1287 not provide a personality routine or LSDA. */
1288 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1289 #else
1290 return _URC_END_OF_STACK;
1291 #endif
1294 fs->pc = context->bases.func;
1296 cie = get_cie (fde);
1297 insn = extract_cie_info (cie, context, fs);
1298 if (insn == NULL)
1299 /* CIE contained unknown augmentation. */
1300 return _URC_FATAL_PHASE1_ERROR;
1302 /* First decode all the insns in the CIE. */
1303 end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
1304 execute_cfa_program (insn, end, context, fs);
1306 /* Locate augmentation for the fde. */
1307 aug = (const unsigned char *) fde + sizeof (*fde);
1308 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1309 insn = NULL;
1310 if (fs->saw_z)
1312 _uleb128_t i;
1313 aug = read_uleb128 (aug, &i);
1314 insn = aug + i;
1316 if (fs->lsda_encoding != DW_EH_PE_omit)
1318 _Unwind_Ptr lsda;
1320 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1321 context->lsda = (void *) lsda;
1324 /* Then the insns in the FDE up to our target PC. */
1325 if (insn == NULL)
1326 insn = aug;
1327 end = (const unsigned char *) next_fde (fde);
1328 execute_cfa_program (insn, end, context, fs);
1330 return _URC_NO_REASON;
1333 typedef struct frame_state
1335 void *cfa;
1336 void *eh_ptr;
1337 long cfa_offset;
1338 long args_size;
1339 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1340 unsigned short cfa_reg;
1341 unsigned short retaddr_column;
1342 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1343 } frame_state;
1345 struct frame_state * __frame_state_for (void *, struct frame_state *);
1347 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1348 a given PC_TARGET. The caller should allocate a local variable of
1349 `struct frame_state' and pass its address to STATE_IN. */
1351 struct frame_state *
1352 __frame_state_for (void *pc_target, struct frame_state *state_in)
1354 struct _Unwind_Context context;
1355 _Unwind_FrameState fs;
1356 int reg;
1358 memset (&context, 0, sizeof (struct _Unwind_Context));
1359 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1360 context.flags = EXTENDED_CONTEXT_BIT;
1361 context.ra = pc_target + 1;
1363 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1364 return 0;
1366 /* We have no way to pass a location expression for the CFA to our
1367 caller. It wouldn't understand it anyway. */
1368 if (fs.regs.cfa_how == CFA_EXP)
1369 return 0;
1371 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1373 state_in->saved[reg] = fs.regs.how[reg];
1374 switch (state_in->saved[reg])
1376 case REG_SAVED_REG:
1377 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1378 break;
1379 case REG_SAVED_OFFSET:
1380 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1381 break;
1382 default:
1383 state_in->reg_or_offset[reg] = 0;
1384 break;
1388 state_in->cfa_offset = fs.regs.cfa_offset;
1389 state_in->cfa_reg = fs.regs.cfa_reg;
1390 state_in->retaddr_column = fs.retaddr_column;
1391 state_in->args_size = context.args_size;
1392 state_in->eh_ptr = fs.eh_ptr;
1394 return state_in;
1397 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1399 static inline void
1400 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1401 _Unwind_SpTmp *tmp_sp)
1403 int size = dwarf_reg_size (__builtin_dwarf_sp_column ());
1405 if (size == sizeof(_Unwind_Ptr))
1406 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1407 else
1409 gcc_assert (size == sizeof(_Unwind_Word));
1410 tmp_sp->word = (_Unwind_Ptr) cfa;
1412 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1415 static void
1416 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1418 struct _Unwind_Context orig_context = *context;
1419 void *cfa;
1420 long i;
1422 #ifdef __LIBGCC_EH_RETURN_STACKADJ_RTX__
1423 /* Special handling here: Many machines do not use a frame pointer,
1424 and track the CFA only through offsets from the stack pointer from
1425 one frame to the next. In this case, the stack pointer is never
1426 stored, so it has no saved address in the context. What we do
1427 have is the CFA from the previous stack frame.
1429 In very special situations (such as unwind info for signal return),
1430 there may be location expressions that use the stack pointer as well.
1432 Do this conditionally for one frame. This allows the unwind info
1433 for one frame to save a copy of the stack pointer from the previous
1434 frame, and be able to use much easier CFA mechanisms to do it.
1435 Always zap the saved stack pointer value for the next frame; carrying
1436 the value over from one frame to another doesn't make sense. */
1438 _Unwind_SpTmp tmp_sp;
1440 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1441 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1442 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1443 #endif
1445 /* Compute this frame's CFA. */
1446 switch (fs->regs.cfa_how)
1448 case CFA_REG_OFFSET:
1449 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1450 cfa += fs->regs.cfa_offset;
1451 break;
1453 case CFA_EXP:
1455 const unsigned char *exp = fs->regs.cfa_exp;
1456 _uleb128_t len;
1458 exp = read_uleb128 (exp, &len);
1459 cfa = (void *) (_Unwind_Ptr)
1460 execute_stack_op (exp, exp + len, &orig_context, 0);
1461 break;
1464 default:
1465 gcc_unreachable ();
1467 context->cfa = cfa;
1469 /* Compute the addresses of all registers saved in this frame. */
1470 for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__ + 1; ++i)
1471 switch (fs->regs.how[i])
1473 case REG_UNSAVED:
1474 case REG_UNDEFINED:
1475 break;
1477 case REG_SAVED_OFFSET:
1478 _Unwind_SetGRPtr (context, i,
1479 (void *) (cfa + fs->regs.reg[i].loc.offset));
1480 break;
1482 case REG_SAVED_REG:
1483 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1484 _Unwind_SetGRValue (context, i,
1485 _Unwind_GetGR (&orig_context,
1486 fs->regs.reg[i].loc.reg));
1487 else
1488 _Unwind_SetGRPtr (context, i,
1489 _Unwind_GetGRPtr (&orig_context,
1490 fs->regs.reg[i].loc.reg));
1491 break;
1493 case REG_SAVED_EXP:
1495 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1496 _uleb128_t len;
1497 _Unwind_Ptr val;
1499 exp = read_uleb128 (exp, &len);
1500 val = execute_stack_op (exp, exp + len, &orig_context,
1501 (_Unwind_Ptr) cfa);
1502 _Unwind_SetGRPtr (context, i, (void *) val);
1504 break;
1506 case REG_SAVED_VAL_OFFSET:
1507 _Unwind_SetGRValue (context, i,
1508 (_Unwind_Internal_Ptr)
1509 (cfa + fs->regs.reg[i].loc.offset));
1510 break;
1512 case REG_SAVED_VAL_EXP:
1514 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1515 _uleb128_t len;
1516 _Unwind_Ptr val;
1518 exp = read_uleb128 (exp, &len);
1519 val = execute_stack_op (exp, exp + len, &orig_context,
1520 (_Unwind_Ptr) cfa);
1521 _Unwind_SetGRValue (context, i, val);
1523 break;
1526 _Unwind_SetSignalFrame (context, fs->signal_frame);
1528 #ifdef MD_FROB_UPDATE_CONTEXT
1529 MD_FROB_UPDATE_CONTEXT (context, fs);
1530 #endif
1533 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1534 of its caller. Update CONTEXT to refer to the caller as well. Note
1535 that the args_size and lsda members are not updated here, but later in
1536 uw_frame_state_for. */
1538 static void
1539 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1541 uw_update_context_1 (context, fs);
1543 /* In general this unwinder doesn't make any distinction between
1544 undefined and same_value rule. Call-saved registers are assumed
1545 to have same_value rule by default and explicit undefined
1546 rule is handled like same_value. The only exception is
1547 DW_CFA_undefined on retaddr_column which is supposed to
1548 mark outermost frame in DWARF 3. */
1549 if (fs->regs.how[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)]
1550 == REG_UNDEFINED)
1551 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1552 stack frame. */
1553 context->ra = 0;
1554 else
1556 /* Compute the return address now, since the return address column
1557 can change from frame to frame. */
1558 void *ret_addr;
1559 #ifdef MD_DEMANGLE_RETURN_ADDR
1560 _Unwind_Word ra = _Unwind_GetGR (context, fs->retaddr_column);
1561 ret_addr = MD_DEMANGLE_RETURN_ADDR (context, fs, ra);
1562 #else
1563 ret_addr = _Unwind_GetPtr (context, fs->retaddr_column);
1564 #endif
1565 context->ra = __builtin_extract_return_addr (ret_addr);
1569 static void
1570 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1572 uw_update_context (context, fs);
1575 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1576 level will be the return address and the CFA. */
1578 #define uw_init_context(CONTEXT) \
1579 do \
1581 /* Do any necessary initialization to access arbitrary stack frames. \
1582 On the SPARC, this means flushing the register windows. */ \
1583 __builtin_unwind_init (); \
1584 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1585 __builtin_return_address (0)); \
1587 while (0)
1589 #ifndef __LIBGCC_DWARF_REG_SIZES_CONSTANT__
1590 static inline void
1591 init_dwarf_reg_size_table (void)
1593 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1595 #endif
1597 static void __attribute__((noinline))
1598 uw_init_context_1 (struct _Unwind_Context *context,
1599 void *outer_cfa, void *outer_ra)
1601 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1602 _Unwind_FrameState fs;
1603 _Unwind_SpTmp sp_slot;
1604 _Unwind_Reason_Code code;
1606 memset (context, 0, sizeof (struct _Unwind_Context));
1607 context->ra = ra;
1608 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1609 context->flags = EXTENDED_CONTEXT_BIT;
1611 code = uw_frame_state_for (context, &fs);
1612 gcc_assert (code == _URC_NO_REASON);
1614 #ifndef __LIBGCC_DWARF_REG_SIZES_CONSTANT__
1615 # if __GTHREADS
1617 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1618 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1619 && dwarf_reg_size_table[0] == 0)
1620 init_dwarf_reg_size_table ();
1622 # else
1623 if (dwarf_reg_size_table[0] == 0)
1624 init_dwarf_reg_size_table ();
1625 # endif
1626 #endif
1628 /* Force the frame state to use the known cfa value. */
1629 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1630 fs.regs.cfa_how = CFA_REG_OFFSET;
1631 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1632 fs.regs.cfa_offset = 0;
1634 uw_update_context_1 (context, &fs);
1636 /* If the return address column was saved in a register in the
1637 initialization context, then we can't see it in the given
1638 call frame data. So have the initialization context tell us. */
1639 context->ra = __builtin_extract_return_addr (outer_ra);
1642 static void _Unwind_DebugHook (void *, void *)
1643 __attribute__ ((__noinline__, __used__, __noclone__));
1645 /* This function is called during unwinding. It is intended as a hook
1646 for a debugger to intercept exceptions. CFA is the CFA of the
1647 target frame. HANDLER is the PC to which control will be
1648 transferred. */
1649 static void
1650 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
1651 void *handler __attribute__ ((__unused__)))
1653 /* We only want to use stap probes starting with v3. Earlier
1654 versions added too much startup cost. */
1655 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1656 STAP_PROBE2 (libgcc, unwind, cfa, handler);
1657 #else
1658 asm ("");
1659 #endif
1662 /* Install TARGET into CURRENT so that we can return to it. This is a
1663 macro because __builtin_eh_return must be invoked in the context of
1664 our caller. FRAMES is a number of frames to be unwind.
1665 _Unwind_Frames_Extra is a macro to do additional work during unwinding
1666 if needed, for example shadow stack pointer adjustment for Intel CET
1667 technology. */
1669 #define uw_install_context(CURRENT, TARGET, FRAMES) \
1670 do \
1672 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1673 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1674 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1675 _Unwind_Frames_Extra (FRAMES); \
1676 __builtin_eh_return (offset, handler); \
1678 while (0)
1680 static long
1681 uw_install_context_1 (struct _Unwind_Context *current,
1682 struct _Unwind_Context *target)
1684 long i;
1685 _Unwind_SpTmp sp_slot;
1687 /* If the target frame does not have a saved stack pointer,
1688 then set up the target's CFA. */
1689 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1690 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1692 for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__; ++i)
1694 void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
1695 void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
1697 gcc_assert (current->by_value[i] == 0);
1698 if (target->by_value[i] && c)
1700 _Unwind_Word w;
1701 _Unwind_Ptr p;
1702 if (dwarf_reg_size (i) == sizeof (_Unwind_Word))
1704 w = (_Unwind_Internal_Ptr) t;
1705 memcpy (c, &w, sizeof (_Unwind_Word));
1707 else
1709 gcc_assert (dwarf_reg_size (i) == sizeof (_Unwind_Ptr));
1710 p = (_Unwind_Internal_Ptr) t;
1711 memcpy (c, &p, sizeof (_Unwind_Ptr));
1714 else if (t && c && t != c)
1715 memcpy (c, t, dwarf_reg_size (i));
1718 /* If the current frame doesn't have a saved stack pointer, then we
1719 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1720 pointer value reloaded. */
1721 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1723 void *target_cfa;
1725 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1727 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1728 if (__LIBGCC_STACK_GROWS_DOWNWARD__)
1729 return target_cfa - current->cfa + target->args_size;
1730 else
1731 return current->cfa - target_cfa - target->args_size;
1733 return 0;
1736 static inline _Unwind_Ptr
1737 uw_identify_context (struct _Unwind_Context *context)
1739 /* The CFA is not sufficient to disambiguate the context of a function
1740 interrupted by a signal before establishing its frame and the context
1741 of the signal itself. */
1742 if (__LIBGCC_STACK_GROWS_DOWNWARD__)
1743 return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
1744 else
1745 return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
1749 #include "unwind.inc"
1751 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1752 alias (_Unwind_Backtrace);
1753 alias (_Unwind_DeleteException);
1754 alias (_Unwind_FindEnclosingFunction);
1755 alias (_Unwind_ForcedUnwind);
1756 alias (_Unwind_GetDataRelBase);
1757 alias (_Unwind_GetTextRelBase);
1758 alias (_Unwind_GetCFA);
1759 alias (_Unwind_GetGR);
1760 alias (_Unwind_GetIP);
1761 alias (_Unwind_GetLanguageSpecificData);
1762 alias (_Unwind_GetRegionStart);
1763 alias (_Unwind_RaiseException);
1764 alias (_Unwind_Resume);
1765 alias (_Unwind_Resume_or_Rethrow);
1766 alias (_Unwind_SetGR);
1767 alias (_Unwind_SetIP);
1768 #endif
1770 #endif /* !USING_SJLJ_EXCEPTIONS */