Skip to content

Commit 2ca8501

Browse files
authored
Revert "[ELF] Change Ctx::target to unique_ptr (#111260)" (#111449)
This patch seems to be breaking the windows build bots. https://lab.llvm.org/buildbot/#/builders/63/builds/1953 We also see this in Fuchsia's Linux CI: https://fxbug.dev/372010530 This reverts commit 4ec06b1.
1 parent 376b5c0 commit 2ca8501

19 files changed

+122
-81
lines changed

lld/ELF/Arch/AArch64.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -1208,10 +1208,12 @@ void elf::createTaggedSymbols(Ctx &ctx) {
12081208
}
12091209
}
12101210

1211-
void elf::setAArch64TargetInfo(Ctx &ctx) {
1211+
TargetInfo *elf::getAArch64TargetInfo(Ctx &ctx) {
12121212
if ((ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
1213-
ctx.arg.zPacPlt)
1214-
ctx.target.reset(new AArch64BtiPac(ctx));
1215-
else
1216-
ctx.target.reset(new AArch64(ctx));
1213+
ctx.arg.zPacPlt) {
1214+
static AArch64BtiPac t(ctx);
1215+
return &t;
1216+
}
1217+
static AArch64 t(ctx);
1218+
return &t;
12171219
}

lld/ELF/Arch/AMDGPU.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,7 @@ int64_t AMDGPU::getImplicitAddend(const uint8_t *buf, RelType type) const {
219219
}
220220
}
221221

222-
void elf::setAMDGPUTargetInfo(Ctx &ctx) { ctx.target.reset(new AMDGPU(ctx)); }
222+
TargetInfo *elf::getAMDGPUTargetInfo(Ctx &ctx) {
223+
static AMDGPU target(ctx);
224+
return ⌖
225+
}

lld/ELF/Arch/ARM.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,10 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
15331533
"': " + toString(std::move(e)));
15341534
}
15351535

1536-
void elf::setARMTargetInfo(Ctx &ctx) { ctx.target.reset(new ARM(ctx)); }
1536+
TargetInfo *elf::getARMTargetInfo(Ctx &ctx) {
1537+
static ARM target(ctx);
1538+
return &target;
1539+
}
15371540

15381541
template void elf::writeARMCmseImportLib<ELF32LE>(Ctx &);
15391542
template void elf::writeARMCmseImportLib<ELF32BE>(Ctx &);

lld/ELF/Arch/AVR.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ void AVR::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
267267
}
268268
}
269269

270-
void elf::setAVRTargetInfo(Ctx &ctx) { ctx.target.reset(new AVR(ctx)); }
270+
TargetInfo *elf::getAVRTargetInfo(Ctx &ctx) {
271+
static AVR target(ctx);
272+
return &target;
273+
}
271274

272275
static uint32_t getEFlags(InputFile *file) {
273276
return cast<ObjFile<ELF32LE>>(file)->getObj().getHeader().e_flags;

lld/ELF/Arch/Hexagon.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,7 @@ int64_t Hexagon::getImplicitAddend(const uint8_t *buf, RelType type) const {
404404
}
405405
}
406406

407-
void elf::setHexagonTargetInfo(Ctx &ctx) { ctx.target.reset(new Hexagon(ctx)); }
407+
TargetInfo *elf::getHexagonTargetInfo(Ctx &ctx) {
408+
static Hexagon target(ctx);
409+
return &target;
410+
}

lld/ELF/Arch/LoongArch.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ void LoongArch::finalizeRelax(int passes) const {
893893
}
894894
}
895895

896-
void elf::setLoongArchTargetInfo(Ctx &ctx) {
897-
ctx.target.reset(new LoongArch(ctx));
896+
TargetInfo *elf::getLoongArchTargetInfo(Ctx &ctx) {
897+
static LoongArch target(ctx);
898+
return &target;
898899
}

lld/ELF/Arch/MSP430.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,7 @@ void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
8888
}
8989
}
9090

91-
void elf::setMSP430TargetInfo(Ctx &ctx) { ctx.target.reset(new MSP430(ctx)); }
91+
TargetInfo *elf::getMSP430TargetInfo(Ctx &ctx) {
92+
static MSP430 target(ctx);
93+
return &target;
94+
}

lld/ELF/Arch/Mips.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -779,23 +779,23 @@ template <class ELFT> bool elf::isMipsPIC(const Defined *sym) {
779779
return cast<ObjFile<ELFT>>(file)->getObj().getHeader().e_flags & EF_MIPS_PIC;
780780
}
781781

782-
void elf::setMipsTargetInfo(Ctx &ctx) {
782+
TargetInfo *elf::getMipsTargetInfo(Ctx &ctx) {
783783
switch (ctx.arg.ekind) {
784784
case ELF32LEKind: {
785-
ctx.target.reset(new MIPS<ELF32LE>(ctx));
786-
return;
785+
static MIPS<ELF32LE> t(ctx);
786+
return &t;
787787
}
788788
case ELF32BEKind: {
789-
ctx.target.reset(new MIPS<ELF32BE>(ctx));
790-
return;
789+
static MIPS<ELF32BE> t(ctx);
790+
return &t;
791791
}
792792
case ELF64LEKind: {
793-
ctx.target.reset(new MIPS<ELF64LE>(ctx));
794-
return;
793+
static MIPS<ELF64LE> t(ctx);
794+
return &t;
795795
}
796796
case ELF64BEKind: {
797-
ctx.target.reset(new MIPS<ELF64BE>(ctx));
798-
return;
797+
static MIPS<ELF64BE> t(ctx);
798+
return &t;
799799
}
800800
default:
801801
llvm_unreachable("unsupported target");

lld/ELF/Arch/PPC.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,7 @@ void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
523523
}
524524
}
525525

526-
void elf::setPPCTargetInfo(Ctx &ctx) { ctx.target.reset(new PPC(ctx)); }
526+
TargetInfo *elf::getPPCTargetInfo(Ctx &ctx) {
527+
static PPC target(ctx);
528+
return &target;
529+
}

lld/ELF/Arch/PPC64.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1747,4 +1747,7 @@ bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
17471747
return true;
17481748
}
17491749

1750-
void elf::setPPC64TargetInfo(Ctx &ctx) { ctx.target.reset(new PPC64(ctx)); }
1750+
TargetInfo *elf::getPPC64TargetInfo(Ctx &ctx) {
1751+
static PPC64 target(ctx);
1752+
return &target;
1753+
}

lld/ELF/Arch/RISCV.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1329,4 +1329,7 @@ void elf::mergeRISCVAttributesSections(Ctx &ctx) {
13291329
mergeAttributesSection(ctx, sections));
13301330
}
13311331

1332-
void elf::setRISCVTargetInfo(Ctx &ctx) { ctx.target.reset(new RISCV(ctx)); }
1332+
TargetInfo *elf::getRISCVTargetInfo(Ctx &ctx) {
1333+
static RISCV target(ctx);
1334+
return &target;
1335+
}

lld/ELF/Arch/SPARCV9.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,7 @@ void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
193193
relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
194194
}
195195

196-
void elf::setSPARCV9TargetInfo(Ctx &ctx) { ctx.target.reset(new SPARCV9(ctx)); }
196+
TargetInfo *elf::getSPARCV9TargetInfo(Ctx &ctx) {
197+
static SPARCV9 target(ctx);
198+
return &target;
199+
}

lld/ELF/Arch/SystemZ.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -600,4 +600,7 @@ void SystemZ::relocate(uint8_t *loc, const Relocation &rel,
600600
}
601601
}
602602

603-
void elf::setSystemZTargetInfo(Ctx &ctx) { ctx.target.reset(new SystemZ(ctx)); }
603+
TargetInfo *elf::getSystemZTargetInfo(Ctx &ctx) {
604+
static SystemZ t(ctx);
605+
return &t;
606+
}

lld/ELF/Arch/X86.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -706,17 +706,21 @@ void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
706706
write32le(buf + 22, -off - 26);
707707
}
708708

709-
void elf::setX86TargetInfo(Ctx &ctx) {
709+
TargetInfo *elf::getX86TargetInfo(Ctx &ctx) {
710710
if (ctx.arg.zRetpolineplt) {
711-
if (ctx.arg.isPic)
712-
ctx.target.reset(new RetpolinePic(ctx));
713-
else
714-
ctx.target.reset(new RetpolineNoPic(ctx));
715-
return;
711+
if (ctx.arg.isPic) {
712+
static RetpolinePic t(ctx);
713+
return &t;
714+
}
715+
static RetpolineNoPic t(ctx);
716+
return &t;
717+
}
718+
719+
if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
720+
static IntelIBT t(ctx);
721+
return &t;
716722
}
717723

718-
if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
719-
ctx.target.reset(new IntelIBT(ctx));
720-
else
721-
ctx.target.reset(new X86(ctx));
724+
static X86 t(ctx);
725+
return &t;
722726
}

lld/ELF/Arch/X86_64.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -1237,17 +1237,21 @@ void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym,
12371237
write32le(buf + 8, ctx.in.plt->getVA() - pltEntryAddr - 12);
12381238
}
12391239

1240-
void elf::setX86_64TargetInfo(Ctx &ctx) {
1240+
TargetInfo *elf::getX86_64TargetInfo(Ctx &ctx) {
12411241
if (ctx.arg.zRetpolineplt) {
1242-
if (ctx.arg.zNow)
1243-
ctx.target.reset(new RetpolineZNow(ctx));
1244-
else
1245-
ctx.target.reset(new Retpoline(ctx));
1246-
return;
1242+
if (ctx.arg.zNow) {
1243+
static RetpolineZNow t(ctx);
1244+
return &t;
1245+
}
1246+
static Retpoline t(ctx);
1247+
return &t;
12471248
}
12481249

1249-
if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
1250-
ctx.target.reset(new IntelIBT(ctx));
1251-
else
1252-
ctx.target.reset(new X86_64(ctx));
1250+
if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
1251+
static IntelIBT t(ctx);
1252+
return &t;
1253+
}
1254+
1255+
static X86_64 t(ctx);
1256+
return &t;
12531257
}

lld/ELF/Config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ struct Ctx {
545545
Config arg;
546546
LinkerDriver driver;
547547
LinkerScript *script;
548-
std::unique_ptr<TargetInfo> target;
548+
TargetInfo *target;
549549

550550
// These variables are initialized by Writer and should not be used before
551551
// Writer is initialized.

lld/ELF/Driver.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void Ctx::reset() {
9999
driver.~LinkerDriver();
100100
new (&driver) LinkerDriver(*this);
101101
script = nullptr;
102-
target.reset();
102+
target = nullptr;
103103

104104
bufferStart = nullptr;
105105
mainPart = nullptr;
@@ -3126,7 +3126,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31263126
// The Target instance handles target-specific stuff, such as applying
31273127
// relocations or writing a PLT section. It also contains target-dependent
31283128
// values such as a default image base address.
3129-
setTarget(ctx);
3129+
ctx.target = getTarget(ctx);
31303130

31313131
ctx.arg.eflags = ctx.target->calcEFlags();
31323132
// maxPageSize (sometimes called abi page size) is the maximum page size that

lld/ELF/Target.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,39 @@ std::string lld::toString(RelType type) {
4545
return std::string(s);
4646
}
4747

48-
void elf::setTarget(Ctx &ctx) {
48+
TargetInfo *elf::getTarget(Ctx &ctx) {
4949
switch (ctx.arg.emachine) {
5050
case EM_386:
5151
case EM_IAMCU:
52-
return setX86TargetInfo(ctx);
52+
return getX86TargetInfo(ctx);
5353
case EM_AARCH64:
54-
return setAArch64TargetInfo(ctx);
54+
return getAArch64TargetInfo(ctx);
5555
case EM_AMDGPU:
56-
return setAMDGPUTargetInfo(ctx);
56+
return getAMDGPUTargetInfo(ctx);
5757
case EM_ARM:
58-
return setARMTargetInfo(ctx);
58+
return getARMTargetInfo(ctx);
5959
case EM_AVR:
60-
return setAVRTargetInfo(ctx);
60+
return getAVRTargetInfo(ctx);
6161
case EM_HEXAGON:
62-
return setHexagonTargetInfo(ctx);
62+
return getHexagonTargetInfo(ctx);
6363
case EM_LOONGARCH:
64-
return setLoongArchTargetInfo(ctx);
64+
return getLoongArchTargetInfo(ctx);
6565
case EM_MIPS:
66-
return setMipsTargetInfo(ctx);
66+
return getMipsTargetInfo(ctx);
6767
case EM_MSP430:
68-
return setMSP430TargetInfo(ctx);
68+
return getMSP430TargetInfo(ctx);
6969
case EM_PPC:
70-
return setPPCTargetInfo(ctx);
70+
return getPPCTargetInfo(ctx);
7171
case EM_PPC64:
72-
return setPPC64TargetInfo(ctx);
72+
return getPPC64TargetInfo(ctx);
7373
case EM_RISCV:
74-
return setRISCVTargetInfo(ctx);
74+
return getRISCVTargetInfo(ctx);
7575
case EM_SPARCV9:
76-
return setSPARCV9TargetInfo(ctx);
76+
return getSPARCV9TargetInfo(ctx);
7777
case EM_S390:
78-
return setSystemZTargetInfo(ctx);
78+
return getSystemZTargetInfo(ctx);
7979
case EM_X86_64:
80-
return setX86_64TargetInfo(ctx);
80+
return getX86_64TargetInfo(ctx);
8181
default:
8282
fatal("unsupported e_machine value: " + Twine(ctx.arg.emachine));
8383
}

lld/ELF/Target.h

+16-16
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,21 @@ class TargetInfo {
179179
uint64_t defaultImageBase = 0x10000;
180180
};
181181

182-
void setAArch64TargetInfo(Ctx &);
183-
void setAMDGPUTargetInfo(Ctx &);
184-
void setARMTargetInfo(Ctx &);
185-
void setAVRTargetInfo(Ctx &);
186-
void setHexagonTargetInfo(Ctx &);
187-
void setLoongArchTargetInfo(Ctx &);
188-
void setMSP430TargetInfo(Ctx &);
189-
void setMipsTargetInfo(Ctx &);
190-
void setPPC64TargetInfo(Ctx &);
191-
void setPPCTargetInfo(Ctx &);
192-
void setRISCVTargetInfo(Ctx &);
193-
void setSPARCV9TargetInfo(Ctx &);
194-
void setSystemZTargetInfo(Ctx &);
195-
void setX86TargetInfo(Ctx &);
196-
void setX86_64TargetInfo(Ctx &);
182+
TargetInfo *getAArch64TargetInfo(Ctx &);
183+
TargetInfo *getAMDGPUTargetInfo(Ctx &);
184+
TargetInfo *getARMTargetInfo(Ctx &);
185+
TargetInfo *getAVRTargetInfo(Ctx &);
186+
TargetInfo *getHexagonTargetInfo(Ctx &);
187+
TargetInfo *getLoongArchTargetInfo(Ctx &);
188+
TargetInfo *getMSP430TargetInfo(Ctx &);
189+
TargetInfo *getMipsTargetInfo(Ctx &);
190+
TargetInfo *getPPC64TargetInfo(Ctx &);
191+
TargetInfo *getPPCTargetInfo(Ctx &);
192+
TargetInfo *getRISCVTargetInfo(Ctx &);
193+
TargetInfo *getSPARCV9TargetInfo(Ctx &);
194+
TargetInfo *getSystemZTargetInfo(Ctx &);
195+
TargetInfo *getX86TargetInfo(Ctx &);
196+
TargetInfo *getX86_64TargetInfo(Ctx &);
197197

198198
struct ErrorPlace {
199199
InputSectionBase *isec;
@@ -251,7 +251,7 @@ void convertArmInstructionstoBE8(InputSection *sec, uint8_t *buf);
251251
void createTaggedSymbols(Ctx &);
252252
void initSymbolAnchors(Ctx &);
253253

254-
void setTarget(Ctx &);
254+
TargetInfo *getTarget(Ctx &);
255255

256256
template <class ELFT> bool isMipsPIC(const Defined *sym);
257257

0 commit comments

Comments
 (0)