Skip to content

Conversation

@alban-moizan-smile
Copy link

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
Copy link
Collaborator

rustbot commented Jan 15, 2026

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
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.

Copy link
Member

@bjorn3 bjorn3 Jan 15, 2026

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.

@bjorn3
Copy link
Member

bjorn3 commented Jan 15, 2026

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.

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.

4 participants