[lld] [lld][Hexagon] Fix R_HEX_B22_PCREL range checks (PR #115925)
Alexey Karyakin via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 18 18:49:44 PST 2024
https://github.com/quic-akaryaki updated https://github.com/llvm/llvm-project/pull/115925
>From 3b2f81c2ab5907b9640a5d8073d9acd8a6cda55f Mon Sep 17 00:00:00 2001
From: Alexey Karyakin <akaryaki at quicinc.com>
Date: Fri, 1 Nov 2024 07:45:15 -0700
Subject: [PATCH 1/2] [lld][Hexagon] Fix R_HEX_B22_PCREL range checks
Range checks for R_HEX_B22_PCREL did not account for the
fact that offset is measured in instructions, not bytes.
Add a test for all range-checked relocations.
---
lld/ELF/Arch/Hexagon.cpp | 2 +-
lld/test/ELF/hexagon-jump-error.s | 2 +-
lld/test/ELF/hexagon-jump.s | 45 +++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 2 deletions(-)
create mode 100644 lld/test/ELF/hexagon-jump.s
diff --git a/lld/ELF/Arch/Hexagon.cpp b/lld/ELF/Arch/Hexagon.cpp
index b648b3bdc1da69..fc201e3c40543e 100644
--- a/lld/ELF/Arch/Hexagon.cpp
+++ b/lld/ELF/Arch/Hexagon.cpp
@@ -329,7 +329,7 @@ void Hexagon::relocate(uint8_t *loc, const Relocation &rel,
case R_HEX_B22_PCREL:
case R_HEX_GD_PLT_B22_PCREL:
case R_HEX_PLT_B22_PCREL:
- checkInt(ctx, loc, val, 22, rel);
+ checkInt(ctx, loc, val, 24, rel);
or32le(loc, applyMask(0x1ff3ffe, val >> 2));
break;
case R_HEX_B22_PCREL_X:
diff --git a/lld/test/ELF/hexagon-jump-error.s b/lld/test/ELF/hexagon-jump-error.s
index fec873827e573d..53860b5daf2b16 100644
--- a/lld/test/ELF/hexagon-jump-error.s
+++ b/lld/test/ELF/hexagon-jump-error.s
@@ -25,7 +25,7 @@ if (p0) jump #1f
.section b15, "ax"
1:
-# CHECK: relocation R_HEX_B22_PCREL out of range: 8388612 is not in [-2097152, 2097151]
+# CHECK: relocation R_HEX_B22_PCREL out of range: 8388612 is not in [-8388608, 8388607]
jump #1f
.space (1<<23)
.section b22, "ax"
diff --git a/lld/test/ELF/hexagon-jump.s b/lld/test/ELF/hexagon-jump.s
new file mode 100644
index 00000000000000..bff063deda0000
--- /dev/null
+++ b/lld/test/ELF/hexagon-jump.s
@@ -0,0 +1,45 @@
+# REQUIRES: hexagon
+# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %s -o %t.o
+
+## Make sure we got the right relocations.
+# RUN: llvm-readelf -r %t.o | FileCheck %s --check-prefix=REL
+# REL: R_HEX_B9_PCREL 00000000 b9
+# REL: R_HEX_B13_PCREL 00000000 b13
+# REL: R_HEX_B15_PCREL 00000000 b15
+# REL: R_HEX_B22_PCREL 00000000 b22
+
+# RUN: ld.lld %t.o -o %t.out --section-start=.text=0x1000000 \
+# RUN: --section-start=b9=0x1000400 --section-start=b13=0x1004000 \
+# RUN: --section-start=b15=0x1010000 --section-start=b22=0x1800000 \
+# RUN: --threads=1
+# RUN: llvm-objdump -d --no-show-raw-insn %t.out | FileCheck %s
+
+# CHECK-NOT: trampoline
+# CHECK: 01000000 <_start>:
+# CHECK-NEXT: 1000000: { nop }
+# CHECK-NEXT: 1000004: { r0 = #0x0 ; jump 0x1000400 }
+# CHECK-NEXT: 1000008: { if (r0==#0) jump:t 0x1004000 }
+# CHECK-NEXT: 100000c: { if (p0) jump:nt 0x1010000 }
+# CHECK-NEXT: 1000010: { jump 0x1800000 }
+
+ .globl _start
+ .type _start, @function
+_start:
+## Make sure the first jump is within range
+nop
+{ r0 = #0; jump #b9 }
+if (r0==#0) jump:t #b13
+if (p0) jump #b15
+jump #b22
+
+.section b9, "ax"
+nop
+
+.section b13, "ax"
+nop
+
+.section b15, "ax"
+nop
+
+.section b22, "ax"
+nop
>From ce3215da1c922f92e6209af70996b6103c85ff9c Mon Sep 17 00:00:00 2001
From: Alexey Karyakin <akaryaki at quicinc.com>
Date: Wed, 13 Nov 2024 11:40:26 -0800
Subject: [PATCH 2/2] Combine the tests.
---
lld/test/ELF/hexagon-jump.s | 45 -------------------------------------
lld/test/ELF/hexagon.s | 41 ++++++++++++++++++++++++++++++++-
2 files changed, 40 insertions(+), 46 deletions(-)
delete mode 100644 lld/test/ELF/hexagon-jump.s
diff --git a/lld/test/ELF/hexagon-jump.s b/lld/test/ELF/hexagon-jump.s
deleted file mode 100644
index bff063deda0000..00000000000000
--- a/lld/test/ELF/hexagon-jump.s
+++ /dev/null
@@ -1,45 +0,0 @@
-# REQUIRES: hexagon
-# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %s -o %t.o
-
-## Make sure we got the right relocations.
-# RUN: llvm-readelf -r %t.o | FileCheck %s --check-prefix=REL
-# REL: R_HEX_B9_PCREL 00000000 b9
-# REL: R_HEX_B13_PCREL 00000000 b13
-# REL: R_HEX_B15_PCREL 00000000 b15
-# REL: R_HEX_B22_PCREL 00000000 b22
-
-# RUN: ld.lld %t.o -o %t.out --section-start=.text=0x1000000 \
-# RUN: --section-start=b9=0x1000400 --section-start=b13=0x1004000 \
-# RUN: --section-start=b15=0x1010000 --section-start=b22=0x1800000 \
-# RUN: --threads=1
-# RUN: llvm-objdump -d --no-show-raw-insn %t.out | FileCheck %s
-
-# CHECK-NOT: trampoline
-# CHECK: 01000000 <_start>:
-# CHECK-NEXT: 1000000: { nop }
-# CHECK-NEXT: 1000004: { r0 = #0x0 ; jump 0x1000400 }
-# CHECK-NEXT: 1000008: { if (r0==#0) jump:t 0x1004000 }
-# CHECK-NEXT: 100000c: { if (p0) jump:nt 0x1010000 }
-# CHECK-NEXT: 1000010: { jump 0x1800000 }
-
- .globl _start
- .type _start, @function
-_start:
-## Make sure the first jump is within range
-nop
-{ r0 = #0; jump #b9 }
-if (r0==#0) jump:t #b13
-if (p0) jump #b15
-jump #b22
-
-.section b9, "ax"
-nop
-
-.section b13, "ax"
-nop
-
-.section b15, "ax"
-nop
-
-.section b22, "ax"
-nop
diff --git a/lld/test/ELF/hexagon.s b/lld/test/ELF/hexagon.s
index 8ef9b8eead8f19..b1576fb47d81af 100644
--- a/lld/test/ELF/hexagon.s
+++ b/lld/test/ELF/hexagon.s
@@ -1,7 +1,9 @@
# REQUIRES: hexagon
# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %s -o %t.o
# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %S/Inputs/hexagon.s -o %t1.o
-# RUN: ld.lld %t.o %t1.o -o %t
+# RUN: ld.lld %t.o %t1.o -o %t --Ttext=0x200b4 --section-start=b_1000000=0x1000000 \
+# RUN: --section-start=b_1000400=0x1000400 --section-start=b_1004000=0x1004000 \
+# RUN: --section-start=b_1010000=0x1010000 --section-start=b_1800000=0x1800000
# RUN: llvm-objdump --no-print-imm-hex -d %t | FileCheck %s
# Note: 131584 == 0x20200
@@ -221,3 +223,40 @@ r0 = memw(r1+##_start)
memw(r0+##_start) = r1
# CHECK: memw(r0+##131644) = r1
+
+
+## Tests for maximum branch ranges reachable without trampolines.
+
+.section b_1000000, "ax"
+## The nop makes sure the first jump is within range.
+nop
+{ r0 = #0; jump #b_1000400 } // R_HEX_B9_PCREL
+if (r0==#0) jump:t #b_1004000 // R_HEX_B13_PCREL
+if (p0) jump #b_1010000 // R_HEX_B15_PCREL
+jump #b_1800000 // R_HEX_B22_PCREL
+
+.section b_1000400, "ax"
+nop
+
+.section b_1004000, "ax"
+nop
+
+.section b_1010000, "ax"
+nop
+
+.section b_1800000, "ax"
+nop
+
+## Make sure we got the right relocations.
+# RUN: llvm-readelf -r %t.o | FileCheck %s --check-prefix=REL
+# REL: R_HEX_B9_PCREL 00000000 b_1000400
+# REL: R_HEX_B13_PCREL 00000000 b_1004000
+# REL: R_HEX_B15_PCREL 00000000 b_1010000
+# REL: R_HEX_B22_PCREL 00000000 b_1800000
+
+# CHECK: 01000000 <b_1000000>:
+# CHECK-NEXT: 1000000: {{.*}} { nop }
+# CHECK-NEXT: 1000004: {{.*}} { r0 = #0 ; jump 0x1000400 }
+# CHECK-NEXT: 1000008: {{.*}} { if (r0==#0) jump:t 0x1004000 }
+# CHECK-NEXT: 100000c: {{.*}} { if (p0) jump:nt 0x1010000 }
+# CHECK-NEXT: 1000010: {{.*}} { jump 0x1800000 }
More information about the llvm-commits
mailing list