Skip to content

Commit 7cd89c3

Browse files
committed
[lld][LoongArch] Support the R_LARCH_CALL36 relocation type
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 9ff7d0e commit 7cd89c3

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

lld/ELF/Arch/LoongArch.cpp

Lines changed: 15 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,20 @@ 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 type is designed for the adjancent pcaddu18i+jirl pair,
596+
// so patch these 2 instructions in one time.
597+
checkInt(loc, val, 38, rel);
598+
checkAlignment(loc, val, 4, rel);
599+
// Since jirl performs sign extension on the offset immediate, adds (1<<17)
600+
// to original val to get the correct hi20.
601+
uint32_t hi20 = extractBits(val + (1 << 17), 37, 18);
602+
uint32_t lo18 = val & (1 << 18) - 1;
603+
write32le(loc, setJ20(read32le(loc), hi20));
604+
write32le(loc + 4, setK16(read32le(loc + 4), lo18 >> 2));
605+
return;
606+
}
607+
593608
// Relocs intended for `addi`, `ld` or `st`.
594609
case R_LARCH_PCALA_LO12:
595610
// We have to again inspect the insn word to handle the R_LARCH_PCALA_LO12
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# REQUIRES: loongarch
2+
# RUN: rm -rf %t && split-file %s %t
3+
# RUN: llvm-mc --filetype=obj --triple=loongarch64-unknown-elf %t/a.s -o %t/a.o
4+
# RUN: ld.lld %t/a.o -shared -T %t/a.t -o %t/a.so
5+
# RUN: llvm-readelf -x .got.plt %t/a.so | FileCheck --check-prefix=GOTPLT %s
6+
# RUN: llvm-objdump -d --no-show-raw-insn %t/a.so | FileCheck %s
7+
8+
## PLT should be present in this case.
9+
# CHECK: Disassembly of section .plt:
10+
# CHECK: <.plt>:
11+
## bar@plt:
12+
# CHECK: 1234520: pcaddu12i $t3, 64{{$}}
13+
# CHECK-NEXT: ld.d $t3, $t3, 536{{$}}
14+
# CHECK-NEXT: jirl $t1, $t3, 0
15+
# CHECK-NEXT: nop
16+
17+
# CHECK: Disassembly of section .text:
18+
# CHECK: <foo>:
19+
## hi20 = bar@plt - pc + (1 << 17) >> 18 = 0x1234520 - 0x1274670 + 0x20000 >> 18 = -1
20+
## lo18 = bar@plt - pc & (1 << 18) - 1 = 0x1234520 - 0x1274670 & 0x3ffff = -336
21+
# CHECK-NEXT: pcaddu18i $t0, -1{{$}}
22+
# CHECK-NEXT: jirl $zero, $t0, -336{{$}}
23+
24+
# GOTPLT: section '.got.plt':
25+
# GOTPLT-NEXT: 0x01274728 00000000 00000000 00000000 00000000
26+
# GOTPLT-NEXT: 0x01274738 00452301 00000000
27+
28+
#--- a.t
29+
SECTIONS {
30+
.plt 0x1234500: { *(.plt) }
31+
.text 0x1274670: { *(.text) }
32+
}
33+
34+
#--- a.s
35+
.text
36+
.global foo
37+
.global bar
38+
foo:
39+
pcaddu18i $t0, 0
40+
jirl $zero, $t0, 0
41+
.reloc foo, R_LARCH_CALL36, bar

lld/test/ELF/loongarch-call36.s

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# REQUIRES: loongarch
2+
3+
# RUN: llvm-mc --filetype=obj --triple=loongarch64-unknown-elf %s -o %t.o
4+
5+
# RUN: ld.lld %t.o --section-start=.text=0x20010 --section-start=.sec.foo=0x60020 -o %t1
6+
# RUN: llvm-objdump --no-show-raw-insn -d %t1 | FileCheck --match-full-lines %s --check-prefix=CASE1
7+
## hi20 = target - pc + (1 << 17) >> 18 = 0x60020 - 0x20010 + 0x20000 >> 18 = 1
8+
## lo18 = target - pc & (1 << 18) - 1 = 0x60020 - 0x20010 & 0x3ffff = 16
9+
# CASE1: 20010: pcaddu18i $ra, 1
10+
# CASE1-NEXT: 20014: jirl $zero, $ra, 16
11+
12+
# RUN: ld.lld %t.o --section-start=.text=0x20010 --section-start=.sec.foo=0x40020 -o %t2
13+
# RUN: llvm-objdump --no-show-raw-insn -d %t2 | FileCheck --match-full-lines %s --check-prefix=CASE2
14+
## hi20 = target - pc + (1 << 17) >> 18 = 0x40020 - 0x20010 + 0x20000 >> 18 = 1
15+
## lo18 = target - pc & (1 << 18) - 1 = 0x40020 - 0x20010 & 0x3ffff = -131056
16+
# CASE2: 20010: pcaddu18i $ra, 1
17+
# CASE2-NEXT: 20014: jirl $zero, $ra, -131056
18+
19+
# RUN: not ld.lld %t.o --section-start=.text=0x20000 --section-start=.sec.foo=0x2000020000 -o /dev/null 2>&1 | \
20+
# RUN: FileCheck -DFILE=%t.o --check-prefix=ERROR-RANGE %s
21+
# ERROR-RANGE: error: [[FILE]]:(.text+0x0): relocation R_LARCH_CALL36 out of range: 137438953472 is not in [-137438953472, 137438953471]; references 'foo'
22+
23+
## Impossible case in reality becasue all LoongArch instructions are fixed 4-bytes long.
24+
# RUN: not ld.lld %t.o --section-start=.text=0x20000 --section-start=.sec.foo=0x40001 -o /dev/null 2>&1 | \
25+
# RUN: FileCheck -DFILE=%t.o --check-prefix=ERROR-ALIGN %s
26+
# ERROR-ALIGN: error: [[FILE]]:(.text+0x0): improper alignment for relocation R_LARCH_CALL36: 0x20001 is not aligned to 4 bytes
27+
28+
.text
29+
.global _start
30+
_start:
31+
1:
32+
pcaddu18i $ra, 0
33+
jirl $zero, $ra, 0
34+
.reloc 1b, R_LARCH_CALL36, foo
35+
36+
.section .sec.foo,"ax"
37+
.global foo
38+
foo:
39+
nop
40+
ret

0 commit comments

Comments
 (0)