Skip to content

Commit 4ec06b1

Browse files
authored
[ELF] Change Ctx::target to unique_ptr (#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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 4 deletions
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)); }

0 commit comments

Comments
 (0)