]> git.lizzy.rs Git - rust.git/blob - library/unwind/src/libunwind.rs
Rollup merge of #100500 - cjgillot:verify-self-predicate, r=lcnr
[rust.git] / library / unwind / src / libunwind.rs
1 #![allow(nonstandard_style)]
2
3 use libc::{c_int, c_void, uintptr_t};
4
5 #[repr(C)]
6 #[derive(Debug, Copy, Clone, PartialEq)]
7 pub enum _Unwind_Reason_Code {
8     _URC_NO_REASON = 0,
9     _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
10     _URC_FATAL_PHASE2_ERROR = 2,
11     _URC_FATAL_PHASE1_ERROR = 3,
12     _URC_NORMAL_STOP = 4,
13     _URC_END_OF_STACK = 5,
14     _URC_HANDLER_FOUND = 6,
15     _URC_INSTALL_CONTEXT = 7,
16     _URC_CONTINUE_UNWIND = 8,
17     _URC_FAILURE = 9, // used only by ARM EHABI
18 }
19 pub use _Unwind_Reason_Code::*;
20
21 pub type _Unwind_Exception_Class = u64;
22 pub type _Unwind_Word = uintptr_t;
23 pub type _Unwind_Ptr = uintptr_t;
24 pub type _Unwind_Trace_Fn =
25     extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut c_void) -> _Unwind_Reason_Code;
26
27 #[cfg(target_arch = "x86")]
28 pub const unwinder_private_data_size: usize = 5;
29
30 #[cfg(target_arch = "x86_64")]
31 pub const unwinder_private_data_size: usize = 6;
32
33 #[cfg(all(target_arch = "arm", not(any(target_os = "ios", target_os = "watchos"))))]
34 pub const unwinder_private_data_size: usize = 20;
35
36 #[cfg(all(target_arch = "arm", any(target_os = "ios", target_os = "watchos")))]
37 pub const unwinder_private_data_size: usize = 5;
38
39 #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))]
40 pub const unwinder_private_data_size: usize = 2;
41
42 #[cfg(all(target_arch = "aarch64", target_pointer_width = "32"))]
43 pub const unwinder_private_data_size: usize = 5;
44
45 #[cfg(target_arch = "m68k")]
46 pub const unwinder_private_data_size: usize = 2;
47
48 #[cfg(target_arch = "mips")]
49 pub const unwinder_private_data_size: usize = 2;
50
51 #[cfg(target_arch = "mips64")]
52 pub const unwinder_private_data_size: usize = 2;
53
54 #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
55 pub const unwinder_private_data_size: usize = 2;
56
57 #[cfg(target_arch = "s390x")]
58 pub const unwinder_private_data_size: usize = 2;
59
60 #[cfg(any(target_arch = "sparc", target_arch = "sparc64"))]
61 pub const unwinder_private_data_size: usize = 2;
62
63 #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
64 pub const unwinder_private_data_size: usize = 2;
65
66 #[cfg(target_os = "emscripten")]
67 pub const unwinder_private_data_size: usize = 20;
68
69 #[cfg(all(target_arch = "hexagon", target_os = "linux"))]
70 pub const unwinder_private_data_size: usize = 35;
71
72 #[repr(C)]
73 pub struct _Unwind_Exception {
74     pub exception_class: _Unwind_Exception_Class,
75     pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
76     pub private: [_Unwind_Word; unwinder_private_data_size],
77 }
78
79 pub enum _Unwind_Context {}
80
81 pub type _Unwind_Exception_Cleanup_Fn =
82     extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
83
84 // FIXME: The `#[link]` attributes on `extern "C"` block marks those symbols declared in
85 // the block are reexported in dylib build of libstd. This is needed when build rustc with
86 // feature `llvm-libunwind', as no other cdylib will provided those _Unwind_* symbols.
87 // However the `link` attribute is duplicated multiple times and does not just export symbol,
88 // a better way to manually export symbol would be another attribute like `#[export]`.
89 // See the logic in function rustc_codegen_ssa::src::back::exported_symbols, module
90 // rustc_codegen_ssa::src::back::symbol_export, rustc_middle::middle::exported_symbols
91 // and RFC 2841
92 #[cfg_attr(
93     all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
94     link(name = "unwind", kind = "static", modifiers = "-bundle")
95 )]
96 extern "C-unwind" {
97     pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
98 }
99 extern "C" {
100     pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
101     pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> *mut c_void;
102     pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
103     pub fn _Unwind_GetTextRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
104     pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
105 }
106
107 cfg_if::cfg_if! {
108 if #[cfg(any(target_os = "ios", target_os = "watchos", target_os = "netbsd", not(target_arch = "arm")))] {
109     // Not ARM EHABI
110     #[repr(C)]
111     #[derive(Copy, Clone, PartialEq)]
112     pub enum _Unwind_Action {
113         _UA_SEARCH_PHASE = 1,
114         _UA_CLEANUP_PHASE = 2,
115         _UA_HANDLER_FRAME = 4,
116         _UA_FORCE_UNWIND = 8,
117         _UA_END_OF_STACK = 16,
118     }
119     pub use _Unwind_Action::*;
120
121     #[cfg_attr(
122         all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
123         link(name = "unwind", kind = "static", modifiers = "-bundle")
124     )]
125     extern "C" {
126         pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
127         pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
128         pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
129         pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
130         pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
131                                  -> _Unwind_Word;
132         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
133     }
134
135 } else {
136     // ARM EHABI
137     #[repr(C)]
138     #[derive(Copy, Clone, PartialEq)]
139     pub enum _Unwind_State {
140         _US_VIRTUAL_UNWIND_FRAME = 0,
141         _US_UNWIND_FRAME_STARTING = 1,
142         _US_UNWIND_FRAME_RESUME = 2,
143         _US_ACTION_MASK = 3,
144         _US_FORCE_UNWIND = 8,
145         _US_END_OF_STACK = 16,
146     }
147     pub use _Unwind_State::*;
148
149     #[repr(C)]
150     enum _Unwind_VRS_Result {
151         _UVRSR_OK = 0,
152         _UVRSR_NOT_IMPLEMENTED = 1,
153         _UVRSR_FAILED = 2,
154     }
155     #[repr(C)]
156     enum _Unwind_VRS_RegClass {
157         _UVRSC_CORE = 0,
158         _UVRSC_VFP = 1,
159         _UVRSC_FPA = 2,
160         _UVRSC_WMMXD = 3,
161         _UVRSC_WMMXC = 4,
162     }
163     use _Unwind_VRS_RegClass::*;
164     #[repr(C)]
165     enum _Unwind_VRS_DataRepresentation {
166         _UVRSD_UINT32 = 0,
167         _UVRSD_VFPX = 1,
168         _UVRSD_FPAX = 2,
169         _UVRSD_UINT64 = 3,
170         _UVRSD_FLOAT = 4,
171         _UVRSD_DOUBLE = 5,
172     }
173     use _Unwind_VRS_DataRepresentation::*;
174
175     pub const UNWIND_POINTER_REG: c_int = 12;
176     pub const UNWIND_SP_REG: c_int = 13;
177     pub const UNWIND_IP_REG: c_int = 15;
178
179     #[cfg_attr(
180         all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
181         link(name = "unwind", kind = "static", modifiers = "-bundle")
182     )]
183     extern "C" {
184         fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
185                            regclass: _Unwind_VRS_RegClass,
186                            regno: _Unwind_Word,
187                            repr: _Unwind_VRS_DataRepresentation,
188                            data: *mut c_void)
189                            -> _Unwind_VRS_Result;
190
191         fn _Unwind_VRS_Set(ctx: *mut _Unwind_Context,
192                            regclass: _Unwind_VRS_RegClass,
193                            regno: _Unwind_Word,
194                            repr: _Unwind_VRS_DataRepresentation,
195                            data: *mut c_void)
196                            -> _Unwind_VRS_Result;
197     }
198
199     // On Android or ARM/Linux, these are implemented as macros:
200
201     pub unsafe fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word {
202         let mut val: _Unwind_Word = 0;
203         _Unwind_VRS_Get(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
204                         &mut val as *mut _ as *mut c_void);
205         val
206     }
207
208     pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) {
209         let mut value = value;
210         _Unwind_VRS_Set(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
211                         &mut value as *mut _ as *mut c_void);
212     }
213
214     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
215                                 -> _Unwind_Word {
216         let val = _Unwind_GetGR(ctx, UNWIND_IP_REG);
217         (val & !1) as _Unwind_Word
218     }
219
220     pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
221                                 value: _Unwind_Word) {
222         // Propagate thumb bit to instruction pointer
223         let thumb_state = _Unwind_GetGR(ctx, UNWIND_IP_REG) & 1;
224         let value = value | thumb_state;
225         _Unwind_SetGR(ctx, UNWIND_IP_REG, value);
226     }
227
228     pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
229                                     ip_before_insn: *mut c_int)
230                                     -> _Unwind_Word {
231         *ip_before_insn = 0;
232         _Unwind_GetIP(ctx)
233     }
234
235     // This function also doesn't exist on Android or ARM/Linux, so make it a no-op
236     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
237         pc
238     }
239 }
240 } // cfg_if!
241
242 cfg_if::cfg_if! {
243 if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
244     // Not 32-bit iOS
245     #[cfg_attr(
246         all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
247         link(name = "unwind", kind = "static", modifiers = "-bundle")
248     )]
249     extern "C-unwind" {
250         pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
251     }
252     #[cfg_attr(
253         all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
254         link(name = "unwind", kind = "static", modifiers = "-bundle")
255     )]
256     extern "C" {
257         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
258                                  trace_argument: *mut c_void)
259                                  -> _Unwind_Reason_Code;
260     }
261 } else {
262     // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
263     extern "C-unwind" {
264         pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
265     }
266
267     pub use _Unwind_SjLj_RaiseException as _Unwind_RaiseException;
268 }
269 } // cfg_if!
270
271 cfg_if::cfg_if! {
272 if #[cfg(all(windows, target_arch = "x86_64", target_env = "gnu"))] {
273     // We declare these as opaque types. This is fine since you just need to
274     // pass them to _GCC_specific_handler and forget about them.
275     pub enum EXCEPTION_RECORD {}
276     pub type LPVOID = *mut c_void;
277     pub enum CONTEXT {}
278     pub enum DISPATCHER_CONTEXT {}
279     pub type EXCEPTION_DISPOSITION = c_int;
280     type PersonalityFn = unsafe extern "C" fn(version: c_int,
281                                               actions: _Unwind_Action,
282                                               exception_class: _Unwind_Exception_Class,
283                                               exception_object: *mut _Unwind_Exception,
284                                               context: *mut _Unwind_Context)
285                                               -> _Unwind_Reason_Code;
286
287     extern "C" {
288         pub fn _GCC_specific_handler(exceptionRecord: *mut EXCEPTION_RECORD,
289                                 establisherFrame: LPVOID,
290                                 contextRecord: *mut CONTEXT,
291                                 dispatcherContext: *mut DISPATCHER_CONTEXT,
292                                 personality: PersonalityFn)
293                                 -> EXCEPTION_DISPOSITION;
294     }
295 }
296 } // cfg_if!