[PATCH v2] RISC-V: Support 32_PCREL relocation type in kernel module

Nylon Chen posted 1 patch 2 years, 7 months ago
arch/riscv/kernel/module.c | 10 ++++++++++
1 file changed, 10 insertions(+)
[PATCH v2] RISC-V: Support 32_PCREL relocation type in kernel module
Posted by Nylon Chen 2 years, 7 months ago
Fix the 'unsupported relocation type' error caused by
enabling the -fasynchronous-unwind-tables flag,
which generates relocation types that are not supported.

Signed-off-by: Nylon Chen <nylon.chen@sifive.com>
Reviewed-by: Zong Li <zong.li@sifive.com>
---
Changed in v2:
- add commit message.

 arch/riscv/kernel/module.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
index 7c651d55fcbd..65be0360a494 100644
--- a/arch/riscv/kernel/module.c
+++ b/arch/riscv/kernel/module.c
@@ -310,6 +310,15 @@ static int apply_r_riscv_sub64_rela(struct module *me, u32 *location,
 	return 0;
 }
 
+static int apply_r_riscv_pcrel_32_rela(struct module *me, u32 *location,
+				       Elf_Addr v)
+{
+	ptrdiff_t offset = (void *)v - (void *)location;
+
+	*location = (*location & 0xffff0000) | (offset & 0xffff);
+	return 0;
+}
+
 static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
 				Elf_Addr v) = {
 	[R_RISCV_32]			= apply_r_riscv_32_rela,
@@ -335,6 +344,7 @@ static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
 	[R_RISCV_SUB16]			= apply_r_riscv_sub16_rela,
 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
 	[R_RISCV_SUB64]			= apply_r_riscv_sub64_rela,
+	[R_RISCV_32_PCREL]		= apply_r_riscv_pcrel_32_rela,
 };
 
 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
-- 
2.40.1
Re: [PATCH v2] RISC-V: Support 32_PCREL relocation type in kernel module
Posted by Conor Dooley 2 years, 7 months ago
Hey Nylon, thanks for the update.

On Thu, Jun 15, 2023 at 03:23:02PM +0800, Nylon Chen wrote:
> Fix the 'unsupported relocation type' error caused by
> enabling the -fasynchronous-unwind-tables flag,
> which generates relocation types that are not supported.

What commit adds the -fasynchronous-unwind-tables flag?
Should there be a Fixes: tag for that commit?

Cheers,
Conor.

> 
> Signed-off-by: Nylon Chen <nylon.chen@sifive.com>
> Reviewed-by: Zong Li <zong.li@sifive.com>
> ---
> Changed in v2:
> - add commit message.
> 
>  arch/riscv/kernel/module.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
> index 7c651d55fcbd..65be0360a494 100644
> --- a/arch/riscv/kernel/module.c
> +++ b/arch/riscv/kernel/module.c
> @@ -310,6 +310,15 @@ static int apply_r_riscv_sub64_rela(struct module *me, u32 *location,
>  	return 0;
>  }
>  
> +static int apply_r_riscv_pcrel_32_rela(struct module *me, u32 *location,
> +				       Elf_Addr v)
> +{
> +	ptrdiff_t offset = (void *)v - (void *)location;
> +
> +	*location = (*location & 0xffff0000) | (offset & 0xffff);
> +	return 0;
> +}
> +
>  static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
>  				Elf_Addr v) = {
>  	[R_RISCV_32]			= apply_r_riscv_32_rela,
> @@ -335,6 +344,7 @@ static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
>  	[R_RISCV_SUB16]			= apply_r_riscv_sub16_rela,
>  	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
>  	[R_RISCV_SUB64]			= apply_r_riscv_sub64_rela,
> +	[R_RISCV_32_PCREL]		= apply_r_riscv_pcrel_32_rela,
>  };
>  
>  int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
> -- 
> 2.40.1
> 
Re: [PATCH v2] RISC-V: Support 32_PCREL relocation type in kernel module
Posted by Andreas Schwab 2 years, 7 months ago
On Jun 15 2023, Nylon Chen wrote:

> Fix the 'unsupported relocation type' error caused by
> enabling the -fasynchronous-unwind-tables flag,
> which generates relocation types that are not supported.

arch/riscv/Makefile has
KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."