Skip to content

Commit 4ec06b1

Browse files
authored
[ELF] Change Ctx::target to unique_ptr (llvm#111260)
also rename `TargetInfo *getXXXTargetInfo` to `void setXXXTargetInfo` and change it to set `ctx.target`. This ensures that when `ctx` becomes a local variable, two lld invocations will not reuse the function-local static variable.
1 parent cfd3289 commit 4ec06b1

19 files changed

+81
-122
lines changed

lld/ELF/Arch/AArch64.cpp

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

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

lld/ELF/Arch/AMDGPU.cpp

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

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

lld/ELF/Arch/ARM.cpp

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

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

15411538
template void elf::writeARMCmseImportLib<ELF32LE>(Ctx &);
15421539
template void elf::writeARMCmseImportLib<ELF32BE>(Ctx &);

lld/ELF/Arch/AVR.cpp

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

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

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

lld/ELF/Arch/Hexagon.cpp

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

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

lld/ELF/Arch/LoongArch.cpp

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

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

lld/ELF/Arch/MSP430.cpp

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

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

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

lld/ELF/Arch/PPC.cpp

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

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

lld/ELF/Arch/PPC64.cpp

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

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

lld/ELF/Arch/RISCV.cpp

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

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

lld/ELF/Arch/SPARCV9.cpp

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

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

lld/ELF/Arch/SystemZ.cpp

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

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

lld/ELF/Arch/X86.cpp

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

709-
TargetInfo *elf::getX86TargetInfo(Ctx &ctx) {
709+
void elf::setX86TargetInfo(Ctx &ctx) {
710710
if (ctx.arg.zRetpolineplt) {
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;
711+
if (ctx.arg.isPic)
712+
ctx.target.reset(new RetpolinePic(ctx));
713+
else
714+
ctx.target.reset(new RetpolineNoPic(ctx));
715+
return;
722716
}
723717

724-
static X86 t(ctx);
725-
return &t;
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));
726722
}

lld/ELF/Arch/X86_64.cpp

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

1240-
TargetInfo *elf::getX86_64TargetInfo(Ctx &ctx) {
1240+
void elf::setX86_64TargetInfo(Ctx &ctx) {
12411241
if (ctx.arg.zRetpolineplt) {
1242-
if (ctx.arg.zNow) {
1243-
static RetpolineZNow t(ctx);
1244-
return &t;
1245-
}
1246-
static Retpoline t(ctx);
1247-
return &t;
1248-
}
1249-
1250-
if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
1251-
static IntelIBT t(ctx);
1252-
return &t;
1242+
if (ctx.arg.zNow)
1243+
ctx.target.reset(new RetpolineZNow(ctx));
1244+
else
1245+
ctx.target.reset(new Retpoline(ctx));
1246+
return;
12531247
}
12541248

1255-
static X86_64 t(ctx);
1256-
return &t;
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));
12571253
}

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-
TargetInfo *target;
548+
std::unique_ptr<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 = nullptr;
102+
target.reset();
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-
ctx.target = getTarget(ctx);
3129+
setTarget(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-
TargetInfo *elf::getTarget(Ctx &ctx) {
48+
void elf::setTarget(Ctx &ctx) {
4949
switch (ctx.arg.emachine) {
5050
case EM_386:
5151
case EM_IAMCU:
52-
return getX86TargetInfo(ctx);
52+
return setX86TargetInfo(ctx);
5353
case EM_AARCH64:
54-
return getAArch64TargetInfo(ctx);
54+
return setAArch64TargetInfo(ctx);
5555
case EM_AMDGPU:
56-
return getAMDGPUTargetInfo(ctx);
56+
return setAMDGPUTargetInfo(ctx);
5757
case EM_ARM:
58-
return getARMTargetInfo(ctx);
58+
return setARMTargetInfo(ctx);
5959
case EM_AVR:
60-
return getAVRTargetInfo(ctx);
60+
return setAVRTargetInfo(ctx);
6161
case EM_HEXAGON:
62-
return getHexagonTargetInfo(ctx);
62+
return setHexagonTargetInfo(ctx);
6363
case EM_LOONGARCH:
64-
return getLoongArchTargetInfo(ctx);
64+
return setLoongArchTargetInfo(ctx);
6565
case EM_MIPS:
66-
return getMipsTargetInfo(ctx);
66+
return setMipsTargetInfo(ctx);
6767
case EM_MSP430:
68-
return getMSP430TargetInfo(ctx);
68+
return setMSP430TargetInfo(ctx);
6969
case EM_PPC:
70-
return getPPCTargetInfo(ctx);
70+
return setPPCTargetInfo(ctx);
7171
case EM_PPC64:
72-
return getPPC64TargetInfo(ctx);
72+
return setPPC64TargetInfo(ctx);
7373
case EM_RISCV:
74-
return getRISCVTargetInfo(ctx);
74+
return setRISCVTargetInfo(ctx);
7575
case EM_SPARCV9:
76-
return getSPARCV9TargetInfo(ctx);
76+
return setSPARCV9TargetInfo(ctx);
7777
case EM_S390:
78-
return getSystemZTargetInfo(ctx);
78+
return setSystemZTargetInfo(ctx);
7979
case EM_X86_64:
80-
return getX86_64TargetInfo(ctx);
80+
return setX86_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-
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 &);
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 &);
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-
TargetInfo *getTarget(Ctx &);
254+
void setTarget(Ctx &);
255255

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

0 commit comments

Comments
 (0)