Skip to content

Commit 9b400d1

Browse files
ardbiesheuvelmasahir0y
authored andcommitted
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 <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent e22bbb8 commit 9b400d1

File tree

10 files changed

+17
-12
lines changed

10 files changed

+17
-12
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,10 @@ ifdef CONFIG_LD_ORPHAN_WARN
11201120
LDFLAGS_vmlinux += --orphan-handling=$(CONFIG_LD_ORPHAN_WARN_LEVEL)
11211121
endif
11221122

1123+
ifneq ($(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),)
1124+
LDFLAGS_vmlinux += --emit-relocs --discard-none
1125+
endif
1126+
11231127
# Align the bit size of userspace programs with the kernel
11241128
KBUILD_USERCFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))
11251129
KBUILD_USERLDFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS))

arch/Kconfig

+7
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,13 @@ config ARCH_HAS_KERNEL_FPU_SUPPORT
16951695
Architectures that select this option can run floating-point code in
16961696
the kernel, as described in Documentation/core-api/floating-point.rst.
16971697

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+
16981705
source "kernel/gcov/Kconfig"
16991706

17001707
source "scripts/gcc-plugins/Kconfig"

arch/mips/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,7 @@ config RELOCATABLE
26172617
CPU_MIPS32_R6 || CPU_MIPS64_R6 || \
26182618
CPU_P5600 || CAVIUM_OCTEON_SOC || \
26192619
CPU_LOONGSON64
2620+
select ARCH_VMLINUX_NEEDS_RELOCS
26202621
help
26212622
This builds a kernel image that retains relocation information
26222623
so it can be loaded someplace besides the default 1MB.

arch/mips/Makefile

-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ LDFLAGS_vmlinux += -G 0 -static -n -nostdlib
100100
KBUILD_AFLAGS_MODULE += -mlong-calls
101101
KBUILD_CFLAGS_MODULE += -mlong-calls
102102

103-
ifeq ($(CONFIG_RELOCATABLE),y)
104-
LDFLAGS_vmlinux += --emit-relocs
105-
endif
106-
107103
cflags-y += -ffreestanding
108104

109105
cflags-$(CONFIG_CPU_BIG_ENDIAN) += -EB

arch/riscv/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ config RELOCATABLE
10771077
bool "Build a relocatable kernel"
10781078
depends on MMU && 64BIT && !XIP_KERNEL
10791079
select MODULE_SECTIONS if MODULES
1080+
select ARCH_VMLINUX_NEEDS_RELOCS
10801081
help
10811082
This builds a kernel as a Position Independent Executable (PIE),
10821083
which retains all relocation metadata required to relocate the

arch/riscv/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
LDFLAGS_vmlinux := -z norelro
1010
ifeq ($(CONFIG_RELOCATABLE),y)
11-
LDFLAGS_vmlinux += -shared -Bsymbolic -z notext --emit-relocs
11+
LDFLAGS_vmlinux += -shared -Bsymbolic -z notext
1212
KBUILD_CFLAGS += -fPIE
1313
endif
1414
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)

arch/s390/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ endchoice
630630

631631
config RELOCATABLE
632632
def_bool y
633+
select ARCH_VMLINUX_NEEDS_RELOCS
633634
help
634635
This builds a kernel image that retains relocation information
635636
so it can be loaded at an arbitrary address.

arch/s390/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ KBUILD_CFLAGS_MODULE += -fPIC
1515
KBUILD_AFLAGS += -m64
1616
KBUILD_CFLAGS += -m64
1717
KBUILD_CFLAGS += -fPIC
18-
LDFLAGS_vmlinux := -no-pie --emit-relocs --discard-none
18+
LDFLAGS_vmlinux := -no-pie
1919
extra_tools := relocs
2020
aflags_dwarf := -Wa,-gdwarf-2
2121
KBUILD_AFLAGS_DECOMPRESSOR := $(CLANG_FLAGS) -m64 -D__ASSEMBLY__

arch/x86/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,7 @@ config RANDOMIZE_BASE
22002200
config X86_NEED_RELOCS
22012201
def_bool y
22022202
depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE)
2203+
select ARCH_VMLINUX_NEEDS_RELOCS
22032204

22042205
config PHYSICAL_ALIGN
22052206
hex "Alignment value to which kernel should be aligned"

arch/x86/Makefile

-6
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,6 @@ endif
251251

252252
KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE)
253253

254-
ifdef CONFIG_X86_NEED_RELOCS
255-
LDFLAGS_vmlinux := --emit-relocs --discard-none
256-
else
257-
LDFLAGS_vmlinux :=
258-
endif
259-
260254
#
261255
# The 64-bit kernel must be aligned to 2MB. Pass -z max-page-size=0x200000 to
262256
# the linker to force 2MB page size regardless of the default page size used

0 commit comments

Comments
 (0)