Unverified Commit 310c33dc authored by Palmer Dabbelt's avatar Palmer Dabbelt
Browse files

Merge patch series "Introduce 64b relocatable kernel"

Alexandre Ghiti <alexghiti@rivosinc.com> says:

After multiple attempts, this patchset is now based on the fact that the
64b kernel mapping was moved outside the linear mapping.

The first patch allows to build relocatable kernels but is not selected
by default. That patch is a requirement for KASLR.
The second and third patches take advantage of an already existing powerpc
script that checks relocations at compile-time, and uses it for riscv.

* b4-shazam-merge:
  riscv: Use --emit-relocs in order to move .rela.dyn in init
  riscv: Check relocations at compile time
  powerpc: Move script to check relocations at compile time in scripts/
  riscv: Introduce CONFIG_RELOCATABLE
  riscv: Move .rela.dyn outside of init to avoid empty relocations
  riscv: Prepare EFI header for relocatable kernels

Link: https://lore.kernel.org/r/20230329045329.64565-1-alexghiti@rivosinc.com


Signed-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
parents 2667e367 559d1e45
Loading
Loading
Loading
Loading
+2 −16
Original line number Diff line number Diff line
@@ -15,21 +15,8 @@ if [ $# -lt 3 ]; then
	exit 1
fi

# Have Kbuild supply the path to objdump and nm so we handle cross compilation.
objdump="$1"
nm="$2"
vmlinux="$3"

# Remove from the bad relocations those that match an undefined weak symbol
# which will result in an absolute relocation to 0.
# Weak unresolved symbols are of that form in nm output:
# "                  w _binary__btf_vmlinux_bin_end"
undef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }')

bad_relocs=$(
$objdump -R "$vmlinux" |
	# Only look at relocation lines.
	grep -E '\<R_' |
${srctree}/scripts/relocs_check.sh "$@" |
	# These relocations are okay
	# On PPC64:
	#	R_PPC64_RELATIVE, R_PPC64_NONE
@@ -44,8 +31,7 @@ R_PPC_ADDR16_LO
R_PPC_ADDR16_HI
R_PPC_ADDR16_HA
R_PPC_RELATIVE
R_PPC_NONE' |
	([ "$undef_weak_symbols" ] && grep -F -w -v "$undef_weak_symbols" || cat)
R_PPC_NONE'
)

if [ -z "$bad_relocs" ]; then
+14 −0
Original line number Diff line number Diff line
@@ -603,6 +603,20 @@ config COMPAT

	  If you want to execute 32-bit userspace applications, say Y.

config RELOCATABLE
	bool "Build a relocatable kernel"
	depends on MMU && 64BIT && !XIP_KERNEL
	help
          This builds a kernel as a Position Independent Executable (PIE),
          which retains all relocation metadata required to relocate the
          kernel binary at runtime to a different virtual address than the
          address it was linked at.
          Since RISCV uses the RELA relocation format, this requires a
          relocation pass at runtime even if the kernel is loaded at the
          same address it was linked at.

          If unsure, say N.

endmenu # "Kernel features"

menu "Boot options"
+5 −2
Original line number Diff line number Diff line
@@ -7,9 +7,12 @@
#

OBJCOPYFLAGS    := -O binary
LDFLAGS_vmlinux :=
ifeq ($(CONFIG_RELOCATABLE),y)
	LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro --emit-relocs
	KBUILD_CFLAGS += -fPIE
endif
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
	LDFLAGS_vmlinux := --no-relax
	LDFLAGS_vmlinux += --no-relax
	KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
ifeq ($(CONFIG_RISCV_ISA_C),y)
	CC_FLAGS_FTRACE := -fpatchable-function-entry=4
+49 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0
# ===========================================================================
# Post-link riscv pass
# ===========================================================================
#
# Check that vmlinux relocations look sane

PHONY := __archpost
__archpost:

-include include/config/auto.conf
include $(srctree)/scripts/Kbuild.include

quiet_cmd_relocs_check = CHKREL  $@
cmd_relocs_check = 							\
	$(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@"

ifdef CONFIG_RELOCATABLE
quiet_cmd_cp_vmlinux_relocs = CPREL   vmlinux.relocs
cmd_cp_vmlinux_relocs = cp vmlinux vmlinux.relocs

quiet_cmd_relocs_strip = STRIPREL $@
cmd_relocs_strip = $(OBJCOPY)   --remove-section='.rel.*'       \
                                --remove-section='.rel__*'      \
                                --remove-section='.rela.*'      \
                                --remove-section='.rela__*' $@
endif

# `@true` prevents complaint when there is nothing to be done

vmlinux: FORCE
	@true
ifdef CONFIG_RELOCATABLE
	$(call if_changed,relocs_check)
	$(call if_changed,cp_vmlinux_relocs)
	$(call if_changed,relocs_strip)
endif

%.ko: FORCE
	@true

clean:
	@true

PHONY += FORCE clean

FORCE:

.PHONY: $(PHONY)
+7 −0
Original line number Diff line number Diff line
@@ -33,7 +33,14 @@ $(obj)/xipImage: vmlinux FORCE

endif

ifdef CONFIG_RELOCATABLE
vmlinux.relocs: vmlinux
	@ (! [ -f vmlinux.relocs ] && echo "vmlinux.relocs can't be found, please remove vmlinux and try again") || true

$(obj)/Image: vmlinux.relocs FORCE
else
$(obj)/Image: vmlinux FORCE
endif
	$(call if_changed,objcopy)

$(obj)/Image.gz: $(obj)/Image FORCE
Loading