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