Rust for Linux List
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb+git@google.com>
To: linux-kbuild@vger.kernel.org
Cc: rust-for-linux@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	 Gary Guo <gary@garyguo.net>, Boqun Feng <boqun.feng@gmail.com>,
	 Matthew Maurer <mmaurer@google.com>,
	Alice Ryhl <aliceryhl@google.com>,
	 Nathan Chancellor <nathan@kernel.org>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	 Miguel Ojeda <ojeda@kernel.org>
Subject: [PATCH] kbuild: rust: Pass $(modkern_cflags) when compiling inline-helper bitcode
Date: Tue, 24 Nov 2026 08:41:39 +0100	[thread overview]
Message-ID: <20261124074138.450793-2-ardb+git@google.com> (raw)

From: Ard Biesheuvel <ardb@kernel.org>

When CONFIG_RUST_INLINE_HELPERS=y, rustc emits LLVM bitcode instead of
an object file, llvm-link merges the C helpers bitcode, and Clang
compiles the combined bitcode into the final object file.

While rustc receives $(modkern_rustflags) via $(rust_flags) and the
helper bitcode files are compiled with $(modkern_cflags) via $(c_flags),
the Clang invocation that performs final code generation in both
cmd_rustc_library and cmd_rustc_o_rs only passes $(KBUILD_CFLAGS) and
omits $(modkern_cflags).

As a result, target-specific built-in and module codegen flags in
KBUILD_CFLAGS_KERNEL / CFLAGS_KERNEL and KBUILD_CFLAGS_MODULE /
CFLAGS_MODULE (such as -ffunction-sections and -fdata-sections under
CONFIG_LD_DEAD_CODE_DATA_ELIMINATION, or architecture-specific code
model and relocation flags that override defaults in KBUILD_CFLAGS) are
lost during final code generation.

Pass $(modkern_cflags) alongside $(KBUILD_CFLAGS) when compiling the
merged bitcode in both cmd_rustc_library and cmd_rustc_o_rs.

Fixes: 3a2486cc1da5 ("kbuild: rust: provide an option to inline C helpers into Rust")
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
Cc: Gary Guo <gary@garyguo.net>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Matthew Maurer <mmaurer@google.com>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Andreas Hindborg <a.hindborg@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>

 rust/Makefile          | 2 +-
 scripts/Makefile.build | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/rust/Makefile b/rust/Makefile
index 0ac9ab28cc2b..49b139639339 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -657,7 +657,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
 		-Zunstable-options \
 	$(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
 		$(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
-		$(CC) $(CLANG_FLAGS) $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS)) \
+		$(CC) $(CLANG_FLAGS) $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS) $(modkern_cflags)) \
 		$(CC_FLAGS_RUST_INLINE_HELPERS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \
 		$(cmd_ld_single)) \
 	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 6127feb970c6..2e03e960387f 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -368,7 +368,7 @@ quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
       cmd_rustc_o_rs = $(rust_common_cmd) --emit=$(if $(CONFIG_RUST_INLINE_HELPERS),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) $< \
 	$(if $(CONFIG_RUST_INLINE_HELPERS),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
 		$(objtree)/rust/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
-		$(CC) $(CLANG_FLAGS) $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS)) \
+		$(CC) $(CLANG_FLAGS) $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS) $(modkern_cflags)) \
 		$(CC_FLAGS_RUST_INLINE_HELPERS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \
 		$(cmd_ld_single)) \
 	$(cmd_objtool)
-- 
2.56.1.831.g720f3f5937-goog


                 reply	other threads:[~2026-11-24  7:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124074138.450793-2-ardb+git@google.com \
    --to=ardb+git@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=ardb@kernel.org \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=mmaurer@google.com \
    --cc=nathan@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox