re PR inline-asm/61692 (ICE in extract_insn in recog.c for asm with many parameters)
[official-gcc.git] / libgcc / unwind-dw2.c
blobb262fd9f5b92e2d0ea4f0e65152927de0290fcbd
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 __LIBGCC_STACK_GROWS_DOWNWARD__
47 #define __LIBGCC_STACK_GROWS_DOWNWARD__ 0
48 #else
49 #undef __LIBGCC_STACK_GROWS_DOWNWARD__
50 #define __LIBGCC_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 __LIBGCC_DWARF_FRAME_REGISTERS__
56 #endif
58 /* ??? For the public function interfaces, we tend to gcc_assert that the
59 column numbers are in range. For the dwarf2 unwind info this does happen,
60 although so far in a case that doesn't actually matter.
62 See PR49146, in which a call from x86_64 ms abi to x86_64 unix abi stores
63 the call-saved xmm registers and annotates them. We havn't bothered
64 providing support for the xmm registers for the x86_64 port primarily
65 because the 64-bit windows targets don't use dwarf2 unwind, using sjlj or
66 SEH instead. Adding the support for unix targets would generally be a
67 waste. However, some runtime libraries supplied with ICC do contain such
68 an unorthodox transition, as well as the unwind info to match. This loss
69 of register restoration doesn't matter in practice, because the exception
70 is caught in the native unix abi, where all of the xmm registers are
71 call clobbered.
73 Ideally, we'd record some bit to notice when we're failing to restore some
74 register recorded in the unwind info, but to do that we need annotation on
75 the unix->ms abi edge, so that we know when the register data may be
76 discarded. And since this edge is also within the ICC library, we're
77 unlikely to be able to get the new annotation.
79 Barring a magic solution to restore the ms abi defined 128-bit xmm registers
80 (as distictly opposed to the full runtime width) without causing extra
81 overhead for normal unix abis, the best solution seems to be to simply
82 ignore unwind data for unknown columns. */
84 #define UNWIND_COLUMN_IN_RANGE(x) \
85 __builtin_expect((x) <= __LIBGCC_DWARF_FRAME_REGISTERS__, 1)
87 #ifdef REG_VALUE_IN_UNWIND_CONTEXT
88 typedef _Unwind_Word _Unwind_Context_Reg_Val;
90 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
91 #define ASSUME_EXTENDED_UNWIND_CONTEXT 1
92 #endif
94 static inline _Unwind_Word
95 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
97 return val;
100 static inline _Unwind_Context_Reg_Val
101 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
103 return val;
105 #else
106 typedef void *_Unwind_Context_Reg_Val;
108 static inline _Unwind_Word
109 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
111 return (_Unwind_Word) (_Unwind_Internal_Ptr) val;
114 static inline _Unwind_Context_Reg_Val
115 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
117 return (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) val;
119 #endif
121 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
122 #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
123 #endif
125 /* This is the register and unwind state for a particular frame. This
126 provides the information necessary to unwind up past a frame and return
127 to its caller. */
128 struct _Unwind_Context
130 _Unwind_Context_Reg_Val reg[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
131 void *cfa;
132 void *ra;
133 void *lsda;
134 struct dwarf_eh_bases bases;
135 /* Signal frame context. */
136 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
137 /* Context which has version/args_size/by_value fields. */
138 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
139 _Unwind_Word flags;
140 /* 0 for now, can be increased when further fields are added to
141 struct _Unwind_Context. */
142 _Unwind_Word version;
143 _Unwind_Word args_size;
144 char by_value[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
147 /* Byte size of every register managed by these routines. */
148 static unsigned char dwarf_reg_size_table[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
151 /* Read unaligned data from the instruction buffer. */
153 union unaligned
155 void *p;
156 unsigned u2 __attribute__ ((mode (HI)));
157 unsigned u4 __attribute__ ((mode (SI)));
158 unsigned u8 __attribute__ ((mode (DI)));
159 signed s2 __attribute__ ((mode (HI)));
160 signed s4 __attribute__ ((mode (SI)));
161 signed s8 __attribute__ ((mode (DI)));
162 } __attribute__ ((packed));
164 static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
165 static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
166 _Unwind_FrameState *);
168 static inline void *
169 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
171 static inline int
172 read_1u (const void *p) { return *(const unsigned char *) p; }
174 static inline int
175 read_1s (const void *p) { return *(const signed char *) p; }
177 static inline int
178 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
180 static inline int
181 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
183 static inline unsigned int
184 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
186 static inline int
187 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
189 static inline unsigned long
190 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
192 static inline unsigned long
193 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
195 static inline _Unwind_Word
196 _Unwind_IsSignalFrame (struct _Unwind_Context *context)
198 return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
201 static inline void
202 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
204 if (val)
205 context->flags |= SIGNAL_FRAME_BIT;
206 else
207 context->flags &= ~SIGNAL_FRAME_BIT;
210 static inline _Unwind_Word
211 _Unwind_IsExtendedContext (struct _Unwind_Context *context)
213 return (ASSUME_EXTENDED_UNWIND_CONTEXT
214 || (context->flags & EXTENDED_CONTEXT_BIT));
217 /* Get the value of register INDEX as saved in CONTEXT. */
219 inline _Unwind_Word
220 _Unwind_GetGR (struct _Unwind_Context *context, int index)
222 int size;
223 _Unwind_Context_Reg_Val val;
225 #ifdef DWARF_ZERO_REG
226 if (index == DWARF_ZERO_REG)
227 return 0;
228 #endif
230 index = DWARF_REG_TO_UNWIND_COLUMN (index);
231 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
232 size = dwarf_reg_size_table[index];
233 val = context->reg[index];
235 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
236 return _Unwind_Get_Unwind_Word (val);
238 /* This will segfault if the register hasn't been saved. */
239 if (size == sizeof(_Unwind_Ptr))
240 return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
241 else
243 gcc_assert (size == sizeof(_Unwind_Word));
244 return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
248 static inline void *
249 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
251 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
254 /* Get the value of the CFA as saved in CONTEXT. */
256 _Unwind_Word
257 _Unwind_GetCFA (struct _Unwind_Context *context)
259 return (_Unwind_Ptr) context->cfa;
262 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
264 inline void
265 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
267 int size;
268 void *ptr;
270 index = DWARF_REG_TO_UNWIND_COLUMN (index);
271 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
272 size = dwarf_reg_size_table[index];
274 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
276 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
277 return;
280 ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
282 if (size == sizeof(_Unwind_Ptr))
283 * (_Unwind_Ptr *) ptr = val;
284 else
286 gcc_assert (size == sizeof(_Unwind_Word));
287 * (_Unwind_Word *) ptr = val;
291 /* Get the pointer to a register INDEX as saved in CONTEXT. */
293 static inline void *
294 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
296 index = DWARF_REG_TO_UNWIND_COLUMN (index);
297 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
298 return &context->reg[index];
299 return (void *) (_Unwind_Internal_Ptr) context->reg[index];
302 /* Set the pointer to a register INDEX as saved in CONTEXT. */
304 static inline void
305 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
307 index = DWARF_REG_TO_UNWIND_COLUMN (index);
308 if (_Unwind_IsExtendedContext (context))
309 context->by_value[index] = 0;
310 context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
313 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
315 static inline void
316 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
317 _Unwind_Word val)
319 index = DWARF_REG_TO_UNWIND_COLUMN (index);
320 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
321 /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
322 gcc_assert (dwarf_reg_size_table[index] <= sizeof (_Unwind_Context_Reg_Val));
324 context->by_value[index] = 1;
325 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
328 /* Return nonzero if register INDEX is stored by value rather than
329 by reference. */
331 static inline int
332 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
334 index = DWARF_REG_TO_UNWIND_COLUMN (index);
335 return context->by_value[index];
338 /* Retrieve the return address for CONTEXT. */
340 inline _Unwind_Ptr
341 _Unwind_GetIP (struct _Unwind_Context *context)
343 return (_Unwind_Ptr) context->ra;
346 /* Retrieve the return address and flag whether that IP is before
347 or after first not yet fully executed instruction. */
349 inline _Unwind_Ptr
350 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
352 *ip_before_insn = _Unwind_IsSignalFrame (context);
353 return (_Unwind_Ptr) context->ra;
356 /* Overwrite the return address for CONTEXT with VAL. */
358 inline void
359 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
361 context->ra = (void *) val;
364 void *
365 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
367 return context->lsda;
370 _Unwind_Ptr
371 _Unwind_GetRegionStart (struct _Unwind_Context *context)
373 return (_Unwind_Ptr) context->bases.func;
376 void *
377 _Unwind_FindEnclosingFunction (void *pc)
379 struct dwarf_eh_bases bases;
380 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
381 if (fde)
382 return bases.func;
383 else
384 return NULL;
387 #ifndef __ia64__
388 _Unwind_Ptr
389 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
391 return (_Unwind_Ptr) context->bases.dbase;
394 _Unwind_Ptr
395 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
397 return (_Unwind_Ptr) context->bases.tbase;
399 #endif
401 #include "md-unwind-support.h"
403 /* Extract any interesting information from the CIE for the translation
404 unit F belongs to. Return a pointer to the byte after the augmentation,
405 or NULL if we encountered an undecipherable augmentation. */
407 static const unsigned char *
408 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
409 _Unwind_FrameState *fs)
411 const unsigned char *aug = cie->augmentation;
412 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
413 const unsigned char *ret = NULL;
414 _uleb128_t utmp;
415 _sleb128_t stmp;
417 /* g++ v2 "eh" has pointer immediately following augmentation string,
418 so it must be handled first. */
419 if (aug[0] == 'e' && aug[1] == 'h')
421 fs->eh_ptr = read_pointer (p);
422 p += sizeof (void *);
423 aug += 2;
426 /* After the augmentation resp. pointer for "eh" augmentation
427 follows for CIE version >= 4 address size byte and
428 segment size byte. */
429 if (__builtin_expect (cie->version >= 4, 0))
431 if (p[0] != sizeof (void *) || p[1] != 0)
432 return NULL;
433 p += 2;
435 /* Immediately following this are the code and
436 data alignment and return address column. */
437 p = read_uleb128 (p, &utmp);
438 fs->code_align = (_Unwind_Word)utmp;
439 p = read_sleb128 (p, &stmp);
440 fs->data_align = (_Unwind_Sword)stmp;
441 if (cie->version == 1)
442 fs->retaddr_column = *p++;
443 else
445 p = read_uleb128 (p, &utmp);
446 fs->retaddr_column = (_Unwind_Word)utmp;
448 fs->lsda_encoding = DW_EH_PE_omit;
450 /* If the augmentation starts with 'z', then a uleb128 immediately
451 follows containing the length of the augmentation field following
452 the size. */
453 if (*aug == 'z')
455 p = read_uleb128 (p, &utmp);
456 ret = p + utmp;
458 fs->saw_z = 1;
459 ++aug;
462 /* Iterate over recognized augmentation subsequences. */
463 while (*aug != '\0')
465 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
466 if (aug[0] == 'L')
468 fs->lsda_encoding = *p++;
469 aug += 1;
472 /* "R" indicates a byte indicating how FDE addresses are encoded. */
473 else if (aug[0] == 'R')
475 fs->fde_encoding = *p++;
476 aug += 1;
479 /* "P" indicates a personality routine in the CIE augmentation. */
480 else if (aug[0] == 'P')
482 _Unwind_Ptr personality;
484 p = read_encoded_value (context, *p, p + 1, &personality);
485 fs->personality = (_Unwind_Personality_Fn) personality;
486 aug += 1;
489 /* "S" indicates a signal frame. */
490 else if (aug[0] == 'S')
492 fs->signal_frame = 1;
493 aug += 1;
496 /* Otherwise we have an unknown augmentation string.
497 Bail unless we saw a 'z' prefix. */
498 else
499 return ret;
502 return ret ? ret : p;
506 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
507 onto the stack to start. */
509 static _Unwind_Word
510 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
511 struct _Unwind_Context *context, _Unwind_Word initial)
513 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
514 int stack_elt;
516 stack[0] = initial;
517 stack_elt = 1;
519 while (op_ptr < op_end)
521 enum dwarf_location_atom op = *op_ptr++;
522 _Unwind_Word result;
523 _uleb128_t reg, utmp;
524 _sleb128_t offset, stmp;
526 switch (op)
528 case DW_OP_lit0:
529 case DW_OP_lit1:
530 case DW_OP_lit2:
531 case DW_OP_lit3:
532 case DW_OP_lit4:
533 case DW_OP_lit5:
534 case DW_OP_lit6:
535 case DW_OP_lit7:
536 case DW_OP_lit8:
537 case DW_OP_lit9:
538 case DW_OP_lit10:
539 case DW_OP_lit11:
540 case DW_OP_lit12:
541 case DW_OP_lit13:
542 case DW_OP_lit14:
543 case DW_OP_lit15:
544 case DW_OP_lit16:
545 case DW_OP_lit17:
546 case DW_OP_lit18:
547 case DW_OP_lit19:
548 case DW_OP_lit20:
549 case DW_OP_lit21:
550 case DW_OP_lit22:
551 case DW_OP_lit23:
552 case DW_OP_lit24:
553 case DW_OP_lit25:
554 case DW_OP_lit26:
555 case DW_OP_lit27:
556 case DW_OP_lit28:
557 case DW_OP_lit29:
558 case DW_OP_lit30:
559 case DW_OP_lit31:
560 result = op - DW_OP_lit0;
561 break;
563 case DW_OP_addr:
564 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
565 op_ptr += sizeof (void *);
566 break;
568 case DW_OP_GNU_encoded_addr:
570 _Unwind_Ptr presult;
571 op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
572 result = presult;
574 break;
576 case DW_OP_const1u:
577 result = read_1u (op_ptr);
578 op_ptr += 1;
579 break;
580 case DW_OP_const1s:
581 result = read_1s (op_ptr);
582 op_ptr += 1;
583 break;
584 case DW_OP_const2u:
585 result = read_2u (op_ptr);
586 op_ptr += 2;
587 break;
588 case DW_OP_const2s:
589 result = read_2s (op_ptr);
590 op_ptr += 2;
591 break;
592 case DW_OP_const4u:
593 result = read_4u (op_ptr);
594 op_ptr += 4;
595 break;
596 case DW_OP_const4s:
597 result = read_4s (op_ptr);
598 op_ptr += 4;
599 break;
600 case DW_OP_const8u:
601 result = read_8u (op_ptr);
602 op_ptr += 8;
603 break;
604 case DW_OP_const8s:
605 result = read_8s (op_ptr);
606 op_ptr += 8;
607 break;
608 case DW_OP_constu:
609 op_ptr = read_uleb128 (op_ptr, &utmp);
610 result = (_Unwind_Word)utmp;
611 break;
612 case DW_OP_consts:
613 op_ptr = read_sleb128 (op_ptr, &stmp);
614 result = (_Unwind_Sword)stmp;
615 break;
617 case DW_OP_reg0:
618 case DW_OP_reg1:
619 case DW_OP_reg2:
620 case DW_OP_reg3:
621 case DW_OP_reg4:
622 case DW_OP_reg5:
623 case DW_OP_reg6:
624 case DW_OP_reg7:
625 case DW_OP_reg8:
626 case DW_OP_reg9:
627 case DW_OP_reg10:
628 case DW_OP_reg11:
629 case DW_OP_reg12:
630 case DW_OP_reg13:
631 case DW_OP_reg14:
632 case DW_OP_reg15:
633 case DW_OP_reg16:
634 case DW_OP_reg17:
635 case DW_OP_reg18:
636 case DW_OP_reg19:
637 case DW_OP_reg20:
638 case DW_OP_reg21:
639 case DW_OP_reg22:
640 case DW_OP_reg23:
641 case DW_OP_reg24:
642 case DW_OP_reg25:
643 case DW_OP_reg26:
644 case DW_OP_reg27:
645 case DW_OP_reg28:
646 case DW_OP_reg29:
647 case DW_OP_reg30:
648 case DW_OP_reg31:
649 result = _Unwind_GetGR (context, op - DW_OP_reg0);
650 break;
651 case DW_OP_regx:
652 op_ptr = read_uleb128 (op_ptr, &reg);
653 result = _Unwind_GetGR (context, reg);
654 break;
656 case DW_OP_breg0:
657 case DW_OP_breg1:
658 case DW_OP_breg2:
659 case DW_OP_breg3:
660 case DW_OP_breg4:
661 case DW_OP_breg5:
662 case DW_OP_breg6:
663 case DW_OP_breg7:
664 case DW_OP_breg8:
665 case DW_OP_breg9:
666 case DW_OP_breg10:
667 case DW_OP_breg11:
668 case DW_OP_breg12:
669 case DW_OP_breg13:
670 case DW_OP_breg14:
671 case DW_OP_breg15:
672 case DW_OP_breg16:
673 case DW_OP_breg17:
674 case DW_OP_breg18:
675 case DW_OP_breg19:
676 case DW_OP_breg20:
677 case DW_OP_breg21:
678 case DW_OP_breg22:
679 case DW_OP_breg23:
680 case DW_OP_breg24:
681 case DW_OP_breg25:
682 case DW_OP_breg26:
683 case DW_OP_breg27:
684 case DW_OP_breg28:
685 case DW_OP_breg29:
686 case DW_OP_breg30:
687 case DW_OP_breg31:
688 op_ptr = read_sleb128 (op_ptr, &offset);
689 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
690 break;
691 case DW_OP_bregx:
692 op_ptr = read_uleb128 (op_ptr, &reg);
693 op_ptr = read_sleb128 (op_ptr, &offset);
694 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
695 break;
697 case DW_OP_dup:
698 gcc_assert (stack_elt);
699 result = stack[stack_elt - 1];
700 break;
702 case DW_OP_drop:
703 gcc_assert (stack_elt);
704 stack_elt -= 1;
705 goto no_push;
707 case DW_OP_pick:
708 offset = *op_ptr++;
709 gcc_assert (offset < stack_elt - 1);
710 result = stack[stack_elt - 1 - offset];
711 break;
713 case DW_OP_over:
714 gcc_assert (stack_elt >= 2);
715 result = stack[stack_elt - 2];
716 break;
718 case DW_OP_swap:
720 _Unwind_Word t;
721 gcc_assert (stack_elt >= 2);
722 t = stack[stack_elt - 1];
723 stack[stack_elt - 1] = stack[stack_elt - 2];
724 stack[stack_elt - 2] = t;
725 goto no_push;
728 case DW_OP_rot:
730 _Unwind_Word t1, t2, t3;
732 gcc_assert (stack_elt >= 3);
733 t1 = stack[stack_elt - 1];
734 t2 = stack[stack_elt - 2];
735 t3 = stack[stack_elt - 3];
736 stack[stack_elt - 1] = t2;
737 stack[stack_elt - 2] = t3;
738 stack[stack_elt - 3] = t1;
739 goto no_push;
742 case DW_OP_deref:
743 case DW_OP_deref_size:
744 case DW_OP_abs:
745 case DW_OP_neg:
746 case DW_OP_not:
747 case DW_OP_plus_uconst:
748 /* Unary operations. */
749 gcc_assert (stack_elt);
750 stack_elt -= 1;
752 result = stack[stack_elt];
754 switch (op)
756 case DW_OP_deref:
758 void *ptr = (void *) (_Unwind_Ptr) result;
759 result = (_Unwind_Ptr) read_pointer (ptr);
761 break;
763 case DW_OP_deref_size:
765 void *ptr = (void *) (_Unwind_Ptr) result;
766 switch (*op_ptr++)
768 case 1:
769 result = read_1u (ptr);
770 break;
771 case 2:
772 result = read_2u (ptr);
773 break;
774 case 4:
775 result = read_4u (ptr);
776 break;
777 case 8:
778 result = read_8u (ptr);
779 break;
780 default:
781 gcc_unreachable ();
784 break;
786 case DW_OP_abs:
787 if ((_Unwind_Sword) result < 0)
788 result = -result;
789 break;
790 case DW_OP_neg:
791 result = -result;
792 break;
793 case DW_OP_not:
794 result = ~result;
795 break;
796 case DW_OP_plus_uconst:
797 op_ptr = read_uleb128 (op_ptr, &utmp);
798 result += (_Unwind_Word)utmp;
799 break;
801 default:
802 gcc_unreachable ();
804 break;
806 case DW_OP_and:
807 case DW_OP_div:
808 case DW_OP_minus:
809 case DW_OP_mod:
810 case DW_OP_mul:
811 case DW_OP_or:
812 case DW_OP_plus:
813 case DW_OP_shl:
814 case DW_OP_shr:
815 case DW_OP_shra:
816 case DW_OP_xor:
817 case DW_OP_le:
818 case DW_OP_ge:
819 case DW_OP_eq:
820 case DW_OP_lt:
821 case DW_OP_gt:
822 case DW_OP_ne:
824 /* Binary operations. */
825 _Unwind_Word first, second;
826 gcc_assert (stack_elt >= 2);
827 stack_elt -= 2;
829 second = stack[stack_elt];
830 first = stack[stack_elt + 1];
832 switch (op)
834 case DW_OP_and:
835 result = second & first;
836 break;
837 case DW_OP_div:
838 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
839 break;
840 case DW_OP_minus:
841 result = second - first;
842 break;
843 case DW_OP_mod:
844 result = second % first;
845 break;
846 case DW_OP_mul:
847 result = second * first;
848 break;
849 case DW_OP_or:
850 result = second | first;
851 break;
852 case DW_OP_plus:
853 result = second + first;
854 break;
855 case DW_OP_shl:
856 result = second << first;
857 break;
858 case DW_OP_shr:
859 result = second >> first;
860 break;
861 case DW_OP_shra:
862 result = (_Unwind_Sword) second >> first;
863 break;
864 case DW_OP_xor:
865 result = second ^ first;
866 break;
867 case DW_OP_le:
868 result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
869 break;
870 case DW_OP_ge:
871 result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
872 break;
873 case DW_OP_eq:
874 result = (_Unwind_Sword) second == (_Unwind_Sword) first;
875 break;
876 case DW_OP_lt:
877 result = (_Unwind_Sword) second < (_Unwind_Sword) first;
878 break;
879 case DW_OP_gt:
880 result = (_Unwind_Sword) second > (_Unwind_Sword) first;
881 break;
882 case DW_OP_ne:
883 result = (_Unwind_Sword) second != (_Unwind_Sword) first;
884 break;
886 default:
887 gcc_unreachable ();
890 break;
892 case DW_OP_skip:
893 offset = read_2s (op_ptr);
894 op_ptr += 2;
895 op_ptr += offset;
896 goto no_push;
898 case DW_OP_bra:
899 gcc_assert (stack_elt);
900 stack_elt -= 1;
902 offset = read_2s (op_ptr);
903 op_ptr += 2;
904 if (stack[stack_elt] != 0)
905 op_ptr += offset;
906 goto no_push;
908 case DW_OP_nop:
909 goto no_push;
911 default:
912 gcc_unreachable ();
915 /* Most things push a result value. */
916 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
917 stack[stack_elt++] = result;
918 no_push:;
921 /* We were executing this program to get a value. It should be
922 at top of stack. */
923 gcc_assert (stack_elt);
924 stack_elt -= 1;
925 return stack[stack_elt];
929 /* Decode DWARF 2 call frame information. Takes pointers the
930 instruction sequence to decode, current register information and
931 CIE info, and the PC range to evaluate. */
933 static void
934 execute_cfa_program (const unsigned char *insn_ptr,
935 const unsigned char *insn_end,
936 struct _Unwind_Context *context,
937 _Unwind_FrameState *fs)
939 struct frame_state_reg_info *unused_rs = NULL;
941 /* Don't allow remember/restore between CIE and FDE programs. */
942 fs->regs.prev = NULL;
944 /* The comparison with the return address uses < rather than <= because
945 we are only interested in the effects of code before the call; for a
946 noreturn function, the return address may point to unrelated code with
947 a different stack configuration that we are not interested in. We
948 assume that the call itself is unwind info-neutral; if not, or if
949 there are delay instructions that adjust the stack, these must be
950 reflected at the point immediately before the call insn.
951 In signal frames, return address is after last completed instruction,
952 so we add 1 to return address to make the comparison <=. */
953 while (insn_ptr < insn_end
954 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
956 unsigned char insn = *insn_ptr++;
957 _uleb128_t reg, utmp;
958 _sleb128_t offset, stmp;
960 if ((insn & 0xc0) == DW_CFA_advance_loc)
961 fs->pc += (insn & 0x3f) * fs->code_align;
962 else if ((insn & 0xc0) == DW_CFA_offset)
964 reg = insn & 0x3f;
965 insn_ptr = read_uleb128 (insn_ptr, &utmp);
966 offset = (_Unwind_Sword) utmp * fs->data_align;
967 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
968 if (UNWIND_COLUMN_IN_RANGE (reg))
970 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
971 fs->regs.reg[reg].loc.offset = offset;
974 else if ((insn & 0xc0) == DW_CFA_restore)
976 reg = insn & 0x3f;
977 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
978 if (UNWIND_COLUMN_IN_RANGE (reg))
979 fs->regs.reg[reg].how = REG_UNSAVED;
981 else switch (insn)
983 case DW_CFA_set_loc:
985 _Unwind_Ptr pc;
987 insn_ptr = read_encoded_value (context, fs->fde_encoding,
988 insn_ptr, &pc);
989 fs->pc = (void *) pc;
991 break;
993 case DW_CFA_advance_loc1:
994 fs->pc += read_1u (insn_ptr) * fs->code_align;
995 insn_ptr += 1;
996 break;
997 case DW_CFA_advance_loc2:
998 fs->pc += read_2u (insn_ptr) * fs->code_align;
999 insn_ptr += 2;
1000 break;
1001 case DW_CFA_advance_loc4:
1002 fs->pc += read_4u (insn_ptr) * fs->code_align;
1003 insn_ptr += 4;
1004 break;
1006 case DW_CFA_offset_extended:
1007 insn_ptr = read_uleb128 (insn_ptr, &reg);
1008 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1009 offset = (_Unwind_Sword) utmp * fs->data_align;
1010 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1011 if (UNWIND_COLUMN_IN_RANGE (reg))
1013 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1014 fs->regs.reg[reg].loc.offset = offset;
1016 break;
1018 case DW_CFA_restore_extended:
1019 insn_ptr = read_uleb128 (insn_ptr, &reg);
1020 /* FIXME, this is wrong; the CIE might have said that the
1021 register was saved somewhere. */
1022 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1023 if (UNWIND_COLUMN_IN_RANGE (reg))
1024 fs->regs.reg[reg].how = REG_UNSAVED;
1025 break;
1027 case DW_CFA_same_value:
1028 insn_ptr = read_uleb128 (insn_ptr, &reg);
1029 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1030 if (UNWIND_COLUMN_IN_RANGE (reg))
1031 fs->regs.reg[reg].how = REG_UNSAVED;
1032 break;
1034 case DW_CFA_undefined:
1035 insn_ptr = read_uleb128 (insn_ptr, &reg);
1036 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1037 if (UNWIND_COLUMN_IN_RANGE (reg))
1038 fs->regs.reg[reg].how = REG_UNDEFINED;
1039 break;
1041 case DW_CFA_nop:
1042 break;
1044 case DW_CFA_register:
1046 _uleb128_t reg2;
1047 insn_ptr = read_uleb128 (insn_ptr, &reg);
1048 insn_ptr = read_uleb128 (insn_ptr, &reg2);
1049 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1050 if (UNWIND_COLUMN_IN_RANGE (reg))
1052 fs->regs.reg[reg].how = REG_SAVED_REG;
1053 fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2;
1056 break;
1058 case DW_CFA_remember_state:
1060 struct frame_state_reg_info *new_rs;
1061 if (unused_rs)
1063 new_rs = unused_rs;
1064 unused_rs = unused_rs->prev;
1066 else
1067 new_rs = alloca (sizeof (struct frame_state_reg_info));
1069 *new_rs = fs->regs;
1070 fs->regs.prev = new_rs;
1072 break;
1074 case DW_CFA_restore_state:
1076 struct frame_state_reg_info *old_rs = fs->regs.prev;
1077 fs->regs = *old_rs;
1078 old_rs->prev = unused_rs;
1079 unused_rs = old_rs;
1081 break;
1083 case DW_CFA_def_cfa:
1084 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1085 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1086 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1087 fs->regs.cfa_offset = (_Unwind_Word)utmp;
1088 fs->regs.cfa_how = CFA_REG_OFFSET;
1089 break;
1091 case DW_CFA_def_cfa_register:
1092 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1093 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1094 fs->regs.cfa_how = CFA_REG_OFFSET;
1095 break;
1097 case DW_CFA_def_cfa_offset:
1098 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1099 fs->regs.cfa_offset = utmp;
1100 /* cfa_how deliberately not set. */
1101 break;
1103 case DW_CFA_def_cfa_expression:
1104 fs->regs.cfa_exp = insn_ptr;
1105 fs->regs.cfa_how = CFA_EXP;
1106 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1107 insn_ptr += utmp;
1108 break;
1110 case DW_CFA_expression:
1111 insn_ptr = read_uleb128 (insn_ptr, &reg);
1112 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1113 if (UNWIND_COLUMN_IN_RANGE (reg))
1115 fs->regs.reg[reg].how = REG_SAVED_EXP;
1116 fs->regs.reg[reg].loc.exp = insn_ptr;
1118 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1119 insn_ptr += utmp;
1120 break;
1122 /* Dwarf3. */
1123 case DW_CFA_offset_extended_sf:
1124 insn_ptr = read_uleb128 (insn_ptr, &reg);
1125 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1126 offset = stmp * fs->data_align;
1127 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1128 if (UNWIND_COLUMN_IN_RANGE (reg))
1130 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1131 fs->regs.reg[reg].loc.offset = offset;
1133 break;
1135 case DW_CFA_def_cfa_sf:
1136 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1137 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1138 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1139 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1140 fs->regs.cfa_how = CFA_REG_OFFSET;
1141 fs->regs.cfa_offset *= fs->data_align;
1142 break;
1144 case DW_CFA_def_cfa_offset_sf:
1145 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1146 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1147 fs->regs.cfa_offset *= fs->data_align;
1148 /* cfa_how deliberately not set. */
1149 break;
1151 case DW_CFA_val_offset:
1152 insn_ptr = read_uleb128 (insn_ptr, &reg);
1153 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1154 offset = (_Unwind_Sword) utmp * fs->data_align;
1155 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1156 if (UNWIND_COLUMN_IN_RANGE (reg))
1158 fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
1159 fs->regs.reg[reg].loc.offset = offset;
1161 break;
1163 case DW_CFA_val_offset_sf:
1164 insn_ptr = read_uleb128 (insn_ptr, &reg);
1165 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1166 offset = stmp * fs->data_align;
1167 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1168 if (UNWIND_COLUMN_IN_RANGE (reg))
1170 fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
1171 fs->regs.reg[reg].loc.offset = offset;
1173 break;
1175 case DW_CFA_val_expression:
1176 insn_ptr = read_uleb128 (insn_ptr, &reg);
1177 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1178 if (UNWIND_COLUMN_IN_RANGE (reg))
1180 fs->regs.reg[reg].how = REG_SAVED_VAL_EXP;
1181 fs->regs.reg[reg].loc.exp = insn_ptr;
1183 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1184 insn_ptr += utmp;
1185 break;
1187 case DW_CFA_GNU_window_save:
1188 /* ??? Hardcoded for SPARC register window configuration. */
1189 if (__LIBGCC_DWARF_FRAME_REGISTERS__ >= 32)
1190 for (reg = 16; reg < 32; ++reg)
1192 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1193 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1195 break;
1197 case DW_CFA_GNU_args_size:
1198 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1199 context->args_size = (_Unwind_Word)utmp;
1200 break;
1202 case DW_CFA_GNU_negative_offset_extended:
1203 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1204 older PowerPC code. */
1205 insn_ptr = read_uleb128 (insn_ptr, &reg);
1206 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1207 offset = (_Unwind_Word) utmp * fs->data_align;
1208 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1209 if (UNWIND_COLUMN_IN_RANGE (reg))
1211 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1212 fs->regs.reg[reg].loc.offset = -offset;
1214 break;
1216 default:
1217 gcc_unreachable ();
1222 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1223 its caller and decode it into FS. This function also sets the
1224 args_size and lsda members of CONTEXT, as they are really information
1225 about the caller's frame. */
1227 static _Unwind_Reason_Code
1228 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1230 const struct dwarf_fde *fde;
1231 const struct dwarf_cie *cie;
1232 const unsigned char *aug, *insn, *end;
1234 memset (fs, 0, sizeof (*fs));
1235 context->args_size = 0;
1236 context->lsda = 0;
1238 if (context->ra == 0)
1239 return _URC_END_OF_STACK;
1241 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1242 &context->bases);
1243 if (fde == NULL)
1245 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1246 /* Couldn't find frame unwind info for this function. Try a
1247 target-specific fallback mechanism. This will necessarily
1248 not provide a personality routine or LSDA. */
1249 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1250 #else
1251 return _URC_END_OF_STACK;
1252 #endif
1255 fs->pc = context->bases.func;
1257 cie = get_cie (fde);
1258 insn = extract_cie_info (cie, context, fs);
1259 if (insn == NULL)
1260 /* CIE contained unknown augmentation. */
1261 return _URC_FATAL_PHASE1_ERROR;
1263 /* First decode all the insns in the CIE. */
1264 end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
1265 execute_cfa_program (insn, end, context, fs);
1267 /* Locate augmentation for the fde. */
1268 aug = (const unsigned char *) fde + sizeof (*fde);
1269 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1270 insn = NULL;
1271 if (fs->saw_z)
1273 _uleb128_t i;
1274 aug = read_uleb128 (aug, &i);
1275 insn = aug + i;
1277 if (fs->lsda_encoding != DW_EH_PE_omit)
1279 _Unwind_Ptr lsda;
1281 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1282 context->lsda = (void *) lsda;
1285 /* Then the insns in the FDE up to our target PC. */
1286 if (insn == NULL)
1287 insn = aug;
1288 end = (const unsigned char *) next_fde (fde);
1289 execute_cfa_program (insn, end, context, fs);
1291 return _URC_NO_REASON;
1294 typedef struct frame_state
1296 void *cfa;
1297 void *eh_ptr;
1298 long cfa_offset;
1299 long args_size;
1300 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1301 unsigned short cfa_reg;
1302 unsigned short retaddr_column;
1303 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1304 } frame_state;
1306 struct frame_state * __frame_state_for (void *, struct frame_state *);
1308 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1309 a given PC_TARGET. The caller should allocate a local variable of
1310 `struct frame_state' and pass its address to STATE_IN. */
1312 struct frame_state *
1313 __frame_state_for (void *pc_target, struct frame_state *state_in)
1315 struct _Unwind_Context context;
1316 _Unwind_FrameState fs;
1317 int reg;
1319 memset (&context, 0, sizeof (struct _Unwind_Context));
1320 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1321 context.flags = EXTENDED_CONTEXT_BIT;
1322 context.ra = pc_target + 1;
1324 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1325 return 0;
1327 /* We have no way to pass a location expression for the CFA to our
1328 caller. It wouldn't understand it anyway. */
1329 if (fs.regs.cfa_how == CFA_EXP)
1330 return 0;
1332 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1334 state_in->saved[reg] = fs.regs.reg[reg].how;
1335 switch (state_in->saved[reg])
1337 case REG_SAVED_REG:
1338 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1339 break;
1340 case REG_SAVED_OFFSET:
1341 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1342 break;
1343 default:
1344 state_in->reg_or_offset[reg] = 0;
1345 break;
1349 state_in->cfa_offset = fs.regs.cfa_offset;
1350 state_in->cfa_reg = fs.regs.cfa_reg;
1351 state_in->retaddr_column = fs.retaddr_column;
1352 state_in->args_size = context.args_size;
1353 state_in->eh_ptr = fs.eh_ptr;
1355 return state_in;
1358 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1360 static inline void
1361 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1362 _Unwind_SpTmp *tmp_sp)
1364 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1366 if (size == sizeof(_Unwind_Ptr))
1367 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1368 else
1370 gcc_assert (size == sizeof(_Unwind_Word));
1371 tmp_sp->word = (_Unwind_Ptr) cfa;
1373 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1376 static void
1377 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1379 struct _Unwind_Context orig_context = *context;
1380 void *cfa;
1381 long i;
1383 #ifdef __LIBGCC_EH_RETURN_STACKADJ_RTX__
1384 /* Special handling here: Many machines do not use a frame pointer,
1385 and track the CFA only through offsets from the stack pointer from
1386 one frame to the next. In this case, the stack pointer is never
1387 stored, so it has no saved address in the context. What we do
1388 have is the CFA from the previous stack frame.
1390 In very special situations (such as unwind info for signal return),
1391 there may be location expressions that use the stack pointer as well.
1393 Do this conditionally for one frame. This allows the unwind info
1394 for one frame to save a copy of the stack pointer from the previous
1395 frame, and be able to use much easier CFA mechanisms to do it.
1396 Always zap the saved stack pointer value for the next frame; carrying
1397 the value over from one frame to another doesn't make sense. */
1399 _Unwind_SpTmp tmp_sp;
1401 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1402 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1403 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1404 #endif
1406 /* Compute this frame's CFA. */
1407 switch (fs->regs.cfa_how)
1409 case CFA_REG_OFFSET:
1410 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1411 cfa += fs->regs.cfa_offset;
1412 break;
1414 case CFA_EXP:
1416 const unsigned char *exp = fs->regs.cfa_exp;
1417 _uleb128_t len;
1419 exp = read_uleb128 (exp, &len);
1420 cfa = (void *) (_Unwind_Ptr)
1421 execute_stack_op (exp, exp + len, &orig_context, 0);
1422 break;
1425 default:
1426 gcc_unreachable ();
1428 context->cfa = cfa;
1430 /* Compute the addresses of all registers saved in this frame. */
1431 for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__ + 1; ++i)
1432 switch (fs->regs.reg[i].how)
1434 case REG_UNSAVED:
1435 case REG_UNDEFINED:
1436 break;
1438 case REG_SAVED_OFFSET:
1439 _Unwind_SetGRPtr (context, i,
1440 (void *) (cfa + fs->regs.reg[i].loc.offset));
1441 break;
1443 case REG_SAVED_REG:
1444 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1445 _Unwind_SetGRValue (context, i,
1446 _Unwind_GetGR (&orig_context,
1447 fs->regs.reg[i].loc.reg));
1448 else
1449 _Unwind_SetGRPtr (context, i,
1450 _Unwind_GetGRPtr (&orig_context,
1451 fs->regs.reg[i].loc.reg));
1452 break;
1454 case REG_SAVED_EXP:
1456 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1457 _uleb128_t len;
1458 _Unwind_Ptr val;
1460 exp = read_uleb128 (exp, &len);
1461 val = execute_stack_op (exp, exp + len, &orig_context,
1462 (_Unwind_Ptr) cfa);
1463 _Unwind_SetGRPtr (context, i, (void *) val);
1465 break;
1467 case REG_SAVED_VAL_OFFSET:
1468 _Unwind_SetGRValue (context, i,
1469 (_Unwind_Internal_Ptr)
1470 (cfa + fs->regs.reg[i].loc.offset));
1471 break;
1473 case REG_SAVED_VAL_EXP:
1475 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1476 _uleb128_t len;
1477 _Unwind_Ptr val;
1479 exp = read_uleb128 (exp, &len);
1480 val = execute_stack_op (exp, exp + len, &orig_context,
1481 (_Unwind_Ptr) cfa);
1482 _Unwind_SetGRValue (context, i, val);
1484 break;
1487 _Unwind_SetSignalFrame (context, fs->signal_frame);
1489 #ifdef MD_FROB_UPDATE_CONTEXT
1490 MD_FROB_UPDATE_CONTEXT (context, fs);
1491 #endif
1494 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1495 of its caller. Update CONTEXT to refer to the caller as well. Note
1496 that the args_size and lsda members are not updated here, but later in
1497 uw_frame_state_for. */
1499 static void
1500 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1502 uw_update_context_1 (context, fs);
1504 /* In general this unwinder doesn't make any distinction between
1505 undefined and same_value rule. Call-saved registers are assumed
1506 to have same_value rule by default and explicit undefined
1507 rule is handled like same_value. The only exception is
1508 DW_CFA_undefined on retaddr_column which is supposed to
1509 mark outermost frame in DWARF 3. */
1510 if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
1511 == REG_UNDEFINED)
1512 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1513 stack frame. */
1514 context->ra = 0;
1515 else
1516 /* Compute the return address now, since the return address column
1517 can change from frame to frame. */
1518 context->ra = __builtin_extract_return_addr
1519 (_Unwind_GetPtr (context, fs->retaddr_column));
1522 static void
1523 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1525 uw_update_context (context, fs);
1528 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1529 level will be the return address and the CFA. */
1531 #define uw_init_context(CONTEXT) \
1532 do \
1534 /* Do any necessary initialization to access arbitrary stack frames. \
1535 On the SPARC, this means flushing the register windows. */ \
1536 __builtin_unwind_init (); \
1537 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1538 __builtin_return_address (0)); \
1540 while (0)
1542 static inline void
1543 init_dwarf_reg_size_table (void)
1545 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1548 static void __attribute__((noinline))
1549 uw_init_context_1 (struct _Unwind_Context *context,
1550 void *outer_cfa, void *outer_ra)
1552 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1553 _Unwind_FrameState fs;
1554 _Unwind_SpTmp sp_slot;
1555 _Unwind_Reason_Code code;
1557 memset (context, 0, sizeof (struct _Unwind_Context));
1558 context->ra = ra;
1559 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1560 context->flags = EXTENDED_CONTEXT_BIT;
1562 code = uw_frame_state_for (context, &fs);
1563 gcc_assert (code == _URC_NO_REASON);
1565 #if __GTHREADS
1567 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1568 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1569 && dwarf_reg_size_table[0] == 0)
1570 init_dwarf_reg_size_table ();
1572 #else
1573 if (dwarf_reg_size_table[0] == 0)
1574 init_dwarf_reg_size_table ();
1575 #endif
1577 /* Force the frame state to use the known cfa value. */
1578 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1579 fs.regs.cfa_how = CFA_REG_OFFSET;
1580 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1581 fs.regs.cfa_offset = 0;
1583 uw_update_context_1 (context, &fs);
1585 /* If the return address column was saved in a register in the
1586 initialization context, then we can't see it in the given
1587 call frame data. So have the initialization context tell us. */
1588 context->ra = __builtin_extract_return_addr (outer_ra);
1591 static void _Unwind_DebugHook (void *, void *)
1592 __attribute__ ((__noinline__, __used__, __noclone__));
1594 /* This function is called during unwinding. It is intended as a hook
1595 for a debugger to intercept exceptions. CFA is the CFA of the
1596 target frame. HANDLER is the PC to which control will be
1597 transferred. */
1598 static void
1599 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
1600 void *handler __attribute__ ((__unused__)))
1602 /* We only want to use stap probes starting with v3. Earlier
1603 versions added too much startup cost. */
1604 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1605 STAP_PROBE2 (libgcc, unwind, cfa, handler);
1606 #else
1607 asm ("");
1608 #endif
1611 /* Install TARGET into CURRENT so that we can return to it. This is a
1612 macro because __builtin_eh_return must be invoked in the context of
1613 our caller. */
1615 #define uw_install_context(CURRENT, TARGET) \
1616 do \
1618 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1619 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1620 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1621 __builtin_eh_return (offset, handler); \
1623 while (0)
1625 static long
1626 uw_install_context_1 (struct _Unwind_Context *current,
1627 struct _Unwind_Context *target)
1629 long i;
1630 _Unwind_SpTmp sp_slot;
1632 /* If the target frame does not have a saved stack pointer,
1633 then set up the target's CFA. */
1634 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1635 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1637 for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__; ++i)
1639 void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
1640 void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
1642 gcc_assert (current->by_value[i] == 0);
1643 if (target->by_value[i] && c)
1645 _Unwind_Word w;
1646 _Unwind_Ptr p;
1647 if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
1649 w = (_Unwind_Internal_Ptr) t;
1650 memcpy (c, &w, sizeof (_Unwind_Word));
1652 else
1654 gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
1655 p = (_Unwind_Internal_Ptr) t;
1656 memcpy (c, &p, sizeof (_Unwind_Ptr));
1659 else if (t && c && t != c)
1660 memcpy (c, t, dwarf_reg_size_table[i]);
1663 /* If the current frame doesn't have a saved stack pointer, then we
1664 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1665 pointer value reloaded. */
1666 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1668 void *target_cfa;
1670 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1672 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1673 if (__LIBGCC_STACK_GROWS_DOWNWARD__)
1674 return target_cfa - current->cfa + target->args_size;
1675 else
1676 return current->cfa - target_cfa - target->args_size;
1678 return 0;
1681 static inline _Unwind_Ptr
1682 uw_identify_context (struct _Unwind_Context *context)
1684 /* The CFA is not sufficient to disambiguate the context of a function
1685 interrupted by a signal before establishing its frame and the context
1686 of the signal itself. */
1687 if (__LIBGCC_STACK_GROWS_DOWNWARD__)
1688 return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
1689 else
1690 return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
1694 #include "unwind.inc"
1696 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1697 alias (_Unwind_Backtrace);
1698 alias (_Unwind_DeleteException);
1699 alias (_Unwind_FindEnclosingFunction);
1700 alias (_Unwind_ForcedUnwind);
1701 alias (_Unwind_GetDataRelBase);
1702 alias (_Unwind_GetTextRelBase);
1703 alias (_Unwind_GetCFA);
1704 alias (_Unwind_GetGR);
1705 alias (_Unwind_GetIP);
1706 alias (_Unwind_GetLanguageSpecificData);
1707 alias (_Unwind_GetRegionStart);
1708 alias (_Unwind_RaiseException);
1709 alias (_Unwind_Resume);
1710 alias (_Unwind_Resume_or_Rethrow);
1711 alias (_Unwind_SetGR);
1712 alias (_Unwind_SetIP);
1713 #endif
1715 #endif /* !USING_SJLJ_EXCEPTIONS */