Skip to content

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

@xobs

Description

@xobs

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-atomicArea: Atomics, barriers, and sync primitivesA-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-embeddedWorking group: Embedded systems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions