[PATCH v2] RISC-V: Bind defined symbol locally in PIE

Linsen Zhou i@lin.moe
Mon Jul 7 22:34:18 GMT 2025


Reference commit 1dcb9720d62cd053a72c31881b7724ce9f74332c

bfd/
	* elfnn-riscv.c (RISCV_COPY_INPUT_RELOC): Bind defined symbol
	locally in PIE.

ld/
	* testsuite/ld-riscv-elf/pie-bind-locally-a.s: New test source.
	* testsuite/ld-riscv-elf/pie-bind-locally-b.s: Likewise.
	* testsuite/ld-riscv-elf/pie-bind-locally-rv32.d: New testcase.
	* testsuite/ld-riscv-elf/pie-bind-locally-rv64.d: Likewise.

Signed-off-by: Linsen Zhou <i@lin.moe>
---
Changes in v2:
- Revert manually edited Changelog
- Replace auipc+lw/ld with .option pic+la in pie-bind-locally-a.s
- Add Signed-off-by line

Thanks to Nelson Chu for your help.

 bfd/elfnn-riscv.c                                 |  2 +-
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp        |  3 +++
 ld/testsuite/ld-riscv-elf/pie-bind-locally-a.s    |  5 +++++
 ld/testsuite/ld-riscv-elf/pie-bind-locally-b.s    |  8 ++++++++
 ld/testsuite/ld-riscv-elf/pie-bind-locally-rv32.d | 10 ++++++++++
 ld/testsuite/ld-riscv-elf/pie-bind-locally-rv64.d | 10 ++++++++++
 6 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 ld/testsuite/ld-riscv-elf/pie-bind-locally-a.s
 create mode 100644 ld/testsuite/ld-riscv-elf/pie-bind-locally-b.s
 create mode 100644 ld/testsuite/ld-riscv-elf/pie-bind-locally-rv32.d
 create mode 100644 ld/testsuite/ld-riscv-elf/pie-bind-locally-rv64.d

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 790f0397cf5..2fd01299249 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -99,7 +99,7 @@
   ((H) != NULL \
    && (H)->dynindx != -1 \
    && (!bfd_link_pic (INFO) \
-       || !SYMBOLIC_BIND ((INFO), (H)) \
+       || !(bfd_link_pie ((INFO)) || SYMBOLIC_BIND ((INFO), (H))) \
        || !(H)->def_regular))
 
 /* True if this is actually a static link, or it is a -Bsymbolic link
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index e103df66e11..70c9aa7f66a 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -235,6 +235,9 @@ if [istarget "riscv*-*-*"] {
 
     run_dump_test "zicfilp-unlabeled-plt"
 
+    run_dump_test "pie-bind-locally-rv32"
+    run_dump_test "pie-bind-locally-rv64"
+
     # IFUNC testcases.
     # Check IFUNC by single type relocs.
     run_dump_test_ifunc "ifunc-reloc-call-01" rv32 exe
diff --git a/ld/testsuite/ld-riscv-elf/pie-bind-locally-a.s b/ld/testsuite/ld-riscv-elf/pie-bind-locally-a.s
new file mode 100644
index 00000000000..c1ff19bc6b7
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pie-bind-locally-a.s
@@ -0,0 +1,5 @@
+	.text
+	.global	_start
+_start:
+	.option pic
+	la a0, _start
diff --git a/ld/testsuite/ld-riscv-elf/pie-bind-locally-b.s b/ld/testsuite/ld-riscv-elf/pie-bind-locally-b.s
new file mode 100644
index 00000000000..e5f16cdf9df
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pie-bind-locally-b.s
@@ -0,0 +1,8 @@
+	.section	.data.rel,"aw",%progbits
+	.type	q, %object
+q:
+.ifdef __64_bit__
+	.quad	_start
+.else
+	.word	_start
+.endif
diff --git a/ld/testsuite/ld-riscv-elf/pie-bind-locally-rv32.d b/ld/testsuite/ld-riscv-elf/pie-bind-locally-rv32.d
new file mode 100644
index 00000000000..01339952225
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pie-bind-locally-rv32.d
@@ -0,0 +1,10 @@
+#source: pie-bind-locally-a.s
+#source: pie-bind-locally-b.s
+#as: -march=rv32i -mabi=ilp32
+#ld: -m[riscv_choose_ilp32_emul] -pie
+#readelf: -Wr
+
+Relocation section '\.rela\.dyn' at offset .* contains 2 entries:
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+[0-9a-f]+[ 	]+[0-9a-f]+[ 	]+R_RISCV_RELATIVE[ ].*
+[0-9a-f]+[ 	]+[0-9a-f]+[ 	]+R_RISCV_RELATIVE[ ].*
diff --git a/ld/testsuite/ld-riscv-elf/pie-bind-locally-rv64.d b/ld/testsuite/ld-riscv-elf/pie-bind-locally-rv64.d
new file mode 100644
index 00000000000..6ae627a7060
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pie-bind-locally-rv64.d
@@ -0,0 +1,10 @@
+#source: pie-bind-locally-a.s
+#source: pie-bind-locally-b.s
+#as: -march=rv64i -mabi=lp64 -defsym __64_bit__=1
+#ld: -m[riscv_choose_lp64_emul] -pie
+#readelf: -Wr
+
+Relocation section '\.rela\.dyn' at offset .* contains 2 entries:
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+[0-9a-f]+[ 	]+[0-9a-f]+[ 	]+R_RISCV_RELATIVE[ ].*
+[0-9a-f]+[ 	]+[0-9a-f]+[ 	]+R_RISCV_RELATIVE[ ].*
-- 
2.50.0



-- 
Linsen Zhou
https://lin.moe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 243 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20250708/16d0477d/attachment.sig>


More information about the Binutils mailing list