1 //===------------------------------- unwind.h -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //
8 // C++ ABI Level 1 ABI documented at:
9 // http://mentorembedded.github.io/cxx-abi/abi-eh.html
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef __UNWIND_H__
14 #define __UNWIND_H__
15
16 #include <__libunwind_config.h>
17
18 #include <stdint.h>
19 #include <stddef.h>
20
21 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) && defined(_WIN32)
22 #include <windows.h>
23 #include <ntverp.h>
24 #endif
25
26 #if defined(__APPLE__)
27 #define LIBUNWIND_UNAVAIL __attribute__ (( unavailable ))
28 #else
29 #define LIBUNWIND_UNAVAIL
30 #endif
31
32 typedef enum {
33 _URC_NO_REASON = 0,
34 _URC_OK = 0,
35 _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
36 _URC_FATAL_PHASE2_ERROR = 2,
37 _URC_FATAL_PHASE1_ERROR = 3,
38 _URC_NORMAL_STOP = 4,
39 _URC_END_OF_STACK = 5,
40 _URC_HANDLER_FOUND = 6,
41 _URC_INSTALL_CONTEXT = 7,
42 _URC_CONTINUE_UNWIND = 8,
43 #if defined(_LIBUNWIND_ARM_EHABI)
44 _URC_FAILURE = 9
45 #endif
46 } _Unwind_Reason_Code;
47
48 typedef enum {
49 _UA_SEARCH_PHASE = 1,
50 _UA_CLEANUP_PHASE = 2,
51 _UA_HANDLER_FRAME = 4,
52 _UA_FORCE_UNWIND = 8,
53 _UA_END_OF_STACK = 16 // gcc extension to C++ ABI
54 } _Unwind_Action;
55
56 typedef struct _Unwind_Context _Unwind_Context; // opaque
57
58 #if defined(_LIBUNWIND_ARM_EHABI)
59 typedef uint32_t _Unwind_State;
60
61 static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME = 0;
62 static const _Unwind_State _US_UNWIND_FRAME_STARTING = 1;
63 static const _Unwind_State _US_UNWIND_FRAME_RESUME = 2;
64 static const _Unwind_State _US_ACTION_MASK = 3;
65 /* Undocumented flag for force unwinding. */
66 static const _Unwind_State _US_FORCE_UNWIND = 8;
67
68 typedef uint32_t _Unwind_EHT_Header;
69
70 struct _Unwind_Control_Block;
71 typedef struct _Unwind_Control_Block _Unwind_Control_Block;
72 typedef struct _Unwind_Control_Block _Unwind_Exception; /* Alias */
73
74 struct _Unwind_Control_Block {
75 uint64_t exception_class;
76 void (*exception_cleanup)(_Unwind_Reason_Code, _Unwind_Control_Block*);
77
78 /* Unwinder cache, private fields for the unwinder's use */
79 struct {
80 uint32_t reserved1; /* init reserved1 to 0, then don't touch */
81 uint32_t reserved2;
82 uint32_t reserved3;
83 uint32_t reserved4;
84 uint32_t reserved5;
85 } unwinder_cache;
86
87 /* Propagation barrier cache (valid after phase 1): */
88 struct {
89 uint32_t sp;
90 uint32_t bitpattern[5];
91 } barrier_cache;
92
93 /* Cleanup cache (preserved over cleanup): */
94 struct {
95 uint32_t bitpattern[4];
96 } cleanup_cache;
97
98 /* Pr cache (for pr's benefit): */
99 struct {
100 uint32_t fnstart; /* function start address */
101 _Unwind_EHT_Header* ehtp; /* pointer to EHT entry header word */
102 uint32_t additional;
103 uint32_t reserved1;
104 } pr_cache;
105
106 long long int :0; /* Enforce the 8-byte alignment */
107 } __attribute__((__aligned__(8)));
108
109 typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
110 (_Unwind_State state,
111 _Unwind_Exception* exceptionObject,
112 struct _Unwind_Context* context);
113
114 typedef _Unwind_Reason_Code (*__personality_routine)
115 (_Unwind_State state,
116 _Unwind_Exception* exceptionObject,
117 struct _Unwind_Context* context);
118 #else
119 struct _Unwind_Context; // opaque
120 struct _Unwind_Exception; // forward declaration
121 typedef struct _Unwind_Exception _Unwind_Exception;
122
123 struct _Unwind_Exception {
124 uint64_t exception_class;
125 void (*exception_cleanup)(_Unwind_Reason_Code reason,
126 _Unwind_Exception *exc);
127 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
128 uintptr_t private_[6];
129 #else
130 uintptr_t private_1; // non-zero means forced unwind
131 uintptr_t private_2; // holds sp that phase1 found for phase2 to use
132 #endif
133 #if __SIZEOF_POINTER__ == 4
134 // The implementation of _Unwind_Exception uses an attribute mode on the
135 // above fields which has the side effect of causing this whole struct to
136 // round up to 32 bytes in size (48 with SEH). To be more explicit, we add
137 // pad fields added for binary compatibility.
138 uint32_t reserved[3];
139 #endif
140 // The Itanium ABI requires that _Unwind_Exception objects are "double-word
141 // aligned". GCC has interpreted this to mean "use the maximum useful
142 // alignment for the target"; so do we.
143 } __attribute__((__aligned__));
144
145 typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
146 (int version,
147 _Unwind_Action actions,
148 uint64_t exceptionClass,
149 _Unwind_Exception* exceptionObject,
150 struct _Unwind_Context* context,
151 void* stop_parameter );
152
153 typedef _Unwind_Reason_Code (*__personality_routine)
154 (int version,
155 _Unwind_Action actions,
156 uint64_t exceptionClass,
157 _Unwind_Exception* exceptionObject,
158 struct _Unwind_Context* context);
159 #endif
160
161 #ifdef __cplusplus
162 extern "C" {
163 #endif
164
165 //
166 // The following are the base functions documented by the C++ ABI
167 //
168 #ifdef __USING_SJLJ_EXCEPTIONS__
169 extern _Unwind_Reason_Code
170 _Unwind_SjLj_RaiseException(_Unwind_Exception *exception_object);
171 extern void _Unwind_SjLj_Resume(_Unwind_Exception *exception_object);
172 #else
173 extern _Unwind_Reason_Code
174 _Unwind_RaiseException(_Unwind_Exception *exception_object);
175 extern void _Unwind_Resume(_Unwind_Exception *exception_object);
176 #endif
177 extern void _Unwind_DeleteException(_Unwind_Exception *exception_object);
178
179 #if defined(_LIBUNWIND_ARM_EHABI)
180 typedef enum {
181 _UVRSC_CORE = 0, /* integer register */
182 _UVRSC_VFP = 1, /* vfp */
183 _UVRSC_WMMXD = 3, /* Intel WMMX data register */
184 _UVRSC_WMMXC = 4 /* Intel WMMX control register */
185 } _Unwind_VRS_RegClass;
186
187 typedef enum {
188 _UVRSD_UINT32 = 0,
189 _UVRSD_VFPX = 1,
190 _UVRSD_UINT64 = 3,
191 _UVRSD_FLOAT = 4,
192 _UVRSD_DOUBLE = 5
193 } _Unwind_VRS_DataRepresentation;
194
195 typedef enum {
196 _UVRSR_OK = 0,
197 _UVRSR_NOT_IMPLEMENTED = 1,
198 _UVRSR_FAILED = 2
199 } _Unwind_VRS_Result;
200
201 extern void _Unwind_Complete(_Unwind_Exception* exception_object);
202
203 extern _Unwind_VRS_Result
204 _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
205 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
206 void *valuep);
207
208 extern _Unwind_VRS_Result
209 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
210 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
211 void *valuep);
212
213 extern _Unwind_VRS_Result
214 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
215 uint32_t discriminator,
216 _Unwind_VRS_DataRepresentation representation);
217 #endif
218
219 #if !defined(_LIBUNWIND_ARM_EHABI)
220
221 extern uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index);
222 extern void _Unwind_SetGR(struct _Unwind_Context *context, int index,
223 uintptr_t new_value);
224 extern uintptr_t _Unwind_GetIP(struct _Unwind_Context *context);
225 extern void _Unwind_SetIP(struct _Unwind_Context *, uintptr_t new_value);
226
227 #else // defined(_LIBUNWIND_ARM_EHABI)
228
229 #if defined(_LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE)
230 #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 extern
231 #else
232 #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 static __inline__
233 #endif
234
235 // These are de facto helper functions for ARM, which delegate the function
236 // calls to _Unwind_VRS_Get/Set(). These are not a part of ARM EHABI
237 // specification, thus these function MUST be inlined. Please don't replace
238 // these with the "extern" function declaration; otherwise, the program
239 // including this <unwind.h> header won't be ABI compatible and will result in
240 // link error when we are linking the program with libgcc.
241
242 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetGR(struct _Unwind_Context * context,int index)243 uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index) {
244 uintptr_t value = 0;
245 _Unwind_VRS_Get(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
246 return value;
247 }
248
249 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetGR(struct _Unwind_Context * context,int index,uintptr_t value)250 void _Unwind_SetGR(struct _Unwind_Context *context, int index,
251 uintptr_t value) {
252 _Unwind_VRS_Set(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
253 }
254
255 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetIP(struct _Unwind_Context * context)256 uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
257 // remove the thumb-bit before returning
258 return _Unwind_GetGR(context, 15) & (~(uintptr_t)0x1);
259 }
260
261 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetIP(struct _Unwind_Context * context,uintptr_t value)262 void _Unwind_SetIP(struct _Unwind_Context *context, uintptr_t value) {
263 uintptr_t thumb_bit = _Unwind_GetGR(context, 15) & ((uintptr_t)0x1);
264 _Unwind_SetGR(context, 15, value | thumb_bit);
265 }
266 #endif // defined(_LIBUNWIND_ARM_EHABI)
267
268 extern uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context *context);
269 extern uintptr_t
270 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context);
271 #ifdef __USING_SJLJ_EXCEPTIONS__
272 extern _Unwind_Reason_Code
273 _Unwind_SjLj_ForcedUnwind(_Unwind_Exception *exception_object,
274 _Unwind_Stop_Fn stop, void *stop_parameter);
275 #else
276 extern _Unwind_Reason_Code
277 _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
278 _Unwind_Stop_Fn stop, void *stop_parameter);
279 #endif
280
281 #ifdef __USING_SJLJ_EXCEPTIONS__
282 typedef struct _Unwind_FunctionContext *_Unwind_FunctionContext_t;
283 extern void _Unwind_SjLj_Register(_Unwind_FunctionContext_t fc);
284 extern void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t fc);
285 #endif
286
287 //
288 // The following are semi-suppoted extensions to the C++ ABI
289 //
290
291 //
292 // called by __cxa_rethrow().
293 //
294 #ifdef __USING_SJLJ_EXCEPTIONS__
295 extern _Unwind_Reason_Code
296 _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception *exception_object);
297 #else
298 extern _Unwind_Reason_Code
299 _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object);
300 #endif
301
302 // _Unwind_Backtrace() is a gcc extension that walks the stack and calls the
303 // _Unwind_Trace_Fn once per frame until it reaches the bottom of the stack
304 // or the _Unwind_Trace_Fn function returns something other than _URC_NO_REASON.
305 typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
306 void *);
307 extern _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
308
309 // _Unwind_GetCFA is a gcc extension that can be called from within a
310 // personality handler to get the CFA (stack pointer before call) of
311 // current frame.
312 extern uintptr_t _Unwind_GetCFA(struct _Unwind_Context *);
313
314
315 // _Unwind_GetIPInfo is a gcc extension that can be called from within a
316 // personality handler. Similar to _Unwind_GetIP() but also returns in
317 // *ipBefore a non-zero value if the instruction pointer is at or before the
318 // instruction causing the unwind. Normally, in a function call, the IP returned
319 // is the return address which is after the call instruction and may be past the
320 // end of the function containing the call instruction.
321 extern uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
322 int *ipBefore);
323
324
325 // __register_frame() is used with dynamically generated code to register the
326 // FDE for a generated (JIT) code. The FDE must use pc-rel addressing to point
327 // to its function and optional LSDA.
328 // __register_frame() has existed in all versions of Mac OS X, but in 10.4 and
329 // 10.5 it was buggy and did not actually register the FDE with the unwinder.
330 // In 10.6 and later it does register properly.
331 extern void __register_frame(const void *fde);
332 extern void __deregister_frame(const void *fde);
333
334 // _Unwind_Find_FDE() will locate the FDE if the pc is in some function that has
335 // an associated FDE. Note, Mac OS X 10.6 and later, introduces "compact unwind
336 // info" which the runtime uses in preference to DWARF unwind info. This
337 // function will only work if the target function has an FDE but no compact
338 // unwind info.
339 struct dwarf_eh_bases {
340 uintptr_t tbase;
341 uintptr_t dbase;
342 uintptr_t func;
343 };
344 extern const void *_Unwind_Find_FDE(const void *pc, struct dwarf_eh_bases *);
345
346
347 // This function attempts to find the start (address of first instruction) of
348 // a function given an address inside the function. It only works if the
349 // function has an FDE (DWARF unwind info).
350 // This function is unimplemented on Mac OS X 10.6 and later. Instead, use
351 // _Unwind_Find_FDE() and look at the dwarf_eh_bases.func result.
352 extern void *_Unwind_FindEnclosingFunction(void *pc);
353
354 // Mac OS X does not support text-rel and data-rel addressing so these functions
355 // are unimplemented
356 extern uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context *context)
357 LIBUNWIND_UNAVAIL;
358 extern uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context *context)
359 LIBUNWIND_UNAVAIL;
360
361 // Mac OS X 10.4 and 10.5 had implementations of these functions in
362 // libgcc_s.dylib, but they never worked.
363 /// These functions are no longer available on Mac OS X.
364 extern void __register_frame_info_bases(const void *fde, void *ob, void *tb,
365 void *db) LIBUNWIND_UNAVAIL;
366 extern void __register_frame_info(const void *fde, void *ob)
367 LIBUNWIND_UNAVAIL;
368 extern void __register_frame_info_table_bases(const void *fde, void *ob,
369 void *tb, void *db)
370 LIBUNWIND_UNAVAIL;
371 extern void __register_frame_info_table(const void *fde, void *ob)
372 LIBUNWIND_UNAVAIL;
373 extern void __register_frame_table(const void *fde)
374 LIBUNWIND_UNAVAIL;
375 extern void *__deregister_frame_info(const void *fde)
376 LIBUNWIND_UNAVAIL;
377 extern void *__deregister_frame_info_bases(const void *fde)
378 LIBUNWIND_UNAVAIL;
379
380 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
381 #ifndef _WIN32
382 typedef struct _EXCEPTION_RECORD EXCEPTION_RECORD;
383 typedef struct _CONTEXT CONTEXT;
384 typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
385 #elif !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
386 typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
387 #endif
388 // This is the common wrapper for GCC-style personality functions with SEH.
389 extern EXCEPTION_DISPOSITION _GCC_specific_handler(EXCEPTION_RECORD *exc,
390 void *frame,
391 CONTEXT *ctx,
392 DISPATCHER_CONTEXT *disp,
393 __personality_routine pers);
394 #endif
395
396 #ifdef __cplusplus
397 }
398 #endif
399
400 #endif // __UNWIND_H__
401