[v2] LoongArch: Avoid signed overflow when dumping relocation addends

Message ID 20261029065520.57394-1-zhaozhou@loongson.cn
State New
Headers
Series [v2] LoongArch: Avoid signed overflow when dumping relocation addends |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed

Commit Message

zhaozhou Oct. 29, 2026, 6:55 a.m. UTC
  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.

---
This Patch update:
1. adjust data type.
---
 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(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-addend.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-addend.l
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-addend.s
  

Patch

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 15f44420b4c..aecda28ebf7 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 7ea99f1d0cc..265438372a7 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