[PATCH 5/7] sparc: handle a local IFUNC referenced only from a non-alloc section

Harshit Kumar harshit7kr@gmail.com
Wed Nov 11 11:03:48 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.  Handle it
with _bfd_elf_ifunc_nonalloc_reloc, like a global symbol, giving the
same "unresolvable ... relocation" error in the reject case.  That error
also printed "(null)" for a local symbol; print its name.

bfd/
	PR ld/34988
	* elfxx-sparc.c (_bfd_sparc_elf_relocate_section): Use
	_bfd_elf_ifunc_nonalloc_reloc for a local STT_GNU_IFUNC symbol
	with no hash entry.  Name a local STT_GNU_IFUNC symbol in the
	unresolvable relocation error.

ld/
	PR ld/34988
	* 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/ifunc-skip-nonalloc.d: New test.
	* testsuite/ld-sparc/ifunc-skip-nonalloc.s: New file.
	* testsuite/ld-sparc/sparc.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfxx-sparc.c                             | 37 ++++++++++++++++---
 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      |  5 +++
 .../ld-sparc/ifunc-plain-nonalloc-data.s      | 15 ++++++++
 ld/testsuite/ld-sparc/ifunc-plain-nonalloc.d  |  5 +++
 ld/testsuite/ld-sparc/ifunc-plain-nonalloc.s  | 13 +++++++
 ld/testsuite/ld-sparc/ifunc-skip-nonalloc.d   |  9 +++++
 ld/testsuite/ld-sparc/ifunc-skip-nonalloc.s   | 13 +++++++
 ld/testsuite/ld-sparc/sparc.exp               |  7 ++++
 10 files changed, 120 insertions(+), 6 deletions(-)
 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
 create mode 100644 ld/testsuite/ld-sparc/ifunc-skip-nonalloc.d
 create mode 100644 ld/testsuite/ld-sparc/ifunc-skip-nonalloc.s

diff --git a/bfd/elfxx-sparc.c b/bfd/elfxx-sparc.c
index 694ae638c1..b2d4ef57c9 100644
--- a/bfd/elfxx-sparc.c
+++ b/bfd/elfxx-sparc.c
@@ -2851,11 +2851,34 @@ _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 section the scan skipped refers to the symbol.  */
+		  if ((input_section->flags & SEC_ALLOC) != 0)
+		    abort ();
+		  switch (_bfd_elf_ifunc_nonalloc_reloc (input_section))
+		    {
+		    case elf_ifunc_nonalloc_func:
+		      break;
+		    case elf_ifunc_nonalloc_skip:
+		      continue;
+		    case elf_ifunc_nonalloc_reject:
+		      _bfd_error_handler
+			/* xgettext:c-format */
+			(_("%pB(%pA+%#" PRIx64 "): "
+			   "unresolvable %s relocation against symbol `%s'"),
+			 input_bfd, input_section, (uint64_t) rel->r_offset,
+			 howto->name,
+			 bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL));
+		      bfd_set_error (bfd_error_bad_value);
+		      return false;
+		    }
+		}
+	      else
+		{
+		  /* Set STT_GNU_IFUNC symbol value.  */
+		  h->root.u.def.value = sym->st_value;
+		  h->root.u.def.section = sec;
+		}
 	    }
 	}
       else
@@ -2916,7 +2939,9 @@ _bfd_sparc_elf_relocate_section (struct bfd_link_info *info,
 		 input_section,
 		 (uint64_t) rel->r_offset,
 		 howto->name,
-		 h->root.root.string);
+		 h->root.root.string != NULL
+		 ? h->root.root.string
+		 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL));
 	      bfd_set_error (bfd_error_bad_value);
 	      return false;
 	    }
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..f3aeb19f25
--- /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: -e _start -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..c31cd6fac3
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc-data.d
@@ -0,0 +1,5 @@
+#name: local IFUNC symbol referenced from .data and a plain non-alloc section
+#as: -64 -Av9
+#target: [check_shared_lib_support]
+#ld: -shared -melf64_sparc
+#error: .*unresolvable R_SPARC_64 relocation against symbol `ifn'.*
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..b55499bbab
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-plain-nonalloc.d
@@ -0,0 +1,5 @@
+#name: local IFUNC symbol referenced only from a plain non-alloc section
+#as: -64 -Av9
+#target: [check_shared_lib_support]
+#ld: -e _start -melf64_sparc
+#error: .*unresolvable R_SPARC_64 relocation against symbol `ifn'.*
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/ifunc-skip-nonalloc.d b/ld/testsuite/ld-sparc/ifunc-skip-nonalloc.d
new file mode 100644
index 0000000000..a66d50128a
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-skip-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: -e _start -melf64_sparc
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 00000000 00000000 +.*
+#pass
diff --git a/ld/testsuite/ld-sparc/ifunc-skip-nonalloc.s b/ld/testsuite/ld-sparc/ifunc-skip-nonalloc.s
new file mode 100644
index 0000000000..c76082bb42
--- /dev/null
+++ b/ld/testsuite/ld-sparc/ifunc-skip-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/sparc.exp b/ld/testsuite/ld-sparc/sparc.exp
index 4f6f0cf895..3a0b2165cd 100644
--- a/ld/testsuite/ld-sparc/sparc.exp
+++ b/ld/testsuite/ld-sparc/sparc.exp
@@ -158,6 +158,13 @@ if { ![istarget "sparc-*-elf*"] } {
     run_ld_link_tests $sparc64tests
 }
 
+if { [istarget "sparc64-*-*"] || [istarget "sparcv9-*-*"] } {
+    run_dump_test "ifunc-skip-nonalloc"
+    run_dump_test "ifunc-note-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