1 /* Exception handling and frame unwind runtime interface routines.
2 Copyright (C) 2001, 2002, 2003, 2004 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, 51 Franklin Street, Fifth Floor, 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 #ifndef GCC_UNWIND_PE_H
35 #define GCC_UNWIND_PE_H
37 /* If using C++, references to abort have to be qualified with std::. */
39 #define __gxx_abort std::abort
41 #define __gxx_abort abort
44 /* Pointer encodings, from dwarf2.h. */
45 #define DW_EH_PE_absptr 0x00
46 #define DW_EH_PE_omit 0xff
48 #define DW_EH_PE_uleb128 0x01
49 #define DW_EH_PE_udata2 0x02
50 #define DW_EH_PE_udata4 0x03
51 #define DW_EH_PE_udata8 0x04
52 #define DW_EH_PE_sleb128 0x09
53 #define DW_EH_PE_sdata2 0x0A
54 #define DW_EH_PE_sdata4 0x0B
55 #define DW_EH_PE_sdata8 0x0C
56 #define DW_EH_PE_signed 0x08
58 #define DW_EH_PE_pcrel 0x10
59 #define DW_EH_PE_textrel 0x20
60 #define DW_EH_PE_datarel 0x30
61 #define DW_EH_PE_funcrel 0x40
62 #define DW_EH_PE_aligned 0x50
64 #define DW_EH_PE_indirect 0x80
67 #ifndef NO_SIZE_OF_ENCODED_VALUE
69 /* Given an encoding, return the number of bytes the format occupies.
70 This is only defined for fixed-size encodings, and so does not
74 size_of_encoded_value (unsigned char encoding
)
76 if (encoding
== DW_EH_PE_omit
)
79 switch (encoding
& 0x07)
82 return sizeof (void *);
95 #ifndef NO_BASE_OF_ENCODED_VALUE
97 /* Given an encoding and an _Unwind_Context, return the base to which
98 the encoding is relative. This base may then be passed to
99 read_encoded_value_with_base for use when the _Unwind_Context is
103 base_of_encoded_value (unsigned char encoding
, struct _Unwind_Context
*context
)
105 if (encoding
== DW_EH_PE_omit
)
108 switch (encoding
& 0x70)
110 case DW_EH_PE_absptr
:
112 case DW_EH_PE_aligned
:
115 case DW_EH_PE_textrel
:
116 return _Unwind_GetTextRelBase (context
);
117 case DW_EH_PE_datarel
:
118 return _Unwind_GetDataRelBase (context
);
119 case DW_EH_PE_funcrel
:
120 return _Unwind_GetRegionStart (context
);
127 /* Read an unsigned leb128 value from P, store the value in VAL, return
128 P incremented past the value. We assume that a word is large enough to
129 hold any value so encoded; if it is smaller than a pointer on some target,
130 pointers should not be leb128 encoded on that target. */
132 static const unsigned char *
133 read_uleb128 (const unsigned char *p
, _uleb128_t
*val
)
135 unsigned int shift
= 0;
143 result
|= ((_uleb128_t
)byte
& 0x7f) << shift
;
152 /* Similar, but read a signed leb128 value. */
154 static const unsigned char *
155 read_sleb128 (const unsigned char *p
, _sleb128_t
*val
)
157 unsigned int shift
= 0;
165 result
|= ((_uleb128_t
)byte
& 0x7f) << shift
;
170 /* Sign-extend a negative value. */
171 if (shift
< 8 * sizeof(result
) && (byte
& 0x40) != 0)
172 result
|= -(((_uleb128_t
)1L) << shift
);
174 *val
= (_sleb128_t
) result
;
178 /* Load an encoded value from memory at P. The value is returned in VAL;
179 The function returns P incremented past the value. BASE is as given
180 by base_of_encoded_value for this encoding in the appropriate context. */
182 static const unsigned char *
183 read_encoded_value_with_base (unsigned char encoding
, _Unwind_Ptr base
,
184 const unsigned char *p
, _Unwind_Ptr
*val
)
189 unsigned u2
__attribute__ ((mode (HI
)));
190 unsigned u4
__attribute__ ((mode (SI
)));
191 unsigned u8
__attribute__ ((mode (DI
)));
192 signed s2
__attribute__ ((mode (HI
)));
193 signed s4
__attribute__ ((mode (SI
)));
194 signed s8
__attribute__ ((mode (DI
)));
195 } __attribute__((__packed__
));
197 const union unaligned
*u
= (const union unaligned
*) p
;
198 _Unwind_Internal_Ptr result
;
200 if (encoding
== DW_EH_PE_aligned
)
202 _Unwind_Internal_Ptr a
= (_Unwind_Internal_Ptr
) p
;
203 a
= (a
+ sizeof (void *) - 1) & - sizeof(void *);
204 result
= *(_Unwind_Internal_Ptr
*) a
;
205 p
= (const unsigned char *) (_Unwind_Internal_Ptr
) (a
+ sizeof (void *));
209 switch (encoding
& 0x0f)
211 case DW_EH_PE_absptr
:
212 result
= (_Unwind_Internal_Ptr
) u
->ptr
;
213 p
+= sizeof (void *);
216 case DW_EH_PE_uleb128
:
219 p
= read_uleb128 (p
, &tmp
);
220 result
= (_Unwind_Internal_Ptr
) tmp
;
224 case DW_EH_PE_sleb128
:
227 p
= read_sleb128 (p
, &tmp
);
228 result
= (_Unwind_Internal_Ptr
) tmp
;
232 case DW_EH_PE_udata2
:
236 case DW_EH_PE_udata4
:
240 case DW_EH_PE_udata8
:
245 case DW_EH_PE_sdata2
:
249 case DW_EH_PE_sdata4
:
253 case DW_EH_PE_sdata8
:
264 result
+= ((encoding
& 0x70) == DW_EH_PE_pcrel
265 ? (_Unwind_Internal_Ptr
) u
: base
);
266 if (encoding
& DW_EH_PE_indirect
)
267 result
= *(_Unwind_Internal_Ptr
*) result
;
275 #ifndef NO_BASE_OF_ENCODED_VALUE
277 /* Like read_encoded_value_with_base, but get the base from the context
278 rather than providing it directly. */
280 static inline const unsigned char *
281 read_encoded_value (struct _Unwind_Context
*context
, unsigned char encoding
,
282 const unsigned char *p
, _Unwind_Ptr
*val
)
284 return read_encoded_value_with_base (encoding
,
285 base_of_encoded_value (encoding
, context
),
291 #endif /* unwind-pe.h */