[PATCH] LoongArch: Use canonical PLT entries for read-only function pointers

zhaozhou zhaozhou@loongson.cn
Wed Nov 25 02:39:11 GMT 2026


When a non-PIE executable stores the address of a shared library
function in read-only data, the relocation scan increments the PLT
reference count but does not set needs_plt.  If the function is only
referenced through the pointer, the linker leaves an R_LARCH_64
dynamic relocation in .rodata and the link fails with -z text.

Resolve read-only pointers to shared library functions through
canonical PLT entries in non-PIE executables.  This lets -z text links
succeed while keeping writable-only function pointers eligible for
dynamic relocation.

PR ld/35006
---
 bfd/elfnn-loongarch.c                         | 17 ++++++++++++-
 .../ld-loongarch-elf/ld-loongarch-elf.exp     | 25 +++++++++++++++++++
 .../ld-loongarch-elf/rodata-func-ptr-lib.s    |  6 +++++
 .../ld-loongarch-elf/rodata-func-ptr.d        | 11 ++++++++
 .../ld-loongarch-elf/rodata-func-ptr.s        |  4 +++
 5 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/rodata-func-ptr-lib.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/rodata-func-ptr.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/rodata-func-ptr.s

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 3860ae81be5..08e95e58f48 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -1481,6 +1481,14 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	      && (!bfd_link_pic (info)
 		  || h->type == STT_GNU_IFUNC))
 	    {
+	      /* The loader can initialize a writable function pointer.
+		 For a read-only pointer, use the PLT address.  */
+	      if (r_type == R_LARCH_64 && h->type == STT_FUNC
+		  && h->def_dynamic && !h->def_regular
+		  && h->ref_regular_nonweak
+		  && (sec->flags & SEC_READONLY))
+		h->needs_plt = 1;
+
 	      /* This reloc might not bind locally.  */
 	      h->non_got_ref = 1;
 	      h->pointer_equality_needed = 1;
@@ -1745,6 +1753,11 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 	{
 	  h->root.u.def.section = plt;
 	  h->root.u.def.value = h->plt.offset;
+	  if (h->type == STT_FUNC
+	      && h->pointer_equality_needed
+	      && h->ref_regular_nonweak)
+	    /* Absolute references now resolve to the canonical PLT entry.  */
+	    h->dyn_relocs = NULL;
 	}
 
       h->needs_plt = 1;
@@ -3735,7 +3748,9 @@ loongarch_elf_relocate_section (struct bfd_link_info *info,
 
 	case R_LARCH_32:
 	case R_LARCH_64:
-	  if (resolved_dynly || (is_pic && resolved_local))
+	  if (resolved_dynly && sec != NULL && sec == plt && !h->dyn_relocs)
+	    unresolved_reloc = false;
+	  else if (resolved_dynly || (is_pic && resolved_local))
 	    {
 	      Elf_Internal_Rela outrel;
 
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index b8e335bf777..ebc2a526112 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -111,6 +111,31 @@ if [istarget "loongarch64-*-*"] {
 	pass "data plt"
       }
     }
+
+    run_ld_link_tests \
+	[list \
+	    [list \
+		"rodata-func-ptr-lib" \
+		"-shared" "" \
+		"" \
+		{rodata-func-ptr-lib.s} \
+		{} \
+		"rodata-func-ptr-lib.so"] \
+	]
+
+    if [file exist "tmpdir/rodata-func-ptr-lib.so"] {
+      run_ld_link_tests \
+	[list \
+	    [list \
+	    "rodata-func-ptr" \
+	    "-no-pie -z text -e 0 --section-start=.plt=0x120010000" \
+	    "tmpdir/rodata-func-ptr-lib.so" "" \
+	    {rodata-func-ptr.s} \
+	    {{readelf {-Wr --dyn-syms -x .rodata} rodata-func-ptr.d}} \
+	    "rodata-func-ptr"
+	    ]
+	]
+    }
   }
 
   run_ld_link_tests \
diff --git a/ld/testsuite/ld-loongarch-elf/rodata-func-ptr-lib.s b/ld/testsuite/ld-loongarch-elf/rodata-func-ptr-lib.s
new file mode 100644
index 00000000000..05654879db4
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/rodata-func-ptr-lib.s
@@ -0,0 +1,6 @@
+	.text
+	.globl	libfunc
+	.type	libfunc, @function
+libfunc:
+	jr	$ra
+	.size	libfunc, . - libfunc
diff --git a/ld/testsuite/ld-loongarch-elf/rodata-func-ptr.d b/ld/testsuite/ld-loongarch-elf/rodata-func-ptr.d
new file mode 100644
index 00000000000..9e9c96019ab
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/rodata-func-ptr.d
@@ -0,0 +1,11 @@
+# The sole PLT entry follows the 0x20-byte header at 0x120010000.
+# Both the dynamic symbol and the read-only pointer must hold its address.
+Relocation section '\.rela\.plt' at offset 0x[0-9a-f]+ contains 1 entry:
+ +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend
+[0-9a-f]+ +[0-9a-f]+ R_LARCH_JUMP_SLOT +0*120010020 libfunc \+ 0
+Symbol table '\.dynsym' contains [0-9]+ entries:
+#...
+ +[0-9]+: +0*120010020 +[0-9]+ FUNC +GLOBAL +DEFAULT +UND libfunc
+#...
+Hex dump of section '\.rodata':
+ +0x[0-9a-f]+ 20000120 01000000 +.*
diff --git a/ld/testsuite/ld-loongarch-elf/rodata-func-ptr.s b/ld/testsuite/ld-loongarch-elf/rodata-func-ptr.s
new file mode 100644
index 00000000000..0732c76c0e4
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/rodata-func-ptr.s
@@ -0,0 +1,4 @@
+	.section .rodata
+	.balign	8
+slot:
+	.dword	libfunc
-- 
2.20.1



More information about the Binutils mailing list