Skip to content

Commit 6b98ab9

Browse files
authored
[lld][ELF][LoongArch] Add support for R_LARCH_LE_{HI20,ADD,LO12}_R relocations
Reviewed By: SixWeining Pull Request: #99486
1 parent 0f23156 commit 6b98ab9

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

lld/ELF/Arch/LoongArch.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Symbols.h"
1212
#include "SyntheticSections.h"
1313
#include "Target.h"
14+
#include "llvm/BinaryFormat/ELF.h"
1415
#include "llvm/Support/LEB128.h"
1516

1617
using namespace llvm;
@@ -407,7 +408,9 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
407408
case R_LARCH_TLS_TPREL32:
408409
case R_LARCH_TLS_TPREL64:
409410
case R_LARCH_TLS_LE_HI20:
411+
case R_LARCH_TLS_LE_HI20_R:
410412
case R_LARCH_TLS_LE_LO12:
413+
case R_LARCH_TLS_LE_LO12_R:
411414
case R_LARCH_TLS_LE64_LO20:
412415
case R_LARCH_TLS_LE64_HI12:
413416
return R_TPREL;
@@ -490,6 +493,7 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
490493
return R_TLSLD_GOT;
491494
case R_LARCH_TLS_GD_HI20:
492495
return R_TLSGD_GOT;
496+
case R_LARCH_TLS_LE_ADD_R:
493497
case R_LARCH_RELAX:
494498
return config->relax ? R_RELAX_HINT : R_NONE;
495499
case R_LARCH_ALIGN:
@@ -617,6 +621,7 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
617621
case R_LARCH_TLS_LE_LO12:
618622
case R_LARCH_TLS_IE_PC_LO12:
619623
case R_LARCH_TLS_IE_LO12:
624+
case R_LARCH_TLS_LE_LO12_R:
620625
case R_LARCH_TLS_DESC_PC_LO12:
621626
case R_LARCH_TLS_DESC_LO12:
622627
write32le(loc, setK12(read32le(loc), extractBits(val, 11, 0)));
@@ -638,6 +643,9 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
638643
case R_LARCH_TLS_DESC_HI20:
639644
write32le(loc, setJ20(read32le(loc), extractBits(val, 31, 12)));
640645
return;
646+
case R_LARCH_TLS_LE_HI20_R:
647+
write32le(loc, setJ20(read32le(loc), extractBits(val + 0x800, 31, 12)));
648+
return;
641649

642650
// Relocs intended for `lu32i.d`.
643651
case R_LARCH_ABS64_LO20:
@@ -707,6 +715,7 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
707715
// no-op
708716
return;
709717

718+
case R_LARCH_TLS_LE_ADD_R:
710719
case R_LARCH_RELAX:
711720
return; // Ignored (for now)
712721

lld/test/ELF/loongarch-tls-le.s

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
# REQUIRES: loongarch
22

3-
# RUN: llvm-mc --filetype=obj --triple=loongarch32 %s -o %t.32.o
3+
# RUN: llvm-mc --filetype=obj --triple=loongarch32 --defsym ELF32=1 %s -o %t.32.o
44
# RUN: llvm-mc --filetype=obj --triple=loongarch64 %s -o %t.64.o
55

66
# RUN: ld.lld %t.32.o -o %t.32
77
# RUN: llvm-nm -p %t.32 | FileCheck --check-prefixes=NM %s
8-
# RUN: llvm-objdump -d --no-show-raw-insn %t.32 | FileCheck --check-prefixes=LE %s
8+
# RUN: llvm-objdump -d --no-show-raw-insn %t.32 | FileCheck --check-prefixes=LE,LE32 %s
99

1010
# RUN: ld.lld %t.64.o -o %t.64
11-
# RUN: llvm-objdump -d --no-show-raw-insn %t.64 | FileCheck --check-prefixes=LE %s
11+
# RUN: llvm-objdump -d --no-show-raw-insn %t.64 | FileCheck --check-prefixes=LE,LE64 %s
1212

1313
# RUN: not ld.lld -shared %t.32.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
1414

1515
# ERR: error: relocation R_LARCH_TLS_LE_HI20 against .LANCHOR0 cannot be used with -shared
1616
# ERR: error: relocation R_LARCH_TLS_LE_LO12 against .LANCHOR0 cannot be used with -shared
1717
# ERR: error: relocation R_LARCH_TLS_LE_HI20 against a cannot be used with -shared
1818
# ERR: error: relocation R_LARCH_TLS_LE_LO12 against a cannot be used with -shared
19+
# ERR: error: relocation R_LARCH_TLS_LE_HI20_R against .LANCHOR0 cannot be used with -shared
20+
# ERR: error: relocation R_LARCH_TLS_LE_LO12_R against .LANCHOR0 cannot be used with -shared
21+
# ERR: error: relocation R_LARCH_TLS_LE_HI20_R against a cannot be used with -shared
22+
# ERR: error: relocation R_LARCH_TLS_LE_LO12_R against a cannot be used with -shared
1923

2024
# NM: {{0*}}00000008 b .LANCHOR0
2125
# NM: {{0*}}00000800 B a
@@ -26,13 +30,50 @@
2630
# LE-NEXT: ori $a0, $a0, 8
2731
# LE-NEXT: lu12i.w $a1, 0
2832
# LE-NEXT: ori $a1, $a1, 2048
33+
34+
# LE32: add.w $a0, $a0, $tp
35+
# LE32-NEXT: addi.w $a0, $a0, 8
36+
# LE32-NEXT: lu12i.w $a0, 1
37+
# LE32-NEXT: add.w $a0, $a0, $tp
38+
# LE32-NEXT: addi.w $a0, $a0, -2048
39+
40+
# LE64: add.d $a0, $a0, $tp
41+
# LE64-NEXT: addi.d $a0, $a0, 8
42+
# LE64-NEXT: lu12i.w $a0, 1
43+
# LE64-NEXT: add.d $a0, $a0, $tp
44+
# LE64-NEXT: addi.d $a0, $a0, -2048
45+
2946
# LE-EMPTY:
3047

48+
.macro add dst, src1, src2, src3
49+
.ifdef ELF32
50+
add.w \dst, \src1, \src2, \src3
51+
.else
52+
add.d \dst, \src1, \src2, \src3
53+
.endif
54+
.endm
55+
.macro addi dst, src1, src2
56+
.ifdef ELF32
57+
addi.w \dst, \src1, \src2
58+
.else
59+
addi.d \dst, \src1, \src2
60+
.endif
61+
.endm
62+
3163
.text
64+
3265
_start:
3366
la.tls.le $a0, .LANCHOR0
3467
la.tls.le $a1, a
3568

69+
lu12i.w $a0, %le_hi20_r(.LANCHOR0)
70+
add $a0, $a0, $tp, %le_add_r(.LANCHOR0)
71+
addi $a0, $a0, %le_lo12_r(.LANCHOR0)
72+
73+
lu12i.w $a0, %le_hi20_r(a)
74+
add $a0, $a0, $tp, %le_add_r(a)
75+
addi $a0, $a0, %le_lo12_r(a)
76+
3677
.section .tbss,"awT",@nobits
3778
.space 8
3879
.LANCHOR0:

0 commit comments

Comments
 (0)