* config.gcc (cygwin tm_file): Add cygwin-stdint.h.
[official-gcc.git] / gcc / unwind-dw2.c
blob6df1ffa55a644a68552dac757da90951ae175c04
1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 GCC is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 License for more details.
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
31 #include "tconfig.h"
32 #include "tsystem.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "dwarf2.h"
36 #include "unwind.h"
37 #ifdef __USING_SJLJ_EXCEPTIONS__
38 # define NO_SIZE_OF_ENCODED_VALUE
39 #endif
40 #include "unwind-pe.h"
41 #include "unwind-dw2-fde.h"
42 #include "gthr.h"
43 #include "unwind-dw2.h"
45 #ifndef __USING_SJLJ_EXCEPTIONS__
47 #ifndef STACK_GROWS_DOWNWARD
48 #define STACK_GROWS_DOWNWARD 0
49 #else
50 #undef STACK_GROWS_DOWNWARD
51 #define STACK_GROWS_DOWNWARD 1
52 #endif
54 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
55 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
56 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
57 #endif
59 #ifndef DWARF_REG_TO_UNWIND_COLUMN
60 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
61 #endif
63 /* This is the register and unwind state for a particular frame. This
64 provides the information necessary to unwind up past a frame and return
65 to its caller. */
66 struct _Unwind_Context
68 void *reg[DWARF_FRAME_REGISTERS+1];
69 void *cfa;
70 void *ra;
71 void *lsda;
72 struct dwarf_eh_bases bases;
73 /* Signal frame context. */
74 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
75 /* Context which has version/args_size/by_value fields. */
76 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
77 _Unwind_Word flags;
78 /* 0 for now, can be increased when further fields are added to
79 struct _Unwind_Context. */
80 _Unwind_Word version;
81 _Unwind_Word args_size;
82 char by_value[DWARF_FRAME_REGISTERS+1];
85 /* Byte size of every register managed by these routines. */
86 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
89 /* Read unaligned data from the instruction buffer. */
91 union unaligned
93 void *p;
94 unsigned u2 __attribute__ ((mode (HI)));
95 unsigned u4 __attribute__ ((mode (SI)));
96 unsigned u8 __attribute__ ((mode (DI)));
97 signed s2 __attribute__ ((mode (HI)));
98 signed s4 __attribute__ ((mode (SI)));
99 signed s8 __attribute__ ((mode (DI)));
100 } __attribute__ ((packed));
102 static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
103 static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
104 _Unwind_FrameState *);
106 static inline void *
107 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
109 static inline int
110 read_1u (const void *p) { return *(const unsigned char *) p; }
112 static inline int
113 read_1s (const void *p) { return *(const signed char *) p; }
115 static inline int
116 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
118 static inline int
119 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
121 static inline unsigned int
122 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
124 static inline int
125 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
127 static inline unsigned long
128 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
130 static inline unsigned long
131 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
133 static inline _Unwind_Word
134 _Unwind_IsSignalFrame (struct _Unwind_Context *context)
136 return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
139 static inline void
140 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
142 if (val)
143 context->flags |= SIGNAL_FRAME_BIT;
144 else
145 context->flags &= ~SIGNAL_FRAME_BIT;
148 static inline _Unwind_Word
149 _Unwind_IsExtendedContext (struct _Unwind_Context *context)
151 return context->flags & EXTENDED_CONTEXT_BIT;
154 /* Get the value of register INDEX as saved in CONTEXT. */
156 inline _Unwind_Word
157 _Unwind_GetGR (struct _Unwind_Context *context, int index)
159 int size;
160 void *ptr;
162 #ifdef DWARF_ZERO_REG
163 if (index == DWARF_ZERO_REG)
164 return 0;
165 #endif
167 index = DWARF_REG_TO_UNWIND_COLUMN (index);
168 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
169 size = dwarf_reg_size_table[index];
170 ptr = context->reg[index];
172 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
173 return (_Unwind_Word) (_Unwind_Internal_Ptr) ptr;
175 /* This will segfault if the register hasn't been saved. */
176 if (size == sizeof(_Unwind_Ptr))
177 return * (_Unwind_Ptr *) ptr;
178 else
180 gcc_assert (size == sizeof(_Unwind_Word));
181 return * (_Unwind_Word *) ptr;
185 static inline void *
186 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
188 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
191 /* Get the value of the CFA as saved in CONTEXT. */
193 _Unwind_Word
194 _Unwind_GetCFA (struct _Unwind_Context *context)
196 return (_Unwind_Ptr) context->cfa;
199 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
201 inline void
202 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
204 int size;
205 void *ptr;
207 index = DWARF_REG_TO_UNWIND_COLUMN (index);
208 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
209 size = dwarf_reg_size_table[index];
211 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
213 context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
214 return;
217 ptr = context->reg[index];
219 if (size == sizeof(_Unwind_Ptr))
220 * (_Unwind_Ptr *) ptr = val;
221 else
223 gcc_assert (size == sizeof(_Unwind_Word));
224 * (_Unwind_Word *) ptr = val;
228 /* Get the pointer to a register INDEX as saved in CONTEXT. */
230 static inline void *
231 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
233 index = DWARF_REG_TO_UNWIND_COLUMN (index);
234 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
235 return &context->reg[index];
236 return context->reg[index];
239 /* Set the pointer to a register INDEX as saved in CONTEXT. */
241 static inline void
242 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
244 index = DWARF_REG_TO_UNWIND_COLUMN (index);
245 if (_Unwind_IsExtendedContext (context))
246 context->by_value[index] = 0;
247 context->reg[index] = p;
250 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
252 static inline void
253 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
254 _Unwind_Word val)
256 index = DWARF_REG_TO_UNWIND_COLUMN (index);
257 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
258 gcc_assert (dwarf_reg_size_table[index] == sizeof (_Unwind_Ptr));
260 context->by_value[index] = 1;
261 context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
264 /* Return nonzero if register INDEX is stored by value rather than
265 by reference. */
267 static inline int
268 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
270 index = DWARF_REG_TO_UNWIND_COLUMN (index);
271 return context->by_value[index];
274 /* Retrieve the return address for CONTEXT. */
276 inline _Unwind_Ptr
277 _Unwind_GetIP (struct _Unwind_Context *context)
279 return (_Unwind_Ptr) context->ra;
282 /* Retrieve the return address and flag whether that IP is before
283 or after first not yet fully executed instruction. */
285 inline _Unwind_Ptr
286 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
288 *ip_before_insn = _Unwind_IsSignalFrame (context);
289 return (_Unwind_Ptr) context->ra;
292 /* Overwrite the return address for CONTEXT with VAL. */
294 inline void
295 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
297 context->ra = (void *) val;
300 void *
301 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
303 return context->lsda;
306 _Unwind_Ptr
307 _Unwind_GetRegionStart (struct _Unwind_Context *context)
309 return (_Unwind_Ptr) context->bases.func;
312 void *
313 _Unwind_FindEnclosingFunction (void *pc)
315 struct dwarf_eh_bases bases;
316 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
317 if (fde)
318 return bases.func;
319 else
320 return NULL;
323 #ifndef __ia64__
324 _Unwind_Ptr
325 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
327 return (_Unwind_Ptr) context->bases.dbase;
330 _Unwind_Ptr
331 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
333 return (_Unwind_Ptr) context->bases.tbase;
335 #endif
337 #ifdef MD_UNWIND_SUPPORT
338 #include MD_UNWIND_SUPPORT
339 #endif
341 /* Extract any interesting information from the CIE for the translation
342 unit F belongs to. Return a pointer to the byte after the augmentation,
343 or NULL if we encountered an undecipherable augmentation. */
345 static const unsigned char *
346 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
347 _Unwind_FrameState *fs)
349 const unsigned char *aug = cie->augmentation;
350 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
351 const unsigned char *ret = NULL;
352 _uleb128_t utmp;
353 _sleb128_t stmp;
355 /* g++ v2 "eh" has pointer immediately following augmentation string,
356 so it must be handled first. */
357 if (aug[0] == 'e' && aug[1] == 'h')
359 fs->eh_ptr = read_pointer (p);
360 p += sizeof (void *);
361 aug += 2;
364 /* Immediately following the augmentation are the code and
365 data alignment and return address column. */
366 p = read_uleb128 (p, &utmp);
367 fs->code_align = (_Unwind_Word)utmp;
368 p = read_sleb128 (p, &stmp);
369 fs->data_align = (_Unwind_Sword)stmp;
370 if (cie->version == 1)
371 fs->retaddr_column = *p++;
372 else
374 p = read_uleb128 (p, &utmp);
375 fs->retaddr_column = (_Unwind_Word)utmp;
377 fs->lsda_encoding = DW_EH_PE_omit;
379 /* If the augmentation starts with 'z', then a uleb128 immediately
380 follows containing the length of the augmentation field following
381 the size. */
382 if (*aug == 'z')
384 p = read_uleb128 (p, &utmp);
385 ret = p + utmp;
387 fs->saw_z = 1;
388 ++aug;
391 /* Iterate over recognized augmentation subsequences. */
392 while (*aug != '\0')
394 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
395 if (aug[0] == 'L')
397 fs->lsda_encoding = *p++;
398 aug += 1;
401 /* "R" indicates a byte indicating how FDE addresses are encoded. */
402 else if (aug[0] == 'R')
404 fs->fde_encoding = *p++;
405 aug += 1;
408 /* "P" indicates a personality routine in the CIE augmentation. */
409 else if (aug[0] == 'P')
411 _Unwind_Ptr personality;
413 p = read_encoded_value (context, *p, p + 1, &personality);
414 fs->personality = (_Unwind_Personality_Fn) personality;
415 aug += 1;
418 /* "S" indicates a signal frame. */
419 else if (aug[0] == 'S')
421 fs->signal_frame = 1;
422 aug += 1;
425 /* Otherwise we have an unknown augmentation string.
426 Bail unless we saw a 'z' prefix. */
427 else
428 return ret;
431 return ret ? ret : p;
435 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
436 onto the stack to start. */
438 static _Unwind_Word
439 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
440 struct _Unwind_Context *context, _Unwind_Word initial)
442 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
443 int stack_elt;
445 stack[0] = initial;
446 stack_elt = 1;
448 while (op_ptr < op_end)
450 enum dwarf_location_atom op = *op_ptr++;
451 _Unwind_Word result;
452 _uleb128_t reg, utmp;
453 _sleb128_t offset, stmp;
455 switch (op)
457 case DW_OP_lit0:
458 case DW_OP_lit1:
459 case DW_OP_lit2:
460 case DW_OP_lit3:
461 case DW_OP_lit4:
462 case DW_OP_lit5:
463 case DW_OP_lit6:
464 case DW_OP_lit7:
465 case DW_OP_lit8:
466 case DW_OP_lit9:
467 case DW_OP_lit10:
468 case DW_OP_lit11:
469 case DW_OP_lit12:
470 case DW_OP_lit13:
471 case DW_OP_lit14:
472 case DW_OP_lit15:
473 case DW_OP_lit16:
474 case DW_OP_lit17:
475 case DW_OP_lit18:
476 case DW_OP_lit19:
477 case DW_OP_lit20:
478 case DW_OP_lit21:
479 case DW_OP_lit22:
480 case DW_OP_lit23:
481 case DW_OP_lit24:
482 case DW_OP_lit25:
483 case DW_OP_lit26:
484 case DW_OP_lit27:
485 case DW_OP_lit28:
486 case DW_OP_lit29:
487 case DW_OP_lit30:
488 case DW_OP_lit31:
489 result = op - DW_OP_lit0;
490 break;
492 case DW_OP_addr:
493 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
494 op_ptr += sizeof (void *);
495 break;
497 case DW_OP_GNU_encoded_addr:
499 _Unwind_Ptr presult;
500 op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
501 result = presult;
503 break;
505 case DW_OP_const1u:
506 result = read_1u (op_ptr);
507 op_ptr += 1;
508 break;
509 case DW_OP_const1s:
510 result = read_1s (op_ptr);
511 op_ptr += 1;
512 break;
513 case DW_OP_const2u:
514 result = read_2u (op_ptr);
515 op_ptr += 2;
516 break;
517 case DW_OP_const2s:
518 result = read_2s (op_ptr);
519 op_ptr += 2;
520 break;
521 case DW_OP_const4u:
522 result = read_4u (op_ptr);
523 op_ptr += 4;
524 break;
525 case DW_OP_const4s:
526 result = read_4s (op_ptr);
527 op_ptr += 4;
528 break;
529 case DW_OP_const8u:
530 result = read_8u (op_ptr);
531 op_ptr += 8;
532 break;
533 case DW_OP_const8s:
534 result = read_8s (op_ptr);
535 op_ptr += 8;
536 break;
537 case DW_OP_constu:
538 op_ptr = read_uleb128 (op_ptr, &utmp);
539 result = (_Unwind_Word)utmp;
540 break;
541 case DW_OP_consts:
542 op_ptr = read_sleb128 (op_ptr, &stmp);
543 result = (_Unwind_Sword)stmp;
544 break;
546 case DW_OP_reg0:
547 case DW_OP_reg1:
548 case DW_OP_reg2:
549 case DW_OP_reg3:
550 case DW_OP_reg4:
551 case DW_OP_reg5:
552 case DW_OP_reg6:
553 case DW_OP_reg7:
554 case DW_OP_reg8:
555 case DW_OP_reg9:
556 case DW_OP_reg10:
557 case DW_OP_reg11:
558 case DW_OP_reg12:
559 case DW_OP_reg13:
560 case DW_OP_reg14:
561 case DW_OP_reg15:
562 case DW_OP_reg16:
563 case DW_OP_reg17:
564 case DW_OP_reg18:
565 case DW_OP_reg19:
566 case DW_OP_reg20:
567 case DW_OP_reg21:
568 case DW_OP_reg22:
569 case DW_OP_reg23:
570 case DW_OP_reg24:
571 case DW_OP_reg25:
572 case DW_OP_reg26:
573 case DW_OP_reg27:
574 case DW_OP_reg28:
575 case DW_OP_reg29:
576 case DW_OP_reg30:
577 case DW_OP_reg31:
578 result = _Unwind_GetGR (context, op - DW_OP_reg0);
579 break;
580 case DW_OP_regx:
581 op_ptr = read_uleb128 (op_ptr, &reg);
582 result = _Unwind_GetGR (context, reg);
583 break;
585 case DW_OP_breg0:
586 case DW_OP_breg1:
587 case DW_OP_breg2:
588 case DW_OP_breg3:
589 case DW_OP_breg4:
590 case DW_OP_breg5:
591 case DW_OP_breg6:
592 case DW_OP_breg7:
593 case DW_OP_breg8:
594 case DW_OP_breg9:
595 case DW_OP_breg10:
596 case DW_OP_breg11:
597 case DW_OP_breg12:
598 case DW_OP_breg13:
599 case DW_OP_breg14:
600 case DW_OP_breg15:
601 case DW_OP_breg16:
602 case DW_OP_breg17:
603 case DW_OP_breg18:
604 case DW_OP_breg19:
605 case DW_OP_breg20:
606 case DW_OP_breg21:
607 case DW_OP_breg22:
608 case DW_OP_breg23:
609 case DW_OP_breg24:
610 case DW_OP_breg25:
611 case DW_OP_breg26:
612 case DW_OP_breg27:
613 case DW_OP_breg28:
614 case DW_OP_breg29:
615 case DW_OP_breg30:
616 case DW_OP_breg31:
617 op_ptr = read_sleb128 (op_ptr, &offset);
618 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
619 break;
620 case DW_OP_bregx:
621 op_ptr = read_uleb128 (op_ptr, &reg);
622 op_ptr = read_sleb128 (op_ptr, &offset);
623 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
624 break;
626 case DW_OP_dup:
627 gcc_assert (stack_elt);
628 result = stack[stack_elt - 1];
629 break;
631 case DW_OP_drop:
632 gcc_assert (stack_elt);
633 stack_elt -= 1;
634 goto no_push;
636 case DW_OP_pick:
637 offset = *op_ptr++;
638 gcc_assert (offset < stack_elt - 1);
639 result = stack[stack_elt - 1 - offset];
640 break;
642 case DW_OP_over:
643 gcc_assert (stack_elt >= 2);
644 result = stack[stack_elt - 2];
645 break;
647 case DW_OP_swap:
649 _Unwind_Word t;
650 gcc_assert (stack_elt >= 2);
651 t = stack[stack_elt - 1];
652 stack[stack_elt - 1] = stack[stack_elt - 2];
653 stack[stack_elt - 2] = t;
654 goto no_push;
657 case DW_OP_rot:
659 _Unwind_Word t1, t2, t3;
661 gcc_assert (stack_elt >= 3);
662 t1 = stack[stack_elt - 1];
663 t2 = stack[stack_elt - 2];
664 t3 = stack[stack_elt - 3];
665 stack[stack_elt - 1] = t2;
666 stack[stack_elt - 2] = t3;
667 stack[stack_elt - 3] = t1;
668 goto no_push;
671 case DW_OP_deref:
672 case DW_OP_deref_size:
673 case DW_OP_abs:
674 case DW_OP_neg:
675 case DW_OP_not:
676 case DW_OP_plus_uconst:
677 /* Unary operations. */
678 gcc_assert (stack_elt);
679 stack_elt -= 1;
681 result = stack[stack_elt];
683 switch (op)
685 case DW_OP_deref:
687 void *ptr = (void *) (_Unwind_Ptr) result;
688 result = (_Unwind_Ptr) read_pointer (ptr);
690 break;
692 case DW_OP_deref_size:
694 void *ptr = (void *) (_Unwind_Ptr) result;
695 switch (*op_ptr++)
697 case 1:
698 result = read_1u (ptr);
699 break;
700 case 2:
701 result = read_2u (ptr);
702 break;
703 case 4:
704 result = read_4u (ptr);
705 break;
706 case 8:
707 result = read_8u (ptr);
708 break;
709 default:
710 gcc_unreachable ();
713 break;
715 case DW_OP_abs:
716 if ((_Unwind_Sword) result < 0)
717 result = -result;
718 break;
719 case DW_OP_neg:
720 result = -result;
721 break;
722 case DW_OP_not:
723 result = ~result;
724 break;
725 case DW_OP_plus_uconst:
726 op_ptr = read_uleb128 (op_ptr, &utmp);
727 result += (_Unwind_Word)utmp;
728 break;
730 default:
731 gcc_unreachable ();
733 break;
735 case DW_OP_and:
736 case DW_OP_div:
737 case DW_OP_minus:
738 case DW_OP_mod:
739 case DW_OP_mul:
740 case DW_OP_or:
741 case DW_OP_plus:
742 case DW_OP_shl:
743 case DW_OP_shr:
744 case DW_OP_shra:
745 case DW_OP_xor:
746 case DW_OP_le:
747 case DW_OP_ge:
748 case DW_OP_eq:
749 case DW_OP_lt:
750 case DW_OP_gt:
751 case DW_OP_ne:
753 /* Binary operations. */
754 _Unwind_Word first, second;
755 gcc_assert (stack_elt >= 2);
756 stack_elt -= 2;
758 second = stack[stack_elt];
759 first = stack[stack_elt + 1];
761 switch (op)
763 case DW_OP_and:
764 result = second & first;
765 break;
766 case DW_OP_div:
767 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
768 break;
769 case DW_OP_minus:
770 result = second - first;
771 break;
772 case DW_OP_mod:
773 result = (_Unwind_Sword) second % (_Unwind_Sword) first;
774 break;
775 case DW_OP_mul:
776 result = second * first;
777 break;
778 case DW_OP_or:
779 result = second | first;
780 break;
781 case DW_OP_plus:
782 result = second + first;
783 break;
784 case DW_OP_shl:
785 result = second << first;
786 break;
787 case DW_OP_shr:
788 result = second >> first;
789 break;
790 case DW_OP_shra:
791 result = (_Unwind_Sword) second >> first;
792 break;
793 case DW_OP_xor:
794 result = second ^ first;
795 break;
796 case DW_OP_le:
797 result = (_Unwind_Sword) first <= (_Unwind_Sword) second;
798 break;
799 case DW_OP_ge:
800 result = (_Unwind_Sword) first >= (_Unwind_Sword) second;
801 break;
802 case DW_OP_eq:
803 result = (_Unwind_Sword) first == (_Unwind_Sword) second;
804 break;
805 case DW_OP_lt:
806 result = (_Unwind_Sword) first < (_Unwind_Sword) second;
807 break;
808 case DW_OP_gt:
809 result = (_Unwind_Sword) first > (_Unwind_Sword) second;
810 break;
811 case DW_OP_ne:
812 result = (_Unwind_Sword) first != (_Unwind_Sword) second;
813 break;
815 default:
816 gcc_unreachable ();
819 break;
821 case DW_OP_skip:
822 offset = read_2s (op_ptr);
823 op_ptr += 2;
824 op_ptr += offset;
825 goto no_push;
827 case DW_OP_bra:
828 gcc_assert (stack_elt);
829 stack_elt -= 1;
831 offset = read_2s (op_ptr);
832 op_ptr += 2;
833 if (stack[stack_elt] != 0)
834 op_ptr += offset;
835 goto no_push;
837 case DW_OP_nop:
838 goto no_push;
840 default:
841 gcc_unreachable ();
844 /* Most things push a result value. */
845 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
846 stack[stack_elt++] = result;
847 no_push:;
850 /* We were executing this program to get a value. It should be
851 at top of stack. */
852 gcc_assert (stack_elt);
853 stack_elt -= 1;
854 return stack[stack_elt];
858 /* Decode DWARF 2 call frame information. Takes pointers the
859 instruction sequence to decode, current register information and
860 CIE info, and the PC range to evaluate. */
862 static void
863 execute_cfa_program (const unsigned char *insn_ptr,
864 const unsigned char *insn_end,
865 struct _Unwind_Context *context,
866 _Unwind_FrameState *fs)
868 struct frame_state_reg_info *unused_rs = NULL;
870 /* Don't allow remember/restore between CIE and FDE programs. */
871 fs->regs.prev = NULL;
873 /* The comparison with the return address uses < rather than <= because
874 we are only interested in the effects of code before the call; for a
875 noreturn function, the return address may point to unrelated code with
876 a different stack configuration that we are not interested in. We
877 assume that the call itself is unwind info-neutral; if not, or if
878 there are delay instructions that adjust the stack, these must be
879 reflected at the point immediately before the call insn.
880 In signal frames, return address is after last completed instruction,
881 so we add 1 to return address to make the comparison <=. */
882 while (insn_ptr < insn_end
883 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
885 unsigned char insn = *insn_ptr++;
886 _uleb128_t reg, utmp;
887 _sleb128_t offset, stmp;
889 if ((insn & 0xc0) == DW_CFA_advance_loc)
890 fs->pc += (insn & 0x3f) * fs->code_align;
891 else if ((insn & 0xc0) == DW_CFA_offset)
893 reg = insn & 0x3f;
894 insn_ptr = read_uleb128 (insn_ptr, &utmp);
895 offset = (_Unwind_Sword) utmp * fs->data_align;
896 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
897 = REG_SAVED_OFFSET;
898 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
900 else if ((insn & 0xc0) == DW_CFA_restore)
902 reg = insn & 0x3f;
903 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
905 else switch (insn)
907 case DW_CFA_set_loc:
909 _Unwind_Ptr pc;
911 insn_ptr = read_encoded_value (context, fs->fde_encoding,
912 insn_ptr, &pc);
913 fs->pc = (void *) pc;
915 break;
917 case DW_CFA_advance_loc1:
918 fs->pc += read_1u (insn_ptr) * fs->code_align;
919 insn_ptr += 1;
920 break;
921 case DW_CFA_advance_loc2:
922 fs->pc += read_2u (insn_ptr) * fs->code_align;
923 insn_ptr += 2;
924 break;
925 case DW_CFA_advance_loc4:
926 fs->pc += read_4u (insn_ptr) * fs->code_align;
927 insn_ptr += 4;
928 break;
930 case DW_CFA_offset_extended:
931 insn_ptr = read_uleb128 (insn_ptr, &reg);
932 insn_ptr = read_uleb128 (insn_ptr, &utmp);
933 offset = (_Unwind_Sword) utmp * fs->data_align;
934 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
935 = REG_SAVED_OFFSET;
936 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
937 break;
939 case DW_CFA_restore_extended:
940 insn_ptr = read_uleb128 (insn_ptr, &reg);
941 /* FIXME, this is wrong; the CIE might have said that the
942 register was saved somewhere. */
943 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
944 break;
946 case DW_CFA_same_value:
947 insn_ptr = read_uleb128 (insn_ptr, &reg);
948 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
949 break;
951 case DW_CFA_undefined:
952 insn_ptr = read_uleb128 (insn_ptr, &reg);
953 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNDEFINED;
954 break;
956 case DW_CFA_nop:
957 break;
959 case DW_CFA_register:
961 _uleb128_t reg2;
962 insn_ptr = read_uleb128 (insn_ptr, &reg);
963 insn_ptr = read_uleb128 (insn_ptr, &reg2);
964 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
965 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg =
966 (_Unwind_Word)reg2;
968 break;
970 case DW_CFA_remember_state:
972 struct frame_state_reg_info *new_rs;
973 if (unused_rs)
975 new_rs = unused_rs;
976 unused_rs = unused_rs->prev;
978 else
979 new_rs = alloca (sizeof (struct frame_state_reg_info));
981 *new_rs = fs->regs;
982 fs->regs.prev = new_rs;
984 break;
986 case DW_CFA_restore_state:
988 struct frame_state_reg_info *old_rs = fs->regs.prev;
989 fs->regs = *old_rs;
990 old_rs->prev = unused_rs;
991 unused_rs = old_rs;
993 break;
995 case DW_CFA_def_cfa:
996 insn_ptr = read_uleb128 (insn_ptr, &utmp);
997 fs->regs.cfa_reg = (_Unwind_Word)utmp;
998 insn_ptr = read_uleb128 (insn_ptr, &utmp);
999 fs->regs.cfa_offset = (_Unwind_Word)utmp;
1000 fs->regs.cfa_how = CFA_REG_OFFSET;
1001 break;
1003 case DW_CFA_def_cfa_register:
1004 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1005 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1006 fs->regs.cfa_how = CFA_REG_OFFSET;
1007 break;
1009 case DW_CFA_def_cfa_offset:
1010 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1011 fs->regs.cfa_offset = utmp;
1012 /* cfa_how deliberately not set. */
1013 break;
1015 case DW_CFA_def_cfa_expression:
1016 fs->regs.cfa_exp = insn_ptr;
1017 fs->regs.cfa_how = CFA_EXP;
1018 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1019 insn_ptr += utmp;
1020 break;
1022 case DW_CFA_expression:
1023 insn_ptr = read_uleb128 (insn_ptr, &reg);
1024 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
1025 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1026 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1027 insn_ptr += utmp;
1028 break;
1030 /* Dwarf3. */
1031 case DW_CFA_offset_extended_sf:
1032 insn_ptr = read_uleb128 (insn_ptr, &reg);
1033 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1034 offset = stmp * fs->data_align;
1035 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1036 = REG_SAVED_OFFSET;
1037 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1038 break;
1040 case DW_CFA_def_cfa_sf:
1041 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1042 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1043 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1044 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1045 fs->regs.cfa_how = CFA_REG_OFFSET;
1046 fs->regs.cfa_offset *= fs->data_align;
1047 break;
1049 case DW_CFA_def_cfa_offset_sf:
1050 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1051 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1052 fs->regs.cfa_offset *= fs->data_align;
1053 /* cfa_how deliberately not set. */
1054 break;
1056 case DW_CFA_val_offset:
1057 insn_ptr = read_uleb128 (insn_ptr, &reg);
1058 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1059 offset = (_Unwind_Sword) utmp * fs->data_align;
1060 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1061 = REG_SAVED_VAL_OFFSET;
1062 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1063 break;
1065 case DW_CFA_val_offset_sf:
1066 insn_ptr = read_uleb128 (insn_ptr, &reg);
1067 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1068 offset = stmp * fs->data_align;
1069 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1070 = REG_SAVED_VAL_OFFSET;
1071 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1072 break;
1074 case DW_CFA_val_expression:
1075 insn_ptr = read_uleb128 (insn_ptr, &reg);
1076 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1077 = REG_SAVED_VAL_EXP;
1078 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1079 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1080 insn_ptr += utmp;
1081 break;
1083 case DW_CFA_GNU_window_save:
1084 /* ??? Hardcoded for SPARC register window configuration. */
1085 for (reg = 16; reg < 32; ++reg)
1087 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1088 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1090 break;
1092 case DW_CFA_GNU_args_size:
1093 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1094 context->args_size = (_Unwind_Word)utmp;
1095 break;
1097 case DW_CFA_GNU_negative_offset_extended:
1098 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1099 older PowerPC code. */
1100 insn_ptr = read_uleb128 (insn_ptr, &reg);
1101 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1102 offset = (_Unwind_Word) utmp * fs->data_align;
1103 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1104 = REG_SAVED_OFFSET;
1105 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
1106 break;
1108 default:
1109 gcc_unreachable ();
1114 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1115 its caller and decode it into FS. This function also sets the
1116 args_size and lsda members of CONTEXT, as they are really information
1117 about the caller's frame. */
1119 static _Unwind_Reason_Code
1120 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1122 const struct dwarf_fde *fde;
1123 const struct dwarf_cie *cie;
1124 const unsigned char *aug, *insn, *end;
1126 memset (fs, 0, sizeof (*fs));
1127 context->args_size = 0;
1128 context->lsda = 0;
1130 if (context->ra == 0)
1131 return _URC_END_OF_STACK;
1133 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1134 &context->bases);
1135 if (fde == NULL)
1137 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1138 /* Couldn't find frame unwind info for this function. Try a
1139 target-specific fallback mechanism. This will necessarily
1140 not provide a personality routine or LSDA. */
1141 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1142 #else
1143 return _URC_END_OF_STACK;
1144 #endif
1147 fs->pc = context->bases.func;
1149 cie = get_cie (fde);
1150 insn = extract_cie_info (cie, context, fs);
1151 if (insn == NULL)
1152 /* CIE contained unknown augmentation. */
1153 return _URC_FATAL_PHASE1_ERROR;
1155 /* First decode all the insns in the CIE. */
1156 end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
1157 execute_cfa_program (insn, end, context, fs);
1159 /* Locate augmentation for the fde. */
1160 aug = (const unsigned char *) fde + sizeof (*fde);
1161 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1162 insn = NULL;
1163 if (fs->saw_z)
1165 _uleb128_t i;
1166 aug = read_uleb128 (aug, &i);
1167 insn = aug + i;
1169 if (fs->lsda_encoding != DW_EH_PE_omit)
1171 _Unwind_Ptr lsda;
1173 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1174 context->lsda = (void *) lsda;
1177 /* Then the insns in the FDE up to our target PC. */
1178 if (insn == NULL)
1179 insn = aug;
1180 end = (const unsigned char *) next_fde (fde);
1181 execute_cfa_program (insn, end, context, fs);
1183 return _URC_NO_REASON;
1186 typedef struct frame_state
1188 void *cfa;
1189 void *eh_ptr;
1190 long cfa_offset;
1191 long args_size;
1192 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1193 unsigned short cfa_reg;
1194 unsigned short retaddr_column;
1195 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1196 } frame_state;
1198 struct frame_state * __frame_state_for (void *, struct frame_state *);
1200 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1201 a given PC_TARGET. The caller should allocate a local variable of
1202 `struct frame_state' and pass its address to STATE_IN. */
1204 struct frame_state *
1205 __frame_state_for (void *pc_target, struct frame_state *state_in)
1207 struct _Unwind_Context context;
1208 _Unwind_FrameState fs;
1209 int reg;
1211 memset (&context, 0, sizeof (struct _Unwind_Context));
1212 context.flags = EXTENDED_CONTEXT_BIT;
1213 context.ra = pc_target + 1;
1215 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1216 return 0;
1218 /* We have no way to pass a location expression for the CFA to our
1219 caller. It wouldn't understand it anyway. */
1220 if (fs.regs.cfa_how == CFA_EXP)
1221 return 0;
1223 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1225 state_in->saved[reg] = fs.regs.reg[reg].how;
1226 switch (state_in->saved[reg])
1228 case REG_SAVED_REG:
1229 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1230 break;
1231 case REG_SAVED_OFFSET:
1232 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1233 break;
1234 default:
1235 state_in->reg_or_offset[reg] = 0;
1236 break;
1240 state_in->cfa_offset = fs.regs.cfa_offset;
1241 state_in->cfa_reg = fs.regs.cfa_reg;
1242 state_in->retaddr_column = fs.retaddr_column;
1243 state_in->args_size = context.args_size;
1244 state_in->eh_ptr = fs.eh_ptr;
1246 return state_in;
1249 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1251 static inline void
1252 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1253 _Unwind_SpTmp *tmp_sp)
1255 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1257 if (size == sizeof(_Unwind_Ptr))
1258 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1259 else
1261 gcc_assert (size == sizeof(_Unwind_Word));
1262 tmp_sp->word = (_Unwind_Ptr) cfa;
1264 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1267 static void
1268 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1270 struct _Unwind_Context orig_context = *context;
1271 void *cfa;
1272 long i;
1274 #ifdef EH_RETURN_STACKADJ_RTX
1275 /* Special handling here: Many machines do not use a frame pointer,
1276 and track the CFA only through offsets from the stack pointer from
1277 one frame to the next. In this case, the stack pointer is never
1278 stored, so it has no saved address in the context. What we do
1279 have is the CFA from the previous stack frame.
1281 In very special situations (such as unwind info for signal return),
1282 there may be location expressions that use the stack pointer as well.
1284 Do this conditionally for one frame. This allows the unwind info
1285 for one frame to save a copy of the stack pointer from the previous
1286 frame, and be able to use much easier CFA mechanisms to do it.
1287 Always zap the saved stack pointer value for the next frame; carrying
1288 the value over from one frame to another doesn't make sense. */
1290 _Unwind_SpTmp tmp_sp;
1292 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1293 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1294 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1295 #endif
1297 /* Compute this frame's CFA. */
1298 switch (fs->regs.cfa_how)
1300 case CFA_REG_OFFSET:
1301 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1302 cfa += fs->regs.cfa_offset;
1303 break;
1305 case CFA_EXP:
1307 const unsigned char *exp = fs->regs.cfa_exp;
1308 _uleb128_t len;
1310 exp = read_uleb128 (exp, &len);
1311 cfa = (void *) (_Unwind_Ptr)
1312 execute_stack_op (exp, exp + len, &orig_context, 0);
1313 break;
1316 default:
1317 gcc_unreachable ();
1319 context->cfa = cfa;
1321 /* Compute the addresses of all registers saved in this frame. */
1322 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1323 switch (fs->regs.reg[i].how)
1325 case REG_UNSAVED:
1326 case REG_UNDEFINED:
1327 break;
1329 case REG_SAVED_OFFSET:
1330 _Unwind_SetGRPtr (context, i,
1331 (void *) (cfa + fs->regs.reg[i].loc.offset));
1332 break;
1334 case REG_SAVED_REG:
1335 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1336 _Unwind_SetGRValue (context, i,
1337 _Unwind_GetGR (&orig_context,
1338 fs->regs.reg[i].loc.reg));
1339 else
1340 _Unwind_SetGRPtr (context, i,
1341 _Unwind_GetGRPtr (&orig_context,
1342 fs->regs.reg[i].loc.reg));
1343 break;
1345 case REG_SAVED_EXP:
1347 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1348 _uleb128_t len;
1349 _Unwind_Ptr val;
1351 exp = read_uleb128 (exp, &len);
1352 val = execute_stack_op (exp, exp + len, &orig_context,
1353 (_Unwind_Ptr) cfa);
1354 _Unwind_SetGRPtr (context, i, (void *) val);
1356 break;
1358 case REG_SAVED_VAL_OFFSET:
1359 _Unwind_SetGRValue (context, i,
1360 (_Unwind_Internal_Ptr)
1361 (cfa + fs->regs.reg[i].loc.offset));
1362 break;
1364 case REG_SAVED_VAL_EXP:
1366 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1367 _uleb128_t len;
1368 _Unwind_Ptr val;
1370 exp = read_uleb128 (exp, &len);
1371 val = execute_stack_op (exp, exp + len, &orig_context,
1372 (_Unwind_Ptr) cfa);
1373 _Unwind_SetGRValue (context, i, val);
1375 break;
1378 _Unwind_SetSignalFrame (context, fs->signal_frame);
1380 #ifdef MD_FROB_UPDATE_CONTEXT
1381 MD_FROB_UPDATE_CONTEXT (context, fs);
1382 #endif
1385 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1386 of its caller. Update CONTEXT to refer to the caller as well. Note
1387 that the args_size and lsda members are not updated here, but later in
1388 uw_frame_state_for. */
1390 static void
1391 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1393 uw_update_context_1 (context, fs);
1395 /* In general this unwinder doesn't make any distinction between
1396 undefined and same_value rule. Call-saved registers are assumed
1397 to have same_value rule by default and explicit undefined
1398 rule is handled like same_value. The only exception is
1399 DW_CFA_undefined on retaddr_column which is supposed to
1400 mark outermost frame in DWARF 3. */
1401 if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
1402 == REG_UNDEFINED)
1403 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1404 stack frame. */
1405 context->ra = 0;
1406 else
1407 /* Compute the return address now, since the return address column
1408 can change from frame to frame. */
1409 context->ra = __builtin_extract_return_addr
1410 (_Unwind_GetPtr (context, fs->retaddr_column));
1413 static void
1414 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1416 uw_update_context (context, fs);
1419 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1420 level will be the return address and the CFA. */
1422 #define uw_init_context(CONTEXT) \
1423 do \
1425 /* Do any necessary initialization to access arbitrary stack frames. \
1426 On the SPARC, this means flushing the register windows. */ \
1427 __builtin_unwind_init (); \
1428 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1429 __builtin_return_address (0)); \
1431 while (0)
1433 static inline void
1434 init_dwarf_reg_size_table (void)
1436 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1439 static void
1440 uw_init_context_1 (struct _Unwind_Context *context,
1441 void *outer_cfa, void *outer_ra)
1443 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1444 _Unwind_FrameState fs;
1445 _Unwind_SpTmp sp_slot;
1446 _Unwind_Reason_Code code;
1448 memset (context, 0, sizeof (struct _Unwind_Context));
1449 context->ra = ra;
1450 context->flags = EXTENDED_CONTEXT_BIT;
1452 code = uw_frame_state_for (context, &fs);
1453 gcc_assert (code == _URC_NO_REASON);
1455 #if __GTHREADS
1457 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1458 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1459 && dwarf_reg_size_table[0] == 0)
1460 init_dwarf_reg_size_table ();
1462 #else
1463 if (dwarf_reg_size_table[0] == 0)
1464 init_dwarf_reg_size_table ();
1465 #endif
1467 /* Force the frame state to use the known cfa value. */
1468 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1469 fs.regs.cfa_how = CFA_REG_OFFSET;
1470 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1471 fs.regs.cfa_offset = 0;
1473 uw_update_context_1 (context, &fs);
1475 /* If the return address column was saved in a register in the
1476 initialization context, then we can't see it in the given
1477 call frame data. So have the initialization context tell us. */
1478 context->ra = __builtin_extract_return_addr (outer_ra);
1482 /* Install TARGET into CURRENT so that we can return to it. This is a
1483 macro because __builtin_eh_return must be invoked in the context of
1484 our caller. */
1486 #define uw_install_context(CURRENT, TARGET) \
1487 do \
1489 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1490 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1491 __builtin_eh_return (offset, handler); \
1493 while (0)
1495 static long
1496 uw_install_context_1 (struct _Unwind_Context *current,
1497 struct _Unwind_Context *target)
1499 long i;
1500 _Unwind_SpTmp sp_slot;
1502 /* If the target frame does not have a saved stack pointer,
1503 then set up the target's CFA. */
1504 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1505 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1507 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1509 void *c = current->reg[i];
1510 void *t = target->reg[i];
1512 gcc_assert (current->by_value[i] == 0);
1513 if (target->by_value[i] && c)
1515 _Unwind_Word w;
1516 _Unwind_Ptr p;
1517 if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
1519 w = (_Unwind_Internal_Ptr) t;
1520 memcpy (c, &w, sizeof (_Unwind_Word));
1522 else
1524 gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
1525 p = (_Unwind_Internal_Ptr) t;
1526 memcpy (c, &p, sizeof (_Unwind_Ptr));
1529 else if (t && c && t != c)
1530 memcpy (c, t, dwarf_reg_size_table[i]);
1533 /* If the current frame doesn't have a saved stack pointer, then we
1534 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1535 pointer value reloaded. */
1536 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1538 void *target_cfa;
1540 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1542 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1543 if (STACK_GROWS_DOWNWARD)
1544 return target_cfa - current->cfa + target->args_size;
1545 else
1546 return current->cfa - target_cfa - target->args_size;
1548 return 0;
1551 static inline _Unwind_Ptr
1552 uw_identify_context (struct _Unwind_Context *context)
1554 return _Unwind_GetCFA (context);
1558 #include "unwind.inc"
1560 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1561 alias (_Unwind_Backtrace);
1562 alias (_Unwind_DeleteException);
1563 alias (_Unwind_FindEnclosingFunction);
1564 alias (_Unwind_ForcedUnwind);
1565 alias (_Unwind_GetDataRelBase);
1566 alias (_Unwind_GetTextRelBase);
1567 alias (_Unwind_GetCFA);
1568 alias (_Unwind_GetGR);
1569 alias (_Unwind_GetIP);
1570 alias (_Unwind_GetLanguageSpecificData);
1571 alias (_Unwind_GetRegionStart);
1572 alias (_Unwind_RaiseException);
1573 alias (_Unwind_Resume);
1574 alias (_Unwind_Resume_or_Rethrow);
1575 alias (_Unwind_SetGR);
1576 alias (_Unwind_SetIP);
1577 #endif
1579 #endif /* !USING_SJLJ_EXCEPTIONS */