2.9
[glibc/nacl-glibc.git] / sysdeps / generic / unwind-pe.h
bloba31b4892b0136ae4026d243a2634eb12ad5a0f26
1 /* Exception handling and frame unwind runtime interface routines.
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 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 #if defined(_LIBC)
57 /* Prototypes. */
58 extern unsigned int size_of_encoded_value (unsigned char encoding)
59 attribute_hidden;
61 extern const unsigned char *read_encoded_value_with_base
62 (unsigned char encoding, _Unwind_Ptr base,
63 const unsigned char *p, _Unwind_Ptr *val)
64 attribute_hidden;
66 extern const unsigned char * read_encoded_value
67 (struct _Unwind_Context *context, unsigned char encoding,
68 const unsigned char *p, _Unwind_Ptr *val)
69 attribute_hidden;
71 extern const unsigned char * read_uleb128 (const unsigned char *p,
72 _Unwind_Word *val)
73 attribute_hidden;
74 extern const unsigned char * read_sleb128 (const unsigned char *p,
75 _Unwind_Sword *val)
76 attribute_hidden;
78 #endif
79 #if defined(_LIBC) && defined(_LIBC_DEFINITIONS)
81 #ifdef _LIBC
82 #define STATIC
83 #else
84 #define STATIC static
85 #endif
87 /* Given an encoding, return the number of bytes the format occupies.
88 This is only defined for fixed-size encodings, and so does not
89 include leb128. */
91 STATIC unsigned int
92 size_of_encoded_value (unsigned char encoding)
94 if (encoding == DW_EH_PE_omit)
95 return 0;
97 switch (encoding & 0x07)
99 case DW_EH_PE_absptr:
100 return sizeof (void *);
101 case DW_EH_PE_udata2:
102 return 2;
103 case DW_EH_PE_udata4:
104 return 4;
105 case DW_EH_PE_udata8:
106 return 8;
108 __gxx_abort ();
111 #ifndef NO_BASE_OF_ENCODED_VALUE
113 /* Given an encoding and an _Unwind_Context, return the base to which
114 the encoding is relative. This base may then be passed to
115 read_encoded_value_with_base for use when the _Unwind_Context is
116 not available. */
118 STATIC _Unwind_Ptr
119 base_of_encoded_value (unsigned char encoding, struct _Unwind_Context *context)
121 if (encoding == DW_EH_PE_omit)
122 return 0;
124 switch (encoding & 0x70)
126 case DW_EH_PE_absptr:
127 case DW_EH_PE_pcrel:
128 case DW_EH_PE_aligned:
129 return 0;
131 case DW_EH_PE_textrel:
132 return _Unwind_GetTextRelBase (context);
133 case DW_EH_PE_datarel:
134 return _Unwind_GetDataRelBase (context);
135 case DW_EH_PE_funcrel:
136 return _Unwind_GetRegionStart (context);
138 __gxx_abort ();
141 #endif
143 /* Read an unsigned leb128 value from P, store the value in VAL, return
144 P incremented past the value. We assume that a word is large enough to
145 hold any value so encoded; if it is smaller than a pointer on some target,
146 pointers should not be leb128 encoded on that target. */
148 STATIC const unsigned char *
149 read_uleb128 (const unsigned char *p, _Unwind_Word *val)
151 unsigned int shift = 0;
152 unsigned char byte;
153 _Unwind_Word result;
155 result = 0;
158 byte = *p++;
159 result |= (byte & 0x7f) << shift;
160 shift += 7;
162 while (byte & 0x80);
164 *val = result;
165 return p;
168 /* Similar, but read a signed leb128 value. */
170 STATIC const unsigned char *
171 read_sleb128 (const unsigned char *p, _Unwind_Sword *val)
173 unsigned int shift = 0;
174 unsigned char byte;
175 _Unwind_Word result;
177 result = 0;
180 byte = *p++;
181 result |= (byte & 0x7f) << shift;
182 shift += 7;
184 while (byte & 0x80);
186 /* Sign-extend a negative value. */
187 if (shift < 8 * sizeof(result) && (byte & 0x40) != 0)
188 result |= -(1L << shift);
190 *val = (_Unwind_Sword) result;
191 return p;
194 /* Load an encoded value from memory at P. The value is returned in VAL;
195 The function returns P incremented past the value. BASE is as given
196 by base_of_encoded_value for this encoding in the appropriate context. */
198 STATIC const unsigned char *
199 read_encoded_value_with_base (unsigned char encoding, _Unwind_Ptr base,
200 const unsigned char *p, _Unwind_Ptr *val)
202 union unaligned
204 void *ptr;
205 unsigned u2 __attribute__ ((mode (HI)));
206 unsigned u4 __attribute__ ((mode (SI)));
207 unsigned u8 __attribute__ ((mode (DI)));
208 signed s2 __attribute__ ((mode (HI)));
209 signed s4 __attribute__ ((mode (SI)));
210 signed s8 __attribute__ ((mode (DI)));
211 } __attribute__((__packed__));
213 union unaligned *u = (union unaligned *) p;
214 _Unwind_Internal_Ptr result;
216 if (encoding == DW_EH_PE_aligned)
218 _Unwind_Internal_Ptr a = (_Unwind_Internal_Ptr) p;
219 a = (a + sizeof (void *) - 1) & - sizeof(void *);
220 result = *(_Unwind_Internal_Ptr *) a;
221 p = (const unsigned char *) (a + sizeof (void *));
223 else
225 switch (encoding & 0x0f)
227 case DW_EH_PE_absptr:
228 result = (_Unwind_Internal_Ptr) u->ptr;
229 p += sizeof (void *);
230 break;
232 case DW_EH_PE_uleb128:
234 _Unwind_Word tmp;
235 p = read_uleb128 (p, &tmp);
236 result = (_Unwind_Internal_Ptr) tmp;
238 break;
240 case DW_EH_PE_sleb128:
242 _Unwind_Sword tmp;
243 p = read_sleb128 (p, &tmp);
244 result = (_Unwind_Internal_Ptr) tmp;
246 break;
248 case DW_EH_PE_udata2:
249 result = u->u2;
250 p += 2;
251 break;
252 case DW_EH_PE_udata4:
253 result = u->u4;
254 p += 4;
255 break;
256 case DW_EH_PE_udata8:
257 result = u->u8;
258 p += 8;
259 break;
261 case DW_EH_PE_sdata2:
262 result = u->s2;
263 p += 2;
264 break;
265 case DW_EH_PE_sdata4:
266 result = u->s4;
267 p += 4;
268 break;
269 case DW_EH_PE_sdata8:
270 result = u->s8;
271 p += 8;
272 break;
274 default:
275 __gxx_abort ();
278 if (result != 0)
280 result += ((encoding & 0x70) == DW_EH_PE_pcrel
281 ? (_Unwind_Internal_Ptr) u : base);
282 if (encoding & DW_EH_PE_indirect)
283 result = *(_Unwind_Internal_Ptr *) result;
287 *val = result;
288 return p;
291 #ifndef NO_BASE_OF_ENCODED_VALUE
293 /* Like read_encoded_value_with_base, but get the base from the context
294 rather than providing it directly. */
296 STATIC const unsigned char *
297 read_encoded_value (struct _Unwind_Context *context, unsigned char encoding,
298 const unsigned char *p, _Unwind_Ptr *val)
300 return read_encoded_value_with_base (encoding,
301 base_of_encoded_value (encoding, context),
302 p, val);
305 #endif
306 #endif /* _LIBC */