Skip to contentSkip to conversation

tests/module_load: detect the linker to use for module build

PATCH In review Ondrej Mosnacek 1 participant 2 messages Jul 18, 2022 – Aug 1, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/module_load: detect the linker to use for module build

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.

---
 tests/module_load/Makefile | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
tests/module_load/Makefile +23 -7
index 272872d..0839532 100644
@@ -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
Signature · 1 line
2.36.1
#1 lore raw
Quoted 64 lines · Ondrej Mosnacek

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.

---
 tests/module_load/Makefile | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
tests/module_load/Makefile +23 -8
index 272872d..0839532 100644
@@ -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
--
Quoted 1 line

2.36.1

Whoops, forgot about this patch... Now applied as:
https://github.com/SELinuxProject/selinux-testsuite/commit/deeff9d73d9abf84fbb76a019186de06004e121e

Signature · 3 lines
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.