Skip to content

Commit 663522c

Browse files
committed
[lld][ELF] Fix a corner case of elf::getLoongArchPageDelta
If `page(dest) - page(pc)` is 0xfffffffffff000, i.e. page(pc) is next to page(dest), and lo12(dest) > 0x7ff, correct %pc64_lo12 and %pc64_hi12 should be both -1 (which can be checked with binutils) but they are both 0 on lld. This patch fixes this issue.
1 parent e752b58 commit 663522c

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

lld/ELF/Arch/LoongArch.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ uint64_t elf::getLoongArchPageDelta(uint64_t dest, uint64_t pc) {
159159
bool negativeA = lo12(dest) > 0x7ff;
160160
bool negativeB = (result & 0x8000'0000) != 0;
161161

162+
// A corner case; directly return the expected result.
163+
if (result == 0xfff'fffff'fffff'000 && negativeA)
164+
return 0xfff'fffff'00000'000;
165+
162166
if (negativeA)
163167
result += 0x1000;
164168
if (negativeA && !negativeB)

lld/test/ELF/loongarch-pc-aligned.s

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,17 @@
260260
# EXTREME15-NEXT: lu32i.d $t0, -349526
261261
# EXTREME15-NEXT: lu52i.d $t0, $t0, -1093
262262

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

276275
#--- a.s
277276
.rodata

0 commit comments

Comments
 (0)