]> git.lizzy.rs Git - rust.git/blob - src/libunwind/libunwind.rs
[emscripten] Disable code paths that don't work on emscripten
[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 macro_rules! cfg_if {
14     ( $( if #[cfg( $meta:meta )] { $($it1:item)* } else { $($it2:item)* } )* ) =>
15         ( $( $( #[cfg($meta)] $it1)* $( #[cfg(not($meta))] $it2)* )* )
16 }
17
18 use libc::{c_int, c_void, uintptr_t};
19
20 #[repr(C)]
21 #[derive(Copy, Clone, PartialEq)]
22 pub enum _Unwind_Reason_Code {
23     _URC_NO_REASON = 0,
24     _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
25     _URC_FATAL_PHASE2_ERROR = 2,
26     _URC_FATAL_PHASE1_ERROR = 3,
27     _URC_NORMAL_STOP = 4,
28     _URC_END_OF_STACK = 5,
29     _URC_HANDLER_FOUND = 6,
30     _URC_INSTALL_CONTEXT = 7,
31     _URC_CONTINUE_UNWIND = 8,
32     _URC_FAILURE = 9, // used only by ARM EHABI
33 }
34 pub use self::_Unwind_Reason_Code::*;
35
36 pub type _Unwind_Exception_Class = u64;
37 pub type _Unwind_Word = uintptr_t;
38 pub type _Unwind_Ptr = uintptr_t;
39 pub type _Unwind_Trace_Fn = extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut c_void)
40                                           -> _Unwind_Reason_Code;
41 #[cfg(target_arch = "x86")]
42 pub const unwinder_private_data_size: usize = 5;
43
44 #[cfg(target_arch = "x86_64")]
45 pub const unwinder_private_data_size: usize = 6;
46
47 #[cfg(all(target_arch = "arm", not(target_os = "ios")))]
48 pub const unwinder_private_data_size: usize = 20;
49
50 #[cfg(all(target_arch = "arm", target_os = "ios"))]
51 pub const unwinder_private_data_size: usize = 5;
52
53 #[cfg(target_arch = "aarch64")]
54 pub const unwinder_private_data_size: usize = 2;
55
56 #[cfg(target_arch = "mips")]
57 pub const unwinder_private_data_size: usize = 2;
58
59 #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
60 pub const unwinder_private_data_size: usize = 2;
61
62 #[cfg(target_arch = "asmjs")]
63 pub const unwinder_private_data_size: usize = 20;
64
65 #[repr(C)]
66 pub struct _Unwind_Exception {
67     pub exception_class: _Unwind_Exception_Class,
68     pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
69     pub private: [_Unwind_Word; unwinder_private_data_size],
70 }
71
72 pub enum _Unwind_Context {}
73
74 pub type _Unwind_Exception_Cleanup_Fn = extern "C" fn(unwind_code: _Unwind_Reason_Code,
75                                                       exception: *mut _Unwind_Exception);
76 extern "C" {
77     #[unwind]
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! {
87 if #[cfg(not(any(all(target_os = "android", target_arch = "arm"),
88                  all(target_os = "linux", target_arch = "arm"))))] {
89     // Not ARM EHABI
90     #[repr(C)]
91     #[derive(Copy, Clone, PartialEq)]
92     pub enum _Unwind_Action {
93         _UA_SEARCH_PHASE = 1,
94         _UA_CLEANUP_PHASE = 2,
95         _UA_HANDLER_FRAME = 4,
96         _UA_FORCE_UNWIND = 8,
97         _UA_END_OF_STACK = 16,
98     }
99     pub use self::_Unwind_Action::*;
100
101     extern "C" {
102         pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
103         pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
104         pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
105         pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
106         pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
107                                  -> _Unwind_Word;
108         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
109     }
110
111 } else {
112     // ARM EHABI
113     #[repr(C)]
114     #[derive(Copy, Clone, PartialEq)]
115     pub enum _Unwind_State {
116         _US_VIRTUAL_UNWIND_FRAME = 0,
117         _US_UNWIND_FRAME_STARTING = 1,
118         _US_UNWIND_FRAME_RESUME = 2,
119         _US_ACTION_MASK = 3,
120         _US_FORCE_UNWIND = 8,
121         _US_END_OF_STACK = 16,
122     }
123     pub use self::_Unwind_State::*;
124
125     #[repr(C)]
126     enum _Unwind_VRS_Result {
127         _UVRSR_OK = 0,
128         _UVRSR_NOT_IMPLEMENTED = 1,
129         _UVRSR_FAILED = 2,
130     }
131     #[repr(C)]
132     enum _Unwind_VRS_RegClass {
133         _UVRSC_CORE = 0,
134         _UVRSC_VFP = 1,
135         _UVRSC_FPA = 2,
136         _UVRSC_WMMXD = 3,
137         _UVRSC_WMMXC = 4,
138     }
139     use self::_Unwind_VRS_RegClass::*;
140     #[repr(C)]
141     enum _Unwind_VRS_DataRepresentation {
142         _UVRSD_UINT32 = 0,
143         _UVRSD_VFPX = 1,
144         _UVRSD_FPAX = 2,
145         _UVRSD_UINT64 = 3,
146         _UVRSD_FLOAT = 4,
147         _UVRSD_DOUBLE = 5,
148     }
149     use self::_Unwind_VRS_DataRepresentation::*;
150
151     pub const UNWIND_POINTER_REG: c_int = 12;
152     pub const UNWIND_IP_REG: c_int = 15;
153
154     extern "C" {
155         fn _Unwind_VRS_Get(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         fn _Unwind_VRS_Set(ctx: *mut _Unwind_Context,
163                            regclass: _Unwind_VRS_RegClass,
164                            regno: _Unwind_Word,
165                            repr: _Unwind_VRS_DataRepresentation,
166                            data: *mut c_void)
167                            -> _Unwind_VRS_Result;
168     }
169
170     // On Android or ARM/Linux, these are implemented as macros:
171
172     pub unsafe fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word {
173         let mut val: _Unwind_Word = 0;
174         _Unwind_VRS_Get(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
175                         &mut val as *mut _ as *mut c_void);
176         val
177     }
178
179     pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) {
180         let mut value = value;
181         _Unwind_VRS_Set(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
182                         &mut value as *mut _ as *mut c_void);
183     }
184
185     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
186                                 -> _Unwind_Word {
187         let val = _Unwind_GetGR(ctx, UNWIND_IP_REG);
188         (val & !1) as _Unwind_Word
189     }
190
191     pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
192                                 value: _Unwind_Word) {
193         // Propagate thumb bit to instruction pointer
194         let thumb_state = _Unwind_GetGR(ctx, UNWIND_IP_REG) & 1;
195         let value = value | thumb_state;
196         _Unwind_SetGR(ctx, UNWIND_IP_REG, value);
197     }
198
199     pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
200                                     ip_before_insn: *mut c_int)
201                                     -> _Unwind_Word {
202         *ip_before_insn = 0;
203         _Unwind_GetIP(ctx)
204     }
205
206     // This function also doesn't exist on Android or ARM/Linux, so make it a no-op
207     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
208         pc
209     }
210 }
211
212 if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
213     // Not 32-bit iOS
214     extern "C" {
215         #[unwind]
216         pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
217         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
218                                  trace_argument: *mut c_void)
219                                  -> _Unwind_Reason_Code;
220     }
221 } else {
222     // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
223     extern "C" {
224         #[unwind]
225         pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
226     }
227
228     #[inline]
229     pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
230         _Unwind_SjLj_RaiseException(exc)
231     }
232 }
233 } // cfg_if!
234
235 #[cfg_attr(any(all(target_os = "linux", not(target_env = "musl")),
236                target_os = "freebsd",
237                target_os = "solaris",
238                all(target_os = "linux",
239                    target_env = "musl",
240                    not(target_arch = "x86"),
241                    not(target_arch = "x86_64"))),
242            link(name = "gcc_s"))]
243 #[cfg_attr(all(target_os = "linux",
244                target_env = "musl",
245                any(target_arch = "x86", target_arch = "x86_64"),
246                not(test)),
247            link(name = "unwind", kind = "static"))]
248 #[cfg_attr(any(target_os = "android", target_os = "openbsd"),
249            link(name = "gcc"))]
250 #[cfg_attr(all(target_os = "netbsd", not(target_vendor = "rumprun")),
251            link(name = "gcc"))]
252 #[cfg_attr(all(target_os = "netbsd", target_vendor = "rumprun"),
253            link(name = "unwind"))]
254 #[cfg_attr(target_os = "dragonfly",
255            link(name = "gcc_pic"))]
256 #[cfg_attr(target_os = "bitrig",
257            link(name = "c++abi"))]
258 #[cfg_attr(all(target_os = "windows", target_env = "gnu"),
259            link(name = "gcc_eh"))]
260 #[cfg(not(cargobuild))]
261 extern "C" {}