1 //===------------------------------- 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
17 #include <__libunwind_config.h>
18
19 #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
28 typedef 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,
39 #if _LIBUNWIND_ARM_EHABI
40 _URC_FAILURE = 9
41 #endif
42 } _Unwind_Reason_Code;
43
44 typedef 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
52 typedef struct _Unwind_Context _Unwind_Context; // opaque
53
54 #if _LIBUNWIND_ARM_EHABI
55 typedef uint32_t _Unwind_State;
56
57 static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME = 0;
58 static const _Unwind_State _US_UNWIND_FRAME_STARTING = 1;
59 static const _Unwind_State _US_UNWIND_FRAME_RESUME = 2;
60 /* Undocumented flag for force unwinding. */
61 static const _Unwind_State _US_FORCE_UNWIND = 8;
62
63 typedef uint32_t _Unwind_EHT_Header;
64
65 struct _Unwind_Control_Block;
66 typedef struct _Unwind_Control_Block _Unwind_Control_Block;
67 typedef struct _Unwind_Control_Block _Unwind_Exception; /* Alias */
68
69 struct _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
104 typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
105 (_Unwind_State state,
106 _Unwind_Exception* exceptionObject,
107 struct _Unwind_Context* context);
108
109 typedef _Unwind_Reason_Code (*__personality_routine)
110 (_Unwind_State state,
111 _Unwind_Exception* exceptionObject,
112 struct _Unwind_Context* context);
113 #else
114 struct _Unwind_Context; // opaque
115 struct _Unwind_Exception; // forward declaration
116 typedef struct _Unwind_Exception _Unwind_Exception;
117
118 struct _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__
125 // The gcc implementation of _Unwind_Exception used attribute mode on the
126 // above fields which had the side effect of causing this whole struct to
127 // 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
131 };
132
133 typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
134 (int version,
135 _Unwind_Action actions,
136 uint64_t exceptionClass,
137 _Unwind_Exception* exceptionObject,
138 struct _Unwind_Context* context,
139 void* stop_parameter );
140
141 typedef _Unwind_Reason_Code (*__personality_routine)
142 (int version,
143 _Unwind_Action actions,
144 uint64_t exceptionClass,
145 _Unwind_Exception* exceptionObject,
146 struct _Unwind_Context* context);
147 #endif
148
149 #ifdef __cplusplus
150 extern "C" {
151 #endif
152
153 //
154 // The following are the base functions documented by the C++ ABI
155 //
156 #ifdef __USING_SJLJ_EXCEPTIONS__
157 extern _Unwind_Reason_Code
158 _Unwind_SjLj_RaiseException(_Unwind_Exception *exception_object);
159 extern void _Unwind_SjLj_Resume(_Unwind_Exception *exception_object);
160 #else
161 extern _Unwind_Reason_Code
162 _Unwind_RaiseException(_Unwind_Exception *exception_object);
163 extern void _Unwind_Resume(_Unwind_Exception *exception_object);
164 #endif
165 extern void _Unwind_DeleteException(_Unwind_Exception *exception_object);
166
167 #if _LIBUNWIND_ARM_EHABI
168 typedef enum {
169 _UVRSC_CORE = 0, /* integer register */
170 _UVRSC_VFP = 1, /* vfp */
171 _UVRSC_WMMXD = 3, /* Intel WMMX data register */
172 _UVRSC_WMMXC = 4 /* Intel WMMX control register */
173 } _Unwind_VRS_RegClass;
174
175 typedef enum {
176 _UVRSD_UINT32 = 0,
177 _UVRSD_VFPX = 1,
178 _UVRSD_UINT64 = 3,
179 _UVRSD_FLOAT = 4,
180 _UVRSD_DOUBLE = 5
181 } _Unwind_VRS_DataRepresentation;
182
183 typedef enum {
184 _UVRSR_OK = 0,
185 _UVRSR_NOT_IMPLEMENTED = 1,
186 _UVRSR_FAILED = 2
187 } _Unwind_VRS_Result;
188
189 extern void _Unwind_Complete(_Unwind_Exception* exception_object);
190
191 extern _Unwind_VRS_Result
192 _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
193 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
194 void *valuep);
195
196 extern _Unwind_VRS_Result
197 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
198 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
199 void *valuep);
200
201 extern _Unwind_VRS_Result
202 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
203 uint32_t discriminator,
204 _Unwind_VRS_DataRepresentation representation);
205 #endif
206
207 #if !_LIBUNWIND_ARM_EHABI
208
209 extern uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index);
210 extern void _Unwind_SetGR(struct _Unwind_Context *context, int index,
211 uintptr_t new_value);
212 extern uintptr_t _Unwind_GetIP(struct _Unwind_Context *context);
213 extern void _Unwind_SetIP(struct _Unwind_Context *, uintptr_t new_value);
214
215 #else // _LIBUNWIND_ARM_EHABI
216
217 #if defined(_LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE)
218 #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 extern
219 #else
220 #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 static __inline__
221 #endif
222
223 // These are de facto helper functions for ARM, which delegate the function
224 // calls to _Unwind_VRS_Get/Set(). These are not a part of ARM EHABI
225 // specification, thus these function MUST be inlined. Please don't replace
226 // these with the "extern" function declaration; otherwise, the program
227 // including this <unwind.h> header won't be ABI compatible and will result in
228 // link error when we are linking the program with libgcc.
229
230 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetGR(struct _Unwind_Context * context,int index)231 uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index) {
232 uintptr_t value = 0;
233 _Unwind_VRS_Get(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
234 return value;
235 }
236
237 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetGR(struct _Unwind_Context * context,int index,uintptr_t value)238 void _Unwind_SetGR(struct _Unwind_Context *context, int index,
239 uintptr_t value) {
240 _Unwind_VRS_Set(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
241 }
242
243 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetIP(struct _Unwind_Context * context)244 uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
245 // remove the thumb-bit before returning
246 return _Unwind_GetGR(context, 15) & (~(uintptr_t)0x1);
247 }
248
249 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetIP(struct _Unwind_Context * context,uintptr_t value)250 void _Unwind_SetIP(struct _Unwind_Context *context, uintptr_t value) {
251 uintptr_t thumb_bit = _Unwind_GetGR(context, 15) & ((uintptr_t)0x1);
252 _Unwind_SetGR(context, 15, value | thumb_bit);
253 }
254 #endif // _LIBUNWIND_ARM_EHABI
255
256 extern uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context *context);
257 extern uintptr_t
258 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context);
259 #ifdef __USING_SJLJ_EXCEPTIONS__
260 extern _Unwind_Reason_Code
261 _Unwind_SjLj_ForcedUnwind(_Unwind_Exception *exception_object,
262 _Unwind_Stop_Fn stop, void *stop_parameter);
263 #else
264 extern _Unwind_Reason_Code
265 _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
266 _Unwind_Stop_Fn stop, void *stop_parameter);
267 #endif
268
269 #ifdef __USING_SJLJ_EXCEPTIONS__
270 typedef struct _Unwind_FunctionContext *_Unwind_FunctionContext_t;
271 extern void _Unwind_SjLj_Register(_Unwind_FunctionContext_t fc);
272 extern void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t fc);
273 #endif
274
275 //
276 // The following are semi-suppoted extensions to the C++ ABI
277 //
278
279 //
280 // called by __cxa_rethrow().
281 //
282 #ifdef __USING_SJLJ_EXCEPTIONS__
283 extern _Unwind_Reason_Code
284 _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception *exception_object);
285 #else
286 extern _Unwind_Reason_Code
287 _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object);
288 #endif
289
290 // _Unwind_Backtrace() is a gcc extension that walks the stack and calls the
291 // _Unwind_Trace_Fn once per frame until it reaches the bottom of the stack
292 // or the _Unwind_Trace_Fn function returns something other than _URC_NO_REASON.
293 typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
294 void *);
295 extern _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
296
297 // _Unwind_GetCFA is a gcc extension that can be called from within a
298 // personality handler to get the CFA (stack pointer before call) of
299 // current frame.
300 extern uintptr_t _Unwind_GetCFA(struct _Unwind_Context *);
301
302
303 // _Unwind_GetIPInfo is a gcc extension that can be called from within a
304 // personality handler. Similar to _Unwind_GetIP() but also returns in
305 // *ipBefore a non-zero value if the instruction pointer is at or before the
306 // instruction causing the unwind. Normally, in a function call, the IP returned
307 // is the return address which is after the call instruction and may be past the
308 // end of the function containing the call instruction.
309 extern uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
310 int *ipBefore);
311
312
313 // __register_frame() is used with dynamically generated code to register the
314 // FDE for a generated (JIT) code. The FDE must use pc-rel addressing to point
315 // to its function and optional LSDA.
316 // __register_frame() has existed in all versions of Mac OS X, but in 10.4 and
317 // 10.5 it was buggy and did not actually register the FDE with the unwinder.
318 // In 10.6 and later it does register properly.
319 extern void __register_frame(const void *fde);
320 extern void __deregister_frame(const void *fde);
321
322 // _Unwind_Find_FDE() will locate the FDE if the pc is in some function that has
323 // an associated FDE. Note, Mac OS X 10.6 and later, introduces "compact unwind
324 // info" which the runtime uses in preference to dwarf unwind info. This
325 // function will only work if the target function has an FDE but no compact
326 // unwind info.
327 struct dwarf_eh_bases {
328 uintptr_t tbase;
329 uintptr_t dbase;
330 uintptr_t func;
331 };
332 extern const void *_Unwind_Find_FDE(const void *pc, struct dwarf_eh_bases *);
333
334
335 // This function attempts to find the start (address of first instruction) of
336 // a function given an address inside the function. It only works if the
337 // function has an FDE (dwarf unwind info).
338 // This function is unimplemented on Mac OS X 10.6 and later. Instead, use
339 // _Unwind_Find_FDE() and look at the dwarf_eh_bases.func result.
340 extern void *_Unwind_FindEnclosingFunction(void *pc);
341
342 // Mac OS X does not support text-rel and data-rel addressing so these functions
343 // are unimplemented
344 extern uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context *context)
345 LIBUNWIND_UNAVAIL;
346 extern uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context *context)
347 LIBUNWIND_UNAVAIL;
348
349 // Mac OS X 10.4 and 10.5 had implementations of these functions in
350 // libgcc_s.dylib, but they never worked.
351 /// These functions are no longer available on Mac OS X.
352 extern void __register_frame_info_bases(const void *fde, void *ob, void *tb,
353 void *db) LIBUNWIND_UNAVAIL;
354 extern void __register_frame_info(const void *fde, void *ob)
355 LIBUNWIND_UNAVAIL;
356 extern void __register_frame_info_table_bases(const void *fde, void *ob,
357 void *tb, void *db)
358 LIBUNWIND_UNAVAIL;
359 extern void __register_frame_info_table(const void *fde, void *ob)
360 LIBUNWIND_UNAVAIL;
361 extern void __register_frame_table(const void *fde)
362 LIBUNWIND_UNAVAIL;
363 extern void *__deregister_frame_info(const void *fde)
364 LIBUNWIND_UNAVAIL;
365 extern void *__deregister_frame_info_bases(const void *fde)
366 LIBUNWIND_UNAVAIL;
367
368 #ifdef __cplusplus
369 }
370 #endif
371
372 #endif // __UNWIND_H__
373