]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/libunwind.rs
Register new snapshots
[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
105     #[cfg(not(target_os = "android"),
106           not(target_os = "linux", target_arch = "arm"))]
107     pub fn _Unwind_GetIP(ctx: *_Unwind_Context) -> libc::uintptr_t;
108     #[cfg(not(target_os = "android"),
109           not(target_os = "linux", target_arch = "arm"))]
110     pub fn _Unwind_FindEnclosingFunction(pc: *libc::c_void) -> *libc::c_void;
111 }
112
113 // On android, the function _Unwind_GetIP is a macro, and this is the expansion
114 // of the macro. This is all copy/pasted directly from the header file with the
115 // definition of _Unwind_GetIP.
116 #[cfg(target_os = "android")]
117 #[cfg(target_os = "linux", target_arch = "arm")]
118 pub unsafe fn _Unwind_GetIP(ctx: *_Unwind_Context) -> libc::uintptr_t {
119     #[repr(C)]
120     enum _Unwind_VRS_Result {
121         _UVRSR_OK = 0,
122         _UVRSR_NOT_IMPLEMENTED = 1,
123         _UVRSR_FAILED = 2,
124     }
125     #[repr(C)]
126     enum _Unwind_VRS_RegClass {
127         _UVRSC_CORE = 0,
128         _UVRSC_VFP = 1,
129         _UVRSC_FPA = 2,
130         _UVRSC_WMMXD = 3,
131         _UVRSC_WMMXC = 4,
132     }
133     #[repr(C)]
134     enum _Unwind_VRS_DataRepresentation {
135         _UVRSD_UINT32 = 0,
136         _UVRSD_VFPX = 1,
137         _UVRSD_FPAX = 2,
138         _UVRSD_UINT64 = 3,
139         _UVRSD_FLOAT = 4,
140         _UVRSD_DOUBLE = 5,
141     }
142
143     type _Unwind_Word = libc::c_uint;
144     extern {
145         fn _Unwind_VRS_Get(ctx: *_Unwind_Context,
146                            klass: _Unwind_VRS_RegClass,
147                            word: _Unwind_Word,
148                            repr: _Unwind_VRS_DataRepresentation,
149                            data: *mut libc::c_void) -> _Unwind_VRS_Result;
150     }
151
152     let mut val: _Unwind_Word = 0;
153     let ptr = &mut val as *mut _Unwind_Word;
154     let _ = _Unwind_VRS_Get(ctx, _UVRSC_CORE, 15, _UVRSD_UINT32,
155                             ptr as *mut libc::c_void);
156     (val & !1) as libc::uintptr_t
157 }
158
159 // This function also doesn't exist on android or arm/linux, so make it a no-op
160 #[cfg(target_os = "android")]
161 #[cfg(target_os = "linux", target_arch = "arm")]
162 pub unsafe fn _Unwind_FindEnclosingFunction(pc: *libc::c_void) -> *libc::c_void {
163     pc
164 }