selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH testsuite] tests/module_load: detect the linker to use for module build
@ 2022-07-18 10:51 Ondrej Mosnacek
  2022-08-01  9:06 ` Ondrej Mosnacek
  0 siblings, 1 reply; 2+ messages in thread
From: Ondrej Mosnacek @ 2022-07-18 10:51 UTC (permalink / raw)
  To: selinux

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH testsuite] tests/module_load: detect the linker to use for module build
  2022-07-18 10:51 [PATCH testsuite] tests/module_load: detect the linker to use for module build Ondrej Mosnacek
@ 2022-08-01  9:06 ` Ondrej Mosnacek
  0 siblings, 0 replies; 2+ messages in thread
From: Ondrej Mosnacek @ 2022-08-01  9:06 UTC (permalink / raw)
  To: SElinux list

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

-- 
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-01  9:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-18 10:51 [PATCH testsuite] tests/module_load: detect the linker to use for module build Ondrej Mosnacek
2022-08-01  9:06 ` Ondrej Mosnacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).