* final.c (output_asm_insn): Correct problem with -fverbose-asm.
[official-gcc.git] / gcc / unwind-dw2.c
blobeb36e207e2ff9068c744942595f9962c078e191f
1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 index = DWARF_REG_TO_UNWIND_COLUMN (index);
129 if (index >= (int) sizeof(dwarf_reg_size_table))
130 abort ();
131 size = dwarf_reg_size_table[index];
132 ptr = context->reg[index];
134 /* This will segfault if the register hasn't been saved. */
135 if (size == sizeof(_Unwind_Ptr))
136 return * (_Unwind_Ptr *) ptr;
138 if (size == sizeof(_Unwind_Word))
139 return * (_Unwind_Word *) ptr;
141 abort ();
144 static inline void *
145 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
147 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
150 /* Get the value of the CFA as saved in CONTEXT. */
152 _Unwind_Word
153 _Unwind_GetCFA (struct _Unwind_Context *context)
155 return (_Unwind_Ptr) context->cfa;
158 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
160 inline void
161 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
163 int size;
164 void *ptr;
166 index = DWARF_REG_TO_UNWIND_COLUMN (index);
167 if (index >= (int) sizeof(dwarf_reg_size_table))
168 abort ();
169 size = dwarf_reg_size_table[index];
170 ptr = context->reg[index];
172 if (size == sizeof(_Unwind_Ptr))
173 * (_Unwind_Ptr *) ptr = val;
174 else if (size == sizeof(_Unwind_Word))
175 * (_Unwind_Word *) ptr = val;
176 else
177 abort ();
180 /* Get the pointer to a register INDEX as saved in CONTEXT. */
182 static inline void *
183 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
185 index = DWARF_REG_TO_UNWIND_COLUMN (index);
186 return context->reg[index];
189 /* Set the pointer to a register INDEX as saved in CONTEXT. */
191 static inline void
192 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
194 index = DWARF_REG_TO_UNWIND_COLUMN (index);
195 context->reg[index] = p;
198 /* Retrieve the return address for CONTEXT. */
200 inline _Unwind_Ptr
201 _Unwind_GetIP (struct _Unwind_Context *context)
203 return (_Unwind_Ptr) context->ra;
206 /* Overwrite the return address for CONTEXT with VAL. */
208 inline void
209 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
211 context->ra = (void *) val;
214 void *
215 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
217 return context->lsda;
220 _Unwind_Ptr
221 _Unwind_GetRegionStart (struct _Unwind_Context *context)
223 return (_Unwind_Ptr) context->bases.func;
226 void *
227 _Unwind_FindEnclosingFunction (void *pc)
229 struct dwarf_eh_bases bases;
230 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
231 if (fde)
232 return bases.func;
233 else
234 return NULL;
237 #ifndef __ia64__
238 _Unwind_Ptr
239 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
241 return (_Unwind_Ptr) context->bases.dbase;
244 _Unwind_Ptr
245 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
247 return (_Unwind_Ptr) context->bases.tbase;
249 #endif
251 #ifdef MD_UNWIND_SUPPORT
252 #include MD_UNWIND_SUPPORT
253 #endif
255 /* Extract any interesting information from the CIE for the translation
256 unit F belongs to. Return a pointer to the byte after the augmentation,
257 or NULL if we encountered an undecipherable augmentation. */
259 static const unsigned char *
260 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
261 _Unwind_FrameState *fs)
263 const unsigned char *aug = cie->augmentation;
264 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
265 const unsigned char *ret = NULL;
266 _Unwind_Word utmp;
268 /* g++ v2 "eh" has pointer immediately following augmentation string,
269 so it must be handled first. */
270 if (aug[0] == 'e' && aug[1] == 'h')
272 fs->eh_ptr = read_pointer (p);
273 p += sizeof (void *);
274 aug += 2;
277 /* Immediately following the augmentation are the code and
278 data alignment and return address column. */
279 p = read_uleb128 (p, &fs->code_align);
280 p = read_sleb128 (p, &fs->data_align);
281 if (cie->version == 1)
282 fs->retaddr_column = *p++;
283 else
284 p = read_uleb128 (p, &fs->retaddr_column);
285 fs->lsda_encoding = DW_EH_PE_omit;
287 /* If the augmentation starts with 'z', then a uleb128 immediately
288 follows containing the length of the augmentation field following
289 the size. */
290 if (*aug == 'z')
292 p = read_uleb128 (p, &utmp);
293 ret = p + utmp;
295 fs->saw_z = 1;
296 ++aug;
299 /* Iterate over recognized augmentation subsequences. */
300 while (*aug != '\0')
302 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
303 if (aug[0] == 'L')
305 fs->lsda_encoding = *p++;
306 aug += 1;
309 /* "R" indicates a byte indicating how FDE addresses are encoded. */
310 else if (aug[0] == 'R')
312 fs->fde_encoding = *p++;
313 aug += 1;
316 /* "P" indicates a personality routine in the CIE augmentation. */
317 else if (aug[0] == 'P')
319 p = read_encoded_value (context, *p, p + 1,
320 (_Unwind_Ptr *) &fs->personality);
321 aug += 1;
324 /* Otherwise we have an unknown augmentation string.
325 Bail unless we saw a 'z' prefix. */
326 else
327 return ret;
330 return ret ? ret : p;
334 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
335 onto the stack to start. */
337 static _Unwind_Word
338 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
339 struct _Unwind_Context *context, _Unwind_Word initial)
341 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
342 int stack_elt;
344 stack[0] = initial;
345 stack_elt = 1;
347 while (op_ptr < op_end)
349 enum dwarf_location_atom op = *op_ptr++;
350 _Unwind_Word result, reg, utmp;
351 _Unwind_Sword offset, stmp;
353 switch (op)
355 case DW_OP_lit0:
356 case DW_OP_lit1:
357 case DW_OP_lit2:
358 case DW_OP_lit3:
359 case DW_OP_lit4:
360 case DW_OP_lit5:
361 case DW_OP_lit6:
362 case DW_OP_lit7:
363 case DW_OP_lit8:
364 case DW_OP_lit9:
365 case DW_OP_lit10:
366 case DW_OP_lit11:
367 case DW_OP_lit12:
368 case DW_OP_lit13:
369 case DW_OP_lit14:
370 case DW_OP_lit15:
371 case DW_OP_lit16:
372 case DW_OP_lit17:
373 case DW_OP_lit18:
374 case DW_OP_lit19:
375 case DW_OP_lit20:
376 case DW_OP_lit21:
377 case DW_OP_lit22:
378 case DW_OP_lit23:
379 case DW_OP_lit24:
380 case DW_OP_lit25:
381 case DW_OP_lit26:
382 case DW_OP_lit27:
383 case DW_OP_lit28:
384 case DW_OP_lit29:
385 case DW_OP_lit30:
386 case DW_OP_lit31:
387 result = op - DW_OP_lit0;
388 break;
390 case DW_OP_addr:
391 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
392 op_ptr += sizeof (void *);
393 break;
395 case DW_OP_const1u:
396 result = read_1u (op_ptr);
397 op_ptr += 1;
398 break;
399 case DW_OP_const1s:
400 result = read_1s (op_ptr);
401 op_ptr += 1;
402 break;
403 case DW_OP_const2u:
404 result = read_2u (op_ptr);
405 op_ptr += 2;
406 break;
407 case DW_OP_const2s:
408 result = read_2s (op_ptr);
409 op_ptr += 2;
410 break;
411 case DW_OP_const4u:
412 result = read_4u (op_ptr);
413 op_ptr += 4;
414 break;
415 case DW_OP_const4s:
416 result = read_4s (op_ptr);
417 op_ptr += 4;
418 break;
419 case DW_OP_const8u:
420 result = read_8u (op_ptr);
421 op_ptr += 8;
422 break;
423 case DW_OP_const8s:
424 result = read_8s (op_ptr);
425 op_ptr += 8;
426 break;
427 case DW_OP_constu:
428 op_ptr = read_uleb128 (op_ptr, &result);
429 break;
430 case DW_OP_consts:
431 op_ptr = read_sleb128 (op_ptr, &stmp);
432 result = stmp;
433 break;
435 case DW_OP_reg0:
436 case DW_OP_reg1:
437 case DW_OP_reg2:
438 case DW_OP_reg3:
439 case DW_OP_reg4:
440 case DW_OP_reg5:
441 case DW_OP_reg6:
442 case DW_OP_reg7:
443 case DW_OP_reg8:
444 case DW_OP_reg9:
445 case DW_OP_reg10:
446 case DW_OP_reg11:
447 case DW_OP_reg12:
448 case DW_OP_reg13:
449 case DW_OP_reg14:
450 case DW_OP_reg15:
451 case DW_OP_reg16:
452 case DW_OP_reg17:
453 case DW_OP_reg18:
454 case DW_OP_reg19:
455 case DW_OP_reg20:
456 case DW_OP_reg21:
457 case DW_OP_reg22:
458 case DW_OP_reg23:
459 case DW_OP_reg24:
460 case DW_OP_reg25:
461 case DW_OP_reg26:
462 case DW_OP_reg27:
463 case DW_OP_reg28:
464 case DW_OP_reg29:
465 case DW_OP_reg30:
466 case DW_OP_reg31:
467 result = _Unwind_GetGR (context, op - DW_OP_reg0);
468 break;
469 case DW_OP_regx:
470 op_ptr = read_uleb128 (op_ptr, &reg);
471 result = _Unwind_GetGR (context, reg);
472 break;
474 case DW_OP_breg0:
475 case DW_OP_breg1:
476 case DW_OP_breg2:
477 case DW_OP_breg3:
478 case DW_OP_breg4:
479 case DW_OP_breg5:
480 case DW_OP_breg6:
481 case DW_OP_breg7:
482 case DW_OP_breg8:
483 case DW_OP_breg9:
484 case DW_OP_breg10:
485 case DW_OP_breg11:
486 case DW_OP_breg12:
487 case DW_OP_breg13:
488 case DW_OP_breg14:
489 case DW_OP_breg15:
490 case DW_OP_breg16:
491 case DW_OP_breg17:
492 case DW_OP_breg18:
493 case DW_OP_breg19:
494 case DW_OP_breg20:
495 case DW_OP_breg21:
496 case DW_OP_breg22:
497 case DW_OP_breg23:
498 case DW_OP_breg24:
499 case DW_OP_breg25:
500 case DW_OP_breg26:
501 case DW_OP_breg27:
502 case DW_OP_breg28:
503 case DW_OP_breg29:
504 case DW_OP_breg30:
505 case DW_OP_breg31:
506 op_ptr = read_sleb128 (op_ptr, &offset);
507 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
508 break;
509 case DW_OP_bregx:
510 op_ptr = read_uleb128 (op_ptr, &reg);
511 op_ptr = read_sleb128 (op_ptr, &offset);
512 result = _Unwind_GetGR (context, reg) + offset;
513 break;
515 case DW_OP_dup:
516 if (stack_elt < 1)
517 abort ();
518 result = stack[stack_elt - 1];
519 break;
521 case DW_OP_drop:
522 if (--stack_elt < 0)
523 abort ();
524 goto no_push;
526 case DW_OP_pick:
527 offset = *op_ptr++;
528 if (offset >= stack_elt - 1)
529 abort ();
530 result = stack[stack_elt - 1 - offset];
531 break;
533 case DW_OP_over:
534 if (stack_elt < 2)
535 abort ();
536 result = stack[stack_elt - 2];
537 break;
539 case DW_OP_rot:
541 _Unwind_Word t1, t2, t3;
543 if (stack_elt < 3)
544 abort ();
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 if (--stack_elt < 0)
562 abort ();
563 result = stack[stack_elt];
565 switch (op)
567 case DW_OP_deref:
569 void *ptr = (void *) (_Unwind_Ptr) result;
570 result = (_Unwind_Ptr) read_pointer (ptr);
572 break;
574 case DW_OP_deref_size:
576 void *ptr = (void *) (_Unwind_Ptr) result;
577 switch (*op_ptr++)
579 case 1:
580 result = read_1u (ptr);
581 break;
582 case 2:
583 result = read_2u (ptr);
584 break;
585 case 4:
586 result = read_4u (ptr);
587 break;
588 case 8:
589 result = read_8u (ptr);
590 break;
591 default:
592 abort ();
595 break;
597 case DW_OP_abs:
598 if ((_Unwind_Sword) result < 0)
599 result = -result;
600 break;
601 case DW_OP_neg:
602 result = -result;
603 break;
604 case DW_OP_not:
605 result = ~result;
606 break;
607 case DW_OP_plus_uconst:
608 op_ptr = read_uleb128 (op_ptr, &utmp);
609 result += utmp;
610 break;
612 default:
613 abort ();
615 break;
617 case DW_OP_and:
618 case DW_OP_div:
619 case DW_OP_minus:
620 case DW_OP_mod:
621 case DW_OP_mul:
622 case DW_OP_or:
623 case DW_OP_plus:
624 case DW_OP_le:
625 case DW_OP_ge:
626 case DW_OP_eq:
627 case DW_OP_lt:
628 case DW_OP_gt:
629 case DW_OP_ne:
631 /* Binary operations. */
632 _Unwind_Word first, second;
633 if ((stack_elt -= 2) < 0)
634 abort ();
635 second = stack[stack_elt];
636 first = stack[stack_elt + 1];
638 switch (op)
640 case DW_OP_and:
641 result = second & first;
642 break;
643 case DW_OP_div:
644 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
645 break;
646 case DW_OP_minus:
647 result = second - first;
648 break;
649 case DW_OP_mod:
650 result = (_Unwind_Sword) second % (_Unwind_Sword) first;
651 break;
652 case DW_OP_mul:
653 result = second * first;
654 break;
655 case DW_OP_or:
656 result = second | first;
657 break;
658 case DW_OP_plus:
659 result = second + first;
660 break;
661 case DW_OP_shl:
662 result = second << first;
663 break;
664 case DW_OP_shr:
665 result = second >> first;
666 break;
667 case DW_OP_shra:
668 result = (_Unwind_Sword) second >> first;
669 break;
670 case DW_OP_xor:
671 result = second ^ first;
672 break;
673 case DW_OP_le:
674 result = (_Unwind_Sword) first <= (_Unwind_Sword) second;
675 break;
676 case DW_OP_ge:
677 result = (_Unwind_Sword) first >= (_Unwind_Sword) second;
678 break;
679 case DW_OP_eq:
680 result = (_Unwind_Sword) first == (_Unwind_Sword) second;
681 break;
682 case DW_OP_lt:
683 result = (_Unwind_Sword) first < (_Unwind_Sword) second;
684 break;
685 case DW_OP_gt:
686 result = (_Unwind_Sword) first > (_Unwind_Sword) second;
687 break;
688 case DW_OP_ne:
689 result = (_Unwind_Sword) first != (_Unwind_Sword) second;
690 break;
692 default:
693 abort ();
696 break;
698 case DW_OP_skip:
699 offset = read_2s (op_ptr);
700 op_ptr += 2;
701 op_ptr += offset;
702 goto no_push;
704 case DW_OP_bra:
705 if (--stack_elt < 0)
706 abort ();
707 offset = read_2s (op_ptr);
708 op_ptr += 2;
709 if (stack[stack_elt] != 0)
710 op_ptr += offset;
711 goto no_push;
713 case DW_OP_nop:
714 goto no_push;
716 default:
717 abort ();
720 /* Most things push a result value. */
721 if ((size_t) stack_elt >= sizeof(stack)/sizeof(*stack))
722 abort ();
723 stack[stack_elt++] = result;
724 no_push:;
727 /* We were executing this program to get a value. It should be
728 at top of stack. */
729 if (--stack_elt < 0)
730 abort ();
731 return stack[stack_elt];
735 /* Decode DWARF 2 call frame information. Takes pointers the
736 instruction sequence to decode, current register information and
737 CIE info, and the PC range to evaluate. */
739 static void
740 execute_cfa_program (const unsigned char *insn_ptr,
741 const unsigned char *insn_end,
742 struct _Unwind_Context *context,
743 _Unwind_FrameState *fs)
745 struct frame_state_reg_info *unused_rs = NULL;
747 /* Don't allow remember/restore between CIE and FDE programs. */
748 fs->regs.prev = NULL;
750 /* The comparison with the return address uses < rather than <= because
751 we are only interested in the effects of code before the call; for a
752 noreturn function, the return address may point to unrelated code with
753 a different stack configuration that we are not interested in. We
754 assume that the call itself is unwind info-neutral; if not, or if
755 there are delay instructions that adjust the stack, these must be
756 reflected at the point immediately before the call insn. */
757 while (insn_ptr < insn_end && fs->pc < context->ra)
759 unsigned char insn = *insn_ptr++;
760 _Unwind_Word reg, utmp;
761 _Unwind_Sword offset, stmp;
763 if ((insn & 0xc0) == DW_CFA_advance_loc)
764 fs->pc += (insn & 0x3f) * fs->code_align;
765 else if ((insn & 0xc0) == DW_CFA_offset)
767 reg = insn & 0x3f;
768 insn_ptr = read_uleb128 (insn_ptr, &utmp);
769 offset = (_Unwind_Sword) utmp * fs->data_align;
770 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
771 = REG_SAVED_OFFSET;
772 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
774 else if ((insn & 0xc0) == DW_CFA_restore)
776 reg = insn & 0x3f;
777 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
779 else switch (insn)
781 case DW_CFA_set_loc:
782 insn_ptr = read_encoded_value (context, fs->fde_encoding,
783 insn_ptr, (_Unwind_Ptr *) &fs->pc);
784 break;
786 case DW_CFA_advance_loc1:
787 fs->pc += read_1u (insn_ptr) * fs->code_align;
788 insn_ptr += 1;
789 break;
790 case DW_CFA_advance_loc2:
791 fs->pc += read_2u (insn_ptr) * fs->code_align;
792 insn_ptr += 2;
793 break;
794 case DW_CFA_advance_loc4:
795 fs->pc += read_4u (insn_ptr) * fs->code_align;
796 insn_ptr += 4;
797 break;
799 case DW_CFA_offset_extended:
800 insn_ptr = read_uleb128 (insn_ptr, &reg);
801 insn_ptr = read_uleb128 (insn_ptr, &utmp);
802 offset = (_Unwind_Sword) utmp * fs->data_align;
803 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
804 = REG_SAVED_OFFSET;
805 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
806 break;
808 case DW_CFA_restore_extended:
809 insn_ptr = read_uleb128 (insn_ptr, &reg);
810 /* FIXME, this is wrong; the CIE might have said that the
811 register was saved somewhere. */
812 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
813 break;
815 case DW_CFA_undefined:
816 case DW_CFA_same_value:
817 insn_ptr = read_uleb128 (insn_ptr, &reg);
818 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
819 break;
821 case DW_CFA_nop:
822 break;
824 case DW_CFA_register:
826 _Unwind_Word reg2;
827 insn_ptr = read_uleb128 (insn_ptr, &reg);
828 insn_ptr = read_uleb128 (insn_ptr, &reg2);
829 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
830 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg = reg2;
832 break;
834 case DW_CFA_remember_state:
836 struct frame_state_reg_info *new_rs;
837 if (unused_rs)
839 new_rs = unused_rs;
840 unused_rs = unused_rs->prev;
842 else
843 new_rs = __builtin_alloca (sizeof (struct frame_state_reg_info));
845 *new_rs = fs->regs;
846 fs->regs.prev = new_rs;
848 break;
850 case DW_CFA_restore_state:
852 struct frame_state_reg_info *old_rs = fs->regs.prev;
853 fs->regs = *old_rs;
854 old_rs->prev = unused_rs;
855 unused_rs = old_rs;
857 break;
859 case DW_CFA_def_cfa:
860 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
861 insn_ptr = read_uleb128 (insn_ptr, &utmp);
862 fs->cfa_offset = utmp;
863 fs->cfa_how = CFA_REG_OFFSET;
864 break;
866 case DW_CFA_def_cfa_register:
867 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
868 fs->cfa_how = CFA_REG_OFFSET;
869 break;
871 case DW_CFA_def_cfa_offset:
872 insn_ptr = read_uleb128 (insn_ptr, &utmp);
873 fs->cfa_offset = utmp;
874 /* cfa_how deliberately not set. */
875 break;
877 case DW_CFA_def_cfa_expression:
878 fs->cfa_exp = insn_ptr;
879 fs->cfa_how = CFA_EXP;
880 insn_ptr = read_uleb128 (insn_ptr, &utmp);
881 insn_ptr += utmp;
882 break;
884 case DW_CFA_expression:
885 insn_ptr = read_uleb128 (insn_ptr, &reg);
886 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
887 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
888 insn_ptr = read_uleb128 (insn_ptr, &utmp);
889 insn_ptr += utmp;
890 break;
892 /* From the 2.1 draft. */
893 case DW_CFA_offset_extended_sf:
894 insn_ptr = read_uleb128 (insn_ptr, &reg);
895 insn_ptr = read_sleb128 (insn_ptr, &stmp);
896 offset = stmp * fs->data_align;
897 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
898 = REG_SAVED_OFFSET;
899 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
900 break;
902 case DW_CFA_def_cfa_sf:
903 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
904 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
905 fs->cfa_how = CFA_REG_OFFSET;
906 break;
908 case DW_CFA_def_cfa_offset_sf:
909 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
910 /* cfa_how deliberately not set. */
911 break;
913 case DW_CFA_GNU_window_save:
914 /* ??? Hardcoded for SPARC register window configuration. */
915 for (reg = 16; reg < 32; ++reg)
917 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
918 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
920 break;
922 case DW_CFA_GNU_args_size:
923 insn_ptr = read_uleb128 (insn_ptr, &context->args_size);
924 break;
926 case DW_CFA_GNU_negative_offset_extended:
927 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
928 older PowerPC code. */
929 insn_ptr = read_uleb128 (insn_ptr, &reg);
930 insn_ptr = read_uleb128 (insn_ptr, &utmp);
931 offset = (_Unwind_Word) utmp * fs->data_align;
932 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
933 = REG_SAVED_OFFSET;
934 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
935 break;
937 default:
938 abort ();
943 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
944 its caller and decode it into FS. This function also sets the
945 args_size and lsda members of CONTEXT, as they are really information
946 about the caller's frame. */
948 static _Unwind_Reason_Code
949 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
951 const struct dwarf_fde *fde;
952 const struct dwarf_cie *cie;
953 const unsigned char *aug, *insn, *end;
955 memset (fs, 0, sizeof (*fs));
956 context->args_size = 0;
957 context->lsda = 0;
959 if (context->ra == 0)
960 return _URC_END_OF_STACK;
962 fde = _Unwind_Find_FDE (context->ra - 1, &context->bases);
963 if (fde == NULL)
965 #ifdef MD_FALLBACK_FRAME_STATE_FOR
966 /* Couldn't find frame unwind info for this function. Try a
967 target-specific fallback mechanism. This will necessarily
968 not provide a personality routine or LSDA. */
969 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
970 #else
971 return _URC_END_OF_STACK;
972 #endif
975 fs->pc = context->bases.func;
977 cie = get_cie (fde);
978 insn = extract_cie_info (cie, context, fs);
979 if (insn == NULL)
980 /* CIE contained unknown augmentation. */
981 return _URC_FATAL_PHASE1_ERROR;
983 /* First decode all the insns in the CIE. */
984 end = (unsigned char *) next_fde ((struct dwarf_fde *) cie);
985 execute_cfa_program (insn, end, context, fs);
987 /* Locate augmentation for the fde. */
988 aug = (unsigned char *) fde + sizeof (*fde);
989 aug += 2 * size_of_encoded_value (fs->fde_encoding);
990 insn = NULL;
991 if (fs->saw_z)
993 _Unwind_Word i;
994 aug = read_uleb128 (aug, &i);
995 insn = aug + i;
997 if (fs->lsda_encoding != DW_EH_PE_omit)
998 aug = read_encoded_value (context, fs->lsda_encoding, aug,
999 (_Unwind_Ptr *) &context->lsda);
1001 /* Then the insns in the FDE up to our target PC. */
1002 if (insn == NULL)
1003 insn = aug;
1004 end = (unsigned char *) next_fde (fde);
1005 execute_cfa_program (insn, end, context, fs);
1007 return _URC_NO_REASON;
1010 typedef struct frame_state
1012 void *cfa;
1013 void *eh_ptr;
1014 long cfa_offset;
1015 long args_size;
1016 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1017 unsigned short cfa_reg;
1018 unsigned short retaddr_column;
1019 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1020 } frame_state;
1022 struct frame_state * __frame_state_for (void *, struct frame_state *);
1024 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1025 a given PC_TARGET. The caller should allocate a local variable of
1026 `struct frame_state' and pass its address to STATE_IN. */
1028 struct frame_state *
1029 __frame_state_for (void *pc_target, struct frame_state *state_in)
1031 struct _Unwind_Context context;
1032 _Unwind_FrameState fs;
1033 int reg;
1035 memset (&context, 0, sizeof (struct _Unwind_Context));
1036 context.ra = pc_target + 1;
1038 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1039 return 0;
1041 /* We have no way to pass a location expression for the CFA to our
1042 caller. It wouldn't understand it anyway. */
1043 if (fs.cfa_how == CFA_EXP)
1044 return 0;
1046 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1048 state_in->saved[reg] = fs.regs.reg[reg].how;
1049 switch (state_in->saved[reg])
1051 case REG_SAVED_REG:
1052 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1053 break;
1054 case REG_SAVED_OFFSET:
1055 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1056 break;
1057 default:
1058 state_in->reg_or_offset[reg] = 0;
1059 break;
1063 state_in->cfa_offset = fs.cfa_offset;
1064 state_in->cfa_reg = fs.cfa_reg;
1065 state_in->retaddr_column = fs.retaddr_column;
1066 state_in->args_size = context.args_size;
1067 state_in->eh_ptr = fs.eh_ptr;
1069 return state_in;
1072 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1074 static inline void
1075 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1076 _Unwind_SpTmp *tmp_sp)
1078 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1080 if (size == sizeof(_Unwind_Ptr))
1081 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1082 else if (size == sizeof(_Unwind_Word))
1083 tmp_sp->word = (_Unwind_Ptr) cfa;
1084 else
1085 abort ();
1086 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1089 static void
1090 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1092 struct _Unwind_Context orig_context = *context;
1093 void *cfa;
1094 long i;
1096 #ifdef EH_RETURN_STACKADJ_RTX
1097 /* Special handling here: Many machines do not use a frame pointer,
1098 and track the CFA only through offsets from the stack pointer from
1099 one frame to the next. In this case, the stack pointer is never
1100 stored, so it has no saved address in the context. What we do
1101 have is the CFA from the previous stack frame.
1103 In very special situations (such as unwind info for signal return),
1104 there may be location expressions that use the stack pointer as well.
1106 Do this conditionally for one frame. This allows the unwind info
1107 for one frame to save a copy of the stack pointer from the previous
1108 frame, and be able to use much easier CFA mechanisms to do it.
1109 Always zap the saved stack pointer value for the next frame; carrying
1110 the value over from one frame to another doesn't make sense. */
1112 _Unwind_SpTmp tmp_sp;
1114 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1115 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1116 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1117 #endif
1119 /* Compute this frame's CFA. */
1120 switch (fs->cfa_how)
1122 case CFA_REG_OFFSET:
1123 cfa = _Unwind_GetPtr (&orig_context, fs->cfa_reg);
1124 cfa += fs->cfa_offset;
1125 break;
1127 case CFA_EXP:
1129 const unsigned char *exp = fs->cfa_exp;
1130 _Unwind_Word len;
1132 exp = read_uleb128 (exp, &len);
1133 cfa = (void *) (_Unwind_Ptr)
1134 execute_stack_op (exp, exp + len, &orig_context, 0);
1135 break;
1138 default:
1139 abort ();
1141 context->cfa = cfa;
1143 /* Compute the addresses of all registers saved in this frame. */
1144 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1145 switch (fs->regs.reg[i].how)
1147 case REG_UNSAVED:
1148 break;
1150 case REG_SAVED_OFFSET:
1151 _Unwind_SetGRPtr (context, i,
1152 (void *) (cfa + fs->regs.reg[i].loc.offset));
1153 break;
1155 case REG_SAVED_REG:
1156 _Unwind_SetGRPtr
1157 (context, i,
1158 _Unwind_GetGRPtr (&orig_context, fs->regs.reg[i].loc.reg));
1159 break;
1161 case REG_SAVED_EXP:
1163 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1164 _Unwind_Word len;
1165 _Unwind_Ptr val;
1167 exp = read_uleb128 (exp, &len);
1168 val = execute_stack_op (exp, exp + len, &orig_context,
1169 (_Unwind_Ptr) cfa);
1170 _Unwind_SetGRPtr (context, i, (void *) val);
1172 break;
1175 #ifdef MD_FROB_UPDATE_CONTEXT
1176 MD_FROB_UPDATE_CONTEXT (context, fs);
1177 #endif
1180 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1181 of its caller. Update CONTEXT to refer to the caller as well. Note
1182 that the args_size and lsda members are not updated here, but later in
1183 uw_frame_state_for. */
1185 static void
1186 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1188 uw_update_context_1 (context, fs);
1190 /* Compute the return address now, since the return address column
1191 can change from frame to frame. */
1192 context->ra = __builtin_extract_return_addr
1193 (_Unwind_GetPtr (context, fs->retaddr_column));
1196 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1197 level will be the return address and the CFA. */
1199 #define uw_init_context(CONTEXT) \
1200 do \
1202 /* Do any necessary initialization to access arbitrary stack frames. \
1203 On the SPARC, this means flushing the register windows. */ \
1204 __builtin_unwind_init (); \
1205 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1206 __builtin_return_address (0)); \
1208 while (0)
1210 static inline void
1211 init_dwarf_reg_size_table (void)
1213 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1216 static void
1217 uw_init_context_1 (struct _Unwind_Context *context,
1218 void *outer_cfa, void *outer_ra)
1220 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1221 _Unwind_FrameState fs;
1222 _Unwind_SpTmp sp_slot;
1224 memset (context, 0, sizeof (struct _Unwind_Context));
1225 context->ra = ra;
1227 if (uw_frame_state_for (context, &fs) != _URC_NO_REASON)
1228 abort ();
1230 #if __GTHREADS
1232 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1233 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1234 || dwarf_reg_size_table[0] == 0)
1235 init_dwarf_reg_size_table ();
1237 #else
1238 if (dwarf_reg_size_table[0] == 0)
1239 init_dwarf_reg_size_table ();
1240 #endif
1242 /* Force the frame state to use the known cfa value. */
1243 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1244 fs.cfa_how = CFA_REG_OFFSET;
1245 fs.cfa_reg = __builtin_dwarf_sp_column ();
1246 fs.cfa_offset = 0;
1248 uw_update_context_1 (context, &fs);
1250 /* If the return address column was saved in a register in the
1251 initialization context, then we can't see it in the given
1252 call frame data. So have the initialization context tell us. */
1253 context->ra = __builtin_extract_return_addr (outer_ra);
1257 /* Install TARGET into CURRENT so that we can return to it. This is a
1258 macro because __builtin_eh_return must be invoked in the context of
1259 our caller. */
1261 #define uw_install_context(CURRENT, TARGET) \
1262 do \
1264 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1265 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1266 __builtin_eh_return (offset, handler); \
1268 while (0)
1270 static long
1271 uw_install_context_1 (struct _Unwind_Context *current,
1272 struct _Unwind_Context *target)
1274 long i;
1275 _Unwind_SpTmp sp_slot;
1277 /* If the target frame does not have a saved stack pointer,
1278 then set up the target's CFA. */
1279 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1280 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1282 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1284 void *c = current->reg[i];
1285 void *t = target->reg[i];
1287 if (t && c && t != c)
1288 memcpy (c, t, dwarf_reg_size_table[i]);
1291 /* If the current frame doesn't have a saved stack pointer, then we
1292 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1293 pointer value reloaded. */
1294 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1296 void *target_cfa;
1298 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1300 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1301 if (STACK_GROWS_DOWNWARD)
1302 return target_cfa - current->cfa + target->args_size;
1303 else
1304 return current->cfa - target_cfa - target->args_size;
1306 return 0;
1309 static inline _Unwind_Ptr
1310 uw_identify_context (struct _Unwind_Context *context)
1312 return _Unwind_GetIP (context);
1316 #include "unwind.inc"
1318 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1319 alias (_Unwind_Backtrace);
1320 alias (_Unwind_DeleteException);
1321 alias (_Unwind_FindEnclosingFunction);
1322 alias (_Unwind_FindTableEntry);
1323 alias (_Unwind_ForcedUnwind);
1324 alias (_Unwind_GetDataRelBase);
1325 alias (_Unwind_GetTextRelBase);
1326 alias (_Unwind_GetCFA);
1327 alias (_Unwind_GetGR);
1328 alias (_Unwind_GetIP);
1329 alias (_Unwind_GetLanguageSpecificData);
1330 alias (_Unwind_GetRegionStart);
1331 alias (_Unwind_RaiseException);
1332 alias (_Unwind_Resume);
1333 alias (_Unwind_Resume_or_Rethrow);
1334 alias (_Unwind_SetGR);
1335 alias (_Unwind_SetIP);
1336 #endif
1338 #endif /* !USING_SJLJ_EXCEPTIONS */