Skip to content

Commit 52b0e2b

Browse files
quic-akaryakiquic-seaswara
authored andcommitted
[RISC-V] Add -no-relax-tlsdesc
Signed-off-by: Alexey Karyakin <akaryaki@qti.qualcomm.com>
1 parent a89e538 commit 52b0e2b

5 files changed

Lines changed: 85 additions & 36 deletions

File tree

include/eld/Config/GeneralOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,10 @@ class GeneralOptions {
928928

929929
bool getRISCVRelaxXqci() const { return BRiscvRelaxXqci; }
930930

931+
void setRISCVRelaxTLSDESC(bool Value) { BRiscvRelaxTLSDESC = Value; }
932+
933+
bool getRISCVRelaxTLSDESC() const { return BRiscvRelaxTLSDESC; }
934+
931935
bool warnCommon() const { return BWarnCommon; }
932936

933937
void setWarnCommon() { BWarnCommon = true; }
@@ -1291,6 +1295,7 @@ class GeneralOptions {
12911295
bool RiscvGPRelax = true; // GP relaxation
12921296
bool BRiscvRelaxToC = true; // enable riscv relax to compressed code
12931297
bool BRiscvRelaxXqci = false; // enable riscv relaxations for xqci
1298+
bool BRiscvRelaxTLSDESC = true; // enable riscv relaxations for TLSDESC
12941299
bool AllowIncompatibleSectionsMix = false; // Allow incompatibleSections;
12951300
bool ProgressBar = false; // Show progressbar.
12961301
bool RecordInputFiles = false; // --reproduce

include/eld/Driver/RISCVLinkerOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def no_riscv_relax_xqci
5050
HelpText<"Disable relaxing to/from Xqci instructions (default behavior)">,
5151
Group<grp_riscv_linker>;
5252

53+
def no_relax_tlsdesc
54+
: Flag<["--"], "no-relax-tlsdesc">,
55+
HelpText<"Disable relaxing TLSDESC instruction sequences">,
56+
Group<grp_riscv_linker>;
57+
5358
def riscv_relax : Flag<["--"], "relax">,
5459
HelpText<"Enable relaxation (default behavior)">,
5560
Group<grp_riscv_linker>;

lib/LinkerWrapper/RISCVLinkDriver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ RISCVLinkDriver::parseOptions(ArrayRef<const char *> Args,
135135
ArgList.hasFlag(OPT_RISCVLinkOptTable::riscv_relax_xqci,
136136
OPT_RISCVLinkOptTable::no_riscv_relax_xqci, false));
137137

138+
// --no-relax-tlsdesc
139+
if (ArgList.hasArg(OPT_RISCVLinkOptTable::no_relax_tlsdesc))
140+
Config.options().setRISCVRelaxTLSDESC(false);
141+
138142
// --enable-bss-mixing
139143
if (ArgList.hasArg(OPT_RISCVLinkOptTable::enable_bss_mixing))
140144
Config.options().setAllowBSSMixing(true);

lib/Target/RISCV/RISCVLDBackend.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,8 @@ bool RISCVLDBackend::doRelaxationTLSDESC(Relocation &R, bool Relax) {
662662
// different type. The conditions and the instruction substitution rules are
663663
// the same whether or not relaxation is enabled.
664664
auto attempt = [&]() -> bool {
665-
bool Relaxed = Relax && config().options().getRISCVRelax();
665+
bool Relaxed = Relax && config().options().getRISCVRelax() &&
666+
config().options().getRISCVRelaxTLSDESC();
666667
if (Relaxed)
667668
relaxDeleteBytes(RelaxType, *region, offset, 4, Sym.name());
668669
else {

test/lld/ELF/riscv-tlsdesc-relax.test

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,37 @@ RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.a.so | FileCheck %s --ch
1212
## Test the TLSDESC to LE optimization. Also check --emit-relocs.
1313
RUN: %link %linkopts -z now --no-align-segments --section-start .text=0x11000 %t.a.o %t.c.o -o %t.a.le --emit-relocs
1414
RUN: %readelf --dyn-relocations %t.a.le | FileCheck %s --match-full-lines --allow-empty --check-prefix=LE-REL
15-
RUN: %objdump --no-show-raw-insn -M no-aliases -hdr %t.a.le | FileCheck %s --check-prefix=LE --check-prefix=LER
15+
RUN: %objdump --no-show-raw-insn -M no-aliases -hdr %t.a.le | FileCheck %s --check-prefix=LE --check-prefix=LER --check-prefix=LE-RELAX --check-prefix=LER-RELAX
1616

1717
RUN: %link %linkopts -z now --no-align-segments --section-start .text=0x11000 %t.aa.o %t.c.o -o %t.aa.le
1818
RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.aa.le | FileCheck %s --check-prefix=LEA
1919

2020
## Test the TLSDESC to IE optimization.
2121
RUN: %link %linkopts -z now --no-align-segments --section-start .text=0x11000 --section-start .got=0x120e0 %t.a.o %t.c.so -o %t.a.ie
2222
RUN: %readelf --dyn-relocations %t.a.ie | FileCheck %s --match-full-lines --check-prefix=IE-REL%xlen
23-
RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.a.ie | FileCheck %s --check-prefix=IE --check-prefix=IE%xlen
23+
RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.a.ie | FileCheck %s --check-prefix=IE --check-prefix=IE%xlen --check-prefix=IE-RELAX --check-prefix=IE%xlen-RELAX
24+
25+
## Same as above but with --no-relax-tlsdesc, only check assembly output.
26+
RUN: %link %linkopts -z now --no-relax-tlsdesc --no-align-segments --section-start .text=0x11000 %t.a.o %t.c.o -o %t.a.norelax.le --emit-relocs
27+
RUN: %objdump --no-show-raw-insn -M no-aliases -hdr %t.a.norelax.le | FileCheck %s --check-prefix=LE --check-prefix=LER --check-prefix=LE-NORELAX --check-prefix=LER-NORELAX
28+
29+
RUN: %link %linkopts -z now --no-relax-tlsdesc --no-align-segments --section-start .text=0x11000 --section-start .got=0x120e0 %t.a.o %t.c.so -o %t.a.norelax.ie
30+
RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.a.norelax.ie | FileCheck %s --check-prefix=IE --check-prefix=IE%xlen --check-prefix=IE-NORELAX --check-prefix=IE%xlen-NORELAX
2431

2532
## The same tests for executables are repeated with -pie, except for output static relocation checks as --emit-relocs does not work with -pie.
2633

2734
## Test the TLSDESC to LE optimization. Also check --emit-relocs.
2835
RUN: %link %linkopts -pie -z now --keep-labels --no-align-segments --section-start .text=0x11000 %t.a.o %t.c.o -o %t.a.pie.le --emit-relocs
2936
RUN: %readelf --dyn-relocations %t.a.pie.le | FileCheck %s --match-full-lines --allow-empty --check-prefix=LE-REL
30-
RUN: %objdump --no-show-raw-insn -M no-aliases -hdr %t.a.pie.le | FileCheck %s --check-prefix=LE
37+
RUN: %objdump --no-show-raw-insn -M no-aliases -hdr %t.a.pie.le | FileCheck %s --check-prefix=LE --check-prefix=LE-RELAX
3138

3239
RUN: %link %linkopts -pie -z now --keep-labels --no-align-segments --section-start .text=0x11000 %t.aa.o %t.c.o -o %t.aa.pie.le
3340
RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.aa.pie.le | FileCheck %s --check-prefix=LEA
3441

3542
## Test the TLSDESC to IE optimization.
3643
RUN: %link %linkopts -pie -z now --keep-labels --no-align-segments --section-start .text=0x11000 --section-start .got=0x120e0 %t.a.o %t.c.so -o %t.a.pie.ie
3744
RUN: %readelf --dyn-relocations %t.a.pie.ie | FileCheck %s --match-full-lines --check-prefix=IE-REL%xlen
38-
RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.a.pie.ie | FileCheck %s --check-prefix=IE --check-prefix=IE%xlen
45+
RUN: %objdump --no-show-raw-insn -M no-aliases -h -d %t.a.pie.ie | FileCheck %s --check-prefix=IE --check-prefix=IE%xlen --check-prefix=IE-RELAX --check-prefix=IE%xlen-RELAX
3946

4047
## .got has 3 additional header slots before the TLS slot.
4148

@@ -92,50 +99,64 @@ GD-NEXT: c.add a0, tp
9299
LE-REL-NOT: R_RISCV
93100

94101
LE-LABEL: <foo>:
102+
LE-NORELAX-NEXT: addi zero, zero, 0x0
103+
LER-NORELAX-NEXT: R_RISCV_NONE b
104+
LER-NORELAX-NEXT: R_RISCV_RELAX *ABS*
95105
LE-NEXT: c.add a7, a7
96106
LE-NEXT-LLD: R_RISCV_TLSDESC_HI20 b
107+
LE-NORELAX-NEXT: addi zero, zero, 0x0
97108
LER-NEXT: R_RISCV_NONE b
98109
LER-NEXT: R_RISCV_RELAX *ABS*
99110
LE-NEXT: c.add a7, a7
100111
LE-NEXT-LLD: R_RISCV_TLSDESC_LOAD_LO12 .Ltlsdesc_hi0
112+
LE-NORELAX-NEXT: addi zero, zero, 0x0
101113
LER-NEXT: R_RISCV_NONE b
102114
LER-NEXT: R_RISCV_RELAX *ABS*
103-
LE-NEXT: 11008: c.add a7, a7
104-
LE-NEXT-LLD: R_RISCV_TLSDESC_ADD_LO12 .Ltlsdesc_hi0
105-
LER-NEXT: R_RISCV_NONE b
106-
LER-NEXT: R_RISCV_RELAX *ABS*
115+
LE-NORELAX-NEXT: 11014: c.add a7, a7
116+
LE-RELAX-NEXT: 11008: c.add a7, a7
117+
LE-NEXT-LLD: R_RISCV_TLSDESC_LOAD_LO12 .Ltlsdesc_hi0
118+
LER-RELAX-NEXT: R_RISCV_NONE b
119+
LER-RELAX-NEXT: R_RISCV_RELAX *ABS*
120+
107121
LE-NEXT: addi a0, zero, 0x7ff
108122
LE-NEXT-LLD: R_RISCV_TLSDESC_CALL .Ltlsdesc_hi0
109123
LER-NEXT: R_RISCV_LO12_I b
110124
LER-NEXT: R_RISCV_RELAX *ABS*
111125
LE-NEXT: c.add a0, tp
112126

113127
LE-LABEL: <.Ltlsdesc_hi1>:
128+
LE-NORELAX-NEXT: addi zero, zero, 0x0
129+
LER-NORELAX-NEXT: R_RISCV_NONE b
130+
LER-NORELAX-NEXT: R_RISCV_RELAX *ABS*
131+
LE-NORELAX-NEXT: addi zero, zero, 0x0
132+
LER-NORELAX-NEXT: R_RISCV_NONE b
133+
LE-NORELAX-NEXT: addi zero, zero, 0x0
134+
LER-NORELAX-NEXT: R_RISCV_NONE b
114135
LE-NEXT: addi a0, zero, 0x7ff
115-
LE-NEXT-LLD: R_RISCV_TLSDESC_HI20 b
116-
LER-NEXT: R_RISCV_NONE b
117-
LER-NEXT: R_RISCV_RELAX *ABS*
118-
LE-NEXT-LLD: R_RISCV_TLSDESC_LOAD_LO12 .Ltlsdesc_hi1
119-
LE-NEXT-LLD: R_RISCV_TLSDESC_ADD_LO12 .Ltlsdesc_hi1
120-
LER-NEXT: R_RISCV_NONE b
121-
LER-NEXT: R_RISCV_NONE b
122-
LE-NEXT-LLD: R_RISCV_TLSDESC_CALL .Ltlsdesc_hi1
136+
LE-NEXT-LLD: R_RISCV_TLSDESC_HI20 b
137+
LER-RELAX-NEXT: R_RISCV_NONE b
138+
LER-RELAX-NEXT: R_RISCV_RELAX *ABS*
139+
LE-NEXT-LLD: R_RISCV_TLSDESC_LOAD_LO12 .Ltlsdesc_hi1
140+
LE-NEXT-LLD: R_RISCV_TLSDESC_ADD_LO12 .Ltlsdesc_hi1
141+
LER-RELAX-NEXT: R_RISCV_NONE b
142+
LER-RELAX-NEXT: R_RISCV_NONE b
143+
LE-NEXT-LLD: R_RISCV_TLSDESC_CALL .Ltlsdesc_hi1
123144
LER-NEXT: R_RISCV_LO12_I b
124145
LE-NEXT: c.add a0, tp
125146
LE-LABEL: <.Ltlsdesc_hi2>:
126147
LE-NEXT: addi zero, zero, 0x0
127-
LER-NEXT-LLD: R_RISCV_TLSDESC_HI20 b
148+
LER-NEXT-LLD: R_RISCV_TLSDESC_HI20 b
128149
LER-NEXT: R_RISCV_NONE b
129150
LE-NEXT: addi zero, zero, 0x0
130-
LE-NEXT-LLD: R_RISCV_TLSDESC_LOAD_LO12 .Ltlsdesc_hi2
151+
LE-NEXT-LLD: R_RISCV_TLSDESC_LOAD_LO12 .Ltlsdesc_hi2
131152
LER-NEXT: R_RISCV_NONE b
132153
LER-NEXT: R_RISCV_RELAX *ABS*
133154
LE-NEXT: addi zero, zero, 0x0
134-
LE-NEXT-LLD: R_RISCV_TLSDESC_ADD_LO12 .Ltlsdesc_hi2
155+
LE-NEXT-LLD: R_RISCV_TLSDESC_ADD_LO12 .Ltlsdesc_hi2
135156
LER-NEXT: R_RISCV_NONE b
136157
LER-NEXT: R_RISCV_RELAX *ABS*
137158
LE-NEXT: addi a0, zero, 0x7ff
138-
LE-NEXT-LLD: R_RISCV_TLSDESC_CALL .Ltlsdesc_hi2
159+
LE-NEXT-LLD: R_RISCV_TLSDESC_CALL .Ltlsdesc_hi2
139160
LER-NEXT: R_RISCV_LO12_I b
140161
LE-NEXT: c.add a0, tp
141162

@@ -165,29 +186,42 @@ IE-REL4: {{0*}}120e4 {{0*}}010a R_RISCV_TLS_TPREL32 {{0+}} b + 0
165186
IE-REL8: {{0*}}120e8 {{0*}}010000000b R_RISCV_TLS_TPREL64 {{0+}} b + 0
166187

167188
IE-LABEL: <foo>:
189+
IE-NORELAX-NEXT: addi zero, zero, 0x0
168190
IE-NEXT: c.add a7, a7
191+
IE-NORELAX-NEXT: addi zero, zero, 0x0
169192
IE-NEXT: c.add a7, a7
170-
## &.got[c]-. = 0x120e4 - 0x11008 = 0x10dc
171-
## &.got[c]-. = 0x120e8 - 0x11008 = 0x10e0
172-
IE-NEXT: 11008: auipc a0, 0x1
193+
## &.got[c]-. = 0x120e4 - 0x11008 = 0x10dc (8 bytes less if previous nops are not relaxed)
194+
## &.got[c]-. = 0x120e8 - 0x11008 = 0x10e0 (8 bytes less if previous nops are not relaxed)
195+
IE-NORELAX-NEXT: 11010: auipc a0, 0x1
196+
IE-RELAX-NEXT: 11008: auipc a0, 0x1
173197
IE-NEXT: c.add a7, a7
174-
IE4-NEXT: lw a0, 0xdc(a0)
175-
IE8-NEXT: ld a0, 0xe0(a0)
198+
IE4-NORELAX-NEXT: lw a0, 0xd4(a0)
199+
IE4-RELAX-NEXT: lw a0, 0xdc(a0)
200+
IE8-NORELAX-NEXT: ld a0, 0xd8(a0)
201+
IE8-RELAX-NEXT: ld a0, 0xe0(a0)
176202
IE-NEXT: c.add a0, tp
177203

178204
IE-LABEL: <.Ltlsdesc_hi1>:
179-
## &.got[c]-. = 0x120e4 - 0x11018 = 0x10cc
180-
## &.got[c]-. = 0x120e8 - 0x11018 = 0x10d0
181-
IE-NEXT: 11018: auipc a0, 0x1
182-
IE4-NEXT: lw a0, 0xcc(a0)
183-
IE8-NEXT: ld a0, 0xd0(a0)
205+
## &.got[c]-. = 0x120e4 - 0x11018 = 0x10cc (16 bytes less if previous nops are not relaxed)
206+
## &.got[c]-. = 0x120e8 - 0x11018 = 0x10d0 (16 bytes less if previous nops are not relaxed)
207+
IE-NORELAX-NEXT: addi zero, zero, 0x0
208+
IE-NORELAX-NEXT: addi zero, zero, 0x0
209+
IE-NORELAX-NEXT: 11028: auipc a0, 0x1
210+
IE-RELAX-NEXT: 11018: auipc a0, 0x1
211+
IE4-NORELAX-NEXT: lw a0, 0xbc(a0)
212+
IE4-RELAX-NEXT: lw a0, 0xcc(a0)
213+
IE8-NORELAX-NEXT: ld a0, 0xc0(a0)
214+
IE8-RELAX-NEXT: ld a0, 0xd0(a0)
184215
IE-NEXT: c.add a0, tp
185-
## &.got[c]-. = 0x120e4 - 0x1102a = 0x10ba
186-
## &.got[c]-. = 0x120e8 - 0x1102a = 0x10be
216+
## &.got[c]-. = 0x120e4 - 0x1102a = 0x10ba (16 bytes less if previous nops are not relaxed)
217+
## &.got[c]-. = 0x120e8 - 0x1102a = 0x10be (16 bytes less if previous nops are not relaxed)
187218
IE-LABEL: <.Ltlsdesc_hi2>:
188219
IE-NEXT: addi zero, zero, 0x0
189220
IE-NEXT: addi zero, zero, 0x0
190-
IE-NEXT: 1102a: auipc a0, 0x1
191-
IE4-NEXT: lw a0, 0xba(a0)
192-
IE8-NEXT: ld a0, 0xbe(a0)
221+
IE-NORELAX-NEXT: 1103a: auipc a0, 0x1
222+
IE-RELAX-NEXT: 1102a: auipc a0, 0x1
223+
IE4-NORELAX-NEXT: lw a0, 0xaa(a0)
224+
IE4-RELAX-NEXT: lw a0, 0xba(a0)
225+
IE8-NORELAX-NEXT: ld a0, 0xae(a0)
226+
IE8-RELAX-NEXT: ld a0, 0xbe(a0)
193227
IE-NEXT: c.add a0, tp

0 commit comments

Comments
 (0)