Daily bump.
[official-gcc.git] / gcc / unwind-dw2.c
blob4ffdd02cd20aa1838323b5f6ac4fbffd9ab3469e
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 if (index >= (int) sizeof(dwarf_reg_size_table))
135 abort ();
136 size = dwarf_reg_size_table[index];
137 ptr = context->reg[index];
139 /* This will segfault if the register hasn't been saved. */
140 if (size == sizeof(_Unwind_Ptr))
141 return * (_Unwind_Ptr *) ptr;
143 if (size == sizeof(_Unwind_Word))
144 return * (_Unwind_Word *) ptr;
146 abort ();
149 static inline void *
150 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
152 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
155 /* Get the value of the CFA as saved in CONTEXT. */
157 _Unwind_Word
158 _Unwind_GetCFA (struct _Unwind_Context *context)
160 return (_Unwind_Ptr) context->cfa;
163 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
165 inline void
166 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
168 int size;
169 void *ptr;
171 index = DWARF_REG_TO_UNWIND_COLUMN (index);
172 if (index >= (int) sizeof(dwarf_reg_size_table))
173 abort ();
174 size = dwarf_reg_size_table[index];
175 ptr = context->reg[index];
177 if (size == sizeof(_Unwind_Ptr))
178 * (_Unwind_Ptr *) ptr = val;
179 else if (size == sizeof(_Unwind_Word))
180 * (_Unwind_Word *) ptr = val;
181 else
182 abort ();
185 /* Get the pointer to a register INDEX as saved in CONTEXT. */
187 static inline void *
188 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
190 index = DWARF_REG_TO_UNWIND_COLUMN (index);
191 return context->reg[index];
194 /* Set the pointer to a register INDEX as saved in CONTEXT. */
196 static inline void
197 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
199 index = DWARF_REG_TO_UNWIND_COLUMN (index);
200 context->reg[index] = p;
203 /* Retrieve the return address for CONTEXT. */
205 inline _Unwind_Ptr
206 _Unwind_GetIP (struct _Unwind_Context *context)
208 return (_Unwind_Ptr) context->ra;
211 /* Overwrite the return address for CONTEXT with VAL. */
213 inline void
214 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
216 context->ra = (void *) val;
219 void *
220 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
222 return context->lsda;
225 _Unwind_Ptr
226 _Unwind_GetRegionStart (struct _Unwind_Context *context)
228 return (_Unwind_Ptr) context->bases.func;
231 void *
232 _Unwind_FindEnclosingFunction (void *pc)
234 struct dwarf_eh_bases bases;
235 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
236 if (fde)
237 return bases.func;
238 else
239 return NULL;
242 #ifndef __ia64__
243 _Unwind_Ptr
244 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
246 return (_Unwind_Ptr) context->bases.dbase;
249 _Unwind_Ptr
250 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
252 return (_Unwind_Ptr) context->bases.tbase;
254 #endif
256 #ifdef MD_UNWIND_SUPPORT
257 #include MD_UNWIND_SUPPORT
258 #endif
260 /* Extract any interesting information from the CIE for the translation
261 unit F belongs to. Return a pointer to the byte after the augmentation,
262 or NULL if we encountered an undecipherable augmentation. */
264 static const unsigned char *
265 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
266 _Unwind_FrameState *fs)
268 const unsigned char *aug = cie->augmentation;
269 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
270 const unsigned char *ret = NULL;
271 _Unwind_Word utmp;
273 /* g++ v2 "eh" has pointer immediately following augmentation string,
274 so it must be handled first. */
275 if (aug[0] == 'e' && aug[1] == 'h')
277 fs->eh_ptr = read_pointer (p);
278 p += sizeof (void *);
279 aug += 2;
282 /* Immediately following the augmentation are the code and
283 data alignment and return address column. */
284 p = read_uleb128 (p, &fs->code_align);
285 p = read_sleb128 (p, &fs->data_align);
286 if (cie->version == 1)
287 fs->retaddr_column = *p++;
288 else
289 p = read_uleb128 (p, &fs->retaddr_column);
290 fs->lsda_encoding = DW_EH_PE_omit;
292 /* If the augmentation starts with 'z', then a uleb128 immediately
293 follows containing the length of the augmentation field following
294 the size. */
295 if (*aug == 'z')
297 p = read_uleb128 (p, &utmp);
298 ret = p + utmp;
300 fs->saw_z = 1;
301 ++aug;
304 /* Iterate over recognized augmentation subsequences. */
305 while (*aug != '\0')
307 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
308 if (aug[0] == 'L')
310 fs->lsda_encoding = *p++;
311 aug += 1;
314 /* "R" indicates a byte indicating how FDE addresses are encoded. */
315 else if (aug[0] == 'R')
317 fs->fde_encoding = *p++;
318 aug += 1;
321 /* "P" indicates a personality routine in the CIE augmentation. */
322 else if (aug[0] == 'P')
324 p = read_encoded_value (context, *p, p + 1,
325 (_Unwind_Ptr *) &fs->personality);
326 aug += 1;
329 /* Otherwise we have an unknown augmentation string.
330 Bail unless we saw a 'z' prefix. */
331 else
332 return ret;
335 return ret ? ret : p;
339 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
340 onto the stack to start. */
342 static _Unwind_Word
343 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
344 struct _Unwind_Context *context, _Unwind_Word initial)
346 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
347 int stack_elt;
349 stack[0] = initial;
350 stack_elt = 1;
352 while (op_ptr < op_end)
354 enum dwarf_location_atom op = *op_ptr++;
355 _Unwind_Word result, reg, utmp;
356 _Unwind_Sword offset, stmp;
358 switch (op)
360 case DW_OP_lit0:
361 case DW_OP_lit1:
362 case DW_OP_lit2:
363 case DW_OP_lit3:
364 case DW_OP_lit4:
365 case DW_OP_lit5:
366 case DW_OP_lit6:
367 case DW_OP_lit7:
368 case DW_OP_lit8:
369 case DW_OP_lit9:
370 case DW_OP_lit10:
371 case DW_OP_lit11:
372 case DW_OP_lit12:
373 case DW_OP_lit13:
374 case DW_OP_lit14:
375 case DW_OP_lit15:
376 case DW_OP_lit16:
377 case DW_OP_lit17:
378 case DW_OP_lit18:
379 case DW_OP_lit19:
380 case DW_OP_lit20:
381 case DW_OP_lit21:
382 case DW_OP_lit22:
383 case DW_OP_lit23:
384 case DW_OP_lit24:
385 case DW_OP_lit25:
386 case DW_OP_lit26:
387 case DW_OP_lit27:
388 case DW_OP_lit28:
389 case DW_OP_lit29:
390 case DW_OP_lit30:
391 case DW_OP_lit31:
392 result = op - DW_OP_lit0;
393 break;
395 case DW_OP_addr:
396 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
397 op_ptr += sizeof (void *);
398 break;
400 case DW_OP_const1u:
401 result = read_1u (op_ptr);
402 op_ptr += 1;
403 break;
404 case DW_OP_const1s:
405 result = read_1s (op_ptr);
406 op_ptr += 1;
407 break;
408 case DW_OP_const2u:
409 result = read_2u (op_ptr);
410 op_ptr += 2;
411 break;
412 case DW_OP_const2s:
413 result = read_2s (op_ptr);
414 op_ptr += 2;
415 break;
416 case DW_OP_const4u:
417 result = read_4u (op_ptr);
418 op_ptr += 4;
419 break;
420 case DW_OP_const4s:
421 result = read_4s (op_ptr);
422 op_ptr += 4;
423 break;
424 case DW_OP_const8u:
425 result = read_8u (op_ptr);
426 op_ptr += 8;
427 break;
428 case DW_OP_const8s:
429 result = read_8s (op_ptr);
430 op_ptr += 8;
431 break;
432 case DW_OP_constu:
433 op_ptr = read_uleb128 (op_ptr, &result);
434 break;
435 case DW_OP_consts:
436 op_ptr = read_sleb128 (op_ptr, &stmp);
437 result = stmp;
438 break;
440 case DW_OP_reg0:
441 case DW_OP_reg1:
442 case DW_OP_reg2:
443 case DW_OP_reg3:
444 case DW_OP_reg4:
445 case DW_OP_reg5:
446 case DW_OP_reg6:
447 case DW_OP_reg7:
448 case DW_OP_reg8:
449 case DW_OP_reg9:
450 case DW_OP_reg10:
451 case DW_OP_reg11:
452 case DW_OP_reg12:
453 case DW_OP_reg13:
454 case DW_OP_reg14:
455 case DW_OP_reg15:
456 case DW_OP_reg16:
457 case DW_OP_reg17:
458 case DW_OP_reg18:
459 case DW_OP_reg19:
460 case DW_OP_reg20:
461 case DW_OP_reg21:
462 case DW_OP_reg22:
463 case DW_OP_reg23:
464 case DW_OP_reg24:
465 case DW_OP_reg25:
466 case DW_OP_reg26:
467 case DW_OP_reg27:
468 case DW_OP_reg28:
469 case DW_OP_reg29:
470 case DW_OP_reg30:
471 case DW_OP_reg31:
472 result = _Unwind_GetGR (context, op - DW_OP_reg0);
473 break;
474 case DW_OP_regx:
475 op_ptr = read_uleb128 (op_ptr, &reg);
476 result = _Unwind_GetGR (context, reg);
477 break;
479 case DW_OP_breg0:
480 case DW_OP_breg1:
481 case DW_OP_breg2:
482 case DW_OP_breg3:
483 case DW_OP_breg4:
484 case DW_OP_breg5:
485 case DW_OP_breg6:
486 case DW_OP_breg7:
487 case DW_OP_breg8:
488 case DW_OP_breg9:
489 case DW_OP_breg10:
490 case DW_OP_breg11:
491 case DW_OP_breg12:
492 case DW_OP_breg13:
493 case DW_OP_breg14:
494 case DW_OP_breg15:
495 case DW_OP_breg16:
496 case DW_OP_breg17:
497 case DW_OP_breg18:
498 case DW_OP_breg19:
499 case DW_OP_breg20:
500 case DW_OP_breg21:
501 case DW_OP_breg22:
502 case DW_OP_breg23:
503 case DW_OP_breg24:
504 case DW_OP_breg25:
505 case DW_OP_breg26:
506 case DW_OP_breg27:
507 case DW_OP_breg28:
508 case DW_OP_breg29:
509 case DW_OP_breg30:
510 case DW_OP_breg31:
511 op_ptr = read_sleb128 (op_ptr, &offset);
512 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
513 break;
514 case DW_OP_bregx:
515 op_ptr = read_uleb128 (op_ptr, &reg);
516 op_ptr = read_sleb128 (op_ptr, &offset);
517 result = _Unwind_GetGR (context, reg) + offset;
518 break;
520 case DW_OP_dup:
521 if (stack_elt < 1)
522 abort ();
523 result = stack[stack_elt - 1];
524 break;
526 case DW_OP_drop:
527 if (--stack_elt < 0)
528 abort ();
529 goto no_push;
531 case DW_OP_pick:
532 offset = *op_ptr++;
533 if (offset >= stack_elt - 1)
534 abort ();
535 result = stack[stack_elt - 1 - offset];
536 break;
538 case DW_OP_over:
539 if (stack_elt < 2)
540 abort ();
541 result = stack[stack_elt - 2];
542 break;
544 case DW_OP_rot:
546 _Unwind_Word t1, t2, t3;
548 if (stack_elt < 3)
549 abort ();
550 t1 = stack[stack_elt - 1];
551 t2 = stack[stack_elt - 2];
552 t3 = stack[stack_elt - 3];
553 stack[stack_elt - 1] = t2;
554 stack[stack_elt - 2] = t3;
555 stack[stack_elt - 3] = t1;
556 goto no_push;
559 case DW_OP_deref:
560 case DW_OP_deref_size:
561 case DW_OP_abs:
562 case DW_OP_neg:
563 case DW_OP_not:
564 case DW_OP_plus_uconst:
565 /* Unary operations. */
566 if (--stack_elt < 0)
567 abort ();
568 result = stack[stack_elt];
570 switch (op)
572 case DW_OP_deref:
574 void *ptr = (void *) (_Unwind_Ptr) result;
575 result = (_Unwind_Ptr) read_pointer (ptr);
577 break;
579 case DW_OP_deref_size:
581 void *ptr = (void *) (_Unwind_Ptr) result;
582 switch (*op_ptr++)
584 case 1:
585 result = read_1u (ptr);
586 break;
587 case 2:
588 result = read_2u (ptr);
589 break;
590 case 4:
591 result = read_4u (ptr);
592 break;
593 case 8:
594 result = read_8u (ptr);
595 break;
596 default:
597 abort ();
600 break;
602 case DW_OP_abs:
603 if ((_Unwind_Sword) result < 0)
604 result = -result;
605 break;
606 case DW_OP_neg:
607 result = -result;
608 break;
609 case DW_OP_not:
610 result = ~result;
611 break;
612 case DW_OP_plus_uconst:
613 op_ptr = read_uleb128 (op_ptr, &utmp);
614 result += utmp;
615 break;
617 default:
618 abort ();
620 break;
622 case DW_OP_and:
623 case DW_OP_div:
624 case DW_OP_minus:
625 case DW_OP_mod:
626 case DW_OP_mul:
627 case DW_OP_or:
628 case DW_OP_plus:
629 case DW_OP_shl:
630 case DW_OP_shr:
631 case DW_OP_shra:
632 case DW_OP_xor:
633 case DW_OP_le:
634 case DW_OP_ge:
635 case DW_OP_eq:
636 case DW_OP_lt:
637 case DW_OP_gt:
638 case DW_OP_ne:
640 /* Binary operations. */
641 _Unwind_Word first, second;
642 if ((stack_elt -= 2) < 0)
643 abort ();
644 second = stack[stack_elt];
645 first = stack[stack_elt + 1];
647 switch (op)
649 case DW_OP_and:
650 result = second & first;
651 break;
652 case DW_OP_div:
653 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
654 break;
655 case DW_OP_minus:
656 result = second - first;
657 break;
658 case DW_OP_mod:
659 result = (_Unwind_Sword) second % (_Unwind_Sword) first;
660 break;
661 case DW_OP_mul:
662 result = second * first;
663 break;
664 case DW_OP_or:
665 result = second | first;
666 break;
667 case DW_OP_plus:
668 result = second + first;
669 break;
670 case DW_OP_shl:
671 result = second << first;
672 break;
673 case DW_OP_shr:
674 result = second >> first;
675 break;
676 case DW_OP_shra:
677 result = (_Unwind_Sword) second >> first;
678 break;
679 case DW_OP_xor:
680 result = second ^ first;
681 break;
682 case DW_OP_le:
683 result = (_Unwind_Sword) first <= (_Unwind_Sword) second;
684 break;
685 case DW_OP_ge:
686 result = (_Unwind_Sword) first >= (_Unwind_Sword) second;
687 break;
688 case DW_OP_eq:
689 result = (_Unwind_Sword) first == (_Unwind_Sword) second;
690 break;
691 case DW_OP_lt:
692 result = (_Unwind_Sword) first < (_Unwind_Sword) second;
693 break;
694 case DW_OP_gt:
695 result = (_Unwind_Sword) first > (_Unwind_Sword) second;
696 break;
697 case DW_OP_ne:
698 result = (_Unwind_Sword) first != (_Unwind_Sword) second;
699 break;
701 default:
702 abort ();
705 break;
707 case DW_OP_skip:
708 offset = read_2s (op_ptr);
709 op_ptr += 2;
710 op_ptr += offset;
711 goto no_push;
713 case DW_OP_bra:
714 if (--stack_elt < 0)
715 abort ();
716 offset = read_2s (op_ptr);
717 op_ptr += 2;
718 if (stack[stack_elt] != 0)
719 op_ptr += offset;
720 goto no_push;
722 case DW_OP_nop:
723 goto no_push;
725 default:
726 abort ();
729 /* Most things push a result value. */
730 if ((size_t) stack_elt >= sizeof(stack)/sizeof(*stack))
731 abort ();
732 stack[stack_elt++] = result;
733 no_push:;
736 /* We were executing this program to get a value. It should be
737 at top of stack. */
738 if (--stack_elt < 0)
739 abort ();
740 return stack[stack_elt];
744 /* Decode DWARF 2 call frame information. Takes pointers the
745 instruction sequence to decode, current register information and
746 CIE info, and the PC range to evaluate. */
748 static void
749 execute_cfa_program (const unsigned char *insn_ptr,
750 const unsigned char *insn_end,
751 struct _Unwind_Context *context,
752 _Unwind_FrameState *fs)
754 struct frame_state_reg_info *unused_rs = NULL;
756 /* Don't allow remember/restore between CIE and FDE programs. */
757 fs->regs.prev = NULL;
759 /* The comparison with the return address uses < rather than <= because
760 we are only interested in the effects of code before the call; for a
761 noreturn function, the return address may point to unrelated code with
762 a different stack configuration that we are not interested in. We
763 assume that the call itself is unwind info-neutral; if not, or if
764 there are delay instructions that adjust the stack, these must be
765 reflected at the point immediately before the call insn. */
766 while (insn_ptr < insn_end && fs->pc < context->ra)
768 unsigned char insn = *insn_ptr++;
769 _Unwind_Word reg, utmp;
770 _Unwind_Sword offset, stmp;
772 if ((insn & 0xc0) == DW_CFA_advance_loc)
773 fs->pc += (insn & 0x3f) * fs->code_align;
774 else if ((insn & 0xc0) == DW_CFA_offset)
776 reg = insn & 0x3f;
777 insn_ptr = read_uleb128 (insn_ptr, &utmp);
778 offset = (_Unwind_Sword) utmp * fs->data_align;
779 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
780 = REG_SAVED_OFFSET;
781 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
783 else if ((insn & 0xc0) == DW_CFA_restore)
785 reg = insn & 0x3f;
786 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
788 else switch (insn)
790 case DW_CFA_set_loc:
791 insn_ptr = read_encoded_value (context, fs->fde_encoding,
792 insn_ptr, (_Unwind_Ptr *) &fs->pc);
793 break;
795 case DW_CFA_advance_loc1:
796 fs->pc += read_1u (insn_ptr) * fs->code_align;
797 insn_ptr += 1;
798 break;
799 case DW_CFA_advance_loc2:
800 fs->pc += read_2u (insn_ptr) * fs->code_align;
801 insn_ptr += 2;
802 break;
803 case DW_CFA_advance_loc4:
804 fs->pc += read_4u (insn_ptr) * fs->code_align;
805 insn_ptr += 4;
806 break;
808 case DW_CFA_offset_extended:
809 insn_ptr = read_uleb128 (insn_ptr, &reg);
810 insn_ptr = read_uleb128 (insn_ptr, &utmp);
811 offset = (_Unwind_Sword) utmp * fs->data_align;
812 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
813 = REG_SAVED_OFFSET;
814 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
815 break;
817 case DW_CFA_restore_extended:
818 insn_ptr = read_uleb128 (insn_ptr, &reg);
819 /* FIXME, this is wrong; the CIE might have said that the
820 register was saved somewhere. */
821 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
822 break;
824 case DW_CFA_undefined:
825 case DW_CFA_same_value:
826 insn_ptr = read_uleb128 (insn_ptr, &reg);
827 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
828 break;
830 case DW_CFA_nop:
831 break;
833 case DW_CFA_register:
835 _Unwind_Word reg2;
836 insn_ptr = read_uleb128 (insn_ptr, &reg);
837 insn_ptr = read_uleb128 (insn_ptr, &reg2);
838 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
839 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg = reg2;
841 break;
843 case DW_CFA_remember_state:
845 struct frame_state_reg_info *new_rs;
846 if (unused_rs)
848 new_rs = unused_rs;
849 unused_rs = unused_rs->prev;
851 else
852 new_rs = alloca (sizeof (struct frame_state_reg_info));
854 *new_rs = fs->regs;
855 fs->regs.prev = new_rs;
857 break;
859 case DW_CFA_restore_state:
861 struct frame_state_reg_info *old_rs = fs->regs.prev;
862 fs->regs = *old_rs;
863 old_rs->prev = unused_rs;
864 unused_rs = old_rs;
866 break;
868 case DW_CFA_def_cfa:
869 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
870 insn_ptr = read_uleb128 (insn_ptr, &utmp);
871 fs->cfa_offset = utmp;
872 fs->cfa_how = CFA_REG_OFFSET;
873 break;
875 case DW_CFA_def_cfa_register:
876 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
877 fs->cfa_how = CFA_REG_OFFSET;
878 break;
880 case DW_CFA_def_cfa_offset:
881 insn_ptr = read_uleb128 (insn_ptr, &utmp);
882 fs->cfa_offset = utmp;
883 /* cfa_how deliberately not set. */
884 break;
886 case DW_CFA_def_cfa_expression:
887 fs->cfa_exp = insn_ptr;
888 fs->cfa_how = CFA_EXP;
889 insn_ptr = read_uleb128 (insn_ptr, &utmp);
890 insn_ptr += utmp;
891 break;
893 case DW_CFA_expression:
894 insn_ptr = read_uleb128 (insn_ptr, &reg);
895 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
896 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
897 insn_ptr = read_uleb128 (insn_ptr, &utmp);
898 insn_ptr += utmp;
899 break;
901 /* From the 2.1 draft. */
902 case DW_CFA_offset_extended_sf:
903 insn_ptr = read_uleb128 (insn_ptr, &reg);
904 insn_ptr = read_sleb128 (insn_ptr, &stmp);
905 offset = stmp * fs->data_align;
906 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
907 = REG_SAVED_OFFSET;
908 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
909 break;
911 case DW_CFA_def_cfa_sf:
912 insn_ptr = read_uleb128 (insn_ptr, &fs->cfa_reg);
913 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
914 fs->cfa_how = CFA_REG_OFFSET;
915 break;
917 case DW_CFA_def_cfa_offset_sf:
918 insn_ptr = read_sleb128 (insn_ptr, &fs->cfa_offset);
919 /* cfa_how deliberately not set. */
920 break;
922 case DW_CFA_GNU_window_save:
923 /* ??? Hardcoded for SPARC register window configuration. */
924 for (reg = 16; reg < 32; ++reg)
926 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
927 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
929 break;
931 case DW_CFA_GNU_args_size:
932 insn_ptr = read_uleb128 (insn_ptr, &context->args_size);
933 break;
935 case DW_CFA_GNU_negative_offset_extended:
936 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
937 older PowerPC code. */
938 insn_ptr = read_uleb128 (insn_ptr, &reg);
939 insn_ptr = read_uleb128 (insn_ptr, &utmp);
940 offset = (_Unwind_Word) utmp * fs->data_align;
941 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
942 = REG_SAVED_OFFSET;
943 fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
944 break;
946 default:
947 abort ();
952 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
953 its caller and decode it into FS. This function also sets the
954 args_size and lsda members of CONTEXT, as they are really information
955 about the caller's frame. */
957 static _Unwind_Reason_Code
958 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
960 const struct dwarf_fde *fde;
961 const struct dwarf_cie *cie;
962 const unsigned char *aug, *insn, *end;
964 memset (fs, 0, sizeof (*fs));
965 context->args_size = 0;
966 context->lsda = 0;
968 if (context->ra == 0)
969 return _URC_END_OF_STACK;
971 fde = _Unwind_Find_FDE (context->ra - 1, &context->bases);
972 if (fde == NULL)
974 #ifdef MD_FALLBACK_FRAME_STATE_FOR
975 /* Couldn't find frame unwind info for this function. Try a
976 target-specific fallback mechanism. This will necessarily
977 not provide a personality routine or LSDA. */
978 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
979 #else
980 return _URC_END_OF_STACK;
981 #endif
984 fs->pc = context->bases.func;
986 cie = get_cie (fde);
987 insn = extract_cie_info (cie, context, fs);
988 if (insn == NULL)
989 /* CIE contained unknown augmentation. */
990 return _URC_FATAL_PHASE1_ERROR;
992 /* First decode all the insns in the CIE. */
993 end = (unsigned char *) next_fde ((struct dwarf_fde *) cie);
994 execute_cfa_program (insn, end, context, fs);
996 /* Locate augmentation for the fde. */
997 aug = (unsigned char *) fde + sizeof (*fde);
998 aug += 2 * size_of_encoded_value (fs->fde_encoding);
999 insn = NULL;
1000 if (fs->saw_z)
1002 _Unwind_Word i;
1003 aug = read_uleb128 (aug, &i);
1004 insn = aug + i;
1006 if (fs->lsda_encoding != DW_EH_PE_omit)
1007 aug = read_encoded_value (context, fs->lsda_encoding, aug,
1008 (_Unwind_Ptr *) &context->lsda);
1010 /* Then the insns in the FDE up to our target PC. */
1011 if (insn == NULL)
1012 insn = aug;
1013 end = (unsigned char *) next_fde (fde);
1014 execute_cfa_program (insn, end, context, fs);
1016 return _URC_NO_REASON;
1019 typedef struct frame_state
1021 void *cfa;
1022 void *eh_ptr;
1023 long cfa_offset;
1024 long args_size;
1025 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1026 unsigned short cfa_reg;
1027 unsigned short retaddr_column;
1028 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1029 } frame_state;
1031 struct frame_state * __frame_state_for (void *, struct frame_state *);
1033 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1034 a given PC_TARGET. The caller should allocate a local variable of
1035 `struct frame_state' and pass its address to STATE_IN. */
1037 struct frame_state *
1038 __frame_state_for (void *pc_target, struct frame_state *state_in)
1040 struct _Unwind_Context context;
1041 _Unwind_FrameState fs;
1042 int reg;
1044 memset (&context, 0, sizeof (struct _Unwind_Context));
1045 context.ra = pc_target + 1;
1047 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1048 return 0;
1050 /* We have no way to pass a location expression for the CFA to our
1051 caller. It wouldn't understand it anyway. */
1052 if (fs.cfa_how == CFA_EXP)
1053 return 0;
1055 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1057 state_in->saved[reg] = fs.regs.reg[reg].how;
1058 switch (state_in->saved[reg])
1060 case REG_SAVED_REG:
1061 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1062 break;
1063 case REG_SAVED_OFFSET:
1064 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1065 break;
1066 default:
1067 state_in->reg_or_offset[reg] = 0;
1068 break;
1072 state_in->cfa_offset = fs.cfa_offset;
1073 state_in->cfa_reg = fs.cfa_reg;
1074 state_in->retaddr_column = fs.retaddr_column;
1075 state_in->args_size = context.args_size;
1076 state_in->eh_ptr = fs.eh_ptr;
1078 return state_in;
1081 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1083 static inline void
1084 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1085 _Unwind_SpTmp *tmp_sp)
1087 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1089 if (size == sizeof(_Unwind_Ptr))
1090 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1091 else if (size == sizeof(_Unwind_Word))
1092 tmp_sp->word = (_Unwind_Ptr) cfa;
1093 else
1094 abort ();
1095 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1098 static void
1099 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1101 struct _Unwind_Context orig_context = *context;
1102 void *cfa;
1103 long i;
1105 #ifdef EH_RETURN_STACKADJ_RTX
1106 /* Special handling here: Many machines do not use a frame pointer,
1107 and track the CFA only through offsets from the stack pointer from
1108 one frame to the next. In this case, the stack pointer is never
1109 stored, so it has no saved address in the context. What we do
1110 have is the CFA from the previous stack frame.
1112 In very special situations (such as unwind info for signal return),
1113 there may be location expressions that use the stack pointer as well.
1115 Do this conditionally for one frame. This allows the unwind info
1116 for one frame to save a copy of the stack pointer from the previous
1117 frame, and be able to use much easier CFA mechanisms to do it.
1118 Always zap the saved stack pointer value for the next frame; carrying
1119 the value over from one frame to another doesn't make sense. */
1121 _Unwind_SpTmp tmp_sp;
1123 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1124 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1125 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1126 #endif
1128 /* Compute this frame's CFA. */
1129 switch (fs->cfa_how)
1131 case CFA_REG_OFFSET:
1132 cfa = _Unwind_GetPtr (&orig_context, fs->cfa_reg);
1133 cfa += fs->cfa_offset;
1134 break;
1136 case CFA_EXP:
1138 const unsigned char *exp = fs->cfa_exp;
1139 _Unwind_Word len;
1141 exp = read_uleb128 (exp, &len);
1142 cfa = (void *) (_Unwind_Ptr)
1143 execute_stack_op (exp, exp + len, &orig_context, 0);
1144 break;
1147 default:
1148 abort ();
1150 context->cfa = cfa;
1152 /* Compute the addresses of all registers saved in this frame. */
1153 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1154 switch (fs->regs.reg[i].how)
1156 case REG_UNSAVED:
1157 break;
1159 case REG_SAVED_OFFSET:
1160 _Unwind_SetGRPtr (context, i,
1161 (void *) (cfa + fs->regs.reg[i].loc.offset));
1162 break;
1164 case REG_SAVED_REG:
1165 _Unwind_SetGRPtr
1166 (context, i,
1167 _Unwind_GetGRPtr (&orig_context, fs->regs.reg[i].loc.reg));
1168 break;
1170 case REG_SAVED_EXP:
1172 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1173 _Unwind_Word len;
1174 _Unwind_Ptr val;
1176 exp = read_uleb128 (exp, &len);
1177 val = execute_stack_op (exp, exp + len, &orig_context,
1178 (_Unwind_Ptr) cfa);
1179 _Unwind_SetGRPtr (context, i, (void *) val);
1181 break;
1184 #ifdef MD_FROB_UPDATE_CONTEXT
1185 MD_FROB_UPDATE_CONTEXT (context, fs);
1186 #endif
1189 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1190 of its caller. Update CONTEXT to refer to the caller as well. Note
1191 that the args_size and lsda members are not updated here, but later in
1192 uw_frame_state_for. */
1194 static void
1195 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1197 uw_update_context_1 (context, fs);
1199 /* Compute the return address now, since the return address column
1200 can change from frame to frame. */
1201 context->ra = __builtin_extract_return_addr
1202 (_Unwind_GetPtr (context, fs->retaddr_column));
1205 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1206 level will be the return address and the CFA. */
1208 #define uw_init_context(CONTEXT) \
1209 do \
1211 /* Do any necessary initialization to access arbitrary stack frames. \
1212 On the SPARC, this means flushing the register windows. */ \
1213 __builtin_unwind_init (); \
1214 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1215 __builtin_return_address (0)); \
1217 while (0)
1219 static inline void
1220 init_dwarf_reg_size_table (void)
1222 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1225 static void
1226 uw_init_context_1 (struct _Unwind_Context *context,
1227 void *outer_cfa, void *outer_ra)
1229 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1230 _Unwind_FrameState fs;
1231 _Unwind_SpTmp sp_slot;
1233 memset (context, 0, sizeof (struct _Unwind_Context));
1234 context->ra = ra;
1236 if (uw_frame_state_for (context, &fs) != _URC_NO_REASON)
1237 abort ();
1239 #if __GTHREADS
1241 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1242 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1243 || dwarf_reg_size_table[0] == 0)
1244 init_dwarf_reg_size_table ();
1246 #else
1247 if (dwarf_reg_size_table[0] == 0)
1248 init_dwarf_reg_size_table ();
1249 #endif
1251 /* Force the frame state to use the known cfa value. */
1252 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1253 fs.cfa_how = CFA_REG_OFFSET;
1254 fs.cfa_reg = __builtin_dwarf_sp_column ();
1255 fs.cfa_offset = 0;
1257 uw_update_context_1 (context, &fs);
1259 /* If the return address column was saved in a register in the
1260 initialization context, then we can't see it in the given
1261 call frame data. So have the initialization context tell us. */
1262 context->ra = __builtin_extract_return_addr (outer_ra);
1266 /* Install TARGET into CURRENT so that we can return to it. This is a
1267 macro because __builtin_eh_return must be invoked in the context of
1268 our caller. */
1270 #define uw_install_context(CURRENT, TARGET) \
1271 do \
1273 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1274 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1275 __builtin_eh_return (offset, handler); \
1277 while (0)
1279 static long
1280 uw_install_context_1 (struct _Unwind_Context *current,
1281 struct _Unwind_Context *target)
1283 long i;
1284 _Unwind_SpTmp sp_slot;
1286 /* If the target frame does not have a saved stack pointer,
1287 then set up the target's CFA. */
1288 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1289 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1291 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1293 void *c = current->reg[i];
1294 void *t = target->reg[i];
1296 if (t && c && t != c)
1297 memcpy (c, t, dwarf_reg_size_table[i]);
1300 /* If the current frame doesn't have a saved stack pointer, then we
1301 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1302 pointer value reloaded. */
1303 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1305 void *target_cfa;
1307 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1309 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1310 if (STACK_GROWS_DOWNWARD)
1311 return target_cfa - current->cfa + target->args_size;
1312 else
1313 return current->cfa - target_cfa - target->args_size;
1315 return 0;
1318 static inline _Unwind_Ptr
1319 uw_identify_context (struct _Unwind_Context *context)
1321 return _Unwind_GetIP (context);
1325 #include "unwind.inc"
1327 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1328 alias (_Unwind_Backtrace);
1329 alias (_Unwind_DeleteException);
1330 alias (_Unwind_FindEnclosingFunction);
1331 alias (_Unwind_ForcedUnwind);
1332 alias (_Unwind_GetDataRelBase);
1333 alias (_Unwind_GetTextRelBase);
1334 alias (_Unwind_GetCFA);
1335 alias (_Unwind_GetGR);
1336 alias (_Unwind_GetIP);
1337 alias (_Unwind_GetLanguageSpecificData);
1338 alias (_Unwind_GetRegionStart);
1339 alias (_Unwind_RaiseException);
1340 alias (_Unwind_Resume);
1341 alias (_Unwind_Resume_or_Rethrow);
1342 alias (_Unwind_SetGR);
1343 alias (_Unwind_SetIP);
1344 #endif
1346 #endif /* !USING_SJLJ_EXCEPTIONS */