Skip to content

Revert "[ELF] Change Ctx::target to unique_ptr (#111260)" #111449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Oct 7, 2024

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.

Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2024

@llvm/pr-subscribers-backend-hexagon

@llvm/pr-subscribers-lld

Author: Paul Kirth (ilovepi)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/111449.diff

19 Files Affected:

  • (modified) lld/ELF/Arch/AArch64.cpp (+7-5)
  • (modified) lld/ELF/Arch/AMDGPU.cpp (+4-1)
  • (modified) lld/ELF/Arch/ARM.cpp (+4-1)
  • (modified) lld/ELF/Arch/AVR.cpp (+4-1)
  • (modified) lld/ELF/Arch/Hexagon.cpp (+4-1)
  • (modified) lld/ELF/Arch/LoongArch.cpp (+3-2)
  • (modified) lld/ELF/Arch/MSP430.cpp (+4-1)
  • (modified) lld/ELF/Arch/Mips.cpp (+9-9)
  • (modified) lld/ELF/Arch/PPC.cpp (+4-1)
  • (modified) lld/ELF/Arch/PPC64.cpp (+4-1)
  • (modified) lld/ELF/Arch/RISCV.cpp (+4-1)
  • (modified) lld/ELF/Arch/SPARCV9.cpp (+4-1)
  • (modified) lld/ELF/Arch/SystemZ.cpp (+4-1)
  • (modified) lld/ELF/Arch/X86.cpp (+14-10)
  • (modified) lld/ELF/Arch/X86_64.cpp (+14-10)
  • (modified) lld/ELF/Config.h (+1-1)
  • (modified) lld/ELF/Driver.cpp (+2-2)
  • (modified) lld/ELF/Target.cpp (+16-16)
  • (modified) lld/ELF/Target.h (+16-16)
diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 1368e209c8cfd1..33a1db86dd3c70 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -1208,10 +1208,12 @@ void elf::createTaggedSymbols(Ctx &ctx) {
   }
 }
 
-void elf::setAArch64TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getAArch64TargetInfo(Ctx &ctx) {
   if ((ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
-      ctx.arg.zPacPlt)
-    ctx.target.reset(new AArch64BtiPac(ctx));
-  else
-    ctx.target.reset(new AArch64(ctx));
+      ctx.arg.zPacPlt) {
+    static AArch64BtiPac t(ctx);
+    return &t;
+  }
+  static AArch64 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Arch/AMDGPU.cpp b/lld/ELF/Arch/AMDGPU.cpp
index 130da19d0e210d..1c20abdb82f228 100644
--- a/lld/ELF/Arch/AMDGPU.cpp
+++ b/lld/ELF/Arch/AMDGPU.cpp
@@ -219,4 +219,7 @@ int64_t AMDGPU::getImplicitAddend(const uint8_t *buf, RelType type) const {
   }
 }
 
-void elf::setAMDGPUTargetInfo(Ctx &ctx) { ctx.target.reset(new AMDGPU(ctx)); }
+TargetInfo *elf::getAMDGPUTargetInfo(Ctx &ctx) {
+  static AMDGPU target(ctx);
+  return ⌖
+}
diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp
index 43fbbc8d49131a..4e45be05e71b6c 100644
--- a/lld/ELF/Arch/ARM.cpp
+++ b/lld/ELF/Arch/ARM.cpp
@@ -1533,7 +1533,10 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
           "': " + toString(std::move(e)));
 }
 
-void elf::setARMTargetInfo(Ctx &ctx) { ctx.target.reset(new ARM(ctx)); }
+TargetInfo *elf::getARMTargetInfo(Ctx &ctx) {
+  static ARM target(ctx);
+  return &target;
+}
 
 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 53c10b89e5db7e..24215f0b00e0d5 100644
--- a/lld/ELF/Arch/AVR.cpp
+++ b/lld/ELF/Arch/AVR.cpp
@@ -267,7 +267,10 @@ void AVR::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
   }
 }
 
-void elf::setAVRTargetInfo(Ctx &ctx) { ctx.target.reset(new AVR(ctx)); }
+TargetInfo *elf::getAVRTargetInfo(Ctx &ctx) {
+  static AVR target(ctx);
+  return &target;
+}
 
 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 5d2477cc800651..de5444c88ad159 100644
--- a/lld/ELF/Arch/Hexagon.cpp
+++ b/lld/ELF/Arch/Hexagon.cpp
@@ -404,4 +404,7 @@ int64_t Hexagon::getImplicitAddend(const uint8_t *buf, RelType type) const {
   }
 }
 
-void elf::setHexagonTargetInfo(Ctx &ctx) { ctx.target.reset(new Hexagon(ctx)); }
+TargetInfo *elf::getHexagonTargetInfo(Ctx &ctx) {
+  static Hexagon target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index eca1d2fdc08caf..6c910bdf07c8d5 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -893,6 +893,7 @@ void LoongArch::finalizeRelax(int passes) const {
   }
 }
 
-void elf::setLoongArchTargetInfo(Ctx &ctx) {
-  ctx.target.reset(new LoongArch(ctx));
+TargetInfo *elf::getLoongArchTargetInfo(Ctx &ctx) {
+  static LoongArch target(ctx);
+  return &target;
 }
diff --git a/lld/ELF/Arch/MSP430.cpp b/lld/ELF/Arch/MSP430.cpp
index b3aab52e179c97..d2fd401835dbc6 100644
--- a/lld/ELF/Arch/MSP430.cpp
+++ b/lld/ELF/Arch/MSP430.cpp
@@ -88,4 +88,7 @@ void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
   }
 }
 
-void elf::setMSP430TargetInfo(Ctx &ctx) { ctx.target.reset(new MSP430(ctx)); }
+TargetInfo *elf::getMSP430TargetInfo(Ctx &ctx) {
+  static MSP430 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/Mips.cpp b/lld/ELF/Arch/Mips.cpp
index 975fa9ead762d7..2ac37e86ec88ec 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;
 }
 
-void elf::setMipsTargetInfo(Ctx &ctx) {
+TargetInfo *elf::getMipsTargetInfo(Ctx &ctx) {
   switch (ctx.arg.ekind) {
   case ELF32LEKind: {
-    ctx.target.reset(new MIPS<ELF32LE>(ctx));
-    return;
+    static MIPS<ELF32LE> t(ctx);
+    return &t;
   }
   case ELF32BEKind: {
-    ctx.target.reset(new MIPS<ELF32BE>(ctx));
-    return;
+    static MIPS<ELF32BE> t(ctx);
+    return &t;
   }
   case ELF64LEKind: {
-    ctx.target.reset(new MIPS<ELF64LE>(ctx));
-    return;
+    static MIPS<ELF64LE> t(ctx);
+    return &t;
   }
   case ELF64BEKind: {
-    ctx.target.reset(new MIPS<ELF64BE>(ctx));
-    return;
+    static MIPS<ELF64BE> t(ctx);
+    return &t;
   }
   default:
     llvm_unreachable("unsupported target");
diff --git a/lld/ELF/Arch/PPC.cpp b/lld/ELF/Arch/PPC.cpp
index fa1b2a6c39d494..ff9970ba494c1c 100644
--- a/lld/ELF/Arch/PPC.cpp
+++ b/lld/ELF/Arch/PPC.cpp
@@ -523,4 +523,7 @@ void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
   }
 }
 
-void elf::setPPCTargetInfo(Ctx &ctx) { ctx.target.reset(new PPC(ctx)); }
+TargetInfo *elf::getPPCTargetInfo(Ctx &ctx) {
+  static PPC target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 056d495ff1f7db..361e30b0843b9a 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -1747,4 +1747,7 @@ bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
   return true;
 }
 
-void elf::setPPC64TargetInfo(Ctx &ctx) { ctx.target.reset(new PPC64(ctx)); }
+TargetInfo *elf::getPPC64TargetInfo(Ctx &ctx) {
+  static PPC64 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 57cc26b3f0a3ff..6970eccf51bdea 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -1329,4 +1329,7 @@ void elf::mergeRISCVAttributesSections(Ctx &ctx) {
                            mergeAttributesSection(ctx, sections));
 }
 
-void elf::setRISCVTargetInfo(Ctx &ctx) { ctx.target.reset(new RISCV(ctx)); }
+TargetInfo *elf::getRISCVTargetInfo(Ctx &ctx) {
+  static RISCV target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index bc9724c3ba342a..6d72fcbd1dde3a 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -193,4 +193,7 @@ void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
   relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
 }
 
-void elf::setSPARCV9TargetInfo(Ctx &ctx) { ctx.target.reset(new SPARCV9(ctx)); }
+TargetInfo *elf::getSPARCV9TargetInfo(Ctx &ctx) {
+  static SPARCV9 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/SystemZ.cpp b/lld/ELF/Arch/SystemZ.cpp
index 1bc3b60af5c44d..e1f76e482823a2 100644
--- a/lld/ELF/Arch/SystemZ.cpp
+++ b/lld/ELF/Arch/SystemZ.cpp
@@ -600,4 +600,7 @@ void SystemZ::relocate(uint8_t *loc, const Relocation &rel,
   }
 }
 
-void elf::setSystemZTargetInfo(Ctx &ctx) { ctx.target.reset(new SystemZ(ctx)); }
+TargetInfo *elf::getSystemZTargetInfo(Ctx &ctx) {
+  static SystemZ t(ctx);
+  return &t;
+}
diff --git a/lld/ELF/Arch/X86.cpp b/lld/ELF/Arch/X86.cpp
index 3314dcfc172f8c..795c783951da5a 100644
--- a/lld/ELF/Arch/X86.cpp
+++ b/lld/ELF/Arch/X86.cpp
@@ -706,17 +706,21 @@ void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
   write32le(buf + 22, -off - 26);
 }
 
-void elf::setX86TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getX86TargetInfo(Ctx &ctx) {
   if (ctx.arg.zRetpolineplt) {
-    if (ctx.arg.isPic)
-      ctx.target.reset(new RetpolinePic(ctx));
-    else
-      ctx.target.reset(new RetpolineNoPic(ctx));
-    return;
+    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.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
-    ctx.target.reset(new IntelIBT(ctx));
-  else
-    ctx.target.reset(new X86(ctx));
+  static X86 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index fbf1076fc71e52..5cf5ea366aa7f7 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -1237,17 +1237,21 @@ void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym,
   write32le(buf + 8, ctx.in.plt->getVA() - pltEntryAddr - 12);
 }
 
-void elf::setX86_64TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getX86_64TargetInfo(Ctx &ctx) {
   if (ctx.arg.zRetpolineplt) {
-    if (ctx.arg.zNow)
-      ctx.target.reset(new RetpolineZNow(ctx));
-    else
-      ctx.target.reset(new Retpoline(ctx));
-    return;
+    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)
-    ctx.target.reset(new IntelIBT(ctx));
-  else
-    ctx.target.reset(new X86_64(ctx));
+  if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
+    static IntelIBT t(ctx);
+    return &t;
+  }
+
+  static X86_64 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index f5d65d26ef4479..12a6513fae91dc 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -545,7 +545,7 @@ struct Ctx {
   Config arg;
   LinkerDriver driver;
   LinkerScript *script;
-  std::unique_ptr<TargetInfo> target;
+  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 43f75bab12775f..cde65de4142df9 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.reset();
+  target = nullptr;
 
   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.
-  setTarget(ctx);
+  ctx.target = getTarget(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 96d132a2a8f369..08f12b08bf80b6 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);
 }
 
-void elf::setTarget(Ctx &ctx) {
+TargetInfo *elf::getTarget(Ctx &ctx) {
   switch (ctx.arg.emachine) {
   case EM_386:
   case EM_IAMCU:
-    return setX86TargetInfo(ctx);
+    return getX86TargetInfo(ctx);
   case EM_AARCH64:
-    return setAArch64TargetInfo(ctx);
+    return getAArch64TargetInfo(ctx);
   case EM_AMDGPU:
-    return setAMDGPUTargetInfo(ctx);
+    return getAMDGPUTargetInfo(ctx);
   case EM_ARM:
-    return setARMTargetInfo(ctx);
+    return getARMTargetInfo(ctx);
   case EM_AVR:
-    return setAVRTargetInfo(ctx);
+    return getAVRTargetInfo(ctx);
   case EM_HEXAGON:
-    return setHexagonTargetInfo(ctx);
+    return getHexagonTargetInfo(ctx);
   case EM_LOONGARCH:
-    return setLoongArchTargetInfo(ctx);
+    return getLoongArchTargetInfo(ctx);
   case EM_MIPS:
-    return setMipsTargetInfo(ctx);
+    return getMipsTargetInfo(ctx);
   case EM_MSP430:
-    return setMSP430TargetInfo(ctx);
+    return getMSP430TargetInfo(ctx);
   case EM_PPC:
-    return setPPCTargetInfo(ctx);
+    return getPPCTargetInfo(ctx);
   case EM_PPC64:
-    return setPPC64TargetInfo(ctx);
+    return getPPC64TargetInfo(ctx);
   case EM_RISCV:
-    return setRISCVTargetInfo(ctx);
+    return getRISCVTargetInfo(ctx);
   case EM_SPARCV9:
-    return setSPARCV9TargetInfo(ctx);
+    return getSPARCV9TargetInfo(ctx);
   case EM_S390:
-    return setSystemZTargetInfo(ctx);
+    return getSystemZTargetInfo(ctx);
   case EM_X86_64:
-    return setX86_64TargetInfo(ctx);
+    return getX86_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 f3eb421b494d89..0bc5e3881190e5 100644
--- a/lld/ELF/Target.h
+++ b/lld/ELF/Target.h
@@ -179,21 +179,21 @@ class TargetInfo {
   uint64_t defaultImageBase = 0x10000;
 };
 
-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 &);
+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 &);
 
 struct ErrorPlace {
   InputSectionBase *isec;
@@ -251,7 +251,7 @@ void convertArmInstructionstoBE8(InputSection *sec, uint8_t *buf);
 void createTaggedSymbols(Ctx &);
 void initSymbolAnchors(Ctx &);
 
-void setTarget(Ctx &);
+TargetInfo *getTarget(Ctx &);
 
 template <class ELFT> bool isMipsPIC(const Defined *sym);
 

@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2024

@llvm/pr-subscribers-lld-elf

Author: Paul Kirth (ilovepi)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/111449.diff

19 Files Affected:

  • (modified) lld/ELF/Arch/AArch64.cpp (+7-5)
  • (modified) lld/ELF/Arch/AMDGPU.cpp (+4-1)
  • (modified) lld/ELF/Arch/ARM.cpp (+4-1)
  • (modified) lld/ELF/Arch/AVR.cpp (+4-1)
  • (modified) lld/ELF/Arch/Hexagon.cpp (+4-1)
  • (modified) lld/ELF/Arch/LoongArch.cpp (+3-2)
  • (modified) lld/ELF/Arch/MSP430.cpp (+4-1)
  • (modified) lld/ELF/Arch/Mips.cpp (+9-9)
  • (modified) lld/ELF/Arch/PPC.cpp (+4-1)
  • (modified) lld/ELF/Arch/PPC64.cpp (+4-1)
  • (modified) lld/ELF/Arch/RISCV.cpp (+4-1)
  • (modified) lld/ELF/Arch/SPARCV9.cpp (+4-1)
  • (modified) lld/ELF/Arch/SystemZ.cpp (+4-1)
  • (modified) lld/ELF/Arch/X86.cpp (+14-10)
  • (modified) lld/ELF/Arch/X86_64.cpp (+14-10)
  • (modified) lld/ELF/Config.h (+1-1)
  • (modified) lld/ELF/Driver.cpp (+2-2)
  • (modified) lld/ELF/Target.cpp (+16-16)
  • (modified) lld/ELF/Target.h (+16-16)
diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 1368e209c8cfd1..33a1db86dd3c70 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -1208,10 +1208,12 @@ void elf::createTaggedSymbols(Ctx &ctx) {
   }
 }
 
-void elf::setAArch64TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getAArch64TargetInfo(Ctx &ctx) {
   if ((ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
-      ctx.arg.zPacPlt)
-    ctx.target.reset(new AArch64BtiPac(ctx));
-  else
-    ctx.target.reset(new AArch64(ctx));
+      ctx.arg.zPacPlt) {
+    static AArch64BtiPac t(ctx);
+    return &t;
+  }
+  static AArch64 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Arch/AMDGPU.cpp b/lld/ELF/Arch/AMDGPU.cpp
index 130da19d0e210d..1c20abdb82f228 100644
--- a/lld/ELF/Arch/AMDGPU.cpp
+++ b/lld/ELF/Arch/AMDGPU.cpp
@@ -219,4 +219,7 @@ int64_t AMDGPU::getImplicitAddend(const uint8_t *buf, RelType type) const {
   }
 }
 
-void elf::setAMDGPUTargetInfo(Ctx &ctx) { ctx.target.reset(new AMDGPU(ctx)); }
+TargetInfo *elf::getAMDGPUTargetInfo(Ctx &ctx) {
+  static AMDGPU target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp
index 43fbbc8d49131a..4e45be05e71b6c 100644
--- a/lld/ELF/Arch/ARM.cpp
+++ b/lld/ELF/Arch/ARM.cpp
@@ -1533,7 +1533,10 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
           "': " + toString(std::move(e)));
 }
 
-void elf::setARMTargetInfo(Ctx &ctx) { ctx.target.reset(new ARM(ctx)); }
+TargetInfo *elf::getARMTargetInfo(Ctx &ctx) {
+  static ARM target(ctx);
+  return &target;
+}
 
 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 53c10b89e5db7e..24215f0b00e0d5 100644
--- a/lld/ELF/Arch/AVR.cpp
+++ b/lld/ELF/Arch/AVR.cpp
@@ -267,7 +267,10 @@ void AVR::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
   }
 }
 
-void elf::setAVRTargetInfo(Ctx &ctx) { ctx.target.reset(new AVR(ctx)); }
+TargetInfo *elf::getAVRTargetInfo(Ctx &ctx) {
+  static AVR target(ctx);
+  return &target;
+}
 
 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 5d2477cc800651..de5444c88ad159 100644
--- a/lld/ELF/Arch/Hexagon.cpp
+++ b/lld/ELF/Arch/Hexagon.cpp
@@ -404,4 +404,7 @@ int64_t Hexagon::getImplicitAddend(const uint8_t *buf, RelType type) const {
   }
 }
 
-void elf::setHexagonTargetInfo(Ctx &ctx) { ctx.target.reset(new Hexagon(ctx)); }
+TargetInfo *elf::getHexagonTargetInfo(Ctx &ctx) {
+  static Hexagon target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index eca1d2fdc08caf..6c910bdf07c8d5 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -893,6 +893,7 @@ void LoongArch::finalizeRelax(int passes) const {
   }
 }
 
-void elf::setLoongArchTargetInfo(Ctx &ctx) {
-  ctx.target.reset(new LoongArch(ctx));
+TargetInfo *elf::getLoongArchTargetInfo(Ctx &ctx) {
+  static LoongArch target(ctx);
+  return &target;
 }
diff --git a/lld/ELF/Arch/MSP430.cpp b/lld/ELF/Arch/MSP430.cpp
index b3aab52e179c97..d2fd401835dbc6 100644
--- a/lld/ELF/Arch/MSP430.cpp
+++ b/lld/ELF/Arch/MSP430.cpp
@@ -88,4 +88,7 @@ void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
   }
 }
 
-void elf::setMSP430TargetInfo(Ctx &ctx) { ctx.target.reset(new MSP430(ctx)); }
+TargetInfo *elf::getMSP430TargetInfo(Ctx &ctx) {
+  static MSP430 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/Mips.cpp b/lld/ELF/Arch/Mips.cpp
index 975fa9ead762d7..2ac37e86ec88ec 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;
 }
 
-void elf::setMipsTargetInfo(Ctx &ctx) {
+TargetInfo *elf::getMipsTargetInfo(Ctx &ctx) {
   switch (ctx.arg.ekind) {
   case ELF32LEKind: {
-    ctx.target.reset(new MIPS<ELF32LE>(ctx));
-    return;
+    static MIPS<ELF32LE> t(ctx);
+    return &t;
   }
   case ELF32BEKind: {
-    ctx.target.reset(new MIPS<ELF32BE>(ctx));
-    return;
+    static MIPS<ELF32BE> t(ctx);
+    return &t;
   }
   case ELF64LEKind: {
-    ctx.target.reset(new MIPS<ELF64LE>(ctx));
-    return;
+    static MIPS<ELF64LE> t(ctx);
+    return &t;
   }
   case ELF64BEKind: {
-    ctx.target.reset(new MIPS<ELF64BE>(ctx));
-    return;
+    static MIPS<ELF64BE> t(ctx);
+    return &t;
   }
   default:
     llvm_unreachable("unsupported target");
diff --git a/lld/ELF/Arch/PPC.cpp b/lld/ELF/Arch/PPC.cpp
index fa1b2a6c39d494..ff9970ba494c1c 100644
--- a/lld/ELF/Arch/PPC.cpp
+++ b/lld/ELF/Arch/PPC.cpp
@@ -523,4 +523,7 @@ void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
   }
 }
 
-void elf::setPPCTargetInfo(Ctx &ctx) { ctx.target.reset(new PPC(ctx)); }
+TargetInfo *elf::getPPCTargetInfo(Ctx &ctx) {
+  static PPC target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 056d495ff1f7db..361e30b0843b9a 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -1747,4 +1747,7 @@ bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
   return true;
 }
 
-void elf::setPPC64TargetInfo(Ctx &ctx) { ctx.target.reset(new PPC64(ctx)); }
+TargetInfo *elf::getPPC64TargetInfo(Ctx &ctx) {
+  static PPC64 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 57cc26b3f0a3ff..6970eccf51bdea 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -1329,4 +1329,7 @@ void elf::mergeRISCVAttributesSections(Ctx &ctx) {
                            mergeAttributesSection(ctx, sections));
 }
 
-void elf::setRISCVTargetInfo(Ctx &ctx) { ctx.target.reset(new RISCV(ctx)); }
+TargetInfo *elf::getRISCVTargetInfo(Ctx &ctx) {
+  static RISCV target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index bc9724c3ba342a..6d72fcbd1dde3a 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -193,4 +193,7 @@ void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
   relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
 }
 
-void elf::setSPARCV9TargetInfo(Ctx &ctx) { ctx.target.reset(new SPARCV9(ctx)); }
+TargetInfo *elf::getSPARCV9TargetInfo(Ctx &ctx) {
+  static SPARCV9 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/SystemZ.cpp b/lld/ELF/Arch/SystemZ.cpp
index 1bc3b60af5c44d..e1f76e482823a2 100644
--- a/lld/ELF/Arch/SystemZ.cpp
+++ b/lld/ELF/Arch/SystemZ.cpp
@@ -600,4 +600,7 @@ void SystemZ::relocate(uint8_t *loc, const Relocation &rel,
   }
 }
 
-void elf::setSystemZTargetInfo(Ctx &ctx) { ctx.target.reset(new SystemZ(ctx)); }
+TargetInfo *elf::getSystemZTargetInfo(Ctx &ctx) {
+  static SystemZ t(ctx);
+  return &t;
+}
diff --git a/lld/ELF/Arch/X86.cpp b/lld/ELF/Arch/X86.cpp
index 3314dcfc172f8c..795c783951da5a 100644
--- a/lld/ELF/Arch/X86.cpp
+++ b/lld/ELF/Arch/X86.cpp
@@ -706,17 +706,21 @@ void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
   write32le(buf + 22, -off - 26);
 }
 
-void elf::setX86TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getX86TargetInfo(Ctx &ctx) {
   if (ctx.arg.zRetpolineplt) {
-    if (ctx.arg.isPic)
-      ctx.target.reset(new RetpolinePic(ctx));
-    else
-      ctx.target.reset(new RetpolineNoPic(ctx));
-    return;
+    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.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
-    ctx.target.reset(new IntelIBT(ctx));
-  else
-    ctx.target.reset(new X86(ctx));
+  static X86 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index fbf1076fc71e52..5cf5ea366aa7f7 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -1237,17 +1237,21 @@ void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym,
   write32le(buf + 8, ctx.in.plt->getVA() - pltEntryAddr - 12);
 }
 
-void elf::setX86_64TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getX86_64TargetInfo(Ctx &ctx) {
   if (ctx.arg.zRetpolineplt) {
-    if (ctx.arg.zNow)
-      ctx.target.reset(new RetpolineZNow(ctx));
-    else
-      ctx.target.reset(new Retpoline(ctx));
-    return;
+    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)
-    ctx.target.reset(new IntelIBT(ctx));
-  else
-    ctx.target.reset(new X86_64(ctx));
+  if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
+    static IntelIBT t(ctx);
+    return &t;
+  }
+
+  static X86_64 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index f5d65d26ef4479..12a6513fae91dc 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -545,7 +545,7 @@ struct Ctx {
   Config arg;
   LinkerDriver driver;
   LinkerScript *script;
-  std::unique_ptr<TargetInfo> target;
+  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 43f75bab12775f..cde65de4142df9 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.reset();
+  target = nullptr;
 
   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.
-  setTarget(ctx);
+  ctx.target = getTarget(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 96d132a2a8f369..08f12b08bf80b6 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);
 }
 
-void elf::setTarget(Ctx &ctx) {
+TargetInfo *elf::getTarget(Ctx &ctx) {
   switch (ctx.arg.emachine) {
   case EM_386:
   case EM_IAMCU:
-    return setX86TargetInfo(ctx);
+    return getX86TargetInfo(ctx);
   case EM_AARCH64:
-    return setAArch64TargetInfo(ctx);
+    return getAArch64TargetInfo(ctx);
   case EM_AMDGPU:
-    return setAMDGPUTargetInfo(ctx);
+    return getAMDGPUTargetInfo(ctx);
   case EM_ARM:
-    return setARMTargetInfo(ctx);
+    return getARMTargetInfo(ctx);
   case EM_AVR:
-    return setAVRTargetInfo(ctx);
+    return getAVRTargetInfo(ctx);
   case EM_HEXAGON:
-    return setHexagonTargetInfo(ctx);
+    return getHexagonTargetInfo(ctx);
   case EM_LOONGARCH:
-    return setLoongArchTargetInfo(ctx);
+    return getLoongArchTargetInfo(ctx);
   case EM_MIPS:
-    return setMipsTargetInfo(ctx);
+    return getMipsTargetInfo(ctx);
   case EM_MSP430:
-    return setMSP430TargetInfo(ctx);
+    return getMSP430TargetInfo(ctx);
   case EM_PPC:
-    return setPPCTargetInfo(ctx);
+    return getPPCTargetInfo(ctx);
   case EM_PPC64:
-    return setPPC64TargetInfo(ctx);
+    return getPPC64TargetInfo(ctx);
   case EM_RISCV:
-    return setRISCVTargetInfo(ctx);
+    return getRISCVTargetInfo(ctx);
   case EM_SPARCV9:
-    return setSPARCV9TargetInfo(ctx);
+    return getSPARCV9TargetInfo(ctx);
   case EM_S390:
-    return setSystemZTargetInfo(ctx);
+    return getSystemZTargetInfo(ctx);
   case EM_X86_64:
-    return setX86_64TargetInfo(ctx);
+    return getX86_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 f3eb421b494d89..0bc5e3881190e5 100644
--- a/lld/ELF/Target.h
+++ b/lld/ELF/Target.h
@@ -179,21 +179,21 @@ class TargetInfo {
   uint64_t defaultImageBase = 0x10000;
 };
 
-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 &);
+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 &);
 
 struct ErrorPlace {
   InputSectionBase *isec;
@@ -251,7 +251,7 @@ void convertArmInstructionstoBE8(InputSection *sec, uint8_t *buf);
 void createTaggedSymbols(Ctx &);
 void initSymbolAnchors(Ctx &);
 
-void setTarget(Ctx &);
+TargetInfo *getTarget(Ctx &);
 
 template <class ELFT> bool isMipsPIC(const Defined *sym);
 

@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2024

@llvm/pr-subscribers-backend-amdgpu

Author: Paul Kirth (ilovepi)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/111449.diff

19 Files Affected:

  • (modified) lld/ELF/Arch/AArch64.cpp (+7-5)
  • (modified) lld/ELF/Arch/AMDGPU.cpp (+4-1)
  • (modified) lld/ELF/Arch/ARM.cpp (+4-1)
  • (modified) lld/ELF/Arch/AVR.cpp (+4-1)
  • (modified) lld/ELF/Arch/Hexagon.cpp (+4-1)
  • (modified) lld/ELF/Arch/LoongArch.cpp (+3-2)
  • (modified) lld/ELF/Arch/MSP430.cpp (+4-1)
  • (modified) lld/ELF/Arch/Mips.cpp (+9-9)
  • (modified) lld/ELF/Arch/PPC.cpp (+4-1)
  • (modified) lld/ELF/Arch/PPC64.cpp (+4-1)
  • (modified) lld/ELF/Arch/RISCV.cpp (+4-1)
  • (modified) lld/ELF/Arch/SPARCV9.cpp (+4-1)
  • (modified) lld/ELF/Arch/SystemZ.cpp (+4-1)
  • (modified) lld/ELF/Arch/X86.cpp (+14-10)
  • (modified) lld/ELF/Arch/X86_64.cpp (+14-10)
  • (modified) lld/ELF/Config.h (+1-1)
  • (modified) lld/ELF/Driver.cpp (+2-2)
  • (modified) lld/ELF/Target.cpp (+16-16)
  • (modified) lld/ELF/Target.h (+16-16)
diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 1368e209c8cfd1..33a1db86dd3c70 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -1208,10 +1208,12 @@ void elf::createTaggedSymbols(Ctx &ctx) {
   }
 }
 
-void elf::setAArch64TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getAArch64TargetInfo(Ctx &ctx) {
   if ((ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) ||
-      ctx.arg.zPacPlt)
-    ctx.target.reset(new AArch64BtiPac(ctx));
-  else
-    ctx.target.reset(new AArch64(ctx));
+      ctx.arg.zPacPlt) {
+    static AArch64BtiPac t(ctx);
+    return &t;
+  }
+  static AArch64 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Arch/AMDGPU.cpp b/lld/ELF/Arch/AMDGPU.cpp
index 130da19d0e210d..1c20abdb82f228 100644
--- a/lld/ELF/Arch/AMDGPU.cpp
+++ b/lld/ELF/Arch/AMDGPU.cpp
@@ -219,4 +219,7 @@ int64_t AMDGPU::getImplicitAddend(const uint8_t *buf, RelType type) const {
   }
 }
 
-void elf::setAMDGPUTargetInfo(Ctx &ctx) { ctx.target.reset(new AMDGPU(ctx)); }
+TargetInfo *elf::getAMDGPUTargetInfo(Ctx &ctx) {
+  static AMDGPU target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp
index 43fbbc8d49131a..4e45be05e71b6c 100644
--- a/lld/ELF/Arch/ARM.cpp
+++ b/lld/ELF/Arch/ARM.cpp
@@ -1533,7 +1533,10 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
           "': " + toString(std::move(e)));
 }
 
-void elf::setARMTargetInfo(Ctx &ctx) { ctx.target.reset(new ARM(ctx)); }
+TargetInfo *elf::getARMTargetInfo(Ctx &ctx) {
+  static ARM target(ctx);
+  return &target;
+}
 
 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 53c10b89e5db7e..24215f0b00e0d5 100644
--- a/lld/ELF/Arch/AVR.cpp
+++ b/lld/ELF/Arch/AVR.cpp
@@ -267,7 +267,10 @@ void AVR::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
   }
 }
 
-void elf::setAVRTargetInfo(Ctx &ctx) { ctx.target.reset(new AVR(ctx)); }
+TargetInfo *elf::getAVRTargetInfo(Ctx &ctx) {
+  static AVR target(ctx);
+  return &target;
+}
 
 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 5d2477cc800651..de5444c88ad159 100644
--- a/lld/ELF/Arch/Hexagon.cpp
+++ b/lld/ELF/Arch/Hexagon.cpp
@@ -404,4 +404,7 @@ int64_t Hexagon::getImplicitAddend(const uint8_t *buf, RelType type) const {
   }
 }
 
-void elf::setHexagonTargetInfo(Ctx &ctx) { ctx.target.reset(new Hexagon(ctx)); }
+TargetInfo *elf::getHexagonTargetInfo(Ctx &ctx) {
+  static Hexagon target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index eca1d2fdc08caf..6c910bdf07c8d5 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -893,6 +893,7 @@ void LoongArch::finalizeRelax(int passes) const {
   }
 }
 
-void elf::setLoongArchTargetInfo(Ctx &ctx) {
-  ctx.target.reset(new LoongArch(ctx));
+TargetInfo *elf::getLoongArchTargetInfo(Ctx &ctx) {
+  static LoongArch target(ctx);
+  return &target;
 }
diff --git a/lld/ELF/Arch/MSP430.cpp b/lld/ELF/Arch/MSP430.cpp
index b3aab52e179c97..d2fd401835dbc6 100644
--- a/lld/ELF/Arch/MSP430.cpp
+++ b/lld/ELF/Arch/MSP430.cpp
@@ -88,4 +88,7 @@ void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
   }
 }
 
-void elf::setMSP430TargetInfo(Ctx &ctx) { ctx.target.reset(new MSP430(ctx)); }
+TargetInfo *elf::getMSP430TargetInfo(Ctx &ctx) {
+  static MSP430 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/Mips.cpp b/lld/ELF/Arch/Mips.cpp
index 975fa9ead762d7..2ac37e86ec88ec 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;
 }
 
-void elf::setMipsTargetInfo(Ctx &ctx) {
+TargetInfo *elf::getMipsTargetInfo(Ctx &ctx) {
   switch (ctx.arg.ekind) {
   case ELF32LEKind: {
-    ctx.target.reset(new MIPS<ELF32LE>(ctx));
-    return;
+    static MIPS<ELF32LE> t(ctx);
+    return &t;
   }
   case ELF32BEKind: {
-    ctx.target.reset(new MIPS<ELF32BE>(ctx));
-    return;
+    static MIPS<ELF32BE> t(ctx);
+    return &t;
   }
   case ELF64LEKind: {
-    ctx.target.reset(new MIPS<ELF64LE>(ctx));
-    return;
+    static MIPS<ELF64LE> t(ctx);
+    return &t;
   }
   case ELF64BEKind: {
-    ctx.target.reset(new MIPS<ELF64BE>(ctx));
-    return;
+    static MIPS<ELF64BE> t(ctx);
+    return &t;
   }
   default:
     llvm_unreachable("unsupported target");
diff --git a/lld/ELF/Arch/PPC.cpp b/lld/ELF/Arch/PPC.cpp
index fa1b2a6c39d494..ff9970ba494c1c 100644
--- a/lld/ELF/Arch/PPC.cpp
+++ b/lld/ELF/Arch/PPC.cpp
@@ -523,4 +523,7 @@ void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
   }
 }
 
-void elf::setPPCTargetInfo(Ctx &ctx) { ctx.target.reset(new PPC(ctx)); }
+TargetInfo *elf::getPPCTargetInfo(Ctx &ctx) {
+  static PPC target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 056d495ff1f7db..361e30b0843b9a 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -1747,4 +1747,7 @@ bool PPC64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
   return true;
 }
 
-void elf::setPPC64TargetInfo(Ctx &ctx) { ctx.target.reset(new PPC64(ctx)); }
+TargetInfo *elf::getPPC64TargetInfo(Ctx &ctx) {
+  static PPC64 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 57cc26b3f0a3ff..6970eccf51bdea 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -1329,4 +1329,7 @@ void elf::mergeRISCVAttributesSections(Ctx &ctx) {
                            mergeAttributesSection(ctx, sections));
 }
 
-void elf::setRISCVTargetInfo(Ctx &ctx) { ctx.target.reset(new RISCV(ctx)); }
+TargetInfo *elf::getRISCVTargetInfo(Ctx &ctx) {
+  static RISCV target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/SPARCV9.cpp b/lld/ELF/Arch/SPARCV9.cpp
index bc9724c3ba342a..6d72fcbd1dde3a 100644
--- a/lld/ELF/Arch/SPARCV9.cpp
+++ b/lld/ELF/Arch/SPARCV9.cpp
@@ -193,4 +193,7 @@ void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
   relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
 }
 
-void elf::setSPARCV9TargetInfo(Ctx &ctx) { ctx.target.reset(new SPARCV9(ctx)); }
+TargetInfo *elf::getSPARCV9TargetInfo(Ctx &ctx) {
+  static SPARCV9 target(ctx);
+  return &target;
+}
diff --git a/lld/ELF/Arch/SystemZ.cpp b/lld/ELF/Arch/SystemZ.cpp
index 1bc3b60af5c44d..e1f76e482823a2 100644
--- a/lld/ELF/Arch/SystemZ.cpp
+++ b/lld/ELF/Arch/SystemZ.cpp
@@ -600,4 +600,7 @@ void SystemZ::relocate(uint8_t *loc, const Relocation &rel,
   }
 }
 
-void elf::setSystemZTargetInfo(Ctx &ctx) { ctx.target.reset(new SystemZ(ctx)); }
+TargetInfo *elf::getSystemZTargetInfo(Ctx &ctx) {
+  static SystemZ t(ctx);
+  return &t;
+}
diff --git a/lld/ELF/Arch/X86.cpp b/lld/ELF/Arch/X86.cpp
index 3314dcfc172f8c..795c783951da5a 100644
--- a/lld/ELF/Arch/X86.cpp
+++ b/lld/ELF/Arch/X86.cpp
@@ -706,17 +706,21 @@ void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
   write32le(buf + 22, -off - 26);
 }
 
-void elf::setX86TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getX86TargetInfo(Ctx &ctx) {
   if (ctx.arg.zRetpolineplt) {
-    if (ctx.arg.isPic)
-      ctx.target.reset(new RetpolinePic(ctx));
-    else
-      ctx.target.reset(new RetpolineNoPic(ctx));
-    return;
+    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.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)
-    ctx.target.reset(new IntelIBT(ctx));
-  else
-    ctx.target.reset(new X86(ctx));
+  static X86 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index fbf1076fc71e52..5cf5ea366aa7f7 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -1237,17 +1237,21 @@ void RetpolineZNow::writePlt(uint8_t *buf, const Symbol &sym,
   write32le(buf + 8, ctx.in.plt->getVA() - pltEntryAddr - 12);
 }
 
-void elf::setX86_64TargetInfo(Ctx &ctx) {
+TargetInfo *elf::getX86_64TargetInfo(Ctx &ctx) {
   if (ctx.arg.zRetpolineplt) {
-    if (ctx.arg.zNow)
-      ctx.target.reset(new RetpolineZNow(ctx));
-    else
-      ctx.target.reset(new Retpoline(ctx));
-    return;
+    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)
-    ctx.target.reset(new IntelIBT(ctx));
-  else
-    ctx.target.reset(new X86_64(ctx));
+  if (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
+    static IntelIBT t(ctx);
+    return &t;
+  }
+
+  static X86_64 t(ctx);
+  return &t;
 }
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index f5d65d26ef4479..12a6513fae91dc 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -545,7 +545,7 @@ struct Ctx {
   Config arg;
   LinkerDriver driver;
   LinkerScript *script;
-  std::unique_ptr<TargetInfo> target;
+  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 43f75bab12775f..cde65de4142df9 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.reset();
+  target = nullptr;
 
   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.
-  setTarget(ctx);
+  ctx.target = getTarget(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 96d132a2a8f369..08f12b08bf80b6 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);
 }
 
-void elf::setTarget(Ctx &ctx) {
+TargetInfo *elf::getTarget(Ctx &ctx) {
   switch (ctx.arg.emachine) {
   case EM_386:
   case EM_IAMCU:
-    return setX86TargetInfo(ctx);
+    return getX86TargetInfo(ctx);
   case EM_AARCH64:
-    return setAArch64TargetInfo(ctx);
+    return getAArch64TargetInfo(ctx);
   case EM_AMDGPU:
-    return setAMDGPUTargetInfo(ctx);
+    return getAMDGPUTargetInfo(ctx);
   case EM_ARM:
-    return setARMTargetInfo(ctx);
+    return getARMTargetInfo(ctx);
   case EM_AVR:
-    return setAVRTargetInfo(ctx);
+    return getAVRTargetInfo(ctx);
   case EM_HEXAGON:
-    return setHexagonTargetInfo(ctx);
+    return getHexagonTargetInfo(ctx);
   case EM_LOONGARCH:
-    return setLoongArchTargetInfo(ctx);
+    return getLoongArchTargetInfo(ctx);
   case EM_MIPS:
-    return setMipsTargetInfo(ctx);
+    return getMipsTargetInfo(ctx);
   case EM_MSP430:
-    return setMSP430TargetInfo(ctx);
+    return getMSP430TargetInfo(ctx);
   case EM_PPC:
-    return setPPCTargetInfo(ctx);
+    return getPPCTargetInfo(ctx);
   case EM_PPC64:
-    return setPPC64TargetInfo(ctx);
+    return getPPC64TargetInfo(ctx);
   case EM_RISCV:
-    return setRISCVTargetInfo(ctx);
+    return getRISCVTargetInfo(ctx);
   case EM_SPARCV9:
-    return setSPARCV9TargetInfo(ctx);
+    return getSPARCV9TargetInfo(ctx);
   case EM_S390:
-    return setSystemZTargetInfo(ctx);
+    return getSystemZTargetInfo(ctx);
   case EM_X86_64:
-    return setX86_64TargetInfo(ctx);
+    return getX86_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 f3eb421b494d89..0bc5e3881190e5 100644
--- a/lld/ELF/Target.h
+++ b/lld/ELF/Target.h
@@ -179,21 +179,21 @@ class TargetInfo {
   uint64_t defaultImageBase = 0x10000;
 };
 
-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 &);
+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 &);
 
 struct ErrorPlace {
   InputSectionBase *isec;
@@ -251,7 +251,7 @@ void convertArmInstructionstoBE8(InputSection *sec, uint8_t *buf);
 void createTaggedSymbols(Ctx &);
 void initSymbolAnchors(Ctx &);
 
-void setTarget(Ctx &);
+TargetInfo *getTarget(Ctx &);
 
 template <class ELFT> bool isMipsPIC(const Defined *sym);
 

@ilovepi ilovepi merged commit 2ca8501 into main Oct 7, 2024
10 of 11 checks passed
@ilovepi ilovepi deleted the users/ilovepi/spr/revert-elf-change-ctxtarget-to-unique_ptr-111260 branch October 7, 2024 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants