]> git.proxmox.com Git - rustc.git/blob - src/libunwind/libunwind.rs
Imported Upstream version 1.11.0+dfsg1
[rustc.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
62 pub type _Unwind_Trace_Fn = extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut libc::c_void)
63 -> _Unwind_Reason_Code;
64
65 #[cfg(target_arch = "x86")]
66 pub const unwinder_private_data_size: usize = 5;
67
68 #[cfg(target_arch = "x86_64")]
69 pub const unwinder_private_data_size: usize = 6;
70
71 #[cfg(all(target_arch = "arm", not(target_os = "ios")))]
72 pub const unwinder_private_data_size: usize = 20;
73
74 #[cfg(all(target_arch = "arm", target_os = "ios"))]
75 pub const unwinder_private_data_size: usize = 5;
76
77 #[cfg(target_arch = "aarch64")]
78 pub const unwinder_private_data_size: usize = 2;
79
80 #[cfg(target_arch = "mips")]
81 pub const unwinder_private_data_size: usize = 2;
82
83 #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
84 pub const unwinder_private_data_size: usize = 2;
85
86 #[cfg(target_arch = "asmjs")]
87 // FIXME: Copied from arm. Need to confirm.
88 pub const unwinder_private_data_size: usize = 20;
89
90 #[repr(C)]
91 pub struct _Unwind_Exception {
92 pub exception_class: _Unwind_Exception_Class,
93 pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
94 pub private: [_Unwind_Word; unwinder_private_data_size],
95 }
96
97 pub enum _Unwind_Context {}
98
99 pub type _Unwind_Exception_Cleanup_Fn = extern "C" fn(unwind_code: _Unwind_Reason_Code,
100 exception: *mut _Unwind_Exception);
101
102 #[cfg_attr(any(all(target_os = "linux", not(target_env = "musl")),
103 target_os = "freebsd",
104 target_os = "solaris",
105 all(target_os = "linux",
106 target_env = "musl",
107 not(target_arch = "x86"),
108 not(target_arch = "x86_64"))),
109 link(name = "gcc_s"))]
110 #[cfg_attr(all(target_os = "linux",
111 target_env = "musl",
112 any(target_arch = "x86", target_arch = "x86_64"),
113 not(test)),
114 link(name = "unwind", kind = "static"))]
115 #[cfg_attr(any(target_os = "android", target_os = "openbsd"),
116 link(name = "gcc"))]
117 #[cfg_attr(all(target_os = "netbsd", not(target_vendor = "rumprun")),
118 link(name = "gcc"))]
119 #[cfg_attr(all(target_os = "netbsd", target_vendor = "rumprun"),
120 link(name = "unwind"))]
121 #[cfg_attr(target_os = "dragonfly",
122 link(name = "gcc_pic"))]
123 #[cfg_attr(target_os = "bitrig",
124 link(name = "c++abi"))]
125 #[cfg_attr(all(target_os = "windows", target_env = "gnu"),
126 link(name = "gcc_eh"))]
127 #[cfg(not(cargobuild))]
128 extern "C" {}
129
130 extern "C" {
131 // iOS on armv7 uses SjLj exceptions and requires to link
132 // against corresponding routine (..._SjLj_...)
133 #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
134 #[unwind]
135 pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
136
137 #[cfg(all(target_os = "ios", target_arch = "arm"))]
138 #[unwind]
139 fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
140
141 pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
142
143 #[unwind]
144 pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
145
146 // No native _Unwind_Backtrace on iOS
147 #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
148 pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
149 trace_argument: *mut libc::c_void)
150 -> _Unwind_Reason_Code;
151
152 // available since GCC 4.2.0, should be fine for our purpose
153 #[cfg(all(not(all(target_os = "android", target_arch = "arm")),
154 not(all(target_os = "linux", target_arch = "arm"))))]
155 pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
156 ip_before_insn: *mut libc::c_int)
157 -> libc::uintptr_t;
158
159 #[cfg(all(not(target_os = "android"),
160 not(all(target_os = "linux", target_arch = "arm"))))]
161 pub fn _Unwind_FindEnclosingFunction(pc: *mut libc::c_void) -> *mut libc::c_void;
162 }
163
164 // ... and now we just providing access to SjLj counterspart
165 // through a standard name to hide those details from others
166 // (see also comment above regarding _Unwind_RaiseException)
167 #[cfg(all(target_os = "ios", target_arch = "arm"))]
168 #[inline]
169 pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
170 _Unwind_SjLj_RaiseException(exc)
171 }
172
173 // On android, the function _Unwind_GetIP is a macro, and this is the
174 // expansion of the macro. This is all copy/pasted directly from the
175 // header file with the definition of _Unwind_GetIP.
176 #[cfg(any(all(target_os = "android", target_arch = "arm"),
177 all(target_os = "linux", target_arch = "arm")))]
178 pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
179 #[repr(C)]
180 enum _Unwind_VRS_Result {
181 _UVRSR_OK = 0,
182 _UVRSR_NOT_IMPLEMENTED = 1,
183 _UVRSR_FAILED = 2,
184 }
185 #[repr(C)]
186 enum _Unwind_VRS_RegClass {
187 _UVRSC_CORE = 0,
188 _UVRSC_VFP = 1,
189 _UVRSC_FPA = 2,
190 _UVRSC_WMMXD = 3,
191 _UVRSC_WMMXC = 4,
192 }
193 #[repr(C)]
194 enum _Unwind_VRS_DataRepresentation {
195 _UVRSD_UINT32 = 0,
196 _UVRSD_VFPX = 1,
197 _UVRSD_FPAX = 2,
198 _UVRSD_UINT64 = 3,
199 _UVRSD_FLOAT = 4,
200 _UVRSD_DOUBLE = 5,
201 }
202
203 type _Unwind_Word = libc::c_uint;
204 extern "C" {
205 fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
206 klass: _Unwind_VRS_RegClass,
207 word: _Unwind_Word,
208 repr: _Unwind_VRS_DataRepresentation,
209 data: *mut libc::c_void)
210 -> _Unwind_VRS_Result;
211 }
212
213 let mut val: _Unwind_Word = 0;
214 let ptr = &mut val as *mut _Unwind_Word;
215 let _ = _Unwind_VRS_Get(ctx,
216 _Unwind_VRS_RegClass::_UVRSC_CORE,
217 15,
218 _Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
219 ptr as *mut libc::c_void);
220 (val & !1) as libc::uintptr_t
221 }
222
223 // This function doesn't exist on Android or ARM/Linux, so make it same
224 // to _Unwind_GetIP
225 #[cfg(any(all(target_os = "android", target_arch = "arm"),
226 all(target_os = "linux", target_arch = "arm")))]
227 pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
228 ip_before_insn: *mut libc::c_int)
229 -> libc::uintptr_t {
230 *ip_before_insn = 0;
231 _Unwind_GetIP(ctx)
232 }
233
234 // This function also doesn't exist on Android or ARM/Linux, so make it
235 // a no-op
236 #[cfg(any(target_os = "android",
237 all(target_os = "linux", target_arch = "arm")))]
238 pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut libc::c_void) -> *mut libc::c_void {
239 pc
240 }