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