rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
#![no_std]
|
|
|
|
|
#![unstable(feature = "panic_unwind", issue = "32837")]
|
2017-08-22 16:24:29 -05:00
|
|
|
#![feature(link_cfg)]
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
#![feature(staged_api)]
|
2021-06-08 11:23:58 -07:00
|
|
|
#![feature(c_unwind)]
|
2022-03-06 15:12:50 +01:00
|
|
|
#![feature(cfg_target_abi)]
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
|
|
|
|
|
|
2019-06-10 08:12:14 -07:00
|
|
|
cfg_if::cfg_if! {
|
2017-10-22 20:01:00 -07:00
|
|
|
if #[cfg(target_env = "msvc")] {
|
2020-07-06 20:31:12 -07:00
|
|
|
// Windows MSVC no extra unwinder support needed
|
|
|
|
|
} else if #[cfg(any(
|
|
|
|
|
target_os = "l4re",
|
|
|
|
|
target_os = "none",
|
2021-07-29 20:18:22 +03:00
|
|
|
target_os = "espidf",
|
2020-07-06 20:31:12 -07:00
|
|
|
))] {
|
|
|
|
|
// These "unix" family members do not have unwinder.
|
2020-10-11 18:01:32 -04:00
|
|
|
// Note this also matches x86_64-unknown-none-linuxkernel.
|
2020-07-06 20:31:12 -07:00
|
|
|
} else if #[cfg(any(
|
|
|
|
|
unix,
|
|
|
|
|
windows,
|
2020-08-08 02:14:40 -04:00
|
|
|
target_os = "psp",
|
Add SOLID targets
SOLID[1] is an embedded development platform provided by Kyoto
Microcomputer Co., Ltd. This commit introduces a basic Tier 3 support
for SOLID.
# New Targets
The following targets are added:
- `aarch64-kmc-solid_asp3`
- `armv7a-kmc-solid_asp3-eabi`
- `armv7a-kmc-solid_asp3-eabihf`
SOLID's target software system can be divided into two parts: an
RTOS kernel, which is responsible for threading and synchronization,
and Core Services, which provides filesystems, networking, and other
things. The RTOS kernel is a μITRON4.0[2][3]-derived kernel based on
the open-source TOPPERS RTOS kernels[4]. For uniprocessor systems
(more precisely, systems where only one processor core is allocated for
SOLID), this will be the TOPPERS/ASP3 kernel. As μITRON is
traditionally only specified at the source-code level, the ABI is
unique to each implementation, which is why `asp3` is included in the
target names.
More targets could be added later, as we support other base kernels
(there are at least three at the point of writing) and are interested
in supporting other processor architectures in the future.
# C Compiler
Although SOLID provides its own supported C/C++ build toolchain, GNU Arm
Embedded Toolchain seems to work for the purpose of building Rust.
# Unresolved Questions
A μITRON4 kernel can support `Thread::unpark` natively, but it's not
used by this commit's implementation because the underlying kernel
feature is also used to implement `Condvar`, and it's unclear whether
`std` should guarantee that parking tokens are not clobbered by other
synchronization primitives.
# Unsupported or Unimplemented Features
Most features are implemented. The following features are not
implemented due to the lack of native support:
- `fs::File::{file_attr, truncate, duplicate, set_permissions}`
- `fs::{symlink, link, canonicalize}`
- Process creation
- Command-line arguments
Backtrace generation is not really a good fit for embedded targets, so
it's intentionally left unimplemented. Unwinding is functional, however.
## Dynamic Linking
Dynamic linking is not supported. The target platform supports dynamic
linking, but enabling this in Rust causes several problems.
- The linker invocation used to build the shared object of `std` is
too long for the platform-provided linker to handle.
- A linker script with specific requirements is required for the
compiled shared object to be actually loadable.
As such, we decided to disable dynamic linking for now. Regardless, the
users can try to create shared objects by manually invoking the linker.
## Executable
Building an executable is not supported as the notion of "executable
files" isn't well-defined for these targets.
[1] https://solid.kmckk.com/SOLID/
[2] http://ertl.jp/ITRON/SPEC/mitron4-e.html
[3] https://en.wikipedia.org/wiki/ITRON_project
[4] https://toppers.jp/
2021-09-28 11:20:46 +09:00
|
|
|
target_os = "solid_asp3",
|
2020-07-06 20:31:12 -07:00
|
|
|
all(target_vendor = "fortanix", target_env = "sgx"),
|
|
|
|
|
))] {
|
2017-10-22 20:01:00 -07:00
|
|
|
mod libunwind;
|
|
|
|
|
pub use libunwind::*;
|
2020-07-06 20:31:12 -07:00
|
|
|
} else {
|
|
|
|
|
// no unwinder on the system!
|
|
|
|
|
// - wasm32 (not emscripten, which is "unix" family)
|
|
|
|
|
// - os=none ("bare metal" targets)
|
|
|
|
|
// - os=hermit
|
|
|
|
|
// - os=uefi
|
|
|
|
|
// - os=cuda
|
|
|
|
|
// - nvptx64-nvidia-cuda
|
|
|
|
|
// - Any new targets not listed above.
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-08-22 16:24:29 -05:00
|
|
|
|
2018-01-12 21:22:06 -02:00
|
|
|
#[cfg(target_env = "musl")]
|
2021-05-24 17:53:09 +08:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
|
if #[cfg(all(feature = "llvm-libunwind", feature = "system-llvm-libunwind"))] {
|
|
|
|
|
compile_error!("`llvm-libunwind` and `system-llvm-libunwind` cannot be enabled at the same time");
|
|
|
|
|
} else if #[cfg(feature = "llvm-libunwind")] {
|
2021-07-26 22:47:07 +08:00
|
|
|
#[link(name = "unwind", kind = "static", modifiers = "-bundle")]
|
2021-05-24 17:53:09 +08:00
|
|
|
extern "C" {}
|
|
|
|
|
} else if #[cfg(feature = "system-llvm-libunwind")] {
|
2021-07-26 22:47:07 +08:00
|
|
|
#[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
|
2021-05-24 17:53:09 +08:00
|
|
|
#[link(name = "unwind", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
|
|
|
|
} else {
|
2021-07-26 22:47:07 +08:00
|
|
|
#[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
|
2021-05-24 17:53:09 +08:00
|
|
|
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-07 08:39:54 -06:00
|
|
|
|
2022-07-16 19:33:54 +03:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
|
if #[cfg(feature = "llvm-libunwind")] {
|
|
|
|
|
compile_error!("`llvm-libunwind` is not supported for Android targets");
|
|
|
|
|
} else if #[cfg(feature = "system-llvm-libunwind")] {
|
|
|
|
|
#[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
|
|
|
|
|
#[link(name = "unwind", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
|
|
|
|
} else {
|
|
|
|
|
#[link(name = "gcc", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
|
|
|
|
|
#[link(name = "gcc", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Android's unwinding library depends on dl_iterate_phdr in `libdl`.
|
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
|
#[link(name = "dl", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
|
|
|
|
|
#[link(name = "dl", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
|
|
|
|
|
2020-10-01 11:13:13 -07:00
|
|
|
// When building with crt-static, we get `gcc_eh` from the `libc` crate, since
|
|
|
|
|
// glibc needs it, and needs it listed later on the linker command line. We
|
|
|
|
|
// don't want to duplicate it here.
|
2020-10-08 15:05:31 +02:00
|
|
|
#[cfg(all(
|
|
|
|
|
target_os = "linux",
|
2021-09-14 20:50:35 -04:00
|
|
|
any(target_env = "gnu", target_env = "uclibc"),
|
2020-10-08 15:05:31 +02:00
|
|
|
not(feature = "llvm-libunwind"),
|
|
|
|
|
not(feature = "system-llvm-libunwind")
|
|
|
|
|
))]
|
2020-09-20 21:19:34 -07:00
|
|
|
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
|
|
|
|
|
2020-10-08 15:05:31 +02:00
|
|
|
#[cfg(all(
|
|
|
|
|
target_os = "linux",
|
2021-09-14 20:50:35 -04:00
|
|
|
any(target_env = "gnu", target_env = "uclibc"),
|
2020-10-08 15:05:31 +02:00
|
|
|
not(feature = "llvm-libunwind"),
|
|
|
|
|
feature = "system-llvm-libunwind"
|
|
|
|
|
))]
|
|
|
|
|
#[link(name = "unwind", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
|
|
|
|
|
2019-04-07 08:39:54 -06:00
|
|
|
#[cfg(target_os = "redox")]
|
2021-07-26 22:47:07 +08:00
|
|
|
#[link(name = "gcc_eh", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
|
2019-04-07 08:39:54 -06:00
|
|
|
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
|
|
|
|
|
extern "C" {}
|
2020-05-17 21:08:23 +03:00
|
|
|
|
|
|
|
|
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
|
2021-07-26 22:47:07 +08:00
|
|
|
#[link(name = "unwind", kind = "static", modifiers = "-bundle")]
|
2020-05-17 21:08:23 +03:00
|
|
|
extern "C" {}
|
2022-03-06 15:12:50 +01:00
|
|
|
|
|
|
|
|
#[cfg(all(target_os = "windows", target_env = "gnu", target_abi = "llvm"))]
|
|
|
|
|
#[link(name = "unwind", kind = "static", modifiers = "-bundle")]
|
|
|
|
|
extern "C" {}
|