Similar to the compiler, matching the linker used to compile the kernel is also important for an external kernel module build. Add code to detect the linker from the kernel config similar to the existing compiler detection. Speicifically, this fixes kernel module builds under kernels built with clang and with LTO enabled. Signed-off-by: Ondrej Mosnacek <omosnace@xxxxxxxxxx> --- tests/module_load/Makefile | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/module_load/Makefile b/tests/module_load/Makefile index 272872d..0839532 100644 --- a/tests/module_load/Makefile +++ b/tests/module_load/Makefile @@ -4,12 +4,15 @@ TARGETS = finit_load init_load LDLIBS += -lselinux KDIR = /lib/modules/$(shell uname -r)/build -# Make sure to use the same compiler as the kernel was built with. -# If the compilers don't match, the build will fail on unsupported compiler +# Make sure to use the same compiler+linker as the kernel was built with. +# If the compilers/linkers don't match, the build could fail on unsupported # flags and even if not, the resulting module would likely fail to load. -# If the kernel was compiled with neither GCC nor clang (currently the only -# supported compilers), fall back to the default compiler and hope for the best. -# In all cases allow the user to override the compiler via the KCC variable. +# If the kernel was compiled with a compiler other than GCC or clang or a +# linker other than ld.bfd or ld.lld, fall back to the default compiler/linker +# and hope for the best. +# In all cases allow the user to override the compiler via the KCC/KLD +# variables. + DETECTED_KCC = unknown ifeq ($(shell grep -qFx CONFIG_CC_IS_GCC=y $(KDIR)/.config && echo true),true) DETECTED_KCC = gcc @@ -23,9 +26,22 @@ else KCC ?= $(CC) endif +DETECTED_KLD = unknown +ifeq ($(shell grep -qFx CONFIG_LD_IS_BFD=y $(KDIR)/.config && echo true),true) + DETECTED_KLD = ld.bfd +endif +ifeq ($(shell grep -qFx CONFIG_LD_IS_LLD=y $(KDIR)/.config && echo true),true) + DETECTED_KLD = ld.lld +endif +ifneq ($(DETECTED_KLD),unknown) + KLD ?= $(DETECTED_KLD) +else + KLD ?= $(LD) +endif + all: $(TARGETS) - $(MAKE) -C $(KDIR) CC=$(KCC) M=$(PWD) + $(MAKE) -C $(KDIR) CC=$(KCC) LD=$(KLD) M=$(PWD) clean: rm -f $(TARGETS) - $(MAKE) -C $(KDIR) CC=$(KCC) M=$(PWD) clean + $(MAKE) -C $(KDIR) CC=$(KCC) LD=$(KLD) M=$(PWD) clean -- 2.36.1