Amanieu / minicov

Code coverage support for Rust `no_std` and embedded programs
Apache License 2.0
51 stars 11 forks source link

Can't link minicov for thumbv7em-none-eabihf target #4

Closed xd009642 closed 2 years ago

xd009642 commented 2 years ago

I'm sure this is probably also the case for other similar targets but I've only tried for this target hence the issue. When attempting to build minicov for an embedded target with no OS I encounter linker errors. I'll put the full printout from the bottom for the symbols it can't find are:

I see both of these are implemented in the rust code so I wasn't quite sure what was going on there. However, I removed those implementations and moved them into the C code (https://github.com/xd009642/minicov/tree/working-on-arm) and now it builds fine without linker errors. Kinda curious what's happening here because as far as I can see both ways of doing it should lead to the same symbols added to the binary :thinking:

RUSTFLAGS="-Zinstrument-coverage -Zno-profiler-runtime" cargo +nightly build
    Updating crates.io index
   Compiling minicov v0.2.1
   Compiling llvm-embedded-coverage v0.1.0 (/llvm-embedded-coverage)
error: linking with `rust-lld` failed: exit status: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/llvm_embedded_coverage-6fb101017c34d64d.6uov4bbsuur4eff.rcgu.o" "--as-needed" "-L" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps" "-L" "/llvm-embedded-coverage/target/debug/deps" "-L" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/build/cortex-m-78743fa8465f0c61/out" "-L" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/build/cortex-m-rt-f1a3d6a8ddc5ca0e/out" "-L" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/build/cortex-m-semihosting-d622abfdb4beb985/out" "-L" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/build/defmt-33dc332803fe4b0f/out" "-L" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/build/minicov-68a47f113b7f772b/out" "-L" "/home/daniel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib" "-Bstatic" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libminicov-9ae9b9386bab6f94.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libcfg_if-ed9af3ef0fb0500d.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/librtt_target-c9177c23c05e5d21.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libcortex_m-d962a26cbcfaf3a6.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libembedded_hal-5a64baad222ea878.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libvoid-e245d597db37bba7.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libnb-2153bd107d507d8d.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libnb-0f2968f351785e3b.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libvolatile_register-a2a6ec7e983eb9bb.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libvcell-861062d5242e30aa.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libbare_metal-5e3367dd91bb6c30.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libufmt_write-d785522930dc655c.rlib" "--start-group" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libpanic_halt-a90c36ef06bbfc94.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libcortex_m_rt-549ad210761229c1.rlib" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libr0-f9908adae0ed5eba.rlib" "/home/daniel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib/librustc_std_workspace_core-9ae03c706dab5b95.rlib" "/home/daniel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib/libcore-4c81109e506dada5.rlib" "--end-group" "/home/daniel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib/libcompiler_builtins-aef8f029b4258cbf.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "/home/daniel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib" "-o" "/llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/llvm_embedded_coverage-6fb101017c34d64d" "--gc-sections" "-u" "__llvm_profile_runtime"
  = note: rust-lld: error: undefined symbol: __llvm_profile_register_function.1
          >>> referenced by minicov.b5b36f93-cgu.1
          >>>               minicov-9ae9b9386bab6f94.minicov.b5b36f93-cgu.1.rcgu.o:(__llvm_profile_init) in archive /llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libminicov-9ae9b9386bab6f94.rlib
          >>> referenced by minicov.b5b36f93-cgu.1
          >>>               minicov-9ae9b9386bab6f94.minicov.b5b36f93-cgu.1.rcgu.o:(__llvm_profile_init) in archive /llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libminicov-9ae9b9386bab6f94.rlib
          >>> referenced by minicov.b5b36f93-cgu.1
          >>>               minicov-9ae9b9386bab6f94.minicov.b5b36f93-cgu.1.rcgu.o:(__llvm_profile_init) in archive /llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libminicov-9ae9b9386bab6f94.rlib
          >>> referenced 7 more times

          rust-lld: error: undefined symbol: __llvm_profile_register_names_function.2
          >>> referenced by minicov.b5b36f93-cgu.1
          >>>               minicov-9ae9b9386bab6f94.minicov.b5b36f93-cgu.1.rcgu.o:(__llvm_profile_init) in archive /llvm-embedded-coverage/target/thumbv7em-none-eabihf/debug/deps/libminicov-9ae9b9386bab6f94.rlib

error: could not compile `llvm-embedded-coverage` due to previous error
Amanieu commented 2 years ago

The problem is that __llvm_profile_register_function already exists in the codegen unit when LLVM adds a new one as an external reference. Since there is a name conflict LLVM adds a .1, .2 to the name. LLVM is getting a bit confused because it expects that function to be in an external runtime library, not in the same code it's compiling right now.

xd009642 commented 2 years ago

Ah that makes sense, thanks for the quick fix :+1: