[PATCH v2 3/6] riscv: relocate IFUNC references from non-alloc sections as STT_FUNC

Harshit Kumar harshit7kr@gmail.com
Sat Nov 14 09:38:26 GMT 2026


A local STT_GNU_IFUNC symbol gets its hash entry from the relocation
scan, which skips non-SEC_ALLOC sections (PR ld/26080).  When only such
a section, for example .debug_info, refers to it,
riscv_elf_relocate_section finds no entry and aborts.

A non-alloc section is not loaded and ld.so never processes it, so
relocate a reference from any non-alloc section, debugging and note
sections included, as against STT_FUNC: it gets the address of the
resolver.  This replaces the separate SHT_NOTE (PR ld/22929) and
SEC_DEBUGGING (PR ld/18808) cases for a global symbol, which left a
debugging section's field unrelocated (0, or the bare addend, in
.debug_info) and rejected or aborted on other non-alloc sections.

bfd/
	PR ld/34988
	* elfnn-riscv.c (riscv_elf_relocate_section): Relocate a reference
	from a non-alloc section to an STT_GNU_IFUNC symbol, local without a
	hash entry or defined, as against STT_FUNC.
ld/
	PR ld/34988
	* testsuite/ld-riscv-elf/ifunc-debug-nonalloc.d: New test.
	* testsuite/ld-riscv-elf/ifunc-debug-nonalloc.s: New file.
	* testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.d: New test.
	* testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.s: New file.
	* testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.d: New test.
	* testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.s: New file.
	* testsuite/ld-riscv-elf/ifunc-note-nonalloc.d: New test.
	* testsuite/ld-riscv-elf/ifunc-note-nonalloc.s: New file.
	* testsuite/ld-riscv-elf/ifunc-plain-nonalloc.d: New test.
	* testsuite/ld-riscv-elf/ifunc-plain-nonalloc.s: New file.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfnn-riscv.c                             | 33 +++++++++----------
 .../ld-riscv-elf/ifunc-debug-nonalloc.d       |  7 ++++
 .../ld-riscv-elf/ifunc-debug-nonalloc.s       | 11 +++++++
 .../ifunc-global-debug-nonalloc.d             |  7 ++++
 .../ifunc-global-debug-nonalloc.s             | 13 ++++++++
 .../ifunc-global-plain-nonalloc.d             |  7 ++++
 .../ifunc-global-plain-nonalloc.s             | 12 +++++++
 .../ld-riscv-elf/ifunc-note-nonalloc.d        |  7 ++++
 .../ld-riscv-elf/ifunc-note-nonalloc.s        | 11 +++++++
 .../ld-riscv-elf/ifunc-plain-nonalloc.d       |  7 ++++
 .../ld-riscv-elf/ifunc-plain-nonalloc.s       | 11 +++++++
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp    |  5 +++
 12 files changed, 114 insertions(+), 17 deletions(-)
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.s
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.s
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.s
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.s
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.s

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 17c714ab2c..d1592ff1df 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -3105,11 +3105,19 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
 	    {
 	      h = riscv_elf_get_local_sym_hash (htab, input_bfd, rel, false);
 	      if (h == NULL)
-		abort ();
-
-	      /* Set STT_GNU_IFUNC symbol value.  */
-	      h->root.u.def.value = sym->st_value;
-	      h->root.u.def.section = sec;
+		{
+		  /* Only a non-alloc section, which the relocation scan
+		     skips, refers to the symbol.  Relocate it as against
+		     STT_FUNC.  */
+		  if ((input_section->flags & SEC_ALLOC) != 0)
+		    abort ();
+		}
+	      else
+		{
+		  /* Set STT_GNU_IFUNC symbol value.  */
+		  h->root.u.def.value = sym->st_value;
+		  h->root.u.def.section = sec;
+		}
 	    }
 	}
       else
@@ -3150,18 +3158,9 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
 
 	  if ((input_section->flags & SEC_ALLOC) == 0)
 	    {
-	      /* If this is a SHT_NOTE section without SHF_ALLOC, treat
-		 STT_GNU_IFUNC symbol as STT_FUNC.  */
-	      if (elf_section_type (input_section) == SHT_NOTE)
-		goto skip_ifunc;
-
-	      /* Dynamic relocs are not propagated for SEC_DEBUGGING
-		 sections because such sections are not SEC_ALLOC and
-		 thus ld.so will not process them.  */
-	      if ((input_section->flags & SEC_DEBUGGING) != 0)
-		continue;
-
-	      abort ();
+	      /* A non-alloc section is not loaded, so relocate a
+		 reference from it as against STT_FUNC.  */
+	      goto skip_ifunc;
 	    }
 	  else if (h->plt.offset == (bfd_vma) -1
 		   /* The following relocation may not need the .plt entries
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.d b/ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.d
new file mode 100644
index 0000000000..6544172bf5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.d
@@ -0,0 +1,7 @@
+#name: local IFUNC symbol referenced only from a debugging section
+#ld: -Ttext=0x10000
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 (00000100 00000000|00000000 00010000) +.*
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.s b/ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.s
new file mode 100644
index 0000000000..1a85903dad
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-debug-nonalloc.s
@@ -0,0 +1,11 @@
+	.text
+	.type	resolver, %function
+resolver:
+	ret
+	.type	ifn, %gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	ret
+	.section .debug_info,"",%progbits
+	.dword	ifn
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.d b/ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.d
new file mode 100644
index 0000000000..3e3cca9c1b
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.d
@@ -0,0 +1,7 @@
+#name: global IFUNC symbol referenced from a debugging section
+#ld: -Ttext=0x10000
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 (00000100 00000000|00000000 00010000) +.*
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.s b/ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.s
new file mode 100644
index 0000000000..4be647bde8
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-global-debug-nonalloc.s
@@ -0,0 +1,13 @@
+	.text
+	.type	resolver, %function
+resolver:
+	ret
+	.globl	ifn
+	.type	ifn, %gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	call	ifn
+	ret
+	.section .debug_info,"",%progbits
+	.dword	ifn
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.d b/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.d
new file mode 100644
index 0000000000..1489fc7685
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.d
@@ -0,0 +1,7 @@
+#name: global IFUNC symbol referenced from a plain non-alloc section
+#ld: -Ttext=0x10000
+#objdump: -s -j .probe
+#...
+Contents of section .probe:
+ 0000 (00000100 00000000|00000000 00010000) +.*
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.s b/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.s
new file mode 100644
index 0000000000..4ac426ca95
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.s
@@ -0,0 +1,12 @@
+	.text
+	.type	resolver, %function
+resolver:
+	ret
+	.globl	ifn
+	.type	ifn, %gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	ret
+	.section .probe,"",%progbits
+	.dword	ifn
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.d b/ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.d
new file mode 100644
index 0000000000..de6c1d9830
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.d
@@ -0,0 +1,7 @@
+#name: local IFUNC symbol referenced only from a non-alloc note section
+#ld: -Ttext=0x10000
+#objdump: -s -j .note.probe
+#...
+Contents of section .note.probe:
+ 0000 (00000100 00000000|00000000 00010000) +.*
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.s b/ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.s
new file mode 100644
index 0000000000..66744554db
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-note-nonalloc.s
@@ -0,0 +1,11 @@
+	.text
+	.type	resolver, %function
+resolver:
+	ret
+	.type	ifn, %gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	ret
+	.section .note.probe,"",%note
+	.dword	ifn
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.d b/ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.d
new file mode 100644
index 0000000000..99a985d8a5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.d
@@ -0,0 +1,7 @@
+#name: local IFUNC symbol referenced only from a plain non-alloc section
+#ld: -Ttext=0x10000
+#objdump: -s -j .probe
+#...
+Contents of section .probe:
+ 0000 (00000100 00000000|00000000 00010000) +.*
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.s b/ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.s
new file mode 100644
index 0000000000..6711455f0c
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.s
@@ -0,0 +1,11 @@
+	.text
+	.type	resolver, %function
+resolver:
+	ret
+	.type	ifn, %gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	ret
+	.section .probe,"",%progbits
+	.dword	ifn
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index ad12f8d63b..b3c66fc563 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -391,6 +391,11 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "ifunc-seperate-plt-pic"
     run_dump_test "ifunc-seperate-pcrel-pie"
     run_dump_test "ifunc-seperate-pcrel-pic"
+    run_dump_test "ifunc-debug-nonalloc"
+    run_dump_test "ifunc-note-nonalloc"
+    run_dump_test "ifunc-global-debug-nonalloc"
+    run_dump_test "ifunc-plain-nonalloc"
+    run_dump_test "ifunc-global-plain-nonalloc"
 
     # Tests related to mixing medany code into position-independent targets,
     # where it's not always possible to generate correct addressing sequences.
-- 
2.55.0




More information about the Binutils mailing list