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