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)
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
28 #include "unwind-pe.h"
29 #include "unwind-dw2-fde.h"
33 #ifndef __USING_SJLJ_EXCEPTIONS__
35 #ifndef STACK_GROWS_DOWNWARD
36 #define STACK_GROWS_DOWNWARD 0
38 #undef STACK_GROWS_DOWNWARD
39 #define STACK_GROWS_DOWNWARD 1
42 /* A target can override (perhaps for backward compatibility) how
43 many dwarf2 columns are unwound. */
44 #ifndef DWARF_FRAME_REGISTERS
45 #define DWARF_FRAME_REGISTERS FIRST_PSEUDO_REGISTER
48 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
49 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
50 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
53 #ifndef DWARF_REG_TO_UNWIND_COLUMN
54 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
57 /* A target can do some update context frobbing. */
58 #ifndef MD_FROB_UPDATE_CONTEXT
59 #define MD_FROB_UPDATE_CONTEXT(CTX, FS) do { } while (0)
62 /* This is the register and unwind state for a particular frame. This
63 provides the information necessary to unwind up past a frame and return
65 struct _Unwind_Context
67 void *reg
[DWARF_FRAME_REGISTERS
+1];
71 struct dwarf_eh_bases bases
;
72 _Unwind_Word args_size
;
75 /* Byte size of every register managed by these routines. */
76 static unsigned char dwarf_reg_size_table
[DWARF_FRAME_REGISTERS
];
79 /* The result of interpreting the frame unwind info for a frame.
80 This is all symbolic at this point, as none of the values can
81 be resolved until the target pc is located. */
84 /* Each register save state can be described in terms of a CFA slot,
85 another register, or a location expression. */
86 struct frame_state_reg_info
92 const unsigned char *exp
;
100 } reg
[DWARF_FRAME_REGISTERS
+1];
102 /* Used to implement DW_CFA_remember_state. */
103 struct frame_state_reg_info
*prev
;
106 /* The CFA can be described in terms of a reg+offset or a
107 location expression. */
108 _Unwind_Sword cfa_offset
;
109 _Unwind_Word cfa_reg
;
110 const unsigned char *cfa_exp
;
117 /* The PC described by the current frame state. */
120 /* The information we care about from the CIE/FDE. */
121 _Unwind_Personality_Fn personality
;
122 _Unwind_Sword data_align
;
123 _Unwind_Word code_align
;
124 unsigned char retaddr_column
;
125 unsigned char fde_encoding
;
126 unsigned char lsda_encoding
;
129 } _Unwind_FrameState
;
131 /* Read unaligned data from the instruction buffer. */
136 unsigned u2
__attribute__ ((mode (HI
)));
137 unsigned u4
__attribute__ ((mode (SI
)));
138 unsigned u8
__attribute__ ((mode (DI
)));
139 signed s2
__attribute__ ((mode (HI
)));
140 signed s4
__attribute__ ((mode (SI
)));
141 signed s8
__attribute__ ((mode (DI
)));
142 } __attribute__ ((packed
));
145 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
148 read_1u (const void *p
) { return *(const unsigned char *) p
; }
151 read_1s (const void *p
) { return *(const signed char *) p
; }
154 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
157 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
159 static inline unsigned int
160 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
163 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
165 static inline unsigned long
166 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
168 static inline unsigned long
169 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
171 /* Get the value of register REG as saved in CONTEXT. */
174 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
179 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
180 size
= dwarf_reg_size_table
[index
];
181 ptr
= context
->reg
[index
];
183 /* This will segfault if the register hasn't been saved. */
184 if (size
== sizeof(_Unwind_Ptr
))
185 return * (_Unwind_Ptr
*) ptr
;
187 if (size
== sizeof(_Unwind_Word
))
188 return * (_Unwind_Word
*) ptr
;
194 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
196 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
199 /* Get the value of the CFA as saved in CONTEXT. */
202 _Unwind_GetCFA (struct _Unwind_Context
*context
)
204 return (_Unwind_Ptr
) context
->cfa
;
207 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
210 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
215 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
216 size
= dwarf_reg_size_table
[index
];
217 ptr
= context
->reg
[index
];
219 if (size
== sizeof(_Unwind_Ptr
))
220 * (_Unwind_Ptr
*) ptr
= val
;
221 else if (size
== sizeof(_Unwind_Word
))
222 * (_Unwind_Word
*) ptr
= val
;
227 /* Get the pointer to a register INDEX as saved in CONTEXT. */
230 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
232 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
233 return context
->reg
[index
];
236 /* Set the pointer to a register INDEX as saved in CONTEXT. */
239 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
241 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
242 context
->reg
[index
] = p
;
245 /* Retrieve the return address for CONTEXT. */
248 _Unwind_GetIP (struct _Unwind_Context
*context
)
250 return (_Unwind_Ptr
) context
->ra
;
253 /* Overwrite the return address for CONTEXT with VAL. */
256 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
258 context
->ra
= (void *) val
;
262 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
264 return context
->lsda
;
268 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
270 return (_Unwind_Ptr
) context
->bases
.func
;
274 _Unwind_FindEnclosingFunction (void *pc
)
276 struct dwarf_eh_bases bases
;
277 struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
286 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
288 return (_Unwind_Ptr
) context
->bases
.dbase
;
292 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
294 return (_Unwind_Ptr
) context
->bases
.tbase
;
298 /* Extract any interesting information from the CIE for the translation
299 unit F belongs to. Return a pointer to the byte after the augmentation,
300 or NULL if we encountered an undecipherable augmentation. */
302 static const unsigned char *
303 extract_cie_info (struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
304 _Unwind_FrameState
*fs
)
306 const unsigned char *aug
= cie
->augmentation
;
307 const unsigned char *p
= aug
+ strlen (aug
) + 1;
308 const unsigned char *ret
= NULL
;
311 /* g++ v2 "eh" has pointer immediately following augmentation string,
312 so it must be handled first. */
313 if (aug
[0] == 'e' && aug
[1] == 'h')
315 fs
->eh_ptr
= read_pointer (p
);
316 p
+= sizeof (void *);
320 /* Immediately following the augmentation are the code and
321 data alignment and return address column. */
322 p
= read_uleb128 (p
, &fs
->code_align
);
323 p
= read_sleb128 (p
, &fs
->data_align
);
324 fs
->retaddr_column
= *p
++;
325 fs
->lsda_encoding
= DW_EH_PE_omit
;
327 /* If the augmentation starts with 'z', then a uleb128 immediately
328 follows containing the length of the augmentation field following
332 p
= read_uleb128 (p
, &utmp
);
339 /* Iterate over recognized augmentation subsequences. */
342 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
345 fs
->lsda_encoding
= *p
++;
349 /* "R" indicates a byte indicating how FDE addresses are encoded. */
350 else if (aug
[0] == 'R')
352 fs
->fde_encoding
= *p
++;
356 /* "P" indicates a personality routine in the CIE augmentation. */
357 else if (aug
[0] == 'P')
359 p
= read_encoded_value (context
, *p
, p
+ 1,
360 (_Unwind_Ptr
*) &fs
->personality
);
364 /* Otherwise we have an unknown augmentation string.
365 Bail unless we saw a 'z' prefix. */
370 return ret
? ret
: p
;
374 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
375 onto the stack to start. */
378 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
379 struct _Unwind_Context
*context
, _Unwind_Word initial
)
381 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
387 while (op_ptr
< op_end
)
389 enum dwarf_location_atom op
= *op_ptr
++;
390 _Unwind_Word result
, reg
, utmp
;
391 _Unwind_Sword offset
, stmp
;
427 result
= op
- DW_OP_lit0
;
431 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
432 op_ptr
+= sizeof (void *);
436 result
= read_1u (op_ptr
);
440 result
= read_1s (op_ptr
);
444 result
= read_2u (op_ptr
);
448 result
= read_2s (op_ptr
);
452 result
= read_4u (op_ptr
);
456 result
= read_4s (op_ptr
);
460 result
= read_8u (op_ptr
);
464 result
= read_8s (op_ptr
);
468 op_ptr
= read_uleb128 (op_ptr
, &result
);
471 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
507 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
510 op_ptr
= read_uleb128 (op_ptr
, ®
);
511 result
= _Unwind_GetGR (context
, reg
);
546 op_ptr
= read_sleb128 (op_ptr
, &offset
);
547 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
550 op_ptr
= read_uleb128 (op_ptr
, ®
);
551 op_ptr
= read_sleb128 (op_ptr
, &offset
);
552 result
= _Unwind_GetGR (context
, reg
) + offset
;
558 result
= stack
[stack_elt
- 1];
568 if (offset
>= stack_elt
- 1)
570 result
= stack
[stack_elt
- 1 - offset
];
576 result
= stack
[stack_elt
- 2];
581 _Unwind_Word t1
, t2
, t3
;
585 t1
= stack
[stack_elt
- 1];
586 t2
= stack
[stack_elt
- 2];
587 t3
= stack
[stack_elt
- 3];
588 stack
[stack_elt
- 1] = t2
;
589 stack
[stack_elt
- 2] = t3
;
590 stack
[stack_elt
- 3] = t1
;
595 case DW_OP_deref_size
:
599 case DW_OP_plus_uconst
:
600 /* Unary operations. */
603 result
= stack
[stack_elt
];
609 void *ptr
= (void *) (_Unwind_Ptr
) result
;
610 result
= (_Unwind_Ptr
) read_pointer (ptr
);
614 case DW_OP_deref_size
:
616 void *ptr
= (void *) (_Unwind_Ptr
) result
;
620 result
= read_1u (ptr
);
623 result
= read_2u (ptr
);
626 result
= read_4u (ptr
);
629 result
= read_8u (ptr
);
638 if ((_Unwind_Sword
) result
< 0)
647 case DW_OP_plus_uconst
:
648 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
671 /* Binary operations. */
672 _Unwind_Word first
, second
;
673 if ((stack_elt
-= 2) < 0)
675 second
= stack
[stack_elt
];
676 first
= stack
[stack_elt
+ 1];
681 result
= second
& first
;
684 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
687 result
= second
- first
;
690 result
= (_Unwind_Sword
) second
% (_Unwind_Sword
) first
;
693 result
= second
* first
;
696 result
= second
| first
;
699 result
= second
+ first
;
702 result
= second
<< first
;
705 result
= second
>> first
;
708 result
= (_Unwind_Sword
) second
>> first
;
711 result
= second
^ first
;
714 result
= (_Unwind_Sword
) first
<= (_Unwind_Sword
) second
;
717 result
= (_Unwind_Sword
) first
>= (_Unwind_Sword
) second
;
720 result
= (_Unwind_Sword
) first
== (_Unwind_Sword
) second
;
723 result
= (_Unwind_Sword
) first
< (_Unwind_Sword
) second
;
726 result
= (_Unwind_Sword
) first
> (_Unwind_Sword
) second
;
729 result
= (_Unwind_Sword
) first
!= (_Unwind_Sword
) second
;
739 offset
= read_2s (op_ptr
);
747 offset
= read_2s (op_ptr
);
749 if (stack
[stack_elt
] != 0)
760 /* Most things push a result value. */
761 if ((size_t) stack_elt
>= sizeof(stack
)/sizeof(*stack
))
763 stack
[stack_elt
++] = result
;
767 /* We were executing this program to get a value. It should be
771 return stack
[stack_elt
];
775 /* Decode DWARF 2 call frame information. Takes pointers the
776 instruction sequence to decode, current register information and
777 CIE info, and the PC range to evaluate. */
780 execute_cfa_program (const unsigned char *insn_ptr
,
781 const unsigned char *insn_end
,
782 struct _Unwind_Context
*context
,
783 _Unwind_FrameState
*fs
)
785 struct frame_state_reg_info
*unused_rs
= NULL
;
787 /* Don't allow remember/restore between CIE and FDE programs. */
788 fs
->regs
.prev
= NULL
;
790 /* The comparison with the return address uses < rather than <= because
791 we are only interested in the effects of code before the call; for a
792 noreturn function, the return address may point to unrelated code with
793 a different stack configuration that we are not interested in. We
794 assume that the call itself is unwind info-neutral; if not, or if
795 there are delay instructions that adjust the stack, these must be
796 reflected at the point immediately before the call insn. */
797 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
)
799 unsigned char insn
= *insn_ptr
++;
800 _Unwind_Word reg
, utmp
;
801 _Unwind_Sword offset
, stmp
;
803 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
804 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
805 else if ((insn
& 0xc0) == DW_CFA_offset
)
808 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
809 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
810 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
812 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
814 else if ((insn
& 0xc0) == DW_CFA_restore
)
817 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_UNSAVED
;
822 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
823 insn_ptr
, (_Unwind_Ptr
*) &fs
->pc
);
826 case DW_CFA_advance_loc1
:
827 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
830 case DW_CFA_advance_loc2
:
831 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
834 case DW_CFA_advance_loc4
:
835 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
839 case DW_CFA_offset_extended
:
840 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
841 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
842 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
843 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
845 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
848 case DW_CFA_restore_extended
:
849 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
850 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
853 case DW_CFA_undefined
:
854 case DW_CFA_same_value
:
855 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
861 case DW_CFA_register
:
864 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
865 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
866 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_REG
;
867 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.reg
= reg2
;
871 case DW_CFA_remember_state
:
873 struct frame_state_reg_info
*new_rs
;
877 unused_rs
= unused_rs
->prev
;
880 new_rs
= __builtin_alloca (sizeof (struct frame_state_reg_info
));
883 fs
->regs
.prev
= new_rs
;
887 case DW_CFA_restore_state
:
889 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
891 old_rs
->prev
= unused_rs
;
897 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
898 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
899 fs
->cfa_offset
= utmp
;
900 fs
->cfa_how
= CFA_REG_OFFSET
;
903 case DW_CFA_def_cfa_register
:
904 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
905 fs
->cfa_how
= CFA_REG_OFFSET
;
908 case DW_CFA_def_cfa_offset
:
909 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
910 fs
->cfa_offset
= utmp
;
911 /* cfa_how deliberately not set. */
914 case DW_CFA_def_cfa_expression
:
915 fs
->cfa_exp
= insn_ptr
;
916 fs
->cfa_how
= CFA_EXP
;
917 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
921 case DW_CFA_expression
:
922 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
923 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_EXP
;
924 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
925 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
929 /* From the 2.1 draft. */
930 case DW_CFA_offset_extended_sf
:
931 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
932 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
933 offset
= stmp
* fs
->data_align
;
934 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
936 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
939 case DW_CFA_def_cfa_sf
:
940 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
941 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
942 fs
->cfa_how
= CFA_REG_OFFSET
;
945 case DW_CFA_def_cfa_offset_sf
:
946 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
947 /* cfa_how deliberately not set. */
950 case DW_CFA_GNU_window_save
:
951 /* ??? Hardcoded for SPARC register window configuration. */
952 for (reg
= 16; reg
< 32; ++reg
)
954 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
955 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
959 case DW_CFA_GNU_args_size
:
960 insn_ptr
= read_uleb128 (insn_ptr
, &context
->args_size
);
963 case DW_CFA_GNU_negative_offset_extended
:
964 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
965 older PowerPC code. */
966 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
967 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
968 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
969 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
971 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= -offset
;
980 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
981 its caller and decode it into FS. This function also sets the
982 args_size and lsda members of CONTEXT, as they are really information
983 about the caller's frame. */
985 static _Unwind_Reason_Code
986 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
988 struct dwarf_fde
*fde
;
989 struct dwarf_cie
*cie
;
990 const unsigned char *aug
, *insn
, *end
;
992 memset (fs
, 0, sizeof (*fs
));
993 context
->args_size
= 0;
996 fde
= _Unwind_Find_FDE (context
->ra
- 1, &context
->bases
);
999 /* Couldn't find frame unwind info for this function. Try a
1000 target-specific fallback mechanism. This will necessarily
1001 not provide a personality routine or LSDA. */
1002 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1003 MD_FALLBACK_FRAME_STATE_FOR (context
, fs
, success
);
1004 return _URC_END_OF_STACK
;
1006 return _URC_NO_REASON
;
1008 return _URC_END_OF_STACK
;
1012 fs
->pc
= context
->bases
.func
;
1014 cie
= get_cie (fde
);
1015 insn
= extract_cie_info (cie
, context
, fs
);
1017 /* CIE contained unknown augmentation. */
1018 return _URC_FATAL_PHASE1_ERROR
;
1020 /* First decode all the insns in the CIE. */
1021 end
= (unsigned char *) next_fde ((struct dwarf_fde
*) cie
);
1022 execute_cfa_program (insn
, end
, context
, fs
);
1024 /* Locate augmentation for the fde. */
1025 aug
= (unsigned char *) fde
+ sizeof (*fde
);
1026 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1031 aug
= read_uleb128 (aug
, &i
);
1034 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1035 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
,
1036 (_Unwind_Ptr
*) &context
->lsda
);
1038 /* Then the insns in the FDE up to our target PC. */
1041 end
= (unsigned char *) next_fde (fde
);
1042 execute_cfa_program (insn
, end
, context
, fs
);
1044 return _URC_NO_REASON
;
1047 typedef struct frame_state
1053 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1054 unsigned short cfa_reg
;
1055 unsigned short retaddr_column
;
1056 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1059 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1061 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1062 a given PC_TARGET. The caller should allocate a local variable of
1063 `struct frame_state' and pass its address to STATE_IN. */
1065 struct frame_state
*
1066 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1068 struct _Unwind_Context context
;
1069 _Unwind_FrameState fs
;
1072 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1073 context
.ra
= pc_target
+ 1;
1075 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1078 /* We have no way to pass a location expression for the CFA to our
1079 caller. It wouldn't understand it anyway. */
1080 if (fs
.cfa_how
== CFA_EXP
)
1083 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1085 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1086 switch (state_in
->saved
[reg
])
1089 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1091 case REG_SAVED_OFFSET
:
1092 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1095 state_in
->reg_or_offset
[reg
] = 0;
1100 state_in
->cfa_offset
= fs
.cfa_offset
;
1101 state_in
->cfa_reg
= fs
.cfa_reg
;
1102 state_in
->retaddr_column
= fs
.retaddr_column
;
1103 state_in
->args_size
= context
.args_size
;
1104 state_in
->eh_ptr
= fs
.eh_ptr
;
1109 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1112 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1113 _Unwind_SpTmp
*tmp_sp
)
1115 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1117 if (size
== sizeof(_Unwind_Ptr
))
1118 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1119 else if (size
== sizeof(_Unwind_Word
))
1120 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1123 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1127 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1129 struct _Unwind_Context orig_context
= *context
;
1133 #ifdef EH_RETURN_STACKADJ_RTX
1134 /* Special handling here: Many machines do not use a frame pointer,
1135 and track the CFA only through offsets from the stack pointer from
1136 one frame to the next. In this case, the stack pointer is never
1137 stored, so it has no saved address in the context. What we do
1138 have is the CFA from the previous stack frame.
1140 In very special situations (such as unwind info for signal return),
1141 there may be location expressions that use the stack pointer as well.
1143 Do this conditionally for one frame. This allows the unwind info
1144 for one frame to save a copy of the stack pointer from the previous
1145 frame, and be able to use much easier CFA mechanisms to do it.
1146 Always zap the saved stack pointer value for the next frame; carrying
1147 the value over from one frame to another doesn't make sense. */
1149 _Unwind_SpTmp tmp_sp
;
1151 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1152 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1153 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1156 /* Compute this frame's CFA. */
1157 switch (fs
->cfa_how
)
1159 case CFA_REG_OFFSET
:
1160 cfa
= _Unwind_GetPtr (&orig_context
, fs
->cfa_reg
);
1161 cfa
+= fs
->cfa_offset
;
1166 const unsigned char *exp
= fs
->cfa_exp
;
1169 exp
= read_uleb128 (exp
, &len
);
1170 cfa
= (void *) (_Unwind_Ptr
)
1171 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1180 /* Compute the addresses of all registers saved in this frame. */
1181 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1182 switch (fs
->regs
.reg
[i
].how
)
1187 case REG_SAVED_OFFSET
:
1188 _Unwind_SetGRPtr (context
, i
,
1189 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1195 _Unwind_GetGRPtr (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
));
1200 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1204 exp
= read_uleb128 (exp
, &len
);
1205 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1207 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1212 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1215 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1216 of its caller. Update CONTEXT to refer to the caller as well. Note
1217 that the args_size and lsda members are not updated here, but later in
1218 uw_frame_state_for. */
1221 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1223 uw_update_context_1 (context
, fs
);
1225 /* Compute the return address now, since the return address column
1226 can change from frame to frame. */
1227 context
->ra
= __builtin_extract_return_addr
1228 (_Unwind_GetPtr (context
, fs
->retaddr_column
));
1231 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1232 level will be the return address and the CFA. */
1234 #define uw_init_context(CONTEXT) \
1237 /* Do any necessary initialization to access arbitrary stack frames. \
1238 On the SPARC, this means flushing the register windows. */ \
1239 __builtin_unwind_init (); \
1240 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1241 __builtin_return_address (0)); \
1246 init_dwarf_reg_size_table (void)
1248 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1252 uw_init_context_1 (struct _Unwind_Context
*context
,
1253 void *outer_cfa
, void *outer_ra
)
1255 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1256 _Unwind_FrameState fs
;
1257 _Unwind_SpTmp sp_slot
;
1259 memset (context
, 0, sizeof (struct _Unwind_Context
));
1262 if (uw_frame_state_for (context
, &fs
) != _URC_NO_REASON
)
1267 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1268 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1269 || dwarf_reg_size_table
[0] == 0)
1270 init_dwarf_reg_size_table ();
1273 if (dwarf_reg_size_table
[0] == 0)
1274 init_dwarf_reg_size_table ();
1277 /* Force the frame state to use the known cfa value. */
1278 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1279 fs
.cfa_how
= CFA_REG_OFFSET
;
1280 fs
.cfa_reg
= __builtin_dwarf_sp_column ();
1283 uw_update_context_1 (context
, &fs
);
1285 /* If the return address column was saved in a register in the
1286 initialization context, then we can't see it in the given
1287 call frame data. So have the initialization context tell us. */
1288 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1292 /* Install TARGET into CURRENT so that we can return to it. This is a
1293 macro because __builtin_eh_return must be invoked in the context of
1296 #define uw_install_context(CURRENT, TARGET) \
1299 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1300 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1301 __builtin_eh_return (offset, handler); \
1306 uw_install_context_1 (struct _Unwind_Context
*current
,
1307 struct _Unwind_Context
*target
)
1311 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1313 void *c
= current
->reg
[i
];
1314 void *t
= target
->reg
[i
];
1316 if (t
&& c
&& t
!= c
)
1317 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1320 #ifdef EH_RETURN_STACKADJ_RTX
1324 /* If the last frame records a saved stack pointer, use it. */
1325 if (_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1326 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1328 target_cfa
= target
->cfa
;
1330 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1331 if (STACK_GROWS_DOWNWARD
)
1332 return target_cfa
- current
->cfa
+ target
->args_size
;
1334 return current
->cfa
- target_cfa
- target
->args_size
;
1341 static inline _Unwind_Ptr
1342 uw_identify_context (struct _Unwind_Context
*context
)
1344 return _Unwind_GetIP (context
);
1348 #include "unwind.inc"
1350 #endif /* !USING_SJLJ_EXCEPTIONS */