PR other/54324
[official-gcc.git] / libgcc / unwind-dw2.c
blobd1c62eef153a6e117cc74a025b3ec487b79494ef
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 "libgcc_tm.h"
31 #include "dwarf2.h"
32 #include "unwind.h"
33 #ifdef __USING_SJLJ_EXCEPTIONS__
34 # define NO_SIZE_OF_ENCODED_VALUE
35 #endif
36 #include "unwind-pe.h"
37 #include "unwind-dw2-fde.h"
38 #include "gthr.h"
39 #include "unwind-dw2.h"
41 #ifdef HAVE_SYS_SDT_H
42 #include <sys/sdt.h>
43 #endif
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 #ifdef REG_VALUE_IN_UNWIND_CONTEXT
64 typedef _Unwind_Word _Unwind_Context_Reg_Val;
66 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
67 #define ASSUME_EXTENDED_UNWIND_CONTEXT 1
68 #endif
70 static inline _Unwind_Word
71 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
73 return val;
76 static inline _Unwind_Context_Reg_Val
77 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
79 return val;
81 #else
82 typedef void *_Unwind_Context_Reg_Val;
84 static inline _Unwind_Word
85 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
87 return (_Unwind_Word) (_Unwind_Internal_Ptr) val;
90 static inline _Unwind_Context_Reg_Val
91 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
93 return (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) val;
95 #endif
97 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
98 #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
99 #endif
101 /* This is the register and unwind state for a particular frame. This
102 provides the information necessary to unwind up past a frame and return
103 to its caller. */
104 struct _Unwind_Context
106 _Unwind_Context_Reg_Val reg[DWARF_FRAME_REGISTERS+1];
107 void *cfa;
108 void *ra;
109 void *lsda;
110 struct dwarf_eh_bases bases;
111 /* Signal frame context. */
112 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
113 /* Context which has version/args_size/by_value fields. */
114 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
115 _Unwind_Word flags;
116 /* 0 for now, can be increased when further fields are added to
117 struct _Unwind_Context. */
118 _Unwind_Word version;
119 _Unwind_Word args_size;
120 char by_value[DWARF_FRAME_REGISTERS+1];
123 /* Byte size of every register managed by these routines. */
124 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
127 /* Read unaligned data from the instruction buffer. */
129 union unaligned
131 void *p;
132 unsigned u2 __attribute__ ((mode (HI)));
133 unsigned u4 __attribute__ ((mode (SI)));
134 unsigned u8 __attribute__ ((mode (DI)));
135 signed s2 __attribute__ ((mode (HI)));
136 signed s4 __attribute__ ((mode (SI)));
137 signed s8 __attribute__ ((mode (DI)));
138 } __attribute__ ((packed));
140 static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
141 static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
142 _Unwind_FrameState *);
144 static inline void *
145 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
147 static inline int
148 read_1u (const void *p) { return *(const unsigned char *) p; }
150 static inline int
151 read_1s (const void *p) { return *(const signed char *) p; }
153 static inline int
154 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
156 static inline int
157 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
159 static inline unsigned int
160 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
162 static inline int
163 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
165 static inline unsigned long
166 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
168 static inline unsigned long
169 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
171 static inline _Unwind_Word
172 _Unwind_IsSignalFrame (struct _Unwind_Context *context)
174 return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
177 static inline void
178 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
180 if (val)
181 context->flags |= SIGNAL_FRAME_BIT;
182 else
183 context->flags &= ~SIGNAL_FRAME_BIT;
186 static inline _Unwind_Word
187 _Unwind_IsExtendedContext (struct _Unwind_Context *context)
189 return (ASSUME_EXTENDED_UNWIND_CONTEXT
190 || (context->flags & EXTENDED_CONTEXT_BIT));
193 /* Get the value of register INDEX as saved in CONTEXT. */
195 inline _Unwind_Word
196 _Unwind_GetGR (struct _Unwind_Context *context, int index)
198 int size;
199 _Unwind_Context_Reg_Val val;
201 #ifdef DWARF_ZERO_REG
202 if (index == DWARF_ZERO_REG)
203 return 0;
204 #endif
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];
209 val = context->reg[index];
211 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
212 return _Unwind_Get_Unwind_Word (val);
214 /* This will segfault if the register hasn't been saved. */
215 if (size == sizeof(_Unwind_Ptr))
216 return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
217 else
219 gcc_assert (size == sizeof(_Unwind_Word));
220 return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
224 static inline void *
225 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
227 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
230 /* Get the value of the CFA as saved in CONTEXT. */
232 _Unwind_Word
233 _Unwind_GetCFA (struct _Unwind_Context *context)
235 return (_Unwind_Ptr) context->cfa;
238 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
240 inline void
241 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
243 int size;
244 void *ptr;
246 index = DWARF_REG_TO_UNWIND_COLUMN (index);
247 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
248 size = dwarf_reg_size_table[index];
250 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
252 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
253 return;
256 ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
258 if (size == sizeof(_Unwind_Ptr))
259 * (_Unwind_Ptr *) ptr = val;
260 else
262 gcc_assert (size == sizeof(_Unwind_Word));
263 * (_Unwind_Word *) ptr = val;
267 /* Get the pointer to a register INDEX as saved in CONTEXT. */
269 static inline void *
270 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
272 index = DWARF_REG_TO_UNWIND_COLUMN (index);
273 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
274 return &context->reg[index];
275 return (void *) (_Unwind_Internal_Ptr) context->reg[index];
278 /* Set the pointer to a register INDEX as saved in CONTEXT. */
280 static inline void
281 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
283 index = DWARF_REG_TO_UNWIND_COLUMN (index);
284 if (_Unwind_IsExtendedContext (context))
285 context->by_value[index] = 0;
286 context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
289 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
291 static inline void
292 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
293 _Unwind_Word val)
295 index = DWARF_REG_TO_UNWIND_COLUMN (index);
296 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
297 /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
298 gcc_assert (dwarf_reg_size_table[index] <= sizeof (_Unwind_Context_Reg_Val));
300 context->by_value[index] = 1;
301 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
304 /* Return nonzero if register INDEX is stored by value rather than
305 by reference. */
307 static inline int
308 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
310 index = DWARF_REG_TO_UNWIND_COLUMN (index);
311 return context->by_value[index];
314 /* Retrieve the return address for CONTEXT. */
316 inline _Unwind_Ptr
317 _Unwind_GetIP (struct _Unwind_Context *context)
319 return (_Unwind_Ptr) context->ra;
322 /* Retrieve the return address and flag whether that IP is before
323 or after first not yet fully executed instruction. */
325 inline _Unwind_Ptr
326 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
328 *ip_before_insn = _Unwind_IsSignalFrame (context);
329 return (_Unwind_Ptr) context->ra;
332 /* Overwrite the return address for CONTEXT with VAL. */
334 inline void
335 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
337 context->ra = (void *) val;
340 void *
341 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
343 return context->lsda;
346 _Unwind_Ptr
347 _Unwind_GetRegionStart (struct _Unwind_Context *context)
349 return (_Unwind_Ptr) context->bases.func;
352 void *
353 _Unwind_FindEnclosingFunction (void *pc)
355 struct dwarf_eh_bases bases;
356 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
357 if (fde)
358 return bases.func;
359 else
360 return NULL;
363 #ifndef __ia64__
364 _Unwind_Ptr
365 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
367 return (_Unwind_Ptr) context->bases.dbase;
370 _Unwind_Ptr
371 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
373 return (_Unwind_Ptr) context->bases.tbase;
375 #endif
377 #include "md-unwind-support.h"
379 /* Extract any interesting information from the CIE for the translation
380 unit F belongs to. Return a pointer to the byte after the augmentation,
381 or NULL if we encountered an undecipherable augmentation. */
383 static const unsigned char *
384 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
385 _Unwind_FrameState *fs)
387 const unsigned char *aug = cie->augmentation;
388 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
389 const unsigned char *ret = NULL;
390 _uleb128_t utmp;
391 _sleb128_t stmp;
393 /* g++ v2 "eh" has pointer immediately following augmentation string,
394 so it must be handled first. */
395 if (aug[0] == 'e' && aug[1] == 'h')
397 fs->eh_ptr = read_pointer (p);
398 p += sizeof (void *);
399 aug += 2;
402 /* After the augmentation resp. pointer for "eh" augmentation
403 follows for CIE version >= 4 address size byte and
404 segment size byte. */
405 if (__builtin_expect (cie->version >= 4, 0))
407 if (p[0] != sizeof (void *) || p[1] != 0)
408 return NULL;
409 p += 2;
411 /* Immediately following this are the code and
412 data alignment and return address column. */
413 p = read_uleb128 (p, &utmp);
414 fs->code_align = (_Unwind_Word)utmp;
415 p = read_sleb128 (p, &stmp);
416 fs->data_align = (_Unwind_Sword)stmp;
417 if (cie->version == 1)
418 fs->retaddr_column = *p++;
419 else
421 p = read_uleb128 (p, &utmp);
422 fs->retaddr_column = (_Unwind_Word)utmp;
424 fs->lsda_encoding = DW_EH_PE_omit;
426 /* If the augmentation starts with 'z', then a uleb128 immediately
427 follows containing the length of the augmentation field following
428 the size. */
429 if (*aug == 'z')
431 p = read_uleb128 (p, &utmp);
432 ret = p + utmp;
434 fs->saw_z = 1;
435 ++aug;
438 /* Iterate over recognized augmentation subsequences. */
439 while (*aug != '\0')
441 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
442 if (aug[0] == 'L')
444 fs->lsda_encoding = *p++;
445 aug += 1;
448 /* "R" indicates a byte indicating how FDE addresses are encoded. */
449 else if (aug[0] == 'R')
451 fs->fde_encoding = *p++;
452 aug += 1;
455 /* "P" indicates a personality routine in the CIE augmentation. */
456 else if (aug[0] == 'P')
458 _Unwind_Ptr personality;
460 p = read_encoded_value (context, *p, p + 1, &personality);
461 fs->personality = (_Unwind_Personality_Fn) personality;
462 aug += 1;
465 /* "S" indicates a signal frame. */
466 else if (aug[0] == 'S')
468 fs->signal_frame = 1;
469 aug += 1;
472 /* Otherwise we have an unknown augmentation string.
473 Bail unless we saw a 'z' prefix. */
474 else
475 return ret;
478 return ret ? ret : p;
482 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
483 onto the stack to start. */
485 static _Unwind_Word
486 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
487 struct _Unwind_Context *context, _Unwind_Word initial)
489 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
490 int stack_elt;
492 stack[0] = initial;
493 stack_elt = 1;
495 while (op_ptr < op_end)
497 enum dwarf_location_atom op = *op_ptr++;
498 _Unwind_Word result;
499 _uleb128_t reg, utmp;
500 _sleb128_t offset, stmp;
502 switch (op)
504 case DW_OP_lit0:
505 case DW_OP_lit1:
506 case DW_OP_lit2:
507 case DW_OP_lit3:
508 case DW_OP_lit4:
509 case DW_OP_lit5:
510 case DW_OP_lit6:
511 case DW_OP_lit7:
512 case DW_OP_lit8:
513 case DW_OP_lit9:
514 case DW_OP_lit10:
515 case DW_OP_lit11:
516 case DW_OP_lit12:
517 case DW_OP_lit13:
518 case DW_OP_lit14:
519 case DW_OP_lit15:
520 case DW_OP_lit16:
521 case DW_OP_lit17:
522 case DW_OP_lit18:
523 case DW_OP_lit19:
524 case DW_OP_lit20:
525 case DW_OP_lit21:
526 case DW_OP_lit22:
527 case DW_OP_lit23:
528 case DW_OP_lit24:
529 case DW_OP_lit25:
530 case DW_OP_lit26:
531 case DW_OP_lit27:
532 case DW_OP_lit28:
533 case DW_OP_lit29:
534 case DW_OP_lit30:
535 case DW_OP_lit31:
536 result = op - DW_OP_lit0;
537 break;
539 case DW_OP_addr:
540 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
541 op_ptr += sizeof (void *);
542 break;
544 case DW_OP_GNU_encoded_addr:
546 _Unwind_Ptr presult;
547 op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
548 result = presult;
550 break;
552 case DW_OP_const1u:
553 result = read_1u (op_ptr);
554 op_ptr += 1;
555 break;
556 case DW_OP_const1s:
557 result = read_1s (op_ptr);
558 op_ptr += 1;
559 break;
560 case DW_OP_const2u:
561 result = read_2u (op_ptr);
562 op_ptr += 2;
563 break;
564 case DW_OP_const2s:
565 result = read_2s (op_ptr);
566 op_ptr += 2;
567 break;
568 case DW_OP_const4u:
569 result = read_4u (op_ptr);
570 op_ptr += 4;
571 break;
572 case DW_OP_const4s:
573 result = read_4s (op_ptr);
574 op_ptr += 4;
575 break;
576 case DW_OP_const8u:
577 result = read_8u (op_ptr);
578 op_ptr += 8;
579 break;
580 case DW_OP_const8s:
581 result = read_8s (op_ptr);
582 op_ptr += 8;
583 break;
584 case DW_OP_constu:
585 op_ptr = read_uleb128 (op_ptr, &utmp);
586 result = (_Unwind_Word)utmp;
587 break;
588 case DW_OP_consts:
589 op_ptr = read_sleb128 (op_ptr, &stmp);
590 result = (_Unwind_Sword)stmp;
591 break;
593 case DW_OP_reg0:
594 case DW_OP_reg1:
595 case DW_OP_reg2:
596 case DW_OP_reg3:
597 case DW_OP_reg4:
598 case DW_OP_reg5:
599 case DW_OP_reg6:
600 case DW_OP_reg7:
601 case DW_OP_reg8:
602 case DW_OP_reg9:
603 case DW_OP_reg10:
604 case DW_OP_reg11:
605 case DW_OP_reg12:
606 case DW_OP_reg13:
607 case DW_OP_reg14:
608 case DW_OP_reg15:
609 case DW_OP_reg16:
610 case DW_OP_reg17:
611 case DW_OP_reg18:
612 case DW_OP_reg19:
613 case DW_OP_reg20:
614 case DW_OP_reg21:
615 case DW_OP_reg22:
616 case DW_OP_reg23:
617 case DW_OP_reg24:
618 case DW_OP_reg25:
619 case DW_OP_reg26:
620 case DW_OP_reg27:
621 case DW_OP_reg28:
622 case DW_OP_reg29:
623 case DW_OP_reg30:
624 case DW_OP_reg31:
625 result = _Unwind_GetGR (context, op - DW_OP_reg0);
626 break;
627 case DW_OP_regx:
628 op_ptr = read_uleb128 (op_ptr, &reg);
629 result = _Unwind_GetGR (context, reg);
630 break;
632 case DW_OP_breg0:
633 case DW_OP_breg1:
634 case DW_OP_breg2:
635 case DW_OP_breg3:
636 case DW_OP_breg4:
637 case DW_OP_breg5:
638 case DW_OP_breg6:
639 case DW_OP_breg7:
640 case DW_OP_breg8:
641 case DW_OP_breg9:
642 case DW_OP_breg10:
643 case DW_OP_breg11:
644 case DW_OP_breg12:
645 case DW_OP_breg13:
646 case DW_OP_breg14:
647 case DW_OP_breg15:
648 case DW_OP_breg16:
649 case DW_OP_breg17:
650 case DW_OP_breg18:
651 case DW_OP_breg19:
652 case DW_OP_breg20:
653 case DW_OP_breg21:
654 case DW_OP_breg22:
655 case DW_OP_breg23:
656 case DW_OP_breg24:
657 case DW_OP_breg25:
658 case DW_OP_breg26:
659 case DW_OP_breg27:
660 case DW_OP_breg28:
661 case DW_OP_breg29:
662 case DW_OP_breg30:
663 case DW_OP_breg31:
664 op_ptr = read_sleb128 (op_ptr, &offset);
665 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
666 break;
667 case DW_OP_bregx:
668 op_ptr = read_uleb128 (op_ptr, &reg);
669 op_ptr = read_sleb128 (op_ptr, &offset);
670 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
671 break;
673 case DW_OP_dup:
674 gcc_assert (stack_elt);
675 result = stack[stack_elt - 1];
676 break;
678 case DW_OP_drop:
679 gcc_assert (stack_elt);
680 stack_elt -= 1;
681 goto no_push;
683 case DW_OP_pick:
684 offset = *op_ptr++;
685 gcc_assert (offset < stack_elt - 1);
686 result = stack[stack_elt - 1 - offset];
687 break;
689 case DW_OP_over:
690 gcc_assert (stack_elt >= 2);
691 result = stack[stack_elt - 2];
692 break;
694 case DW_OP_swap:
696 _Unwind_Word t;
697 gcc_assert (stack_elt >= 2);
698 t = stack[stack_elt - 1];
699 stack[stack_elt - 1] = stack[stack_elt - 2];
700 stack[stack_elt - 2] = t;
701 goto no_push;
704 case DW_OP_rot:
706 _Unwind_Word t1, t2, t3;
708 gcc_assert (stack_elt >= 3);
709 t1 = stack[stack_elt - 1];
710 t2 = stack[stack_elt - 2];
711 t3 = stack[stack_elt - 3];
712 stack[stack_elt - 1] = t2;
713 stack[stack_elt - 2] = t3;
714 stack[stack_elt - 3] = t1;
715 goto no_push;
718 case DW_OP_deref:
719 case DW_OP_deref_size:
720 case DW_OP_abs:
721 case DW_OP_neg:
722 case DW_OP_not:
723 case DW_OP_plus_uconst:
724 /* Unary operations. */
725 gcc_assert (stack_elt);
726 stack_elt -= 1;
728 result = stack[stack_elt];
730 switch (op)
732 case DW_OP_deref:
734 void *ptr = (void *) (_Unwind_Ptr) result;
735 result = (_Unwind_Ptr) read_pointer (ptr);
737 break;
739 case DW_OP_deref_size:
741 void *ptr = (void *) (_Unwind_Ptr) result;
742 switch (*op_ptr++)
744 case 1:
745 result = read_1u (ptr);
746 break;
747 case 2:
748 result = read_2u (ptr);
749 break;
750 case 4:
751 result = read_4u (ptr);
752 break;
753 case 8:
754 result = read_8u (ptr);
755 break;
756 default:
757 gcc_unreachable ();
760 break;
762 case DW_OP_abs:
763 if ((_Unwind_Sword) result < 0)
764 result = -result;
765 break;
766 case DW_OP_neg:
767 result = -result;
768 break;
769 case DW_OP_not:
770 result = ~result;
771 break;
772 case DW_OP_plus_uconst:
773 op_ptr = read_uleb128 (op_ptr, &utmp);
774 result += (_Unwind_Word)utmp;
775 break;
777 default:
778 gcc_unreachable ();
780 break;
782 case DW_OP_and:
783 case DW_OP_div:
784 case DW_OP_minus:
785 case DW_OP_mod:
786 case DW_OP_mul:
787 case DW_OP_or:
788 case DW_OP_plus:
789 case DW_OP_shl:
790 case DW_OP_shr:
791 case DW_OP_shra:
792 case DW_OP_xor:
793 case DW_OP_le:
794 case DW_OP_ge:
795 case DW_OP_eq:
796 case DW_OP_lt:
797 case DW_OP_gt:
798 case DW_OP_ne:
800 /* Binary operations. */
801 _Unwind_Word first, second;
802 gcc_assert (stack_elt >= 2);
803 stack_elt -= 2;
805 second = stack[stack_elt];
806 first = stack[stack_elt + 1];
808 switch (op)
810 case DW_OP_and:
811 result = second & first;
812 break;
813 case DW_OP_div:
814 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
815 break;
816 case DW_OP_minus:
817 result = second - first;
818 break;
819 case DW_OP_mod:
820 result = second % first;
821 break;
822 case DW_OP_mul:
823 result = second * first;
824 break;
825 case DW_OP_or:
826 result = second | first;
827 break;
828 case DW_OP_plus:
829 result = second + first;
830 break;
831 case DW_OP_shl:
832 result = second << first;
833 break;
834 case DW_OP_shr:
835 result = second >> first;
836 break;
837 case DW_OP_shra:
838 result = (_Unwind_Sword) second >> first;
839 break;
840 case DW_OP_xor:
841 result = second ^ first;
842 break;
843 case DW_OP_le:
844 result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
845 break;
846 case DW_OP_ge:
847 result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
848 break;
849 case DW_OP_eq:
850 result = (_Unwind_Sword) second == (_Unwind_Sword) first;
851 break;
852 case DW_OP_lt:
853 result = (_Unwind_Sword) second < (_Unwind_Sword) first;
854 break;
855 case DW_OP_gt:
856 result = (_Unwind_Sword) second > (_Unwind_Sword) first;
857 break;
858 case DW_OP_ne:
859 result = (_Unwind_Sword) second != (_Unwind_Sword) first;
860 break;
862 default:
863 gcc_unreachable ();
866 break;
868 case DW_OP_skip:
869 offset = read_2s (op_ptr);
870 op_ptr += 2;
871 op_ptr += offset;
872 goto no_push;
874 case DW_OP_bra:
875 gcc_assert (stack_elt);
876 stack_elt -= 1;
878 offset = read_2s (op_ptr);
879 op_ptr += 2;
880 if (stack[stack_elt] != 0)
881 op_ptr += offset;
882 goto no_push;
884 case DW_OP_nop:
885 goto no_push;
887 default:
888 gcc_unreachable ();
891 /* Most things push a result value. */
892 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
893 stack[stack_elt++] = result;
894 no_push:;
897 /* We were executing this program to get a value. It should be
898 at top of stack. */
899 gcc_assert (stack_elt);
900 stack_elt -= 1;
901 return stack[stack_elt];
905 /* Decode DWARF 2 call frame information. Takes pointers the
906 instruction sequence to decode, current register information and
907 CIE info, and the PC range to evaluate. */
909 static void
910 execute_cfa_program (const unsigned char *insn_ptr,
911 const unsigned char *insn_end,
912 struct _Unwind_Context *context,
913 _Unwind_FrameState *fs)
915 struct frame_state_reg_info *unused_rs = NULL;
917 /* Don't allow remember/restore between CIE and FDE programs. */
918 fs->regs.prev = NULL;
920 /* The comparison with the return address uses < rather than <= because
921 we are only interested in the effects of code before the call; for a
922 noreturn function, the return address may point to unrelated code with
923 a different stack configuration that we are not interested in. We
924 assume that the call itself is unwind info-neutral; if not, or if
925 there are delay instructions that adjust the stack, these must be
926 reflected at the point immediately before the call insn.
927 In signal frames, return address is after last completed instruction,
928 so we add 1 to return address to make the comparison <=. */
929 while (insn_ptr < insn_end
930 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
932 unsigned char insn = *insn_ptr++;
933 _uleb128_t reg, utmp;
934 _sleb128_t offset, stmp;
936 if ((insn & 0xc0) == DW_CFA_advance_loc)
937 fs->pc += (insn & 0x3f) * fs->code_align;
938 else if ((insn & 0xc0) == DW_CFA_offset)
940 reg = insn & 0x3f;
941 insn_ptr = read_uleb128 (insn_ptr, &utmp);
942 offset = (_Unwind_Sword) utmp * fs->data_align;
943 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
944 = REG_SAVED_OFFSET;
945 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
947 else if ((insn & 0xc0) == DW_CFA_restore)
949 reg = insn & 0x3f;
950 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
952 else switch (insn)
954 case DW_CFA_set_loc:
956 _Unwind_Ptr pc;
958 insn_ptr = read_encoded_value (context, fs->fde_encoding,
959 insn_ptr, &pc);
960 fs->pc = (void *) pc;
962 break;
964 case DW_CFA_advance_loc1:
965 fs->pc += read_1u (insn_ptr) * fs->code_align;
966 insn_ptr += 1;
967 break;
968 case DW_CFA_advance_loc2:
969 fs->pc += read_2u (insn_ptr) * fs->code_align;
970 insn_ptr += 2;
971 break;
972 case DW_CFA_advance_loc4:
973 fs->pc += read_4u (insn_ptr) * fs->code_align;
974 insn_ptr += 4;
975 break;
977 case DW_CFA_offset_extended:
978 insn_ptr = read_uleb128 (insn_ptr, &reg);
979 insn_ptr = read_uleb128 (insn_ptr, &utmp);
980 offset = (_Unwind_Sword) utmp * fs->data_align;
981 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
982 = REG_SAVED_OFFSET;
983 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
984 break;
986 case DW_CFA_restore_extended:
987 insn_ptr = read_uleb128 (insn_ptr, &reg);
988 /* FIXME, this is wrong; the CIE might have said that the
989 register was saved somewhere. */
990 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
991 break;
993 case DW_CFA_same_value:
994 insn_ptr = read_uleb128 (insn_ptr, &reg);
995 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
996 break;
998 case DW_CFA_undefined:
999 insn_ptr = read_uleb128 (insn_ptr, &reg);
1000 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNDEFINED;
1001 break;
1003 case DW_CFA_nop:
1004 break;
1006 case DW_CFA_register:
1008 _uleb128_t reg2;
1009 insn_ptr = read_uleb128 (insn_ptr, &reg);
1010 insn_ptr = read_uleb128 (insn_ptr, &reg2);
1011 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
1012 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg =
1013 (_Unwind_Word)reg2;
1015 break;
1017 case DW_CFA_remember_state:
1019 struct frame_state_reg_info *new_rs;
1020 if (unused_rs)
1022 new_rs = unused_rs;
1023 unused_rs = unused_rs->prev;
1025 else
1026 new_rs = alloca (sizeof (struct frame_state_reg_info));
1028 *new_rs = fs->regs;
1029 fs->regs.prev = new_rs;
1031 break;
1033 case DW_CFA_restore_state:
1035 struct frame_state_reg_info *old_rs = fs->regs.prev;
1036 fs->regs = *old_rs;
1037 old_rs->prev = unused_rs;
1038 unused_rs = old_rs;
1040 break;
1042 case DW_CFA_def_cfa:
1043 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1044 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1045 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1046 fs->regs.cfa_offset = (_Unwind_Word)utmp;
1047 fs->regs.cfa_how = CFA_REG_OFFSET;
1048 break;
1050 case DW_CFA_def_cfa_register:
1051 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1052 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1053 fs->regs.cfa_how = CFA_REG_OFFSET;
1054 break;
1056 case DW_CFA_def_cfa_offset:
1057 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1058 fs->regs.cfa_offset = utmp;
1059 /* cfa_how deliberately not set. */
1060 break;
1062 case DW_CFA_def_cfa_expression:
1063 fs->regs.cfa_exp = insn_ptr;
1064 fs->regs.cfa_how = CFA_EXP;
1065 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1066 insn_ptr += utmp;
1067 break;
1069 case DW_CFA_expression:
1070 insn_ptr = read_uleb128 (insn_ptr, &reg);
1071 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
1072 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1073 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1074 insn_ptr += utmp;
1075 break;
1077 /* Dwarf3. */
1078 case DW_CFA_offset_extended_sf:
1079 insn_ptr = read_uleb128 (insn_ptr, &reg);
1080 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1081 offset = stmp * fs->data_align;
1082 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1083 = REG_SAVED_OFFSET;
1084 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1085 break;
1087 case DW_CFA_def_cfa_sf:
1088 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1089 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1090 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1091 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1092 fs->regs.cfa_how = CFA_REG_OFFSET;
1093 fs->regs.cfa_offset *= fs->data_align;
1094 break;
1096 case DW_CFA_def_cfa_offset_sf:
1097 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1098 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1099 fs->regs.cfa_offset *= fs->data_align;
1100 /* cfa_how deliberately not set. */
1101 break;
1103 case DW_CFA_val_offset:
1104 insn_ptr = read_uleb128 (insn_ptr, &reg);
1105 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1106 offset = (_Unwind_Sword) utmp * fs->data_align;
1107 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1108 = REG_SAVED_VAL_OFFSET;
1109 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1110 break;
1112 case DW_CFA_val_offset_sf:
1113 insn_ptr = read_uleb128 (insn_ptr, &reg);
1114 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1115 offset = stmp * fs->data_align;
1116 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1117 = REG_SAVED_VAL_OFFSET;
1118 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
1119 break;
1121 case DW_CFA_val_expression:
1122 insn_ptr = read_uleb128 (insn_ptr, &reg);
1123 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1124 = REG_SAVED_VAL_EXP;
1125 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
1126 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1127 insn_ptr += utmp;
1128 break;
1130 case DW_CFA_GNU_window_save:
1131 /* ??? Hardcoded for SPARC register window configuration. */
1132 for (reg = 16; reg < 32; ++reg)
1134 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1135 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1137 break;
1139 case DW_CFA_GNU_args_size:
1140 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1141 context->args_size = (_Unwind_Word)utmp;
1142 break;
1144 case DW_CFA_GNU_negative_offset_extended:
1145 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1146 older PowerPC code. */
1147 insn_ptr = read_uleb128 (insn_ptr, &reg);
1148 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1149 offset = (_Unwind_Word) utmp * fs->data_align;
1150 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
1151 = REG_SAVED_OFFSET;
1152 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
1153 break;
1155 default:
1156 gcc_unreachable ();
1161 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1162 its caller and decode it into FS. This function also sets the
1163 args_size and lsda members of CONTEXT, as they are really information
1164 about the caller's frame. */
1166 static _Unwind_Reason_Code
1167 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1169 const struct dwarf_fde *fde;
1170 const struct dwarf_cie *cie;
1171 const unsigned char *aug, *insn, *end;
1173 memset (fs, 0, sizeof (*fs));
1174 context->args_size = 0;
1175 context->lsda = 0;
1177 if (context->ra == 0)
1178 return _URC_END_OF_STACK;
1180 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1181 &context->bases);
1182 if (fde == NULL)
1184 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1185 /* Couldn't find frame unwind info for this function. Try a
1186 target-specific fallback mechanism. This will necessarily
1187 not provide a personality routine or LSDA. */
1188 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1189 #else
1190 return _URC_END_OF_STACK;
1191 #endif
1194 fs->pc = context->bases.func;
1196 cie = get_cie (fde);
1197 insn = extract_cie_info (cie, context, fs);
1198 if (insn == NULL)
1199 /* CIE contained unknown augmentation. */
1200 return _URC_FATAL_PHASE1_ERROR;
1202 /* First decode all the insns in the CIE. */
1203 end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
1204 execute_cfa_program (insn, end, context, fs);
1206 /* Locate augmentation for the fde. */
1207 aug = (const unsigned char *) fde + sizeof (*fde);
1208 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1209 insn = NULL;
1210 if (fs->saw_z)
1212 _uleb128_t i;
1213 aug = read_uleb128 (aug, &i);
1214 insn = aug + i;
1216 if (fs->lsda_encoding != DW_EH_PE_omit)
1218 _Unwind_Ptr lsda;
1220 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1221 context->lsda = (void *) lsda;
1224 /* Then the insns in the FDE up to our target PC. */
1225 if (insn == NULL)
1226 insn = aug;
1227 end = (const unsigned char *) next_fde (fde);
1228 execute_cfa_program (insn, end, context, fs);
1230 return _URC_NO_REASON;
1233 typedef struct frame_state
1235 void *cfa;
1236 void *eh_ptr;
1237 long cfa_offset;
1238 long args_size;
1239 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1240 unsigned short cfa_reg;
1241 unsigned short retaddr_column;
1242 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1243 } frame_state;
1245 struct frame_state * __frame_state_for (void *, struct frame_state *);
1247 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1248 a given PC_TARGET. The caller should allocate a local variable of
1249 `struct frame_state' and pass its address to STATE_IN. */
1251 struct frame_state *
1252 __frame_state_for (void *pc_target, struct frame_state *state_in)
1254 struct _Unwind_Context context;
1255 _Unwind_FrameState fs;
1256 int reg;
1258 memset (&context, 0, sizeof (struct _Unwind_Context));
1259 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1260 context.flags = EXTENDED_CONTEXT_BIT;
1261 context.ra = pc_target + 1;
1263 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1264 return 0;
1266 /* We have no way to pass a location expression for the CFA to our
1267 caller. It wouldn't understand it anyway. */
1268 if (fs.regs.cfa_how == CFA_EXP)
1269 return 0;
1271 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1273 state_in->saved[reg] = fs.regs.reg[reg].how;
1274 switch (state_in->saved[reg])
1276 case REG_SAVED_REG:
1277 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1278 break;
1279 case REG_SAVED_OFFSET:
1280 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1281 break;
1282 default:
1283 state_in->reg_or_offset[reg] = 0;
1284 break;
1288 state_in->cfa_offset = fs.regs.cfa_offset;
1289 state_in->cfa_reg = fs.regs.cfa_reg;
1290 state_in->retaddr_column = fs.retaddr_column;
1291 state_in->args_size = context.args_size;
1292 state_in->eh_ptr = fs.eh_ptr;
1294 return state_in;
1297 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1299 static inline void
1300 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1301 _Unwind_SpTmp *tmp_sp)
1303 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1305 if (size == sizeof(_Unwind_Ptr))
1306 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1307 else
1309 gcc_assert (size == sizeof(_Unwind_Word));
1310 tmp_sp->word = (_Unwind_Ptr) cfa;
1312 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1315 static void
1316 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1318 struct _Unwind_Context orig_context = *context;
1319 void *cfa;
1320 long i;
1322 #ifdef EH_RETURN_STACKADJ_RTX
1323 /* Special handling here: Many machines do not use a frame pointer,
1324 and track the CFA only through offsets from the stack pointer from
1325 one frame to the next. In this case, the stack pointer is never
1326 stored, so it has no saved address in the context. What we do
1327 have is the CFA from the previous stack frame.
1329 In very special situations (such as unwind info for signal return),
1330 there may be location expressions that use the stack pointer as well.
1332 Do this conditionally for one frame. This allows the unwind info
1333 for one frame to save a copy of the stack pointer from the previous
1334 frame, and be able to use much easier CFA mechanisms to do it.
1335 Always zap the saved stack pointer value for the next frame; carrying
1336 the value over from one frame to another doesn't make sense. */
1338 _Unwind_SpTmp tmp_sp;
1340 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1341 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1342 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1343 #endif
1345 /* Compute this frame's CFA. */
1346 switch (fs->regs.cfa_how)
1348 case CFA_REG_OFFSET:
1349 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1350 cfa += fs->regs.cfa_offset;
1351 break;
1353 case CFA_EXP:
1355 const unsigned char *exp = fs->regs.cfa_exp;
1356 _uleb128_t len;
1358 exp = read_uleb128 (exp, &len);
1359 cfa = (void *) (_Unwind_Ptr)
1360 execute_stack_op (exp, exp + len, &orig_context, 0);
1361 break;
1364 default:
1365 gcc_unreachable ();
1367 context->cfa = cfa;
1369 /* Compute the addresses of all registers saved in this frame. */
1370 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1371 switch (fs->regs.reg[i].how)
1373 case REG_UNSAVED:
1374 case REG_UNDEFINED:
1375 break;
1377 case REG_SAVED_OFFSET:
1378 _Unwind_SetGRPtr (context, i,
1379 (void *) (cfa + fs->regs.reg[i].loc.offset));
1380 break;
1382 case REG_SAVED_REG:
1383 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1384 _Unwind_SetGRValue (context, i,
1385 _Unwind_GetGR (&orig_context,
1386 fs->regs.reg[i].loc.reg));
1387 else
1388 _Unwind_SetGRPtr (context, i,
1389 _Unwind_GetGRPtr (&orig_context,
1390 fs->regs.reg[i].loc.reg));
1391 break;
1393 case REG_SAVED_EXP:
1395 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1396 _uleb128_t len;
1397 _Unwind_Ptr val;
1399 exp = read_uleb128 (exp, &len);
1400 val = execute_stack_op (exp, exp + len, &orig_context,
1401 (_Unwind_Ptr) cfa);
1402 _Unwind_SetGRPtr (context, i, (void *) val);
1404 break;
1406 case REG_SAVED_VAL_OFFSET:
1407 _Unwind_SetGRValue (context, i,
1408 (_Unwind_Internal_Ptr)
1409 (cfa + fs->regs.reg[i].loc.offset));
1410 break;
1412 case REG_SAVED_VAL_EXP:
1414 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1415 _uleb128_t len;
1416 _Unwind_Ptr val;
1418 exp = read_uleb128 (exp, &len);
1419 val = execute_stack_op (exp, exp + len, &orig_context,
1420 (_Unwind_Ptr) cfa);
1421 _Unwind_SetGRValue (context, i, val);
1423 break;
1426 _Unwind_SetSignalFrame (context, fs->signal_frame);
1428 #ifdef MD_FROB_UPDATE_CONTEXT
1429 MD_FROB_UPDATE_CONTEXT (context, fs);
1430 #endif
1433 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1434 of its caller. Update CONTEXT to refer to the caller as well. Note
1435 that the args_size and lsda members are not updated here, but later in
1436 uw_frame_state_for. */
1438 static void
1439 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1441 uw_update_context_1 (context, fs);
1443 /* In general this unwinder doesn't make any distinction between
1444 undefined and same_value rule. Call-saved registers are assumed
1445 to have same_value rule by default and explicit undefined
1446 rule is handled like same_value. The only exception is
1447 DW_CFA_undefined on retaddr_column which is supposed to
1448 mark outermost frame in DWARF 3. */
1449 if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
1450 == REG_UNDEFINED)
1451 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1452 stack frame. */
1453 context->ra = 0;
1454 else
1455 /* Compute the return address now, since the return address column
1456 can change from frame to frame. */
1457 context->ra = __builtin_extract_return_addr
1458 (_Unwind_GetPtr (context, fs->retaddr_column));
1461 static void
1462 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1464 uw_update_context (context, fs);
1467 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1468 level will be the return address and the CFA. */
1470 #define uw_init_context(CONTEXT) \
1471 do \
1473 /* Do any necessary initialization to access arbitrary stack frames. \
1474 On the SPARC, this means flushing the register windows. */ \
1475 __builtin_unwind_init (); \
1476 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1477 __builtin_return_address (0)); \
1479 while (0)
1481 static inline void
1482 init_dwarf_reg_size_table (void)
1484 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1487 static void __attribute__((noinline))
1488 uw_init_context_1 (struct _Unwind_Context *context,
1489 void *outer_cfa, void *outer_ra)
1491 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1492 _Unwind_FrameState fs;
1493 _Unwind_SpTmp sp_slot;
1494 _Unwind_Reason_Code code;
1496 memset (context, 0, sizeof (struct _Unwind_Context));
1497 context->ra = ra;
1498 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1499 context->flags = EXTENDED_CONTEXT_BIT;
1501 code = uw_frame_state_for (context, &fs);
1502 gcc_assert (code == _URC_NO_REASON);
1504 #if __GTHREADS
1506 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1507 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1508 && dwarf_reg_size_table[0] == 0)
1509 init_dwarf_reg_size_table ();
1511 #else
1512 if (dwarf_reg_size_table[0] == 0)
1513 init_dwarf_reg_size_table ();
1514 #endif
1516 /* Force the frame state to use the known cfa value. */
1517 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1518 fs.regs.cfa_how = CFA_REG_OFFSET;
1519 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1520 fs.regs.cfa_offset = 0;
1522 uw_update_context_1 (context, &fs);
1524 /* If the return address column was saved in a register in the
1525 initialization context, then we can't see it in the given
1526 call frame data. So have the initialization context tell us. */
1527 context->ra = __builtin_extract_return_addr (outer_ra);
1530 static void _Unwind_DebugHook (void *, void *)
1531 __attribute__ ((__noinline__, __used__, __noclone__));
1533 /* This function is called during unwinding. It is intended as a hook
1534 for a debugger to intercept exceptions. CFA is the CFA of the
1535 target frame. HANDLER is the PC to which control will be
1536 transferred. */
1537 static void
1538 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
1539 void *handler __attribute__ ((__unused__)))
1541 /* We only want to use stap probes starting with v3. Earlier
1542 versions added too much startup cost. */
1543 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1544 STAP_PROBE2 (libgcc, unwind, cfa, handler);
1545 #else
1546 asm ("");
1547 #endif
1550 /* Install TARGET into CURRENT so that we can return to it. This is a
1551 macro because __builtin_eh_return must be invoked in the context of
1552 our caller. */
1554 #define uw_install_context(CURRENT, TARGET) \
1555 do \
1557 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1558 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1559 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1560 __builtin_eh_return (offset, handler); \
1562 while (0)
1564 static long
1565 uw_install_context_1 (struct _Unwind_Context *current,
1566 struct _Unwind_Context *target)
1568 long i;
1569 _Unwind_SpTmp sp_slot;
1571 /* If the target frame does not have a saved stack pointer,
1572 then set up the target's CFA. */
1573 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1574 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1576 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1578 void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
1579 void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
1581 gcc_assert (current->by_value[i] == 0);
1582 if (target->by_value[i] && c)
1584 _Unwind_Word w;
1585 _Unwind_Ptr p;
1586 if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
1588 w = (_Unwind_Internal_Ptr) t;
1589 memcpy (c, &w, sizeof (_Unwind_Word));
1591 else
1593 gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
1594 p = (_Unwind_Internal_Ptr) t;
1595 memcpy (c, &p, sizeof (_Unwind_Ptr));
1598 else if (t && c && t != c)
1599 memcpy (c, t, dwarf_reg_size_table[i]);
1602 /* If the current frame doesn't have a saved stack pointer, then we
1603 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1604 pointer value reloaded. */
1605 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1607 void *target_cfa;
1609 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1611 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1612 if (STACK_GROWS_DOWNWARD)
1613 return target_cfa - current->cfa + target->args_size;
1614 else
1615 return current->cfa - target_cfa - target->args_size;
1617 return 0;
1620 static inline _Unwind_Ptr
1621 uw_identify_context (struct _Unwind_Context *context)
1623 /* The CFA is not sufficient to disambiguate the context of a function
1624 interrupted by a signal before establishing its frame and the context
1625 of the signal itself. */
1626 if (STACK_GROWS_DOWNWARD)
1627 return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
1628 else
1629 return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
1633 #include "unwind.inc"
1635 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1636 alias (_Unwind_Backtrace);
1637 alias (_Unwind_DeleteException);
1638 alias (_Unwind_FindEnclosingFunction);
1639 alias (_Unwind_ForcedUnwind);
1640 alias (_Unwind_GetDataRelBase);
1641 alias (_Unwind_GetTextRelBase);
1642 alias (_Unwind_GetCFA);
1643 alias (_Unwind_GetGR);
1644 alias (_Unwind_GetIP);
1645 alias (_Unwind_GetLanguageSpecificData);
1646 alias (_Unwind_GetRegionStart);
1647 alias (_Unwind_RaiseException);
1648 alias (_Unwind_Resume);
1649 alias (_Unwind_Resume_or_Rethrow);
1650 alias (_Unwind_SetGR);
1651 alias (_Unwind_SetIP);
1652 #endif
1654 #endif /* !USING_SJLJ_EXCEPTIONS */