|
1 | | -/// Hook into .init_array to enable LSE atomic operations at startup, if |
2 | | -/// supported. |
3 | | -#[cfg(all(target_arch = "aarch64", target_os = "linux", not(feature = "compiler-builtins-c")))] |
| 1 | +/// Enable LSE atomic operations at startup, if supported. |
| 2 | +/// |
| 3 | +/// Linker sections are based on what [`ctor`] does, with priorities to run slightly before user |
| 4 | +/// code: |
| 5 | +/// |
| 6 | +/// - Apple uses the section `__mod_init_func`, `mod_init_funcs` is needed to set |
| 7 | +/// `S_MOD_INIT_FUNC_POINTERS`. There doesn't seem to be a way to indicate priorities. |
| 8 | +/// - Windows uses `.CRT$XCT`, which is run before user constructors (these should use `.CRT$XCU`). |
| 9 | +/// - ELF uses `.init_array` with a priority of 90, which runs before our `ARGV_INIT_ARRAY` |
| 10 | +/// initializer (priority 99). Both are within the 0-100 implementation-reserved range, per docs |
| 11 | +/// for the [`prio-ctor-dtor`] warning, and this matches compiler-rt's `CONSTRUCTOR_PRIORITY`. |
| 12 | +/// |
| 13 | +/// To save startup time, the initializer is only run if outline atomic routines from |
| 14 | +/// compiler-builtins may be used. If LSE is known to be available then the calls are never |
| 15 | +/// emitted, and if we build the C intrinsics then it has its own initializer using the symbol |
| 16 | +/// `__aarch64_have_lse_atomics`. |
| 17 | +/// |
| 18 | +/// Initialization is done in a global constructor to so we get the same behavior regardless of |
| 19 | +/// whether Rust's `init` is used, or if we are in a `dylib` or `no_main` situation (as opposed |
| 20 | +/// to doing it as part of pre-main startup). This also matches C implementations. |
| 21 | +/// |
| 22 | +/// Ideally `core` would have something similar, but detecting the CPU features requires the |
| 23 | +/// auxiliary vector from the OS. We do the initialization in `std` rather than as part of |
| 24 | +/// `compiler-builtins` because a builtins->std dependency isn't possible, and inlining parts of |
| 25 | +/// `std-detect` would be much messier. |
| 26 | +/// |
| 27 | +/// [`ctor`]: https://github.com/mmastrac/rust-ctor/blob/63382b833ddcbfb8b064f4e86bfa1ed4026ff356/shared/src/macros/mod.rs#L522-L534 |
| 28 | +/// [`prio-ctor-dtor`]: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html |
| 29 | +#[cfg(all( |
| 30 | + target_arch = "aarch64", |
| 31 | + target_feature = "outline-atomics", |
| 32 | + not(target_feature = "lse"), |
| 33 | + not(feature = "compiler-builtins-c"), |
| 34 | +))] |
4 | 35 | #[used] |
5 | | -#[unsafe(link_section = ".init_array.90")] |
| 36 | +#[cfg_attr(target_vendor = "apple", unsafe(link_section = "__DATA,__mod_init_func,mod_init_funcs"))] |
| 37 | +#[cfg_attr(target_os = "windows", unsafe(link_section = ".CRT$XCT"))] |
| 38 | +#[cfg_attr( |
| 39 | + not(any(target_vendor = "apple", target_os = "windows")), |
| 40 | + unsafe(link_section = ".init_array.90") |
| 41 | +)] |
6 | 42 | static RUST_LSE_INIT: extern "C" fn() = { |
7 | 43 | extern "C" fn init_lse() { |
8 | 44 | use crate::arch; |
9 | 45 |
|
10 | | - // This is provided by compiler-builtins::aarch64_linux. |
| 46 | + // This is provided by compiler-builtins::aarch64_outline_atomics. |
11 | 47 | unsafe extern "C" { |
12 | 48 | fn __rust_enable_lse(); |
13 | 49 | } |
|
0 commit comments