1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 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
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, 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "coretypes.h"
37 #ifdef __USING_SJLJ_EXCEPTIONS__
38 # define NO_SIZE_OF_ENCODED_VALUE
40 #include "unwind-pe.h"
41 #include "unwind-dw2-fde.h"
43 #include "unwind-dw2.h"
45 #ifndef __USING_SJLJ_EXCEPTIONS__
47 #ifndef STACK_GROWS_DOWNWARD
48 #define STACK_GROWS_DOWNWARD 0
50 #undef STACK_GROWS_DOWNWARD
51 #define STACK_GROWS_DOWNWARD 1
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
59 #ifndef DWARF_REG_TO_UNWIND_COLUMN
60 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
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
66 struct _Unwind_Context
68 void *reg
[DWARF_FRAME_REGISTERS
+1];
72 struct dwarf_eh_bases bases
;
73 _Unwind_Word args_size
;
75 char by_value
[DWARF_FRAME_REGISTERS
+1];
78 /* Byte size of every register managed by these routines. */
79 static unsigned char dwarf_reg_size_table
[DWARF_FRAME_REGISTERS
+1];
82 /* Read unaligned data from the instruction buffer. */
87 unsigned u2
__attribute__ ((mode (HI
)));
88 unsigned u4
__attribute__ ((mode (SI
)));
89 unsigned u8
__attribute__ ((mode (DI
)));
90 signed s2
__attribute__ ((mode (HI
)));
91 signed s4
__attribute__ ((mode (SI
)));
92 signed s8
__attribute__ ((mode (DI
)));
93 } __attribute__ ((packed
));
96 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
99 read_1u (const void *p
) { return *(const unsigned char *) p
; }
102 read_1s (const void *p
) { return *(const signed char *) p
; }
105 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
108 read_2s (const void *p
) { const union unaligned
*up
= p
; return up
->s2
; }
110 static inline unsigned int
111 read_4u (const void *p
) { const union unaligned
*up
= p
; return up
->u4
; }
114 read_4s (const void *p
) { const union unaligned
*up
= p
; return up
->s4
; }
116 static inline unsigned long
117 read_8u (const void *p
) { const union unaligned
*up
= p
; return up
->u8
; }
119 static inline unsigned long
120 read_8s (const void *p
) { const union unaligned
*up
= p
; return up
->s8
; }
122 /* Get the value of register INDEX as saved in CONTEXT. */
125 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
130 #ifdef DWARF_ZERO_REG
131 if (index
== DWARF_ZERO_REG
)
135 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
136 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
137 size
= dwarf_reg_size_table
[index
];
138 ptr
= context
->reg
[index
];
140 if (context
->by_value
[index
])
141 return (_Unwind_Word
) (_Unwind_Internal_Ptr
) ptr
;
143 /* This will segfault if the register hasn't been saved. */
144 if (size
== sizeof(_Unwind_Ptr
))
145 return * (_Unwind_Ptr
*) ptr
;
148 gcc_assert (size
== sizeof(_Unwind_Word
));
149 return * (_Unwind_Word
*) ptr
;
154 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
156 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
159 /* Get the value of the CFA as saved in CONTEXT. */
162 _Unwind_GetCFA (struct _Unwind_Context
*context
)
164 return (_Unwind_Ptr
) context
->cfa
;
167 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
170 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
175 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
176 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
177 size
= dwarf_reg_size_table
[index
];
179 if (context
->by_value
[index
])
181 context
->reg
[index
] = (void *) (_Unwind_Internal_Ptr
) val
;
185 ptr
= context
->reg
[index
];
187 if (size
== sizeof(_Unwind_Ptr
))
188 * (_Unwind_Ptr
*) ptr
= val
;
191 gcc_assert (size
== sizeof(_Unwind_Word
));
192 * (_Unwind_Word
*) ptr
= val
;
196 /* Get the pointer to a register INDEX as saved in CONTEXT. */
199 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
201 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
202 if (context
->by_value
[index
])
203 return &context
->reg
[index
];
204 return context
->reg
[index
];
207 /* Set the pointer to a register INDEX as saved in CONTEXT. */
210 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
212 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
213 context
->by_value
[index
] = 0;
214 context
->reg
[index
] = p
;
217 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
220 _Unwind_SetGRValue (struct _Unwind_Context
*context
, int index
,
223 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
224 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
225 gcc_assert (dwarf_reg_size_table
[index
] == sizeof (_Unwind_Ptr
));
227 context
->by_value
[index
] = 1;
228 context
->reg
[index
] = (void *) (_Unwind_Internal_Ptr
) val
;
231 /* Return nonzero if register INDEX is stored by value rather than
235 _Unwind_GRByValue (struct _Unwind_Context
*context
, int index
)
237 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
238 return context
->by_value
[index
];
241 /* Retrieve the return address for CONTEXT. */
244 _Unwind_GetIP (struct _Unwind_Context
*context
)
246 return (_Unwind_Ptr
) context
->ra
;
249 /* Retrieve the return address and flag whether that IP is before
250 or after first not yet fully executed instruction. */
253 _Unwind_GetIPInfo (struct _Unwind_Context
*context
, int *ip_before_insn
)
255 *ip_before_insn
= context
->signal_frame
!= 0;
256 return (_Unwind_Ptr
) context
->ra
;
259 /* Overwrite the return address for CONTEXT with VAL. */
262 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
264 context
->ra
= (void *) val
;
268 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
270 return context
->lsda
;
274 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
276 return (_Unwind_Ptr
) context
->bases
.func
;
280 _Unwind_FindEnclosingFunction (void *pc
)
282 struct dwarf_eh_bases bases
;
283 const struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
292 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
294 return (_Unwind_Ptr
) context
->bases
.dbase
;
298 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
300 return (_Unwind_Ptr
) context
->bases
.tbase
;
304 #ifdef MD_UNWIND_SUPPORT
305 #include MD_UNWIND_SUPPORT
308 /* Extract any interesting information from the CIE for the translation
309 unit F belongs to. Return a pointer to the byte after the augmentation,
310 or NULL if we encountered an undecipherable augmentation. */
312 static const unsigned char *
313 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
314 _Unwind_FrameState
*fs
)
316 const unsigned char *aug
= cie
->augmentation
;
317 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
318 const unsigned char *ret
= NULL
;
321 /* g++ v2 "eh" has pointer immediately following augmentation string,
322 so it must be handled first. */
323 if (aug
[0] == 'e' && aug
[1] == 'h')
325 fs
->eh_ptr
= read_pointer (p
);
326 p
+= sizeof (void *);
330 /* Immediately following the augmentation are the code and
331 data alignment and return address column. */
332 p
= read_uleb128 (p
, &fs
->code_align
);
333 p
= read_sleb128 (p
, &fs
->data_align
);
334 if (cie
->version
== 1)
335 fs
->retaddr_column
= *p
++;
337 p
= read_uleb128 (p
, &fs
->retaddr_column
);
338 fs
->lsda_encoding
= DW_EH_PE_omit
;
340 /* If the augmentation starts with 'z', then a uleb128 immediately
341 follows containing the length of the augmentation field following
345 p
= read_uleb128 (p
, &utmp
);
352 /* Iterate over recognized augmentation subsequences. */
355 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
358 fs
->lsda_encoding
= *p
++;
362 /* "R" indicates a byte indicating how FDE addresses are encoded. */
363 else if (aug
[0] == 'R')
365 fs
->fde_encoding
= *p
++;
369 /* "P" indicates a personality routine in the CIE augmentation. */
370 else if (aug
[0] == 'P')
372 _Unwind_Ptr personality
;
374 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
375 fs
->personality
= (_Unwind_Personality_Fn
) personality
;
379 /* "S" indicates a signal frame. */
380 else if (aug
[0] == 'S')
382 fs
->signal_frame
= 1;
386 /* Otherwise we have an unknown augmentation string.
387 Bail unless we saw a 'z' prefix. */
392 return ret
? ret
: p
;
396 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
397 onto the stack to start. */
400 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
401 struct _Unwind_Context
*context
, _Unwind_Word initial
)
403 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
409 while (op_ptr
< op_end
)
411 enum dwarf_location_atom op
= *op_ptr
++;
412 _Unwind_Word result
, reg
, utmp
;
413 _Unwind_Sword offset
, stmp
;
449 result
= op
- DW_OP_lit0
;
453 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
454 op_ptr
+= sizeof (void *);
458 result
= read_1u (op_ptr
);
462 result
= read_1s (op_ptr
);
466 result
= read_2u (op_ptr
);
470 result
= read_2s (op_ptr
);
474 result
= read_4u (op_ptr
);
478 result
= read_4s (op_ptr
);
482 result
= read_8u (op_ptr
);
486 result
= read_8s (op_ptr
);
490 op_ptr
= read_uleb128 (op_ptr
, &result
);
493 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
529 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
532 op_ptr
= read_uleb128 (op_ptr
, ®
);
533 result
= _Unwind_GetGR (context
, reg
);
568 op_ptr
= read_sleb128 (op_ptr
, &offset
);
569 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
572 op_ptr
= read_uleb128 (op_ptr
, ®
);
573 op_ptr
= read_sleb128 (op_ptr
, &offset
);
574 result
= _Unwind_GetGR (context
, reg
) + offset
;
578 gcc_assert (stack_elt
);
579 result
= stack
[stack_elt
- 1];
583 gcc_assert (stack_elt
);
589 gcc_assert (offset
< stack_elt
- 1);
590 result
= stack
[stack_elt
- 1 - offset
];
594 gcc_assert (stack_elt
>= 2);
595 result
= stack
[stack_elt
- 2];
600 _Unwind_Word t1
, t2
, t3
;
602 gcc_assert (stack_elt
>= 3);
603 t1
= stack
[stack_elt
- 1];
604 t2
= stack
[stack_elt
- 2];
605 t3
= stack
[stack_elt
- 3];
606 stack
[stack_elt
- 1] = t2
;
607 stack
[stack_elt
- 2] = t3
;
608 stack
[stack_elt
- 3] = t1
;
613 case DW_OP_deref_size
:
617 case DW_OP_plus_uconst
:
618 /* Unary operations. */
619 gcc_assert (stack_elt
);
622 result
= stack
[stack_elt
];
628 void *ptr
= (void *) (_Unwind_Ptr
) result
;
629 result
= (_Unwind_Ptr
) read_pointer (ptr
);
633 case DW_OP_deref_size
:
635 void *ptr
= (void *) (_Unwind_Ptr
) result
;
639 result
= read_1u (ptr
);
642 result
= read_2u (ptr
);
645 result
= read_4u (ptr
);
648 result
= read_8u (ptr
);
657 if ((_Unwind_Sword
) result
< 0)
666 case DW_OP_plus_uconst
:
667 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
694 /* Binary operations. */
695 _Unwind_Word first
, second
;
696 gcc_assert (stack_elt
>= 2);
699 second
= stack
[stack_elt
];
700 first
= stack
[stack_elt
+ 1];
705 result
= second
& first
;
708 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
711 result
= second
- first
;
714 result
= (_Unwind_Sword
) second
% (_Unwind_Sword
) first
;
717 result
= second
* first
;
720 result
= second
| first
;
723 result
= second
+ first
;
726 result
= second
<< first
;
729 result
= second
>> first
;
732 result
= (_Unwind_Sword
) second
>> first
;
735 result
= second
^ first
;
738 result
= (_Unwind_Sword
) first
<= (_Unwind_Sword
) second
;
741 result
= (_Unwind_Sword
) first
>= (_Unwind_Sword
) second
;
744 result
= (_Unwind_Sword
) first
== (_Unwind_Sword
) second
;
747 result
= (_Unwind_Sword
) first
< (_Unwind_Sword
) second
;
750 result
= (_Unwind_Sword
) first
> (_Unwind_Sword
) second
;
753 result
= (_Unwind_Sword
) first
!= (_Unwind_Sword
) second
;
763 offset
= read_2s (op_ptr
);
769 gcc_assert (stack_elt
);
772 offset
= read_2s (op_ptr
);
774 if (stack
[stack_elt
] != 0)
785 /* Most things push a result value. */
786 gcc_assert ((size_t) stack_elt
< sizeof(stack
)/sizeof(*stack
));
787 stack
[stack_elt
++] = result
;
791 /* We were executing this program to get a value. It should be
793 gcc_assert (stack_elt
);
795 return stack
[stack_elt
];
799 /* Decode DWARF 2 call frame information. Takes pointers the
800 instruction sequence to decode, current register information and
801 CIE info, and the PC range to evaluate. */
804 execute_cfa_program (const unsigned char *insn_ptr
,
805 const unsigned char *insn_end
,
806 struct _Unwind_Context
*context
,
807 _Unwind_FrameState
*fs
)
809 struct frame_state_reg_info
*unused_rs
= NULL
;
811 /* Don't allow remember/restore between CIE and FDE programs. */
812 fs
->regs
.prev
= NULL
;
814 /* The comparison with the return address uses < rather than <= because
815 we are only interested in the effects of code before the call; for a
816 noreturn function, the return address may point to unrelated code with
817 a different stack configuration that we are not interested in. We
818 assume that the call itself is unwind info-neutral; if not, or if
819 there are delay instructions that adjust the stack, these must be
820 reflected at the point immediately before the call insn.
821 In signal frames, return address is after last completed instruction,
822 so we add 1 to return address to make the comparison <=. */
823 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
+ context
->signal_frame
)
825 unsigned char insn
= *insn_ptr
++;
826 _Unwind_Word reg
, utmp
;
827 _Unwind_Sword offset
, stmp
;
829 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
830 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
831 else if ((insn
& 0xc0) == DW_CFA_offset
)
834 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
835 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
836 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
838 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
840 else if ((insn
& 0xc0) == DW_CFA_restore
)
843 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_UNSAVED
;
851 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
853 fs
->pc
= (void *) pc
;
857 case DW_CFA_advance_loc1
:
858 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
861 case DW_CFA_advance_loc2
:
862 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
865 case DW_CFA_advance_loc4
:
866 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
870 case DW_CFA_offset_extended
:
871 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
872 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
873 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
874 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
876 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
879 case DW_CFA_restore_extended
:
880 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
881 /* FIXME, this is wrong; the CIE might have said that the
882 register was saved somewhere. */
883 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
886 case DW_CFA_undefined
:
887 case DW_CFA_same_value
:
888 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
889 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
895 case DW_CFA_register
:
898 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
899 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
900 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_REG
;
901 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.reg
= reg2
;
905 case DW_CFA_remember_state
:
907 struct frame_state_reg_info
*new_rs
;
911 unused_rs
= unused_rs
->prev
;
914 new_rs
= alloca (sizeof (struct frame_state_reg_info
));
917 fs
->regs
.prev
= new_rs
;
921 case DW_CFA_restore_state
:
923 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
925 old_rs
->prev
= unused_rs
;
931 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
932 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
933 fs
->cfa_offset
= utmp
;
934 fs
->cfa_how
= CFA_REG_OFFSET
;
937 case DW_CFA_def_cfa_register
:
938 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
939 fs
->cfa_how
= CFA_REG_OFFSET
;
942 case DW_CFA_def_cfa_offset
:
943 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
944 fs
->cfa_offset
= utmp
;
945 /* cfa_how deliberately not set. */
948 case DW_CFA_def_cfa_expression
:
949 fs
->cfa_exp
= insn_ptr
;
950 fs
->cfa_how
= CFA_EXP
;
951 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
955 case DW_CFA_expression
:
956 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
957 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_EXP
;
958 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
959 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
964 case DW_CFA_offset_extended_sf
:
965 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
966 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
967 offset
= stmp
* fs
->data_align
;
968 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
970 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
973 case DW_CFA_def_cfa_sf
:
974 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
975 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
976 fs
->cfa_how
= CFA_REG_OFFSET
;
977 fs
->cfa_offset
*= fs
->data_align
;
980 case DW_CFA_def_cfa_offset_sf
:
981 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
982 fs
->cfa_offset
*= fs
->data_align
;
983 /* cfa_how deliberately not set. */
986 case DW_CFA_val_offset
:
987 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
988 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
989 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
990 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
991 = REG_SAVED_VAL_OFFSET
;
992 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
995 case DW_CFA_val_offset_sf
:
996 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
997 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
998 offset
= stmp
* fs
->data_align
;
999 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1000 = REG_SAVED_VAL_OFFSET
;
1001 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
1004 case DW_CFA_val_expression
:
1005 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1006 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1007 = REG_SAVED_VAL_EXP
;
1008 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
1009 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1013 case DW_CFA_GNU_window_save
:
1014 /* ??? Hardcoded for SPARC register window configuration. */
1015 for (reg
= 16; reg
< 32; ++reg
)
1017 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
1018 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
1022 case DW_CFA_GNU_args_size
:
1023 insn_ptr
= read_uleb128 (insn_ptr
, &context
->args_size
);
1026 case DW_CFA_GNU_negative_offset_extended
:
1027 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1028 older PowerPC code. */
1029 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
1030 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
1031 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
1032 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
1034 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= -offset
;
1043 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1044 its caller and decode it into FS. This function also sets the
1045 args_size and lsda members of CONTEXT, as they are really information
1046 about the caller's frame. */
1048 static _Unwind_Reason_Code
1049 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1051 const struct dwarf_fde
*fde
;
1052 const struct dwarf_cie
*cie
;
1053 const unsigned char *aug
, *insn
, *end
;
1055 memset (fs
, 0, sizeof (*fs
));
1056 context
->args_size
= 0;
1059 if (context
->ra
== 0)
1060 return _URC_END_OF_STACK
;
1062 fde
= _Unwind_Find_FDE (context
->ra
+ context
->signal_frame
- 1,
1066 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1067 /* Couldn't find frame unwind info for this function. Try a
1068 target-specific fallback mechanism. This will necessarily
1069 not provide a personality routine or LSDA. */
1070 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
1072 return _URC_END_OF_STACK
;
1076 fs
->pc
= context
->bases
.func
;
1078 cie
= get_cie (fde
);
1079 insn
= extract_cie_info (cie
, context
, fs
);
1081 /* CIE contained unknown augmentation. */
1082 return _URC_FATAL_PHASE1_ERROR
;
1084 /* First decode all the insns in the CIE. */
1085 end
= (unsigned char *) next_fde ((struct dwarf_fde
*) cie
);
1086 execute_cfa_program (insn
, end
, context
, fs
);
1088 /* Locate augmentation for the fde. */
1089 aug
= (unsigned char *) fde
+ sizeof (*fde
);
1090 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1095 aug
= read_uleb128 (aug
, &i
);
1098 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1102 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1103 context
->lsda
= (void *) lsda
;
1106 /* Then the insns in the FDE up to our target PC. */
1109 end
= (unsigned char *) next_fde (fde
);
1110 execute_cfa_program (insn
, end
, context
, fs
);
1112 return _URC_NO_REASON
;
1115 typedef struct frame_state
1121 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1122 unsigned short cfa_reg
;
1123 unsigned short retaddr_column
;
1124 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1127 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1129 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1130 a given PC_TARGET. The caller should allocate a local variable of
1131 `struct frame_state' and pass its address to STATE_IN. */
1133 struct frame_state
*
1134 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1136 struct _Unwind_Context context
;
1137 _Unwind_FrameState fs
;
1140 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1141 context
.ra
= pc_target
+ 1;
1143 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1146 /* We have no way to pass a location expression for the CFA to our
1147 caller. It wouldn't understand it anyway. */
1148 if (fs
.cfa_how
== CFA_EXP
)
1151 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1153 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1154 switch (state_in
->saved
[reg
])
1157 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1159 case REG_SAVED_OFFSET
:
1160 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1163 state_in
->reg_or_offset
[reg
] = 0;
1168 state_in
->cfa_offset
= fs
.cfa_offset
;
1169 state_in
->cfa_reg
= fs
.cfa_reg
;
1170 state_in
->retaddr_column
= fs
.retaddr_column
;
1171 state_in
->args_size
= context
.args_size
;
1172 state_in
->eh_ptr
= fs
.eh_ptr
;
1177 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1180 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1181 _Unwind_SpTmp
*tmp_sp
)
1183 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1185 if (size
== sizeof(_Unwind_Ptr
))
1186 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1189 gcc_assert (size
== sizeof(_Unwind_Word
));
1190 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1192 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1196 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1198 struct _Unwind_Context orig_context
= *context
;
1202 #ifdef EH_RETURN_STACKADJ_RTX
1203 /* Special handling here: Many machines do not use a frame pointer,
1204 and track the CFA only through offsets from the stack pointer from
1205 one frame to the next. In this case, the stack pointer is never
1206 stored, so it has no saved address in the context. What we do
1207 have is the CFA from the previous stack frame.
1209 In very special situations (such as unwind info for signal return),
1210 there may be location expressions that use the stack pointer as well.
1212 Do this conditionally for one frame. This allows the unwind info
1213 for one frame to save a copy of the stack pointer from the previous
1214 frame, and be able to use much easier CFA mechanisms to do it.
1215 Always zap the saved stack pointer value for the next frame; carrying
1216 the value over from one frame to another doesn't make sense. */
1218 _Unwind_SpTmp tmp_sp
;
1220 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1221 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1222 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1225 /* Compute this frame's CFA. */
1226 switch (fs
->cfa_how
)
1228 case CFA_REG_OFFSET
:
1229 cfa
= _Unwind_GetPtr (&orig_context
, fs
->cfa_reg
);
1230 cfa
+= fs
->cfa_offset
;
1235 const unsigned char *exp
= fs
->cfa_exp
;
1238 exp
= read_uleb128 (exp
, &len
);
1239 cfa
= (void *) (_Unwind_Ptr
)
1240 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1249 /* Compute the addresses of all registers saved in this frame. */
1250 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1251 switch (fs
->regs
.reg
[i
].how
)
1256 case REG_SAVED_OFFSET
:
1257 _Unwind_SetGRPtr (context
, i
,
1258 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1262 if (_Unwind_GRByValue (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
))
1263 _Unwind_SetGRValue (context
, i
,
1264 _Unwind_GetGR (&orig_context
,
1265 fs
->regs
.reg
[i
].loc
.reg
));
1267 _Unwind_SetGRPtr (context
, i
,
1268 _Unwind_GetGRPtr (&orig_context
,
1269 fs
->regs
.reg
[i
].loc
.reg
));
1274 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1278 exp
= read_uleb128 (exp
, &len
);
1279 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1281 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1285 case REG_SAVED_VAL_OFFSET
:
1286 _Unwind_SetGRValue (context
, i
,
1287 (_Unwind_Internal_Ptr
)
1288 (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1291 case REG_SAVED_VAL_EXP
:
1293 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1297 exp
= read_uleb128 (exp
, &len
);
1298 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1300 _Unwind_SetGRValue (context
, i
, val
);
1305 context
->signal_frame
= fs
->signal_frame
;
1307 #ifdef MD_FROB_UPDATE_CONTEXT
1308 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1312 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1313 of its caller. Update CONTEXT to refer to the caller as well. Note
1314 that the args_size and lsda members are not updated here, but later in
1315 uw_frame_state_for. */
1318 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1320 uw_update_context_1 (context
, fs
);
1322 /* Compute the return address now, since the return address column
1323 can change from frame to frame. */
1324 context
->ra
= __builtin_extract_return_addr
1325 (_Unwind_GetPtr (context
, fs
->retaddr_column
));
1329 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1331 uw_update_context (context
, fs
);
1334 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1335 level will be the return address and the CFA. */
1337 #define uw_init_context(CONTEXT) \
1340 /* Do any necessary initialization to access arbitrary stack frames. \
1341 On the SPARC, this means flushing the register windows. */ \
1342 __builtin_unwind_init (); \
1343 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1344 __builtin_return_address (0)); \
1349 init_dwarf_reg_size_table (void)
1351 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1355 uw_init_context_1 (struct _Unwind_Context
*context
,
1356 void *outer_cfa
, void *outer_ra
)
1358 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1359 _Unwind_FrameState fs
;
1360 _Unwind_SpTmp sp_slot
;
1361 _Unwind_Reason_Code code
;
1363 memset (context
, 0, sizeof (struct _Unwind_Context
));
1366 code
= uw_frame_state_for (context
, &fs
);
1367 gcc_assert (code
== _URC_NO_REASON
);
1371 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1372 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1373 || dwarf_reg_size_table
[0] == 0)
1374 init_dwarf_reg_size_table ();
1377 if (dwarf_reg_size_table
[0] == 0)
1378 init_dwarf_reg_size_table ();
1381 /* Force the frame state to use the known cfa value. */
1382 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1383 fs
.cfa_how
= CFA_REG_OFFSET
;
1384 fs
.cfa_reg
= __builtin_dwarf_sp_column ();
1387 uw_update_context_1 (context
, &fs
);
1389 /* If the return address column was saved in a register in the
1390 initialization context, then we can't see it in the given
1391 call frame data. So have the initialization context tell us. */
1392 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1396 /* Install TARGET into CURRENT so that we can return to it. This is a
1397 macro because __builtin_eh_return must be invoked in the context of
1400 #define uw_install_context(CURRENT, TARGET) \
1403 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1404 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1405 __builtin_eh_return (offset, handler); \
1410 uw_install_context_1 (struct _Unwind_Context
*current
,
1411 struct _Unwind_Context
*target
)
1414 _Unwind_SpTmp sp_slot
;
1416 /* If the target frame does not have a saved stack pointer,
1417 then set up the target's CFA. */
1418 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1419 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1421 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1423 void *c
= current
->reg
[i
];
1424 void *t
= target
->reg
[i
];
1426 gcc_assert (current
->by_value
[i
] == 0);
1427 if (target
->by_value
[i
] && c
)
1431 if (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Word
))
1433 w
= (_Unwind_Internal_Ptr
) t
;
1434 memcpy (c
, &w
, sizeof (_Unwind_Word
));
1438 gcc_assert (dwarf_reg_size_table
[i
] == sizeof (_Unwind_Ptr
));
1439 p
= (_Unwind_Internal_Ptr
) t
;
1440 memcpy (c
, &p
, sizeof (_Unwind_Ptr
));
1443 else if (t
&& c
&& t
!= c
)
1444 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1447 /* If the current frame doesn't have a saved stack pointer, then we
1448 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1449 pointer value reloaded. */
1450 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1454 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1456 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1457 if (STACK_GROWS_DOWNWARD
)
1458 return target_cfa
- current
->cfa
+ target
->args_size
;
1460 return current
->cfa
- target_cfa
- target
->args_size
;
1465 static inline _Unwind_Ptr
1466 uw_identify_context (struct _Unwind_Context
*context
)
1468 return _Unwind_GetIP (context
);
1472 #include "unwind.inc"
1474 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1475 alias (_Unwind_Backtrace
);
1476 alias (_Unwind_DeleteException
);
1477 alias (_Unwind_FindEnclosingFunction
);
1478 alias (_Unwind_ForcedUnwind
);
1479 alias (_Unwind_GetDataRelBase
);
1480 alias (_Unwind_GetTextRelBase
);
1481 alias (_Unwind_GetCFA
);
1482 alias (_Unwind_GetGR
);
1483 alias (_Unwind_GetIP
);
1484 alias (_Unwind_GetLanguageSpecificData
);
1485 alias (_Unwind_GetRegionStart
);
1486 alias (_Unwind_RaiseException
);
1487 alias (_Unwind_Resume
);
1488 alias (_Unwind_Resume_or_Rethrow
);
1489 alias (_Unwind_SetGR
);
1490 alias (_Unwind_SetIP
);
1493 #endif /* !USING_SJLJ_EXCEPTIONS */