unwind.h revision e45805f0d3f8dafef1297cc7dc49e610713f023b
132d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//===------------------------------- unwind.h -----------------------------===//
232d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//
332d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//                     The LLVM Compiler Infrastructure
432d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//
532d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org// This file is dual licensed under the MIT and the University of Illinois Open
632d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org// Source Licenses. See LICENSE.TXT for details.
732d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//
832d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//
932d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org// C++ ABI Level 1 ABI documented at:
1032d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//   http://mentorembedded.github.io/cxx-abi/abi-eh.html
1132d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//
1232d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org//===----------------------------------------------------------------------===//
1332d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org
1432d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#ifndef __UNWIND_H__
1532d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#define __UNWIND_H__
1632d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org
1732d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#include <stdint.h>
1832d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#include <stddef.h>
1932d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org
2032d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#if defined(__APPLE__)
2132d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#define LIBUNWIND_UNAVAIL __attribute__ (( unavailable ))
2232d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#else
2332d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#define LIBUNWIND_UNAVAIL
2432d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#endif
2532d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org
2632d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org// FIXME: This is also in cxxabi.h and libunwind.h, can we consolidate?
2732d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#if !defined(__USING_SJLJ_EXCEPTIONS__) && defined(__arm__) && \
2832d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org    !defined(__ARM_DWARF_EH__) && !defined(__APPLE__)
2932d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#define LIBCXXABI_ARM_EHABI 1
3032d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#else
3132d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#define LIBCXXABI_ARM_EHABI 0
3232d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org#endif
3332d961d4454609ab4251a760fc46b19f661da90clrn@chromium.org
3432d961d4454609ab4251a760fc46b19f661da90clrn@chromium.orgtypedef enum {
357bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org  _URC_NO_REASON = 0,
36  _URC_OK = 0,
37  _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
38  _URC_FATAL_PHASE2_ERROR = 2,
39  _URC_FATAL_PHASE1_ERROR = 3,
40  _URC_NORMAL_STOP = 4,
41  _URC_END_OF_STACK = 5,
42  _URC_HANDLER_FOUND = 6,
43  _URC_INSTALL_CONTEXT = 7,
44  _URC_CONTINUE_UNWIND = 8,
45#if LIBCXXABI_ARM_EHABI
46  _URC_FAILURE = 9
47#endif
48} _Unwind_Reason_Code;
49
50typedef enum {
51  _UA_SEARCH_PHASE = 1,
52  _UA_CLEANUP_PHASE = 2,
53  _UA_HANDLER_FRAME = 4,
54  _UA_FORCE_UNWIND = 8,
55  _UA_END_OF_STACK = 16 // gcc extension to C++ ABI
56} _Unwind_Action;
57
58typedef struct _Unwind_Context _Unwind_Context;   // opaque
59
60#if LIBCXXABI_ARM_EHABI
61typedef uint32_t _Unwind_State;
62
63static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME   = 0;
64static const _Unwind_State _US_UNWIND_FRAME_STARTING  = 1;
65static const _Unwind_State _US_UNWIND_FRAME_RESUME    = 2;
66
67typedef uint32_t _Unwind_EHT_Header;
68
69struct _Unwind_Control_Block;
70typedef struct _Unwind_Control_Block _Unwind_Control_Block;
71typedef struct _Unwind_Control_Block _Unwind_Exception; /* Alias */
72
73struct _Unwind_Control_Block {
74  uint64_t exception_class;
75  void (*exception_cleanup)(_Unwind_Reason_Code, _Unwind_Control_Block*);
76
77  /* Unwinder cache, private fields for the unwinder's use */
78  struct {
79    uint32_t reserved1; /* init reserved1 to 0, then don't touch */
80    uint32_t reserved2;
81    uint32_t reserved3;
82    uint32_t reserved4;
83    uint32_t reserved5;
84  } unwinder_cache;
85
86  /* Propagation barrier cache (valid after phase 1): */
87  struct {
88    uint32_t sp;
89    uint32_t bitpattern[5];
90  } barrier_cache;
91
92  /* Cleanup cache (preserved over cleanup): */
93  struct {
94    uint32_t bitpattern[4];
95  } cleanup_cache;
96
97  /* Pr cache (for pr's benefit): */
98  struct {
99    uint32_t fnstart; /* function start address */
100    _Unwind_EHT_Header* ehtp; /* pointer to EHT entry header word */
101    uint32_t additional;
102    uint32_t reserved1;
103  } pr_cache;
104
105  long long int :0; /* Enforce the 8-byte alignment */
106};
107
108typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
109      (_Unwind_State state,
110       _Unwind_Exception* exceptionObject,
111       struct _Unwind_Context* context);
112
113typedef _Unwind_Reason_Code (*__personality_routine)
114      (_Unwind_State state,
115       _Unwind_Exception* exceptionObject,
116       struct _Unwind_Context* context);
117#else
118struct _Unwind_Context;   // opaque
119struct _Unwind_Exception; // forward declaration
120typedef struct _Unwind_Exception _Unwind_Exception;
121
122struct _Unwind_Exception {
123  uint64_t exception_class;
124  void (*exception_cleanup)(_Unwind_Reason_Code reason,
125                            _Unwind_Exception *exc);
126  uintptr_t private_1; // non-zero means forced unwind
127  uintptr_t private_2; // holds sp that phase1 found for phase2 to use
128#if !__LP64__
129  // The gcc implementation of _Unwind_Exception used attribute mode on the
130  // above fields which had the side effect of causing this whole struct to
131  // round up to 32 bytes in size. To be more explicit, we add pad fields
132  // added for binary compatibility.
133  uint32_t reserved[3];
134#endif
135};
136
137typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
138    (int version,
139     _Unwind_Action actions,
140     uint64_t exceptionClass,
141     _Unwind_Exception* exceptionObject,
142     struct _Unwind_Context* context,
143     void* stop_parameter );
144
145typedef _Unwind_Reason_Code (*__personality_routine)
146      (int version,
147       _Unwind_Action actions,
148       uint64_t exceptionClass,
149       _Unwind_Exception* exceptionObject,
150       struct _Unwind_Context* context);
151#endif
152
153#ifdef __cplusplus
154extern "C" {
155#endif
156
157//
158// The following are the base functions documented by the C++ ABI
159//
160#if __USING_SJLJ_EXCEPTIONS__
161extern _Unwind_Reason_Code
162    _Unwind_SjLj_RaiseException(_Unwind_Exception *exception_object);
163extern void _Unwind_SjLj_Resume(_Unwind_Exception *exception_object);
164#else
165extern _Unwind_Reason_Code
166    _Unwind_RaiseException(_Unwind_Exception *exception_object);
167extern void _Unwind_Resume(_Unwind_Exception *exception_object);
168#endif
169extern void _Unwind_DeleteException(_Unwind_Exception *exception_object);
170
171#if LIBCXXABI_ARM_EHABI
172typedef enum {
173  _UVRSC_CORE = 0, /* integer register */
174  _UVRSC_VFP = 1, /* vfp */
175  _UVRSC_WMMXD = 3, /* Intel WMMX data register */
176  _UVRSC_WMMXC = 4 /* Intel WMMX control register */
177} _Unwind_VRS_RegClass;
178
179typedef enum {
180  _UVRSD_UINT32 = 0,
181  _UVRSD_VFPX = 1,
182  _UVRSD_UINT64 = 3,
183  _UVRSD_FLOAT = 4,
184  _UVRSD_DOUBLE = 5
185} _Unwind_VRS_DataRepresentation;
186
187typedef enum {
188  _UVRSR_OK = 0,
189  _UVRSR_NOT_IMPLEMENTED = 1,
190  _UVRSR_FAILED = 2
191} _Unwind_VRS_Result;
192
193extern void _Unwind_Complete(_Unwind_Exception* exception_object);
194
195extern _Unwind_VRS_Result
196_Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
197                uint32_t regno, _Unwind_VRS_DataRepresentation representation,
198                void *valuep);
199
200extern _Unwind_VRS_Result
201_Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
202                uint32_t regno, _Unwind_VRS_DataRepresentation representation,
203                void *valuep);
204
205extern _Unwind_VRS_Result
206_Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
207                uint32_t discriminator,
208                _Unwind_VRS_DataRepresentation representation);
209
210extern _Unwind_Reason_Code _Unwind_VRS_Interpret(_Unwind_Context *context,
211                                                 uint32_t *data, size_t offset,
212                                                 size_t len);
213
214static inline uintptr_t _Unwind_GetGR(struct _Unwind_Context* context,
215                                      int index) {
216  uintptr_t value = 0;
217  _Unwind_VRS_Get(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
218  return value;
219}
220
221static inline void _Unwind_SetGR(struct _Unwind_Context* context, int index,
222                                 uintptr_t new_value) {
223  _Unwind_VRS_Set(context, _UVRSC_CORE, (uint32_t)index,
224                  _UVRSD_UINT32, &new_value);
225}
226
227static inline uintptr_t _Unwind_GetIP(struct _Unwind_Context* context) {
228  // remove the thumb-bit before returning
229  return (_Unwind_GetGR(context, 15) & (~(uintptr_t)0x1));
230}
231
232static inline void _Unwind_SetIP(struct _Unwind_Context* context,
233                                 uintptr_t new_value) {
234  uintptr_t thumb_bit = _Unwind_GetGR(context, 15) & ((uintptr_t)0x1);
235  _Unwind_SetGR(context, 15, new_value | thumb_bit);
236}
237#else
238extern uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index);
239extern void _Unwind_SetGR(struct _Unwind_Context *context, int index,
240                          uintptr_t new_value);
241extern uintptr_t _Unwind_GetIP(struct _Unwind_Context *context);
242extern void _Unwind_SetIP(struct _Unwind_Context *, uintptr_t new_value);
243#endif
244
245extern uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context *context);
246extern uintptr_t
247    _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context);
248#if __USING_SJLJ_EXCEPTIONS__
249extern _Unwind_Reason_Code
250    _Unwind_SjLj_ForcedUnwind(_Unwind_Exception *exception_object,
251                              _Unwind_Stop_Fn stop, void *stop_parameter);
252#else
253extern _Unwind_Reason_Code
254    _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
255                         _Unwind_Stop_Fn stop, void *stop_parameter);
256#endif
257
258#if __USING_SJLJ_EXCEPTIONS__
259typedef struct _Unwind_FunctionContext *_Unwind_FunctionContext_t;
260extern void _Unwind_SjLj_Register(_Unwind_FunctionContext_t fc);
261extern void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t fc);
262#endif
263
264//
265// The following are semi-suppoted extensions to the C++ ABI
266//
267
268//
269//  called by __cxa_rethrow().
270//
271#if __USING_SJLJ_EXCEPTIONS__
272extern _Unwind_Reason_Code
273    _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception *exception_object);
274#else
275extern _Unwind_Reason_Code
276    _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object);
277#endif
278
279// _Unwind_Backtrace() is a gcc extension that walks the stack and calls the
280// _Unwind_Trace_Fn once per frame until it reaches the bottom of the stack
281// or the _Unwind_Trace_Fn function returns something other than _URC_NO_REASON.
282typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
283                                                void *);
284extern _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
285
286// _Unwind_GetCFA is a gcc extension that can be called from within a
287// personality handler to get the CFA (stack pointer before call) of
288// current frame.
289extern uintptr_t _Unwind_GetCFA(struct _Unwind_Context *);
290
291
292// _Unwind_GetIPInfo is a gcc extension that can be called from within a
293// personality handler.  Similar to _Unwind_GetIP() but also returns in
294// *ipBefore a non-zero value if the instruction pointer is at or before the
295// instruction causing the unwind. Normally, in a function call, the IP returned
296// is the return address which is after the call instruction and may be past the
297// end of the function containing the call instruction.
298extern uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
299                                   int *ipBefore);
300
301
302// __register_frame() is used with dynamically generated code to register the
303// FDE for a generated (JIT) code.  The FDE must use pc-rel addressing to point
304// to its function and optional LSDA.
305// __register_frame() has existed in all versions of Mac OS X, but in 10.4 and
306// 10.5 it was buggy and did not actually register the FDE with the unwinder.
307// In 10.6 and later it does register properly.
308extern void __register_frame(const void *fde);
309extern void __deregister_frame(const void *fde);
310
311// _Unwind_Find_FDE() will locate the FDE if the pc is in some function that has
312// an associated FDE. Note, Mac OS X 10.6 and later, introduces "compact unwind
313// info" which the runtime uses in preference to dwarf unwind info.  This
314// function will only work if the target function has an FDE but no compact
315// unwind info.
316struct dwarf_eh_bases {
317  uintptr_t tbase;
318  uintptr_t dbase;
319  uintptr_t func;
320};
321extern const void *_Unwind_Find_FDE(const void *pc, struct dwarf_eh_bases *);
322
323
324// This function attempts to find the start (address of first instruction) of
325// a function given an address inside the function.  It only works if the
326// function has an FDE (dwarf unwind info).
327// This function is unimplemented on Mac OS X 10.6 and later.  Instead, use
328// _Unwind_Find_FDE() and look at the dwarf_eh_bases.func result.
329extern void *_Unwind_FindEnclosingFunction(void *pc);
330
331// Mac OS X does not support text-rel and data-rel addressing so these functions
332// are unimplemented
333extern uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context *context)
334    LIBUNWIND_UNAVAIL;
335extern uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context *context)
336    LIBUNWIND_UNAVAIL;
337
338// Mac OS X 10.4 and 10.5 had implementations of these functions in
339// libgcc_s.dylib, but they never worked.
340/// These functions are no longer available on Mac OS X.
341extern void __register_frame_info_bases(const void *fde, void *ob, void *tb,
342                                        void *db) LIBUNWIND_UNAVAIL;
343extern void __register_frame_info(const void *fde, void *ob)
344    LIBUNWIND_UNAVAIL;
345extern void __register_frame_info_table_bases(const void *fde, void *ob,
346                                              void *tb, void *db)
347    LIBUNWIND_UNAVAIL;
348extern void __register_frame_info_table(const void *fde, void *ob)
349    LIBUNWIND_UNAVAIL;
350extern void __register_frame_table(const void *fde)
351    LIBUNWIND_UNAVAIL;
352extern void *__deregister_frame_info(const void *fde)
353    LIBUNWIND_UNAVAIL;
354extern void *__deregister_frame_info_bases(const void *fde)
355    LIBUNWIND_UNAVAIL;
356
357#ifdef __cplusplus
358}
359#endif
360
361#endif // __UNWIND_H__
362