Skip to contentSkip to conversation

RISC-V: errata: Use medany for relocatable builds

PATCH Applied Palmer Dabbelt 3 participants 5 messages Mar 26, 2025 – Mar 31, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RISC-V: errata: Use medany for relocatable builds

We're trying to mix non-PIC/PIE objects into the otherwise-PIE relocatable kernels, to avoid GOT/PLT references during early boot alternative resolution (which happens before the GOT/PLT are set up).

riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC

Fixes 8dc2a7e8027f ("riscv: Fix relocatable kernels with early alternatives using -fno-pie")

---
I'm not 100% sure it's possible to expose this without Samuel's NOMMU relocatable builds, see
<https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/>

---
 arch/riscv/errata/Makefile | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/riscv/errata/Makefile +5 -1
index f0da9d7b39c3..bc6c77ba837d 100644
@@ -1,5 +1,9 @@
 ifdef CONFIG_RELOCATABLE
-KBUILD_CFLAGS += -fno-pie
+# We can't use PIC/PIE when handling early-boot errata parsing, as the kernel
+# doesn't have a GOT setup at that point.  So instead just use medany: it's
+# usually position-independent, so it should be good enough for the errata
+# handling.
+KBUILD_CFLAGS += -fno-pie -mcmodel=medany
 endif
 
 ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
Signature · 7 lines
2.45.2


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

We're trying to mix non-PIC/PIE objects into the otherwise-PIE relocatable kernels, to avoid GOT/PLT references during early boot alternative resolution (which happens before the GOT/PLT are set up).

riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC

thead erratas are already compiled with medany if CONFIG_RISCV_ALTERNATIVE_EARLY is set (https://elixir.bootlin.com/linux/v6.14-rc6/source/arch/riscv/errata/thead/Makefile#L2) so why isn't it enough?

Quoted 16 lines
Fixes 8dc2a7e8027f ("riscv: Fix relocatable kernels with early alternatives using -fno-pie")

---
I'm not 100% sure it's possible to expose this without Samuel's NOMMU relocatable builds, see
<https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/>

---
  arch/riscv/errata/Makefile | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
arch/riscv/errata/Makefile +0 -0
index f0da9d7b39c3..bc6c77ba837d 100644

arch/riscv/errata/Makefile line 9

Quoted 10 lines
arch/riscv/errata/Makefile +5 -1
@@ -1,5 +1,9 @@
  ifdef CONFIG_RELOCATABLE
-KBUILD_CFLAGS += -fno-pie
+# We can't use PIC/PIE when handling early-boot errata parsing, as the kernel
+# doesn't have a GOT setup at that point.  So instead just use medany: it's
+# usually position-independent, so it should be good enough for the errata
+# handling.
+KBUILD_CFLAGS += -fno-pie -mcmodel=medany
  endif
 
  ifdef CONFIG_RISCV_ALTERNATIVE_EARLY

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

#2 lore raw
Quoted 11 lines · Alexandre Ghiti
Quoted 6 lines · Palmer Dabbelt

We're trying to mix non-PIC/PIE objects into the otherwise-PIE relocatable kernels, to avoid GOT/PLT references during early boot alternative resolution (which happens before the GOT/PLT are set up).

riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC

thead erratas are already compiled with medany if CONFIG_RISCV_ALTERNATIVE_EARLY is set (https://elixir.bootlin.com/linux/v6.14-rc6/source/arch/riscv/errata/thead/Makefile#L2) so why isn't it enough?

FYI, this config without early boot alternatives trigger the same relocation issue with thead erratas: https://download.01.org/0day-ci/archive/20250328/[email protected]/config

Quoted 27 lines
Quoted 27 lines
Fixes 8dc2a7e8027f ("riscv: Fix relocatable kernels with early alternatives using -fno-pie")

---
I'm not 100% sure it's possible to expose this without Samuel's NOMMU relocatable builds, see
<https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/>

---
  arch/riscv/errata/Makefile | 6 +++++-

  1 file changed, 5 insertions(+), 1 deletion(-)
arch/riscv/errata/Makefile +0 -0
index f0da9d7b39c3..bc6c77ba837d 100644
@@ -1,5 +1,9 @@

  ifdef CONFIG_RELOCATABLE

arch/riscv/errata/Makefile +1 -1
-KBUILD_CFLAGS += -fno-pie
+# We can't use PIC/PIE when handling early-boot errata parsing, as 

the kernel
+# doesn't have a GOT setup at that point.  So instead just use
medany: it's
+# usually position-independent, so it should be good enough for the errata
+# handling.

Quoted 8 lines
Quoted 3 lines

+KBUILD_CFLAGS += -fno-pie -mcmodel=medany
  endif
    ifdef CONFIG_RISCV_ALTERNATIVE_EARLY

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

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

#3 lore raw
In reply to Alexandre Ghiti
Quoted 5 lines · Alexandre Ghiti
Quoted 4 lines · Palmer Dabbelt

We're trying to mix non-PIC/PIE objects into the otherwise-PIE relocatable kernels, to avoid GOT/PLT references during early boot alternative resolution (which happens before the GOT/PLT are set up).

Quoted 8 lines
Quoted 2 lines

riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC

thead erratas are already compiled with medany if
CONFIG_RISCV_ALTERNATIVE_EARLY is set
(https://elixir.bootlin.com/linux/v6.14-rc6/source/arch/riscv/errata/thead/Makefile#L2)
so why isn't it enough?

Those are linker errors, we can't pull the medlow code into PIC objects. So any objects in relocatable kernels need to either be PIC/PIE or be medany (which can't reference any absolute symbols).

Quoted 24 lines
Quoted 24 lines
Fixes 8dc2a7e8027f ("riscv: Fix relocatable kernels with early alternatives using -fno-pie")

---
I'm not 100% sure it's possible to expose this without Samuel's NOMMU relocatable builds, see
<https://lore.kernel.org/all/mhng-517c0318-c8fb-4c4a-aa0e-0d08f8d34d78@palmer-ri-x1c9/>

---
  arch/riscv/errata/Makefile | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
arch/riscv/errata/Makefile +5 -1
index f0da9d7b39c3..bc6c77ba837d 100644
@@ -1,5 +1,9 @@
  ifdef CONFIG_RELOCATABLE
-KBUILD_CFLAGS += -fno-pie
+# We can't use PIC/PIE when handling early-boot errata parsing, as the kernel
+# doesn't have a GOT setup at that point.  So instead just use medany: it's
+# usually position-independent, so it should be good enough for the errata
+# handling.
+KBUILD_CFLAGS += -fno-pie -mcmodel=medany
  endif
 
Quoted 6 lines

arch/riscv/errata/Makefile line 9

Quoted 1 line
arch/riscv/errata/Makefile +0 -0
  ifdef CONFIG_RISCV_ALTERNATIVE_EARLY

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

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

#4 lore raw
In reply to Palmer Dabbelt
This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 8 lines · On Wed, 26 Mar 2025 15:45:07 -0700 you wrote:

We're trying to mix non-PIC/PIE objects into the otherwise-PIE relocatable kernels, to avoid GOT/PLT references during early boot alternative resolution (which happens before the GOT/PLT are set up).

riscv64-unknown-linux-gnu-ld: arch/riscv/errata/sifive/errata.o: relocation R_RISCV_HI20 against tlb_flush_all_threshold' can not be used when making a shared object; recompile with -fPIC riscv64-unknown-linux-gnu-ld: arch/riscv/errata/thead/errata.o: relocation R_RISCV_HI20 against riscv_cbom_block_size' can not be used when making a shared object; recompile with -fPIC

[...]

Here is the summary with links:
- RISC-V: errata: Use medany for relocatable builds

    https://git.kernel.org/riscv/c/bb58e1579f43

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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