[PATCH 1/3] RISC-V: Enable shared library and PIE support for riscv*-elf

mintsuki mintsuki@protonmail.com
Thu Sep 3 17:48:40 GMT 2026


riscv*-elf is the only RISC-V target for which ld refuses -shared and
-pie.  elf32lriscv-defs.sh has withheld GENERATE_SHLIB_SCRIPT and
GENERATE_PIE_SCRIPT from it since be1dce26256a ("RISC-V: Disable shared
library support for embedded elf."), whose commit message is a ChangeLog
entry and records no reason.  Nothing in the RISC-V backend depends on
the exclusion, and riscv*-elf does not even set EMBEDDED.

Other bare-metal ELF targets do generate the scripts.  aarch64*-elf was
given PIE support deliberately, in 3af09e8db308 ("Support -pie for
aarch64*-elf targets."), and arm-elf and x86_64-elf have both.  The
general question was raised in f2aaebdb9797 ("GENERATE_SHLIB_SCRIPT vs.
EMBEDDED.") and settled the other way: that patch was reverted in
8a46e447a88d, the author concluding that the ELF and program headers are
still written to the file, so an embedded ELF target producing an ET_DYN
image is not the mistake he had taken it for.

The practical case is UEFI.  A UEFI application is an ET_DYN image with
a PE header stapled on, self-relocating through its R_RISCV_RELATIVE
relocations, so building one needs -pie or -shared -Bsymbolic.  With a
riscv64-elf toolchain the compiler accepts -fPIE and the link then fails
with "-pie not supported", which leaves substituting a riscv*-linux ld
or lld as the only way out of an otherwise complete toolchain.

ld/
	* emulparams/elf32lriscv-defs.sh (GENERATE_SHLIB_SCRIPT): Set
	unconditionally rather than only for non-riscv*-elf targets.
	(GENERATE_PIE_SCRIPT): Likewise.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Mintsuki <mintsuki@protonmail.com>
---
 ld/emulparams/elf32lriscv-defs.sh | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/ld/emulparams/elf32lriscv-defs.sh b/ld/emulparams/elf32lriscv-defs.sh
index 01655616..5c0dac2a 100644
--- a/ld/emulparams/elf32lriscv-defs.sh
+++ b/ld/emulparams/elf32lriscv-defs.sh
@@ -16,15 +16,8 @@ if test `echo "$host" | sed -e s/64//` = `echo "$target" | sed -e s/64//`; then
   esac
 fi
 
-# Enable shared library support for everything except an embedded elf target.
-case "$target" in
-  riscv*-elf)
-    ;;
-  *)
-    GENERATE_SHLIB_SCRIPT=yes
-    GENERATE_PIE_SCRIPT=yes
-    ;;
-esac
+GENERATE_SHLIB_SCRIPT=yes
+GENERATE_PIE_SCRIPT=yes
 
 IREL_IN_PLT=
 TEXT_START_ADDR=0x10000
-- 
2.55.0




More information about the Binutils mailing list