1 /* Exception handling and frame unwind runtime interface routines.
2 Copyright (C) 2001-2023 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, see
18 <https://www.gnu.org/licenses/>. */
20 /* @@@ Really this should be out of line, but this also causes link
21 compatibility problems with the base ABI. This is slightly better
22 than duplicating code, however. */
24 /* If using C++, references to abort have to be qualified with std::. */
26 #define __gxx_abort std::abort
28 #define __gxx_abort abort
31 /* Pointer encodings, from dwarf2.h. */
32 #define DW_EH_PE_absptr 0x00
33 #define DW_EH_PE_omit 0xff
35 #define DW_EH_PE_uleb128 0x01
36 #define DW_EH_PE_udata2 0x02
37 #define DW_EH_PE_udata4 0x03
38 #define DW_EH_PE_udata8 0x04
39 #define DW_EH_PE_sleb128 0x09
40 #define DW_EH_PE_sdata2 0x0A
41 #define DW_EH_PE_sdata4 0x0B
42 #define DW_EH_PE_sdata8 0x0C
43 #define DW_EH_PE_signed 0x08
45 #define DW_EH_PE_pcrel 0x10
46 #define DW_EH_PE_textrel 0x20
47 #define DW_EH_PE_datarel 0x30
48 #define DW_EH_PE_funcrel 0x40
49 #define DW_EH_PE_aligned 0x50
51 #define DW_EH_PE_indirect 0x80
57 extern unsigned int size_of_encoded_value (unsigned char encoding
)
60 extern const unsigned char *read_encoded_value_with_base
61 (unsigned char encoding
, _Unwind_Ptr base
,
62 const unsigned char *p
, _Unwind_Ptr
*val
)
65 extern const unsigned char * read_encoded_value
66 (struct _Unwind_Context
*context
, unsigned char encoding
,
67 const unsigned char *p
, _Unwind_Ptr
*val
)
70 extern const unsigned char * read_uleb128 (const unsigned char *p
,
73 extern const unsigned char * read_sleb128 (const unsigned char *p
,
78 #if defined(_LIBC) && defined(_LIBC_DEFINITIONS)
86 /* Given an encoding, return the number of bytes the format occupies.
87 This is only defined for fixed-size encodings, and so does not
91 size_of_encoded_value (unsigned char encoding
)
93 if (encoding
== DW_EH_PE_omit
)
96 switch (encoding
& 0x07)
99 return sizeof (void *);
100 case DW_EH_PE_udata2
:
102 case DW_EH_PE_udata4
:
104 case DW_EH_PE_udata8
:
110 #ifndef NO_BASE_OF_ENCODED_VALUE
112 /* Given an encoding and an _Unwind_Context, return the base to which
113 the encoding is relative. This base may then be passed to
114 read_encoded_value_with_base for use when the _Unwind_Context is
118 base_of_encoded_value (unsigned char encoding
, struct _Unwind_Context
*context
)
120 if (encoding
== DW_EH_PE_omit
)
123 switch (encoding
& 0x70)
125 case DW_EH_PE_absptr
:
127 case DW_EH_PE_aligned
:
130 case DW_EH_PE_textrel
:
131 return _Unwind_GetTextRelBase (context
);
132 case DW_EH_PE_datarel
:
133 return _Unwind_GetDataRelBase (context
);
134 case DW_EH_PE_funcrel
:
135 return _Unwind_GetRegionStart (context
);
142 /* Read an unsigned leb128 value from P, store the value in VAL, return
143 P incremented past the value. We assume that a word is large enough to
144 hold any value so encoded; if it is smaller than a pointer on some target,
145 pointers should not be leb128 encoded on that target. */
147 STATIC
const unsigned char *
148 read_uleb128 (const unsigned char *p
, _Unwind_Word
*val
)
150 unsigned int shift
= 0;
158 result
|= (byte
& 0x7f) << shift
;
167 /* Similar, but read a signed leb128 value. */
169 STATIC
const unsigned char *
170 read_sleb128 (const unsigned char *p
, _Unwind_Sword
*val
)
172 unsigned int shift
= 0;
180 result
|= (byte
& 0x7f) << shift
;
185 /* Sign-extend a negative value. */
186 if (shift
< 8 * sizeof (result
) && (byte
& 0x40) != 0)
187 result
|= -(1L << shift
);
189 *val
= (_Unwind_Sword
) result
;
193 /* Load an encoded value from memory at P. The value is returned in VAL;
194 The function returns P incremented past the value. BASE is as given
195 by base_of_encoded_value for this encoding in the appropriate context. */
197 STATIC
const unsigned char *
198 read_encoded_value_with_base (unsigned char encoding
, _Unwind_Ptr base
,
199 const unsigned char *p
, _Unwind_Ptr
*val
)
204 unsigned u2
__attribute__ ((mode (HI
)));
205 unsigned u4
__attribute__ ((mode (SI
)));
206 unsigned u8
__attribute__ ((mode (DI
)));
207 signed s2
__attribute__ ((mode (HI
)));
208 signed s4
__attribute__ ((mode (SI
)));
209 signed s8
__attribute__ ((mode (DI
)));
210 } __attribute__((__packed__
));
212 union unaligned
*u
= (union unaligned
*) p
;
213 _Unwind_Internal_Ptr result
;
215 if (encoding
== DW_EH_PE_aligned
)
217 _Unwind_Internal_Ptr a
= (_Unwind_Internal_Ptr
) p
;
218 a
= (a
+ sizeof (void *) - 1) & - sizeof (void *);
219 result
= *(_Unwind_Internal_Ptr
*) a
;
220 p
= (const unsigned char *) (a
+ sizeof (void *));
224 switch (encoding
& 0x0f)
226 case DW_EH_PE_absptr
:
227 result
= (_Unwind_Internal_Ptr
) u
->ptr
;
228 p
+= sizeof (void *);
231 case DW_EH_PE_uleb128
:
234 p
= read_uleb128 (p
, &tmp
);
235 result
= (_Unwind_Internal_Ptr
) tmp
;
239 case DW_EH_PE_sleb128
:
242 p
= read_sleb128 (p
, &tmp
);
243 result
= (_Unwind_Internal_Ptr
) tmp
;
247 case DW_EH_PE_udata2
:
251 case DW_EH_PE_udata4
:
255 case DW_EH_PE_udata8
:
260 case DW_EH_PE_sdata2
:
264 case DW_EH_PE_sdata4
:
268 case DW_EH_PE_sdata8
:
279 result
+= ((encoding
& 0x70) == DW_EH_PE_pcrel
280 ? (_Unwind_Internal_Ptr
) u
: base
);
281 if (encoding
& DW_EH_PE_indirect
)
282 result
= *(_Unwind_Internal_Ptr
*) result
;
290 #ifndef NO_BASE_OF_ENCODED_VALUE
292 /* Like read_encoded_value_with_base, but get the base from the context
293 rather than providing it directly. */
295 STATIC
const unsigned char *
296 read_encoded_value (struct _Unwind_Context
*context
, unsigned char encoding
,
297 const unsigned char *p
, _Unwind_Ptr
*val
)
299 return read_encoded_value_with_base (encoding
,
300 base_of_encoded_value (encoding
, context
),