Skip to content

Commit

Permalink
tests/module_load: detect the linker to use for module build
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
WOnder93 committed Aug 1, 2022
1 parent db1c3fb commit deeff9d
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions tests/module_load/Makefile
Expand Up @@ -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
Expand All @@ -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

0 comments on commit deeff9d

Please sign in to comment.