Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

riscv: unable to use atomics with linker-plugin-lto #69689

Open
xobs opened this issue Mar 4, 2020 · 2 comments
Open

riscv: unable to use atomics with linker-plugin-lto #69689

xobs opened this issue Mar 4, 2020 · 2 comments
Labels
A-atomic Area: atomics, barriers, and sync primitives A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-embedded Working group: Embedded systems

Comments

@xobs
Copy link
Contributor

xobs commented Mar 4, 2020

The following code compiles fine without linker-plugin-lto, but fails to compile when it is enabled:

#![no_std]
#![no_main]

use core::sync::atomic::{AtomicUsize, Ordering};
static MM_HANDLE_COUNT: AtomicUsize = AtomicUsize::new(0);

#[no_mangle]
pub extern "C" fn _start() -> ! {
    let _count = MM_HANDLE_COUNT.fetch_add(1, Ordering::SeqCst);
    loop {}
}

use core::panic::PanicInfo;
#[panic_handler]
fn handle_panic(_arg: &PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn abort() -> ! {
    loop {}
}

This code should compile cleanly with:

$ rustc +nightly main.rs --target riscv64gc-unknown-none-elf
$

However, with the lto plugin, it doesn't compile:

$ rustc +nightly -C linker-plugin-lto main.rs --target riscv64gc-unknown-none-elf error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "-plugin-opt=O0" "-plugin-opt=mcpu=generic-rv64" "-L" "/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/riscv64gc-unknown-none-elf/lib" "main.main.7rcbfp3g-cgu.0.rcgu.o" "main.main.7rcbfp3g-cgu.1.rcgu.o" "main.main.7rcbfp3g-cgu.2.rcgu.o" "-o" "main" "--gc-sections" "-L" "/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/riscv64gc-unknown-none-elf/lib" "-Bstatic" "/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/riscv64gc-unknown-none-elf/lib/librustc_std_workspace_core-a43e904133f39ebe.rlib" "/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/riscv64gc-unknown-none-elf/lib/libcore-de86a0d6e9117873.rlib" "/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/riscv64gc-unknown-none-elf/lib/libcompiler_builtins-980292810b049237.rlib" "-Bdynamic"
  = note: rust-lld: error: undefined symbol: __atomic_fetch_add_8
          >>> referenced by main.7rcbfp3g-cgu.2
          >>>               lto.tmp:(core::sync::atomic::AtomicUsize::fetch_add::h6aa949cc8cd90341)


error: aborting due to previous error
$

Meta

rustc --version --verbose:

rustc 1.43.0-nightly (4ad624882 2020-03-03)
binary: rustc
commit-hash: 4ad62488258972bdb0e2df225d100f99ef58dbad
commit-date: 2020-03-03
host: x86_64-unknown-linux-gnu
release: 1.43.0-nightly
LLVM version: 9.0

This occurs on both riscv64gc-unknown-none-elf and riscv32imac-unknown-none-elf.

@xobs xobs added the C-bug Category: This is a bug. label Mar 4, 2020
@xobs
Copy link
Contributor Author

xobs commented Mar 4, 2020

This may be a bug in rust-lld caused by it not passing (or supporting passing?) the "features" flags to the linker: rust-lang/compiler-builtins#346 (comment)

@xobs
Copy link
Contributor Author

xobs commented Mar 4, 2020

Note that passing "-plugin-opt=--mattr=+a" seems to fix the issue. It seems like compiling with -C linker-plugin-lto should also pass the target's features to lld, via -plugin-opt=--mattr=.

@jonas-schievink jonas-schievink added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-embedded Working group: Embedded systems labels Mar 4, 2020
@workingjubilee workingjubilee added the A-atomic Area: atomics, barriers, and sync primitives label Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-atomic Area: atomics, barriers, and sync primitives A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-embedded Working group: Embedded systems
Projects
None yet
Development

No branches or pull requests

3 participants