[binutils-gdb] LoongArch: Avoid signed overflow when dumping relocation addends

liu zhensong liuzhensong@sourceware.org
Fri Nov 13 07:33:49 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8934d26068eeb344e81073efe498f0b3b5427dd7

commit 8934d26068eeb344e81073efe498f0b3b5427dd7
Author: zhaozhou <zhaozhou@loongson.cn>
Date:   Thu Oct 29 14:55:20 2026 +0800

    LoongArch: Avoid signed overflow when dumping relocation addends
    
    loongarch_dump_reloc_record negates a negative long addend to print.
    
    When the addend is INT64_MIN, the negation overflows and trigger
    undefined behavior, using bfd_vma subtraction to compute the absolute
    value of addend and print it with PRIu64.
    
    This also handles INT64_MIN without signed overflow and preserves the
    output for other addends.
    
            PR ld/34808
    bfd/
            * elfnn-loongarch.c (loongarch_dump_reloc_record): Using
            bfd_vma subtraction to compute the absolute value of addend and
            print it with PRIu64.

Diff:
---
 bfd/elfnn-loongarch.c                              | 10 +++++++---
 ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp |  1 +
 ld/testsuite/ld-loongarch-elf/reloc-addend.d       |  3 +++
 ld/testsuite/ld-loongarch-elf/reloc-addend.l       |  3 +++
 ld/testsuite/ld-loongarch-elf/reloc-addend.s       |  5 +++++
 5 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index f19049d59b3..beb7e0ace46 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -3326,11 +3326,15 @@ loongarch_dump_reloc_record (void (*p) (const char *fmt, ...))
 	 loongarch_sym_name (larch_reloc_queue[i].bfd, larch_reloc_queue[i].h,
 			     larch_reloc_queue[i].sym));
 
-      long addend = larch_reloc_queue[i].addend;
+      bfd_signed_vma addend = larch_reloc_queue[i].addend;
       if (addend < 0)
-	p (" - %ld", -addend);
+	{
+	  bfd_vma addend_u = (bfd_vma) 0 - (bfd_vma) addend;
+	  p (" - %" PRIu64, (uint64_t) addend_u);
+	}
       else if (0 < addend)
-	p (" + %ld(0x%v)", addend, larch_reloc_queue[i].addend);
+	p (" + %" PRId64 "(0x%v)", (int64_t) addend,
+	   larch_reloc_queue[i].addend);
 
       p ("\n");
       i = (i + 1) % LARCH_RECENT_RELOC_QUEUE_LENGTH;
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index 8d43c0802c1..e26774eddc5 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -29,6 +29,7 @@ if [istarget "loongarch64-*-*"] {
     run_dump_test "disas-jirl"
     run_dump_test "local-ifunc-reloc"
     run_dump_test "anno-sym"
+    run_dump_test "reloc-addend"
     run_dump_test "pcala64"
     run_dump_test "overflow_s_10_5"
     run_dump_test "overflow_s_10_12"
diff --git a/ld/testsuite/ld-loongarch-elf/reloc-addend.d b/ld/testsuite/ld-loongarch-elf/reloc-addend.d
new file mode 100644
index 00000000000..659b3569c30
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc-addend.d
@@ -0,0 +1,3 @@
+#as: -mabi=lp64d -mno-relax
+#ld: -e _start
+#error_output: reloc-addend.l
diff --git a/ld/testsuite/ld-loongarch-elf/reloc-addend.l b/ld/testsuite/ld-loongarch-elf/reloc-addend.l
new file mode 100644
index 00000000000..8dd00ba60be
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc-addend.l
@@ -0,0 +1,3 @@
+#...
+0x[0-9a-f]+ R_LARCH_SOP_SR[ 	]+`_start' - 9223372036854775808
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/reloc-addend.s b/ld/testsuite/ld-loongarch-elf/reloc-addend.s
new file mode 100644
index 00000000000..a8dbec58936
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc-addend.s
@@ -0,0 +1,5 @@
+  .text
+  .globl _start
+_start:
+  nop
+  .reloc _start, R_LARCH_SOP_SR, _start - 0x8000000000000000


More information about the Binutils-cvs mailing list