2005-06-07 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / unwind-dw2.c
blobb50ae010283dd14af61437fce7360eb6154ee26a
1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 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, 59 Temple Place - Suite 330, Boston, MA
29 02111-1307, 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 _Unwind_Word args_size;
76 /* Byte size of every register managed by these routines. */
77 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
80 /* Read unaligned data from the instruction buffer. */
82 union unaligned
84 void *p;
85 unsigned u2 __attribute__ ((mode (HI)));
86 unsigned u4 __attribute__ ((mode (SI)));
87 unsigned u8 __attribute__ ((mode (DI)));
88 signed s2 __attribute__ ((mode (HI)));
89 signed s4 __attribute__ ((mode (SI)));
90 signed s8 __attribute__ ((mode (DI)));
91 } __attribute__ ((packed));
93 static inline void *
94 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
96 static inline int
97 read_1u (const void *p) { return *(const unsigned char *) p; }
99 static inline int
100 read_1s (const void *p) { return *(const signed char *) p; }
102 static inline int
103 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
105 static inline int
106 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
108 static inline unsigned int
109 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
111 static inline int
112 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
114 static inline unsigned long
115 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
117 static inline unsigned long
118 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
120 /* Get the value of register REG as saved in CONTEXT. */
122 inline _Unwind_Word
123 _Unwind_GetGR (struct _Unwind_Context *context, int index)
125 int size;
126 void *ptr;
128 #ifdef DWARF_ZERO_REG
129 if (index == DWARF_ZERO_REG)
130 return 0;
131 #endif
133 index = DWARF_REG_TO_UNWIND_COLUMN (index);
134 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
135 size = dwarf_reg_size_table[index];
136 ptr = context->reg[index];
138 /* This will segfault if the register hasn't been saved. */
139 if (size == sizeof(_Unwind_Ptr))
140 return * (_Unwind_Ptr *) ptr;
141 else
143 gcc_assert (size == sizeof(_Unwind_Word));
144 return * (_Unwind_Word *) ptr;
148 static inline void *
149 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
151 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
154 /* Get the value of the CFA as saved in CONTEXT. */
156 _Unwind_Word
157 _Unwind_GetCFA (struct _Unwind_Context *context)
159 return (_Unwind_Ptr) context->cfa;
162 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
164 inline void
165 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
167 int size;
168 void *ptr;
170 index = DWARF_REG_TO_UNWIND_COLUMN (index);
171 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
172 size = dwarf_reg_size_table[index];
173 ptr = context->reg[index];
175 if (size == sizeof(_Unwind_Ptr))
176 * (_Unwind_Ptr *) ptr = val;
177 else
179 gcc_assert (size == sizeof(_Unwind_Word));
180 * (_Unwind_Word *) ptr = val;
184 /* Get the pointer to a register INDEX as saved in CONTEXT. */
186 static inline void *
187 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
189 index = DWARF_REG_TO_UNWIND_COLUMN (index);
190 return context->reg[index];
193 /* Set the pointer to a register INDEX as saved in CONTEXT. */
195 static inline void
196 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
198 index = DWARF_REG_TO_UNWIND_COLUMN (index);
199 context->reg[index] = p;
202 /* Retrieve the return address for CONTEXT. */
204 inline _Unwind_Ptr
205 _Unwind_GetIP (struct _Unwind_Context *context)
207 return (_Unwind_Ptr) context->ra;
210 /* Overwrite the return address for CONTEXT with VAL. */
212 inline void
213 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
215 context->ra = (void *) val;
218 void *
219 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
221 return context->lsda;
224 _Unwind_Ptr
225 _Unwind_GetRegionStart (struct _Unwind_Context *context)
227 return (_Unwind_Ptr) context->bases.func;
230 void *
231 _Unwind_FindEnclosingFunction (void *pc)
233 struct dwarf_eh_bases bases;
234 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
235 if (fde)
236 return bases.func;
237 else
238 return NULL;
241 #ifndef __ia64__
242 _Unwind_Ptr
243 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
245 return (_Unwind_Ptr) context->bases.dbase;
248 _Unwind_Ptr
249 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
251 return (_Unwind_Ptr) context->bases.tbase;
253 #endif
255 #ifdef MD_UNWIND_SUPPORT
256 #include MD_UNWIND_SUPPORT
257 #endif
259 /* Extract any interesting information from the CIE for the translation
260 unit F belongs to. Return a pointer to the byte after the augmentation,
261 or NULL if we encountered an undecipherable augmentation. */
263 static const unsigned char *
264 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
265 _Unwind_FrameState *fs)
267 const unsigned char *aug = cie->augmentation;
268 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
269 const unsigned char *ret = NULL;
270 _Unwind_Word utmp;
272 /* g++ v2 "eh" has pointer immediately following augmentation string,
273 so it must be handled first. */
274 if (aug[0] == 'e' && aug[1] == 'h')
276 fs->eh_ptr = read_pointer (p);
277 p += sizeof (void *);
278 aug += 2;
281 /* Immediately following the augmentation are the code and
282 data alignment and return address column. */
283 p = read_uleb128 (p, &fs->code_align);
284 p = read_sleb128 (p, &fs->data_align);
285 if (cie->version == 1)
286 fs->retaddr_column = *p++;
287 else
288 p = read_uleb128 (p, &fs->retaddr_column);
289 fs->lsda_encoding = DW_EH_PE_omit;
291 /* If the augmentation starts with 'z', then a uleb128 immediately
292 follows containing the length of the augmentation field following
293 the size. */
294 if (*aug == 'z')
296 p = read_uleb128 (p, &utmp);
297 ret = p + utmp;
299 fs->saw_z = 1;
300 ++aug;
303 /* Iterate over recognized augmentation subsequences. */
304 while (*aug != '\0')
306 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
307 if (aug[0] == 'L')
309 fs->lsda_encoding = *p++;
310 aug += 1;
313 /* "R" indicates a byte indicating how FDE addresses are encoded. */
314 else if (aug[0] == 'R')
316 fs->fde_encoding = *p++;
317 aug += 1;
320 /* "P" indicates a personality routine in the CIE augmentation. */
321 else if (aug[0] == 'P')
323 p = read_encoded_value (context, *p, p + 1,
324 (_Unwind_Ptr *) &fs->personality);
325 aug += 1;
328 /* Otherwise we have an unknown augmentation string.
329 Bail unless we saw a 'z' prefix. */
330 else
331 return ret;
334 return ret ? ret : p;
338 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
339 onto the stack to start. */
341 static _Unwind_Word
342 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
343 struct _Unwind_Context *context, _Unwind_Word initial)
345 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
346 int stack_elt;
348 stack[0] = initial;
349 stack_elt = 1;
351 while (op_ptr < op_end)
353 enum dwarf_location_atom op = *op_ptr++;
354 _Unwind_Word result, reg, utmp;
355 _Unwind_Sword offset, stmp;
357 switch (op)
359 case DW_OP_lit0:
360 case DW_OP_lit1:
361 case DW_OP_lit2:
362 case DW_OP_lit3:
363 case DW_OP_lit4:
364 case DW_OP_lit5:
365 case DW_OP_lit6:
366 case DW_OP_lit7:
367 case DW_OP_lit8:
368 case DW_OP_lit9:
369 case DW_OP_lit10:
370 case DW_OP_lit11:
371 case DW_OP_lit12:
372 case DW_OP_lit13:
373 case DW_OP_lit14:
374 case DW_OP_lit15:
375 case DW_OP_lit16:
376 case DW_OP_lit17:
377 case DW_OP_lit18:
378 case DW_OP_lit19:
379 case DW_OP_lit20:
380 case DW_OP_lit21:
381 case DW_OP_lit22:
382 case DW_OP_lit23:
383 case DW_OP_lit24:
384 case DW_OP_lit25:
385 case DW_OP_lit26:
386 case DW_OP_lit27:
387 case DW_OP_lit28:
388 case DW_OP_lit29:
389 case DW_OP_lit30:
390 case DW_OP_lit31:
391 result = op - DW_OP_lit0;
392 break;
394 case DW_OP_addr:
395 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
396 op_ptr += sizeof (void *);
397 break;
399 case DW_OP_const1u:
400 result = read_1u (op_ptr);
401 op_ptr += 1;
402 break;
403 case DW_OP_const1s:
404 result = read_1s (op_ptr);
405 op_ptr += 1;
406 break;
407 case DW_OP_const2u:
408 result = read_2u (op_ptr);
409 op_ptr += 2;
410 break;
411 case DW_OP_const2s:
412 result = read_2s (op_ptr);
413 op_ptr += 2;
414 break;
415 case DW_OP_const4u:
416 result = read_4u (op_ptr);
417 op_ptr += 4;
418 break;
419 case DW_OP_const4s:
420 result = read_4s (op_ptr);
421 op_ptr += 4;
422 break;
423 case DW_OP_const8u:
424 result = read_8u (op_ptr);
425 op_ptr += 8;
426 break;
427 case DW_OP_const8s:
428 result = read_8s (op_ptr);
429 op_ptr += 8;
430 break;
431 case DW_OP_constu:
432 op_ptr = read_uleb128 (op_ptr, &result);
433 break;
434 case DW_OP_consts:
435 op_ptr = read_sleb128 (op_ptr, &stmp);
436 result = stmp;
437 break;
439 case DW_OP_reg0:
440 case DW_OP_reg1:
441 case DW_OP_reg2:
442 case DW_OP_reg3:
443 case DW_OP_reg4:
444 case DW_OP_reg5:
445 case DW_OP_reg6:
446 case DW_OP_reg7:
447 case DW_OP_reg8:
448 case DW_OP_reg9:
449 case DW_OP_reg10:
450 case DW_OP_reg11:
451 case DW_OP_reg12:
452 case DW_OP_reg13:
453 case DW_OP_reg14:
454 case DW_OP_reg15:
455 case DW_OP_reg16:
456 case DW_OP_reg17:
457 case DW_OP_reg18:
458 case DW_OP_reg19:
459 case DW_OP_reg20:
460 case DW_OP_reg21:
461 case DW_OP_reg22:
462 case DW_OP_reg23:
463 case DW_OP_reg24:
464 case DW_OP_reg25:
465 case DW_OP_reg26:
466 case DW_OP_reg27:
467 case DW_OP_reg28:
468 case DW_OP_reg29:
469 case DW_OP_reg30:
470 case DW_OP_reg31:
471 result = _Unwind_GetGR (context, op - DW_OP_reg0);
472 break;
473 case DW_OP_regx:
474 op_ptr = read_uleb128 (op_ptr, &reg);
475 result = _Unwind_GetGR (context, reg);
476 break;
478 case DW_OP_breg0:
479 case DW_OP_breg1:
480 case DW_OP_breg2:
481 case DW_OP_breg3:
482 case DW_OP_breg4:
483 case DW_OP_breg5:
484 case DW_OP_breg6:
485 case DW_OP_breg7:
486 case DW_OP_breg8:
487 case DW_OP_breg9:
488 case DW_OP_breg10:
489 case DW_OP_breg11:
490 case DW_OP_breg12:
491 case DW_OP_breg13:
492 case DW_OP_breg14:
493 case DW_OP_breg15:
494 case DW_OP_breg16:
495 case DW_OP_breg17:
496 case DW_OP_breg18:
497 case DW_OP_breg19:
498 case DW_OP_breg20:
499 case DW_OP_breg21:
500 case DW_OP_breg22:
501 case DW_OP_breg23:
502 case DW_OP_breg24:
503 case DW_OP_breg25:
504 case DW_OP_breg26:
505 case DW_OP_breg27:
506 case DW_OP_breg28:
507 case DW_OP_breg29:
508 case DW_OP_breg30:
509 case DW_OP_breg31:
510 op_ptr = read_sleb128 (op_ptr, &offset);
511 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
512 break;
513 case DW_OP_bregx:
514 op_ptr = read_uleb128 (op_ptr, &reg);
515 op_ptr = read_sleb128 (op_ptr, &offset);
516 result = _Unwind_GetGR (context, reg) + offset;
517 break;
519 case DW_OP_dup:
520 gcc_assert (stack_elt);
521 result = stack[stack_elt - 1];
522 break;
524 case DW_OP_drop:
525 gcc_assert (stack_elt);
526 stack_elt -= 1;
527 goto no_push;
529 case DW_OP_pick:
530 offset = *op_ptr++;
531 gcc_assert (offset < stack_elt - 1);
532 result = stack[stack_elt - 1 - offset];
533 break;
535 case DW_OP_over:
536 gcc_assert (stack_elt >= 2);
537 result = stack[stack_elt - 2];
538 break;
540 case DW_OP_rot:
542 _Unwind_Word t1, t2, t3;
544 gcc_assert (stack_elt >= 3);
545 t1 = stack[stack_elt - 1];
546 t2 = stack[stack_elt - 2];
547 t3 = stack[stack_elt - 3];
548 stack[stack_elt - 1] = t2;
549 stack[stack_elt - 2] = t3;
550 stack[stack_elt - 3] = t1;
551 goto no_push;
554 case DW_OP_deref:
555 case DW_OP_deref_size:
556 case DW_OP_abs:
557 case DW_OP_neg:
558 case DW_OP_not:
559 case DW_OP_plus_uconst:
560 /* Unary operations. */
561 gcc_assert (stack_elt);
562 stack_elt -= 1;
564 result = stack[stack_elt];
566 switch (op)
568 case DW_OP_deref:
570 void *ptr = (void *) (_Unwind_Ptr) result;
571 result = (_Unwind_Ptr) read_pointer (ptr);
573 break;
575 case DW_OP_deref_size:
577 void *ptr = (void *) (_Unwind_Ptr) result;
578 switch (*op_ptr++)
580 case 1:
581 result = read_1u (ptr);
582 break;
583 case 2:
584 result = read_2u (ptr);
585 break;
586 case 4:
587 result = read_4u (ptr);
588 break;
589 case 8:
590 result = read_8u (ptr);
591 break;
592 default:
593 gcc_unreachable ();
596 break;
598 case DW_OP_abs:
599 if ((_Unwind_Sword) result < 0)
600 result = -result;
601 break;
602 case DW_OP_neg:
603 result = -result;
604 break;
605 case DW_OP_not:
606 result = ~result;
607 break;
608 case DW_OP_plus_uconst:
609 op_ptr = read_uleb128 (op_ptr, &utmp);
610 result += utmp;
611 break;
613 default:
614 gcc_unreachable ();
616 break;
618 case DW_OP_and:
619 case DW_OP_div:
620 case DW_OP_minus:
621 case DW_OP_mod:
622 case DW_OP_mul:
623 case DW_OP_or:
624 case DW_OP_plus:
625 case DW_OP_shl:
626 case DW_OP_shr:
627 case DW_OP_shra:
628 case DW_OP_xor:
629 case DW_OP_le:
630 case DW_OP_ge:
631 case DW_OP_eq:
632 case DW_OP_lt:
633 case DW_OP_gt:
634 case DW_OP_ne:
636 /* Binary operations. */
637 _Unwind_Word first, second;
638 gcc_assert (stack_elt >= 2);
639 stack_elt -= 2;
641 second = stack[stack_elt];
642 first = stack[stack_elt + 1];
644 switch (op)
646 case DW_OP_and:
647 result = second & first;
648 break;
649 case DW_OP_div:
650 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
651 break;
652 case DW_OP_minus:
653 result = second - first;
654 break;
655 case DW_OP_mod:
656 result = (_Unwind_Sword) second % (_Unwind_Sword) first;
657 break;
658 case DW_OP_mul:
659 result = second * first;
660 break;
661 case DW_OP_or:
662 result = second | first;
663 break;
664 case DW_OP_plus:
665 result = second + first;
666 break;
667 case DW_OP_shl:
668 result = second << first;
669 break;
670 case DW_OP_shr:
671 result = second >> first;
672 break;
673 case DW_OP_shra:
674 result = (_Unwind_Sword) second >> first;
675 break;
676 case DW_OP_xor:
677 result = second ^ first;
678 break;
679 case DW_OP_le:
680 result = (_Unwind_Sword) first <= (_Unwind_Sword) second;
681 break;
682 case DW_OP_ge:
683 result = (_Unwind_Sword) first >= (_Unwind_Sword) second;
684 break;
685 case DW_OP_eq:
686 result = (_Unwind_Sword) first == (_Unwind_Sword) second;
687 break;
688 case DW_OP_lt:
689 result = (_Unwind_Sword) first < (_Unwind_Sword) second;
690 break;
691 case DW_OP_gt:
692 result = (_Unwind_Sword) first > (_Unwind_Sword) second;
693 break;
694 case DW_OP_ne:
695 result = (_Unwind_Sword) first != (_Unwind_Sword) second;
696 break;
698 default:
699 gcc_unreachable ();
702 break;
704 case DW_OP_skip:
705 offset = read_2s (op_ptr);
706 op_ptr += 2;
707 op_ptr += offset;
708 goto no_push;
710 case DW_OP_bra:
711 gcc_assert (stack_elt);
712 stack_elt -= 1;
714 offset = read_2s (op_ptr);
715 op_ptr += 2;
716 if (stack[stack_elt] != 0)
717 op_ptr += offset;
718 goto no_push;
720 case DW_OP_nop:
721 goto no_push;
723 default:
724 gcc_unreachable ();
727 /* Most things push a result value. */
728 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
729 stack[stack_elt++] = result;
730 no_push:;
733 /* We were executing this program to get a value. It should be
734 at top of stack. */
735 gcc_assert (stack_elt);
736 stack_elt -= 1;
737 return stack[stack_elt];
741 /* Decode DWARF 2 call frame information. Takes pointers the
742 instruction sequence to decode, current register information and
743 CIE info, and the PC range to evaluate. */
745 static void
746 execute_cfa_program (const unsigned char *insn_ptr,
747 const unsigned char *insn_end,
748 struct _Unwind_Context *context,
749 _Unwind_FrameState *fs)
751 struct frame_state_reg_info *unused_rs = NULL;
753 /* Don't allow remember/restore between CIE and FDE programs. */
754 fs->regs.prev = NULL;
756 /* The comparison with the return address uses < rather than <= because
757 we are only interested in the effects of code before the call; for a
758 noreturn function, the return address may point to unrelated code with
759 a different stack configuration that we are not interested in. We
760 assume that the call itself is unwind info-neutral; if not, or if
761 there are delay instructions that adjust the stack, these must be
762 reflected at the point immediately before the call insn. */
763 while (insn_ptr < insn_end && fs->pc < context->ra)
765 unsigned char insn = *insn_ptr++;
766 _Unwind_Word reg, utmp;
767 _Unwind_Sword offset, stmp;
769 if ((insn & 0xc0) == DW_CFA_advance_loc)
770 fs->pc += (insn & 0x3f) * fs->code_align;
771 else if ((insn & 0xc0) == DW_CFA_offset)
773 reg = insn & 0x3f;
774 insn_ptr = read_uleb128 (insn_ptr, &utmp);
775 offset = (_Unwind_Sword) utmp * fs->data_align;
776 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
777 = REG_SAVED_OFFSET;
778 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
780 else if ((insn & 0xc0) == DW_CFA_restore)
782 reg = insn & 0x3f;
783 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
785 else switch (insn)
787 case DW_CFA_set_loc:
788 insn_ptr = read_encoded_value (context, fs->fde_encoding,
789 insn_ptr, (_Unwind_Ptr *) &fs->pc);
790 break;
792 case DW_CFA_advance_loc1:
793 fs->pc += read_1u (insn_ptr) * fs->code_align;
794 insn_ptr += 1;
795 break;
796 case DW_CFA_advance_loc2:
797 fs->pc += read_2u (insn_ptr) * fs->code_align;
798 insn_ptr += 2;
799 break;
800 case DW_CFA_advance_loc4:
801 fs->pc += read_4u (insn_ptr) * fs->code_align;
802 insn_ptr += 4;
803 break;
805 case DW_CFA_offset_extended:
806 insn_ptr = read_uleb128 (insn_ptr, &reg);
807 insn_ptr = read_uleb128 (insn_ptr, &utmp);
808 offset = (_Unwind_Sword) utmp * fs->data_align;
809 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
810 = REG_SAVED_OFFSET;
811 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
812 break;
814 case DW_CFA_restore_extended:
815 insn_ptr = read_uleb128 (insn_ptr, &reg);
816 /* FIXME, this is wrong; the CIE might have said that the
817 register was saved somewhere. */
818 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
819 break;
821 case DW_CFA_undefined:
822 case DW_CFA_same_value:
823 insn_ptr = read_uleb128 (insn_ptr, &reg);
824 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
825 break;
827 case DW_CFA_nop:
828 break;
830 case DW_CFA_register:
832 _Unwind_Word reg2;
833 insn_ptr = read_uleb128 (insn_ptr, &reg);
834 insn_ptr = read_uleb128 (insn_ptr, &reg2);
835 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
836 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg = reg2;
838 break;
840 case DW_CFA_remember_state:
842 struct frame_state_reg_info *new_rs;
843 if (unused_rs)
845 new_rs = unused_rs;
846 unused_rs = unused_rs->prev;
848 else
849 new_rs = alloca (sizeof (struct frame_state_reg_info));
851 *new_rs = fs->regs;
852 fs->regs.prev = new_rs;
854 break;
856 case DW_CFA_restore_state:
858 struct frame_state_reg_info *old_rs = fs->regs.prev;
859 fs->regs = *old_rs;
860 old_rs->prev = unused_rs;
861 unused_rs = old_rs;
863 break;
865 case DW_CFA_def_cfa:
866 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
867 insn_ptr = read_uleb128 (insn_ptr, &utmp);
868 fs->cfa_offset = utmp;
869 fs->cfa_how = CFA_REG_OFFSET;
870 break;
872 case DW_CFA_def_cfa_register:
873 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
874 fs->cfa_how = CFA_REG_OFFSET;
875 break;
877 case DW_CFA_def_cfa_offset:
878 insn_ptr = read_uleb128 (insn_ptr, &utmp);
879 fs->cfa_offset = utmp;
880 /* cfa_how deliberately not set. */
881 break;
883 case DW_CFA_def_cfa_expression:
884 fs->cfa_exp = insn_ptr;
885 fs->cfa_how = CFA_EXP;
886 insn_ptr = read_uleb128 (insn_ptr, &utmp);
887 insn_ptr += utmp;
888 break;
890 case DW_CFA_expression:
891 insn_ptr = read_uleb128 (insn_ptr, &reg);
892 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
893 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
894 insn_ptr = read_uleb128 (insn_ptr, &utmp);
895 insn_ptr += utmp;
896 break;
898 /* From the 2.1 draft. */
899 case DW_CFA_offset_extended_sf:
900 insn_ptr = read_uleb128 (insn_ptr, &reg);
901 insn_ptr = read_sleb128 (insn_ptr, &stmp);
902 offset = stmp * fs->data_align;
903 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
904 = REG_SAVED_OFFSET;
905 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
906 break;
908 case DW_CFA_def_cfa_sf:
909 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
910 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
911 fs->cfa_how = CFA_REG_OFFSET;
912 break;
914 case DW_CFA_def_cfa_offset_sf:
915 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
916 /* cfa_how deliberately not set. */
917 break;
919 case DW_CFA_GNU_window_save:
920 /* ??? Hardcoded for SPARC register window configuration. */
921 for (reg = 16; reg < 32; ++reg)
923 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
924 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
926 break;
928 case DW_CFA_GNU_args_size:
929 insn_ptr = read_uleb128 (insn_ptr, &context->args_size);
930 break;
932 case DW_CFA_GNU_negative_offset_extended:
933 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
934 older PowerPC code. */
935 insn_ptr = read_uleb128 (insn_ptr, &reg);
936 insn_ptr = read_uleb128 (insn_ptr, &utmp);
937 offset = (_Unwind_Word) utmp * fs->data_align;
938 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
939 = REG_SAVED_OFFSET;
940 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
941 break;
943 default:
944 gcc_unreachable ();
949 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
950 its caller and decode it into FS. This function also sets the
951 args_size and lsda members of CONTEXT, as they are really information
952 about the caller's frame. */
954 static _Unwind_Reason_Code
955 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
957 const struct dwarf_fde *fde;
958 const struct dwarf_cie *cie;
959 const unsigned char *aug, *insn, *end;
961 memset (fs, 0, sizeof (*fs));
962 context->args_size = 0;
963 context->lsda = 0;
965 if (context->ra == 0)
966 return _URC_END_OF_STACK;
968 fde = _Unwind_Find_FDE (context->ra - 1, &context->bases);
969 if (fde == NULL)
971 #ifdef MD_FALLBACK_FRAME_STATE_FOR
972 /* Couldn't find frame unwind info for this function. Try a
973 target-specific fallback mechanism. This will necessarily
974 not provide a personality routine or LSDA. */
975 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
976 #else
977 return _URC_END_OF_STACK;
978 #endif
981 fs->pc = context->bases.func;
983 cie = get_cie (fde);
984 insn = extract_cie_info (cie, context, fs);
985 if (insn == NULL)
986 /* CIE contained unknown augmentation. */
987 return _URC_FATAL_PHASE1_ERROR;
989 /* First decode all the insns in the CIE. */
990 end = (unsigned char *) next_fde ((struct dwarf_fde *) cie);
991 execute_cfa_program (insn, end, context, fs);
993 /* Locate augmentation for the fde. */
994 aug = (unsigned char *) fde + sizeof (*fde);
995 aug += 2 * size_of_encoded_value (fs->fde_encoding);
996 insn = NULL;
997 if (fs->saw_z)
999 _Unwind_Word i;
1000 aug = read_uleb128 (aug, &i);
1001 insn = aug + i;
1003 if (fs->lsda_encoding != DW_EH_PE_omit)
1004 aug = read_encoded_value (context, fs->lsda_encoding, aug,
1005 (_Unwind_Ptr *) &context->lsda);
1007 /* Then the insns in the FDE up to our target PC. */
1008 if (insn == NULL)
1009 insn = aug;
1010 end = (unsigned char *) next_fde (fde);
1011 execute_cfa_program (insn, end, context, fs);
1013 return _URC_NO_REASON;
1016 typedef struct frame_state
1018 void *cfa;
1019 void *eh_ptr;
1020 long cfa_offset;
1021 long args_size;
1022 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1023 unsigned short cfa_reg;
1024 unsigned short retaddr_column;
1025 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1026 } frame_state;
1028 struct frame_state * __frame_state_for (void *, struct frame_state *);
1030 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1031 a given PC_TARGET. The caller should allocate a local variable of
1032 `struct frame_state' and pass its address to STATE_IN. */
1034 struct frame_state *
1035 __frame_state_for (void *pc_target, struct frame_state *state_in)
1037 struct _Unwind_Context context;
1038 _Unwind_FrameState fs;
1039 int reg;
1041 memset (&context, 0, sizeof (struct _Unwind_Context));
1042 context.ra = pc_target + 1;
1044 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1045 return 0;
1047 /* We have no way to pass a location expression for the CFA to our
1048 caller. It wouldn't understand it anyway. */
1049 if (fs.cfa_how == CFA_EXP)
1050 return 0;
1052 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1054 state_in->saved[reg] = fs.regs.reg[reg].how;
1055 switch (state_in->saved[reg])
1057 case REG_SAVED_REG:
1058 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1059 break;
1060 case REG_SAVED_OFFSET:
1061 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1062 break;
1063 default:
1064 state_in->reg_or_offset[reg] = 0;
1065 break;
1069 state_in->cfa_offset = fs.cfa_offset;
1070 state_in->cfa_reg = fs.cfa_reg;
1071 state_in->retaddr_column = fs.retaddr_column;
1072 state_in->args_size = context.args_size;
1073 state_in->eh_ptr = fs.eh_ptr;
1075 return state_in;
1078 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1080 static inline void
1081 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1082 _Unwind_SpTmp *tmp_sp)
1084 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1086 if (size == sizeof(_Unwind_Ptr))
1087 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1088 else
1090 gcc_assert (size == sizeof(_Unwind_Word));
1091 tmp_sp->word = (_Unwind_Ptr) cfa;
1093 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1096 static void
1097 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1099 struct _Unwind_Context orig_context = *context;
1100 void *cfa;
1101 long i;
1103 #ifdef EH_RETURN_STACKADJ_RTX
1104 /* Special handling here: Many machines do not use a frame pointer,
1105 and track the CFA only through offsets from the stack pointer from
1106 one frame to the next. In this case, the stack pointer is never
1107 stored, so it has no saved address in the context. What we do
1108 have is the CFA from the previous stack frame.
1110 In very special situations (such as unwind info for signal return),
1111 there may be location expressions that use the stack pointer as well.
1113 Do this conditionally for one frame. This allows the unwind info
1114 for one frame to save a copy of the stack pointer from the previous
1115 frame, and be able to use much easier CFA mechanisms to do it.
1116 Always zap the saved stack pointer value for the next frame; carrying
1117 the value over from one frame to another doesn't make sense. */
1119 _Unwind_SpTmp tmp_sp;
1121 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1122 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1123 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1124 #endif
1126 /* Compute this frame's CFA. */
1127 switch (fs->cfa_how)
1129 case CFA_REG_OFFSET:
1130 cfa = _Unwind_GetPtr (&orig_context, fs->cfa_reg);
1131 cfa += fs->cfa_offset;
1132 break;
1134 case CFA_EXP:
1136 const unsigned char *exp = fs->cfa_exp;
1137 _Unwind_Word len;
1139 exp = read_uleb128 (exp, &len);
1140 cfa = (void *) (_Unwind_Ptr)
1141 execute_stack_op (exp, exp + len, &orig_context, 0);
1142 break;
1145 default:
1146 gcc_unreachable ();
1148 context->cfa = cfa;
1150 /* Compute the addresses of all registers saved in this frame. */
1151 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1152 switch (fs->regs.reg[i].how)
1154 case REG_UNSAVED:
1155 break;
1157 case REG_SAVED_OFFSET:
1158 _Unwind_SetGRPtr (context, i,
1159 (void *) (cfa + fs->regs.reg[i].loc.offset));
1160 break;
1162 case REG_SAVED_REG:
1163 _Unwind_SetGRPtr
1164 (context, i,
1165 _Unwind_GetGRPtr (&orig_context, fs->regs.reg[i].loc.reg));
1166 break;
1168 case REG_SAVED_EXP:
1170 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1171 _Unwind_Word len;
1172 _Unwind_Ptr val;
1174 exp = read_uleb128 (exp, &len);
1175 val = execute_stack_op (exp, exp + len, &orig_context,
1176 (_Unwind_Ptr) cfa);
1177 _Unwind_SetGRPtr (context, i, (void *) val);
1179 break;
1182 #ifdef MD_FROB_UPDATE_CONTEXT
1183 MD_FROB_UPDATE_CONTEXT (context, fs);
1184 #endif
1187 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1188 of its caller. Update CONTEXT to refer to the caller as well. Note
1189 that the args_size and lsda members are not updated here, but later in
1190 uw_frame_state_for. */
1192 static void
1193 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1195 uw_update_context_1 (context, fs);
1197 /* Compute the return address now, since the return address column
1198 can change from frame to frame. */
1199 context->ra = __builtin_extract_return_addr
1200 (_Unwind_GetPtr (context, fs->retaddr_column));
1203 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1204 level will be the return address and the CFA. */
1206 #define uw_init_context(CONTEXT) \
1207 do \
1209 /* Do any necessary initialization to access arbitrary stack frames. \
1210 On the SPARC, this means flushing the register windows. */ \
1211 __builtin_unwind_init (); \
1212 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1213 __builtin_return_address (0)); \
1215 while (0)
1217 static inline void
1218 init_dwarf_reg_size_table (void)
1220 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1223 static void
1224 uw_init_context_1 (struct _Unwind_Context *context,
1225 void *outer_cfa, void *outer_ra)
1227 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1228 _Unwind_FrameState fs;
1229 _Unwind_SpTmp sp_slot;
1230 _Unwind_Reason_Code code;
1232 memset (context, 0, sizeof (struct _Unwind_Context));
1233 context->ra = ra;
1235 code = uw_frame_state_for (context, &fs);
1236 gcc_assert (code == _URC_NO_REASON);
1238 #if __GTHREADS
1240 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1241 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1242 || dwarf_reg_size_table[0] == 0)
1243 init_dwarf_reg_size_table ();
1245 #else
1246 if (dwarf_reg_size_table[0] == 0)
1247 init_dwarf_reg_size_table ();
1248 #endif
1250 /* Force the frame state to use the known cfa value. */
1251 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1252 fs.cfa_how = CFA_REG_OFFSET;
1253 fs.cfa_reg = __builtin_dwarf_sp_column ();
1254 fs.cfa_offset = 0;
1256 uw_update_context_1 (context, &fs);
1258 /* If the return address column was saved in a register in the
1259 initialization context, then we can't see it in the given
1260 call frame data. So have the initialization context tell us. */
1261 context->ra = __builtin_extract_return_addr (outer_ra);
1265 /* Install TARGET into CURRENT so that we can return to it. This is a
1266 macro because __builtin_eh_return must be invoked in the context of
1267 our caller. */
1269 #define uw_install_context(CURRENT, TARGET) \
1270 do \
1272 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1273 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1274 __builtin_eh_return (offset, handler); \
1276 while (0)
1278 static long
1279 uw_install_context_1 (struct _Unwind_Context *current,
1280 struct _Unwind_Context *target)
1282 long i;
1283 _Unwind_SpTmp sp_slot;
1285 /* If the target frame does not have a saved stack pointer,
1286 then set up the target's CFA. */
1287 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1288 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1290 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1292 void *c = current->reg[i];
1293 void *t = target->reg[i];
1295 if (t && c && t != c)
1296 memcpy (c, t, dwarf_reg_size_table[i]);
1299 /* If the current frame doesn't have a saved stack pointer, then we
1300 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1301 pointer value reloaded. */
1302 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1304 void *target_cfa;
1306 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1308 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1309 if (STACK_GROWS_DOWNWARD)
1310 return target_cfa - current->cfa + target->args_size;
1311 else
1312 return current->cfa - target_cfa - target->args_size;
1314 return 0;
1317 static inline _Unwind_Ptr
1318 uw_identify_context (struct _Unwind_Context *context)
1320 return _Unwind_GetIP (context);
1324 #include "unwind.inc"
1326 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1327 alias (_Unwind_Backtrace);
1328 alias (_Unwind_DeleteException);
1329 alias (_Unwind_FindEnclosingFunction);
1330 alias (_Unwind_ForcedUnwind);
1331 alias (_Unwind_GetDataRelBase);
1332 alias (_Unwind_GetTextRelBase);
1333 alias (_Unwind_GetCFA);
1334 alias (_Unwind_GetGR);
1335 alias (_Unwind_GetIP);
1336 alias (_Unwind_GetLanguageSpecificData);
1337 alias (_Unwind_GetRegionStart);
1338 alias (_Unwind_RaiseException);
1339 alias (_Unwind_Resume);
1340 alias (_Unwind_Resume_or_Rethrow);
1341 alias (_Unwind_SetGR);
1342 alias (_Unwind_SetIP);
1343 #endif
1345 #endif /* !USING_SJLJ_EXCEPTIONS */