[lld] [lld][Hexagon] Fix R_HEX_B22_PCREL range checks (PR #115925)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 12 11:00:41 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-hexagon

Author: Alexey Karyakin (quic-akaryaki)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/115925.diff


3 Files Affected:

- (modified) lld/ELF/Arch/Hexagon.cpp (+1-1) 
- (modified) lld/test/ELF/hexagon-jump-error.s (+1-1) 
- (added) lld/test/ELF/hexagon-jump.s (+45) 


``````````diff
diff --git a/lld/ELF/Arch/Hexagon.cpp b/lld/ELF/Arch/Hexagon.cpp
index 54821c299bde9e..2cea751dbe402d 100644
--- a/lld/ELF/Arch/Hexagon.cpp
+++ b/lld/ELF/Arch/Hexagon.cpp
@@ -317,7 +317,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(loc, val, 22, rel);
+    checkInt(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..9c55958524f52a
--- /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: 1000000: {  nop }
+# CHECK: 1000004: {  r0 = #0x0 ; jump 0x1000400 }
+# CHECK: 1000008: {  if (r0==#0) jump:t 0x1004000 }
+# CHECK: 100000c: {  if (p0) jump:nt 0x1010000 }
+# CHECK: 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

``````````

</details>


https://github.com/llvm/llvm-project/pull/115925


More information about the llvm-commits mailing list