Skip to content

Commit 88548df

Browse files
authored
[lld][LoongArch] Support the R_LARCH_CALL36 relocation type (#73346)
R_LARCH_CALL36 was designed for function call on medium code model where the 2 instructions (pcaddu18i + jirl) must be adjacent. This is expected to replace current medium code model implementation, i.e. R_LARCH_PCALA_{HI20,LO12} on pcalau12i + jirl. See loongson/la-abi-specs#3 for more details.
1 parent 03b7747 commit 88548df

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lld/ELF/Arch/LoongArch.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
463463
case R_LARCH_B16:
464464
case R_LARCH_B21:
465465
case R_LARCH_B26:
466+
case R_LARCH_CALL36:
466467
return R_PLT_PC;
467468
case R_LARCH_GOT_PC_HI20:
468469
case R_LARCH_GOT64_PC_LO20:
@@ -590,6 +591,25 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
590591
write32le(loc, setD10k16(read32le(loc), val >> 2));
591592
return;
592593

594+
case R_LARCH_CALL36: {
595+
// This relocation is designed for adjancent pcaddu18i+jirl pairs that
596+
// are patched in one time. Because of sign extension of these insns'
597+
// immediate fields, the relocation range is [-128G - 0x20000, +128G -
598+
// 0x20000) (of course must be 4-byte aligned).
599+
if (((int64_t)val + 0x20000) != llvm::SignExtend64(val + 0x20000, 38))
600+
reportRangeError(loc, rel, Twine(val), llvm::minIntN(38) - 0x20000,
601+
llvm::maxIntN(38) - 0x20000);
602+
checkAlignment(loc, val, 4, rel);
603+
// Since jirl performs sign extension on the offset immediate, adds (1<<17)
604+
// to original val to get the correct hi20.
605+
uint32_t hi20 = extractBits(val + (1 << 17), 37, 18);
606+
// Despite the name, the lower part is actually 18 bits with 4-byte aligned.
607+
uint32_t lo16 = extractBits(val, 17, 2);
608+
write32le(loc, setJ20(read32le(loc), hi20));
609+
write32le(loc + 4, setK16(read32le(loc + 4), lo16));
610+
return;
611+
}
612+
593613
// Relocs intended for `addi`, `ld` or `st`.
594614
case R_LARCH_PCALA_LO12:
595615
// We have to again inspect the insn word to handle the R_LARCH_PCALA_LO12

lld/test/ELF/loongarch-call36.s

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# REQUIRES: loongarch
2+
3+
# RUN: rm -rf %t && split-file %s %t
4+
# RUN: llvm-mc --filetype=obj --triple=loongarch64-unknown-elf %t/a.s -o %t/a.o
5+
6+
# RUN: ld.lld %t/a.o --section-start=.text=0x20010 --section-start=.sec.foo=0x60020 -o %t/exe1
7+
# RUN: llvm-objdump --no-show-raw-insn -d %t/exe1 | FileCheck --match-full-lines %s --check-prefix=EXE1
8+
## hi20 = target - pc + (1 << 17) >> 18 = 0x60020 - 0x20010 + 0x20000 >> 18 = 1
9+
## lo18 = target - pc & (1 << 18) - 1 = 0x60020 - 0x20010 & 0x3ffff = 16
10+
# EXE1: 20010: pcaddu18i $t0, 1
11+
# EXE1-NEXT: 20014: jirl $zero, $t0, 16
12+
13+
# RUN: ld.lld %t/a.o --section-start=.text=0x20010 --section-start=.sec.foo=0x40020 -o %t/exe2
14+
# RUN: llvm-objdump --no-show-raw-insn -d %t/exe2 | FileCheck --match-full-lines %s --check-prefix=EXE2
15+
## hi20 = target - pc + (1 << 17) >> 18 = 0x40020 - 0x20010 + 0x20000 >> 18 = 1
16+
## lo18 = target - pc & (1 << 18) - 1 = 0x40020 - 0x20010 & 0x3ffff = -131056
17+
# EXE2: 20010: pcaddu18i $t0, 1
18+
# EXE2-NEXT: 20014: jirl $zero, $t0, -131056
19+
20+
# RUN: ld.lld %t/a.o -shared -T %t/a.t -o %t/a.so
21+
# RUN: llvm-readelf -x .got.plt %t/a.so | FileCheck --check-prefix=GOTPLT %s
22+
# RUN: llvm-objdump -d --no-show-raw-insn %t/a.so | FileCheck --check-prefix=SO %s
23+
## PLT should be present in this case.
24+
# SO: Disassembly of section .plt:
25+
# SO: <.plt>:
26+
## foo@plt:
27+
# SO: 1234520: pcaddu12i $t3, 64{{$}}
28+
# SO-NEXT: ld.d $t3, $t3, 544{{$}}
29+
# SO-NEXT: jirl $t1, $t3, 0
30+
# SO-NEXT: nop
31+
32+
# SO: Disassembly of section .text:
33+
# SO: <_start>:
34+
## hi20 = foo@plt - pc + (1 << 17) >> 18 = 0x1234520 - 0x1274670 + 0x20000 >> 18 = -1
35+
## lo18 = foo@plt - pc & (1 << 18) - 1 = 0x1234520 - 0x1274670 & 0x3ffff = -336
36+
# SO-NEXT: pcaddu18i $t0, -1{{$}}
37+
# SO-NEXT: jirl $zero, $t0, -336{{$}}
38+
39+
# GOTPLT: section '.got.plt':
40+
# GOTPLT-NEXT: 0x01274730 00000000 00000000 00000000 00000000
41+
# GOTPLT-NEXT: 0x01274740 00452301 00000000
42+
43+
# RUN: not ld.lld %t/a.o --section-start=.text=0x20000 --section-start=.sec.foo=0x2000020000 -o /dev/null 2>&1 | \
44+
# RUN: FileCheck -DFILE=%t/a.o --check-prefix=ERROR-RANGE %s
45+
# ERROR-RANGE: error: [[FILE]]:(.text+0x0): relocation R_LARCH_CALL36 out of range: 137438953472 is not in [-137439084544, 137438822399]; references 'foo'
46+
47+
## Impossible case in reality becasue all LoongArch instructions are fixed 4-bytes long.
48+
# RUN: not ld.lld %t/a.o --section-start=.text=0x20000 --section-start=.sec.foo=0x40001 -o /dev/null 2>&1 | \
49+
# RUN: FileCheck -DFILE=%t/a.o --check-prefix=ERROR-ALIGN %s
50+
# ERROR-ALIGN: error: [[FILE]]:(.text+0x0): improper alignment for relocation R_LARCH_CALL36: 0x20001 is not aligned to 4 bytes
51+
52+
#--- a.t
53+
SECTIONS {
54+
.plt 0x1234500: { *(.plt) }
55+
.text 0x1274670: { *(.text) }
56+
}
57+
58+
#--- a.s
59+
.text
60+
.global _start
61+
_start:
62+
.reloc ., R_LARCH_CALL36, foo
63+
pcaddu18i $t0, 0
64+
jirl $zero, $t0, 0
65+
66+
.section .sec.foo,"ax"
67+
.global foo
68+
foo:
69+
ret

0 commit comments

Comments
 (0)