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