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)
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
;
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. */
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
));
94 read_pointer (const void *p
) { const union unaligned
*up
= p
; return up
->p
; }
97 read_1u (const void *p
) { return *(const unsigned char *) p
; }
100 read_1s (const void *p
) { return *(const signed char *) p
; }
103 read_2u (const void *p
) { const union unaligned
*up
= p
; return up
->u2
; }
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
; }
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. */
123 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
128 #ifdef DWARF_ZERO_REG
129 if (index
== DWARF_ZERO_REG
)
133 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
134 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
135 size
= dwarf_reg_size_table
[index
];
136 ptr
= context
->reg
[index
];
138 /* This will segfault if the register hasn't been saved. */
139 if (size
== sizeof(_Unwind_Ptr
))
140 return * (_Unwind_Ptr
*) ptr
;
143 gcc_assert (size
== sizeof(_Unwind_Word
));
144 return * (_Unwind_Word
*) ptr
;
149 _Unwind_GetPtr (struct _Unwind_Context
*context
, int index
)
151 return (void *)(_Unwind_Ptr
) _Unwind_GetGR (context
, index
);
154 /* Get the value of the CFA as saved in CONTEXT. */
157 _Unwind_GetCFA (struct _Unwind_Context
*context
)
159 return (_Unwind_Ptr
) context
->cfa
;
162 /* Overwrite the saved value for register REG in CONTEXT with VAL. */
165 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
170 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
171 gcc_assert (index
< (int) sizeof(dwarf_reg_size_table
));
172 size
= dwarf_reg_size_table
[index
];
173 ptr
= context
->reg
[index
];
175 if (size
== sizeof(_Unwind_Ptr
))
176 * (_Unwind_Ptr
*) ptr
= val
;
179 gcc_assert (size
== sizeof(_Unwind_Word
));
180 * (_Unwind_Word
*) ptr
= val
;
184 /* Get the pointer to a register INDEX as saved in CONTEXT. */
187 _Unwind_GetGRPtr (struct _Unwind_Context
*context
, int index
)
189 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
190 return context
->reg
[index
];
193 /* Set the pointer to a register INDEX as saved in CONTEXT. */
196 _Unwind_SetGRPtr (struct _Unwind_Context
*context
, int index
, void *p
)
198 index
= DWARF_REG_TO_UNWIND_COLUMN (index
);
199 context
->reg
[index
] = p
;
202 /* Retrieve the return address for CONTEXT. */
205 _Unwind_GetIP (struct _Unwind_Context
*context
)
207 return (_Unwind_Ptr
) context
->ra
;
210 /* Overwrite the return address for CONTEXT with VAL. */
213 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
215 context
->ra
= (void *) val
;
219 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
221 return context
->lsda
;
225 _Unwind_GetRegionStart (struct _Unwind_Context
*context
)
227 return (_Unwind_Ptr
) context
->bases
.func
;
231 _Unwind_FindEnclosingFunction (void *pc
)
233 struct dwarf_eh_bases bases
;
234 const struct dwarf_fde
*fde
= _Unwind_Find_FDE (pc
-1, &bases
);
243 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
)
245 return (_Unwind_Ptr
) context
->bases
.dbase
;
249 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
)
251 return (_Unwind_Ptr
) context
->bases
.tbase
;
255 #ifdef MD_UNWIND_SUPPORT
256 #include MD_UNWIND_SUPPORT
259 /* Extract any interesting information from the CIE for the translation
260 unit F belongs to. Return a pointer to the byte after the augmentation,
261 or NULL if we encountered an undecipherable augmentation. */
263 static const unsigned char *
264 extract_cie_info (const struct dwarf_cie
*cie
, struct _Unwind_Context
*context
,
265 _Unwind_FrameState
*fs
)
267 const unsigned char *aug
= cie
->augmentation
;
268 const unsigned char *p
= aug
+ strlen ((const char *)aug
) + 1;
269 const unsigned char *ret
= NULL
;
272 /* g++ v2 "eh" has pointer immediately following augmentation string,
273 so it must be handled first. */
274 if (aug
[0] == 'e' && aug
[1] == 'h')
276 fs
->eh_ptr
= read_pointer (p
);
277 p
+= sizeof (void *);
281 /* Immediately following the augmentation are the code and
282 data alignment and return address column. */
283 p
= read_uleb128 (p
, &fs
->code_align
);
284 p
= read_sleb128 (p
, &fs
->data_align
);
285 if (cie
->version
== 1)
286 fs
->retaddr_column
= *p
++;
288 p
= read_uleb128 (p
, &fs
->retaddr_column
);
289 fs
->lsda_encoding
= DW_EH_PE_omit
;
291 /* If the augmentation starts with 'z', then a uleb128 immediately
292 follows containing the length of the augmentation field following
296 p
= read_uleb128 (p
, &utmp
);
303 /* Iterate over recognized augmentation subsequences. */
306 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
309 fs
->lsda_encoding
= *p
++;
313 /* "R" indicates a byte indicating how FDE addresses are encoded. */
314 else if (aug
[0] == 'R')
316 fs
->fde_encoding
= *p
++;
320 /* "P" indicates a personality routine in the CIE augmentation. */
321 else if (aug
[0] == 'P')
323 _Unwind_Ptr personality
;
325 p
= read_encoded_value (context
, *p
, p
+ 1, &personality
);
326 fs
->personality
= (_Unwind_Personality_Fn
) personality
;
330 /* Otherwise we have an unknown augmentation string.
331 Bail unless we saw a 'z' prefix. */
336 return ret
? ret
: p
;
340 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
341 onto the stack to start. */
344 execute_stack_op (const unsigned char *op_ptr
, const unsigned char *op_end
,
345 struct _Unwind_Context
*context
, _Unwind_Word initial
)
347 _Unwind_Word stack
[64]; /* ??? Assume this is enough. */
353 while (op_ptr
< op_end
)
355 enum dwarf_location_atom op
= *op_ptr
++;
356 _Unwind_Word result
, reg
, utmp
;
357 _Unwind_Sword offset
, stmp
;
393 result
= op
- DW_OP_lit0
;
397 result
= (_Unwind_Word
) (_Unwind_Ptr
) read_pointer (op_ptr
);
398 op_ptr
+= sizeof (void *);
402 result
= read_1u (op_ptr
);
406 result
= read_1s (op_ptr
);
410 result
= read_2u (op_ptr
);
414 result
= read_2s (op_ptr
);
418 result
= read_4u (op_ptr
);
422 result
= read_4s (op_ptr
);
426 result
= read_8u (op_ptr
);
430 result
= read_8s (op_ptr
);
434 op_ptr
= read_uleb128 (op_ptr
, &result
);
437 op_ptr
= read_sleb128 (op_ptr
, &stmp
);
473 result
= _Unwind_GetGR (context
, op
- DW_OP_reg0
);
476 op_ptr
= read_uleb128 (op_ptr
, ®
);
477 result
= _Unwind_GetGR (context
, reg
);
512 op_ptr
= read_sleb128 (op_ptr
, &offset
);
513 result
= _Unwind_GetGR (context
, op
- DW_OP_breg0
) + offset
;
516 op_ptr
= read_uleb128 (op_ptr
, ®
);
517 op_ptr
= read_sleb128 (op_ptr
, &offset
);
518 result
= _Unwind_GetGR (context
, reg
) + offset
;
522 gcc_assert (stack_elt
);
523 result
= stack
[stack_elt
- 1];
527 gcc_assert (stack_elt
);
533 gcc_assert (offset
< stack_elt
- 1);
534 result
= stack
[stack_elt
- 1 - offset
];
538 gcc_assert (stack_elt
>= 2);
539 result
= stack
[stack_elt
- 2];
544 _Unwind_Word t1
, t2
, t3
;
546 gcc_assert (stack_elt
>= 3);
547 t1
= stack
[stack_elt
- 1];
548 t2
= stack
[stack_elt
- 2];
549 t3
= stack
[stack_elt
- 3];
550 stack
[stack_elt
- 1] = t2
;
551 stack
[stack_elt
- 2] = t3
;
552 stack
[stack_elt
- 3] = t1
;
557 case DW_OP_deref_size
:
561 case DW_OP_plus_uconst
:
562 /* Unary operations. */
563 gcc_assert (stack_elt
);
566 result
= stack
[stack_elt
];
572 void *ptr
= (void *) (_Unwind_Ptr
) result
;
573 result
= (_Unwind_Ptr
) read_pointer (ptr
);
577 case DW_OP_deref_size
:
579 void *ptr
= (void *) (_Unwind_Ptr
) result
;
583 result
= read_1u (ptr
);
586 result
= read_2u (ptr
);
589 result
= read_4u (ptr
);
592 result
= read_8u (ptr
);
601 if ((_Unwind_Sword
) result
< 0)
610 case DW_OP_plus_uconst
:
611 op_ptr
= read_uleb128 (op_ptr
, &utmp
);
638 /* Binary operations. */
639 _Unwind_Word first
, second
;
640 gcc_assert (stack_elt
>= 2);
643 second
= stack
[stack_elt
];
644 first
= stack
[stack_elt
+ 1];
649 result
= second
& first
;
652 result
= (_Unwind_Sword
) second
/ (_Unwind_Sword
) first
;
655 result
= second
- first
;
658 result
= (_Unwind_Sword
) second
% (_Unwind_Sword
) first
;
661 result
= second
* first
;
664 result
= second
| first
;
667 result
= second
+ first
;
670 result
= second
<< first
;
673 result
= second
>> first
;
676 result
= (_Unwind_Sword
) second
>> first
;
679 result
= second
^ first
;
682 result
= (_Unwind_Sword
) first
<= (_Unwind_Sword
) second
;
685 result
= (_Unwind_Sword
) first
>= (_Unwind_Sword
) second
;
688 result
= (_Unwind_Sword
) first
== (_Unwind_Sword
) second
;
691 result
= (_Unwind_Sword
) first
< (_Unwind_Sword
) second
;
694 result
= (_Unwind_Sword
) first
> (_Unwind_Sword
) second
;
697 result
= (_Unwind_Sword
) first
!= (_Unwind_Sword
) second
;
707 offset
= read_2s (op_ptr
);
713 gcc_assert (stack_elt
);
716 offset
= read_2s (op_ptr
);
718 if (stack
[stack_elt
] != 0)
729 /* Most things push a result value. */
730 gcc_assert ((size_t) stack_elt
< sizeof(stack
)/sizeof(*stack
));
731 stack
[stack_elt
++] = result
;
735 /* We were executing this program to get a value. It should be
737 gcc_assert (stack_elt
);
739 return stack
[stack_elt
];
743 /* Decode DWARF 2 call frame information. Takes pointers the
744 instruction sequence to decode, current register information and
745 CIE info, and the PC range to evaluate. */
748 execute_cfa_program (const unsigned char *insn_ptr
,
749 const unsigned char *insn_end
,
750 struct _Unwind_Context
*context
,
751 _Unwind_FrameState
*fs
)
753 struct frame_state_reg_info
*unused_rs
= NULL
;
755 /* Don't allow remember/restore between CIE and FDE programs. */
756 fs
->regs
.prev
= NULL
;
758 /* The comparison with the return address uses < rather than <= because
759 we are only interested in the effects of code before the call; for a
760 noreturn function, the return address may point to unrelated code with
761 a different stack configuration that we are not interested in. We
762 assume that the call itself is unwind info-neutral; if not, or if
763 there are delay instructions that adjust the stack, these must be
764 reflected at the point immediately before the call insn. */
765 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
)
767 unsigned char insn
= *insn_ptr
++;
768 _Unwind_Word reg
, utmp
;
769 _Unwind_Sword offset
, stmp
;
771 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
772 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
773 else if ((insn
& 0xc0) == DW_CFA_offset
)
776 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
777 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
778 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
780 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
782 else if ((insn
& 0xc0) == DW_CFA_restore
)
785 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_UNSAVED
;
793 insn_ptr
= read_encoded_value (context
, fs
->fde_encoding
,
795 fs
->pc
= (void *) pc
;
799 case DW_CFA_advance_loc1
:
800 fs
->pc
+= read_1u (insn_ptr
) * fs
->code_align
;
803 case DW_CFA_advance_loc2
:
804 fs
->pc
+= read_2u (insn_ptr
) * fs
->code_align
;
807 case DW_CFA_advance_loc4
:
808 fs
->pc
+= read_4u (insn_ptr
) * fs
->code_align
;
812 case DW_CFA_offset_extended
:
813 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
814 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
815 offset
= (_Unwind_Sword
) utmp
* fs
->data_align
;
816 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
818 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
821 case DW_CFA_restore_extended
:
822 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
823 /* FIXME, this is wrong; the CIE might have said that the
824 register was saved somewhere. */
825 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
828 case DW_CFA_undefined
:
829 case DW_CFA_same_value
:
830 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
831 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN(reg
)].how
= REG_UNSAVED
;
837 case DW_CFA_register
:
840 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
841 insn_ptr
= read_uleb128 (insn_ptr
, ®2
);
842 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_REG
;
843 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.reg
= reg2
;
847 case DW_CFA_remember_state
:
849 struct frame_state_reg_info
*new_rs
;
853 unused_rs
= unused_rs
->prev
;
856 new_rs
= alloca (sizeof (struct frame_state_reg_info
));
859 fs
->regs
.prev
= new_rs
;
863 case DW_CFA_restore_state
:
865 struct frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
867 old_rs
->prev
= unused_rs
;
873 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
874 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
875 fs
->cfa_offset
= utmp
;
876 fs
->cfa_how
= CFA_REG_OFFSET
;
879 case DW_CFA_def_cfa_register
:
880 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
881 fs
->cfa_how
= CFA_REG_OFFSET
;
884 case DW_CFA_def_cfa_offset
:
885 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
886 fs
->cfa_offset
= utmp
;
887 /* cfa_how deliberately not set. */
890 case DW_CFA_def_cfa_expression
:
891 fs
->cfa_exp
= insn_ptr
;
892 fs
->cfa_how
= CFA_EXP
;
893 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
897 case DW_CFA_expression
:
898 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
899 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
= REG_SAVED_EXP
;
900 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.exp
= insn_ptr
;
901 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
905 /* From the 2.1 draft. */
906 case DW_CFA_offset_extended_sf
:
907 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
908 insn_ptr
= read_sleb128 (insn_ptr
, &stmp
);
909 offset
= stmp
* fs
->data_align
;
910 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
912 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= offset
;
915 case DW_CFA_def_cfa_sf
:
916 insn_ptr
= read_uleb128 (insn_ptr
, &fs
->cfa_reg
);
917 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
918 fs
->cfa_how
= CFA_REG_OFFSET
;
921 case DW_CFA_def_cfa_offset_sf
:
922 insn_ptr
= read_sleb128 (insn_ptr
, &fs
->cfa_offset
);
923 /* cfa_how deliberately not set. */
926 case DW_CFA_GNU_window_save
:
927 /* ??? Hardcoded for SPARC register window configuration. */
928 for (reg
= 16; reg
< 32; ++reg
)
930 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
931 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
935 case DW_CFA_GNU_args_size
:
936 insn_ptr
= read_uleb128 (insn_ptr
, &context
->args_size
);
939 case DW_CFA_GNU_negative_offset_extended
:
940 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
941 older PowerPC code. */
942 insn_ptr
= read_uleb128 (insn_ptr
, ®
);
943 insn_ptr
= read_uleb128 (insn_ptr
, &utmp
);
944 offset
= (_Unwind_Word
) utmp
* fs
->data_align
;
945 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].how
947 fs
->regs
.reg
[DWARF_REG_TO_UNWIND_COLUMN (reg
)].loc
.offset
= -offset
;
956 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
957 its caller and decode it into FS. This function also sets the
958 args_size and lsda members of CONTEXT, as they are really information
959 about the caller's frame. */
961 static _Unwind_Reason_Code
962 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
964 const struct dwarf_fde
*fde
;
965 const struct dwarf_cie
*cie
;
966 const unsigned char *aug
, *insn
, *end
;
968 memset (fs
, 0, sizeof (*fs
));
969 context
->args_size
= 0;
972 if (context
->ra
== 0)
973 return _URC_END_OF_STACK
;
975 fde
= _Unwind_Find_FDE (context
->ra
- 1, &context
->bases
);
978 #ifdef MD_FALLBACK_FRAME_STATE_FOR
979 /* Couldn't find frame unwind info for this function. Try a
980 target-specific fallback mechanism. This will necessarily
981 not provide a personality routine or LSDA. */
982 return MD_FALLBACK_FRAME_STATE_FOR (context
, fs
);
984 return _URC_END_OF_STACK
;
988 fs
->pc
= context
->bases
.func
;
991 insn
= extract_cie_info (cie
, context
, fs
);
993 /* CIE contained unknown augmentation. */
994 return _URC_FATAL_PHASE1_ERROR
;
996 /* First decode all the insns in the CIE. */
997 end
= (unsigned char *) next_fde ((struct dwarf_fde
*) cie
);
998 execute_cfa_program (insn
, end
, context
, fs
);
1000 /* Locate augmentation for the fde. */
1001 aug
= (unsigned char *) fde
+ sizeof (*fde
);
1002 aug
+= 2 * size_of_encoded_value (fs
->fde_encoding
);
1007 aug
= read_uleb128 (aug
, &i
);
1010 if (fs
->lsda_encoding
!= DW_EH_PE_omit
)
1014 aug
= read_encoded_value (context
, fs
->lsda_encoding
, aug
, &lsda
);
1015 context
->lsda
= (void *) lsda
;
1018 /* Then the insns in the FDE up to our target PC. */
1021 end
= (unsigned char *) next_fde (fde
);
1022 execute_cfa_program (insn
, end
, context
, fs
);
1024 return _URC_NO_REASON
;
1027 typedef struct frame_state
1033 long reg_or_offset
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1034 unsigned short cfa_reg
;
1035 unsigned short retaddr_column
;
1036 char saved
[PRE_GCC3_DWARF_FRAME_REGISTERS
+1];
1039 struct frame_state
* __frame_state_for (void *, struct frame_state
*);
1041 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1042 a given PC_TARGET. The caller should allocate a local variable of
1043 `struct frame_state' and pass its address to STATE_IN. */
1045 struct frame_state
*
1046 __frame_state_for (void *pc_target
, struct frame_state
*state_in
)
1048 struct _Unwind_Context context
;
1049 _Unwind_FrameState fs
;
1052 memset (&context
, 0, sizeof (struct _Unwind_Context
));
1053 context
.ra
= pc_target
+ 1;
1055 if (uw_frame_state_for (&context
, &fs
) != _URC_NO_REASON
)
1058 /* We have no way to pass a location expression for the CFA to our
1059 caller. It wouldn't understand it anyway. */
1060 if (fs
.cfa_how
== CFA_EXP
)
1063 for (reg
= 0; reg
< PRE_GCC3_DWARF_FRAME_REGISTERS
+ 1; reg
++)
1065 state_in
->saved
[reg
] = fs
.regs
.reg
[reg
].how
;
1066 switch (state_in
->saved
[reg
])
1069 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.reg
;
1071 case REG_SAVED_OFFSET
:
1072 state_in
->reg_or_offset
[reg
] = fs
.regs
.reg
[reg
].loc
.offset
;
1075 state_in
->reg_or_offset
[reg
] = 0;
1080 state_in
->cfa_offset
= fs
.cfa_offset
;
1081 state_in
->cfa_reg
= fs
.cfa_reg
;
1082 state_in
->retaddr_column
= fs
.retaddr_column
;
1083 state_in
->args_size
= context
.args_size
;
1084 state_in
->eh_ptr
= fs
.eh_ptr
;
1089 typedef union { _Unwind_Ptr ptr
; _Unwind_Word word
; } _Unwind_SpTmp
;
1092 _Unwind_SetSpColumn (struct _Unwind_Context
*context
, void *cfa
,
1093 _Unwind_SpTmp
*tmp_sp
)
1095 int size
= dwarf_reg_size_table
[__builtin_dwarf_sp_column ()];
1097 if (size
== sizeof(_Unwind_Ptr
))
1098 tmp_sp
->ptr
= (_Unwind_Ptr
) cfa
;
1101 gcc_assert (size
== sizeof(_Unwind_Word
));
1102 tmp_sp
->word
= (_Unwind_Ptr
) cfa
;
1104 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), tmp_sp
);
1108 uw_update_context_1 (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1110 struct _Unwind_Context orig_context
= *context
;
1114 #ifdef EH_RETURN_STACKADJ_RTX
1115 /* Special handling here: Many machines do not use a frame pointer,
1116 and track the CFA only through offsets from the stack pointer from
1117 one frame to the next. In this case, the stack pointer is never
1118 stored, so it has no saved address in the context. What we do
1119 have is the CFA from the previous stack frame.
1121 In very special situations (such as unwind info for signal return),
1122 there may be location expressions that use the stack pointer as well.
1124 Do this conditionally for one frame. This allows the unwind info
1125 for one frame to save a copy of the stack pointer from the previous
1126 frame, and be able to use much easier CFA mechanisms to do it.
1127 Always zap the saved stack pointer value for the next frame; carrying
1128 the value over from one frame to another doesn't make sense. */
1130 _Unwind_SpTmp tmp_sp
;
1132 if (!_Unwind_GetGRPtr (&orig_context
, __builtin_dwarf_sp_column ()))
1133 _Unwind_SetSpColumn (&orig_context
, context
->cfa
, &tmp_sp
);
1134 _Unwind_SetGRPtr (context
, __builtin_dwarf_sp_column (), NULL
);
1137 /* Compute this frame's CFA. */
1138 switch (fs
->cfa_how
)
1140 case CFA_REG_OFFSET
:
1141 cfa
= _Unwind_GetPtr (&orig_context
, fs
->cfa_reg
);
1142 cfa
+= fs
->cfa_offset
;
1147 const unsigned char *exp
= fs
->cfa_exp
;
1150 exp
= read_uleb128 (exp
, &len
);
1151 cfa
= (void *) (_Unwind_Ptr
)
1152 execute_stack_op (exp
, exp
+ len
, &orig_context
, 0);
1161 /* Compute the addresses of all registers saved in this frame. */
1162 for (i
= 0; i
< DWARF_FRAME_REGISTERS
+ 1; ++i
)
1163 switch (fs
->regs
.reg
[i
].how
)
1168 case REG_SAVED_OFFSET
:
1169 _Unwind_SetGRPtr (context
, i
,
1170 (void *) (cfa
+ fs
->regs
.reg
[i
].loc
.offset
));
1176 _Unwind_GetGRPtr (&orig_context
, fs
->regs
.reg
[i
].loc
.reg
));
1181 const unsigned char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1185 exp
= read_uleb128 (exp
, &len
);
1186 val
= execute_stack_op (exp
, exp
+ len
, &orig_context
,
1188 _Unwind_SetGRPtr (context
, i
, (void *) val
);
1193 #ifdef MD_FROB_UPDATE_CONTEXT
1194 MD_FROB_UPDATE_CONTEXT (context
, fs
);
1198 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1199 of its caller. Update CONTEXT to refer to the caller as well. Note
1200 that the args_size and lsda members are not updated here, but later in
1201 uw_frame_state_for. */
1204 uw_update_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
1206 uw_update_context_1 (context
, fs
);
1208 /* Compute the return address now, since the return address column
1209 can change from frame to frame. */
1210 context
->ra
= __builtin_extract_return_addr
1211 (_Unwind_GetPtr (context
, fs
->retaddr_column
));
1214 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1215 level will be the return address and the CFA. */
1217 #define uw_init_context(CONTEXT) \
1220 /* Do any necessary initialization to access arbitrary stack frames. \
1221 On the SPARC, this means flushing the register windows. */ \
1222 __builtin_unwind_init (); \
1223 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1224 __builtin_return_address (0)); \
1229 init_dwarf_reg_size_table (void)
1231 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table
);
1235 uw_init_context_1 (struct _Unwind_Context
*context
,
1236 void *outer_cfa
, void *outer_ra
)
1238 void *ra
= __builtin_extract_return_addr (__builtin_return_address (0));
1239 _Unwind_FrameState fs
;
1240 _Unwind_SpTmp sp_slot
;
1241 _Unwind_Reason_Code code
;
1243 memset (context
, 0, sizeof (struct _Unwind_Context
));
1246 code
= uw_frame_state_for (context
, &fs
);
1247 gcc_assert (code
== _URC_NO_REASON
);
1251 static __gthread_once_t once_regsizes
= __GTHREAD_ONCE_INIT
;
1252 if (__gthread_once (&once_regsizes
, init_dwarf_reg_size_table
) != 0
1253 || dwarf_reg_size_table
[0] == 0)
1254 init_dwarf_reg_size_table ();
1257 if (dwarf_reg_size_table
[0] == 0)
1258 init_dwarf_reg_size_table ();
1261 /* Force the frame state to use the known cfa value. */
1262 _Unwind_SetSpColumn (context
, outer_cfa
, &sp_slot
);
1263 fs
.cfa_how
= CFA_REG_OFFSET
;
1264 fs
.cfa_reg
= __builtin_dwarf_sp_column ();
1267 uw_update_context_1 (context
, &fs
);
1269 /* If the return address column was saved in a register in the
1270 initialization context, then we can't see it in the given
1271 call frame data. So have the initialization context tell us. */
1272 context
->ra
= __builtin_extract_return_addr (outer_ra
);
1276 /* Install TARGET into CURRENT so that we can return to it. This is a
1277 macro because __builtin_eh_return must be invoked in the context of
1280 #define uw_install_context(CURRENT, TARGET) \
1283 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1284 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1285 __builtin_eh_return (offset, handler); \
1290 uw_install_context_1 (struct _Unwind_Context
*current
,
1291 struct _Unwind_Context
*target
)
1294 _Unwind_SpTmp sp_slot
;
1296 /* If the target frame does not have a saved stack pointer,
1297 then set up the target's CFA. */
1298 if (!_Unwind_GetGRPtr (target
, __builtin_dwarf_sp_column ()))
1299 _Unwind_SetSpColumn (target
, target
->cfa
, &sp_slot
);
1301 for (i
= 0; i
< DWARF_FRAME_REGISTERS
; ++i
)
1303 void *c
= current
->reg
[i
];
1304 void *t
= target
->reg
[i
];
1306 if (t
&& c
&& t
!= c
)
1307 memcpy (c
, t
, dwarf_reg_size_table
[i
]);
1310 /* If the current frame doesn't have a saved stack pointer, then we
1311 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1312 pointer value reloaded. */
1313 if (!_Unwind_GetGRPtr (current
, __builtin_dwarf_sp_column ()))
1317 target_cfa
= _Unwind_GetPtr (target
, __builtin_dwarf_sp_column ());
1319 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1320 if (STACK_GROWS_DOWNWARD
)
1321 return target_cfa
- current
->cfa
+ target
->args_size
;
1323 return current
->cfa
- target_cfa
- target
->args_size
;
1328 static inline _Unwind_Ptr
1329 uw_identify_context (struct _Unwind_Context
*context
)
1331 return _Unwind_GetIP (context
);
1335 #include "unwind.inc"
1337 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1338 alias (_Unwind_Backtrace
);
1339 alias (_Unwind_DeleteException
);
1340 alias (_Unwind_FindEnclosingFunction
);
1341 alias (_Unwind_ForcedUnwind
);
1342 alias (_Unwind_GetDataRelBase
);
1343 alias (_Unwind_GetTextRelBase
);
1344 alias (_Unwind_GetCFA
);
1345 alias (_Unwind_GetGR
);
1346 alias (_Unwind_GetIP
);
1347 alias (_Unwind_GetLanguageSpecificData
);
1348 alias (_Unwind_GetRegionStart
);
1349 alias (_Unwind_RaiseException
);
1350 alias (_Unwind_Resume
);
1351 alias (_Unwind_Resume_or_Rethrow
);
1352 alias (_Unwind_SetGR
);
1353 alias (_Unwind_SetIP
);
1356 #endif /* !USING_SJLJ_EXCEPTIONS */