Skip to contentSkip to conversation

RISC-V: Support 32_PCREL relocation type in kernel module

PATCH In review Nylon Chen 3 participants 10 messages Jun 15, 2023 – Aug 21, 2023 1 message not in the archive RISC-V

lore
Share & tools
lore

Nylon Chen

author
#0 lore raw

PATCH v2 1/1 RISC-V: Support 32_PCREL relocation type in kernel module

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

---
Changed in v2:
- add commit message.

 arch/riscv/kernel/module.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/riscv/kernel/module.c +10 -0
index 7c651d55fcbd..65be0360a494 100644
@@ -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,
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw
Quoted 3 lines · Nylon Chen

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

Signature · 8 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw
In reply to Nylon Chen

Hey Nylon, thanks for the update.

Quoted 3 lines · Nylon Chen

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.

Quoted 41 lines

---
Changed in v2:
- add commit message.

 arch/riscv/kernel/module.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/riscv/kernel/module.c +10 -0
index 7c651d55fcbd..65be0360a494 100644
@@ -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,
Signature · 1 line
2.40.1

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

A message that is not in the archive (sent off-list or not yet received): CAHh=Yk_=oZJ6eY3jJzLavub5rGsvY=MKv4tGXeiqkz+rgJHwDQ@mail.gmail.com

#4 lore raw
Quoted 1 line · Nylon Chen

I mean, when we use the flag "-fasynchronous-unwind-tables,"

Why?

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#5 lore raw
In reply to a message not in the archive

Hey Nylon,

Firstly, no html emails please :/

Quoted 12 lines · Nylon Chen

Conor Dooley <[email protected]<mailto:[email protected]>> 於 2023年6月15日 週四 下午3:38寫道:

Quoted 6 lines · Nylon Chen
Quoted 3 lines

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?

sorry my description is not correct, please allow me to add

I mean, when we use the flag "-fasynchronous-unwind-tables," it generates the relocation type R_RISCV_32_PCCREL. However, this type is currently not supported, so an error will occur.

(snip)

Quoted 3 lines
Quoted 1 line

Should there be a Fixes: tag for that commit?

yes, I will do it.

What mainline commit actually enables this flag?

Cheers,
Conor.

author
#6 lore raw

On Thu, Jun 15, 2023 at 09:11:33AM +0100, Conor Dooley wrote:
Hi Conor,

Quoted 3 lines

Hey Nylon,

Firstly, no html emails please :/

Sorry, my Gmail settings got messed up. I will be more careful in the future.

Quoted 15 lines · Nylon Chen
Quoted 6 lines

Conor Dooley <[email protected]<mailto:[email protected]>> 於 2023年6月15日 週四 下午3:38寫道:

Quoted 5 lines · Nylon Chen
Quoted 3 lines

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

Quoted 6 lines
Quoted 1 line

What commit adds the -fasynchronous-unwind-tables flag?

sorry my description is not correct, please allow me to add

I mean, when we use the flag "-fasynchronous-unwind-tables," it generates the relocation type R_RISCV_32_PCCREL. However, this type is currently not supported, so an error will occur.

Quoted 7 lines

(snip)

Quoted 3 lines
Quoted 1 line

Should there be a Fixes: tag for that commit?

yes, I will do it.

What mainline commit actually enables this flag?

Because LLVM currently has it enabled by default(https://reviews.llvm.org/D145164), it will generate this relocation type.

From what I know, GCC will also enable it in the future.

Quoted 3 lines

Cheers,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#7 lore raw
Quoted 4 lines · Nylon Chen

Because LLVM currently has it enabled by default(https://reviews.llvm.org/D145164), it will generate this relocation type.

Quoted 1 line

From what I know, GCC will also enable it in the future.

That's why the kernel explicitly disables it.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
author
#8 lore raw
Quoted 2 lines · Andreas Schwab

On Jun 15 2023, Nylon Chen wrote:

Hi Andreas,

Quoted 6 lines
Quoted 4 lines

Because LLVM currently has it enabled by default(https://reviews.llvm.org/D145164), it will generate this relocation type.

Quoted 1 line

From what I know, GCC will also enable it in the future.

That's why the kernel explicitly disables it.

Ok, thanks for your feedback, after I cross-tested, there is indeed no relevant relocation type generated.

If this error no longer occurs.

I am open to the idea of adding this patch to the upstream and would like to hear your thoughts on whether it is still necessary.

Quoted 5 lines
Signature · 4 lines
-- 
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
author
#9 lore raw

Hi Andreas,

Currently, due to the lack of support for the unwind table in RISC-V, we have disabled it.

If RISC-V were to support the unwind table in the future, would we need to re-enable it? Would this require implementing related changes(module relocation type handler) at that time?

Nylon Chen <[email protected]> 於 2023年6月15日 週四 下午5:47寫道:

Nylon Chen <[email protected]> 於 2023年6月15日 週四 下午5:47寫道:

Quoted 21 lines · Andreas Schwab
Quoted 2 lines · Nylon Chen

Hi Andreas,

Quoted 6 lines
Quoted 4 lines

Because LLVM currently has it enabled by default(https://reviews.llvm.org/D145164), it will generate this relocation type.

Quoted 1 line

From what I know, GCC will also enable it in the future.

That's why the kernel explicitly disables it.

Ok, thanks for your feedback, after I cross-tested, there is indeed no relevant relocation type generated.

If this error no longer occurs.

I am open to the idea of adding this patch to the upstream and would like to hear your thoughts on whether it is still necessary.

Quoted 5 lines

--
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."

#10 lore raw
Quoted 3 lines · Nylon Chen

If RISC-V were to support the unwind table in the future, would we need to re-enable it? Would this require implementing related changes(module relocation type handler) at that time?

I'm not quite sure I understand your question. Nothing in the kernel makes use of unwind tables on RISC-V, so there is no point in generating them.

Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."