]> git.lizzy.rs Git - rust.git/blob - src/libunwind/libunwind.rs
Auto merge of #67540 - Mark-Simulacrum:fmt-the-world, r=Centril
[rust.git] / src / libunwind / 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 #[cfg(target_arch = "x86")]
27 pub const unwinder_private_data_size: usize = 5;
28
29 #[cfg(target_arch = "x86_64")]
30 pub const unwinder_private_data_size: usize = 6;
31
32 #[cfg(all(target_arch = "arm", not(target_os = "ios")))]
33 pub const unwinder_private_data_size: usize = 20;
34
35 #[cfg(all(target_arch = "arm", target_os = "ios"))]
36 pub const unwinder_private_data_size: usize = 5;
37
38 #[cfg(target_arch = "aarch64")]
39 pub const unwinder_private_data_size: usize = 2;
40
41 #[cfg(target_arch = "mips")]
42 pub const unwinder_private_data_size: usize = 2;
43
44 #[cfg(target_arch = "mips64")]
45 pub const unwinder_private_data_size: usize = 2;
46
47 #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
48 pub const unwinder_private_data_size: usize = 2;
49
50 #[cfg(target_arch = "s390x")]
51 pub const unwinder_private_data_size: usize = 2;
52
53 #[cfg(target_arch = "sparc64")]
54 pub const unwinder_private_data_size: usize = 2;
55
56 #[cfg(target_os = "emscripten")]
57 pub const unwinder_private_data_size: usize = 20;
58
59 #[cfg(all(target_arch = "hexagon", target_os = "linux"))]
60 pub const unwinder_private_data_size: usize = 35;
61
62 #[repr(C)]
63 pub struct _Unwind_Exception {
64     pub exception_class: _Unwind_Exception_Class,
65     pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
66     pub private: [_Unwind_Word; unwinder_private_data_size],
67 }
68
69 pub enum _Unwind_Context {}
70
71 pub type _Unwind_Exception_Cleanup_Fn =
72     extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
73 #[cfg_attr(
74     all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
75     link(name = "unwind", kind = "static")
76 )]
77 extern "C" {
78     #[unwind(allowed)]
79     pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
80     pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
81     pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> *mut c_void;
82     pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
83     pub fn _Unwind_GetTextRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
84     pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
85 }
86
87 cfg_if::cfg_if! {
88 if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm"))))] {
89     // Not ARM EHABI
90     #[repr(C)]
91     #[derive(Copy, Clone, PartialEq)]
92     pub enum _Unwind_Action {
93         _UA_SEARCH_PHASE = 1,
94         _UA_CLEANUP_PHASE = 2,
95         _UA_HANDLER_FRAME = 4,
96         _UA_FORCE_UNWIND = 8,
97         _UA_END_OF_STACK = 16,
98     }
99     pub use _Unwind_Action::*;
100
101     #[cfg_attr(all(feature = "llvm-libunwind",
102                    any(target_os = "fuchsia", target_os = "linux")),
103                link(name = "unwind", kind = "static"))]
104     extern "C" {
105         pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
106         pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
107         pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
108         pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
109         pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
110                                  -> _Unwind_Word;
111         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
112     }
113
114 } else {
115     // ARM EHABI
116     #[repr(C)]
117     #[derive(Copy, Clone, PartialEq)]
118     pub enum _Unwind_State {
119         _US_VIRTUAL_UNWIND_FRAME = 0,
120         _US_UNWIND_FRAME_STARTING = 1,
121         _US_UNWIND_FRAME_RESUME = 2,
122         _US_ACTION_MASK = 3,
123         _US_FORCE_UNWIND = 8,
124         _US_END_OF_STACK = 16,
125     }
126     pub use _Unwind_State::*;
127
128     #[repr(C)]
129     enum _Unwind_VRS_Result {
130         _UVRSR_OK = 0,
131         _UVRSR_NOT_IMPLEMENTED = 1,
132         _UVRSR_FAILED = 2,
133     }
134     #[repr(C)]
135     enum _Unwind_VRS_RegClass {
136         _UVRSC_CORE = 0,
137         _UVRSC_VFP = 1,
138         _UVRSC_FPA = 2,
139         _UVRSC_WMMXD = 3,
140         _UVRSC_WMMXC = 4,
141     }
142     use _Unwind_VRS_RegClass::*;
143     #[repr(C)]
144     enum _Unwind_VRS_DataRepresentation {
145         _UVRSD_UINT32 = 0,
146         _UVRSD_VFPX = 1,
147         _UVRSD_FPAX = 2,
148         _UVRSD_UINT64 = 3,
149         _UVRSD_FLOAT = 4,
150         _UVRSD_DOUBLE = 5,
151     }
152     use _Unwind_VRS_DataRepresentation::*;
153
154     pub const UNWIND_POINTER_REG: c_int = 12;
155     pub const UNWIND_IP_REG: c_int = 15;
156
157     #[cfg_attr(all(feature = "llvm-libunwind",
158                    any(target_os = "fuchsia", target_os = "linux")),
159                link(name = "unwind", kind = "static"))]
160     extern "C" {
161         fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
162                            regclass: _Unwind_VRS_RegClass,
163                            regno: _Unwind_Word,
164                            repr: _Unwind_VRS_DataRepresentation,
165                            data: *mut c_void)
166                            -> _Unwind_VRS_Result;
167
168         fn _Unwind_VRS_Set(ctx: *mut _Unwind_Context,
169                            regclass: _Unwind_VRS_RegClass,
170                            regno: _Unwind_Word,
171                            repr: _Unwind_VRS_DataRepresentation,
172                            data: *mut c_void)
173                            -> _Unwind_VRS_Result;
174     }
175
176     // On Android or ARM/Linux, these are implemented as macros:
177
178     pub unsafe fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word {
179         let mut val: _Unwind_Word = 0;
180         _Unwind_VRS_Get(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
181                         &mut val as *mut _ as *mut c_void);
182         val
183     }
184
185     pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) {
186         let mut value = value;
187         _Unwind_VRS_Set(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
188                         &mut value as *mut _ as *mut c_void);
189     }
190
191     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
192                                 -> _Unwind_Word {
193         let val = _Unwind_GetGR(ctx, UNWIND_IP_REG);
194         (val & !1) as _Unwind_Word
195     }
196
197     pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
198                                 value: _Unwind_Word) {
199         // Propagate thumb bit to instruction pointer
200         let thumb_state = _Unwind_GetGR(ctx, UNWIND_IP_REG) & 1;
201         let value = value | thumb_state;
202         _Unwind_SetGR(ctx, UNWIND_IP_REG, value);
203     }
204
205     pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
206                                     ip_before_insn: *mut c_int)
207                                     -> _Unwind_Word {
208         *ip_before_insn = 0;
209         _Unwind_GetIP(ctx)
210     }
211
212     // This function also doesn't exist on Android or ARM/Linux, so make it a no-op
213     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
214         pc
215     }
216 }
217 } // cfg_if!
218
219 cfg_if::cfg_if! {
220 if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
221     // Not 32-bit iOS
222     #[cfg_attr(all(feature = "llvm-libunwind",
223                    any(target_os = "fuchsia", target_os = "linux")),
224                link(name = "unwind", kind = "static"))]
225     extern "C" {
226         #[unwind(allowed)]
227         pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
228         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
229                                  trace_argument: *mut c_void)
230                                  -> _Unwind_Reason_Code;
231     }
232 } else {
233     // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
234     #[cfg_attr(all(feature = "llvm-libunwind",
235                    any(target_os = "fuchsia", target_os = "linux")),
236                link(name = "unwind", kind = "static"))]
237     extern "C" {
238         #[unwind(allowed)]
239         pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
240     }
241
242     #[inline]
243     pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
244         _Unwind_SjLj_RaiseException(exc)
245     }
246 }
247 } // cfg_if!
248
249 cfg_if::cfg_if! {
250 if #[cfg(all(windows, target_arch = "x86_64", target_env = "gnu"))] {
251     // We declare these as opaque types. This is fine since you just need to
252     // pass them to _GCC_specific_handler and forget about them.
253     pub enum EXCEPTION_RECORD {}
254     pub type LPVOID = *mut c_void;
255     pub enum CONTEXT {}
256     pub enum DISPATCHER_CONTEXT {}
257     pub type EXCEPTION_DISPOSITION = c_int;
258     type PersonalityFn = unsafe extern "C" fn(version: c_int,
259                                               actions: _Unwind_Action,
260                                               exception_class: _Unwind_Exception_Class,
261                                               exception_object: *mut _Unwind_Exception,
262                                               context: *mut _Unwind_Context)
263                                               -> _Unwind_Reason_Code;
264
265     extern "C" {
266         pub fn _GCC_specific_handler(exceptionRecord: *mut EXCEPTION_RECORD,
267                                 establisherFrame: LPVOID,
268                                 contextRecord: *mut CONTEXT,
269                                 dispatcherContext: *mut DISPATCHER_CONTEXT,
270                                 personality: PersonalityFn)
271                                 -> EXCEPTION_DISPOSITION;
272     }
273 }
274 } // cfg_if!