[PATCH AArch64 1/2] Add execution tests of vget_low and vget_high
[official-gcc.git] / libgcc / unwind-dw2.c
blob55fc4bca132ffec69cc999a7d7fb06e28d25eced
1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997-2014 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"
40 #ifdef HAVE_SYS_SDT_H
41 #include <sys/sdt.h>
42 #endif
44 #ifndef __USING_SJLJ_EXCEPTIONS__
46 #ifndef STACK_GROWS_DOWNWARD
47 #define STACK_GROWS_DOWNWARD 0
48 #else
49 #undef STACK_GROWS_DOWNWARD
50 #define STACK_GROWS_DOWNWARD 1
51 #endif
53 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
54 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
55 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
56 #endif
58 #ifndef DWARF_REG_TO_UNWIND_COLUMN
59 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
60 #endif
62 /* ??? For the public function interfaces, we tend to gcc_assert that the
63 column numbers are in range. For the dwarf2 unwind info this does happen,
64 although so far in a case that doesn't actually matter.
66 See PR49146, in which a call from x86_64 ms abi to x86_64 unix abi stores
67 the call-saved xmm registers and annotates them. We havn't bothered
68 providing support for the xmm registers for the x86_64 port primarily
69 because the 64-bit windows targets don't use dwarf2 unwind, using sjlj or
70 SEH instead. Adding the support for unix targets would generally be a
71 waste. However, some runtime libraries supplied with ICC do contain such
72 an unorthodox transition, as well as the unwind info to match. This loss
73 of register restoration doesn't matter in practice, because the exception
74 is caught in the native unix abi, where all of the xmm registers are
75 call clobbered.
77 Ideally, we'd record some bit to notice when we're failing to restore some
78 register recorded in the unwind info, but to do that we need annotation on
79 the unix->ms abi edge, so that we know when the register data may be
80 discarded. And since this edge is also within the ICC library, we're
81 unlikely to be able to get the new annotation.
83 Barring a magic solution to restore the ms abi defined 128-bit xmm registers
84 (as distictly opposed to the full runtime width) without causing extra
85 overhead for normal unix abis, the best solution seems to be to simply
86 ignore unwind data for unknown columns. */
88 #define UNWIND_COLUMN_IN_RANGE(x) \
89 __builtin_expect((x) <= DWARF_FRAME_REGISTERS, 1)
91 #ifdef REG_VALUE_IN_UNWIND_CONTEXT
92 typedef _Unwind_Word _Unwind_Context_Reg_Val;
94 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
95 #define ASSUME_EXTENDED_UNWIND_CONTEXT 1
96 #endif
98 static inline _Unwind_Word
99 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
101 return val;
104 static inline _Unwind_Context_Reg_Val
105 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
107 return val;
109 #else
110 typedef void *_Unwind_Context_Reg_Val;
112 static inline _Unwind_Word
113 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
115 return (_Unwind_Word) (_Unwind_Internal_Ptr) val;
118 static inline _Unwind_Context_Reg_Val
119 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
121 return (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) val;
123 #endif
125 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
126 #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
127 #endif
129 /* This is the register and unwind state for a particular frame. This
130 provides the information necessary to unwind up past a frame and return
131 to its caller. */
132 struct _Unwind_Context
134 _Unwind_Context_Reg_Val reg[DWARF_FRAME_REGISTERS+1];
135 void *cfa;
136 void *ra;
137 void *lsda;
138 struct dwarf_eh_bases bases;
139 /* Signal frame context. */
140 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
141 /* Context which has version/args_size/by_value fields. */
142 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 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[DWARF_FRAME_REGISTERS+1];
151 /* Byte size of every register managed by these routines. */
152 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
155 /* Read unaligned data from the instruction buffer. */
157 union unaligned
159 void *p;
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 *);
172 static inline void *
173 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
175 static inline int
176 read_1u (const void *p) { return *(const unsigned char *) p; }
178 static inline int
179 read_1s (const void *p) { return *(const signed char *) p; }
181 static inline int
182 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
184 static inline int
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; }
190 static inline int
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;
205 static inline void
206 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
208 if (val)
209 context->flags |= SIGNAL_FRAME_BIT;
210 else
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 INDEX as saved in CONTEXT. */
223 inline _Unwind_Word
224 _Unwind_GetGR (struct _Unwind_Context *context, int index)
226 int size;
227 _Unwind_Context_Reg_Val val;
229 #ifdef DWARF_ZERO_REG
230 if (index == DWARF_ZERO_REG)
231 return 0;
232 #endif
234 index = DWARF_REG_TO_UNWIND_COLUMN (index);
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 /* This will segfault if the register hasn't been saved. */
243 if (size == sizeof(_Unwind_Ptr))
244 return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
245 else
247 gcc_assert (size == sizeof(_Unwind_Word));
248 return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
252 static inline void *
253 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
255 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
258 /* Get the value of the CFA as saved in CONTEXT. */
260 _Unwind_Word
261 _Unwind_GetCFA (struct _Unwind_Context *context)
263 return (_Unwind_Ptr) context->cfa;
266 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
268 inline void
269 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
271 int size;
272 void *ptr;
274 index = DWARF_REG_TO_UNWIND_COLUMN (index);
275 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
276 size = dwarf_reg_size_table[index];
278 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
280 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
281 return;
284 ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
286 if (size == sizeof(_Unwind_Ptr))
287 * (_Unwind_Ptr *) ptr = val;
288 else
290 gcc_assert (size == sizeof(_Unwind_Word));
291 * (_Unwind_Word *) ptr = val;
295 /* Get the pointer to a register INDEX as saved in CONTEXT. */
297 static inline void *
298 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
300 index = DWARF_REG_TO_UNWIND_COLUMN (index);
301 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
302 return &context->reg[index];
303 return (void *) (_Unwind_Internal_Ptr) context->reg[index];
306 /* Set the pointer to a register INDEX as saved in CONTEXT. */
308 static inline void
309 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
311 index = DWARF_REG_TO_UNWIND_COLUMN (index);
312 if (_Unwind_IsExtendedContext (context))
313 context->by_value[index] = 0;
314 context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
317 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
319 static inline void
320 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
321 _Unwind_Word val)
323 index = DWARF_REG_TO_UNWIND_COLUMN (index);
324 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
325 /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
326 gcc_assert (dwarf_reg_size_table[index] <= sizeof (_Unwind_Context_Reg_Val));
328 context->by_value[index] = 1;
329 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
332 /* Return nonzero if register INDEX is stored by value rather than
333 by reference. */
335 static inline int
336 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
338 index = DWARF_REG_TO_UNWIND_COLUMN (index);
339 return context->by_value[index];
342 /* Retrieve the return address for CONTEXT. */
344 inline _Unwind_Ptr
345 _Unwind_GetIP (struct _Unwind_Context *context)
347 return (_Unwind_Ptr) context->ra;
350 /* Retrieve the return address and flag whether that IP is before
351 or after first not yet fully executed instruction. */
353 inline _Unwind_Ptr
354 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
356 *ip_before_insn = _Unwind_IsSignalFrame (context);
357 return (_Unwind_Ptr) context->ra;
360 /* Overwrite the return address for CONTEXT with VAL. */
362 inline void
363 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
365 context->ra = (void *) val;
368 void *
369 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
371 return context->lsda;
374 _Unwind_Ptr
375 _Unwind_GetRegionStart (struct _Unwind_Context *context)
377 return (_Unwind_Ptr) context->bases.func;
380 void *
381 _Unwind_FindEnclosingFunction (void *pc)
383 struct dwarf_eh_bases bases;
384 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
385 if (fde)
386 return bases.func;
387 else
388 return NULL;
391 #ifndef __ia64__
392 _Unwind_Ptr
393 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
395 return (_Unwind_Ptr) context->bases.dbase;
398 _Unwind_Ptr
399 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
401 return (_Unwind_Ptr) context->bases.tbase;
403 #endif
405 #include "md-unwind-support.h"
407 /* Extract any interesting information from the CIE for the translation
408 unit F belongs to. Return a pointer to the byte after the augmentation,
409 or NULL if we encountered an undecipherable augmentation. */
411 static const unsigned char *
412 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
413 _Unwind_FrameState *fs)
415 const unsigned char *aug = cie->augmentation;
416 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
417 const unsigned char *ret = NULL;
418 _uleb128_t utmp;
419 _sleb128_t stmp;
421 /* g++ v2 "eh" has pointer immediately following augmentation string,
422 so it must be handled first. */
423 if (aug[0] == 'e' && aug[1] == 'h')
425 fs->eh_ptr = read_pointer (p);
426 p += sizeof (void *);
427 aug += 2;
430 /* After the augmentation resp. pointer for "eh" augmentation
431 follows for CIE version >= 4 address size byte and
432 segment size byte. */
433 if (__builtin_expect (cie->version >= 4, 0))
435 if (p[0] != sizeof (void *) || p[1] != 0)
436 return NULL;
437 p += 2;
439 /* Immediately following this are the code and
440 data alignment and return address column. */
441 p = read_uleb128 (p, &utmp);
442 fs->code_align = (_Unwind_Word)utmp;
443 p = read_sleb128 (p, &stmp);
444 fs->data_align = (_Unwind_Sword)stmp;
445 if (cie->version == 1)
446 fs->retaddr_column = *p++;
447 else
449 p = read_uleb128 (p, &utmp);
450 fs->retaddr_column = (_Unwind_Word)utmp;
452 fs->lsda_encoding = DW_EH_PE_omit;
454 /* If the augmentation starts with 'z', then a uleb128 immediately
455 follows containing the length of the augmentation field following
456 the size. */
457 if (*aug == 'z')
459 p = read_uleb128 (p, &utmp);
460 ret = p + utmp;
462 fs->saw_z = 1;
463 ++aug;
466 /* Iterate over recognized augmentation subsequences. */
467 while (*aug != '\0')
469 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
470 if (aug[0] == 'L')
472 fs->lsda_encoding = *p++;
473 aug += 1;
476 /* "R" indicates a byte indicating how FDE addresses are encoded. */
477 else if (aug[0] == 'R')
479 fs->fde_encoding = *p++;
480 aug += 1;
483 /* "P" indicates a personality routine in the CIE augmentation. */
484 else if (aug[0] == 'P')
486 _Unwind_Ptr personality;
488 p = read_encoded_value (context, *p, p + 1, &personality);
489 fs->personality = (_Unwind_Personality_Fn) personality;
490 aug += 1;
493 /* "S" indicates a signal frame. */
494 else if (aug[0] == 'S')
496 fs->signal_frame = 1;
497 aug += 1;
500 /* Otherwise we have an unknown augmentation string.
501 Bail unless we saw a 'z' prefix. */
502 else
503 return ret;
506 return ret ? ret : p;
510 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
511 onto the stack to start. */
513 static _Unwind_Word
514 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
515 struct _Unwind_Context *context, _Unwind_Word initial)
517 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
518 int stack_elt;
520 stack[0] = initial;
521 stack_elt = 1;
523 while (op_ptr < op_end)
525 enum dwarf_location_atom op = *op_ptr++;
526 _Unwind_Word result;
527 _uleb128_t reg, utmp;
528 _sleb128_t offset, stmp;
530 switch (op)
532 case DW_OP_lit0:
533 case DW_OP_lit1:
534 case DW_OP_lit2:
535 case DW_OP_lit3:
536 case DW_OP_lit4:
537 case DW_OP_lit5:
538 case DW_OP_lit6:
539 case DW_OP_lit7:
540 case DW_OP_lit8:
541 case DW_OP_lit9:
542 case DW_OP_lit10:
543 case DW_OP_lit11:
544 case DW_OP_lit12:
545 case DW_OP_lit13:
546 case DW_OP_lit14:
547 case DW_OP_lit15:
548 case DW_OP_lit16:
549 case DW_OP_lit17:
550 case DW_OP_lit18:
551 case DW_OP_lit19:
552 case DW_OP_lit20:
553 case DW_OP_lit21:
554 case DW_OP_lit22:
555 case DW_OP_lit23:
556 case DW_OP_lit24:
557 case DW_OP_lit25:
558 case DW_OP_lit26:
559 case DW_OP_lit27:
560 case DW_OP_lit28:
561 case DW_OP_lit29:
562 case DW_OP_lit30:
563 case DW_OP_lit31:
564 result = op - DW_OP_lit0;
565 break;
567 case DW_OP_addr:
568 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
569 op_ptr += sizeof (void *);
570 break;
572 case DW_OP_GNU_encoded_addr:
574 _Unwind_Ptr presult;
575 op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
576 result = presult;
578 break;
580 case DW_OP_const1u:
581 result = read_1u (op_ptr);
582 op_ptr += 1;
583 break;
584 case DW_OP_const1s:
585 result = read_1s (op_ptr);
586 op_ptr += 1;
587 break;
588 case DW_OP_const2u:
589 result = read_2u (op_ptr);
590 op_ptr += 2;
591 break;
592 case DW_OP_const2s:
593 result = read_2s (op_ptr);
594 op_ptr += 2;
595 break;
596 case DW_OP_const4u:
597 result = read_4u (op_ptr);
598 op_ptr += 4;
599 break;
600 case DW_OP_const4s:
601 result = read_4s (op_ptr);
602 op_ptr += 4;
603 break;
604 case DW_OP_const8u:
605 result = read_8u (op_ptr);
606 op_ptr += 8;
607 break;
608 case DW_OP_const8s:
609 result = read_8s (op_ptr);
610 op_ptr += 8;
611 break;
612 case DW_OP_constu:
613 op_ptr = read_uleb128 (op_ptr, &utmp);
614 result = (_Unwind_Word)utmp;
615 break;
616 case DW_OP_consts:
617 op_ptr = read_sleb128 (op_ptr, &stmp);
618 result = (_Unwind_Sword)stmp;
619 break;
621 case DW_OP_reg0:
622 case DW_OP_reg1:
623 case DW_OP_reg2:
624 case DW_OP_reg3:
625 case DW_OP_reg4:
626 case DW_OP_reg5:
627 case DW_OP_reg6:
628 case DW_OP_reg7:
629 case DW_OP_reg8:
630 case DW_OP_reg9:
631 case DW_OP_reg10:
632 case DW_OP_reg11:
633 case DW_OP_reg12:
634 case DW_OP_reg13:
635 case DW_OP_reg14:
636 case DW_OP_reg15:
637 case DW_OP_reg16:
638 case DW_OP_reg17:
639 case DW_OP_reg18:
640 case DW_OP_reg19:
641 case DW_OP_reg20:
642 case DW_OP_reg21:
643 case DW_OP_reg22:
644 case DW_OP_reg23:
645 case DW_OP_reg24:
646 case DW_OP_reg25:
647 case DW_OP_reg26:
648 case DW_OP_reg27:
649 case DW_OP_reg28:
650 case DW_OP_reg29:
651 case DW_OP_reg30:
652 case DW_OP_reg31:
653 result = _Unwind_GetGR (context, op - DW_OP_reg0);
654 break;
655 case DW_OP_regx:
656 op_ptr = read_uleb128 (op_ptr, &reg);
657 result = _Unwind_GetGR (context, reg);
658 break;
660 case DW_OP_breg0:
661 case DW_OP_breg1:
662 case DW_OP_breg2:
663 case DW_OP_breg3:
664 case DW_OP_breg4:
665 case DW_OP_breg5:
666 case DW_OP_breg6:
667 case DW_OP_breg7:
668 case DW_OP_breg8:
669 case DW_OP_breg9:
670 case DW_OP_breg10:
671 case DW_OP_breg11:
672 case DW_OP_breg12:
673 case DW_OP_breg13:
674 case DW_OP_breg14:
675 case DW_OP_breg15:
676 case DW_OP_breg16:
677 case DW_OP_breg17:
678 case DW_OP_breg18:
679 case DW_OP_breg19:
680 case DW_OP_breg20:
681 case DW_OP_breg21:
682 case DW_OP_breg22:
683 case DW_OP_breg23:
684 case DW_OP_breg24:
685 case DW_OP_breg25:
686 case DW_OP_breg26:
687 case DW_OP_breg27:
688 case DW_OP_breg28:
689 case DW_OP_breg29:
690 case DW_OP_breg30:
691 case DW_OP_breg31:
692 op_ptr = read_sleb128 (op_ptr, &offset);
693 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
694 break;
695 case DW_OP_bregx:
696 op_ptr = read_uleb128 (op_ptr, &reg);
697 op_ptr = read_sleb128 (op_ptr, &offset);
698 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
699 break;
701 case DW_OP_dup:
702 gcc_assert (stack_elt);
703 result = stack[stack_elt - 1];
704 break;
706 case DW_OP_drop:
707 gcc_assert (stack_elt);
708 stack_elt -= 1;
709 goto no_push;
711 case DW_OP_pick:
712 offset = *op_ptr++;
713 gcc_assert (offset < stack_elt - 1);
714 result = stack[stack_elt - 1 - offset];
715 break;
717 case DW_OP_over:
718 gcc_assert (stack_elt >= 2);
719 result = stack[stack_elt - 2];
720 break;
722 case DW_OP_swap:
724 _Unwind_Word t;
725 gcc_assert (stack_elt >= 2);
726 t = stack[stack_elt - 1];
727 stack[stack_elt - 1] = stack[stack_elt - 2];
728 stack[stack_elt - 2] = t;
729 goto no_push;
732 case DW_OP_rot:
734 _Unwind_Word t1, t2, t3;
736 gcc_assert (stack_elt >= 3);
737 t1 = stack[stack_elt - 1];
738 t2 = stack[stack_elt - 2];
739 t3 = stack[stack_elt - 3];
740 stack[stack_elt - 1] = t2;
741 stack[stack_elt - 2] = t3;
742 stack[stack_elt - 3] = t1;
743 goto no_push;
746 case DW_OP_deref:
747 case DW_OP_deref_size:
748 case DW_OP_abs:
749 case DW_OP_neg:
750 case DW_OP_not:
751 case DW_OP_plus_uconst:
752 /* Unary operations. */
753 gcc_assert (stack_elt);
754 stack_elt -= 1;
756 result = stack[stack_elt];
758 switch (op)
760 case DW_OP_deref:
762 void *ptr = (void *) (_Unwind_Ptr) result;
763 result = (_Unwind_Ptr) read_pointer (ptr);
765 break;
767 case DW_OP_deref_size:
769 void *ptr = (void *) (_Unwind_Ptr) result;
770 switch (*op_ptr++)
772 case 1:
773 result = read_1u (ptr);
774 break;
775 case 2:
776 result = read_2u (ptr);
777 break;
778 case 4:
779 result = read_4u (ptr);
780 break;
781 case 8:
782 result = read_8u (ptr);
783 break;
784 default:
785 gcc_unreachable ();
788 break;
790 case DW_OP_abs:
791 if ((_Unwind_Sword) result < 0)
792 result = -result;
793 break;
794 case DW_OP_neg:
795 result = -result;
796 break;
797 case DW_OP_not:
798 result = ~result;
799 break;
800 case DW_OP_plus_uconst:
801 op_ptr = read_uleb128 (op_ptr, &utmp);
802 result += (_Unwind_Word)utmp;
803 break;
805 default:
806 gcc_unreachable ();
808 break;
810 case DW_OP_and:
811 case DW_OP_div:
812 case DW_OP_minus:
813 case DW_OP_mod:
814 case DW_OP_mul:
815 case DW_OP_or:
816 case DW_OP_plus:
817 case DW_OP_shl:
818 case DW_OP_shr:
819 case DW_OP_shra:
820 case DW_OP_xor:
821 case DW_OP_le:
822 case DW_OP_ge:
823 case DW_OP_eq:
824 case DW_OP_lt:
825 case DW_OP_gt:
826 case DW_OP_ne:
828 /* Binary operations. */
829 _Unwind_Word first, second;
830 gcc_assert (stack_elt >= 2);
831 stack_elt -= 2;
833 second = stack[stack_elt];
834 first = stack[stack_elt + 1];
836 switch (op)
838 case DW_OP_and:
839 result = second & first;
840 break;
841 case DW_OP_div:
842 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
843 break;
844 case DW_OP_minus:
845 result = second - first;
846 break;
847 case DW_OP_mod:
848 result = second % first;
849 break;
850 case DW_OP_mul:
851 result = second * first;
852 break;
853 case DW_OP_or:
854 result = second | first;
855 break;
856 case DW_OP_plus:
857 result = second + first;
858 break;
859 case DW_OP_shl:
860 result = second << first;
861 break;
862 case DW_OP_shr:
863 result = second >> first;
864 break;
865 case DW_OP_shra:
866 result = (_Unwind_Sword) second >> first;
867 break;
868 case DW_OP_xor:
869 result = second ^ first;
870 break;
871 case DW_OP_le:
872 result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
873 break;
874 case DW_OP_ge:
875 result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
876 break;
877 case DW_OP_eq:
878 result = (_Unwind_Sword) second == (_Unwind_Sword) first;
879 break;
880 case DW_OP_lt:
881 result = (_Unwind_Sword) second < (_Unwind_Sword) first;
882 break;
883 case DW_OP_gt:
884 result = (_Unwind_Sword) second > (_Unwind_Sword) first;
885 break;
886 case DW_OP_ne:
887 result = (_Unwind_Sword) second != (_Unwind_Sword) first;
888 break;
890 default:
891 gcc_unreachable ();
894 break;
896 case DW_OP_skip:
897 offset = read_2s (op_ptr);
898 op_ptr += 2;
899 op_ptr += offset;
900 goto no_push;
902 case DW_OP_bra:
903 gcc_assert (stack_elt);
904 stack_elt -= 1;
906 offset = read_2s (op_ptr);
907 op_ptr += 2;
908 if (stack[stack_elt] != 0)
909 op_ptr += offset;
910 goto no_push;
912 case DW_OP_nop:
913 goto no_push;
915 default:
916 gcc_unreachable ();
919 /* Most things push a result value. */
920 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
921 stack[stack_elt++] = result;
922 no_push:;
925 /* We were executing this program to get a value. It should be
926 at top of stack. */
927 gcc_assert (stack_elt);
928 stack_elt -= 1;
929 return stack[stack_elt];
933 /* Decode DWARF 2 call frame information. Takes pointers the
934 instruction sequence to decode, current register information and
935 CIE info, and the PC range to evaluate. */
937 static void
938 execute_cfa_program (const unsigned char *insn_ptr,
939 const unsigned char *insn_end,
940 struct _Unwind_Context *context,
941 _Unwind_FrameState *fs)
943 struct frame_state_reg_info *unused_rs = NULL;
945 /* Don't allow remember/restore between CIE and FDE programs. */
946 fs->regs.prev = NULL;
948 /* The comparison with the return address uses < rather than <= because
949 we are only interested in the effects of code before the call; for a
950 noreturn function, the return address may point to unrelated code with
951 a different stack configuration that we are not interested in. We
952 assume that the call itself is unwind info-neutral; if not, or if
953 there are delay instructions that adjust the stack, these must be
954 reflected at the point immediately before the call insn.
955 In signal frames, return address is after last completed instruction,
956 so we add 1 to return address to make the comparison <=. */
957 while (insn_ptr < insn_end
958 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
960 unsigned char insn = *insn_ptr++;
961 _uleb128_t reg, utmp;
962 _sleb128_t offset, stmp;
964 if ((insn & 0xc0) == DW_CFA_advance_loc)
965 fs->pc += (insn & 0x3f) * fs->code_align;
966 else if ((insn & 0xc0) == DW_CFA_offset)
968 reg = insn & 0x3f;
969 insn_ptr = read_uleb128 (insn_ptr, &utmp);
970 offset = (_Unwind_Sword) utmp * fs->data_align;
971 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
972 if (UNWIND_COLUMN_IN_RANGE (reg))
974 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
975 fs->regs.reg[reg].loc.offset = offset;
978 else if ((insn & 0xc0) == DW_CFA_restore)
980 reg = insn & 0x3f;
981 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
982 if (UNWIND_COLUMN_IN_RANGE (reg))
983 fs->regs.reg[reg].how = REG_UNSAVED;
985 else switch (insn)
987 case DW_CFA_set_loc:
989 _Unwind_Ptr pc;
991 insn_ptr = read_encoded_value (context, fs->fde_encoding,
992 insn_ptr, &pc);
993 fs->pc = (void *) pc;
995 break;
997 case DW_CFA_advance_loc1:
998 fs->pc += read_1u (insn_ptr) * fs->code_align;
999 insn_ptr += 1;
1000 break;
1001 case DW_CFA_advance_loc2:
1002 fs->pc += read_2u (insn_ptr) * fs->code_align;
1003 insn_ptr += 2;
1004 break;
1005 case DW_CFA_advance_loc4:
1006 fs->pc += read_4u (insn_ptr) * fs->code_align;
1007 insn_ptr += 4;
1008 break;
1010 case DW_CFA_offset_extended:
1011 insn_ptr = read_uleb128 (insn_ptr, &reg);
1012 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1013 offset = (_Unwind_Sword) utmp * fs->data_align;
1014 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1015 if (UNWIND_COLUMN_IN_RANGE (reg))
1017 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1018 fs->regs.reg[reg].loc.offset = offset;
1020 break;
1022 case DW_CFA_restore_extended:
1023 insn_ptr = read_uleb128 (insn_ptr, &reg);
1024 /* FIXME, this is wrong; the CIE might have said that the
1025 register was saved somewhere. */
1026 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1027 if (UNWIND_COLUMN_IN_RANGE (reg))
1028 fs->regs.reg[reg].how = REG_UNSAVED;
1029 break;
1031 case DW_CFA_same_value:
1032 insn_ptr = read_uleb128 (insn_ptr, &reg);
1033 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1034 if (UNWIND_COLUMN_IN_RANGE (reg))
1035 fs->regs.reg[reg].how = REG_UNSAVED;
1036 break;
1038 case DW_CFA_undefined:
1039 insn_ptr = read_uleb128 (insn_ptr, &reg);
1040 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1041 if (UNWIND_COLUMN_IN_RANGE (reg))
1042 fs->regs.reg[reg].how = REG_UNDEFINED;
1043 break;
1045 case DW_CFA_nop:
1046 break;
1048 case DW_CFA_register:
1050 _uleb128_t reg2;
1051 insn_ptr = read_uleb128 (insn_ptr, &reg);
1052 insn_ptr = read_uleb128 (insn_ptr, &reg2);
1053 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1054 if (UNWIND_COLUMN_IN_RANGE (reg))
1056 fs->regs.reg[reg].how = REG_SAVED_REG;
1057 fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2;
1060 break;
1062 case DW_CFA_remember_state:
1064 struct frame_state_reg_info *new_rs;
1065 if (unused_rs)
1067 new_rs = unused_rs;
1068 unused_rs = unused_rs->prev;
1070 else
1071 new_rs = alloca (sizeof (struct frame_state_reg_info));
1073 *new_rs = fs->regs;
1074 fs->regs.prev = new_rs;
1076 break;
1078 case DW_CFA_restore_state:
1080 struct frame_state_reg_info *old_rs = fs->regs.prev;
1081 fs->regs = *old_rs;
1082 old_rs->prev = unused_rs;
1083 unused_rs = old_rs;
1085 break;
1087 case DW_CFA_def_cfa:
1088 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1089 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1090 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1091 fs->regs.cfa_offset = (_Unwind_Word)utmp;
1092 fs->regs.cfa_how = CFA_REG_OFFSET;
1093 break;
1095 case DW_CFA_def_cfa_register:
1096 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1097 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1098 fs->regs.cfa_how = CFA_REG_OFFSET;
1099 break;
1101 case DW_CFA_def_cfa_offset:
1102 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1103 fs->regs.cfa_offset = utmp;
1104 /* cfa_how deliberately not set. */
1105 break;
1107 case DW_CFA_def_cfa_expression:
1108 fs->regs.cfa_exp = insn_ptr;
1109 fs->regs.cfa_how = CFA_EXP;
1110 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1111 insn_ptr += utmp;
1112 break;
1114 case DW_CFA_expression:
1115 insn_ptr = read_uleb128 (insn_ptr, &reg);
1116 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1117 if (UNWIND_COLUMN_IN_RANGE (reg))
1119 fs->regs.reg[reg].how = REG_SAVED_EXP;
1120 fs->regs.reg[reg].loc.exp = insn_ptr;
1122 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1123 insn_ptr += utmp;
1124 break;
1126 /* Dwarf3. */
1127 case DW_CFA_offset_extended_sf:
1128 insn_ptr = read_uleb128 (insn_ptr, &reg);
1129 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1130 offset = stmp * fs->data_align;
1131 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1132 if (UNWIND_COLUMN_IN_RANGE (reg))
1134 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1135 fs->regs.reg[reg].loc.offset = offset;
1137 break;
1139 case DW_CFA_def_cfa_sf:
1140 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1141 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1142 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1143 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1144 fs->regs.cfa_how = CFA_REG_OFFSET;
1145 fs->regs.cfa_offset *= fs->data_align;
1146 break;
1148 case DW_CFA_def_cfa_offset_sf:
1149 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1150 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1151 fs->regs.cfa_offset *= fs->data_align;
1152 /* cfa_how deliberately not set. */
1153 break;
1155 case DW_CFA_val_offset:
1156 insn_ptr = read_uleb128 (insn_ptr, &reg);
1157 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1158 offset = (_Unwind_Sword) utmp * fs->data_align;
1159 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1160 if (UNWIND_COLUMN_IN_RANGE (reg))
1162 fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
1163 fs->regs.reg[reg].loc.offset = offset;
1165 break;
1167 case DW_CFA_val_offset_sf:
1168 insn_ptr = read_uleb128 (insn_ptr, &reg);
1169 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1170 offset = stmp * fs->data_align;
1171 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1172 if (UNWIND_COLUMN_IN_RANGE (reg))
1174 fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
1175 fs->regs.reg[reg].loc.offset = offset;
1177 break;
1179 case DW_CFA_val_expression:
1180 insn_ptr = read_uleb128 (insn_ptr, &reg);
1181 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1182 if (UNWIND_COLUMN_IN_RANGE (reg))
1184 fs->regs.reg[reg].how = REG_SAVED_VAL_EXP;
1185 fs->regs.reg[reg].loc.exp = insn_ptr;
1187 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1188 insn_ptr += utmp;
1189 break;
1191 case DW_CFA_GNU_window_save:
1192 /* ??? Hardcoded for SPARC register window configuration. */
1193 if (DWARF_FRAME_REGISTERS >= 32)
1194 for (reg = 16; reg < 32; ++reg)
1196 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1197 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1199 break;
1201 case DW_CFA_GNU_args_size:
1202 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1203 context->args_size = (_Unwind_Word)utmp;
1204 break;
1206 case DW_CFA_GNU_negative_offset_extended:
1207 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1208 older PowerPC code. */
1209 insn_ptr = read_uleb128 (insn_ptr, &reg);
1210 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1211 offset = (_Unwind_Word) utmp * fs->data_align;
1212 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1213 if (UNWIND_COLUMN_IN_RANGE (reg))
1215 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1216 fs->regs.reg[reg].loc.offset = -offset;
1218 break;
1220 default:
1221 gcc_unreachable ();
1226 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1227 its caller and decode it into FS. This function also sets the
1228 args_size and lsda members of CONTEXT, as they are really information
1229 about the caller's frame. */
1231 static _Unwind_Reason_Code
1232 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1234 const struct dwarf_fde *fde;
1235 const struct dwarf_cie *cie;
1236 const unsigned char *aug, *insn, *end;
1238 memset (fs, 0, sizeof (*fs));
1239 context->args_size = 0;
1240 context->lsda = 0;
1242 if (context->ra == 0)
1243 return _URC_END_OF_STACK;
1245 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1246 &context->bases);
1247 if (fde == NULL)
1249 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1250 /* Couldn't find frame unwind info for this function. Try a
1251 target-specific fallback mechanism. This will necessarily
1252 not provide a personality routine or LSDA. */
1253 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1254 #else
1255 return _URC_END_OF_STACK;
1256 #endif
1259 fs->pc = context->bases.func;
1261 cie = get_cie (fde);
1262 insn = extract_cie_info (cie, context, fs);
1263 if (insn == NULL)
1264 /* CIE contained unknown augmentation. */
1265 return _URC_FATAL_PHASE1_ERROR;
1267 /* First decode all the insns in the CIE. */
1268 end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
1269 execute_cfa_program (insn, end, context, fs);
1271 /* Locate augmentation for the fde. */
1272 aug = (const unsigned char *) fde + sizeof (*fde);
1273 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1274 insn = NULL;
1275 if (fs->saw_z)
1277 _uleb128_t i;
1278 aug = read_uleb128 (aug, &i);
1279 insn = aug + i;
1281 if (fs->lsda_encoding != DW_EH_PE_omit)
1283 _Unwind_Ptr lsda;
1285 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1286 context->lsda = (void *) lsda;
1289 /* Then the insns in the FDE up to our target PC. */
1290 if (insn == NULL)
1291 insn = aug;
1292 end = (const unsigned char *) next_fde (fde);
1293 execute_cfa_program (insn, end, context, fs);
1295 return _URC_NO_REASON;
1298 typedef struct frame_state
1300 void *cfa;
1301 void *eh_ptr;
1302 long cfa_offset;
1303 long args_size;
1304 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1305 unsigned short cfa_reg;
1306 unsigned short retaddr_column;
1307 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1308 } frame_state;
1310 struct frame_state * __frame_state_for (void *, struct frame_state *);
1312 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1313 a given PC_TARGET. The caller should allocate a local variable of
1314 `struct frame_state' and pass its address to STATE_IN. */
1316 struct frame_state *
1317 __frame_state_for (void *pc_target, struct frame_state *state_in)
1319 struct _Unwind_Context context;
1320 _Unwind_FrameState fs;
1321 int reg;
1323 memset (&context, 0, sizeof (struct _Unwind_Context));
1324 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1325 context.flags = EXTENDED_CONTEXT_BIT;
1326 context.ra = pc_target + 1;
1328 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1329 return 0;
1331 /* We have no way to pass a location expression for the CFA to our
1332 caller. It wouldn't understand it anyway. */
1333 if (fs.regs.cfa_how == CFA_EXP)
1334 return 0;
1336 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1338 state_in->saved[reg] = fs.regs.reg[reg].how;
1339 switch (state_in->saved[reg])
1341 case REG_SAVED_REG:
1342 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1343 break;
1344 case REG_SAVED_OFFSET:
1345 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1346 break;
1347 default:
1348 state_in->reg_or_offset[reg] = 0;
1349 break;
1353 state_in->cfa_offset = fs.regs.cfa_offset;
1354 state_in->cfa_reg = fs.regs.cfa_reg;
1355 state_in->retaddr_column = fs.retaddr_column;
1356 state_in->args_size = context.args_size;
1357 state_in->eh_ptr = fs.eh_ptr;
1359 return state_in;
1362 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1364 static inline void
1365 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1366 _Unwind_SpTmp *tmp_sp)
1368 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1370 if (size == sizeof(_Unwind_Ptr))
1371 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1372 else
1374 gcc_assert (size == sizeof(_Unwind_Word));
1375 tmp_sp->word = (_Unwind_Ptr) cfa;
1377 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1380 static void
1381 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1383 struct _Unwind_Context orig_context = *context;
1384 void *cfa;
1385 long i;
1387 #ifdef EH_RETURN_STACKADJ_RTX
1388 /* Special handling here: Many machines do not use a frame pointer,
1389 and track the CFA only through offsets from the stack pointer from
1390 one frame to the next. In this case, the stack pointer is never
1391 stored, so it has no saved address in the context. What we do
1392 have is the CFA from the previous stack frame.
1394 In very special situations (such as unwind info for signal return),
1395 there may be location expressions that use the stack pointer as well.
1397 Do this conditionally for one frame. This allows the unwind info
1398 for one frame to save a copy of the stack pointer from the previous
1399 frame, and be able to use much easier CFA mechanisms to do it.
1400 Always zap the saved stack pointer value for the next frame; carrying
1401 the value over from one frame to another doesn't make sense. */
1403 _Unwind_SpTmp tmp_sp;
1405 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1406 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1407 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1408 #endif
1410 /* Compute this frame's CFA. */
1411 switch (fs->regs.cfa_how)
1413 case CFA_REG_OFFSET:
1414 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1415 cfa += fs->regs.cfa_offset;
1416 break;
1418 case CFA_EXP:
1420 const unsigned char *exp = fs->regs.cfa_exp;
1421 _uleb128_t len;
1423 exp = read_uleb128 (exp, &len);
1424 cfa = (void *) (_Unwind_Ptr)
1425 execute_stack_op (exp, exp + len, &orig_context, 0);
1426 break;
1429 default:
1430 gcc_unreachable ();
1432 context->cfa = cfa;
1434 /* Compute the addresses of all registers saved in this frame. */
1435 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1436 switch (fs->regs.reg[i].how)
1438 case REG_UNSAVED:
1439 case REG_UNDEFINED:
1440 break;
1442 case REG_SAVED_OFFSET:
1443 _Unwind_SetGRPtr (context, i,
1444 (void *) (cfa + fs->regs.reg[i].loc.offset));
1445 break;
1447 case REG_SAVED_REG:
1448 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1449 _Unwind_SetGRValue (context, i,
1450 _Unwind_GetGR (&orig_context,
1451 fs->regs.reg[i].loc.reg));
1452 else
1453 _Unwind_SetGRPtr (context, i,
1454 _Unwind_GetGRPtr (&orig_context,
1455 fs->regs.reg[i].loc.reg));
1456 break;
1458 case REG_SAVED_EXP:
1460 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1461 _uleb128_t len;
1462 _Unwind_Ptr val;
1464 exp = read_uleb128 (exp, &len);
1465 val = execute_stack_op (exp, exp + len, &orig_context,
1466 (_Unwind_Ptr) cfa);
1467 _Unwind_SetGRPtr (context, i, (void *) val);
1469 break;
1471 case REG_SAVED_VAL_OFFSET:
1472 _Unwind_SetGRValue (context, i,
1473 (_Unwind_Internal_Ptr)
1474 (cfa + fs->regs.reg[i].loc.offset));
1475 break;
1477 case REG_SAVED_VAL_EXP:
1479 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1480 _uleb128_t len;
1481 _Unwind_Ptr val;
1483 exp = read_uleb128 (exp, &len);
1484 val = execute_stack_op (exp, exp + len, &orig_context,
1485 (_Unwind_Ptr) cfa);
1486 _Unwind_SetGRValue (context, i, val);
1488 break;
1491 _Unwind_SetSignalFrame (context, fs->signal_frame);
1493 #ifdef MD_FROB_UPDATE_CONTEXT
1494 MD_FROB_UPDATE_CONTEXT (context, fs);
1495 #endif
1498 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1499 of its caller. Update CONTEXT to refer to the caller as well. Note
1500 that the args_size and lsda members are not updated here, but later in
1501 uw_frame_state_for. */
1503 static void
1504 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1506 uw_update_context_1 (context, fs);
1508 /* In general this unwinder doesn't make any distinction between
1509 undefined and same_value rule. Call-saved registers are assumed
1510 to have same_value rule by default and explicit undefined
1511 rule is handled like same_value. The only exception is
1512 DW_CFA_undefined on retaddr_column which is supposed to
1513 mark outermost frame in DWARF 3. */
1514 if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
1515 == REG_UNDEFINED)
1516 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1517 stack frame. */
1518 context->ra = 0;
1519 else
1520 /* Compute the return address now, since the return address column
1521 can change from frame to frame. */
1522 context->ra = __builtin_extract_return_addr
1523 (_Unwind_GetPtr (context, fs->retaddr_column));
1526 static void
1527 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1529 uw_update_context (context, fs);
1532 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1533 level will be the return address and the CFA. */
1535 #define uw_init_context(CONTEXT) \
1536 do \
1538 /* Do any necessary initialization to access arbitrary stack frames. \
1539 On the SPARC, this means flushing the register windows. */ \
1540 __builtin_unwind_init (); \
1541 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1542 __builtin_return_address (0)); \
1544 while (0)
1546 static inline void
1547 init_dwarf_reg_size_table (void)
1549 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1552 static void __attribute__((noinline))
1553 uw_init_context_1 (struct _Unwind_Context *context,
1554 void *outer_cfa, void *outer_ra)
1556 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1557 _Unwind_FrameState fs;
1558 _Unwind_SpTmp sp_slot;
1559 _Unwind_Reason_Code code;
1561 memset (context, 0, sizeof (struct _Unwind_Context));
1562 context->ra = ra;
1563 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1564 context->flags = EXTENDED_CONTEXT_BIT;
1566 code = uw_frame_state_for (context, &fs);
1567 gcc_assert (code == _URC_NO_REASON);
1569 #if __GTHREADS
1571 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1572 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1573 && dwarf_reg_size_table[0] == 0)
1574 init_dwarf_reg_size_table ();
1576 #else
1577 if (dwarf_reg_size_table[0] == 0)
1578 init_dwarf_reg_size_table ();
1579 #endif
1581 /* Force the frame state to use the known cfa value. */
1582 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1583 fs.regs.cfa_how = CFA_REG_OFFSET;
1584 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1585 fs.regs.cfa_offset = 0;
1587 uw_update_context_1 (context, &fs);
1589 /* If the return address column was saved in a register in the
1590 initialization context, then we can't see it in the given
1591 call frame data. So have the initialization context tell us. */
1592 context->ra = __builtin_extract_return_addr (outer_ra);
1595 static void _Unwind_DebugHook (void *, void *)
1596 __attribute__ ((__noinline__, __used__, __noclone__));
1598 /* This function is called during unwinding. It is intended as a hook
1599 for a debugger to intercept exceptions. CFA is the CFA of the
1600 target frame. HANDLER is the PC to which control will be
1601 transferred. */
1602 static void
1603 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
1604 void *handler __attribute__ ((__unused__)))
1606 /* We only want to use stap probes starting with v3. Earlier
1607 versions added too much startup cost. */
1608 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1609 STAP_PROBE2 (libgcc, unwind, cfa, handler);
1610 #else
1611 asm ("");
1612 #endif
1615 /* Install TARGET into CURRENT so that we can return to it. This is a
1616 macro because __builtin_eh_return must be invoked in the context of
1617 our caller. */
1619 #define uw_install_context(CURRENT, TARGET) \
1620 do \
1622 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1623 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1624 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1625 __builtin_eh_return (offset, handler); \
1627 while (0)
1629 static long
1630 uw_install_context_1 (struct _Unwind_Context *current,
1631 struct _Unwind_Context *target)
1633 long i;
1634 _Unwind_SpTmp sp_slot;
1636 /* If the target frame does not have a saved stack pointer,
1637 then set up the target's CFA. */
1638 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1639 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1641 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1643 void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
1644 void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
1646 gcc_assert (current->by_value[i] == 0);
1647 if (target->by_value[i] && c)
1649 _Unwind_Word w;
1650 _Unwind_Ptr p;
1651 if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
1653 w = (_Unwind_Internal_Ptr) t;
1654 memcpy (c, &w, sizeof (_Unwind_Word));
1656 else
1658 gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
1659 p = (_Unwind_Internal_Ptr) t;
1660 memcpy (c, &p, sizeof (_Unwind_Ptr));
1663 else if (t && c && t != c)
1664 memcpy (c, t, dwarf_reg_size_table[i]);
1667 /* If the current frame doesn't have a saved stack pointer, then we
1668 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1669 pointer value reloaded. */
1670 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1672 void *target_cfa;
1674 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1676 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1677 if (STACK_GROWS_DOWNWARD)
1678 return target_cfa - current->cfa + target->args_size;
1679 else
1680 return current->cfa - target_cfa - target->args_size;
1682 return 0;
1685 static inline _Unwind_Ptr
1686 uw_identify_context (struct _Unwind_Context *context)
1688 /* The CFA is not sufficient to disambiguate the context of a function
1689 interrupted by a signal before establishing its frame and the context
1690 of the signal itself. */
1691 if (STACK_GROWS_DOWNWARD)
1692 return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
1693 else
1694 return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
1698 #include "unwind.inc"
1700 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1701 alias (_Unwind_Backtrace);
1702 alias (_Unwind_DeleteException);
1703 alias (_Unwind_FindEnclosingFunction);
1704 alias (_Unwind_ForcedUnwind);
1705 alias (_Unwind_GetDataRelBase);
1706 alias (_Unwind_GetTextRelBase);
1707 alias (_Unwind_GetCFA);
1708 alias (_Unwind_GetGR);
1709 alias (_Unwind_GetIP);
1710 alias (_Unwind_GetLanguageSpecificData);
1711 alias (_Unwind_GetRegionStart);
1712 alias (_Unwind_RaiseException);
1713 alias (_Unwind_Resume);
1714 alias (_Unwind_Resume_or_Rethrow);
1715 alias (_Unwind_SetGR);
1716 alias (_Unwind_SetIP);
1717 #endif
1719 #endif /* !USING_SJLJ_EXCEPTIONS */