Skip to contentSkip to conversation

kbuild: rust: Pass $(modkern_cflags) when compiling inline-helper bitcode

PATCH In review Ard Biesheuvel 2 participants 2 messages Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: rust: Pass $(modkern_cflags) when compiling inline-helper bitcode

From: Ard Biesheuvel <[email protected]>

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")

---
Cc: Gary Guo <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Matthew Maurer <[email protected]>
Cc: Alice Ryhl <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Andreas Hindborg <[email protected]>
Cc: Miguel Ojeda <[email protected]>

 rust/Makefile          | 2 +-
 scripts/Makefile.build | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
rust/Makefile +1 -1
index 0ac9ab28cc2b..49b139639339 100644
@@ -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) $@) \
scripts/Makefile.build +1 -1
index 6127feb970c6..2e03e960387f 100644
@@ -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)
Signature · 1 line
2.56.1.831.g720f3f5937-goog
reviewer Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 5 lines · Ard Biesheuvel

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")

Thanks. I didn't run into this because it seems like on arm64 and x86_64 we don't use any options where this matters, but it would affect us if we had riscv builds.

Alice