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