[PATCH] scripts: Introduce ARCH_WANTS_RELA_DYN to avoid stripping .rela.dyn section
From: Alexandre Ghiti
Date: Fri Mar 28 2025 - 03:55:48 EST
riscv uses the .rela.dyn section to relocate the kernel at runtime but
that section is stripped from vmlinux. That prevents kexec to
successfully load vmlinux since it does not contain the relocations info
needed.
Fix this by introducing a new config ARCH_WANTS_RELA_DYN that prevents
.rela.dyn to be stripped.
Fixes: 71d815bf5dfd ("kbuild: Strip runtime const RELA sections correctly")
Signed-off-by: Alexandre Ghiti <alexghiti@xxxxxxxxxxxx>
---
arch/Kconfig | 7 +++++++
arch/riscv/Kconfig | 1 +
scripts/Makefile.lib | 4 +++-
3 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/arch/Kconfig b/arch/Kconfig
index b8a4ff365582..d93f50ecb8f9 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -1544,6 +1544,13 @@ config HAVE_ARCH_PREL32_RELOCATIONS
architectures, and don't require runtime relocation on relocatable
kernels.
+config ARCH_WANTS_RELA_DYN
+ bool
+ depends on RELOCATABLE
+ help
+ This prevents the .rela.dyn section to be stripped from vmlinux since
+ it is needed to relocate the kernel at runtime.
+
config ARCH_USE_MEMREMAP_PROT
bool
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 7612c52e9b1e..d58053efa4e4 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -84,6 +84,7 @@ config RISCV
select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP
select ARCH_WANTS_NO_INSTR
+ select ARCH_WANTS_RELA_DYN if RELOCATABLE
select ARCH_WANTS_THP_SWAP if HAVE_ARCH_TRANSPARENT_HUGEPAGE
select ARCH_WEAK_RELEASE_ACQUIRE if ARCH_USE_QUEUED_SPINLOCKS
select BINFMT_FLAT_NO_DATA_START_OFFSET if !MMU
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index cad20f0e66ee..e59210a9fde0 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -376,8 +376,10 @@ quiet_cmd_ar = AR $@
quiet_cmd_objcopy = OBJCOPY $@
cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
+objcopy-args-$(CONFIG_ARCH_WANTS_RELA_DYN) += --remove-section=!.rela.dyn
+
quiet_cmd_strip_relocs = RSTRIP $@
-cmd_strip_relocs = $(OBJCOPY) --remove-section='.rel*' $@
+cmd_strip_relocs = $(OBJCOPY) --remove-section='.rel*' $(objcopy-args-y) $@
# Gzip
# ---------------------------------------------------------------------------
--
2.39.2