Skip to content

Commit 3b2f81c

Browse files
committed
[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.
1 parent 1791b25 commit 3b2f81c

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

lld/ELF/Arch/Hexagon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void Hexagon::relocate(uint8_t *loc, const Relocation &rel,
329329
case R_HEX_B22_PCREL:
330330
case R_HEX_GD_PLT_B22_PCREL:
331331
case R_HEX_PLT_B22_PCREL:
332-
checkInt(ctx, loc, val, 22, rel);
332+
checkInt(ctx, loc, val, 24, rel);
333333
or32le(loc, applyMask(0x1ff3ffe, val >> 2));
334334
break;
335335
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-jump.s

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# REQUIRES: hexagon
2+
# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %s -o %t.o
3+
4+
## Make sure we got the right relocations.
5+
# RUN: llvm-readelf -r %t.o | FileCheck %s --check-prefix=REL
6+
# REL: R_HEX_B9_PCREL 00000000 b9
7+
# REL: R_HEX_B13_PCREL 00000000 b13
8+
# REL: R_HEX_B15_PCREL 00000000 b15
9+
# REL: R_HEX_B22_PCREL 00000000 b22
10+
11+
# RUN: ld.lld %t.o -o %t.out --section-start=.text=0x1000000 \
12+
# RUN: --section-start=b9=0x1000400 --section-start=b13=0x1004000 \
13+
# RUN: --section-start=b15=0x1010000 --section-start=b22=0x1800000 \
14+
# RUN: --threads=1
15+
# RUN: llvm-objdump -d --no-show-raw-insn %t.out | FileCheck %s
16+
17+
# CHECK-NOT: trampoline
18+
# CHECK: 01000000 <_start>:
19+
# CHECK-NEXT: 1000000: { nop }
20+
# CHECK-NEXT: 1000004: { r0 = #0x0 ; jump 0x1000400 }
21+
# CHECK-NEXT: 1000008: { if (r0==#0) jump:t 0x1004000 }
22+
# CHECK-NEXT: 100000c: { if (p0) jump:nt 0x1010000 }
23+
# CHECK-NEXT: 1000010: { jump 0x1800000 }
24+
25+
.globl _start
26+
.type _start, @function
27+
_start:
28+
## Make sure the first jump is within range
29+
nop
30+
{ r0 = #0; jump #b9 }
31+
if (r0==#0) jump:t #b13
32+
if (p0) jump #b15
33+
jump #b22
34+
35+
.section b9, "ax"
36+
nop
37+
38+
.section b13, "ax"
39+
nop
40+
41+
.section b15, "ax"
42+
nop
43+
44+
.section b22, "ax"
45+
nop

0 commit comments

Comments
 (0)