]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/lib.rs
Use #[rustc_std_internal_symbol] instead of #[no_mangle]
[rust.git] / src / libpanic_unwind / lib.rs
1 //! Implementation of panics via stack unwinding
2 //!
3 //! This crate is an implementation of panics in Rust using "most native" stack
4 //! unwinding mechanism of the platform this is being compiled for. This
5 //! essentially gets categorized into three buckets currently:
6 //!
7 //! 1. MSVC targets use SEH in the `seh.rs` file.
8 //! 2. Emscripten uses C++ exceptions in the `emcc.rs` file.
9 //! 3. All other targets use libunwind/libgcc in the `gcc.rs` file.
10 //!
11 //! More documentation about each implementation can be found in the respective
12 //! module.
13
14 #![no_std]
15 #![unstable(feature = "panic_unwind", issue = "32837")]
16 #![doc(
17     html_root_url = "https://doc.rust-lang.org/nightly/",
18     issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/"
19 )]
20 #![feature(core_intrinsics)]
21 #![feature(lang_items)]
22 #![feature(libc)]
23 #![feature(nll)]
24 #![feature(panic_unwind)]
25 #![feature(staged_api)]
26 #![feature(std_internals)]
27 #![feature(unwind_attributes)]
28 #![feature(abi_thiscall)]
29 #![feature(rustc_attrs)]
30 #![feature(raw)]
31 #![panic_runtime]
32 #![feature(panic_runtime)]
33
34 use alloc::boxed::Box;
35 use core::any::Any;
36 use core::panic::BoxMeUp;
37
38 // If adding to this list, you should also look at the list of TryPayload types
39 // defined in payload.rs and likely add to there as well.
40 cfg_if::cfg_if! {
41     if #[cfg(target_os = "emscripten")] {
42         #[path = "emcc.rs"]
43         mod imp;
44     } else if #[cfg(target_arch = "wasm32")] {
45         #[path = "dummy.rs"]
46         mod imp;
47     } else if #[cfg(target_os = "hermit")] {
48         #[path = "hermit.rs"]
49         mod imp;
50     } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
51         #[path = "dummy.rs"]
52         mod imp;
53     } else if #[cfg(target_env = "msvc")] {
54         #[path = "seh.rs"]
55         mod imp;
56     } else {
57         // Rust runtime's startup objects depend on these symbols, so make them public.
58         #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
59         pub use imp::eh_frame_registry::*;
60         #[path = "gcc.rs"]
61         mod imp;
62     }
63 }
64
65 include!("payload.rs");
66
67 extern "C" {
68     /// Handler in libstd called when a panic object is dropped outside of
69     /// `catch_unwind`.
70     fn __rust_drop_panic() -> !;
71 }
72
73 mod dwarf;
74
75 #[rustc_std_internal_symbol]
76 pub unsafe extern "C" fn __rust_panic_cleanup(
77     payload: TryPayload,
78 ) -> *mut (dyn Any + Send + 'static) {
79     Box::into_raw(imp::cleanup(payload))
80 }
81
82 // Entry point for raising an exception, just delegates to the platform-specific
83 // implementation.
84 #[rustc_std_internal_symbol]
85 #[unwind(allowed)]
86 pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
87     let payload = payload as *mut &mut dyn BoxMeUp;
88     let payload = (*payload).take_box();
89
90     // Miri panic support: cfg'd out of normal builds just to be sure.
91     // When going through normal codegen, `miri_start_panic` is a NOP, so the
92     // Miri-enabled sysroot still supports normal unwinding. But when executed in
93     // Miri, this line initiates unwinding.
94     #[cfg(miri)]
95     core::intrinsics::miri_start_panic(payload);
96
97     imp::panic(Box::from_raw(payload))
98 }