Update.
[glibc.git] / sysdeps / generic / unwind-pe.h
blobe4a564e3e1dc8c5ea34f38815f02f4f8cb3c0926
1 /* Exception handling and frame unwind runtime interface routines.
2 Copyright (C) 2001 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 /* Given an encoding, return the number of bytes the format occupies.
56 This is only defined for fixed-size encodings, and so does not
57 include leb128. */
59 #ifndef _LIBC
60 static
61 #endif
62 unsigned int
63 size_of_encoded_value (unsigned char encoding)
64 #if defined(_LIBC) && !defined(NO_BASE_OF_ENCODED_VALUE)
66 #else
68 if (encoding == DW_EH_PE_omit)
69 return 0;
71 switch (encoding & 0x07)
73 case DW_EH_PE_absptr:
74 return sizeof (void *);
75 case DW_EH_PE_udata2:
76 return 2;
77 case DW_EH_PE_udata4:
78 return 4;
79 case DW_EH_PE_udata8:
80 return 8;
82 __gxx_abort ();
84 #endif
86 #ifndef NO_BASE_OF_ENCODED_VALUE
88 /* Given an encoding and an _Unwind_Context, return the base to which
89 the encoding is relative. This base may then be passed to
90 read_encoded_value_with_base for use when the _Unwind_Context is
91 not available. */
93 static _Unwind_Ptr
94 base_of_encoded_value (unsigned char encoding, struct _Unwind_Context *context)
96 if (encoding == DW_EH_PE_omit)
97 return 0;
99 switch (encoding & 0x70)
101 case DW_EH_PE_absptr:
102 case DW_EH_PE_pcrel:
103 case DW_EH_PE_aligned:
104 return 0;
106 case DW_EH_PE_textrel:
107 return _Unwind_GetTextRelBase (context);
108 case DW_EH_PE_datarel:
109 return _Unwind_GetDataRelBase (context);
110 case DW_EH_PE_funcrel:
111 return _Unwind_GetRegionStart (context);
113 __gxx_abort ();
116 #endif
118 /* Load an encoded value from memory at P. The value is returned in VAL;
119 The function returns P incremented past the value. BASE is as given
120 by base_of_encoded_value for this encoding in the appropriate context. */
122 #ifndef _LIBC
123 static
124 #endif
125 const unsigned char *
126 read_encoded_value_with_base (unsigned char encoding, _Unwind_Ptr base,
127 const unsigned char *p, _Unwind_Ptr *val)
128 #if defined(_LIBC) && !defined(NO_BASE_OF_ENCODED_VALUE)
130 #else
132 union unaligned
134 void *ptr;
135 unsigned u2 __attribute__ ((mode (HI)));
136 unsigned u4 __attribute__ ((mode (SI)));
137 unsigned u8 __attribute__ ((mode (DI)));
138 signed s2 __attribute__ ((mode (HI)));
139 signed s4 __attribute__ ((mode (SI)));
140 signed s8 __attribute__ ((mode (DI)));
141 } __attribute__((__packed__));
143 union unaligned *u = (union unaligned *) p;
144 _Unwind_Ptr result;
146 if (encoding == DW_EH_PE_aligned)
148 _Unwind_Ptr a = (_Unwind_Ptr)p;
149 a = (a + sizeof (void *) - 1) & - sizeof(void *);
150 result = *(_Unwind_Ptr *) a;
151 p = (const unsigned char *)(a + sizeof (void *));
153 else
155 switch (encoding & 0x0f)
157 case DW_EH_PE_absptr:
158 result = (_Unwind_Ptr) u->ptr;
159 p += sizeof (void *);
160 break;
162 case DW_EH_PE_uleb128:
164 unsigned int shift = 0;
165 unsigned char byte;
167 result = 0;
170 byte = *p++;
171 result |= (_Unwind_Ptr)(byte & 0x7f) << shift;
172 shift += 7;
174 while (byte & 0x80);
176 break;
178 case DW_EH_PE_sleb128:
180 unsigned int shift = 0;
181 unsigned char byte;
183 result = 0;
186 byte = *p++;
187 result |= (_Unwind_Ptr)(byte & 0x7f) << shift;
188 shift += 7;
190 while (byte & 0x80);
192 if (shift < 8 * sizeof(result) && (byte & 0x40) != 0)
193 result |= -(1L << shift);
195 break;
197 case DW_EH_PE_udata2:
198 result = u->u2;
199 p += 2;
200 break;
201 case DW_EH_PE_udata4:
202 result = u->u4;
203 p += 4;
204 break;
205 case DW_EH_PE_udata8:
206 result = u->u8;
207 p += 8;
208 break;
210 case DW_EH_PE_sdata2:
211 result = u->s2;
212 p += 2;
213 break;
214 case DW_EH_PE_sdata4:
215 result = u->s4;
216 p += 4;
217 break;
218 case DW_EH_PE_sdata8:
219 result = u->s8;
220 p += 8;
221 break;
223 default:
224 __gxx_abort ();
227 if (result != 0)
229 result += ((encoding & 0x70) == DW_EH_PE_pcrel
230 ? (_Unwind_Ptr)u : base);
231 if (encoding & DW_EH_PE_indirect)
232 result = *(_Unwind_Ptr *)result;
236 *val = result;
237 return p;
239 #endif
241 #ifndef NO_BASE_OF_ENCODED_VALUE
243 /* Like read_encoded_value_with_base, but get the base from the context
244 rather than providing it directly. */
246 static inline const unsigned char *
247 read_encoded_value (struct _Unwind_Context *context, unsigned char encoding,
248 const unsigned char *p, _Unwind_Ptr *val)
250 return read_encoded_value_with_base (encoding,
251 base_of_encoded_value (encoding, context),
252 p, val);
255 #endif
257 /* Read an unsigned leb128 value from P, store the value in VAL, return
258 P incremented past the value. */
260 static inline const unsigned char *
261 read_uleb128 (const unsigned char *p, _Unwind_Ptr *val)
263 return read_encoded_value_with_base (DW_EH_PE_uleb128, 0, p, val);
266 /* Similar, but read a signed leb128 value. */
268 static inline const unsigned char *
269 read_sleb128 (const unsigned char *p, _Unwind_Ptr *val)
271 return read_encoded_value_with_base (DW_EH_PE_sleb128, 0, p, val);