Skip to content

Commit a48a2b9

Browse files
committed
Relax desc_pc_hi20+desc_pc_lo12 ==> desc_pcrel_20
1 parent 880523c commit a48a2b9

File tree

2 files changed

+31
-66
lines changed

2 files changed

+31
-66
lines changed

lld/ELF/Arch/LoongArch.cpp

+5-32
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,12 @@ static bool isPairRelaxable(ArrayRef<Relocation> relocs, size_t i) {
762762
// Relax code sequence.
763763
// From:
764764
// pcalau12i $a0, %pc_hi20(sym) | %ld_pc_hi20(sym) | %gd_pc_hi20(sym)
765+
// | %desc_pc_hi20(sym)
765766
// addi.w/d $a0, $a0, %pc_lo12(sym) | %got_pc_lo12(sym) | %got_pc_lo12(sym)
767+
// | %desc_pc_lo12(sym)
766768
// To:
767-
// pcaddi $a0, %pc_lo12(sym) | %got_pc_lo12(sym) | %got_pc_lo12(sym)
769+
// pcaddi $a0, %pc_lo12(sym) | %got_pc_lo12(sym) | %got_pc_lo12(sym)
770+
// | %desc_pcrel_20(sym)
768771
//
769772
// From:
770773
// pcalau12i $a0, %got_pc_hi20(sym_got)
@@ -909,33 +912,6 @@ static void relaxTlsLe(Ctx &ctx, const InputSection &sec, size_t i,
909912
}
910913
}
911914

912-
// Relax TLSDESC code sequence. In LoongArch, the conversion of TLSDESC GD/LD to
913-
// LE/IE is closely tied to relaxation, similar to how GCC handles it. (Due to
914-
// the lack of an efficient way for handling conversions in the extreme code
915-
// model and the difficulty in determining whether the extreme code model is
916-
// being used in handleTlsRelocation, this approach may seem like a workaround).
917-
// Consequently, the resulting code sequence depends on whether the conversion
918-
// to LE/IE is performed.
919-
//
920-
// Original code sequence:
921-
// * pcalau12i $a0, %desc_pc_hi20(sym_desc)
922-
// * addi.d $a0, $a0, %desc_pc_lo12(sym_desc)
923-
// * ld.d $ra, $a0, %desc_ld(sym_desc)
924-
// * jirl $ra, $ra, %desc_call(sym_desc)
925-
//
926-
// Cannot convert to LE/IE, but relax:
927-
// * pcaddi $a0, %desc_pcrel_20(sym_desc)
928-
// * ld.d $ra, $a0, %desc_ld(sym_desc)
929-
// * jirl $ra, $ra, %desc_call(sym_desc)
930-
//
931-
// FIXME: Implement TLSDESC GD/LD to LE/IE.
932-
static void relaxTlsdesc(Ctx &ctx, const InputSection &sec, size_t i,
933-
uint64_t loc, Relocation &rHi20, Relocation &rLo12,
934-
uint32_t &remove) {
935-
if (ctx.arg.shared && rHi20.type == R_LARCH_TLS_DESC_PC_HI20)
936-
return relaxPCHi20Lo12(ctx, sec, i, loc, rHi20, rLo12, remove);
937-
}
938-
939915
static bool relax(Ctx &ctx, InputSection &sec) {
940916
const uint64_t secAddr = sec.getVA();
941917
const MutableArrayRef<Relocation> relocs = sec.relocs();
@@ -978,6 +954,7 @@ static bool relax(Ctx &ctx, InputSection &sec) {
978954
case R_LARCH_GOT_PC_HI20:
979955
case R_LARCH_TLS_GD_PC_HI20:
980956
case R_LARCH_TLS_LD_PC_HI20:
957+
case R_LARCH_TLS_DESC_PC_HI20:
981958
// The overflow check for i+2 will be carried out in isPairRelaxable.
982959
if (isPairRelaxable(relocs, i))
983960
relaxPCHi20Lo12(ctx, sec, i, loc, r, relocs[i + 2], remove);
@@ -992,10 +969,6 @@ static bool relax(Ctx &ctx, InputSection &sec) {
992969
if (relaxable(relocs, i))
993970
relaxTlsLe(ctx, sec, i, loc, r, remove);
994971
break;
995-
case R_LARCH_TLS_DESC_PC_HI20:
996-
if (isPairRelaxable(relocs, i))
997-
relaxTlsdesc(ctx, sec, i, loc, r, relocs[i + 2], remove);
998-
break;
999972
}
1000973

1001974
// For all anchors whose offsets are <= r.offset, they are preceded by

lld/test/ELF/loongarch-relax-tlsdesc.s

+26-34
Original file line numberDiff line numberDiff line change
@@ -65,78 +65,70 @@
6565
# GD64-NEXT: add.d $a4, $a0, $tp
6666

6767
# LE64-RELA: .rela.dyn {
68-
# LE64-RELA-NEXT: 0x30278 R_LARCH_TLS_DESC64 - 0x8
69-
# LE64-RELA-NEXT: 0x30288 R_LARCH_TLS_DESC64 - 0x800
70-
# LE64-RELA-NEXT: 0x30298 R_LARCH_TLS_DESC64 - 0x1000
71-
# LE64-RELA-NEXT: 0x302A8 R_LARCH_TLS_DESC64 - 0x7FF
68+
# LE64-RELA-NEXT: 0x30268 R_LARCH_TLS_DESC64 - 0x8
69+
# LE64-RELA-NEXT: 0x30278 R_LARCH_TLS_DESC64 - 0x800
70+
# LE64-RELA-NEXT: 0x30288 R_LARCH_TLS_DESC64 - 0x1000
71+
# LE64-RELA-NEXT: 0x30298 R_LARCH_TLS_DESC64 - 0x7FF
7272
# LE64-RELA-NEXT: }
7373
# LE64-RELA: Hex dump of section '.got':
74+
# LE64-RELA-NEXT: 0x00030268 00000000 00000000 00000000 00000000 .
7475
# LE64-RELA-NEXT: 0x00030278 00000000 00000000 00000000 00000000 .
7576
# LE64-RELA-NEXT: 0x00030288 00000000 00000000 00000000 00000000 .
7677
# LE64-RELA-NEXT: 0x00030298 00000000 00000000 00000000 00000000 .
77-
# LE64-RELA-NEXT: 0x000302a8 00000000 00000000 00000000 00000000 .
7878

7979
# LE64-LABEL: <.text>:
80-
## &.got[a]-. = 0x30278 - 0x20228: 0x10 pages, page offset 0x278
81-
# LE64-NEXT: 20228: pcalau12i $a0, 16
82-
# LE64-NEXT: addi.d $a0, $a0, 632
80+
## &.got[a]-. = 0x30268 - 0x20228 = 16400<<2
81+
# LE64-NEXT: 20228: pcaddi $a0, 16400
8382
# LE64-NEXT: ld.d $ra, $a0, 0
8483
# LE64-NEXT: jirl $ra, $ra, 0
8584
# LE64-NEXT: add.d $a1, $a0, $tp
86-
## &.got[b]-. = 0x302a8 - 0x2023c: 0x10 pages, page offset 0x2a8
87-
# LE64-NEXT: 2023c: pcalau12i $a0, 16
88-
# LE64-NEXT: addi.d $a0, $a0, 680
85+
## &.got[b]-. = 0x30298 - 0x20238 = 16408<<2
86+
# LE64-NEXT: 20238: pcaddi $a0, 16408
8987
# LE64-NEXT: ld.d $ra, $a0, 0
9088
# LE64-NEXT: jirl $ra, $ra, 0
9189
# LE64-NEXT: add.d $a2, $a0, $tp
92-
## &.got[c]-. = 0x30288 - 0x20250: 0x10 pages, page offset 0x288
93-
# LE64-NEXT: 20250: pcalau12i $a0, 16
94-
# LE64-NEXT: addi.d $a0, $a0, 648
90+
## &.got[c]-. = 0x30278 - 0x20248 = 16396<<2
91+
# LE64-NEXT: 20248: pcaddi $a0, 16396
9592
# LE64-NEXT: ld.d $ra, $a0, 0
9693
# LE64-NEXT: jirl $ra, $ra, 0
9794
# LE64-NEXT: add.d $a3, $a0, $tp
98-
## &.got[d]-. = 0x30298 - 0x20264: 0x10 pages, page offset 0x298
99-
# LE64-NEXT: 20264: pcalau12i $a0, 16
100-
# LE64-NEXT: addi.d $a0, $a0, 664
95+
## &.got[d]-. = 0x30288 - 0x20258 = 16396<<2
96+
# LE64-NEXT: 20258: pcaddi $a0, 16396
10197
# LE64-NEXT: ld.d $ra, $a0, 0
10298
# LE64-NEXT: jirl $ra, $ra, 0
10399
# LE64-NEXT: add.d $a4, $a0, $tp
104100

105101
# IE64-RELA: .rela.dyn {
106-
# IE64-RELA-NEXT: 0x30428 R_LARCH_TLS_DESC64 - 0x8
107-
# IE64-RELA-NEXT: 0x30458 R_LARCH_TLS_DESC64 - 0x7FF
108-
# IE64-RELA-NEXT: 0x30438 R_LARCH_TLS_DESC64 c 0x0
109-
# IE64-RELA-NEXT: 0x30448 R_LARCH_TLS_DESC64 d 0x0
102+
# IE64-RELA-NEXT: 0x30418 R_LARCH_TLS_DESC64 - 0x8
103+
# IE64-RELA-NEXT: 0x30448 R_LARCH_TLS_DESC64 - 0x7FF
104+
# IE64-RELA-NEXT: 0x30428 R_LARCH_TLS_DESC64 c 0x0
105+
# IE64-RELA-NEXT: 0x30438 R_LARCH_TLS_DESC64 d 0x0
110106
# IE64-RELA-NEXT: }
111107
# IE64-RELA: Hex dump of section '.got':
108+
# IE64-RELA-NEXT: 0x00030418 00000000 00000000 00000000 00000000 .
112109
# IE64-RELA-NEXT: 0x00030428 00000000 00000000 00000000 00000000 .
113110
# IE64-RELA-NEXT: 0x00030438 00000000 00000000 00000000 00000000 .
114111
# IE64-RELA-NEXT: 0x00030448 00000000 00000000 00000000 00000000 .
115-
# IE64-RELA-NEXT: 0x00030458 00000000 00000000 00000000 00000000 .
116112

117113
## a and b are optimized to use LE. c and d are optimized to IE.
118114
# IE64-LABEL: <.text>:
119-
## &.got[a]-. = 0x30428 - 0x202f8: 0x10 pages, page offset 0x428
120-
# IE64-NEXT: 202f8: pcalau12i $a0, 16
121-
# IE64-NEXT: addi.d $a0, $a0, 1064
115+
## &.got[a]-. = 0x30418 - 0x202f8 = 16456<<2
116+
# IE64-NEXT: 202f8: pcaddi $a0, 16456
122117
# IE64-NEXT: ld.d $ra, $a0, 0
123118
# IE64-NEXT: jirl $ra, $ra, 0
124119
# IE64-NEXT: add.d $a1, $a0, $tp
125-
## &.got[b]-. = 0x30458 - 0x2030c: 0x10 pages, page offset 0x458
126-
# IE64-NEXT: 2030c: pcalau12i $a0, 16
127-
# IE64-NEXT: addi.d $a0, $a0, 1112
120+
## &.got[b]-. = 0x30448 - 0x20308 = 16464<<2
121+
# IE64-NEXT: 20308: pcaddi $a0, 16464
128122
# IE64-NEXT: ld.d $ra, $a0, 0
129123
# IE64-NEXT: jirl $ra, $ra, 0
130124
# IE64-NEXT: add.d $a2, $a0, $tp
131-
## &.got[c]-. = 0x30438 - 0x20320: 0x10 pages, page offset 0x438
132-
# IE64-NEXT: 20320: pcalau12i $a0, 16
133-
# IE64-NEXT: addi.d $a0, $a0, 1080
125+
## &.got[c]-. = 0x30428 - 0x20318 = 16452<<2
126+
# IE64-NEXT: 20318: pcaddi $a0, 16452
134127
# IE64-NEXT: ld.d $ra, $a0, 0
135128
# IE64-NEXT: jirl $ra, $ra, 0
136129
# IE64-NEXT: add.d $a3, $a0, $tp
137-
## &.got[d]-. = 0x30448 - 0x20334: 0x10 pages, page offset 0x448
138-
# IE64-NEXT: 20334: pcalau12i $a0, 16
139-
# IE64-NEXT: addi.d $a0, $a0, 1096
130+
## &.got[d]-. = 0x30438 - 0x20328 = 16452<<2
131+
# IE64-NEXT: 20328: pcaddi $a0, 16452
140132
# IE64-NEXT: ld.d $ra, $a0, 0
141133
# IE64-NEXT: jirl $ra, $ra, 0
142134
# IE64-NEXT: add.d $a4, $a0, $tp

0 commit comments

Comments
 (0)