Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: Introduce Kconfig symbol for linking vmlinux with relocations

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>

authored by

Ard Biesheuvel and committed by
Masahiro Yamada
9b400d17 e22bbb8e

+17 -12
+4
Makefile
··· 1120 1120 LDFLAGS_vmlinux += --orphan-handling=$(CONFIG_LD_ORPHAN_WARN_LEVEL) 1121 1121 endif 1122 1122 1123 + ifneq ($(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),) 1124 + LDFLAGS_vmlinux += --emit-relocs --discard-none 1125 + endif 1126 + 1123 1127 # Align the bit size of userspace programs with the kernel 1124 1128 KBUILD_USERCFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)) 1125 1129 KBUILD_USERLDFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
+7
arch/Kconfig
··· 1695 1695 Architectures that select this option can run floating-point code in 1696 1696 the kernel, as described in Documentation/core-api/floating-point.rst. 1697 1697 1698 + config ARCH_VMLINUX_NEEDS_RELOCS 1699 + bool 1700 + help 1701 + Whether the architecture needs vmlinux to be built with static 1702 + relocations preserved. This is used by some architectures to 1703 + construct bespoke relocation tables for KASLR. 1704 + 1698 1705 source "kernel/gcov/Kconfig" 1699 1706 1700 1707 source "scripts/gcc-plugins/Kconfig"
+1
arch/mips/Kconfig
··· 2617 2617 CPU_MIPS32_R6 || CPU_MIPS64_R6 || \ 2618 2618 CPU_P5600 || CAVIUM_OCTEON_SOC || \ 2619 2619 CPU_LOONGSON64 2620 + select ARCH_VMLINUX_NEEDS_RELOCS 2620 2621 help 2621 2622 This builds a kernel image that retains relocation information 2622 2623 so it can be loaded someplace besides the default 1MB.
-4
arch/mips/Makefile
··· 100 100 KBUILD_AFLAGS_MODULE += -mlong-calls 101 101 KBUILD_CFLAGS_MODULE += -mlong-calls 102 102 103 - ifeq ($(CONFIG_RELOCATABLE),y) 104 - LDFLAGS_vmlinux += --emit-relocs 105 - endif 106 - 107 103 cflags-y += -ffreestanding 108 104 109 105 cflags-$(CONFIG_CPU_BIG_ENDIAN) += -EB
+1
arch/riscv/Kconfig
··· 1077 1077 bool "Build a relocatable kernel" 1078 1078 depends on MMU && 64BIT && !XIP_KERNEL 1079 1079 select MODULE_SECTIONS if MODULES 1080 + select ARCH_VMLINUX_NEEDS_RELOCS 1080 1081 help 1081 1082 This builds a kernel as a Position Independent Executable (PIE), 1082 1083 which retains all relocation metadata required to relocate the
+1 -1
arch/riscv/Makefile
··· 8 8 9 9 LDFLAGS_vmlinux := -z norelro 10 10 ifeq ($(CONFIG_RELOCATABLE),y) 11 - LDFLAGS_vmlinux += -shared -Bsymbolic -z notext --emit-relocs 11 + LDFLAGS_vmlinux += -shared -Bsymbolic -z notext 12 12 KBUILD_CFLAGS += -fPIE 13 13 endif 14 14 ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
+1
arch/s390/Kconfig
··· 630 630 631 631 config RELOCATABLE 632 632 def_bool y 633 + select ARCH_VMLINUX_NEEDS_RELOCS 633 634 help 634 635 This builds a kernel image that retains relocation information 635 636 so it can be loaded at an arbitrary address.
+1 -1
arch/s390/Makefile
··· 15 15 KBUILD_AFLAGS += -m64 16 16 KBUILD_CFLAGS += -m64 17 17 KBUILD_CFLAGS += -fPIC 18 - LDFLAGS_vmlinux := -no-pie --emit-relocs --discard-none 18 + LDFLAGS_vmlinux := -no-pie 19 19 extra_tools := relocs 20 20 aflags_dwarf := -Wa,-gdwarf-2 21 21 KBUILD_AFLAGS_DECOMPRESSOR := $(CLANG_FLAGS) -m64 -D__ASSEMBLY__
+1
arch/x86/Kconfig
··· 2200 2200 config X86_NEED_RELOCS 2201 2201 def_bool y 2202 2202 depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) 2203 + select ARCH_VMLINUX_NEEDS_RELOCS 2203 2204 2204 2205 config PHYSICAL_ALIGN 2205 2206 hex "Alignment value to which kernel should be aligned"
-6
arch/x86/Makefile
··· 251 251 252 252 KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE) 253 253 254 - ifdef CONFIG_X86_NEED_RELOCS 255 - LDFLAGS_vmlinux := --emit-relocs --discard-none 256 - else 257 - LDFLAGS_vmlinux := 258 - endif 259 - 260 254 # 261 255 # The 64-bit kernel must be aligned to 2MB. Pass -z max-page-size=0x200000 to 262 256 # the linker to force 2MB page size regardless of the default page size used