* config/sh/sh.c (expand_cbranchdi4): Use a scratch register if
[official-gcc.git] / gcc / unwind-dw2.c
blob25990b4e73ba84df528930d61c9fea7f64c4e652
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, 2010, 2011 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 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "tconfig.h"
27 #include "tsystem.h"
28 #include "coretypes.h"
29 #include "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 /* This is the register and unwind state for a particular frame. This
63 provides the information necessary to unwind up past a frame and return
64 to its caller. */
65 struct _Unwind_Context
67 void *reg[DWARF_FRAME_REGISTERS+1];
68 void *cfa;
69 void *ra;
70 void *lsda;
71 struct dwarf_eh_bases bases;
72 /* Signal frame context. */
73 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
74 /* Context which has version/args_size/by_value fields. */
75 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
76 _Unwind_Word flags;
77 /* 0 for now, can be increased when further fields are added to
78 struct _Unwind_Context. */
79 _Unwind_Word version;
80 _Unwind_Word args_size;
81 char by_value[DWARF_FRAME_REGISTERS+1];
84 /* Byte size of every register managed by these routines. */
85 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
88 /* Read unaligned data from the instruction buffer. */
90 union unaligned
92 void *p;
93 unsigned u2 __attribute__ ((mode (HI)));
94 unsigned u4 __attribute__ ((mode (SI)));
95 unsigned u8 __attribute__ ((mode (DI)));
96 signed s2 __attribute__ ((mode (HI)));
97 signed s4 __attribute__ ((mode (SI)));
98 signed s8 __attribute__ ((mode (DI)));
99 } __attribute__ ((packed));
101 static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
102 static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
103 _Unwind_FrameState *);
105 static inline void *
106 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
108 static inline int
109 read_1u (const void *p) { return *(const unsigned char *) p; }
111 static inline int
112 read_1s (const void *p) { return *(const signed char *) p; }
114 static inline int
115 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
117 static inline int
118 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
120 static inline unsigned int
121 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
123 static inline int
124 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
126 static inline unsigned long
127 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
129 static inline unsigned long
130 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
132 static inline _Unwind_Word
133 _Unwind_IsSignalFrame (struct _Unwind_Context *context)
135 return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
138 static inline void
139 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
141 if (val)
142 context->flags |= SIGNAL_FRAME_BIT;
143 else
144 context->flags &= ~SIGNAL_FRAME_BIT;
147 static inline _Unwind_Word
148 _Unwind_IsExtendedContext (struct _Unwind_Context *context)
150 return context->flags & EXTENDED_CONTEXT_BIT;
153 /* Get the value of register INDEX as saved in CONTEXT. */
155 inline _Unwind_Word
156 _Unwind_GetGR (struct _Unwind_Context *context, int index)
158 int size;
159 void *ptr;
161 #ifdef DWARF_ZERO_REG
162 if (index == DWARF_ZERO_REG)
163 return 0;
164 #endif
166 index = DWARF_REG_TO_UNWIND_COLUMN (index);
167 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
168 size = dwarf_reg_size_table[index];
169 ptr = context->reg[index];
171 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
172 return (_Unwind_Word) (_Unwind_Internal_Ptr) ptr;
174 /* This will segfault if the register hasn't been saved. */
175 if (size == sizeof(_Unwind_Ptr))
176 return * (_Unwind_Ptr *) ptr;
177 else
179 gcc_assert (size == sizeof(_Unwind_Word));
180 return * (_Unwind_Word *) ptr;
184 static inline void *
185 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
187 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
190 /* Get the value of the CFA as saved in CONTEXT. */
192 _Unwind_Word
193 _Unwind_GetCFA (struct _Unwind_Context *context)
195 return (_Unwind_Ptr) context->cfa;
198 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
200 inline void
201 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
203 int size;
204 void *ptr;
206 index = DWARF_REG_TO_UNWIND_COLUMN (index);
207 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
208 size = dwarf_reg_size_table[index];
210 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
212 context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
213 return;
216 ptr = context->reg[index];
218 if (size == sizeof(_Unwind_Ptr))
219 * (_Unwind_Ptr *) ptr = val;
220 else
222 gcc_assert (size == sizeof(_Unwind_Word));
223 * (_Unwind_Word *) ptr = val;
227 /* Get the pointer to a register INDEX as saved in CONTEXT. */
229 static inline void *
230 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
232 index = DWARF_REG_TO_UNWIND_COLUMN (index);
233 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
234 return &context->reg[index];
235 return context->reg[index];
238 /* Set the pointer to a register INDEX as saved in CONTEXT. */
240 static inline void
241 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
243 index = DWARF_REG_TO_UNWIND_COLUMN (index);
244 if (_Unwind_IsExtendedContext (context))
245 context->by_value[index] = 0;
246 context->reg[index] = p;
249 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
251 static inline void
252 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
253 _Unwind_Word val)
255 index = DWARF_REG_TO_UNWIND_COLUMN (index);
256 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
257 gcc_assert (dwarf_reg_size_table[index] == sizeof (_Unwind_Ptr));
259 context->by_value[index] = 1;
260 context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
263 /* Return nonzero if register INDEX is stored by value rather than
264 by reference. */
266 static inline int
267 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
269 index = DWARF_REG_TO_UNWIND_COLUMN (index);
270 return context->by_value[index];
273 /* Retrieve the return address for CONTEXT. */
275 inline _Unwind_Ptr
276 _Unwind_GetIP (struct _Unwind_Context *context)
278 return (_Unwind_Ptr) context->ra;
281 /* Retrieve the return address and flag whether that IP is before
282 or after first not yet fully executed instruction. */
284 inline _Unwind_Ptr
285 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
287 *ip_before_insn = _Unwind_IsSignalFrame (context);
288 return (_Unwind_Ptr) context->ra;
291 /* Overwrite the return address for CONTEXT with VAL. */
293 inline void
294 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
296 context->ra = (void *) val;
299 void *
300 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
302 return context->lsda;
305 _Unwind_Ptr
306 _Unwind_GetRegionStart (struct _Unwind_Context *context)
308 return (_Unwind_Ptr) context->bases.func;
311 void *
312 _Unwind_FindEnclosingFunction (void *pc)
314 struct dwarf_eh_bases bases;
315 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
316 if (fde)
317 return bases.func;
318 else
319 return NULL;
322 #ifndef __ia64__
323 _Unwind_Ptr
324 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
326 return (_Unwind_Ptr) context->bases.dbase;
329 _Unwind_Ptr
330 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
332 return (_Unwind_Ptr) context->bases.tbase;
334 #endif
336 #ifdef MD_UNWIND_SUPPORT
337 #include MD_UNWIND_SUPPORT
338 #endif
340 /* Extract any interesting information from the CIE for the translation
341 unit F belongs to. Return a pointer to the byte after the augmentation,
342 or NULL if we encountered an undecipherable augmentation. */
344 static const unsigned char *
345 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
346 _Unwind_FrameState *fs)
348 const unsigned char *aug = cie->augmentation;
349 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
350 const unsigned char *ret = NULL;
351 _uleb128_t utmp;
352 _sleb128_t stmp;
354 /* g++ v2 "eh" has pointer immediately following augmentation string,
355 so it must be handled first. */
356 if (aug[0] == 'e' && aug[1] == 'h')
358 fs->eh_ptr = read_pointer (p);
359 p += sizeof (void *);
360 aug += 2;
363 /* After the augmentation resp. pointer for "eh" augmentation
364 follows for CIE version >= 4 address size byte and
365 segment size byte. */
366 if (__builtin_expect (cie->version >= 4, 0))
368 if (p[0] != sizeof (void *) || p[1] != 0)
369 return NULL;
370 p += 2;
372 /* Immediately following this are the code and
373 data alignment and return address column. */
374 p = read_uleb128 (p, &utmp);
375 fs->code_align = (_Unwind_Word)utmp;
376 p = read_sleb128 (p, &stmp);
377 fs->data_align = (_Unwind_Sword)stmp;
378 if (cie->version == 1)
379 fs->retaddr_column = *p++;
380 else
382 p = read_uleb128 (p, &utmp);
383 fs->retaddr_column = (_Unwind_Word)utmp;
385 fs->lsda_encoding = DW_EH_PE_omit;
387 /* If the augmentation starts with 'z', then a uleb128 immediately
388 follows containing the length of the augmentation field following
389 the size. */
390 if (*aug == 'z')
392 p = read_uleb128 (p, &utmp);
393 ret = p + utmp;
395 fs->saw_z = 1;
396 ++aug;
399 /* Iterate over recognized augmentation subsequences. */
400 while (*aug != '\0')
402 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
403 if (aug[0] == 'L')
405 fs->lsda_encoding = *p++;
406 aug += 1;
409 /* "R" indicates a byte indicating how FDE addresses are encoded. */
410 else if (aug[0] == 'R')
412 fs->fde_encoding = *p++;
413 aug += 1;
416 /* "P" indicates a personality routine in the CIE augmentation. */
417 else if (aug[0] == 'P')
419 _Unwind_Ptr personality;
421 p = read_encoded_value (context, *p, p + 1, &personality);
422 fs->personality = (_Unwind_Personality_Fn) personality;
423 aug += 1;
426 /* "S" indicates a signal frame. */
427 else if (aug[0] == 'S')
429 fs->signal_frame = 1;
430 aug += 1;
433 /* Otherwise we have an unknown augmentation string.
434 Bail unless we saw a 'z' prefix. */
435 else
436 return ret;
439 return ret ? ret : p;
443 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
444 onto the stack to start. */
446 static _Unwind_Word
447 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
448 struct _Unwind_Context *context, _Unwind_Word initial)
450 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
451 int stack_elt;
453 stack[0] = initial;
454 stack_elt = 1;
456 while (op_ptr < op_end)
458 enum dwarf_location_atom op = *op_ptr++;
459 _Unwind_Word result;
460 _uleb128_t reg, utmp;
461 _sleb128_t offset, stmp;
463 switch (op)
465 case DW_OP_lit0:
466 case DW_OP_lit1:
467 case DW_OP_lit2:
468 case DW_OP_lit3:
469 case DW_OP_lit4:
470 case DW_OP_lit5:
471 case DW_OP_lit6:
472 case DW_OP_lit7:
473 case DW_OP_lit8:
474 case DW_OP_lit9:
475 case DW_OP_lit10:
476 case DW_OP_lit11:
477 case DW_OP_lit12:
478 case DW_OP_lit13:
479 case DW_OP_lit14:
480 case DW_OP_lit15:
481 case DW_OP_lit16:
482 case DW_OP_lit17:
483 case DW_OP_lit18:
484 case DW_OP_lit19:
485 case DW_OP_lit20:
486 case DW_OP_lit21:
487 case DW_OP_lit22:
488 case DW_OP_lit23:
489 case DW_OP_lit24:
490 case DW_OP_lit25:
491 case DW_OP_lit26:
492 case DW_OP_lit27:
493 case DW_OP_lit28:
494 case DW_OP_lit29:
495 case DW_OP_lit30:
496 case DW_OP_lit31:
497 result = op - DW_OP_lit0;
498 break;
500 case DW_OP_addr:
501 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
502 op_ptr += sizeof (void *);
503 break;
505 case DW_OP_GNU_encoded_addr:
507 _Unwind_Ptr presult;
508 op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
509 result = presult;
511 break;
513 case DW_OP_const1u:
514 result = read_1u (op_ptr);
515 op_ptr += 1;
516 break;
517 case DW_OP_const1s:
518 result = read_1s (op_ptr);
519 op_ptr += 1;
520 break;
521 case DW_OP_const2u:
522 result = read_2u (op_ptr);
523 op_ptr += 2;
524 break;
525 case DW_OP_const2s:
526 result = read_2s (op_ptr);
527 op_ptr += 2;
528 break;
529 case DW_OP_const4u:
530 result = read_4u (op_ptr);
531 op_ptr += 4;
532 break;
533 case DW_OP_const4s:
534 result = read_4s (op_ptr);
535 op_ptr += 4;
536 break;
537 case DW_OP_const8u:
538 result = read_8u (op_ptr);
539 op_ptr += 8;
540 break;
541 case DW_OP_const8s:
542 result = read_8s (op_ptr);
543 op_ptr += 8;
544 break;
545 case DW_OP_constu:
546 op_ptr = read_uleb128 (op_ptr, &utmp);
547 result = (_Unwind_Word)utmp;
548 break;
549 case DW_OP_consts:
550 op_ptr = read_sleb128 (op_ptr, &stmp);
551 result = (_Unwind_Sword)stmp;
552 break;
554 case DW_OP_reg0:
555 case DW_OP_reg1:
556 case DW_OP_reg2:
557 case DW_OP_reg3:
558 case DW_OP_reg4:
559 case DW_OP_reg5:
560 case DW_OP_reg6:
561 case DW_OP_reg7:
562 case DW_OP_reg8:
563 case DW_OP_reg9:
564 case DW_OP_reg10:
565 case DW_OP_reg11:
566 case DW_OP_reg12:
567 case DW_OP_reg13:
568 case DW_OP_reg14:
569 case DW_OP_reg15:
570 case DW_OP_reg16:
571 case DW_OP_reg17:
572 case DW_OP_reg18:
573 case DW_OP_reg19:
574 case DW_OP_reg20:
575 case DW_OP_reg21:
576 case DW_OP_reg22:
577 case DW_OP_reg23:
578 case DW_OP_reg24:
579 case DW_OP_reg25:
580 case DW_OP_reg26:
581 case DW_OP_reg27:
582 case DW_OP_reg28:
583 case DW_OP_reg29:
584 case DW_OP_reg30:
585 case DW_OP_reg31:
586 result = _Unwind_GetGR (context, op - DW_OP_reg0);
587 break;
588 case DW_OP_regx:
589 op_ptr = read_uleb128 (op_ptr, &reg);
590 result = _Unwind_GetGR (context, reg);
591 break;
593 case DW_OP_breg0:
594 case DW_OP_breg1:
595 case DW_OP_breg2:
596 case DW_OP_breg3:
597 case DW_OP_breg4:
598 case DW_OP_breg5:
599 case DW_OP_breg6:
600 case DW_OP_breg7:
601 case DW_OP_breg8:
602 case DW_OP_breg9:
603 case DW_OP_breg10:
604 case DW_OP_breg11:
605 case DW_OP_breg12:
606 case DW_OP_breg13:
607 case DW_OP_breg14:
608 case DW_OP_breg15:
609 case DW_OP_breg16:
610 case DW_OP_breg17:
611 case DW_OP_breg18:
612 case DW_OP_breg19:
613 case DW_OP_breg20:
614 case DW_OP_breg21:
615 case DW_OP_breg22:
616 case DW_OP_breg23:
617 case DW_OP_breg24:
618 case DW_OP_breg25:
619 case DW_OP_breg26:
620 case DW_OP_breg27:
621 case DW_OP_breg28:
622 case DW_OP_breg29:
623 case DW_OP_breg30:
624 case DW_OP_breg31:
625 op_ptr = read_sleb128 (op_ptr, &offset);
626 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
627 break;
628 case DW_OP_bregx:
629 op_ptr = read_uleb128 (op_ptr, &reg);
630 op_ptr = read_sleb128 (op_ptr, &offset);
631 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
632 break;
634 case DW_OP_dup:
635 gcc_assert (stack_elt);
636 result = stack[stack_elt - 1];
637 break;
639 case DW_OP_drop:
640 gcc_assert (stack_elt);
641 stack_elt -= 1;
642 goto no_push;
644 case DW_OP_pick:
645 offset = *op_ptr++;
646 gcc_assert (offset < stack_elt - 1);
647 result = stack[stack_elt - 1 - offset];
648 break;
650 case DW_OP_over:
651 gcc_assert (stack_elt >= 2);
652 result = stack[stack_elt - 2];
653 break;
655 case DW_OP_swap:
657 _Unwind_Word t;
658 gcc_assert (stack_elt >= 2);
659 t = stack[stack_elt - 1];
660 stack[stack_elt - 1] = stack[stack_elt - 2];
661 stack[stack_elt - 2] = t;
662 goto no_push;
665 case DW_OP_rot:
667 _Unwind_Word t1, t2, t3;
669 gcc_assert (stack_elt >= 3);
670 t1 = stack[stack_elt - 1];
671 t2 = stack[stack_elt - 2];
672 t3 = stack[stack_elt - 3];
673 stack[stack_elt - 1] = t2;
674 stack[stack_elt - 2] = t3;
675 stack[stack_elt - 3] = t1;
676 goto no_push;
679 case DW_OP_deref:
680 case DW_OP_deref_size:
681 case DW_OP_abs:
682 case DW_OP_neg:
683 case DW_OP_not:
684 case DW_OP_plus_uconst:
685 /* Unary operations. */
686 gcc_assert (stack_elt);
687 stack_elt -= 1;
689 result = stack[stack_elt];
691 switch (op)
693 case DW_OP_deref:
695 void *ptr = (void *) (_Unwind_Ptr) result;
696 result = (_Unwind_Ptr) read_pointer (ptr);
698 break;
700 case DW_OP_deref_size:
702 void *ptr = (void *) (_Unwind_Ptr) result;
703 switch (*op_ptr++)
705 case 1:
706 result = read_1u (ptr);
707 break;
708 case 2:
709 result = read_2u (ptr);
710 break;
711 case 4:
712 result = read_4u (ptr);
713 break;
714 case 8:
715 result = read_8u (ptr);
716 break;
717 default:
718 gcc_unreachable ();
721 break;
723 case DW_OP_abs:
724 if ((_Unwind_Sword) result < 0)
725 result = -result;
726 break;
727 case DW_OP_neg:
728 result = -result;
729 break;
730 case DW_OP_not:
731 result = ~result;
732 break;
733 case DW_OP_plus_uconst:
734 op_ptr = read_uleb128 (op_ptr, &utmp);
735 result += (_Unwind_Word)utmp;
736 break;
738 default:
739 gcc_unreachable ();
741 break;
743 case DW_OP_and:
744 case DW_OP_div:
745 case DW_OP_minus:
746 case DW_OP_mod:
747 case DW_OP_mul:
748 case DW_OP_or:
749 case DW_OP_plus:
750 case DW_OP_shl:
751 case DW_OP_shr:
752 case DW_OP_shra:
753 case DW_OP_xor:
754 case DW_OP_le:
755 case DW_OP_ge:
756 case DW_OP_eq:
757 case DW_OP_lt:
758 case DW_OP_gt:
759 case DW_OP_ne:
761 /* Binary operations. */
762 _Unwind_Word first, second;
763 gcc_assert (stack_elt >= 2);
764 stack_elt -= 2;
766 second = stack[stack_elt];
767 first = stack[stack_elt + 1];
769 switch (op)
771 case DW_OP_and:
772 result = second & first;
773 break;
774 case DW_OP_div:
775 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
776 break;
777 case DW_OP_minus:
778 result = second - first;
779 break;
780 case DW_OP_mod:
781 result = second % first;
782 break;
783 case DW_OP_mul:
784 result = second * first;
785 break;
786 case DW_OP_or:
787 result = second | first;
788 break;
789 case DW_OP_plus:
790 result = second + first;
791 break;
792 case DW_OP_shl:
793 result = second << first;
794 break;
795 case DW_OP_shr:
796 result = second >> first;
797 break;
798 case DW_OP_shra:
799 result = (_Unwind_Sword) second >> first;
800 break;
801 case DW_OP_xor:
802 result = second ^ first;
803 break;
804 case DW_OP_le:
805 result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
806 break;
807 case DW_OP_ge:
808 result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
809 break;
810 case DW_OP_eq:
811 result = (_Unwind_Sword) second == (_Unwind_Sword) first;
812 break;
813 case DW_OP_lt:
814 result = (_Unwind_Sword) second < (_Unwind_Sword) first;
815 break;
816 case DW_OP_gt:
817 result = (_Unwind_Sword) second > (_Unwind_Sword) first;
818 break;
819 case DW_OP_ne:
820 result = (_Unwind_Sword) second != (_Unwind_Sword) first;
821 break;
823 default:
824 gcc_unreachable ();
827 break;
829 case DW_OP_skip:
830 offset = read_2s (op_ptr);
831 op_ptr += 2;
832 op_ptr += offset;
833 goto no_push;
835 case DW_OP_bra:
836 gcc_assert (stack_elt);
837 stack_elt -= 1;
839 offset = read_2s (op_ptr);
840 op_ptr += 2;
841 if (stack[stack_elt] != 0)
842 op_ptr += offset;
843 goto no_push;
845 case DW_OP_nop:
846 goto no_push;
848 default:
849 gcc_unreachable ();
852 /* Most things push a result value. */
853 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
854 stack[stack_elt++] = result;
855 no_push:;
858 /* We were executing this program to get a value. It should be
859 at top of stack. */
860 gcc_assert (stack_elt);
861 stack_elt -= 1;
862 return stack[stack_elt];
866 /* Decode DWARF 2 call frame information. Takes pointers the
867 instruction sequence to decode, current register information and
868 CIE info, and the PC range to evaluate. */
870 static void
871 execute_cfa_program (const unsigned char *insn_ptr,
872 const unsigned char *insn_end,
873 struct _Unwind_Context *context,
874 _Unwind_FrameState *fs)
876 struct frame_state_reg_info *unused_rs = NULL;
878 /* Don't allow remember/restore between CIE and FDE programs. */
879 fs->regs.prev = NULL;
881 /* The comparison with the return address uses < rather than <= because
882 we are only interested in the effects of code before the call; for a
883 noreturn function, the return address may point to unrelated code with
884 a different stack configuration that we are not interested in. We
885 assume that the call itself is unwind info-neutral; if not, or if
886 there are delay instructions that adjust the stack, these must be
887 reflected at the point immediately before the call insn.
888 In signal frames, return address is after last completed instruction,
889 so we add 1 to return address to make the comparison <=. */
890 while (insn_ptr < insn_end
891 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
893 unsigned char insn = *insn_ptr++;
894 _uleb128_t reg, utmp;
895 _sleb128_t offset, stmp;
897 if ((insn & 0xc0) == DW_CFA_advance_loc)
898 fs->pc += (insn & 0x3f) * fs->code_align;
899 else if ((insn & 0xc0) == DW_CFA_offset)
901 reg = insn & 0x3f;
902 insn_ptr = read_uleb128 (insn_ptr, &utmp);
903 offset = (_Unwind_Sword) utmp * fs->data_align;
904 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
905 = REG_SAVED_OFFSET;
906 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
908 else if ((insn & 0xc0) == DW_CFA_restore)
910 reg = insn & 0x3f;
911 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
913 else switch (insn)
915 case DW_CFA_set_loc:
917 _Unwind_Ptr pc;
919 insn_ptr = read_encoded_value (context, fs->fde_encoding,
920 insn_ptr, &pc);
921 fs->pc = (void *) pc;
923 break;
925 case DW_CFA_advance_loc1:
926 fs->pc += read_1u (insn_ptr) * fs->code_align;
927 insn_ptr += 1;
928 break;
929 case DW_CFA_advance_loc2:
930 fs->pc += read_2u (insn_ptr) * fs->code_align;
931 insn_ptr += 2;
932 break;
933 case DW_CFA_advance_loc4:
934 fs->pc += read_4u (insn_ptr) * fs->code_align;
935 insn_ptr += 4;
936 break;
938 case DW_CFA_offset_extended:
939 insn_ptr = read_uleb128 (insn_ptr, &reg);
940 insn_ptr = read_uleb128 (insn_ptr, &utmp);
941 offset = (_Unwind_Sword) utmp * fs->data_align;
942 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
943 = REG_SAVED_OFFSET;
944 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
945 break;
947 case DW_CFA_restore_extended:
948 insn_ptr = read_uleb128 (insn_ptr, &reg);
949 /* FIXME, this is wrong; the CIE might have said that the
950 register was saved somewhere. */
951 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
952 break;
954 case DW_CFA_same_value:
955 insn_ptr = read_uleb128 (insn_ptr, &reg);
956 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
957 break;
959 case DW_CFA_undefined:
960 insn_ptr = read_uleb128 (insn_ptr, &reg);
961 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNDEFINED;
962 break;
964 case DW_CFA_nop:
965 break;
967 case DW_CFA_register:
969 _uleb128_t reg2;
970 insn_ptr = read_uleb128 (insn_ptr, &reg);
971 insn_ptr = read_uleb128 (insn_ptr, &reg2);
972 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
973 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg =
974 (_Unwind_Word)reg2;
976 break;
978 case DW_CFA_remember_state:
980 struct frame_state_reg_info *new_rs;
981 if (unused_rs)
983 new_rs = unused_rs;
984 unused_rs = unused_rs->prev;
986 else
987 new_rs = alloca (sizeof (struct frame_state_reg_info));
989 *new_rs = fs->regs;
990 fs->regs.prev = new_rs;
992 break;
994 case DW_CFA_restore_state:
996 struct frame_state_reg_info *old_rs = fs->regs.prev;
997 fs->regs = *old_rs;
998 old_rs->prev = unused_rs;
999 unused_rs = old_rs;
1001 break;
1003 case DW_CFA_def_cfa:
1004 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1005 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1006 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1007 fs->regs.cfa_offset = (_Unwind_Word)utmp;
1008 fs->regs.cfa_how = CFA_REG_OFFSET;
1009 break;
1011 case DW_CFA_def_cfa_register:
1012 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1013 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1014 fs->regs.cfa_how = CFA_REG_OFFSET;
1015 break;
1017 case DW_CFA_def_cfa_offset:
1018 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1019 fs->regs.cfa_offset = utmp;
1020 /* cfa_how deliberately not set. */
1021 break;
1023 case DW_CFA_def_cfa_expression:
1024 fs->regs.cfa_exp = insn_ptr;
1025 fs->regs.cfa_how = CFA_EXP;
1026 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1027 insn_ptr += utmp;
1028 break;
1030 case DW_CFA_expression:
1031 insn_ptr = read_uleb128 (insn_ptr, &reg);
1032 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
1033 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1034 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1035 insn_ptr += utmp;
1036 break;
1038 /* Dwarf3. */
1039 case DW_CFA_offset_extended_sf:
1040 insn_ptr = read_uleb128 (insn_ptr, &reg);
1041 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1042 offset = stmp * fs->data_align;
1043 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1044 = REG_SAVED_OFFSET;
1045 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1046 break;
1048 case DW_CFA_def_cfa_sf:
1049 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1050 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1051 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1052 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1053 fs->regs.cfa_how = CFA_REG_OFFSET;
1054 fs->regs.cfa_offset *= fs->data_align;
1055 break;
1057 case DW_CFA_def_cfa_offset_sf:
1058 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1059 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1060 fs->regs.cfa_offset *= fs->data_align;
1061 /* cfa_how deliberately not set. */
1062 break;
1064 case DW_CFA_val_offset:
1065 insn_ptr = read_uleb128 (insn_ptr, &reg);
1066 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1067 offset = (_Unwind_Sword) utmp * fs->data_align;
1068 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1069 = REG_SAVED_VAL_OFFSET;
1070 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1071 break;
1073 case DW_CFA_val_offset_sf:
1074 insn_ptr = read_uleb128 (insn_ptr, &reg);
1075 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1076 offset = stmp * fs->data_align;
1077 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1078 = REG_SAVED_VAL_OFFSET;
1079 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1080 break;
1082 case DW_CFA_val_expression:
1083 insn_ptr = read_uleb128 (insn_ptr, &reg);
1084 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1085 = REG_SAVED_VAL_EXP;
1086 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1087 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1088 insn_ptr += utmp;
1089 break;
1091 case DW_CFA_GNU_window_save:
1092 /* ??? Hardcoded for SPARC register window configuration. */
1093 for (reg = 16; reg < 32; ++reg)
1095 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1096 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1098 break;
1100 case DW_CFA_GNU_args_size:
1101 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1102 context->args_size = (_Unwind_Word)utmp;
1103 break;
1105 case DW_CFA_GNU_negative_offset_extended:
1106 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1107 older PowerPC code. */
1108 insn_ptr = read_uleb128 (insn_ptr, &reg);
1109 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1110 offset = (_Unwind_Word) utmp * fs->data_align;
1111 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1112 = REG_SAVED_OFFSET;
1113 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
1114 break;
1116 default:
1117 gcc_unreachable ();
1122 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1123 its caller and decode it into FS. This function also sets the
1124 args_size and lsda members of CONTEXT, as they are really information
1125 about the caller's frame. */
1127 static _Unwind_Reason_Code
1128 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1130 const struct dwarf_fde *fde;
1131 const struct dwarf_cie *cie;
1132 const unsigned char *aug, *insn, *end;
1134 memset (fs, 0, sizeof (*fs));
1135 context->args_size = 0;
1136 context->lsda = 0;
1138 if (context->ra == 0)
1139 return _URC_END_OF_STACK;
1141 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1142 &context->bases);
1143 if (fde == NULL)
1145 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1146 /* Couldn't find frame unwind info for this function. Try a
1147 target-specific fallback mechanism. This will necessarily
1148 not provide a personality routine or LSDA. */
1149 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1150 #else
1151 return _URC_END_OF_STACK;
1152 #endif
1155 fs->pc = context->bases.func;
1157 cie = get_cie (fde);
1158 insn = extract_cie_info (cie, context, fs);
1159 if (insn == NULL)
1160 /* CIE contained unknown augmentation. */
1161 return _URC_FATAL_PHASE1_ERROR;
1163 /* First decode all the insns in the CIE. */
1164 end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
1165 execute_cfa_program (insn, end, context, fs);
1167 /* Locate augmentation for the fde. */
1168 aug = (const unsigned char *) fde + sizeof (*fde);
1169 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1170 insn = NULL;
1171 if (fs->saw_z)
1173 _uleb128_t i;
1174 aug = read_uleb128 (aug, &i);
1175 insn = aug + i;
1177 if (fs->lsda_encoding != DW_EH_PE_omit)
1179 _Unwind_Ptr lsda;
1181 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1182 context->lsda = (void *) lsda;
1185 /* Then the insns in the FDE up to our target PC. */
1186 if (insn == NULL)
1187 insn = aug;
1188 end = (const unsigned char *) next_fde (fde);
1189 execute_cfa_program (insn, end, context, fs);
1191 return _URC_NO_REASON;
1194 typedef struct frame_state
1196 void *cfa;
1197 void *eh_ptr;
1198 long cfa_offset;
1199 long args_size;
1200 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1201 unsigned short cfa_reg;
1202 unsigned short retaddr_column;
1203 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1204 } frame_state;
1206 struct frame_state * __frame_state_for (void *, struct frame_state *);
1208 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1209 a given PC_TARGET. The caller should allocate a local variable of
1210 `struct frame_state' and pass its address to STATE_IN. */
1212 struct frame_state *
1213 __frame_state_for (void *pc_target, struct frame_state *state_in)
1215 struct _Unwind_Context context;
1216 _Unwind_FrameState fs;
1217 int reg;
1219 memset (&context, 0, sizeof (struct _Unwind_Context));
1220 context.flags = EXTENDED_CONTEXT_BIT;
1221 context.ra = pc_target + 1;
1223 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1224 return 0;
1226 /* We have no way to pass a location expression for the CFA to our
1227 caller. It wouldn't understand it anyway. */
1228 if (fs.regs.cfa_how == CFA_EXP)
1229 return 0;
1231 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1233 state_in->saved[reg] = fs.regs.reg[reg].how;
1234 switch (state_in->saved[reg])
1236 case REG_SAVED_REG:
1237 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1238 break;
1239 case REG_SAVED_OFFSET:
1240 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1241 break;
1242 default:
1243 state_in->reg_or_offset[reg] = 0;
1244 break;
1248 state_in->cfa_offset = fs.regs.cfa_offset;
1249 state_in->cfa_reg = fs.regs.cfa_reg;
1250 state_in->retaddr_column = fs.retaddr_column;
1251 state_in->args_size = context.args_size;
1252 state_in->eh_ptr = fs.eh_ptr;
1254 return state_in;
1257 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1259 static inline void
1260 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1261 _Unwind_SpTmp *tmp_sp)
1263 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1265 if (size == sizeof(_Unwind_Ptr))
1266 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1267 else
1269 gcc_assert (size == sizeof(_Unwind_Word));
1270 tmp_sp->word = (_Unwind_Ptr) cfa;
1272 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1275 static void
1276 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1278 struct _Unwind_Context orig_context = *context;
1279 void *cfa;
1280 long i;
1282 #ifdef EH_RETURN_STACKADJ_RTX
1283 /* Special handling here: Many machines do not use a frame pointer,
1284 and track the CFA only through offsets from the stack pointer from
1285 one frame to the next. In this case, the stack pointer is never
1286 stored, so it has no saved address in the context. What we do
1287 have is the CFA from the previous stack frame.
1289 In very special situations (such as unwind info for signal return),
1290 there may be location expressions that use the stack pointer as well.
1292 Do this conditionally for one frame. This allows the unwind info
1293 for one frame to save a copy of the stack pointer from the previous
1294 frame, and be able to use much easier CFA mechanisms to do it.
1295 Always zap the saved stack pointer value for the next frame; carrying
1296 the value over from one frame to another doesn't make sense. */
1298 _Unwind_SpTmp tmp_sp;
1300 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1301 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1302 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1303 #endif
1305 /* Compute this frame's CFA. */
1306 switch (fs->regs.cfa_how)
1308 case CFA_REG_OFFSET:
1309 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1310 cfa += fs->regs.cfa_offset;
1311 break;
1313 case CFA_EXP:
1315 const unsigned char *exp = fs->regs.cfa_exp;
1316 _uleb128_t len;
1318 exp = read_uleb128 (exp, &len);
1319 cfa = (void *) (_Unwind_Ptr)
1320 execute_stack_op (exp, exp + len, &orig_context, 0);
1321 break;
1324 default:
1325 gcc_unreachable ();
1327 context->cfa = cfa;
1329 /* Compute the addresses of all registers saved in this frame. */
1330 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1331 switch (fs->regs.reg[i].how)
1333 case REG_UNSAVED:
1334 case REG_UNDEFINED:
1335 break;
1337 case REG_SAVED_OFFSET:
1338 _Unwind_SetGRPtr (context, i,
1339 (void *) (cfa + fs->regs.reg[i].loc.offset));
1340 break;
1342 case REG_SAVED_REG:
1343 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1344 _Unwind_SetGRValue (context, i,
1345 _Unwind_GetGR (&orig_context,
1346 fs->regs.reg[i].loc.reg));
1347 else
1348 _Unwind_SetGRPtr (context, i,
1349 _Unwind_GetGRPtr (&orig_context,
1350 fs->regs.reg[i].loc.reg));
1351 break;
1353 case REG_SAVED_EXP:
1355 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1356 _uleb128_t len;
1357 _Unwind_Ptr val;
1359 exp = read_uleb128 (exp, &len);
1360 val = execute_stack_op (exp, exp + len, &orig_context,
1361 (_Unwind_Ptr) cfa);
1362 _Unwind_SetGRPtr (context, i, (void *) val);
1364 break;
1366 case REG_SAVED_VAL_OFFSET:
1367 _Unwind_SetGRValue (context, i,
1368 (_Unwind_Internal_Ptr)
1369 (cfa + fs->regs.reg[i].loc.offset));
1370 break;
1372 case REG_SAVED_VAL_EXP:
1374 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1375 _uleb128_t len;
1376 _Unwind_Ptr val;
1378 exp = read_uleb128 (exp, &len);
1379 val = execute_stack_op (exp, exp + len, &orig_context,
1380 (_Unwind_Ptr) cfa);
1381 _Unwind_SetGRValue (context, i, val);
1383 break;
1386 _Unwind_SetSignalFrame (context, fs->signal_frame);
1388 #ifdef MD_FROB_UPDATE_CONTEXT
1389 MD_FROB_UPDATE_CONTEXT (context, fs);
1390 #endif
1393 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1394 of its caller. Update CONTEXT to refer to the caller as well. Note
1395 that the args_size and lsda members are not updated here, but later in
1396 uw_frame_state_for. */
1398 static void
1399 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1401 uw_update_context_1 (context, fs);
1403 /* In general this unwinder doesn't make any distinction between
1404 undefined and same_value rule. Call-saved registers are assumed
1405 to have same_value rule by default and explicit undefined
1406 rule is handled like same_value. The only exception is
1407 DW_CFA_undefined on retaddr_column which is supposed to
1408 mark outermost frame in DWARF 3. */
1409 if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
1410 == REG_UNDEFINED)
1411 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1412 stack frame. */
1413 context->ra = 0;
1414 else
1415 /* Compute the return address now, since the return address column
1416 can change from frame to frame. */
1417 context->ra = __builtin_extract_return_addr
1418 (_Unwind_GetPtr (context, fs->retaddr_column));
1421 static void
1422 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1424 uw_update_context (context, fs);
1427 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1428 level will be the return address and the CFA. */
1430 #define uw_init_context(CONTEXT) \
1431 do \
1433 /* Do any necessary initialization to access arbitrary stack frames. \
1434 On the SPARC, this means flushing the register windows. */ \
1435 __builtin_unwind_init (); \
1436 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1437 __builtin_return_address (0)); \
1439 while (0)
1441 static inline void
1442 init_dwarf_reg_size_table (void)
1444 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1447 static void __attribute__((noinline))
1448 uw_init_context_1 (struct _Unwind_Context *context,
1449 void *outer_cfa, void *outer_ra)
1451 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1452 _Unwind_FrameState fs;
1453 _Unwind_SpTmp sp_slot;
1454 _Unwind_Reason_Code code;
1456 memset (context, 0, sizeof (struct _Unwind_Context));
1457 context->ra = ra;
1458 context->flags = EXTENDED_CONTEXT_BIT;
1460 code = uw_frame_state_for (context, &fs);
1461 gcc_assert (code == _URC_NO_REASON);
1463 #if __GTHREADS
1465 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1466 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1467 && dwarf_reg_size_table[0] == 0)
1468 init_dwarf_reg_size_table ();
1470 #else
1471 if (dwarf_reg_size_table[0] == 0)
1472 init_dwarf_reg_size_table ();
1473 #endif
1475 /* Force the frame state to use the known cfa value. */
1476 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1477 fs.regs.cfa_how = CFA_REG_OFFSET;
1478 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1479 fs.regs.cfa_offset = 0;
1481 uw_update_context_1 (context, &fs);
1483 /* If the return address column was saved in a register in the
1484 initialization context, then we can't see it in the given
1485 call frame data. So have the initialization context tell us. */
1486 context->ra = __builtin_extract_return_addr (outer_ra);
1489 static void _Unwind_DebugHook (void *, void *)
1490 __attribute__ ((__noinline__, __used__, __noclone__));
1492 /* This function is called during unwinding. It is intended as a hook
1493 for a debugger to intercept exceptions. CFA is the CFA of the
1494 target frame. HANDLER is the PC to which control will be
1495 transferred. */
1496 static void
1497 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
1498 void *handler __attribute__ ((__unused__)))
1500 /* We only want to use stap probes starting with v3. Earlier
1501 versions added too much startup cost. */
1502 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1503 STAP_PROBE2 (libgcc, unwind, cfa, handler);
1504 #else
1505 asm ("");
1506 #endif
1509 /* Install TARGET into CURRENT so that we can return to it. This is a
1510 macro because __builtin_eh_return must be invoked in the context of
1511 our caller. */
1513 #define uw_install_context(CURRENT, TARGET) \
1514 do \
1516 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1517 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1518 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1519 __builtin_eh_return (offset, handler); \
1521 while (0)
1523 static long
1524 uw_install_context_1 (struct _Unwind_Context *current,
1525 struct _Unwind_Context *target)
1527 long i;
1528 _Unwind_SpTmp sp_slot;
1530 /* If the target frame does not have a saved stack pointer,
1531 then set up the target's CFA. */
1532 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1533 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1535 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1537 void *c = current->reg[i];
1538 void *t = target->reg[i];
1540 gcc_assert (current->by_value[i] == 0);
1541 if (target->by_value[i] && c)
1543 _Unwind_Word w;
1544 _Unwind_Ptr p;
1545 if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
1547 w = (_Unwind_Internal_Ptr) t;
1548 memcpy (c, &w, sizeof (_Unwind_Word));
1550 else
1552 gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
1553 p = (_Unwind_Internal_Ptr) t;
1554 memcpy (c, &p, sizeof (_Unwind_Ptr));
1557 else if (t && c && t != c)
1558 memcpy (c, t, dwarf_reg_size_table[i]);
1561 /* If the current frame doesn't have a saved stack pointer, then we
1562 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1563 pointer value reloaded. */
1564 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1566 void *target_cfa;
1568 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1570 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1571 if (STACK_GROWS_DOWNWARD)
1572 return target_cfa - current->cfa + target->args_size;
1573 else
1574 return current->cfa - target_cfa - target->args_size;
1576 return 0;
1579 static inline _Unwind_Ptr
1580 uw_identify_context (struct _Unwind_Context *context)
1582 /* The CFA is not sufficient to disambiguate the context of a function
1583 interrupted by a signal before establishing its frame and the context
1584 of the signal itself. */
1585 if (STACK_GROWS_DOWNWARD)
1586 return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
1587 else
1588 return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
1592 #include "unwind.inc"
1594 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1595 alias (_Unwind_Backtrace);
1596 alias (_Unwind_DeleteException);
1597 alias (_Unwind_FindEnclosingFunction);
1598 alias (_Unwind_ForcedUnwind);
1599 alias (_Unwind_GetDataRelBase);
1600 alias (_Unwind_GetTextRelBase);
1601 alias (_Unwind_GetCFA);
1602 alias (_Unwind_GetGR);
1603 alias (_Unwind_GetIP);
1604 alias (_Unwind_GetLanguageSpecificData);
1605 alias (_Unwind_GetRegionStart);
1606 alias (_Unwind_RaiseException);
1607 alias (_Unwind_Resume);
1608 alias (_Unwind_Resume_or_Rethrow);
1609 alias (_Unwind_SetGR);
1610 alias (_Unwind_SetIP);
1611 #endif
1613 #endif /* !USING_SJLJ_EXCEPTIONS */