diff mbox series

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

Message ID 20220718105120.674121-1-omosnace@redhat.com (mailing list archive)
State Accepted
Delegated to: Ondrej Mosnáček
Headers show
Series [testsuite] tests/module_load: detect the linker to use for module build | expand

Commit Message

Ondrej Mosnacek July 18, 2022, 10:51 a.m. UTC
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@redhat.com>
---
 tests/module_load/Makefile | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

Comments

Ondrej Mosnacek Aug. 1, 2022, 9:06 a.m. UTC | #1
On Mon, Jul 18, 2022 at 12:51 PM Ondrej Mosnacek <omosnace@redhat.com> wrote:
> 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@redhat.com>
> ---
>  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

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

Patch

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