]> git.lizzy.rs Git - rust.git/blob - library/unwind/src/libunwind.rs
196be74decba4e4fb9c454f3fdf7a50ede26585e
[rust.git] / library / unwind / src / 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 =
25     extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut c_void) -> _Unwind_Reason_Code;
26
27 #[cfg(target_arch = "x86")]
28 pub const unwinder_private_data_size: usize = 5;
29
30 #[cfg(target_arch = "x86_64")]
31 pub const unwinder_private_data_size: usize = 6;
32
33 #[cfg(all(target_arch = "arm", not(target_os = "ios")))]
34 pub const unwinder_private_data_size: usize = 20;
35
36 #[cfg(all(target_arch = "arm", target_os = "ios"))]
37 pub const unwinder_private_data_size: usize = 5;
38
39 #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))]
40 pub const unwinder_private_data_size: usize = 2;
41
42 #[cfg(all(target_arch = "aarch64", target_pointer_width = "32"))]
43 pub const unwinder_private_data_size: usize = 5;
44
45 #[cfg(target_arch = "mips")]
46 pub const unwinder_private_data_size: usize = 2;
47
48 #[cfg(target_arch = "mips64")]
49 pub const unwinder_private_data_size: usize = 2;
50
51 #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
52 pub const unwinder_private_data_size: usize = 2;
53
54 #[cfg(target_arch = "s390x")]
55 pub const unwinder_private_data_size: usize = 2;
56
57 #[cfg(any(target_arch = "sparc", target_arch = "sparc64"))]
58 pub const unwinder_private_data_size: usize = 2;
59
60 #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
61 pub const unwinder_private_data_size: usize = 2;
62
63 #[cfg(target_os = "emscripten")]
64 pub const unwinder_private_data_size: usize = 20;
65
66 #[cfg(all(target_arch = "hexagon", target_os = "linux"))]
67 pub const unwinder_private_data_size: usize = 35;
68
69 #[repr(C)]
70 pub struct _Unwind_Exception {
71     pub exception_class: _Unwind_Exception_Class,
72     pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
73     pub private: [_Unwind_Word; unwinder_private_data_size],
74 }
75
76 pub enum _Unwind_Context {}
77
78 pub type _Unwind_Exception_Cleanup_Fn =
79     extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
80 #[cfg_attr(
81     all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
82     link(name = "unwind", kind = "static")
83 )]
84 extern "C-unwind" {
85     pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
86 }
87 extern "C" {
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::cfg_if! {
96 if #[cfg(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 _Unwind_Action::*;
108
109     #[cfg_attr(all(feature = "llvm-libunwind",
110                    any(target_os = "fuchsia", target_os = "linux")),
111                link(name = "unwind", kind = "static"))]
112     extern "C" {
113         pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
114         pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
115         pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
116         pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
117         pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
118                                  -> _Unwind_Word;
119         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
120     }
121
122 } else {
123     // ARM EHABI
124     #[repr(C)]
125     #[derive(Copy, Clone, PartialEq)]
126     pub enum _Unwind_State {
127         _US_VIRTUAL_UNWIND_FRAME = 0,
128         _US_UNWIND_FRAME_STARTING = 1,
129         _US_UNWIND_FRAME_RESUME = 2,
130         _US_ACTION_MASK = 3,
131         _US_FORCE_UNWIND = 8,
132         _US_END_OF_STACK = 16,
133     }
134     pub use _Unwind_State::*;
135
136     #[repr(C)]
137     enum _Unwind_VRS_Result {
138         _UVRSR_OK = 0,
139         _UVRSR_NOT_IMPLEMENTED = 1,
140         _UVRSR_FAILED = 2,
141     }
142     #[repr(C)]
143     enum _Unwind_VRS_RegClass {
144         _UVRSC_CORE = 0,
145         _UVRSC_VFP = 1,
146         _UVRSC_FPA = 2,
147         _UVRSC_WMMXD = 3,
148         _UVRSC_WMMXC = 4,
149     }
150     use _Unwind_VRS_RegClass::*;
151     #[repr(C)]
152     enum _Unwind_VRS_DataRepresentation {
153         _UVRSD_UINT32 = 0,
154         _UVRSD_VFPX = 1,
155         _UVRSD_FPAX = 2,
156         _UVRSD_UINT64 = 3,
157         _UVRSD_FLOAT = 4,
158         _UVRSD_DOUBLE = 5,
159     }
160     use _Unwind_VRS_DataRepresentation::*;
161
162     pub const UNWIND_POINTER_REG: c_int = 12;
163     pub const UNWIND_SP_REG: c_int = 13;
164     pub const UNWIND_IP_REG: c_int = 15;
165
166     #[cfg_attr(all(feature = "llvm-libunwind",
167                    any(target_os = "fuchsia", target_os = "linux")),
168                link(name = "unwind", kind = "static"))]
169     extern "C" {
170         fn _Unwind_VRS_Get(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         fn _Unwind_VRS_Set(ctx: *mut _Unwind_Context,
178                            regclass: _Unwind_VRS_RegClass,
179                            regno: _Unwind_Word,
180                            repr: _Unwind_VRS_DataRepresentation,
181                            data: *mut c_void)
182                            -> _Unwind_VRS_Result;
183     }
184
185     // On Android or ARM/Linux, these are implemented as macros:
186
187     pub unsafe fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word {
188         let mut val: _Unwind_Word = 0;
189         _Unwind_VRS_Get(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
190                         &mut val as *mut _ as *mut c_void);
191         val
192     }
193
194     pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) {
195         let mut value = value;
196         _Unwind_VRS_Set(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
197                         &mut value as *mut _ as *mut c_void);
198     }
199
200     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
201                                 -> _Unwind_Word {
202         let val = _Unwind_GetGR(ctx, UNWIND_IP_REG);
203         (val & !1) as _Unwind_Word
204     }
205
206     pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
207                                 value: _Unwind_Word) {
208         // Propagate thumb bit to instruction pointer
209         let thumb_state = _Unwind_GetGR(ctx, UNWIND_IP_REG) & 1;
210         let value = value | thumb_state;
211         _Unwind_SetGR(ctx, UNWIND_IP_REG, value);
212     }
213
214     pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
215                                     ip_before_insn: *mut c_int)
216                                     -> _Unwind_Word {
217         *ip_before_insn = 0;
218         _Unwind_GetIP(ctx)
219     }
220
221     // This function also doesn't exist on Android or ARM/Linux, so make it a no-op
222     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
223         pc
224     }
225 }
226 } // cfg_if!
227
228 cfg_if::cfg_if! {
229 if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
230     // Not 32-bit iOS
231     #[cfg_attr(all(feature = "llvm-libunwind",
232                    any(target_os = "fuchsia", target_os = "linux")),
233                link(name = "unwind", kind = "static"))]
234     extern "C-unwind" {
235         pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
236     }
237     extern "C" {
238         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
239                                  trace_argument: *mut c_void)
240                                  -> _Unwind_Reason_Code;
241     }
242 } else {
243     // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
244     #[cfg_attr(all(feature = "llvm-libunwind",
245                    any(target_os = "fuchsia", target_os = "linux")),
246                link(name = "unwind", kind = "static"))]
247     extern "C-unwind" {
248         pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
249     }
250
251     #[inline]
252     pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
253         _Unwind_SjLj_RaiseException(exc)
254     }
255 }
256 } // cfg_if!
257
258 cfg_if::cfg_if! {
259 if #[cfg(all(windows, target_arch = "x86_64", target_env = "gnu"))] {
260     // We declare these as opaque types. This is fine since you just need to
261     // pass them to _GCC_specific_handler and forget about them.
262     pub enum EXCEPTION_RECORD {}
263     pub type LPVOID = *mut c_void;
264     pub enum CONTEXT {}
265     pub enum DISPATCHER_CONTEXT {}
266     pub type EXCEPTION_DISPOSITION = c_int;
267     type PersonalityFn = unsafe extern "C" fn(version: c_int,
268                                               actions: _Unwind_Action,
269                                               exception_class: _Unwind_Exception_Class,
270                                               exception_object: *mut _Unwind_Exception,
271                                               context: *mut _Unwind_Context)
272                                               -> _Unwind_Reason_Code;
273
274     extern "C" {
275         pub fn _GCC_specific_handler(exceptionRecord: *mut EXCEPTION_RECORD,
276                                 establisherFrame: LPVOID,
277                                 contextRecord: *mut CONTEXT,
278                                 dispatcherContext: *mut DISPATCHER_CONTEXT,
279                                 personality: PersonalityFn)
280                                 -> EXCEPTION_DISPOSITION;
281     }
282 }
283 } // cfg_if!