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

Harshit Kumar harshit7kr@gmail.com
Sat Nov 14 09:38:27 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,
_bfd_sparc_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
	* elfxx-sparc.c (_bfd_sparc_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-sparc/ifunc-debug-nonalloc.d: New test.
	* testsuite/ld-sparc/ifunc-debug-nonalloc.s: New file.
	* testsuite/ld-sparc/ifunc-global-debug-nonalloc.d: New test.
	* testsuite/ld-sparc/ifunc-global-debug-nonalloc.s: New file.
	* testsuite/ld-sparc/ifunc-note-nonalloc.d: New test.
	* testsuite/ld-sparc/ifunc-note-nonalloc.s: New file.
	* testsuite/ld-sparc/ifunc-plain-nonalloc-data.d: New test.
	* testsuite/ld-sparc/ifunc-plain-nonalloc-data.s: New file.
	* testsuite/ld-sparc/ifunc-plain-nonalloc.d: New test.
	* testsuite/ld-sparc/ifunc-plain-nonalloc.s: New file.
	* testsuite/ld-sparc/sparc.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfxx-sparc.c                             | 32 ++++++++++---------
 ld/testsuite/ld-sparc/ifunc-debug-nonalloc.d  |  9 ++++++
 ld/testsuite/ld-sparc/ifunc-debug-nonalloc.s  | 13 ++++++++
 .../ld-sparc/ifunc-global-debug-nonalloc.d    |  9 ++++++
 .../ld-sparc/ifunc-global-debug-nonalloc.s    | 16 ++++++++++
 ld/testsuite/ld-sparc/ifunc-note-nonalloc.d   |  9 ++++++
 ld/testsuite/ld-sparc/ifunc-note-nonalloc.s   | 13 ++++++++
 .../ld-sparc/ifunc-plain-nonalloc-data.d      |  9 ++++++
 .../ld-sparc/ifunc-plain-nonalloc-data.s      | 15 +++++++++
 ld/testsuite/ld-sparc/ifunc-plain-nonalloc.d  |  9 ++++++
 ld/testsuite/ld-sparc/ifunc-plain-nonalloc.s  | 13 ++++++++
 ld/testsuite/ld-sparc/sparc.exp               |  9 ++++++
 12 files changed, 141 insertions(+), 15 deletions(-)
 create mode 100644 ld/testsuite/ld-sparc/ifunc-debug-nonalloc.d
 create mode 100644 ld/testsuite/ld-sparc/ifunc-debug-nonalloc.s
 create mode 100644 ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.d
 create mode 100644 ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.s
 create mode 100644 ld/testsuite/ld-sparc/ifunc-note-nonalloc.d
 create mode 100644 ld/testsuite/ld-sparc/ifunc-note-nonalloc.s
 create mode 100644 ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.d
 create mode 100644 ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.s
 create mode 100644 ld/testsuite/ld-sparc/ifunc-plain-nonalloc.d
 create mode 100644 ld/testsuite/ld-sparc/ifunc-plain-nonalloc.s

diff --git a/bfd/elfxx-sparc.c b/bfd/elfxx-sparc.c
index 694ae638c1..35c40fd1c9 100644
--- a/bfd/elfxx-sparc.c
+++ b/bfd/elfxx-sparc.c
@@ -2851,11 +2851,19 @@ _bfd_sparc_elf_relocate_section (struct bfd_link_info *info,
 	      h = elf_sparc_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
@@ -2896,17 +2904,11 @@ _bfd_sparc_elf_relocate_section (struct bfd_link_info *info,
 	  if ((input_section->flags & SEC_ALLOC) == 0
 	      || h->plt.offset == (bfd_vma) -1)
 	    {
-	      /* 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.  */
+	      /* In a non-alloc section, which is not loaded, or a SHT_NOTE
+		 section, treat STT_GNU_IFUNC symbol as STT_FUNC.  */
 	      if ((input_section->flags & SEC_ALLOC) == 0
-		  && (input_section->flags & SEC_DEBUGGING) != 0)
-		continue;
+		  || elf_section_type (input_section) == SHT_NOTE)
+		goto skip_ifunc;
 
 	      _bfd_error_handler
 		/* xgettext:c-format */
diff --git a/ld/testsuite/ld-sparc/ifunc-debug-nonalloc.d b/ld/testsuite/ld-sparc/ifunc-debug-nonalloc.d
new file mode 100644
index 0000000000..07ac583ba7
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-debug-nonalloc.d
@@ -0,0 +1,9 @@
+#name: local IFUNC symbol referenced only from a debugging section
+#as: -64 -Av9
+#target: [check_shared_lib_support]
+#ld: -Ttext=0x10000 -melf64_sparc
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 00000000 00010000 +.*
+#pass
diff --git a/ld/testsuite/ld-sparc/ifunc-debug-nonalloc.s b/ld/testsuite/ld-sparc/ifunc-debug-nonalloc.s
new file mode 100644
index 0000000000..c76082bb42
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-debug-nonalloc.s
@@ -0,0 +1,13 @@
+	.text
+	.type	resolver, #function
+resolver:
+	retl
+	 nop
+	.type	ifn, #gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	retl
+	 nop
+	.section .debug_info,"",@progbits
+	.xword	ifn
diff --git a/ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.d b/ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.d
new file mode 100644
index 0000000000..d90bdc91d2
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.d
@@ -0,0 +1,9 @@
+#name: global IFUNC symbol referenced from a debugging section
+#as: -64 -Av9
+#target: [check_shared_lib_support]
+#ld: -Ttext=0x10000 -melf64_sparc
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 00000000 00010000 +.*
+#pass
diff --git a/ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.s b/ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.s
new file mode 100644
index 0000000000..cfaefd713a
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-global-debug-nonalloc.s
@@ -0,0 +1,16 @@
+	.text
+	.type	resolver, #function
+resolver:
+	retl
+	 nop
+	.globl	ifn
+	.type	ifn, #gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	call	ifn
+	 nop
+	retl
+	 nop
+	.section .debug_info,"",@progbits
+	.xword	ifn
diff --git a/ld/testsuite/ld-sparc/ifunc-note-nonalloc.d b/ld/testsuite/ld-sparc/ifunc-note-nonalloc.d
new file mode 100644
index 0000000000..d567b251ee
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-note-nonalloc.d
@@ -0,0 +1,9 @@
+#name: local IFUNC symbol referenced only from a non-alloc note section
+#as: -64 -Av9
+#target: [check_shared_lib_support]
+#ld: -Ttext=0x10000 -melf64_sparc
+#objdump: -s -j .note.probe
+#...
+Contents of section .note.probe:
+ 0000 00000000 00010000 +.*
+#pass
diff --git a/ld/testsuite/ld-sparc/ifunc-note-nonalloc.s b/ld/testsuite/ld-sparc/ifunc-note-nonalloc.s
new file mode 100644
index 0000000000..9008fa9945
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-note-nonalloc.s
@@ -0,0 +1,13 @@
+	.text
+	.type	resolver, #function
+resolver:
+	retl
+	 nop
+	.type	ifn, #gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	retl
+	 nop
+	.section .note.probe,"",@note
+	.xword	ifn
diff --git a/ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.d b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.d
new file mode 100644
index 0000000000..59766e0330
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.d
@@ -0,0 +1,9 @@
+#name: local IFUNC symbol referenced from .data and a plain non-alloc section
+#as: -64 -Av9
+#target: [check_shared_lib_support]
+#ld: -Ttext=0x10000 -shared -melf64_sparc
+#objdump: -s -j .probe
+#...
+Contents of section .probe:
+ 0000 00000000 00010000 +.*
+#pass
diff --git a/ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.s b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.s
new file mode 100644
index 0000000000..6b1efbfe8e
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.s
@@ -0,0 +1,15 @@
+	.text
+	.type	resolver, #function
+resolver:
+	retl
+	 nop
+	.type	ifn, #gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	retl
+	 nop
+	.data
+	.xword	ifn
+	.section .probe,"",@progbits
+	.xword	ifn
diff --git a/ld/testsuite/ld-sparc/ifunc-plain-nonalloc.d b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc.d
new file mode 100644
index 0000000000..6d0bcacc83
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc.d
@@ -0,0 +1,9 @@
+#name: local IFUNC symbol referenced only from a plain non-alloc section
+#as: -64 -Av9
+#target: [check_shared_lib_support]
+#ld: -Ttext=0x10000 -melf64_sparc
+#objdump: -s -j .probe
+#...
+Contents of section .probe:
+ 0000 00000000 00010000 +.*
+#pass
diff --git a/ld/testsuite/ld-sparc/ifunc-plain-nonalloc.s b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc.s
new file mode 100644
index 0000000000..2589b272f3
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc.s
@@ -0,0 +1,13 @@
+	.text
+	.type	resolver, #function
+resolver:
+	retl
+	 nop
+	.type	ifn, #gnu_indirect_function
+	.set	ifn, resolver
+	.globl	_start
+_start:
+	retl
+	 nop
+	.section .probe,"",@progbits
+	.xword	ifn
diff --git a/ld/testsuite/ld-sparc/sparc.exp b/ld/testsuite/ld-sparc/sparc.exp
index 4f6f0cf895..f71de80622 100644
--- a/ld/testsuite/ld-sparc/sparc.exp
+++ b/ld/testsuite/ld-sparc/sparc.exp
@@ -158,6 +158,15 @@ if { ![istarget "sparc-*-elf*"] } {
     run_ld_link_tests $sparc64tests
 }
 
+if { ([istarget "sparc64-*-*"] || [istarget "sparcv9-*-*"])
+     && [supports_gnu_osabi] } {
+    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-plain-nonalloc-data"
+}
+
 if { [istarget "sparc*-*-linux*"] && [isnative] } {
     run_ld_link_exec_tests [list \
 	[list \
-- 
2.55.0




More information about the Binutils mailing list