]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
kbuild: Introduce Kconfig symbol for linking vmlinux with relocations
authorArd Biesheuvel <ardb@kernel.org>
Tue, 11 Mar 2025 11:06:19 +0000 (12:06 +0100)
committerMasahiro Yamada <masahiroy@kernel.org>
Sun, 16 Mar 2025 15:29:50 +0000 (00:29 +0900)
Some architectures build vmlinux with static relocations preserved, but
strip them again from the final vmlinux image. Arch specific tools
consume these static relocations in order to construct relocation tables
for KASLR.

The fact that vmlinux is created, consumed and subsequently updated goes
against the typical, declarative paradigm used by Make, which is based
on rules and dependencies. So as a first step towards cleaning this up,
introduce a Kconfig symbol to declare that the arch wants to consume the
static relocations emitted into vmlinux. This will be wired up further
in subsequent patches.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Makefile
arch/Kconfig
arch/mips/Kconfig
arch/mips/Makefile
arch/riscv/Kconfig
arch/riscv/Makefile
arch/s390/Kconfig
arch/s390/Makefile
arch/x86/Kconfig
arch/x86/Makefile

index 30242e731a0d19faa0ffbeada8313e77c4105b41..0904b73872cb5714621941c0d9ddd9814645045c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1120,6 +1120,10 @@ ifdef CONFIG_LD_ORPHAN_WARN
 LDFLAGS_vmlinux += --orphan-handling=$(CONFIG_LD_ORPHAN_WARN_LEVEL)
 endif
 
+ifneq ($(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),)
+LDFLAGS_vmlinux        += --emit-relocs --discard-none
+endif
+
 # Align the bit size of userspace programs with the kernel
 KBUILD_USERCFLAGS  += $(filter -m32 -m64 --target=%, $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
 KBUILD_USERLDFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
index b8a4ff36558228240080a5677f702d37f4f8d547..101a13fcde8e41e86ec61a46c9dd4365073f43f9 100644 (file)
@@ -1695,6 +1695,13 @@ config ARCH_HAS_KERNEL_FPU_SUPPORT
          Architectures that select this option can run floating-point code in
          the kernel, as described in Documentation/core-api/floating-point.rst.
 
+config ARCH_VMLINUX_NEEDS_RELOCS
+       bool
+       help
+         Whether the architecture needs vmlinux to be built with static
+         relocations preserved. This is used by some architectures to
+         construct bespoke relocation tables for KASLR.
+
 source "kernel/gcov/Kconfig"
 
 source "scripts/gcc-plugins/Kconfig"
index 1924f2d839323243e378e32ef7d12640560a7be3..5aedbd7afadbd252cbec8c50798dff79348f587f 100644 (file)
@@ -2617,6 +2617,7 @@ config RELOCATABLE
                   CPU_MIPS32_R6 || CPU_MIPS64_R6 || \
                   CPU_P5600 || CAVIUM_OCTEON_SOC || \
                   CPU_LOONGSON64
+       select ARCH_VMLINUX_NEEDS_RELOCS
        help
          This builds a kernel image that retains relocation information
          so it can be loaded someplace besides the default 1MB.
index be8cb44a89fd058c94beee7738192d2ecee3dba1..d9057e29bc62b57022440593ea62045245a1683f 100644 (file)
@@ -100,10 +100,6 @@ LDFLAGS_vmlinux                    += -G 0 -static -n -nostdlib
 KBUILD_AFLAGS_MODULE           += -mlong-calls
 KBUILD_CFLAGS_MODULE           += -mlong-calls
 
-ifeq ($(CONFIG_RELOCATABLE),y)
-LDFLAGS_vmlinux                        += --emit-relocs
-endif
-
 cflags-y += -ffreestanding
 
 cflags-$(CONFIG_CPU_BIG_ENDIAN)                += -EB
index 7612c52e9b1e35607f1dd4603a596416d3357a71..6f5800114416264cae10f6b2af2e3beb8a1f8515 100644 (file)
@@ -1077,6 +1077,7 @@ config RELOCATABLE
        bool "Build a relocatable kernel"
        depends on MMU && 64BIT && !XIP_KERNEL
        select MODULE_SECTIONS if MODULES
+       select ARCH_VMLINUX_NEEDS_RELOCS
        help
           This builds a kernel as a Position Independent Executable (PIE),
           which retains all relocation metadata required to relocate the
index 13fbc0f94238797acad3b8991b48b0aa02c84990..6ef0d10e0c50a4d211d6827281881750c933d434 100644 (file)
@@ -8,7 +8,7 @@
 
 LDFLAGS_vmlinux := -z norelro
 ifeq ($(CONFIG_RELOCATABLE),y)
-       LDFLAGS_vmlinux += -shared -Bsymbolic -z notext --emit-relocs
+       LDFLAGS_vmlinux += -shared -Bsymbolic -z notext
        KBUILD_CFLAGS += -fPIE
 endif
 ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
index 9c9ec08d78c71b4d227beeafab1b82d6434cb5c7..ea67b731713862e79b85e40832bb2e0e6dc32670 100644 (file)
@@ -630,6 +630,7 @@ endchoice
 
 config RELOCATABLE
        def_bool y
+       select ARCH_VMLINUX_NEEDS_RELOCS
        help
          This builds a kernel image that retains relocation information
          so it can be loaded at an arbitrary address.
index 5fae311203c269f5e0062d267955e1cff4cc6f87..d5f4be440879089b499ae0ea5c2cafd2545217e9 100644 (file)
@@ -15,7 +15,7 @@ KBUILD_CFLAGS_MODULE += -fPIC
 KBUILD_AFLAGS  += -m64
 KBUILD_CFLAGS  += -m64
 KBUILD_CFLAGS  += -fPIC
-LDFLAGS_vmlinux        := -no-pie --emit-relocs --discard-none
+LDFLAGS_vmlinux        := -no-pie
 extra_tools    := relocs
 aflags_dwarf   := -Wa,-gdwarf-2
 KBUILD_AFLAGS_DECOMPRESSOR := $(CLANG_FLAGS) -m64 -D__ASSEMBLY__
index 0e27ebd7e36a9e3d69ad3e77c8db5dcf11ae3016..57fc0e7635c98b16a2b39857c753c560a073140d 100644 (file)
@@ -2200,6 +2200,7 @@ config RANDOMIZE_BASE
 config X86_NEED_RELOCS
        def_bool y
        depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE)
+       select ARCH_VMLINUX_NEEDS_RELOCS
 
 config PHYSICAL_ALIGN
        hex "Alignment value to which kernel should be aligned"
index 5b773b34768d1282ab4b6e0cb56f2b1a851cc251..f65ed6dcd6fbce0b005af5daf706c64cc31afc04 100644 (file)
@@ -251,12 +251,6 @@ endif
 
 KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE)
 
-ifdef CONFIG_X86_NEED_RELOCS
-LDFLAGS_vmlinux := --emit-relocs --discard-none
-else
-LDFLAGS_vmlinux :=
-endif
-
 #
 # The 64-bit kernel must be aligned to 2MB.  Pass -z max-page-size=0x200000 to
 # the linker to force 2MB page size regardless of the default page size used