[PATCH v1 2/3] riscv/kexec: handle R_RISCV_ADD16 and R_RISCV_SUB16 relocation types

From: Petr Tesarik
Date: Tue Jul 25 2023 - 04:49:22 EST


From: Petr Tesarik <petr.tesarik.ext@xxxxxxxxxx>

16-bit add and subtract relocation types are used by the purgatory code
when the kernel is built with CONFIG_RISCV_ALTERNATIVES. If they are not
handled, kexec_file_load(2) fails with:

Unknown rela relocation: 34
kexec_image: Error loading purgatory ret=-8

or later with:

Unknown rela relocation: 38
kexec_image: Error loading purgatory ret=-8

Note that alternatives are not yet handled in purgatory code; this patch
merely allows to load it.

Fixes: 838b3e28488f ("RISC-V: Load purgatory in kexec_file")
Signed-off-by: Petr Tesarik <petr.tesarik.ext@xxxxxxxxxx>
Cc: Li Zhengyu <lizhengyu3@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
arch/riscv/kernel/elf_kexec.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/arch/riscv/kernel/elf_kexec.c b/arch/riscv/kernel/elf_kexec.c
index 38390d3bdcac..f23fd419c402 100644
--- a/arch/riscv/kernel/elf_kexec.c
+++ b/arch/riscv/kernel/elf_kexec.c
@@ -444,6 +444,12 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
case R_RISCV_SUB32:
*(u32 *)loc -= val;
break;
+ case R_RISCV_ADD16:
+ *(u16 *)loc += val;
+ break;
+ case R_RISCV_SUB16:
+ *(u16 *)loc -= val;
+ break;
/* It has been applied by R_RISCV_PCREL_HI20 sym */
case R_RISCV_PCREL_LO12_I:
case R_RISCV_ALIGN:
--
2.25.1