]> git.lizzy.rs Git - rust.git/blob - src/libunwind/libunwind.rs
Implement rust_eh_personality in Rust, remove rust_eh_personality_catch.
[rust.git] / src / libunwind / libunwind.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(bad_style)]
12
13 use libc;
14
15 #[cfg(any(not(target_arch = "arm"), target_os = "ios"))]
16 pub use self::_Unwind_Action::*;
17 #[cfg(target_arch = "arm")]
18 pub use self::_Unwind_State::*;
19 pub use self::_Unwind_Reason_Code::*;
20
21 #[cfg(any(not(target_arch = "arm"), target_os = "ios"))]
22 #[repr(C)]
23 #[derive(Clone, Copy)]
24 pub enum _Unwind_Action {
25     _UA_SEARCH_PHASE = 1,
26     _UA_CLEANUP_PHASE = 2,
27     _UA_HANDLER_FRAME = 4,
28     _UA_FORCE_UNWIND = 8,
29     _UA_END_OF_STACK = 16,
30 }
31
32 #[cfg(target_arch = "arm")]
33 #[repr(C)]
34 #[derive(Clone, Copy)]
35 pub enum _Unwind_State {
36     _US_VIRTUAL_UNWIND_FRAME = 0,
37     _US_UNWIND_FRAME_STARTING = 1,
38     _US_UNWIND_FRAME_RESUME = 2,
39     _US_ACTION_MASK = 3,
40     _US_FORCE_UNWIND = 8,
41     _US_END_OF_STACK = 16,
42 }
43
44 #[repr(C)]
45 pub enum _Unwind_Reason_Code {
46     _URC_NO_REASON = 0,
47     _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
48     _URC_FATAL_PHASE2_ERROR = 2,
49     _URC_FATAL_PHASE1_ERROR = 3,
50     _URC_NORMAL_STOP = 4,
51     _URC_END_OF_STACK = 5,
52     _URC_HANDLER_FOUND = 6,
53     _URC_INSTALL_CONTEXT = 7,
54     _URC_CONTINUE_UNWIND = 8,
55     _URC_FAILURE = 9, // used only by ARM EABI
56 }
57
58 pub type _Unwind_Exception_Class = u64;
59
60 pub type _Unwind_Word = libc::uintptr_t;
61 pub type _Unwind_Ptr = libc::uintptr_t;
62
63 pub type _Unwind_Trace_Fn = extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut libc::c_void)
64                                           -> _Unwind_Reason_Code;
65
66 #[cfg(target_arch = "x86")]
67 pub const unwinder_private_data_size: usize = 5;
68
69 #[cfg(target_arch = "x86_64")]
70 pub const unwinder_private_data_size: usize = 6;
71
72 #[cfg(all(target_arch = "arm", not(target_os = "ios")))]
73 pub const unwinder_private_data_size: usize = 20;
74
75 #[cfg(all(target_arch = "arm", target_os = "ios"))]
76 pub const unwinder_private_data_size: usize = 5;
77
78 #[cfg(target_arch = "aarch64")]
79 pub const unwinder_private_data_size: usize = 2;
80
81 #[cfg(target_arch = "mips")]
82 pub const unwinder_private_data_size: usize = 2;
83
84 #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
85 pub const unwinder_private_data_size: usize = 2;
86
87 #[cfg(target_arch = "asmjs")]
88 // FIXME: Copied from arm. Need to confirm.
89 pub const unwinder_private_data_size: usize = 20;
90
91 #[repr(C)]
92 pub struct _Unwind_Exception {
93     pub exception_class: _Unwind_Exception_Class,
94     pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
95     pub private: [_Unwind_Word; unwinder_private_data_size],
96 }
97
98 pub enum _Unwind_Context {}
99
100 pub type _Unwind_Exception_Cleanup_Fn = extern "C" fn(unwind_code: _Unwind_Reason_Code,
101                                                       exception: *mut _Unwind_Exception);
102
103 #[cfg_attr(any(all(target_os = "linux", not(target_env = "musl")),
104                target_os = "freebsd",
105                target_os = "solaris",
106                all(target_os = "linux",
107                    target_env = "musl",
108                    not(target_arch = "x86"),
109                    not(target_arch = "x86_64"))),
110            link(name = "gcc_s"))]
111 #[cfg_attr(all(target_os = "linux",
112                target_env = "musl",
113                any(target_arch = "x86", target_arch = "x86_64"),
114                not(test)),
115            link(name = "unwind", kind = "static"))]
116 #[cfg_attr(any(target_os = "android", target_os = "openbsd"),
117            link(name = "gcc"))]
118 #[cfg_attr(all(target_os = "netbsd", not(target_vendor = "rumprun")),
119            link(name = "gcc"))]
120 #[cfg_attr(all(target_os = "netbsd", target_vendor = "rumprun"),
121            link(name = "unwind"))]
122 #[cfg_attr(target_os = "dragonfly",
123            link(name = "gcc_pic"))]
124 #[cfg_attr(target_os = "bitrig",
125            link(name = "c++abi"))]
126 #[cfg_attr(all(target_os = "windows", target_env = "gnu"),
127            link(name = "gcc_eh"))]
128 #[cfg(not(cargobuild))]
129 extern "C" {}
130
131 extern "C" {
132     // iOS on armv7 uses SjLj exceptions and requires to link
133     // against corresponding routine (..._SjLj_...)
134     #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
135     #[unwind]
136     pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
137
138     #[cfg(all(target_os = "ios", target_arch = "arm"))]
139     #[unwind]
140     fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
141
142     pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
143
144     #[unwind]
145     pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
146
147     // No native _Unwind_Backtrace on iOS
148     #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
149     pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
150                              trace_argument: *mut libc::c_void)
151                              -> _Unwind_Reason_Code;
152
153     // available since GCC 4.2.0, should be fine for our purpose
154     #[cfg(all(not(all(target_os = "android", target_arch = "arm")),
155               not(all(target_os = "linux", target_arch = "arm"))))]
156     pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
157                              ip_before_insn: *mut libc::c_int)
158                              -> libc::uintptr_t;
159
160     pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
161     pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
162     pub fn _Unwind_GetTextRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
163     pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
164     pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: libc::c_int, value: _Unwind_Ptr);
165     pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Ptr);
166
167     #[cfg(all(not(target_os = "android"),
168               not(all(target_os = "linux", target_arch = "arm"))))]
169     pub fn _Unwind_FindEnclosingFunction(pc: *mut libc::c_void) -> *mut libc::c_void;
170 }
171
172 // ... and now we just providing access to SjLj counterspart
173 // through a standard name to hide those details from others
174 // (see also comment above regarding _Unwind_RaiseException)
175 #[cfg(all(target_os = "ios", target_arch = "arm"))]
176 #[inline]
177 pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
178     _Unwind_SjLj_RaiseException(exc)
179 }
180
181 // On android, the function _Unwind_GetIP is a macro, and this is the
182 // expansion of the macro. This is all copy/pasted directly from the
183 // header file with the definition of _Unwind_GetIP.
184 #[cfg(any(all(target_os = "android", target_arch = "arm"),
185           all(target_os = "linux", target_arch = "arm")))]
186 pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
187     #[repr(C)]
188     enum _Unwind_VRS_Result {
189         _UVRSR_OK = 0,
190         _UVRSR_NOT_IMPLEMENTED = 1,
191         _UVRSR_FAILED = 2,
192     }
193     #[repr(C)]
194     enum _Unwind_VRS_RegClass {
195         _UVRSC_CORE = 0,
196         _UVRSC_VFP = 1,
197         _UVRSC_FPA = 2,
198         _UVRSC_WMMXD = 3,
199         _UVRSC_WMMXC = 4,
200     }
201     #[repr(C)]
202     enum _Unwind_VRS_DataRepresentation {
203         _UVRSD_UINT32 = 0,
204         _UVRSD_VFPX = 1,
205         _UVRSD_FPAX = 2,
206         _UVRSD_UINT64 = 3,
207         _UVRSD_FLOAT = 4,
208         _UVRSD_DOUBLE = 5,
209     }
210
211     type _Unwind_Word = libc::c_uint;
212     extern "C" {
213         fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
214                            klass: _Unwind_VRS_RegClass,
215                            word: _Unwind_Word,
216                            repr: _Unwind_VRS_DataRepresentation,
217                            data: *mut libc::c_void)
218                            -> _Unwind_VRS_Result;
219     }
220
221     let mut val: _Unwind_Word = 0;
222     let ptr = &mut val as *mut _Unwind_Word;
223     let _ = _Unwind_VRS_Get(ctx,
224                             _Unwind_VRS_RegClass::_UVRSC_CORE,
225                             15,
226                             _Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
227                             ptr as *mut libc::c_void);
228     (val & !1) as libc::uintptr_t
229 }
230
231 // This function doesn't exist on Android or ARM/Linux, so make it same
232 // to _Unwind_GetIP
233 #[cfg(any(all(target_os = "android", target_arch = "arm"),
234           all(target_os = "linux", target_arch = "arm")))]
235 pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
236                                 ip_before_insn: *mut libc::c_int)
237                                 -> libc::uintptr_t {
238     *ip_before_insn = 0;
239     _Unwind_GetIP(ctx)
240 }
241
242 // This function also doesn't exist on Android or ARM/Linux, so make it
243 // a no-op
244 #[cfg(any(target_os = "android",
245           all(target_os = "linux", target_arch = "arm")))]
246 pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut libc::c_void) -> *mut libc::c_void {
247     pc
248 }