1//===----------------------------------------------------------------------===//
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
32typedef 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
48typedef 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
56typedef struct _Unwind_Context _Unwind_Context; // opaque
57
58#if defined(_LIBUNWIND_ARM_EHABI)
59#include <unwind_arm_ehabi.h>
60#else
61#include <unwind_itanium.h>
62#endif
63
64#if defined(__WASM_EXCEPTIONS__)
65#include <unwind_wasm.h>
66#endif
67
68typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
69 (int version,
70 _Unwind_Action actions,
71 _Unwind_Exception_Class exceptionClass,
72 _Unwind_Exception* exceptionObject,
73 struct _Unwind_Context* context,
74 void* stop_parameter);
75
76#ifdef __cplusplus
77extern "C" {
78#endif
79
80extern uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context *context);
81extern uintptr_t
82 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context);
83#ifdef __USING_SJLJ_EXCEPTIONS__
84extern _Unwind_Reason_Code
85 _Unwind_SjLj_ForcedUnwind(_Unwind_Exception *exception_object,
86 _Unwind_Stop_Fn stop, void *stop_parameter);
87#else
88extern _Unwind_Reason_Code
89 _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
90 _Unwind_Stop_Fn stop, void *stop_parameter);
91#endif
92
93#ifdef __USING_SJLJ_EXCEPTIONS__
94typedef struct _Unwind_FunctionContext *_Unwind_FunctionContext_t;
95extern void _Unwind_SjLj_Register(_Unwind_FunctionContext_t fc);
96extern void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t fc);
97#endif
98
99//
100// The following are semi-supported extensions to the C++ ABI
101//
102
103//
104// called by __cxa_rethrow().
105//
106#ifdef __USING_SJLJ_EXCEPTIONS__
107extern _Unwind_Reason_Code
108 _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception *exception_object);
109#else
110extern _Unwind_Reason_Code
111 _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object);
112#endif
113
114// _Unwind_Backtrace() is a gcc extension that walks the stack and calls the
115// _Unwind_Trace_Fn once per frame until it reaches the bottom of the stack
116// or the _Unwind_Trace_Fn function returns something other than _URC_NO_REASON.
117typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
118 void *);
119extern _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
120
121// _Unwind_GetCFA is a gcc extension that can be called from within a
122// personality handler to get the CFA (stack pointer before call) of
123// current frame.
124extern uintptr_t _Unwind_GetCFA(struct _Unwind_Context *);
125
126
127// _Unwind_GetIPInfo is a gcc extension that can be called from within a
128// personality handler. Similar to _Unwind_GetIP() but also returns in
129// *ipBefore a non-zero value if the instruction pointer is at or before the
130// instruction causing the unwind. Normally, in a function call, the IP returned
131// is the return address which is after the call instruction and may be past the
132// end of the function containing the call instruction.
133extern uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
134 int *ipBefore);
135
136
137// __register_frame() is used with dynamically generated code to register the
138// FDE for a generated (JIT) code. The FDE must use pc-rel addressing to point
139// to its function and optional LSDA.
140// __register_frame() has existed in all versions of Mac OS X, but in 10.4 and
141// 10.5 it was buggy and did not actually register the FDE with the unwinder.
142// In 10.6 and later it does register properly.
143extern void __register_frame(const void *fde);
144extern void __deregister_frame(const void *fde);
145
146// _Unwind_Find_FDE() will locate the FDE if the pc is in some function that has
147// an associated FDE. Note, Mac OS X 10.6 and later, introduces "compact unwind
148// info" which the runtime uses in preference to DWARF unwind info. This
149// function will only work if the target function has an FDE but no compact
150// unwind info.
151struct dwarf_eh_bases {
152 uintptr_t tbase;
153 uintptr_t dbase;
154 uintptr_t func;
155};
156extern const void *_Unwind_Find_FDE(const void *pc, struct dwarf_eh_bases *);
157
158
159// This function attempts to find the start (address of first instruction) of
160// a function given an address inside the function. It only works if the
161// function has an FDE (DWARF unwind info).
162// This function is unimplemented on Mac OS X 10.6 and later. Instead, use
163// _Unwind_Find_FDE() and look at the dwarf_eh_bases.func result.
164extern void *_Unwind_FindEnclosingFunction(void *pc);
165
166// Mac OS X does not support text-rel and data-rel addressing so these functions
167// are unimplemented.
168extern uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context *context)
169 LIBUNWIND_UNAVAIL;
170extern uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context *context)
171 LIBUNWIND_UNAVAIL;
172
173// Mac OS X 10.4 and 10.5 had implementations of these functions in
174// libgcc_s.dylib, but they never worked.
175/// These functions are no longer available on Mac OS X.
176extern void __register_frame_info_bases(const void *fde, void *ob, void *tb,
177 void *db) LIBUNWIND_UNAVAIL;
178extern void __register_frame_info(const void *fde, void *ob)
179 LIBUNWIND_UNAVAIL;
180extern void __register_frame_info_table_bases(const void *fde, void *ob,
181 void *tb, void *db)
182 LIBUNWIND_UNAVAIL;
183extern void __register_frame_info_table(const void *fde, void *ob)
184 LIBUNWIND_UNAVAIL;
185extern void __register_frame_table(const void *fde)
186 LIBUNWIND_UNAVAIL;
187extern void *__deregister_frame_info(const void *fde)
188 LIBUNWIND_UNAVAIL;
189extern void *__deregister_frame_info_bases(const void *fde)
190 LIBUNWIND_UNAVAIL;
191
192#if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
193#ifndef _WIN32
194typedef struct _EXCEPTION_RECORD EXCEPTION_RECORD;
195typedef struct _CONTEXT CONTEXT;
196typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
197#elif !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
198typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
199#endif
200// This is the common wrapper for GCC-style personality functions with SEH.
201extern EXCEPTION_DISPOSITION _GCC_specific_handler(EXCEPTION_RECORD *exc,
202 void *frame, CONTEXT *ctx,
203 DISPATCHER_CONTEXT *disp,
204 _Unwind_Personality_Fn pers);
205#endif
206
207#ifdef __cplusplus
208}
209#endif
210
211#endif // __UNWIND_H__
212