]> git.lizzy.rs Git - rust.git/blob - src/libunwind/libunwind.rs
add a preliminary existential test; not really enough
[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 #[repr(C)]
60 pub struct _Unwind_Exception {
61     pub exception_class: _Unwind_Exception_Class,
62     pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
63     pub private: [_Unwind_Word; unwinder_private_data_size],
64 }
65
66 pub enum _Unwind_Context {}
67
68 pub type _Unwind_Exception_Cleanup_Fn = extern "C" fn(unwind_code: _Unwind_Reason_Code,
69                                                       exception: *mut _Unwind_Exception);
70 extern "C" {
71     #[unwind(allowed)]
72     pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
73     pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
74     pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> *mut c_void;
75     pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
76     pub fn _Unwind_GetTextRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
77     pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
78 }
79
80 cfg_if::cfg_if! {
81 if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm"))))] {
82     // Not ARM EHABI
83     #[repr(C)]
84     #[derive(Copy, Clone, PartialEq)]
85     pub enum _Unwind_Action {
86         _UA_SEARCH_PHASE = 1,
87         _UA_CLEANUP_PHASE = 2,
88         _UA_HANDLER_FRAME = 4,
89         _UA_FORCE_UNWIND = 8,
90         _UA_END_OF_STACK = 16,
91     }
92     pub use _Unwind_Action::*;
93
94     extern "C" {
95         pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
96         pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
97         pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
98         pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
99         pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
100                                  -> _Unwind_Word;
101         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
102     }
103
104 } else {
105     // ARM EHABI
106     #[repr(C)]
107     #[derive(Copy, Clone, PartialEq)]
108     pub enum _Unwind_State {
109         _US_VIRTUAL_UNWIND_FRAME = 0,
110         _US_UNWIND_FRAME_STARTING = 1,
111         _US_UNWIND_FRAME_RESUME = 2,
112         _US_ACTION_MASK = 3,
113         _US_FORCE_UNWIND = 8,
114         _US_END_OF_STACK = 16,
115     }
116     pub use _Unwind_State::*;
117
118     #[repr(C)]
119     enum _Unwind_VRS_Result {
120         _UVRSR_OK = 0,
121         _UVRSR_NOT_IMPLEMENTED = 1,
122         _UVRSR_FAILED = 2,
123     }
124     #[repr(C)]
125     enum _Unwind_VRS_RegClass {
126         _UVRSC_CORE = 0,
127         _UVRSC_VFP = 1,
128         _UVRSC_FPA = 2,
129         _UVRSC_WMMXD = 3,
130         _UVRSC_WMMXC = 4,
131     }
132     use _Unwind_VRS_RegClass::*;
133     #[repr(C)]
134     enum _Unwind_VRS_DataRepresentation {
135         _UVRSD_UINT32 = 0,
136         _UVRSD_VFPX = 1,
137         _UVRSD_FPAX = 2,
138         _UVRSD_UINT64 = 3,
139         _UVRSD_FLOAT = 4,
140         _UVRSD_DOUBLE = 5,
141     }
142     use _Unwind_VRS_DataRepresentation::*;
143
144     pub const UNWIND_POINTER_REG: c_int = 12;
145     pub const UNWIND_IP_REG: c_int = 15;
146
147     extern "C" {
148         fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
149                            regclass: _Unwind_VRS_RegClass,
150                            regno: _Unwind_Word,
151                            repr: _Unwind_VRS_DataRepresentation,
152                            data: *mut c_void)
153                            -> _Unwind_VRS_Result;
154
155         fn _Unwind_VRS_Set(ctx: *mut _Unwind_Context,
156                            regclass: _Unwind_VRS_RegClass,
157                            regno: _Unwind_Word,
158                            repr: _Unwind_VRS_DataRepresentation,
159                            data: *mut c_void)
160                            -> _Unwind_VRS_Result;
161     }
162
163     // On Android or ARM/Linux, these are implemented as macros:
164
165     pub unsafe fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word {
166         let mut val: _Unwind_Word = 0;
167         _Unwind_VRS_Get(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
168                         &mut val as *mut _ as *mut c_void);
169         val
170     }
171
172     pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) {
173         let mut value = value;
174         _Unwind_VRS_Set(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
175                         &mut value as *mut _ as *mut c_void);
176     }
177
178     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
179                                 -> _Unwind_Word {
180         let val = _Unwind_GetGR(ctx, UNWIND_IP_REG);
181         (val & !1) as _Unwind_Word
182     }
183
184     pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
185                                 value: _Unwind_Word) {
186         // Propagate thumb bit to instruction pointer
187         let thumb_state = _Unwind_GetGR(ctx, UNWIND_IP_REG) & 1;
188         let value = value | thumb_state;
189         _Unwind_SetGR(ctx, UNWIND_IP_REG, value);
190     }
191
192     pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
193                                     ip_before_insn: *mut c_int)
194                                     -> _Unwind_Word {
195         *ip_before_insn = 0;
196         _Unwind_GetIP(ctx)
197     }
198
199     // This function also doesn't exist on Android or ARM/Linux, so make it a no-op
200     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
201         pc
202     }
203 }
204 } // cfg_if!
205
206 cfg_if::cfg_if! {
207 if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
208     // Not 32-bit iOS
209     extern "C" {
210         #[unwind(allowed)]
211         pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
212         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
213                                  trace_argument: *mut c_void)
214                                  -> _Unwind_Reason_Code;
215     }
216 } else {
217     // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
218     extern "C" {
219         #[unwind(allowed)]
220         pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
221     }
222
223     #[inline]
224     pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
225         _Unwind_SjLj_RaiseException(exc)
226     }
227 }
228 } // cfg_if!