Description
Building a simple fuzz target using --cfg fuzzing -Cpasses=sancov-module -Cllvm-args=-sanitizer-coverage-level=4 -Cllvm-args=-sanitizer-coverage-inline-8bit-counters results in a linker error: {symbol} referenced in section {section} at {rcgu}: defined in discarded section {other section} of {other rcgu}.
One possible workaround is to set -Ccodegen-units=1 (any higher value triggers the error).
Alternatively, -Znew-llvm-pass-manager=no and -Cpasses=sancov (instead of -Cpasses=sancov-module) will also result in a successful build.
Code
Repro steps:
$ cat <<EOT > Cargo.toml
[package]
name = "fuzz-test"
version = "0.1.0"
edition = "2021"
[dependencies]
libfuzzer-sys = "0.4"
[[bin]]
name = "fuzz_test"
path = "fuzz_test.rs"
EOT
$ cat <<EOT >fuzz_test.rs
#![no_main]
use libfuzzer_sys::fuzz_target;
fuzz_target!(|_data: &[u8]| { });
EOT
$ cargo rustc --release -- \
--cfg fuzzing \
-Ccodegen-units=2 \
-Cpasses=sancov-module \
-Cllvm-args=-sanitizer-coverage-level=4 \
-Cllvm-args=-sanitizer-coverage-inline-8bit-counters \
-Clink-arg="-Wl,--no-gc-sections"
Meta
rustc --version --verbose
:
rustc 1.63.0-nightly (e71440575 2022-06-02)
binary: rustc
commit-hash: e71440575c930dcecac288b7c3536410d688b351
commit-date: 2022-06-02
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.4
Note: The same error also occurs with rustc 1.61.0 (stable channel).
ld --version
:
GNU ld (GNU Binutils for Debian) 2.35.2
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
Note: GNU ld is the default linker on this system. On another system where the linker is LLD, a similar error occurs, the message is just worded a bit differently.
Error output
error: linking with `cc` failed: exit status: 1
|
= note: "cc" "-m64" "/tmp/rustcburZ4c/symbols.o" "/home/inglorion/fuzz-test/target/release/deps/fuzz_test-81808fa57bf1cda3.fuzz_test.e322ed36-cgu.0.rcgu.o" "/home/inglorion/fuzz-test/target/release/deps/fuzz_"
= note: `.text.sancov.module_ctor_8bit_counters.19' referenced in section `.init_array.2[sancov.module_ctor_8bit_counters.19]' of /home/inglorion/fuzz-test/target/release/deps/fuzz_test-81808fa57bf1cda3.fuzz_o
collect2: error: ld returned 1 exit status
error: could not compile `fuzz-test` due to previous error