rust-lang / wg-cargo-std-aware

Repo for working on "std aware cargo"
133 stars 8 forks source link

Linker can't find core::panicking::panic when lto is turned on #62

Closed hnj2 closed 3 years ago

hnj2 commented 3 years ago

With -Z build-std and lto = true, the linker can't link to core::panicking::panic.

The Issue might have to do with the fact that neither the binutils-arm-none-eabi toolchain nor llvm-objdump can read the generated libcore-***.rlib file:

$ arm-none-eabi-objdump -Cd target/armv7a-none-eabi/debug/deps/libcore-03f9f7bf54691bce.rlib
In archive target/armv7a-none-eabi/debug/deps/libcore-03f9f7bf54691bce.rlib:
arm-none-eabi-objdump: core-03f9f7bf54691bce.core.r68z9s80-cgu.0.rcgu.o: File format not recognized
  ...

$ llvm-objdump -Cd target/armv7a-none-eabi/debug/deps/libcore-03f9f7bf54691bce.rlib
 <no output>

Steps to Repoduce

src/main.rs

#![no_std]
#![no_main]

#[no_mangle]
unsafe fn _start(v: f64, _b: bool) -> f64 {
    v / v   // uses __aeabi_ddiv
            // which uses compiler_builtins::float::div::__divdf3
            // which uses compiler_builtins::float::div::div64
            // which uses <u64 as core::ops::arith::Sub>::sub
            // which uses core::panicking::panic
}

#[panic_handler]
fn panic(_ : &core::panic::PanicInfo<'_>) -> ! {
    loop {}
}

Cargo.toml

[package]
name = "arm-quick-tests"
version = "0.1.0"
edition = "2018"

[profile.dev]
lto = true

With build-std

$ cargo build --target armv7a-none-eabi -Z build-std

  ...

   Compiling arm-quick-tests v0.1.0 (/home/hnj/projects/psp/arm-quick-tests)
error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "--eh-frame-hdr" "-L" "/home/hnj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7a-none-eabi/lib" "/home/hnj/projects/psp/arm-quick-tests/target/armv7a-none-eabi/debug/deps/arm_quick_tests-4b5a9387f1da73da.rustc_std_workspace_core-a7ae2bd4c0ad8c97.rustc_std_workspace_core.16ed7zji-cgu.0.rcgu.o.rcgu.o" "-o" "/home/hnj/projects/psp/arm-quick-tests/target/armv7a-none-eabi/debug/deps/arm_quick_tests-4b5a9387f1da73da" "--gc-sections" "-L" "/home/hnj/projects/psp/arm-quick-tests/target/armv7a-none-eabi/debug/deps" "-L" "/home/hnj/projects/psp/arm-quick-tests/target/debug/deps"
"-L" "/home/hnj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/armv7a-none-eabi/lib" "-Bstatic" "/home/hnj/projects/psp/arm-quick-tests/target/armv7a-none-eabi/debug/deps/libcompiler_builtins-14592acc24880b22.rlib" "-Bdynamic"
  = note: rust-lld: error: undefined symbol: core::panicking::panic::hd649c340f3d8c4c6
          >>> referenced by arith.rs:206 (/home/hnj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:206)
          >>>               compiler_builtins-14592acc24880b22.compiler_builtins.elyi3bsc-cgu.15.rcgu.o:(_$LT$u64$u20$as$u20$core..ops..arith..Sub$GT$::sub::h2cefb96ba13f6e84) in archive /home/hnj/projects/psp/arm-quick-tests/target/armv7a-none-eabi/debug/deps/libcompiler_builtins-14592acc24880b22.rlib
          >>> referenced by arith.rs:731 (/home/hnj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:731)
          >>>               compiler_builtins-14592acc24880b22.compiler_builtins.elyi3bsc-cgu.15.rcgu.o:(_$LT$i64$u20$as$u20$core..ops..arith..AddAssign$GT$::add_assign::h4eb6c8c224d7986a) in archive /home/hnj/projects/psp/arm-quick-tests/target/armv7a-none-eabi/debug/deps/libcompiler_builtins-14592acc24880b22.rlib
          >>> referenced by arith.rs:731 (/home/hnj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:731)
          >>>               compiler_builtins-14592acc24880b22.compiler_builtins.elyi3bsc-cgu.15.rcgu.o:(_$LT$u64$u20$as$u20$core..ops..arith..AddAssign$GT$::add_assign::he3030313ce7894e6) in archive /home/hnj/projects/psp/arm-quick-tests/target/armv7a-none-eabi/debug/deps/libcompiler_builtins-14592acc24880b22.rlib
          >>> referenced 11 more times

error: aborting due to previous error

error: could not compile `arm-quick-tests`

Without build-std:

$ cargo build --target armv7a-none-eabi
   Compiling arm-quick-tests v0.1.0 (/home/hnj/projects/psp/arm-quick-tests)
    Finished dev [unoptimized + debuginfo] target(s) in 0.18s

Without tlo = true:

$ cargo build --target armv7a-none-eabi -Z build-std

   ...

   Compiling arm-quick-tests v0.1.0 (/home/hnj/projects/psp/arm-quick-tests)
    Finished dev [unoptimized + debuginfo] target(s) in 27.63s

Version:

$ rustc --version
rustc 1.50.0-nightly (a0d664bae 2020-11-22)
hnj2 commented 3 years ago

Not sure if this is the right place or if rust-lang/rust is the place to put this issue?

Rahix commented 3 years ago

I think you're running into the same thing as https://github.com/rust-lang/compiler-builtins/issues/347?

Essentially, compiler-builtins expects to be compiled with overflow-checks=false and debug-assertions=false which cargo -Z build-std does not do ... You can add

[profile.dev.package.compiler_builtins]
overflow-checks = false

to your Cargo.toml which gives a warning but still does the trick ...

hnj2 commented 3 years ago

Thank you very much!

lexxvir commented 2 years ago

Hi there,

I've encountered into the same issue and after googling reach this issue. The trick with

[profile.dev.package.compiler_builtins]
overflow-checks = false

works for me, but I think it is a build-std's job to pass right flags to a rustc invocation.

Please correct me if I'm wrong.