[lld] 73bb022 - [ELF] Remove global ctx

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 16 17:24:16 PST 2024


Author: Fangrui Song
Date: 2024-11-16T17:24:11-08:00
New Revision: 73bb022b469a32eb5aee30f2947f3cea5e903caa

URL: https://github.com/llvm/llvm-project/commit/73bb022b469a32eb5aee30f2947f3cea5e903caa
DIFF: https://github.com/llvm/llvm-project/commit/73bb022b469a32eb5aee30f2947f3cea5e903caa.diff

LOG: [ELF] Remove global ctx

This commit completes the work that eliminates global variables like
config/target/inputSections/symTab from lld/ELF.

Key changes:

* Introduced `lld::elf::ctx` to encapsulate global state.
* Moved global variables into `Ctx lld::elf::ctx`
* Updated many functions to accept `Ctx &ctx`
* Made `ctx` a local variable (this commit)

If we don't count `static std::mutex`, this is the last major global
state within lld/ELF (minor ones like `SharedFile::vernauxNum`
(33ff9e43b4c5bdc3da31c6b11ad51d35a69bec5f) might not all be eliminated
yet).

Added: 
    

Modified: 
    lld/ELF/Config.h
    lld/ELF/Driver.cpp
    lld/ELF/SyntheticSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 72feeb9d49cbd0..a9b7a98e5b544a 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -537,8 +537,6 @@ struct InStruct {
   std::unique_ptr<StringTableSection> strTab;
   std::unique_ptr<SymbolTableBaseSection> symTab;
   std::unique_ptr<SymtabShndxSection> symTabShndx;
-
-  void reset();
 };
 
 struct Ctx {
@@ -662,15 +660,12 @@ struct Ctx {
   llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax;
 
   Ctx();
-  void reset();
 
   llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &);
 
   ArrayRef<uint8_t> aarch64PauthAbiCoreInfo;
 };
 
-LLVM_LIBRARY_VISIBILITY extern Ctx ctx;
-
 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and
 // VER_NDX_GLOBAL. This helper returns other elements.
 static inline ArrayRef<VersionDefinition> namedVersionDefs(Ctx &ctx) {

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 334dfc0e3ba150..631051c273810c 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -79,8 +79,6 @@ using namespace llvm::support;
 using namespace lld;
 using namespace lld::elf;
 
-Ctx elf::ctx;
-
 static void setConfigs(Ctx &ctx, opt::InputArgList &args);
 static void readConfigs(Ctx &ctx, opt::InputArgList &args);
 
@@ -102,55 +100,6 @@ ELFSyncStream elf::InternalErr(Ctx &ctx, const uint8_t *buf) {
 
 Ctx::Ctx() : driver(*this) {}
 
-void Ctx::reset() {
-  arg.~Config();
-  new (&arg) Config();
-  driver.~LinkerDriver();
-  new (&driver) LinkerDriver(*this);
-  script = nullptr;
-  target.reset();
-
-  commonCtx = nullptr;
-  errHandler = nullptr;
-
-  bufferStart = nullptr;
-  mainPart = nullptr;
-  tlsPhdr = nullptr;
-  out = OutSections{};
-  outputSections.clear();
-  partitions.clear();
-
-  in.reset();
-  sym = ElfSym{};
-  symtab = std::make_unique<SymbolTable>(*this);
-
-  memoryBuffers.clear();
-  objectFiles.clear();
-  sharedFiles.clear();
-  binaryFiles.clear();
-  bitcodeFiles.clear();
-  lazyBitcodeFiles.clear();
-  inputSections.clear();
-  ehInputSections.clear();
-
-  symAux.clear();
-  duplicates.clear();
-  nonPrevailingSyms.clear();
-  whyExtractRecords.clear();
-  backwardReferences.clear();
-  auxiliaryFiles.clear();
-  tar.reset();
-  internalFile = nullptr;
-  hasSympart.store(false, std::memory_order_relaxed);
-  hasTlsIe.store(false, std::memory_order_relaxed);
-  needsTlsLd.store(false, std::memory_order_relaxed);
-  vernauxNum = 0;
-  scriptSymOrderCounter = 1;
-  scriptSymOrder.clear();
-  ppc64noTocRelax.clear();
-  ltoAllVtablesHaveTypeInfos = false;
-}
-
 llvm::raw_fd_ostream Ctx::openAuxiliaryFile(llvm::StringRef filename,
                                             std::error_code &ec) {
   using namespace llvm::sys::fs;
@@ -163,21 +112,16 @@ namespace lld {
 namespace elf {
 bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
           llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
+  Ctx ctx;
   // This driver-specific context will be freed later by unsafeLldMain().
   auto *context = new CommonLinkerContext;
 
   context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
-  context->e.cleanupCallback = []() {
-    Ctx &ctx = elf::ctx;
-    ctx.reset();
-    ctx.partitions.emplace_back(ctx);
-  };
   context->e.logName = args::getFilenameWithoutExe(args[0]);
   context->e.errorLimitExceededMsg =
       "too many errors emitted, stopping now (use "
       "--error-limit=0 to see all errors)";
 
-  Ctx &ctx = elf::ctx;
   LinkerScript script(ctx);
   ctx.script = &script;
   ctx.commonCtx = context;

diff  --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 49ffc0b8af5fb2..4500a9afa5f3f2 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -4504,37 +4504,6 @@ void PartitionIndexSection::writeTo(uint8_t *buf) {
   }
 }
 
-void InStruct::reset() {
-  attributes.reset();
-  riscvAttributes.reset();
-  bss.reset();
-  bssRelRo.reset();
-  got.reset();
-  gotPlt.reset();
-  igotPlt.reset();
-  relroPadding.reset();
-  armCmseSGSection.reset();
-  ppc64LongBranchTarget.reset();
-  mipsAbiFlags.reset();
-  mipsGot.reset();
-  mipsOptions.reset();
-  mipsReginfo.reset();
-  mipsRldMap.reset();
-  partEnd.reset();
-  partIndex.reset();
-  plt.reset();
-  iplt.reset();
-  ppc32Got2.reset();
-  ibtPlt.reset();
-  relaPlt.reset();
-  debugNames.reset();
-  gdbIndex.reset();
-  shStrTab.reset();
-  strTab.reset();
-  symTab.reset();
-  symTabShndx.reset();
-}
-
 static bool needsInterpSection(Ctx &ctx) {
   return !ctx.arg.relocatable && !ctx.arg.shared &&
          !ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();


        


More information about the llvm-commits mailing list