[binutils-gdb] LoongArch: Fix relocation of ignored undefined symbols
liu zhensong
liuzhensong@sourceware.org
Mon Nov 2 09:35:59 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=86afbefb5085cf6188ee0ddd274e45fbf9463a6e
commit 86afbefb5085cf6188ee0ddd274e45fbf9463a6e
Author: zhaozhou <zhaozhou@loongson.cn>
Date: Tue Oct 27 15:56:43 2026 +0800
LoongArch: Fix relocation of ignored undefined symbols
For LoongArch, an ignored undefined symbol that fails to resolve locally
is treated as dynamically resolved.
Enable --unresolved-symbols=ignore-all Branch and call relocations of
ignored undefined symbol could request a non-exist PLT entry thus
trigger a BFD assertion.
For call relocation, undefined ignored symbols without available PLT
entry as undefined weak symbols.
Diff:
---
bfd/elfnn-loongarch.c | 27 ++++++++++++++++------
.../ld-loongarch-elf/dynamic-branch-no-plt.d | 4 ++++
.../ld-loongarch-elf/dynamic-branch-no-plt.s | 8 +++++++
ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp | 2 ++
.../ld-loongarch-elf/unresolved-ignore-all.d | 13 +++++++++++
.../ld-loongarch-elf/unresolved-ignore-all.s | 7 ++++++
6 files changed, 54 insertions(+), 7 deletions(-)
diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 15f44420b4c..1c94748360f 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -3557,6 +3557,7 @@ loongarch_elf_relocate_section (struct bfd_link_info *info,
bfd_vma relocation, off, ie_off, desc_off;
int i, j;
bool resolve_pcrel_undef_weak = false;
+ bool ignored = false;
/* When an unrecognized relocation is encountered, which usually
occurs when using a newer assembler but an older linker, an error
@@ -3604,7 +3605,7 @@ loongarch_elf_relocate_section (struct bfd_link_info *info,
}
else
{
- bool warned, ignored;
+ bool warned;
RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
r_symndx, symtab_hdr, sym_hashes,
@@ -4282,7 +4283,8 @@ loongarch_elf_relocate_section (struct bfd_link_info *info,
bool via_plt =
plt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
- if (is_undefweak)
+ bool ignored_undef = (ignored && !via_plt);
+ if (is_undefweak || ignored_undef)
{
relocation = 0;
@@ -4321,6 +4323,12 @@ loongarch_elf_relocate_section (struct bfd_link_info *info,
}
}
+ /* B16 and B21 cannot be rewritten as an unconditional jump
+ without changing their semantics. Apply a zero displacement,
+ matching the existing undefined weak symbol behavior. */
+ if (ignored_undef && !via_plt)
+ break;
+
if (resolved_local)
{
relocation -= pc;
@@ -4328,18 +4336,23 @@ loongarch_elf_relocate_section (struct bfd_link_info *info,
}
else if (resolved_dynly)
{
- BFD_ASSERT (h
- && (h->plt.offset != MINUS_ONE
- || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
- && rel->r_addend == 0);
+ BFD_ASSERT (rel->r_addend == 0);
if (h && h->plt.offset == MINUS_ONE
&& ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
{
relocation -= pc;
relocation += rel->r_addend;
}
- else
+ else if (via_plt)
relocation = sec_addr (plt) + h->plt.offset - pc;
+ else
+ {
+ fatal = loongarch_reloc_is_fatal
+ (info, input_bfd, input_section, rel, howto,
+ bfd_reloc_notsupported, is_undefweak, name,
+ _("cannot resolve a dynamic branch without a PLT entry"));
+ break;
+ }
}
break;
diff --git a/ld/testsuite/ld-loongarch-elf/dynamic-branch-no-plt.d b/ld/testsuite/ld-loongarch-elf/dynamic-branch-no-plt.d
new file mode 100644
index 00000000000..9a7c5d8f92b
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/dynamic-branch-no-plt.d
@@ -0,0 +1,4 @@
+#source: dynamic-branch-no-plt.s
+#ld: -e 0 -Ttext=0x100 -shared
+#error: .*\(\.nonalloc\+0x0\): error: R_LARCH_B26 against `foo':\n
+#error: cannot resolve a dynamic branch without a PLT entry
diff --git a/ld/testsuite/ld-loongarch-elf/dynamic-branch-no-plt.s b/ld/testsuite/ld-loongarch-elf/dynamic-branch-no-plt.s
new file mode 100644
index 00000000000..091454b3b0c
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/dynamic-branch-no-plt.s
@@ -0,0 +1,8 @@
+ .text
+ .globl foo
+ .type foo, @function
+foo:
+ ret
+
+ .section .nonalloc,"",@progbits
+ bl foo
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index 7ea99f1d0cc..8d43c0802c1 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -54,6 +54,7 @@ if [istarget "loongarch64-*-*"] {
run_dump_test "relax-sym-size-1"
run_dump_test "relax-sym-size-2"
run_dump_test "check_got_relax"
+ run_dump_test "unresolved-ignore-all"
# Tidy up, to not leave huge intermediate files.
remote_file host delete tmpdir/relax-medium-call.o
@@ -229,6 +230,7 @@ if [istarget "loongarch64-*-*"] {
run_dump_test "weak-undef-hidden-shared"
run_dump_test "overflow_32_pcrel"
run_dump_test "underflow_32_pcrel"
+ run_dump_test "dynamic-branch-no-plt"
}
if [check_pie_support] {
diff --git a/ld/testsuite/ld-loongarch-elf/unresolved-ignore-all.d b/ld/testsuite/ld-loongarch-elf/unresolved-ignore-all.d
new file mode 100644
index 00000000000..fc0742c9c1f
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/unresolved-ignore-all.d
@@ -0,0 +1,13 @@
+#source: unresolved-ignore-all.s
+#ld: -e 0 -Ttext=0x100 --unresolved-symbols=ignore-all
+#objdump: -d -M no-aliases
+
+#...
+0000000000000100 <a>:
+[ ]+100:[ ]+58000085[ ]+beq[ ]+\$a0,[ ]+\$a1,[ ]+0[ ]+#[ ]+100[ ]+<a>
+[ ]+104:[ ]+40000080[ ]+beqz[ ]+\$a0,[ ]+0[ ]+#[ ]+104[ ]+<a\+0x4>
+[ ]+108:[ ]+4c000000[ ]+jirl[ ]+\$zero,[ ]+\$zero,[ ]+0
+[ ]+10c:[ ]+4c000001[ ]+jirl[ ]+\$ra,[ ]+\$zero,[ ]+0
+[ ]+110:[ ]+1e000001[ ]+pcaddu18i[ ]+\$ra,[ ]+0
+[ ]+114:[ ]+4c000001[ ]+jirl[ ]+\$ra,[ ]+\$zero,[ ]+0
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/unresolved-ignore-all.s b/ld/testsuite/ld-loongarch-elf/unresolved-ignore-all.s
new file mode 100644
index 00000000000..7bec5bbd336
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/unresolved-ignore-all.s
@@ -0,0 +1,7 @@
+.text
+a:
+ beq $a0, $a1, undef_func
+ beqz $a0, undef_func
+ b undef_func
+ bl undef_func
+ call undef_func
More information about the Binutils-cvs
mailing list