1 /* Exception handling and frame unwind runtime interface routines.
2 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combined
20 GCC is distributed in the hope that it will be useful, but WITHOUT
21 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
23 License for more details.
25 You should have received a copy of the GNU General Public License
26 along with GCC; see the file COPYING. If not, write to the Free
27 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 /* @@@ Really this should be out of line, but this also causes link
31 compatibility problems with the base ABI. This is slightly better
32 than duplicating code, however. */
34 /* If using C++, references to abort have to be qualified with std::. */
36 #define __gxx_abort std::abort
38 #define __gxx_abort abort
41 /* Pointer encodings, from dwarf2.h. */
42 #define DW_EH_PE_absptr 0x00
43 #define DW_EH_PE_omit 0xff
45 #define DW_EH_PE_uleb128 0x01
46 #define DW_EH_PE_udata2 0x02
47 #define DW_EH_PE_udata4 0x03
48 #define DW_EH_PE_udata8 0x04
49 #define DW_EH_PE_sleb128 0x09
50 #define DW_EH_PE_sdata2 0x0A
51 #define DW_EH_PE_sdata4 0x0B
52 #define DW_EH_PE_sdata8 0x0C
53 #define DW_EH_PE_signed 0x08
55 #define DW_EH_PE_pcrel 0x10
56 #define DW_EH_PE_textrel 0x20
57 #define DW_EH_PE_datarel 0x30
58 #define DW_EH_PE_funcrel 0x40
59 #define DW_EH_PE_aligned 0x50
61 #define DW_EH_PE_indirect 0x80
64 #ifndef NO_SIZE_OF_ENCODED_VALUE
66 /* Given an encoding, return the number of bytes the format occupies.
67 This is only defined for fixed-size encodings, and so does not
71 size_of_encoded_value (unsigned char encoding
)
73 if (encoding
== DW_EH_PE_omit
)
76 switch (encoding
& 0x07)
79 return sizeof (void *);
92 #ifndef NO_BASE_OF_ENCODED_VALUE
94 /* Given an encoding and an _Unwind_Context, return the base to which
95 the encoding is relative. This base may then be passed to
96 read_encoded_value_with_base for use when the _Unwind_Context is
100 base_of_encoded_value (unsigned char encoding
, struct _Unwind_Context
*context
)
102 if (encoding
== DW_EH_PE_omit
)
105 switch (encoding
& 0x70)
107 case DW_EH_PE_absptr
:
109 case DW_EH_PE_aligned
:
112 case DW_EH_PE_textrel
:
113 return _Unwind_GetTextRelBase (context
);
114 case DW_EH_PE_datarel
:
115 return _Unwind_GetDataRelBase (context
);
116 case DW_EH_PE_funcrel
:
117 return _Unwind_GetRegionStart (context
);
124 /* Read an unsigned leb128 value from P, store the value in VAL, return
125 P incremented past the value. We assume that a word is large enough to
126 hold any value so encoded; if it is smaller than a pointer on some target,
127 pointers should not be leb128 encoded on that target. */
129 static const unsigned char *
130 read_uleb128 (const unsigned char *p
, _Unwind_Word
*val
)
132 unsigned int shift
= 0;
140 result
|= ((_Unwind_Word
)byte
& 0x7f) << shift
;
149 /* Similar, but read a signed leb128 value. */
151 static const unsigned char *
152 read_sleb128 (const unsigned char *p
, _Unwind_Sword
*val
)
154 unsigned int shift
= 0;
162 result
|= ((_Unwind_Word
)byte
& 0x7f) << shift
;
167 /* Sign-extend a negative value. */
168 if (shift
< 8 * sizeof(result
) && (byte
& 0x40) != 0)
169 result
|= -(((_Unwind_Word
)1L) << shift
);
171 *val
= (_Unwind_Sword
) result
;
175 /* Load an encoded value from memory at P. The value is returned in VAL;
176 The function returns P incremented past the value. BASE is as given
177 by base_of_encoded_value for this encoding in the appropriate context. */
179 static const unsigned char *
180 read_encoded_value_with_base (unsigned char encoding
, _Unwind_Ptr base
,
181 const unsigned char *p
, _Unwind_Ptr
*val
)
186 unsigned u2
__attribute__ ((mode (HI
)));
187 unsigned u4
__attribute__ ((mode (SI
)));
188 unsigned u8
__attribute__ ((mode (DI
)));
189 signed s2
__attribute__ ((mode (HI
)));
190 signed s4
__attribute__ ((mode (SI
)));
191 signed s8
__attribute__ ((mode (DI
)));
192 } __attribute__((__packed__
));
194 const union unaligned
*u
= (const union unaligned
*) p
;
195 _Unwind_Internal_Ptr result
;
197 if (encoding
== DW_EH_PE_aligned
)
199 _Unwind_Internal_Ptr a
= (_Unwind_Internal_Ptr
) p
;
200 a
= (a
+ sizeof (void *) - 1) & - sizeof(void *);
201 result
= *(_Unwind_Internal_Ptr
*) a
;
202 p
= (const unsigned char *) (_Unwind_Internal_Ptr
) (a
+ sizeof (void *));
206 switch (encoding
& 0x0f)
208 case DW_EH_PE_absptr
:
209 result
= (_Unwind_Internal_Ptr
) u
->ptr
;
210 p
+= sizeof (void *);
213 case DW_EH_PE_uleb128
:
216 p
= read_uleb128 (p
, &tmp
);
217 result
= (_Unwind_Internal_Ptr
) tmp
;
221 case DW_EH_PE_sleb128
:
224 p
= read_sleb128 (p
, &tmp
);
225 result
= (_Unwind_Internal_Ptr
) tmp
;
229 case DW_EH_PE_udata2
:
233 case DW_EH_PE_udata4
:
237 case DW_EH_PE_udata8
:
242 case DW_EH_PE_sdata2
:
246 case DW_EH_PE_sdata4
:
250 case DW_EH_PE_sdata8
:
261 result
+= ((encoding
& 0x70) == DW_EH_PE_pcrel
262 ? (_Unwind_Internal_Ptr
) u
: base
);
263 if (encoding
& DW_EH_PE_indirect
)
264 result
= *(_Unwind_Internal_Ptr
*) result
;
272 #ifndef NO_BASE_OF_ENCODED_VALUE
274 /* Like read_encoded_value_with_base, but get the base from the context
275 rather than providing it directly. */
277 static inline const unsigned char *
278 read_encoded_value (struct _Unwind_Context
*context
, unsigned char encoding
,
279 const unsigned char *p
, _Unwind_Ptr
*val
)
281 return read_encoded_value_with_base (encoding
,
282 base_of_encoded_value (encoding
, context
),