Linux kbuild/kconfig development
 help / color / mirror / Atom feed
* [PATCH] kbuild: rust: Pass $(modkern_cflags) when compiling inline-helper bitcode
@ 2026-11-24  7:41 Ard Biesheuvel
  0 siblings, 0 replies; only message in thread
From: Ard Biesheuvel @ 2026-11-24  7:41 UTC (permalink / raw)
  To: linux-kbuild
  Cc: rust-for-linux, Ard Biesheuvel, Gary Guo, Boqun Feng,
	Matthew Maurer, Alice Ryhl, Nathan Chancellor, Andreas Hindborg,
	Miguel Ojeda

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-24  7:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  7:41 [PATCH] kbuild: rust: Pass $(modkern_cflags) when compiling inline-helper bitcode Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox