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

Linking with clang-XX fails #106454

Closed
jschwe opened this issue Jan 4, 2023 · 0 comments · Fixed by #106489
Closed

Linking with clang-XX fails #106454

jschwe opened this issue Jan 4, 2023 · 0 comments · Fixed by #106489
Labels
C-bug Category: This is a bug.

Comments

@jschwe
Copy link
Contributor

jschwe commented Jan 4, 2023

I tried this code:

main.rs:

#![no_std]
#![no_main]
use core::panic::PanicInfo;

fn main() {
    loop {}
}

#[panic_handler]
pub fn lib_panic(_: &PanicInfo) -> ! {
    loop {}
}

compilation without specifying a linker works (uses rust-lld):

cargo +nightly rustc --target=aarch64-unknown-none -- -Clink-arg=-nostdlib
Finished dev [unoptimized + debuginfo] target(s) in 0.08s

compilation while specifying clang as a linker-driver works:

CARGO_TARGET_AARCH64_UNKNOWN_NONE_LINKER=/usr/bin/clang cargo +nightly rustc --target=aarch64-unknown-none -- -Clink-arg=--target=aarch64-linux-gnu -Clink-arg=-fuse-ld=lld -Clink-arg=-nostdlib
    Finished dev [unoptimized + debuginfo] target(s) in 0.12s

linking with clang-13 (or any other clang-xx version) fails:

$ CARGO_TARGET_AARCH64_UNKNOWN_NONE_LINKER=/usr/bin/clang-13 cargo +nightly rustc --target=aarch64-unknown-none -- -Clink-arg=--target=aarch64-linux-gnu -Clink-arg=-fuse-ld=lld -Clink-arg=-nostdlib

error: linking with `/usr/bin/clang-13` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/jschwender/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/jschwender/.local/bin:/home/jschwender/.vscode-server/bin/e8a3071ea4344d9d48ef8a4df2c097372b0c5161/bin/remote-cli:/home/jschwender/.local/bin:/home/jschwender/.local/bin:/home/jschwender/.local/bin:/home/jschwender/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/jschwender/.dotnet/tools" VSLANG="1033" "/usr/bin/clang-13" "-flavor" "gnu" "/tmp/rustc2MlvQb/symbols.o" "/home/jschwender/xxx/test_linker/target/aarch64-unknown-none/debug/deps/test_linker-d064d105ad8c7d65.364uq591xu04qgaj.rcgu.o" "--as-needed" "-L" "/home/jschwender/xxx/test_linker/target/aarch64-unknown-none/debug/deps" "-L" "/home/jschwender/xxx/test_linker/target/debug/deps" "-L" "/home/jschwender/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-none/lib" "-Bstatic" "/home/jschwender/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-none/lib/librustc_std_workspace_core-f455852cb746dc8f.rlib" "/home/jschwender/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-none/lib/libcore-ca53c4fff67da7f4.rlib" "/home/jschwender/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-bf82cb7204be09eb.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "/home/jschwender/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-none/lib" "-o" "/home/jschwender/xxx/test_linker/target/aarch64-unknown-none/debug/deps/test_linker-d064d105ad8c7d65" "--gc-sections" "--target=aarch64-linux-gnu" "-fuse-ld=lld" "-nostdlib"
  = note: clang: error: unsupported option '--as-needed'
          clang: error: unsupported option '--eh-frame-hdr'
          clang: error: unsupported option '--gc-sections'
          clang: error: unknown argument: '-flavor'
          clang: error: unknown argument: '-znoexecstack'
          clang: error: no such file or directory: 'gnu'
          

warning: `test_linker` (bin "test_linker") generated 1 warning
error: could not compile `test_linker` due to previous error; 1 warning emitted

I expected to see this happen: If linking with clang works, linking with clang-13 or clang-14 etc. should also work.

Instead, this happened: Linking failed with clang-13. It looks like the detection of the linker failed and -Wl, was not added in some flags rust added.

Meta

Rust 1.65 and latest nigthly

rustc --version --verbose:

rustc 1.68.0-nightly (c7572670a 2023-01-03)
binary: rustc
commit-hash: c7572670a1302f5c7e245d069200e22da9df0316
commit-date: 2023-01-03
host: x86_64-unknown-linux-gnu
release: 1.68.0-nightly
LLVM version: 15.0.6
@jschwe jschwe added the C-bug Category: This is a bug. label Jan 4, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…rochenkov

Fix linker detection for linker (drivers) with a version postfix (e.g. clang-12 instead of clang)

Linker (drivers) such as clang / gcc or lld often have a version postfix matching the regex "-\d+$".
Previously, linker detection did not account for the possible version postfix and the fallback value was used, which can cause linker errors due to wrong arguments.
Also remove the check for `-clang`, since there are no architecture specific variants of clang (to my knowledge).

Fixes rust-lang#106454
@bors bors closed this as completed in f9dde54 Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
1 participant