Skip to content

Fix LLD being mistaken for LD - #166273

Open
LuigiPiucco wants to merge 1 commit into
rust-lang:mainfrom
LuigiPiucco:ld-lld-extension-fix
Open

LuigiPiucco wants to merge 1 commit into
rust-lang:mainfrom
LuigiPiucco:ld-lld-extension-fix

Conversation

@LuigiPiucco

Copy link
Copy Markdown
Contributor

When the user sets just the linker to be used, we have to infer the LinkerFlavor from that. Because we do so from the result of .file_stem() on the linker path, if the file name is ld.lld, the .lld will be striped as if it were a file extension, and we get just ld being passed for inference. This fails to detect that the linker is LLD, causing certain conditionals that guard linker flags act wrongly (this was discovered while testing with AVR, which requires the emulation to be passed for GNU LD but breaks if passed for LLD).

When the user sets just the linker to be used, we have to infer the
LinkerFlavor from that. Because we do so from the result of
.file_stem() on the linker path, if the file name is ld.lld, the .lld
will be striped as if it were a file extension, and we get just ld
being passed for inference. This fails to detect that the linker is
LLD, causing certain conditionals that guard linker flags act wrongly
(this was discovered while testing with AVR, which requires the
emulation to be passed for GNU ld but breaks if passed for LLD).
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 20, 2026
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: codegen, compiler
  • codegen, compiler expanded to 79 candidates
  • Random selection from 21 candidates

@jackh726

Copy link
Copy Markdown
Member

This needs a test at minimum. But also generally this feels wrong to just silently change this.

@LuigiPiucco

Copy link
Copy Markdown
Contributor Author

What I observed that made me write this PR is that, when the user requests a specific linker to be used for a target (I was testing with the .cargo/config.toml key target.avr-none.linker), if the passed linker is "ld.lld", the code prior to this PR thinks it's the GNU version "ld". My use-case was to test out lld instead of avr-gcc for AVR. avr-gcc requires the exact microcontroller to be specified, with a flag -mmcu=<cpu> during link-time. The code here, which implements this in Rust, guards this with !self.uses_lld so the flag isn't passed for LLD, because only avr-gcc recognizes it. I noticed it was being passed regardless (LLD exits early due to unknown emulation arguments, and so the Rust compilation fails), for the linker="ld.lld" case. Using println! statements and recompiling rustc, I saw that self.uses_lld was indeed false, which it should be true for "ld.lld". Tracking it further, I observed by placing a println! before this line that the linker there was the string "ld.lld", but the stem there that was passed to infer the linker flavor was actually just "ld". This causes the flavor to be inferred as LinkerFlavor::Gnu(Cc::No, Lld::No), when it should be LinkerFlavor::Gnu(Cc::No, Lld::Yes) in this case. This is a problem at least for AVR, because the flags for ld.lld are incompatible with those for avr-gcc. Inferring the wrong flavor may not have observable side-effects for other architectures (at least, I'm not aware of any other where the flags for one linker are incompatible with the other like this), but it still seems incorrect to me.

One additional thing I learned after opening the PR is that if linker="lld" (the generic driver, not the Linux-flavored one), this is not an issue, and I'm able to link with LLD. It affects only "ld.lld", since the file_stem operation that gets run removes the .lld because it looks like a file extension.

Reproduction steps for "ld.lld" becoming just "ld"
  1. Compile a local checkout of Rust without my PR, with the following line added just before this code;
println!("linker: {} != stem: {}", linker.display(), stem);
  1. Try to compile an executable with the built compiler. For convenience, I uploaded an example AVR project in https://github.com/LuigiPiucco/ld-lld-test that can help reproduction. It has the relevant Cargo config and some stuff needed to target AVR. The compilation should fail because LLD doesn't recognize the mcu=atmega2560 emulation. A bit before that it should have printed the line linker: ld.lld != stem: ld.

If there is further procedure I need to follow, more context I need to provide, or if there's a different course of action for this PR, I'm open to suggestions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants