[PATCH 4/7] riscv: handle a local IFUNC referenced only from a non-alloc section
Harshit Kumar
harshit7kr@gmail.com
Wed Nov 11 11:03:47 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. Handle it with
_bfd_elf_ifunc_nonalloc_reloc, like a global symbol, using the existing
"isn't supported" error in the reject case. Master aborts for a global
symbol there too; send it to the same error.
bfd/
PR ld/34988
* elfnn-riscv.c (riscv_elf_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.
ld/
PR ld/34988
* 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/ifunc-skip-nonalloc.d: New test.
* testsuite/ld-riscv-elf/ifunc-skip-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 | 34 +++++++++++++++----
.../ifunc-global-plain-nonalloc.d | 3 ++
.../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 | 3 ++
.../ld-riscv-elf/ifunc-plain-nonalloc.s | 11 ++++++
.../ld-riscv-elf/ifunc-skip-nonalloc.d | 7 ++++
.../ld-riscv-elf/ifunc-skip-nonalloc.s | 11 ++++++
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 4 +++
10 files changed, 97 insertions(+), 6 deletions(-)
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
create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-skip-nonalloc.d
create mode 100644 ld/testsuite/ld-riscv-elf/ifunc-skip-nonalloc.s
diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 17c714ab2c..08e5258b5e 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -3105,11 +3105,33 @@ 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 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: relocation %s against STT_GNU_IFUNC "
+ "symbol `%s' isn't supported"), input_bfd,
+ howto->name,
+ bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL));
+ bfd_set_error (bfd_error_bad_value);
+ goto out;
+ }
+ }
+ else
+ {
+ /* Set STT_GNU_IFUNC symbol value. */
+ h->root.u.def.value = sym->st_value;
+ h->root.u.def.section = sec;
+ }
}
}
else
@@ -3161,7 +3183,7 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
if ((input_section->flags & SEC_DEBUGGING) != 0)
continue;
- abort ();
+ goto bad_ifunc_reloc;
}
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-global-plain-nonalloc.d b/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.d
new file mode 100644
index 0000000000..52a0b056f1
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-global-plain-nonalloc.d
@@ -0,0 +1,3 @@
+#name: global IFUNC symbol referenced from a plain non-alloc section
+#ld: -e _start
+#error: .*relocation R_RISCV_64 against STT_GNU_IFUNC symbol `ifn' isn't supported.*
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..65b1e05c4e
--- /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: -e _start -Ttext=0x10000
+#objdump: -s -j .note.probe
+#...
+Contents of section .note.probe:
+ 0000 00000100 00000000 +.*
+#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..b1499aee95
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-plain-nonalloc.d
@@ -0,0 +1,3 @@
+#name: local IFUNC symbol referenced only from a plain non-alloc section
+#ld: -e _start
+#error: .*relocation R_RISCV_64 against STT_GNU_IFUNC symbol `ifn' isn't supported.*
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/ifunc-skip-nonalloc.d b/ld/testsuite/ld-riscv-elf/ifunc-skip-nonalloc.d
new file mode 100644
index 0000000000..acc9347bf5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/ifunc-skip-nonalloc.d
@@ -0,0 +1,7 @@
+#name: local IFUNC symbol referenced only from a debugging section
+#ld: -e _start
+#objdump: -s -j .debug_info
+#...
+Contents of section .debug_info:
+ 0000 00000000 00000000 +.*
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/ifunc-skip-nonalloc.s b/ld/testsuite/ld-riscv-elf/ifunc-skip-nonalloc.s
new file mode 100644
index 0000000000..1a85903dad
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/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
+ .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..68336f007a 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -391,6 +391,10 @@ 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-skip-nonalloc"
+ run_dump_test "ifunc-note-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