xref: /freebsd/contrib/llvm-project/libunwind/include/unwind.h (revision 1ac55f4cb0001fed92329746c730aa9a947c09a5)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //
80b57cec5SDimitry Andric // C++ ABI Level 1 ABI documented at:
90b57cec5SDimitry Andric //   https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef __UNWIND_H__
140b57cec5SDimitry Andric #define __UNWIND_H__
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include <__libunwind_config.h>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include <stdint.h>
190b57cec5SDimitry Andric #include <stddef.h>
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) && defined(_WIN32)
220b57cec5SDimitry Andric #include <windows.h>
230b57cec5SDimitry Andric #include <ntverp.h>
240b57cec5SDimitry Andric #endif
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #if defined(__APPLE__)
270b57cec5SDimitry Andric #define LIBUNWIND_UNAVAIL __attribute__ (( unavailable ))
280b57cec5SDimitry Andric #else
290b57cec5SDimitry Andric #define LIBUNWIND_UNAVAIL
300b57cec5SDimitry Andric #endif
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric typedef enum {
330b57cec5SDimitry Andric   _URC_NO_REASON = 0,
340b57cec5SDimitry Andric   _URC_OK = 0,
350b57cec5SDimitry Andric   _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
360b57cec5SDimitry Andric   _URC_FATAL_PHASE2_ERROR = 2,
370b57cec5SDimitry Andric   _URC_FATAL_PHASE1_ERROR = 3,
380b57cec5SDimitry Andric   _URC_NORMAL_STOP = 4,
390b57cec5SDimitry Andric   _URC_END_OF_STACK = 5,
400b57cec5SDimitry Andric   _URC_HANDLER_FOUND = 6,
410b57cec5SDimitry Andric   _URC_INSTALL_CONTEXT = 7,
420b57cec5SDimitry Andric   _URC_CONTINUE_UNWIND = 8,
430b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
440b57cec5SDimitry Andric   _URC_FAILURE = 9
450b57cec5SDimitry Andric #endif
460b57cec5SDimitry Andric } _Unwind_Reason_Code;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric typedef enum {
490b57cec5SDimitry Andric   _UA_SEARCH_PHASE = 1,
500b57cec5SDimitry Andric   _UA_CLEANUP_PHASE = 2,
510b57cec5SDimitry Andric   _UA_HANDLER_FRAME = 4,
520b57cec5SDimitry Andric   _UA_FORCE_UNWIND = 8,
530b57cec5SDimitry Andric   _UA_END_OF_STACK = 16 // gcc extension to C++ ABI
540b57cec5SDimitry Andric } _Unwind_Action;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric typedef struct _Unwind_Context _Unwind_Context;   // opaque
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
59*1ac55f4cSDimitry Andric #include <unwind_arm_ehabi.h>
600b57cec5SDimitry Andric #else
61*1ac55f4cSDimitry Andric #include <unwind_itanium.h>
620b57cec5SDimitry Andric #endif
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
650b57cec5SDimitry Andric     (int version,
660b57cec5SDimitry Andric      _Unwind_Action actions,
67349cc55cSDimitry Andric      _Unwind_Exception_Class exceptionClass,
680b57cec5SDimitry Andric      _Unwind_Exception* exceptionObject,
690b57cec5SDimitry Andric      struct _Unwind_Context* context,
700b57cec5SDimitry Andric      void* stop_parameter);
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric #ifdef __cplusplus
730b57cec5SDimitry Andric extern "C" {
740b57cec5SDimitry Andric #endif
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric extern uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context *context);
770b57cec5SDimitry Andric extern uintptr_t
780b57cec5SDimitry Andric     _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context);
790b57cec5SDimitry Andric #ifdef __USING_SJLJ_EXCEPTIONS__
800b57cec5SDimitry Andric extern _Unwind_Reason_Code
810b57cec5SDimitry Andric     _Unwind_SjLj_ForcedUnwind(_Unwind_Exception *exception_object,
820b57cec5SDimitry Andric                               _Unwind_Stop_Fn stop, void *stop_parameter);
830b57cec5SDimitry Andric #else
840b57cec5SDimitry Andric extern _Unwind_Reason_Code
850b57cec5SDimitry Andric     _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
860b57cec5SDimitry Andric                          _Unwind_Stop_Fn stop, void *stop_parameter);
870b57cec5SDimitry Andric #endif
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric #ifdef __USING_SJLJ_EXCEPTIONS__
900b57cec5SDimitry Andric typedef struct _Unwind_FunctionContext *_Unwind_FunctionContext_t;
910b57cec5SDimitry Andric extern void _Unwind_SjLj_Register(_Unwind_FunctionContext_t fc);
920b57cec5SDimitry Andric extern void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t fc);
930b57cec5SDimitry Andric #endif
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric //
96bdd1243dSDimitry Andric // The following are semi-supported extensions to the C++ ABI
970b57cec5SDimitry Andric //
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric //
1000b57cec5SDimitry Andric //  called by __cxa_rethrow().
1010b57cec5SDimitry Andric //
1020b57cec5SDimitry Andric #ifdef __USING_SJLJ_EXCEPTIONS__
1030b57cec5SDimitry Andric extern _Unwind_Reason_Code
1040b57cec5SDimitry Andric     _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception *exception_object);
1050b57cec5SDimitry Andric #else
1060b57cec5SDimitry Andric extern _Unwind_Reason_Code
1070b57cec5SDimitry Andric     _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object);
1080b57cec5SDimitry Andric #endif
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric // _Unwind_Backtrace() is a gcc extension that walks the stack and calls the
1110b57cec5SDimitry Andric // _Unwind_Trace_Fn once per frame until it reaches the bottom of the stack
1120b57cec5SDimitry Andric // or the _Unwind_Trace_Fn function returns something other than _URC_NO_REASON.
1130b57cec5SDimitry Andric typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
1140b57cec5SDimitry Andric                                                 void *);
1150b57cec5SDimitry Andric extern _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric // _Unwind_GetCFA is a gcc extension that can be called from within a
1180b57cec5SDimitry Andric // personality handler to get the CFA (stack pointer before call) of
1190b57cec5SDimitry Andric // current frame.
1200b57cec5SDimitry Andric extern uintptr_t _Unwind_GetCFA(struct _Unwind_Context *);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric // _Unwind_GetIPInfo is a gcc extension that can be called from within a
1240b57cec5SDimitry Andric // personality handler.  Similar to _Unwind_GetIP() but also returns in
1250b57cec5SDimitry Andric // *ipBefore a non-zero value if the instruction pointer is at or before the
1260b57cec5SDimitry Andric // instruction causing the unwind. Normally, in a function call, the IP returned
1270b57cec5SDimitry Andric // is the return address which is after the call instruction and may be past the
1280b57cec5SDimitry Andric // end of the function containing the call instruction.
1290b57cec5SDimitry Andric extern uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
1300b57cec5SDimitry Andric                                    int *ipBefore);
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric // __register_frame() is used with dynamically generated code to register the
1340b57cec5SDimitry Andric // FDE for a generated (JIT) code.  The FDE must use pc-rel addressing to point
1350b57cec5SDimitry Andric // to its function and optional LSDA.
1360b57cec5SDimitry Andric // __register_frame() has existed in all versions of Mac OS X, but in 10.4 and
1370b57cec5SDimitry Andric // 10.5 it was buggy and did not actually register the FDE with the unwinder.
1380b57cec5SDimitry Andric // In 10.6 and later it does register properly.
1390b57cec5SDimitry Andric extern void __register_frame(const void *fde);
1400b57cec5SDimitry Andric extern void __deregister_frame(const void *fde);
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric // _Unwind_Find_FDE() will locate the FDE if the pc is in some function that has
1430b57cec5SDimitry Andric // an associated FDE. Note, Mac OS X 10.6 and later, introduces "compact unwind
1440b57cec5SDimitry Andric // info" which the runtime uses in preference to DWARF unwind info.  This
1450b57cec5SDimitry Andric // function will only work if the target function has an FDE but no compact
1460b57cec5SDimitry Andric // unwind info.
1470b57cec5SDimitry Andric struct dwarf_eh_bases {
1480b57cec5SDimitry Andric   uintptr_t tbase;
1490b57cec5SDimitry Andric   uintptr_t dbase;
1500b57cec5SDimitry Andric   uintptr_t func;
1510b57cec5SDimitry Andric };
1520b57cec5SDimitry Andric extern const void *_Unwind_Find_FDE(const void *pc, struct dwarf_eh_bases *);
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric // This function attempts to find the start (address of first instruction) of
1560b57cec5SDimitry Andric // a function given an address inside the function.  It only works if the
1570b57cec5SDimitry Andric // function has an FDE (DWARF unwind info).
1580b57cec5SDimitry Andric // This function is unimplemented on Mac OS X 10.6 and later.  Instead, use
1590b57cec5SDimitry Andric // _Unwind_Find_FDE() and look at the dwarf_eh_bases.func result.
1600b57cec5SDimitry Andric extern void *_Unwind_FindEnclosingFunction(void *pc);
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric // Mac OS X does not support text-rel and data-rel addressing so these functions
16381ad6265SDimitry Andric // are unimplemented.
1640b57cec5SDimitry Andric extern uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context *context)
1650b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1660b57cec5SDimitry Andric extern uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context *context)
1670b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric // Mac OS X 10.4 and 10.5 had implementations of these functions in
1700b57cec5SDimitry Andric // libgcc_s.dylib, but they never worked.
1710b57cec5SDimitry Andric /// These functions are no longer available on Mac OS X.
1720b57cec5SDimitry Andric extern void __register_frame_info_bases(const void *fde, void *ob, void *tb,
1730b57cec5SDimitry Andric                                         void *db) LIBUNWIND_UNAVAIL;
1740b57cec5SDimitry Andric extern void __register_frame_info(const void *fde, void *ob)
1750b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1760b57cec5SDimitry Andric extern void __register_frame_info_table_bases(const void *fde, void *ob,
1770b57cec5SDimitry Andric                                               void *tb, void *db)
1780b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1790b57cec5SDimitry Andric extern void __register_frame_info_table(const void *fde, void *ob)
1800b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1810b57cec5SDimitry Andric extern void __register_frame_table(const void *fde)
1820b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1830b57cec5SDimitry Andric extern void *__deregister_frame_info(const void *fde)
1840b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1850b57cec5SDimitry Andric extern void *__deregister_frame_info_bases(const void *fde)
1860b57cec5SDimitry Andric     LIBUNWIND_UNAVAIL;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
1890b57cec5SDimitry Andric #ifndef _WIN32
1900b57cec5SDimitry Andric typedef struct _EXCEPTION_RECORD EXCEPTION_RECORD;
1910b57cec5SDimitry Andric typedef struct _CONTEXT CONTEXT;
1920b57cec5SDimitry Andric typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
1930b57cec5SDimitry Andric #elif !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
1940b57cec5SDimitry Andric typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
1950b57cec5SDimitry Andric #endif
1960b57cec5SDimitry Andric // This is the common wrapper for GCC-style personality functions with SEH.
1970b57cec5SDimitry Andric extern EXCEPTION_DISPOSITION _GCC_specific_handler(EXCEPTION_RECORD *exc,
1985ffd83dbSDimitry Andric                                                    void *frame, CONTEXT *ctx,
1990b57cec5SDimitry Andric                                                    DISPATCHER_CONTEXT *disp,
2005ffd83dbSDimitry Andric                                                    _Unwind_Personality_Fn pers);
2010b57cec5SDimitry Andric #endif
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric #ifdef __cplusplus
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric #endif
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric #endif // __UNWIND_H__
208