Skip to content

[lld][ELF] Fix a corner case of elf::getLoongArchPageDelta #71907

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions lld/ELF/Arch/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ uint64_t elf::getLoongArchPageDelta(uint64_t dest, uint64_t pc) {
bool negativeA = lo12(dest) > 0x7ff;
bool negativeB = (result & 0x8000'0000) != 0;

// A corner case; directly return the expected result.
if (result == 0xfff'fffff'fffff'000 && negativeA)
return 0xfff'fffff'00000'000;

if (negativeA)
result += 0x1000;
if (negativeA && !negativeB)
Expand Down
11 changes: 5 additions & 6 deletions lld/test/ELF/loongarch-pc-aligned.s
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,17 @@
# EXTREME15-NEXT: lu32i.d $t0, -349526
# EXTREME15-NEXT: lu52i.d $t0, $t0, -1093

## FIXME: Correct %pc64_lo20 should be 0xfffff (-1) and %pc64_hi12 should be 0xfff (-1), but current values are:
## page delta = 0x0000000000000000, page offset = 0x888
## page delta = 0xffffffff00000000, page offset = 0x888
## %pc_lo12 = 0x888 = -1912
## %pc_hi20 = 0x00000 = 0
## %pc64_lo20 = 0x00000 = 0
## %pc64_hi12 = 0x00000 = 0
## %pc64_lo20 = 0xfffff = -1
## %pc64_hi12 = 0xfff = -1
# RUN: ld.lld %t/extreme.o --section-start=.rodata=0x0000000012344888 --section-start=.text=0x0000000012345678 -o %t/extreme16
# RUN: llvm-objdump -d --no-show-raw-insn %t/extreme16 | FileCheck %s --check-prefix=EXTREME16
# EXTREME16: addi.d $t0, $zero, -1912
# EXTREME16-NEXT: pcalau12i $t1, 0
# EXTREME16-NEXT: lu32i.d $t0, 0
# EXTREME16-NEXT: lu52i.d $t0, $t0, 0
# EXTREME16-NEXT: lu32i.d $t0, -1
# EXTREME16-NEXT: lu52i.d $t0, $t0, -1

#--- a.s
.rodata
Expand Down