[lld] e1a073c - [ELF] Change Ctx::target to unique_ptr (#111260)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 7 23:14:08 PDT 2024
Author: Fangrui Song
Date: 2024-10-07T23:14:02-07:00
New Revision: e1a073c9d9b85804c46353c5a66c44fe212525ed
URL: https://github.com/llvm/llvm-project/commit/e1a073c9d9b85804c46353c5a66c44fe212525ed
DIFF: https://github.com/llvm/llvm-project/commit/e1a073c9d9b85804c46353c5a66c44fe212525ed.diff
LOG: [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.
---
Reland after commit c35214c131c0bc7f54dc18ceb75c75cba89f58ee
([ELF] Initialize TargetInfo members).
Added:
Modified:
lld/ELF/Arch/AArch64.cpp
lld/ELF/Arch/AMDGPU.cpp
lld/ELF/Arch/ARM.cpp
lld/ELF/Arch/AVR.cpp
lld/ELF/Arch/Hexagon.cpp
lld/ELF/Arch/LoongArch.cpp
lld/ELF/Arch/MSP430.cpp
lld/ELF/Arch/Mips.cpp
lld/ELF/Arch/PPC.cpp
lld/ELF/Arch/PPC64.cpp
lld/ELF/Arch/RISCV.cpp
lld/ELF/Arch/SPARCV9.cpp
lld/ELF/Arch/SystemZ.cpp
lld/ELF/Arch/X86.cpp
lld/ELF/Arch/X86_64.cpp
lld/ELF/Config.h
lld/ELF/Driver.cpp
lld/ELF/Target.cpp
lld/ELF/Target.h
Removed:
################################################################################
diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 33a1db86dd3c70..1368e209c8cfd1 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -1208,12 +1208,10 @@ void elf::createTaggedSymbols(Ctx &ctx) {
}
}
-TargetInfo *elf::getAArch64TargetInfo(Ctx &ctx) {
+void elf::setAArch64TargetInfo(Ctx &ctx) {
if ((ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
- ctx.arg.zPacPlt) {
- static AArch64BtiPac t(ctx);
- return &t;
- }
- static AArch64 t(ctx);
- return &t;
+ ctx.arg.zPacPlt)
+ ctx.target.reset(new AArch64BtiPac(ctx));
+ else
+ ctx.target.reset(new AArch64(ctx));
}
diff --git a/lld/ELF/Arch/AMDGPU.cpp b/lld/ELF/Arch/AMDGPU.cpp
index 1c20abdb82f228..130da19d0e210d 100644
--- a/lld/ELF/Arch/AMDGPU.cpp
+++ b/lld/ELF/Arch/AMDGPU.cpp
@@ -219,7 +219,4 @@ int64_t AMDGPU::getImplicitAddend(const uint8_t *buf, RelType type) const {
}
}
-TargetInfo *elf::getAMDGPUTargetInfo(Ctx &ctx) {
- static AMDGPU target(ctx);
- return ⌖
-}
+void elf::setAMDGPUTargetInfo(Ctx &ctx) { ctx.target.reset(new AMDGPU(ctx)); }
diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp
index 4e45be05e71b6c..43fbbc8d49131a 100644
--- a/lld/ELF/Arch/ARM.cpp
+++ b/lld/ELF/Arch/ARM.cpp
@@ -1533,10 +1533,7 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
"': " + toString(std::move(e)));
}
-TargetInfo *elf::getARMTargetInfo(Ctx &ctx) {
- static ARM target(ctx);
- return ⌖
-}
+void elf::setARMTargetInfo(Ctx &ctx) { ctx.target.reset(new ARM(ctx)); }
template void elf::writeARMCmseImportLib<ELF32LE>(Ctx &);
template void elf::writeARMCmseImportLib<ELF32BE>(Ctx &);
diff --git a/lld/ELF/Arch/AVR.cpp b/lld/ELF/Arch/AVR.cpp
index 24215f0b00e0d5..53c10b89e5db7e 100644
--- a/lld/ELF/Arch/AVR.cpp
+++ b/lld/ELF/Arch/AVR.cpp
@@ -267,10 +267,7 @@ void AVR::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
}
}
-TargetInfo *elf::getAVRTargetInfo(Ctx &ctx) {
- static AVR target(ctx);
- return ⌖
-}
+void elf::setAVRTargetInfo(Ctx &ctx) { ctx.target.reset(new AVR(ctx)); }
static uint32_t getEFlags(InputFile *file) {
return cast<ObjFile<ELF32LE>>(file)->getObj().getHeader().e_flags;
diff --git a/lld/ELF/Arch/Hexagon.cpp b/lld/ELF/Arch/Hexagon.cpp
index de5444c88ad159..5d2477cc800651 100644
--- a/lld/ELF/Arch/Hexagon.cpp
+++ b/lld/ELF/Arch/Hexagon.cpp
@@ -404,7 +404,4 @@ int64_t Hexagon::getImplicitAddend(const uint8_t *buf, RelType type) const {
}
}
-TargetInfo *elf::getHexagonTargetInfo(Ctx &ctx) {
- static Hexagon target(ctx);
- return ⌖
-}
+void elf::setHexagonTargetInfo(Ctx &ctx) { ctx.target.reset(new Hexagon(ctx)); }
diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index 6c910bdf07c8d5..eca1d2fdc08caf 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -893,7 +893,6 @@ void LoongArch::finalizeRelax(int passes) const {
}
}
-TargetInfo *elf::getLoongArchTargetInfo(Ctx &ctx) {
- static LoongArch target(ctx);
- return ⌖
+void elf::setLoongArchTargetInfo(Ctx &ctx) {
+ ctx.target.reset(new LoongArch(ctx));
}
diff --git a/lld/ELF/Arch/MSP430.cpp b/lld/ELF/Arch/MSP430.cpp
index d2fd401835dbc6..b3aab52e179c97 100644
--- a/lld/ELF/Arch/MSP430.cpp
+++ b/lld/ELF/Arch/MSP430.cpp
@@ -88,7 +88,4 @@ void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
}
}
-TargetInfo *elf::getMSP430TargetInfo(Ctx &ctx) {
- static MSP430 target(ctx);
- return ⌖
-}
+void elf::setMSP430TargetInfo(Ctx &ctx) { ctx.target.reset(new MSP430(ctx)); }
diff --git a/lld/ELF/Arch/Mips.cpp b/lld/ELF/Arch/Mips.cpp
index 2ac37e86ec88ec..975fa9ead762d7 100644
--- a/lld/ELF/Arch/Mips.cpp
+++ b/lld/ELF/Arch/Mips.cpp
@@ -779,23 +779,23 @@ template <class ELFT> bool elf::isMipsPIC(const Defined *sym) {
return cast<ObjFile<ELFT>>(file)->getObj().getHeader().e_flags & EF_MIPS_PIC;
}
-TargetInfo *elf::getMipsTargetInfo(Ctx &ctx) {
+void elf::setMipsTargetInfo(Ctx &ctx) {
switch (ctx.arg.ekind) {
case ELF32LEKind: {
- static MIPS<ELF32LE> t(ctx);
- return &t;
+ ctx.target.reset(new MIPS<ELF32LE>(ctx));
+ return;
}
case ELF32BEKind: {
- static MIPS<ELF32BE> t(ctx);
- return &t;
+ ctx.target.reset(new MIPS<ELF32BE>(ctx));
+ return;
}
case ELF64LEKind: {
- static MIPS<ELF64LE> t(ctx);
- return &t;
+ ctx.target.reset(new MIPS<ELF64LE>(ctx));
+ return;
}
case ELF64BEKind: {
- static MIPS<ELF64BE> t(ctx);
- return &t;
+ ctx.target.reset(new MIPS<ELF64BE>(ctx));
+ return;
}
default:
llvm_unreachable("unsupported target");
diff --git a/lld/ELF/Arch/PPC.cpp b/lld/ELF/Arch/PPC.cpp
index ff9970ba494c1c..fa1b2a6c39d494 100644
--- a/lld/ELF/Arch/PPC.cpp
+++ b/lld/ELF/Arch/PPC.cpp
@@ -523,7 +523,4 @@ void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
}
}
-TargetInfo *elf::getPPCTargetInfo(Ctx &ctx) {
- static PPC target(ctx);
- return ⌖
-}
+void elf::setPPCTargetInfo(Ctx &ctx) { ctx.target.reset(new PPC(ctx)); }
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 361e30b0843b9a..056d495ff1f7db 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -1747,7 +1747,4 @@ bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
return true;
}
-TargetInfo *elf::getPPC64TargetInfo(Ctx &ctx) {
- static PPC64 target(ctx);
- return ⌖
-}
+void elf::setPPC64TargetInfo(Ctx &ctx) { ctx.target.reset(new PPC64(ctx)); }
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 6970eccf51bdea..57cc26b3f0a3ff 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -1329,7 +1329,4 @@ void elf::mergeRISCVAttributesSections(Ctx &ctx) {
mergeAttributesSection(ctx, sections));
}
-TargetInfo *elf::getRISCVTargetInfo(Ctx &ctx) {
- static RISCV target(ctx);
- return ⌖
-}
+void elf::setRISCVTargetInfo(Ctx &ctx) { ctx.target.reset(new RISCV(ctx)); }
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index 6d72fcbd1dde3a..bc9724c3ba342a 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -193,7 +193,4 @@ void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
}
-TargetInfo *elf::getSPARCV9TargetInfo(Ctx &ctx) {
- static SPARCV9 target(ctx);
- return ⌖
-}
+void elf::setSPARCV9TargetInfo(Ctx &ctx) { ctx.target.reset(new SPARCV9(ctx)); }
diff --git a/lld/ELF/Arch/SystemZ.cpp b/lld/ELF/Arch/SystemZ.cpp
index e1f76e482823a2..1bc3b60af5c44d 100644
--- a/lld/ELF/Arch/SystemZ.cpp
+++ b/lld/ELF/Arch/SystemZ.cpp
@@ -600,7 +600,4 @@ void SystemZ::relocate(uint8_t *loc, const Relocation &rel,
}
}
-TargetInfo *elf::getSystemZTargetInfo(Ctx &ctx) {
- static SystemZ t(ctx);
- return &t;
-}
+void elf::setSystemZTargetInfo(Ctx &ctx) { ctx.target.reset(new SystemZ(ctx)); }
diff --git a/lld/ELF/Arch/X86.cpp b/lld/ELF/Arch/X86.cpp
index 795c783951da5a..3314dcfc172f8c 100644
--- a/lld/ELF/Arch/X86.cpp
+++ b/lld/ELF/Arch/X86.cpp
@@ -706,21 +706,17 @@ void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
write32le(buf + 22, -off - 26);
}
-TargetInfo *elf::getX86TargetInfo(Ctx &ctx) {
+void elf::setX86TargetInfo(Ctx &ctx) {
if (ctx.arg.zRetpolineplt) {
- if (ctx.arg.isPic) {
- static RetpolinePic t(ctx);
- return &t;
- }
- static RetpolineNoPic t(ctx);
- return &t;
- }
-
- if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
- static IntelIBT t(ctx);
- return &t;
+ if (ctx.arg.isPic)
+ ctx.target.reset(new RetpolinePic(ctx));
+ else
+ ctx.target.reset(new RetpolineNoPic(ctx));
+ return;
}
- static X86 t(ctx);
- return &t;
+ if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
+ ctx.target.reset(new IntelIBT(ctx));
+ else
+ ctx.target.reset(new X86(ctx));
}
diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index 5cf5ea366aa7f7..fbf1076fc71e52 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -1237,21 +1237,17 @@ void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym,
write32le(buf + 8, ctx.in.plt->getVA() - pltEntryAddr - 12);
}
-TargetInfo *elf::getX86_64TargetInfo(Ctx &ctx) {
+void elf::setX86_64TargetInfo(Ctx &ctx) {
if (ctx.arg.zRetpolineplt) {
- if (ctx.arg.zNow) {
- static RetpolineZNow t(ctx);
- return &t;
- }
- static Retpoline t(ctx);
- return &t;
- }
-
- if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
- static IntelIBT t(ctx);
- return &t;
+ if (ctx.arg.zNow)
+ ctx.target.reset(new RetpolineZNow(ctx));
+ else
+ ctx.target.reset(new Retpoline(ctx));
+ return;
}
- static X86_64 t(ctx);
- return &t;
+ if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
+ ctx.target.reset(new IntelIBT(ctx));
+ else
+ ctx.target.reset(new X86_64(ctx));
}
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 12a6513fae91dc..f5d65d26ef4479 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -545,7 +545,7 @@ struct Ctx {
Config arg;
LinkerDriver driver;
LinkerScript *script;
- TargetInfo *target;
+ std::unique_ptr<TargetInfo> target;
// These variables are initialized by Writer and should not be used before
// Writer is initialized.
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index cde65de4142df9..43f75bab12775f 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -99,7 +99,7 @@ void Ctx::reset() {
driver.~LinkerDriver();
new (&driver) LinkerDriver(*this);
script = nullptr;
- target = nullptr;
+ target.reset();
bufferStart = nullptr;
mainPart = nullptr;
@@ -3126,7 +3126,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
// The Target instance handles target-specific stuff, such as applying
// relocations or writing a PLT section. It also contains target-dependent
// values such as a default image base address.
- ctx.target = getTarget(ctx);
+ setTarget(ctx);
ctx.arg.eflags = ctx.target->calcEFlags();
// maxPageSize (sometimes called abi page size) is the maximum page size that
diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp
index 08f12b08bf80b6..96d132a2a8f369 100644
--- a/lld/ELF/Target.cpp
+++ b/lld/ELF/Target.cpp
@@ -45,39 +45,39 @@ std::string lld::toString(RelType type) {
return std::string(s);
}
-TargetInfo *elf::getTarget(Ctx &ctx) {
+void elf::setTarget(Ctx &ctx) {
switch (ctx.arg.emachine) {
case EM_386:
case EM_IAMCU:
- return getX86TargetInfo(ctx);
+ return setX86TargetInfo(ctx);
case EM_AARCH64:
- return getAArch64TargetInfo(ctx);
+ return setAArch64TargetInfo(ctx);
case EM_AMDGPU:
- return getAMDGPUTargetInfo(ctx);
+ return setAMDGPUTargetInfo(ctx);
case EM_ARM:
- return getARMTargetInfo(ctx);
+ return setARMTargetInfo(ctx);
case EM_AVR:
- return getAVRTargetInfo(ctx);
+ return setAVRTargetInfo(ctx);
case EM_HEXAGON:
- return getHexagonTargetInfo(ctx);
+ return setHexagonTargetInfo(ctx);
case EM_LOONGARCH:
- return getLoongArchTargetInfo(ctx);
+ return setLoongArchTargetInfo(ctx);
case EM_MIPS:
- return getMipsTargetInfo(ctx);
+ return setMipsTargetInfo(ctx);
case EM_MSP430:
- return getMSP430TargetInfo(ctx);
+ return setMSP430TargetInfo(ctx);
case EM_PPC:
- return getPPCTargetInfo(ctx);
+ return setPPCTargetInfo(ctx);
case EM_PPC64:
- return getPPC64TargetInfo(ctx);
+ return setPPC64TargetInfo(ctx);
case EM_RISCV:
- return getRISCVTargetInfo(ctx);
+ return setRISCVTargetInfo(ctx);
case EM_SPARCV9:
- return getSPARCV9TargetInfo(ctx);
+ return setSPARCV9TargetInfo(ctx);
case EM_S390:
- return getSystemZTargetInfo(ctx);
+ return setSystemZTargetInfo(ctx);
case EM_X86_64:
- return getX86_64TargetInfo(ctx);
+ return setX86_64TargetInfo(ctx);
default:
fatal("unsupported e_machine value: " + Twine(ctx.arg.emachine));
}
diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h
index ca2706aefc263b..fc905956b9312a 100644
--- a/lld/ELF/Target.h
+++ b/lld/ELF/Target.h
@@ -179,21 +179,21 @@ class TargetInfo {
uint64_t defaultImageBase = 0x10000;
};
-TargetInfo *getAArch64TargetInfo(Ctx &);
-TargetInfo *getAMDGPUTargetInfo(Ctx &);
-TargetInfo *getARMTargetInfo(Ctx &);
-TargetInfo *getAVRTargetInfo(Ctx &);
-TargetInfo *getHexagonTargetInfo(Ctx &);
-TargetInfo *getLoongArchTargetInfo(Ctx &);
-TargetInfo *getMSP430TargetInfo(Ctx &);
-TargetInfo *getMipsTargetInfo(Ctx &);
-TargetInfo *getPPC64TargetInfo(Ctx &);
-TargetInfo *getPPCTargetInfo(Ctx &);
-TargetInfo *getRISCVTargetInfo(Ctx &);
-TargetInfo *getSPARCV9TargetInfo(Ctx &);
-TargetInfo *getSystemZTargetInfo(Ctx &);
-TargetInfo *getX86TargetInfo(Ctx &);
-TargetInfo *getX86_64TargetInfo(Ctx &);
+void setAArch64TargetInfo(Ctx &);
+void setAMDGPUTargetInfo(Ctx &);
+void setARMTargetInfo(Ctx &);
+void setAVRTargetInfo(Ctx &);
+void setHexagonTargetInfo(Ctx &);
+void setLoongArchTargetInfo(Ctx &);
+void setMSP430TargetInfo(Ctx &);
+void setMipsTargetInfo(Ctx &);
+void setPPC64TargetInfo(Ctx &);
+void setPPCTargetInfo(Ctx &);
+void setRISCVTargetInfo(Ctx &);
+void setSPARCV9TargetInfo(Ctx &);
+void setSystemZTargetInfo(Ctx &);
+void setX86TargetInfo(Ctx &);
+void setX86_64TargetInfo(Ctx &);
struct ErrorPlace {
InputSectionBase *isec;
@@ -251,7 +251,7 @@ void convertArmInstructionstoBE8(InputSection *sec, uint8_t *buf);
void createTaggedSymbols(Ctx &);
void initSymbolAnchors(Ctx &);
-TargetInfo *getTarget(Ctx &);
+void setTarget(Ctx &);
template <class ELFT> bool isMipsPIC(const Defined *sym);
More information about the llvm-commits
mailing list