Re: [PATCH RFC] kbuild: Prevent compiler mismatch with external modules

From: Josh Poimboeuf
Date: Mon Feb 01 2021 - 16:15:20 EST


On Fri, Jan 29, 2021 at 08:17:51AM +0900, Masahiro Yamada wrote:
> [3]
> Peterz already pointed out asm-goto as an example of ABI mismatch.
>
> I remember a trouble reported in the past due
> to the mismatch of -mstack-protector-guard-offset.
>
> https://bugzilla.kernel.org/show_bug.cgi?id=201891
>
> This has already been fixed,
> and it will no longer happen though.

This is kind of concerning though. It would be nice to somehow store
KCLAGS in the config and warn if it changes unexpectedly.

This can be a problem not only for OOT modules, but for regular kernel
builds which have a .config copied from somewhere.

Because of the toolchain-dependent kconfig options, features can
silently disappear if the toolchain doesn't support them, due to a
different compiler version, or even a missing library.

> [2]
>
> As for this patch, it is wrong to do this check in the Makefile
> parse stage.
>
> "make M=... clean"
> "make M=... help"
>
> etc. will fail.
> Such targets do not require the compiler in the first place.
>
> This check must be done before starting building something,
>
> Also, this patch is not applicable.
> gcc-version.sh and clang-version.sh do not exist.
> See linux-next.

Something like so?

diff --git a/Makefile b/Makefile
index 95ab9856f357..10ca621369fb 100644
--- a/Makefile
+++ b/Makefile
@@ -1721,12 +1721,25 @@ KBUILD_MODULES := 1

build-dirs := $(KBUILD_EXTMOD)
PHONY += modules
-modules: $(MODORDER)
+modules: ext_compiler_check $(MODORDER)
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost

$(MODORDER): descend
@:

+orig_name := $(if $(CONFIG_CC_IS_GCC),GCC,CLANG)
+orig_minor := $(shell expr $(if $(CONFIG_CC_IS_GCC),$(CONFIG_GCC_VERSION),$(CONFIG_CLANG_VERSION)) / 100)
+cur_namever := $(shell $(srctree)/scripts/cc-version.sh $(CC))
+cur_name := $(word 1,$(cur_namever))
+cur_minor := $(shell expr $(word 2,$(cur_namever)) / 100)
+PHONY += ext_compiler_check
+ext_compiler_check:
+ @if [ $(orig_name) != $(cur_name) ] || [ $(orig_minor) != $(cur_minor) ]; then \
+ echo >&2 "warning: The compiler differs from the version which was used to build the kernel."; \
+ echo >&2 "warning: Some kernel features are compiler-dependent."; \
+ echo >&2 "warning: It's recommended that you change your compiler to match the version in the .config file."; \
+ fi
+
PHONY += modules_install
modules_install: _emodinst_ _emodinst_post