Skip to content

rustc_codegen_ssa: fix pre_link_args order - #151165

Open
alban-moizan-smile wants to merge 1 commit into
rust-lang:mainfrom
alban-moizan-smile:fix-link-arg-ordering-kernel-issue
Open

alban-moizan-smile wants to merge 1 commit into
rust-lang:mainfrom
alban-moizan-smile:fix-link-arg-ordering-kernel-issue

Conversation

@alban-moizan-smile

Copy link
Copy Markdown

Hi,

We had an issue while compiling the linux kernel with a composite linker command in Yocto when activating the rust parts of the kernel.

Kernel Makefiles run rustc with -Clinker=$(HOSTCC) (scripts/Makefile.host, rust/Makefile)

But, HOSTCC can be something like <my-wrapper-tool> <my-cc-as-arg> (in our yocto case ccache gcc), which is incompatible with the way rustc handles -Clinker, it is handled as a PathBuf, used directly as a Command name, but the Command constructor doesn't accept arguments.

We could split HOSTCC to START_HOSTCC and END_HOSTCC to do something like -Clinker=$(START_HOSTCC) -Zpre-link-args='$(END_HOSTCC)', but a rustc limitation occurs.

Indeed, some linker arguments given by target files (ex: -m64 from a target file) and from the function export_symbols (ex: --no-undefined-version here) where added to the command before the pre-link-args.

Therefore, we could end up with a generated call like <my-wrapper-tool> -m64 <my-cc-as-arg> ... (in our case ccache -m64 gcc ...), which is broken.

This PR mitigates this issue sorting pre-link-args calls before target files pre-link-args, and before the call to export_symbols, which allow to use any wrapper tool around the actual linker.

Please make me know if I should rewrite it in another way (a new argument parameter ? directly split the PathBuf into arguments ?).

I could write some tests if needed, but I'm not sure about the best way to test it in the repository.

Thanks for reading, regards,

Alban

@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 Jan 15, 2026
@rustbot

rustbot commented Jan 15, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
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

// FIXME: In practice built-in target specs use this for arbitrary order-independent options,
// introduce a target spec option for order-independent linker options and migrate built-in
// specs to it.
add_pre_link_args(cmd, sess, flavor);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd.export_symbols() has to be before any object files on some targets.

@bjorn3 bjorn3 Jan 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if you really need it before any arguments, this is still too late. get_linker already adds some args in some cases. In particular for Windows UWP it passes /LIBPATH (though there is a FIXME to move it out of get_linker.

@alban-moizan-smile alban-moizan-smile Jan 16, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I understand, so, should I implement a new parameter to rustc named -Cbefore-link-args instead, put the result as the really first args, and patch the FIXME for get_linker ?

@bjorn3

bjorn3 commented Jan 15, 2026

Copy link
Copy Markdown
Member

But, HOSTCC can be something like (in our yocto case ccache gcc)

I believe sccache would auto-detect the tool to use and fallback to cc if it fails to detect. Looks like ccache doesn't do that. However you can create a symlink named gcc with ccache as target. So that may be an alternative to this PR.

@alban-moizan-smile

alban-moizan-smile commented Jan 16, 2026 •

Copy link
Copy Markdown
Author

But, HOSTCC can be something like (in our yocto case ccache gcc)

I believe sccache would auto-detect the tool to use and fallback to cc if it fails to detect. Looks like ccache doesn't do that. However you can create a symlink named gcc with ccache as target. So that may be an alternative to this PR.

Indeed, creating a symlink, or a single executable fake linker running literally ccache gcc before would work, but it becomes less clear what is actually running here, and it only works with ccache or sccache, if you want to do more esoteric things like valgrind gcc, it's broken.

As I proposed in your review, creating a new param like -Cbefore-link-args (or named diffrently) could mitigate the issue while not changing the current ordering.

Or either, just splitting the -Clinker into a Vec and putting the first value in the Command::new and others in args ?

@nnethercote

Copy link
Copy Markdown
Contributor

r? @bjorn3

@rustbot rustbot assigned bjorn3 and unassigned nnethercote Jan 19, 2026
@bjorn3

bjorn3 commented Feb 4, 2026

Copy link
Copy Markdown
Member

I don't have time for reviewing this.

@rustbot reroll

@mati865

mati865 commented Feb 13, 2026

Copy link
Copy Markdown
Member

I won't have time for it either.

@rustbot reroll

@rustbot rustbot assigned BoxyUwU and unassigned mati865 Feb 13, 2026
@BoxyUwU

BoxyUwU commented Feb 14, 2026

Copy link
Copy Markdown
Member

r? compiler

@rustbot rustbot assigned eholk and unassigned BoxyUwU Feb 14, 2026
@eholk

eholk commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

Based on https://forge.rust-lang.org/compiler/proposals-and-stabilization.html#compiler-flags, it sounds like the next step here would be to rework this as a -Z option and then we can merge this PR. If using nightly is okay for you, this should be enough. Otherwise, I'd suggest opening an MCP in parallel to get this promoted to a -C option.

@wesleywiser

Copy link
Copy Markdown
Member

I think this is waiting on the author to resolve the review comments in this thread: #151165 (comment)

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 16, 2026
@rustbot

rustbot commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@JohnCSimon

Copy link
Copy Markdown

@alban-moizan-smile

Ping from triage: - can you post your status on this PR?
I'll be closing because of inactivity otherwise. Thanks

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-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

9 participants