]> git.lizzy.rs Git - rust.git/blob - src/libunwind/libunwind.rs
Rollup merge of #59890 - GuillaumeGomez:empty-json-variables, r=QuietMisdreavus
[rust.git] / src / libunwind / libunwind.rs
1 #![allow(nonstandard_style)]
2
3 macro_rules! cfg_if {
4     ( $( if #[cfg( $meta:meta )] { $($it1:item)* } else { $($it2:item)* } )* ) =>
5         ( $( $( #[cfg($meta)] $it1)* $( #[cfg(not($meta))] $it2)* )* )
6 }
7
8 use libc::{c_int, c_void, uintptr_t};
9
10 #[repr(C)]
11 #[derive(Debug, Copy, Clone, PartialEq)]
12 pub enum _Unwind_Reason_Code {
13     _URC_NO_REASON = 0,
14     _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
15     _URC_FATAL_PHASE2_ERROR = 2,
16     _URC_FATAL_PHASE1_ERROR = 3,
17     _URC_NORMAL_STOP = 4,
18     _URC_END_OF_STACK = 5,
19     _URC_HANDLER_FOUND = 6,
20     _URC_INSTALL_CONTEXT = 7,
21     _URC_CONTINUE_UNWIND = 8,
22     _URC_FAILURE = 9, // used only by ARM EHABI
23 }
24 pub use _Unwind_Reason_Code::*;
25
26 pub type _Unwind_Exception_Class = u64;
27 pub type _Unwind_Word = uintptr_t;
28 pub type _Unwind_Ptr = uintptr_t;
29 pub type _Unwind_Trace_Fn = extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut c_void)
30                                           -> _Unwind_Reason_Code;
31 #[cfg(target_arch = "x86")]
32 pub const unwinder_private_data_size: usize = 5;
33
34 #[cfg(target_arch = "x86_64")]
35 pub const unwinder_private_data_size: usize = 6;
36
37 #[cfg(all(target_arch = "arm", not(target_os = "ios")))]
38 pub const unwinder_private_data_size: usize = 20;
39
40 #[cfg(all(target_arch = "arm", target_os = "ios"))]
41 pub const unwinder_private_data_size: usize = 5;
42
43 #[cfg(target_arch = "aarch64")]
44 pub const unwinder_private_data_size: usize = 2;
45
46 #[cfg(target_arch = "mips")]
47 pub const unwinder_private_data_size: usize = 2;
48
49 #[cfg(target_arch = "mips64")]
50 pub const unwinder_private_data_size: usize = 2;
51
52 #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
53 pub const unwinder_private_data_size: usize = 2;
54
55 #[cfg(target_arch = "s390x")]
56 pub const unwinder_private_data_size: usize = 2;
57
58 #[cfg(target_arch = "sparc64")]
59 pub const unwinder_private_data_size: usize = 2;
60
61 #[cfg(target_os = "emscripten")]
62 pub const unwinder_private_data_size: usize = 20;
63
64 #[repr(C)]
65 pub struct _Unwind_Exception {
66     pub exception_class: _Unwind_Exception_Class,
67     pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
68     pub private: [_Unwind_Word; unwinder_private_data_size],
69 }
70
71 pub enum _Unwind_Context {}
72
73 pub type _Unwind_Exception_Cleanup_Fn = extern "C" fn(unwind_code: _Unwind_Reason_Code,
74                                                       exception: *mut _Unwind_Exception);
75 extern "C" {
76     #[unwind(allowed)]
77     pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
78     pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
79     pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> *mut c_void;
80     pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
81     pub fn _Unwind_GetTextRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
82     pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
83 }
84
85 cfg_if! {
86 if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm"))))] {
87     // Not ARM EHABI
88     #[repr(C)]
89     #[derive(Copy, Clone, PartialEq)]
90     pub enum _Unwind_Action {
91         _UA_SEARCH_PHASE = 1,
92         _UA_CLEANUP_PHASE = 2,
93         _UA_HANDLER_FRAME = 4,
94         _UA_FORCE_UNWIND = 8,
95         _UA_END_OF_STACK = 16,
96     }
97     pub use _Unwind_Action::*;
98
99     extern "C" {
100         pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
101         pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
102         pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
103         pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
104         pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
105                                  -> _Unwind_Word;
106         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
107     }
108
109 } else {
110     // ARM EHABI
111     #[repr(C)]
112     #[derive(Copy, Clone, PartialEq)]
113     pub enum _Unwind_State {
114         _US_VIRTUAL_UNWIND_FRAME = 0,
115         _US_UNWIND_FRAME_STARTING = 1,
116         _US_UNWIND_FRAME_RESUME = 2,
117         _US_ACTION_MASK = 3,
118         _US_FORCE_UNWIND = 8,
119         _US_END_OF_STACK = 16,
120     }
121     pub use _Unwind_State::*;
122
123     #[repr(C)]
124     enum _Unwind_VRS_Result {
125         _UVRSR_OK = 0,
126         _UVRSR_NOT_IMPLEMENTED = 1,
127         _UVRSR_FAILED = 2,
128     }
129     #[repr(C)]
130     enum _Unwind_VRS_RegClass {
131         _UVRSC_CORE = 0,
132         _UVRSC_VFP = 1,
133         _UVRSC_FPA = 2,
134         _UVRSC_WMMXD = 3,
135         _UVRSC_WMMXC = 4,
136     }
137     use _Unwind_VRS_RegClass::*;
138     #[repr(C)]
139     enum _Unwind_VRS_DataRepresentation {
140         _UVRSD_UINT32 = 0,
141         _UVRSD_VFPX = 1,
142         _UVRSD_FPAX = 2,
143         _UVRSD_UINT64 = 3,
144         _UVRSD_FLOAT = 4,
145         _UVRSD_DOUBLE = 5,
146     }
147     use _Unwind_VRS_DataRepresentation::*;
148
149     pub const UNWIND_POINTER_REG: c_int = 12;
150     pub const UNWIND_IP_REG: c_int = 15;
151
152     extern "C" {
153         fn _Unwind_VRS_Get(ctx: *mut _Unwind_Context,
154                            regclass: _Unwind_VRS_RegClass,
155                            regno: _Unwind_Word,
156                            repr: _Unwind_VRS_DataRepresentation,
157                            data: *mut c_void)
158                            -> _Unwind_VRS_Result;
159
160         fn _Unwind_VRS_Set(ctx: *mut _Unwind_Context,
161                            regclass: _Unwind_VRS_RegClass,
162                            regno: _Unwind_Word,
163                            repr: _Unwind_VRS_DataRepresentation,
164                            data: *mut c_void)
165                            -> _Unwind_VRS_Result;
166     }
167
168     // On Android or ARM/Linux, these are implemented as macros:
169
170     pub unsafe fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word {
171         let mut val: _Unwind_Word = 0;
172         _Unwind_VRS_Get(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
173                         &mut val as *mut _ as *mut c_void);
174         val
175     }
176
177     pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) {
178         let mut value = value;
179         _Unwind_VRS_Set(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32,
180                         &mut value as *mut _ as *mut c_void);
181     }
182
183     pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
184                                 -> _Unwind_Word {
185         let val = _Unwind_GetGR(ctx, UNWIND_IP_REG);
186         (val & !1) as _Unwind_Word
187     }
188
189     pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
190                                 value: _Unwind_Word) {
191         // Propagate thumb bit to instruction pointer
192         let thumb_state = _Unwind_GetGR(ctx, UNWIND_IP_REG) & 1;
193         let value = value | thumb_state;
194         _Unwind_SetGR(ctx, UNWIND_IP_REG, value);
195     }
196
197     pub unsafe fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context,
198                                     ip_before_insn: *mut c_int)
199                                     -> _Unwind_Word {
200         *ip_before_insn = 0;
201         _Unwind_GetIP(ctx)
202     }
203
204     // This function also doesn't exist on Android or ARM/Linux, so make it a no-op
205     pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
206         pc
207     }
208 }
209
210 if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
211     // Not 32-bit iOS
212     extern "C" {
213         #[unwind(allowed)]
214         pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
215         pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
216                                  trace_argument: *mut c_void)
217                                  -> _Unwind_Reason_Code;
218     }
219 } else {
220     // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
221     extern "C" {
222         #[unwind(allowed)]
223         pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
224     }
225
226     #[inline]
227     pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
228         _Unwind_SjLj_RaiseException(exc)
229     }
230 }
231 } // cfg_if!