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