Update.
[glibc.git] / sysdeps / generic / unwind-pe.h
blob9a031c1f32edb979c80d81a02d36eaa0ebcdcb5b
1 /* Exception handling and frame unwind runtime interface routines.
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* @@@ Really this should be out of line, but this also causes link
22 compatibility problems with the base ABI. This is slightly better
23 than duplicating code, however. */
25 /* If using C++, references to abort have to be qualified with std::. */
26 #if __cplusplus
27 #define __gxx_abort std::abort
28 #else
29 #define __gxx_abort abort
30 #endif
32 /* Pointer encodings, from dwarf2.h. */
33 #define DW_EH_PE_absptr 0x00
34 #define DW_EH_PE_omit 0xff
36 #define DW_EH_PE_uleb128 0x01
37 #define DW_EH_PE_udata2 0x02
38 #define DW_EH_PE_udata4 0x03
39 #define DW_EH_PE_udata8 0x04
40 #define DW_EH_PE_sleb128 0x09
41 #define DW_EH_PE_sdata2 0x0A
42 #define DW_EH_PE_sdata4 0x0B
43 #define DW_EH_PE_sdata8 0x0C
44 #define DW_EH_PE_signed 0x08
46 #define DW_EH_PE_pcrel 0x10
47 #define DW_EH_PE_textrel 0x20
48 #define DW_EH_PE_datarel 0x30
49 #define DW_EH_PE_funcrel 0x40
50 #define DW_EH_PE_aligned 0x50
52 #define DW_EH_PE_indirect 0x80
55 #ifdef _LIBC
56 /* Prototypes. */
57 extern unsigned int size_of_encoded_value (unsigned char encoding);
58 extern const unsigned char *read_encoded_value_with_base
59 (unsigned char encoding, _Unwind_Ptr base,
60 const unsigned char *p, _Unwind_Ptr *val);
61 #endif
65 /* Given an encoding, return the number of bytes the format occupies.
66 This is only defined for fixed-size encodings, and so does not
67 include leb128. */
69 #ifndef _LIBC
70 static
71 #endif
72 unsigned int
73 size_of_encoded_value (unsigned char encoding)
74 #if defined(_LIBC) && !defined(NO_BASE_OF_ENCODED_VALUE)
76 #else
78 if (encoding == DW_EH_PE_omit)
79 return 0;
81 switch (encoding & 0x07)
83 case DW_EH_PE_absptr:
84 return sizeof (void *);
85 case DW_EH_PE_udata2:
86 return 2;
87 case DW_EH_PE_udata4:
88 return 4;
89 case DW_EH_PE_udata8:
90 return 8;
92 __gxx_abort ();
94 #endif
96 #ifndef NO_BASE_OF_ENCODED_VALUE
98 /* Given an encoding and an _Unwind_Context, return the base to which
99 the encoding is relative. This base may then be passed to
100 read_encoded_value_with_base for use when the _Unwind_Context is
101 not available. */
103 static _Unwind_Ptr
104 base_of_encoded_value (unsigned char encoding, struct _Unwind_Context *context)
106 if (encoding == DW_EH_PE_omit)
107 return 0;
109 switch (encoding & 0x70)
111 case DW_EH_PE_absptr:
112 case DW_EH_PE_pcrel:
113 case DW_EH_PE_aligned:
114 return 0;
116 case DW_EH_PE_textrel:
117 return _Unwind_GetTextRelBase (context);
118 case DW_EH_PE_datarel:
119 return _Unwind_GetDataRelBase (context);
120 case DW_EH_PE_funcrel:
121 return _Unwind_GetRegionStart (context);
123 __gxx_abort ();
126 #endif
128 /* Load an encoded value from memory at P. The value is returned in VAL;
129 The function returns P incremented past the value. BASE is as given
130 by base_of_encoded_value for this encoding in the appropriate context. */
132 #ifndef _LIBC
133 static
134 #endif
135 const unsigned char *
136 read_encoded_value_with_base (unsigned char encoding, _Unwind_Ptr base,
137 const unsigned char *p, _Unwind_Ptr *val)
138 #if defined(_LIBC) && !defined(NO_BASE_OF_ENCODED_VALUE)
140 #else
142 union unaligned
144 void *ptr;
145 unsigned u2 __attribute__ ((mode (HI)));
146 unsigned u4 __attribute__ ((mode (SI)));
147 unsigned u8 __attribute__ ((mode (DI)));
148 signed s2 __attribute__ ((mode (HI)));
149 signed s4 __attribute__ ((mode (SI)));
150 signed s8 __attribute__ ((mode (DI)));
151 } __attribute__((__packed__));
153 union unaligned *u = (union unaligned *) p;
154 _Unwind_Ptr result;
156 if (encoding == DW_EH_PE_aligned)
158 _Unwind_Ptr a = (_Unwind_Ptr)p;
159 a = (a + sizeof (void *) - 1) & - sizeof(void *);
160 result = *(_Unwind_Ptr *) a;
161 p = (const unsigned char *)(a + sizeof (void *));
163 else
165 switch (encoding & 0x0f)
167 case DW_EH_PE_absptr:
168 result = (_Unwind_Ptr) u->ptr;
169 p += sizeof (void *);
170 break;
172 case DW_EH_PE_uleb128:
174 unsigned int shift = 0;
175 unsigned char byte;
177 result = 0;
180 byte = *p++;
181 result |= (_Unwind_Ptr)(byte & 0x7f) << shift;
182 shift += 7;
184 while (byte & 0x80);
186 break;
188 case DW_EH_PE_sleb128:
190 unsigned int shift = 0;
191 unsigned char byte;
193 result = 0;
196 byte = *p++;
197 result |= (_Unwind_Ptr)(byte & 0x7f) << shift;
198 shift += 7;
200 while (byte & 0x80);
202 if (shift < 8 * sizeof(result) && (byte & 0x40) != 0)
203 result |= -(1L << shift);
205 break;
207 case DW_EH_PE_udata2:
208 result = u->u2;
209 p += 2;
210 break;
211 case DW_EH_PE_udata4:
212 result = u->u4;
213 p += 4;
214 break;
215 case DW_EH_PE_udata8:
216 result = u->u8;
217 p += 8;
218 break;
220 case DW_EH_PE_sdata2:
221 result = u->s2;
222 p += 2;
223 break;
224 case DW_EH_PE_sdata4:
225 result = u->s4;
226 p += 4;
227 break;
228 case DW_EH_PE_sdata8:
229 result = u->s8;
230 p += 8;
231 break;
233 default:
234 __gxx_abort ();
237 if (result != 0)
239 result += ((encoding & 0x70) == DW_EH_PE_pcrel
240 ? (_Unwind_Ptr)u : base);
241 if (encoding & DW_EH_PE_indirect)
242 result = *(_Unwind_Ptr *)result;
246 *val = result;
247 return p;
249 #endif
251 #ifndef NO_BASE_OF_ENCODED_VALUE
253 /* Like read_encoded_value_with_base, but get the base from the context
254 rather than providing it directly. */
256 static inline const unsigned char *
257 read_encoded_value (struct _Unwind_Context *context, unsigned char encoding,
258 const unsigned char *p, _Unwind_Ptr *val)
260 return read_encoded_value_with_base (encoding,
261 base_of_encoded_value (encoding, context),
262 p, val);
265 #endif
267 /* Read an unsigned leb128 value from P, store the value in VAL, return
268 P incremented past the value. */
270 static inline const unsigned char *
271 read_uleb128 (const unsigned char *p, _Unwind_Ptr *val)
273 return read_encoded_value_with_base (DW_EH_PE_uleb128, 0, p, val);
276 /* Similar, but read a signed leb128 value. */
278 static inline const unsigned char *
279 read_sleb128 (const unsigned char *p, _Unwind_Ptr *val)
281 return read_encoded_value_with_base (DW_EH_PE_sleb128, 0, p, val);