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