[PATCH 0/3] Add kernel relocation and KASLR support

From: Youling Tang
Date: Fri Sep 02 2022 - 06:09:09 EST


This patch series to support kernel relocation and KASLR (only 64bit).

When CONFIG_RELOCATABLE is enabled add --emit-relocs to vmlinux LDFLAGS so
that fully linked vmlinux contains relocation information. Run the previously
added relocs tool to fill in the .data.relocs section of vmlinux with a table
of relocations.

Call relocate_kernel(),the kernel makes a copy of itself at the new address.
It uses the relocation table inserted by the relocs tool to fix symbol
references within the new image. This function will return the entry point
of the relocated kernel if copy/relocate is successful or the original entry
point if not. The stack pointer must then be pointed into the new image.

This feature is supported in both old and new toolchains, note that patches
need to be applied [1] when building with old toolchains.

[1] link: https://lore.kernel.org/loongarch/1662030659-21558-1-git-send-email-tangyouling@xxxxxxxxxxx/T/#u

Youling Tang (3):
LoongArch: tools: Add relocs tool support
LoongArch: Add support for kernel relocation
LoongArch: Add support for kernel address space layout randomization
(KASLR)

arch/loongarch/Kconfig | 51 +++
arch/loongarch/Makefile | 7 +
arch/loongarch/Makefile.postlink | 33 ++
arch/loongarch/boot/.gitignore | 1 +
arch/loongarch/boot/tools/Makefile | 8 +
arch/loongarch/boot/tools/relocs.c | 544 ++++++++++++++++++++++++
arch/loongarch/boot/tools/relocs.h | 55 +++
arch/loongarch/boot/tools/relocs_64.c | 18 +
arch/loongarch/boot/tools/relocs_main.c | 82 ++++
arch/loongarch/include/asm/page.h | 6 +
arch/loongarch/kernel/Makefile | 2 +
arch/loongarch/kernel/head.S | 17 +
arch/loongarch/kernel/relocate.c | 328 ++++++++++++++
arch/loongarch/kernel/setup.c | 3 +
arch/loongarch/kernel/vmlinux.lds.S | 25 ++
15 files changed, 1180 insertions(+)
create mode 100644 arch/loongarch/Makefile.postlink
create mode 100644 arch/loongarch/boot/tools/Makefile
create mode 100644 arch/loongarch/boot/tools/relocs.c
create mode 100644 arch/loongarch/boot/tools/relocs.h
create mode 100644 arch/loongarch/boot/tools/relocs_64.c
create mode 100644 arch/loongarch/boot/tools/relocs_main.c
create mode 100644 arch/loongarch/kernel/relocate.c

--
2.36.1