unwind.h 14 KB

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