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

Harshit Kumar harshit7kr@gmail.com
Wed Nov 11 11:03:45 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 refers to it, elf_i386_relocate_section and
elf_x86_64_relocate_section find no entry and abort.  "gcc -g -O2"
produces this: once call () below loses its unused argument, only the
call-site DWARF still refers to f, and ld 2.47 aborts:

  static int impl(void){return 1;}
  static int (*resolve(void))(void){return impl;}
  static int f(void) __attribute__((ifunc("resolve")));
  __attribute__((noinline))
  static int call(int (*p)(void), int x){ return x; }
  int g(int x){ return call(f, x); }

Handle the symbol with _bfd_elf_ifunc_nonalloc_reloc, like a global one,
and use the existing "non-alloc section" error for the reject case.
Master aborts for a global symbol there too; give it the same error.

bfd/
	PR ld/34988
	* elf32-i386.c (elf_i386_relocate_section): Use
	_bfd_elf_ifunc_nonalloc_reloc for a local STT_GNU_IFUNC symbol
	with no hash entry.  Report a defined STT_GNU_IFUNC symbol in an
	unsupported non-alloc section.
	* elf64-x86-64.c (elf_x86_64_relocate_section): Likewise.

ld/
	PR ld/34988
	* testsuite/ld-i386/ifunc-global-plain-nonalloc.d: New test.
	* testsuite/ld-i386/ifunc-global-plain-nonalloc.s: New file.
	* testsuite/ld-i386/ifunc-note-nonalloc.d: New test.
	* testsuite/ld-i386/ifunc-note-nonalloc.s: New file.
	* testsuite/ld-i386/ifunc-plain-nonalloc.d: New test.
	* testsuite/ld-i386/ifunc-plain-nonalloc.s: New file.
	* testsuite/ld-i386/ifunc-skip-nonalloc.d: New test.
	* testsuite/ld-i386/ifunc-skip-nonalloc.s: New file.
	* testsuite/ld-i386/i386.exp: Run them.
	* testsuite/ld-x86-64/ifunc-global-plain-nonalloc.d: New test.
	* testsuite/ld-x86-64/ifunc-global-plain-nonalloc.s: New file.
	* testsuite/ld-x86-64/ifunc-note-nonalloc.d: New test.
	* testsuite/ld-x86-64/ifunc-note-nonalloc.s: New file.
	* testsuite/ld-x86-64/ifunc-plain-nonalloc.d: New test.
	* testsuite/ld-x86-64/ifunc-plain-nonalloc.s: New file.
	* testsuite/ld-x86-64/ifunc-skip-nonalloc.d: New test.
	* testsuite/ld-x86-64/ifunc-skip-nonalloc.s: New file.
	* testsuite/ld-x86-64/x86-64.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elf32-i386.c                              | 27 ++++++++++++++-----
 bfd/elf64-x86-64.c                            | 27 ++++++++++++++-----
 ld/testsuite/ld-i386/i386.exp                 |  4 +++
 .../ld-i386/ifunc-global-plain-nonalloc.d     |  4 +++
 .../ld-i386/ifunc-global-plain-nonalloc.s     | 12 +++++++++
 ld/testsuite/ld-i386/ifunc-note-nonalloc.d    |  8 ++++++
 ld/testsuite/ld-i386/ifunc-note-nonalloc.s    | 11 ++++++++
 ld/testsuite/ld-i386/ifunc-plain-nonalloc.d   |  4 +++
 ld/testsuite/ld-i386/ifunc-plain-nonalloc.s   | 11 ++++++++
 ld/testsuite/ld-i386/ifunc-skip-nonalloc.d    |  8 ++++++
 ld/testsuite/ld-i386/ifunc-skip-nonalloc.s    | 11 ++++++++
 .../ld-x86-64/ifunc-global-plain-nonalloc.d   |  4 +++
 .../ld-x86-64/ifunc-global-plain-nonalloc.s   | 12 +++++++++
 ld/testsuite/ld-x86-64/ifunc-note-nonalloc.d  |  8 ++++++
 ld/testsuite/ld-x86-64/ifunc-note-nonalloc.s  | 11 ++++++++
 ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.d |  4 +++
 ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.s | 11 ++++++++
 ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.d  |  8 ++++++
 ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.s  | 11 ++++++++
 ld/testsuite/ld-x86-64/x86-64.exp             |  4 +++
 20 files changed, 188 insertions(+), 12 deletions(-)
 create mode 100644 ld/testsuite/ld-i386/ifunc-global-plain-nonalloc.d
 create mode 100644 ld/testsuite/ld-i386/ifunc-global-plain-nonalloc.s
 create mode 100644 ld/testsuite/ld-i386/ifunc-note-nonalloc.d
 create mode 100644 ld/testsuite/ld-i386/ifunc-note-nonalloc.s
 create mode 100644 ld/testsuite/ld-i386/ifunc-plain-nonalloc.d
 create mode 100644 ld/testsuite/ld-i386/ifunc-plain-nonalloc.s
 create mode 100644 ld/testsuite/ld-i386/ifunc-skip-nonalloc.d
 create mode 100644 ld/testsuite/ld-i386/ifunc-skip-nonalloc.s
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-global-plain-nonalloc.d
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-global-plain-nonalloc.s
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-note-nonalloc.d
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-note-nonalloc.s
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.d
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.s
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.d
 create mode 100644 ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.s

diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index 1f7d9736bb..77c4a401b1 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -2274,11 +2274,26 @@ elf_i386_relocate_section (struct bfd_link_info *info,
 	      h = _bfd_elf_x86_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:
+		      goto non_alloc_error;
+		    }
+		}
+	      else
+		{
+		  /* Set STT_GNU_IFUNC symbol value.  */
+		  h->root.u.def.value = sym->st_value;
+		  h->root.u.def.section = sec;
+		}
 	    }
 	}
       else
@@ -2341,7 +2356,7 @@ elf_i386_relocate_section (struct bfd_link_info *info,
 		 thus ld.so will not process them.  */
 	      if ((input_section->flags & SEC_DEBUGGING) != 0)
 		continue;
-	      abort ();
+	      goto non_alloc_error;
 	    }
 
 	  /* STT_GNU_IFUNC symbol must go through PLT.  */
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 948cc2d48c..4979a874e6 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -3258,11 +3258,26 @@ elf_x86_64_relocate_section (struct bfd_link_info *info,
 	      h = _bfd_elf_x86_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:
+		      goto non_alloc_error;
+		    }
+		}
+	      else
+		{
+		  /* Set STT_GNU_IFUNC symbol value.  */
+		  h->root.u.def.value = sym->st_value;
+		  h->root.u.def.section = sec;
+		}
 	    }
 	}
       else
@@ -3346,7 +3361,7 @@ elf_x86_64_relocate_section (struct bfd_link_info *info,
 		 thus ld.so will not process them.  */
 	      if ((input_section->flags & SEC_DEBUGGING) != 0)
 		continue;
-	      abort ();
+	      goto non_alloc_error;
 	    }
 
 	  switch (r_type)
diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp
index 807bb8e9fc..7f7d349d66 100644
--- a/ld/testsuite/ld-i386/i386.exp
+++ b/ld/testsuite/ld-i386/i386.exp
@@ -445,6 +445,10 @@ run_dump_test "pr20253-4a"
 run_dump_test "pr20253-4b"
 run_dump_test "pr20253-4c"
 run_dump_test "pr20253-5"
+run_dump_test "ifunc-skip-nonalloc"
+run_dump_test "ifunc-note-nonalloc"
+run_dump_test "ifunc-plain-nonalloc"
+run_dump_test "ifunc-global-plain-nonalloc"
 run_dump_test "pr20515"
 run_dump_test "property-x86-3"
 run_dump_test "property-x86-4a"
diff --git a/ld/testsuite/ld-i386/ifunc-global-plain-nonalloc.d b/ld/testsuite/ld-i386/ifunc-global-plain-nonalloc.d
new file mode 100644
index 0000000000..70405458db
--- /dev/null
+++ b/ld/testsuite/ld-i386/ifunc-global-plain-nonalloc.d
@@ -0,0 +1,4 @@
+#name: global IFUNC symbol referenced from a plain non-alloc section
+#as: --32
+#ld: -e _start -melf_i386
+#error: .*relocation R_386_32 against symbol `ifn' in non-alloc section `.probe'.*
diff --git a/ld/testsuite/ld-i386/ifunc-global-plain-nonalloc.s b/ld/testsuite/ld-i386/ifunc-global-plain-nonalloc.s
new file mode 100644
index 0000000000..7bdaf56c92
--- /dev/null
+++ b/ld/testsuite/ld-i386/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
+	.long	ifn
diff --git a/ld/testsuite/ld-i386/ifunc-note-nonalloc.d b/ld/testsuite/ld-i386/ifunc-note-nonalloc.d
new file mode 100644
index 0000000000..0f8d10feb4
--- /dev/null
+++ b/ld/testsuite/ld-i386/ifunc-note-nonalloc.d
@@ -0,0 +1,8 @@
+#name: local IFUNC symbol referenced only from a non-alloc note section
+#as: --32
+#ld: -e _start -Ttext=0x10000 -melf_i386
+#objdump: -s -j .note.probe
+#...
+Contents of section .note.probe:
+ 0000 00000100 +.*
+#pass
diff --git a/ld/testsuite/ld-i386/ifunc-note-nonalloc.s b/ld/testsuite/ld-i386/ifunc-note-nonalloc.s
new file mode 100644
index 0000000000..2b818799f9
--- /dev/null
+++ b/ld/testsuite/ld-i386/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
+	.long	ifn
diff --git a/ld/testsuite/ld-i386/ifunc-plain-nonalloc.d b/ld/testsuite/ld-i386/ifunc-plain-nonalloc.d
new file mode 100644
index 0000000000..3020948ad8
--- /dev/null
+++ b/ld/testsuite/ld-i386/ifunc-plain-nonalloc.d
@@ -0,0 +1,4 @@
+#name: local IFUNC symbol referenced only from a plain non-alloc section
+#as: --32
+#ld: -e _start -melf_i386
+#error: .*relocation R_386_32 against symbol `ifn' in non-alloc section `.probe'.*
diff --git a/ld/testsuite/ld-i386/ifunc-plain-nonalloc.s b/ld/testsuite/ld-i386/ifunc-plain-nonalloc.s
new file mode 100644
index 0000000000..2d9c8be0ae
--- /dev/null
+++ b/ld/testsuite/ld-i386/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
+	.long	ifn
diff --git a/ld/testsuite/ld-i386/ifunc-skip-nonalloc.d b/ld/testsuite/ld-i386/ifunc-skip-nonalloc.d
new file mode 100644
index 0000000000..ef1f8e90e7
--- /dev/null
+++ b/ld/testsuite/ld-i386/ifunc-skip-nonalloc.d
@@ -0,0 +1,8 @@
+#name: local IFUNC symbol referenced only from a debugging section
+#as: --32
+#ld: -e _start -melf_i386
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 00000000 +.*
+#pass
diff --git a/ld/testsuite/ld-i386/ifunc-skip-nonalloc.s b/ld/testsuite/ld-i386/ifunc-skip-nonalloc.s
new file mode 100644
index 0000000000..26e6993ddf
--- /dev/null
+++ b/ld/testsuite/ld-i386/ifunc-skip-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
+	.long	ifn
diff --git a/ld/testsuite/ld-x86-64/ifunc-global-plain-nonalloc.d b/ld/testsuite/ld-x86-64/ifunc-global-plain-nonalloc.d
new file mode 100644
index 0000000000..7ca6d5f44d
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/ifunc-global-plain-nonalloc.d
@@ -0,0 +1,4 @@
+#name: global IFUNC symbol referenced from a plain non-alloc section
+#as: --64
+#ld: -e _start -melf_x86_64
+#error: .*relocation R_X86_64_64 against symbol `ifn' in non-alloc section `.probe'.*
diff --git a/ld/testsuite/ld-x86-64/ifunc-global-plain-nonalloc.s b/ld/testsuite/ld-x86-64/ifunc-global-plain-nonalloc.s
new file mode 100644
index 0000000000..960fae5cce
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/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
+	.quad	ifn
diff --git a/ld/testsuite/ld-x86-64/ifunc-note-nonalloc.d b/ld/testsuite/ld-x86-64/ifunc-note-nonalloc.d
new file mode 100644
index 0000000000..639b752278
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/ifunc-note-nonalloc.d
@@ -0,0 +1,8 @@
+#name: local IFUNC symbol referenced only from a non-alloc note section
+#as: --64
+#ld: -e _start -Ttext=0x10000 -melf_x86_64
+#objdump: -s -j .note.probe
+#...
+Contents of section .note.probe:
+ 0000 00000100 00000000 +.*
+#pass
diff --git a/ld/testsuite/ld-x86-64/ifunc-note-nonalloc.s b/ld/testsuite/ld-x86-64/ifunc-note-nonalloc.s
new file mode 100644
index 0000000000..b93da97ca8
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/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
+	.quad	ifn
diff --git a/ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.d b/ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.d
new file mode 100644
index 0000000000..db62919753
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.d
@@ -0,0 +1,4 @@
+#name: local IFUNC symbol referenced only from a plain non-alloc section
+#as: --64
+#ld: -e _start -melf_x86_64
+#error: .*relocation R_X86_64_64 against symbol `ifn' in non-alloc section `.probe'.*
diff --git a/ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.s b/ld/testsuite/ld-x86-64/ifunc-plain-nonalloc.s
new file mode 100644
index 0000000000..ad056cd1d0
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/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
+	.quad	ifn
diff --git a/ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.d b/ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.d
new file mode 100644
index 0000000000..bed5b6b25f
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.d
@@ -0,0 +1,8 @@
+#name: local IFUNC symbol referenced only from a debugging section
+#as: --64
+#ld: -e _start -melf_x86_64
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 00000000 00000000 +.*
+#pass
diff --git a/ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.s b/ld/testsuite/ld-x86-64/ifunc-skip-nonalloc.s
new file mode 100644
index 0000000000..d6c3cf0d59
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/ifunc-skip-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
+	.quad	ifn
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index 985d96dcfb..cf47469e90 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -376,6 +376,10 @@ run_dump_test "pr19807-2e"
 run_dump_test "pr19969"
 run_dump_test "pr20093-1"
 run_dump_test "pr20093-2"
+run_dump_test "ifunc-skip-nonalloc"
+run_dump_test "ifunc-note-nonalloc"
+run_dump_test "ifunc-plain-nonalloc"
+run_dump_test "ifunc-global-plain-nonalloc"
 run_dump_test "property-x86-3"
 run_dump_test "property-x86-4a"
 run_dump_test "property-x86-4b"
-- 
2.55.0




More information about the Binutils mailing list