[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
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
@@ -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;
@@ -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"
new file mode 100644
@@ -0,0 +1,3 @@
+#as: -mabi=lp64d -mno-relax
+#ld: -e _start
+#error_output: reloc-addend.l
new file mode 100644
@@ -0,0 +1,3 @@
+#...
+0x[0-9a-f]+ R_LARCH_SOP_SR[ ]+`_start' - 9223372036854775808
+#pass
new file mode 100644
@@ -0,0 +1,5 @@
+ .text
+ .globl _start
+_start:
+ nop
+ .reloc _start, R_LARCH_SOP_SR, _start - 0x8000000000000000