]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/libunwind.rs
ac34277dcbdc16e35fe57e0b9dd3c7d78879bef9
[rust.git] / src / libstd / rt / libunwind.rs
1 // Copyright 2014 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 //! Unwind library interface
12
13 #[allow(non_camel_case_types)];
14 #[allow(dead_code)]; // these are just bindings
15
16 use libc;
17
18 #[cfg(not(target_arch = "arm"))]
19 #[repr(C)]
20 pub enum _Unwind_Action
21 {
22     _UA_SEARCH_PHASE = 1,
23     _UA_CLEANUP_PHASE = 2,
24     _UA_HANDLER_FRAME = 4,
25     _UA_FORCE_UNWIND = 8,
26     _UA_END_OF_STACK = 16,
27 }
28
29 #[cfg(target_arch = "arm")]
30 #[repr(C)]
31 pub enum _Unwind_State
32 {
33   _US_VIRTUAL_UNWIND_FRAME = 0,
34   _US_UNWIND_FRAME_STARTING = 1,
35   _US_UNWIND_FRAME_RESUME = 2,
36   _US_ACTION_MASK = 3,
37   _US_FORCE_UNWIND = 8,
38   _US_END_OF_STACK = 16
39 }
40
41 #[repr(C)]
42 pub enum _Unwind_Reason_Code {
43     _URC_NO_REASON = 0,
44     _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
45     _URC_FATAL_PHASE2_ERROR = 2,
46     _URC_FATAL_PHASE1_ERROR = 3,
47     _URC_NORMAL_STOP = 4,
48     _URC_END_OF_STACK = 5,
49     _URC_HANDLER_FOUND = 6,
50     _URC_INSTALL_CONTEXT = 7,
51     _URC_CONTINUE_UNWIND = 8,
52     _URC_FAILURE = 9, // used only by ARM EABI
53 }
54
55 pub type _Unwind_Exception_Class = u64;
56
57 pub type _Unwind_Word = libc::uintptr_t;
58
59 #[cfg(target_arch = "x86")]
60 pub static unwinder_private_data_size: int = 5;
61
62 #[cfg(target_arch = "x86_64")]
63 pub static unwinder_private_data_size: int = 2;
64
65 #[cfg(target_arch = "arm")]
66 pub static unwinder_private_data_size: int = 20;
67
68 #[cfg(target_arch = "mips")]
69 pub static unwinder_private_data_size: int = 2;
70
71 pub struct _Unwind_Exception {
72     exception_class: _Unwind_Exception_Class,
73     exception_cleanup: _Unwind_Exception_Cleanup_Fn,
74     private: [_Unwind_Word, ..unwinder_private_data_size],
75 }
76
77 pub enum _Unwind_Context {}
78
79 pub type _Unwind_Exception_Cleanup_Fn =
80         extern "C" fn(unwind_code: _Unwind_Reason_Code,
81                       exception: *_Unwind_Exception);
82
83 pub type _Unwind_Trace_Fn =
84         extern "C" fn(ctx: *_Unwind_Context,
85                       arg: *libc::c_void) -> _Unwind_Reason_Code;
86
87 #[cfg(target_os = "linux")]
88 #[cfg(target_os = "freebsd")]
89 #[cfg(target_os = "win32")]
90 #[link(name = "gcc_s")]
91 extern {}
92
93 #[cfg(target_os = "android")]
94 #[link(name = "gcc")]
95 extern {}
96
97 extern "C" {
98     pub fn _Unwind_RaiseException(exception: *_Unwind_Exception)
99                 -> _Unwind_Reason_Code;
100     pub fn _Unwind_DeleteException(exception: *_Unwind_Exception);
101     pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
102                              trace_argument: *libc::c_void)
103                 -> _Unwind_Reason_Code;
104     #[cfg(stage0, not(target_os = "android"))]
105     pub fn _Unwind_GetIP(ctx: *_Unwind_Context) -> libc::uintptr_t;
106     #[cfg(stage0, not(target_os = "android"))]
107     pub fn _Unwind_FindEnclosingFunction(pc: *libc::c_void) -> *libc::c_void;
108
109     #[cfg(not(stage0),
110           not(target_os = "android"),
111           not(target_os = "linux", target_arch = "arm"))]
112     pub fn _Unwind_GetIP(ctx: *_Unwind_Context) -> libc::uintptr_t;
113     #[cfg(not(stage0),
114           not(target_os = "android"),
115           not(target_os = "linux", target_arch = "arm"))]
116     pub fn _Unwind_FindEnclosingFunction(pc: *libc::c_void) -> *libc::c_void;
117 }
118
119 // On android, the function _Unwind_GetIP is a macro, and this is the expansion
120 // of the macro. This is all copy/pasted directly from the header file with the
121 // definition of _Unwind_GetIP.
122 #[cfg(target_os = "android")]
123 #[cfg(target_os = "linux", target_os = "arm")]
124 pub unsafe fn _Unwind_GetIP(ctx: *_Unwind_Context) -> libc::uintptr_t {
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     #[repr(C)]
140     enum _Unwind_VRS_DataRepresentation {
141         _UVRSD_UINT32 = 0,
142         _UVRSD_VFPX = 1,
143         _UVRSD_FPAX = 2,
144         _UVRSD_UINT64 = 3,
145         _UVRSD_FLOAT = 4,
146         _UVRSD_DOUBLE = 5,
147     }
148
149     type _Unwind_Word = libc::c_uint;
150     extern {
151         fn _Unwind_VRS_Get(ctx: *_Unwind_Context,
152                            klass: _Unwind_VRS_RegClass,
153                            word: _Unwind_Word,
154                            repr: _Unwind_VRS_DataRepresentation,
155                            data: *mut libc::c_void) -> _Unwind_VRS_Result;
156     }
157
158     let mut val: _Unwind_Word = 0;
159     let ptr = &mut val as *mut _Unwind_Word;
160     let _ = _Unwind_VRS_Get(ctx, _UVRSC_CORE, 15, _UVRSD_UINT32,
161                             ptr as *mut libc::c_void);
162     (val & !1) as libc::uintptr_t
163 }
164
165 // This function also doesn't exist on android, so make it a no-op
166 #[cfg(target_os = "android")]
167 #[cfg(target_os = "linux", target_os = "arm")]
168 pub unsafe fn _Unwind_FindEnclosingFunction(pc: *libc::c_void) -> *libc::c_void{
169     pc
170 }