Skip to content

Commit 64e3466

Browse files
[lld][Hexagon] Fix R_HEX_B22_PCREL range checks (#115925)
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.
1 parent b0afa6b commit 64e3466

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

lld/ELF/Arch/Hexagon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void Hexagon::relocate(uint8_t *loc, const Relocation &rel,
328328
case R_HEX_B22_PCREL:
329329
case R_HEX_GD_PLT_B22_PCREL:
330330
case R_HEX_PLT_B22_PCREL:
331-
checkInt(ctx, loc, val, 22, rel);
331+
checkInt(ctx, loc, val, 24, rel);
332332
or32le(loc, applyMask(0x1ff3ffe, val >> 2));
333333
break;
334334
case R_HEX_B22_PCREL_X:

lld/test/ELF/hexagon-jump-error.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (p0) jump #1f
2525
.section b15, "ax"
2626
1:
2727

28-
# CHECK: relocation R_HEX_B22_PCREL out of range: 8388612 is not in [-2097152, 2097151]
28+
# CHECK: relocation R_HEX_B22_PCREL out of range: 8388612 is not in [-8388608, 8388607]
2929
jump #1f
3030
.space (1<<23)
3131
.section b22, "ax"

lld/test/ELF/hexagon.s

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# REQUIRES: hexagon
22
# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %s -o %t.o
33
# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %S/Inputs/hexagon.s -o %t1.o
4-
# RUN: ld.lld %t.o %t1.o -o %t
4+
# RUN: ld.lld %t.o %t1.o -o %t --Ttext=0x200b4 --section-start=b_1000000=0x1000000 \
5+
# RUN: --section-start=b_1000400=0x1000400 --section-start=b_1004000=0x1004000 \
6+
# RUN: --section-start=b_1010000=0x1010000 --section-start=b_1800000=0x1800000
57
# RUN: llvm-objdump --no-print-imm-hex -d %t | FileCheck %s
68

79
# Note: 131584 == 0x20200
@@ -221,3 +223,40 @@ r0 = memw(r1+##_start)
221223

222224
memw(r0+##_start) = r1
223225
# CHECK: memw(r0+##131644) = r1
226+
227+
228+
## Tests for maximum branch ranges reachable without trampolines.
229+
230+
.section b_1000000, "ax"
231+
## The nop makes sure the first jump is within range.
232+
nop
233+
{ r0 = #0; jump #b_1000400 } // R_HEX_B9_PCREL
234+
if (r0==#0) jump:t #b_1004000 // R_HEX_B13_PCREL
235+
if (p0) jump #b_1010000 // R_HEX_B15_PCREL
236+
jump #b_1800000 // R_HEX_B22_PCREL
237+
238+
.section b_1000400, "ax"
239+
nop
240+
241+
.section b_1004000, "ax"
242+
nop
243+
244+
.section b_1010000, "ax"
245+
nop
246+
247+
.section b_1800000, "ax"
248+
nop
249+
250+
## Make sure we got the right relocations.
251+
# RUN: llvm-readelf -r %t.o | FileCheck %s --check-prefix=REL
252+
# REL: R_HEX_B9_PCREL 00000000 b_1000400
253+
# REL: R_HEX_B13_PCREL 00000000 b_1004000
254+
# REL: R_HEX_B15_PCREL 00000000 b_1010000
255+
# REL: R_HEX_B22_PCREL 00000000 b_1800000
256+
257+
# CHECK: 01000000 <b_1000000>:
258+
# CHECK-NEXT: 1000000: {{.*}} { nop }
259+
# CHECK-NEXT: 1000004: {{.*}} { r0 = #0 ; jump 0x1000400 }
260+
# CHECK-NEXT: 1000008: {{.*}} { if (r0==#0) jump:t 0x1004000 }
261+
# CHECK-NEXT: 100000c: {{.*}} { if (p0) jump:nt 0x1010000 }
262+
# CHECK-NEXT: 1000010: {{.*}} { jump 0x1800000 }

0 commit comments

Comments
 (0)