Re: [PATCH 2/3] kbuild: enable objtool for *.mod.o and additional kernel objects

From: Masahiro Yamada
Date: Tue Nov 19 2024 - 12:28:33 EST


On Tue, Nov 19, 2024 at 11:27 AM Nathan Chancellor <nathan@xxxxxxxxxx> wrote:
>
> Hi Masahiro,
>
> On Thu, Nov 14, 2024 at 08:45:22AM +0900, Masahiro Yamada wrote:
> > Currently, objtool is disabled in scripts/Makefile.{modfinal,vmlinux}.
> >
> > This commit moves rule_cc_o_c and rule_as_o_S to scripts/Makefile.lib
> > and set objtool-enabled to y there.
> >
> > With this change, *.mod.o, .module-common.o, builtin-dtb.o, and
> > vmlinux.export.o will now be covered by objtool.
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@xxxxxxxxxx>
>
> I am seeing some build failures when LTO is enabled with this change in
> -next as commit d8d3f6c6690c ("kbuild: enable objtool for *.mod.o and
> additional kernel objects").
>
> $ printf 'CONFIG_LTO_%s\n' NONE=n CLANG_THIN=y >kernel/configs/thinlto.config
>
> $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 mrproper {def,thinlto.}config all
> ...
> .vmlinux.export.o: warning: objtool: gelf_getehdr: invalid `Elf' handle
> make[4]: *** [scripts/Makefile.vmlinux:13: .vmlinux.export.o] Error 1
> ...
>
> When LTO is enabled, these files are LLVM bitcode, not ELF, so objtool
> can't process them:
>
> $ file .vmlinux.export.o
> .vmlinux.export.o: LLVM IR bitcode

Good catch!

I will squash the attached diff.

Thank you.


--
Best Regards
Masahiro Yamada
From 667464814cdd7cd4f62699be2f7e5871557af1fe Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <masahiroy@kernel.org>
Date: Tue, 19 Nov 2024 17:29:42 +0900
Subject: [PATCH] fixup

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---
scripts/Makefile.build | 6 +-----
scripts/Makefile.lib | 9 ++++++++-
2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 2538a925e04c..41649b8a6631 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -129,11 +129,7 @@ $(obj)/%.ll: $(obj)/%.c FORCE

is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)

-# When a module consists of a single object, there is no reason to keep LLVM IR.
-# Make $(LD) covert LLVM IR to ELF here.
-ifdef CONFIG_LTO_CLANG
-cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
-endif
+need-ld-single = $(is-single-obj-m)

ifdef CONFIG_MODVERSIONS
# When module versioning is enabled the following steps are executed:
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index ad6b8dfa5530..7ac65414699c 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -303,10 +303,17 @@ endef
# These are shared by some Makefile.* files.

objtool-enabled := y
+need-ld-single := y
+
+# When a module consists of a single object, there is no reason to keep LLVM IR.
+# Make $(LD) covert LLVM IR to ELF here.
+ifdef CONFIG_LTO_CLANG
+cmd_ld_single = $(if $(need-ld-single), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
+endif

quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
- $(cmd_ld_single_m) \
+ $(cmd_ld_single) \
$(cmd_objtool)

define rule_cc_o_c
--
2.43.0