Skip to content

Commit 8a31f7d

Browse files
committed
[lld][LoongArch] Support the R_LARCH_PCREL20_S2 relocation type
`R_LARCH_PCREL20_S2` is a new added relocation type in LoongArch ELF psABI v2.10 [1] which is not corvered by D138135 except `R_LARCH_64_PCREL`. A motivation to support `R_LARCH_PCREL20_S2` in lld is to build the runtime of .NET core (a.k.a `CoreCLR`) in which strict PC-relative semantics need to be guaranteed [2]. The normal `pcalau12i + addi.d` approach doesn't work because the code will be copied to other places with different "page" and offsets. To achieve this, we can use `pcaddi` with explicit `R_LARCH_PCREL20_S2` reloc to address +-2MB PC-relative range with 4-bytes aligned. [1]: https://github.com/loongson/la-abi-specs/releases/tag/v2.10 [2]: https://github.com/dotnet/runtime/blob/release/7.0/src/coreclr/vm/loongarch64/asmhelpers.S#L307 Reviewed By: xen0n, MaskRay Differential Revision: https://reviews.llvm.org/D156772
1 parent 2b0bfb6 commit 8a31f7d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lld/ELF/Arch/LoongArch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
457457
return R_RISCV_ADD;
458458
case R_LARCH_32_PCREL:
459459
case R_LARCH_64_PCREL:
460+
case R_LARCH_PCREL20_S2:
460461
return R_PC;
461462
case R_LARCH_B16:
462463
case R_LARCH_B21:
@@ -564,6 +565,12 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
564565
write64le(loc, val);
565566
return;
566567

568+
case R_LARCH_PCREL20_S2:
569+
checkInt(loc, val, 22, rel);
570+
checkAlignment(loc, val, 4, rel);
571+
write32le(loc, setJ20(read32le(loc), val >> 2));
572+
return;
573+
567574
case R_LARCH_B16:
568575
checkInt(loc, val, 18, rel);
569576
checkAlignment(loc, val, 4, rel);

lld/test/ELF/loongarch-pcrel20-s2.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# REQUIRES: loongarch
2+
3+
# RUN: llvm-mc --filetype=obj --triple=loongarch32-unknown-elf %s -o %t.la32.o
4+
# RUN: llvm-mc --filetype=obj --triple=loongarch64-unknown-elf %s -o %t.la64.o
5+
6+
# RUN: ld.lld %t.la32.o --section-start=.text=0x20000 --section-start=.data=0x20008 -o %t.la32.1
7+
# RUN: ld.lld %t.la64.o --section-start=.text=0x20000 --section-start=.data=0x20008 -o %t.la64.1
8+
# RUN: llvm-objdump --no-show-raw-insn -d %t.la32.1 | FileCheck --match-full-lines %s
9+
# RUN: llvm-objdump --no-show-raw-insn -d %t.la64.1 | FileCheck --match-full-lines %s
10+
# CHECK: 20000: pcaddi $t0, 2
11+
12+
# RUN: not ld.lld %t.la32.o --section-start=.text=0x20000 --section-start=.data=0x220000 -o /dev/null 2>&1 | \
13+
# RUN: FileCheck -DFILE=%t.la32.o --check-prefix=ERROR-RANGE %s
14+
# RUN: not ld.lld %t.la64.o --section-start=.text=0x20000 --section-start=.data=0x220000 -o /dev/null 2>&1 | \
15+
# RUN: FileCheck -DFILE=%t.la64.o --check-prefix=ERROR-RANGE %s
16+
# ERROR-RANGE: error: [[FILE]]:(.text+0x0): relocation R_LARCH_PCREL20_S2 out of range: 2097152 is not in [-2097152, 2097151]; references section '.data'
17+
18+
# RUN: not ld.lld %t.la32.o --section-start=.text=0x20000 --section-start=.data=0x40001 -o /dev/null 2>&1 | \
19+
# RUN: FileCheck -DFILE=%t.la32.o --check-prefix=ERROR-ALIGN %s
20+
# RUN: not ld.lld %t.la64.o --section-start=.text=0x20000 --section-start=.data=0x40001 -o /dev/null 2>&1 | \
21+
# RUN: FileCheck -DFILE=%t.la64.o --check-prefix=ERROR-ALIGN %s
22+
# ERROR-ALIGN: error: [[FILE]]:(.text+0x0): improper alignment for relocation R_LARCH_PCREL20_S2: 0x20001 is not aligned to 4 bytes
23+
24+
.global _start
25+
26+
_start:
27+
1:
28+
pcaddi $t0, 0
29+
.reloc 1b, R_LARCH_PCREL20_S2, .data
30+
31+
.data
32+
.word 0

0 commit comments

Comments
 (0)