Skip to content

[lld][Hexagon] Fix R_HEX_B22_PCREL range checks #115925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lld/ELF/Arch/Hexagon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lld/test/ELF/hexagon-jump-error.s
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 40 additions & 1 deletion lld/test/ELF/hexagon.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 }
Loading