[lld] 40e8e4d - [ELF] Move partitions into ctx. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 15 14:53:01 PDT 2024


Author: Fangrui Song
Date: 2024-09-15T14:52:56-07:00
New Revision: 40e8e4ddcb7d48f404223e863e5db38fff75cad2

URL: https://github.com/llvm/llvm-project/commit/40e8e4ddcb7d48f404223e863e5db38fff75cad2
DIFF: https://github.com/llvm/llvm-project/commit/40e8e4ddcb7d48f404223e863e5db38fff75cad2.diff

LOG: [ELF] Move partitions into ctx. NFC

Ctx was introduced in March 2022 as a more suitable place for such
singletons.

Added: 
    

Modified: 
    lld/ELF/Config.h
    lld/ELF/Driver.cpp
    lld/ELF/ICF.cpp
    lld/ELF/InputSection.h
    lld/ELF/LinkerScript.cpp
    lld/ELF/MarkLive.cpp
    lld/ELF/Relocations.cpp
    lld/ELF/SyntheticSections.cpp
    lld/ELF/SyntheticSections.h
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index fd40ec9805aa2b..639bf9a4f22840 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -502,6 +502,7 @@ struct Ctx {
   };
   OutSections out;
   SmallVector<OutputSection *, 0> outputSections;
+  std::vector<Partition> partitions;
 
   // Some linker-generated symbols need to be created as
   // Defined symbols.

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 34d9964407a5f8..54f9a7e02824f5 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -101,6 +101,7 @@ void Ctx::reset() {
   tlsPhdr = nullptr;
   out = OutSections{};
   outputSections.clear();
+  partitions.clear();
 
   sym = ElfSym{};
 
@@ -148,13 +149,11 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
   ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
   ctx->e.cleanupCallback = []() {
     elf::ctx.reset();
+    elf::ctx.partitions.emplace_back();
     symtab = SymbolTable();
 
     in.reset();
 
-    partitions.clear();
-    partitions.emplace_back();
-
     SharedFile::vernauxNum = 0;
   };
   ctx->e.logName = args::getFilenameWithoutExe(args[0]);
@@ -167,8 +166,8 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
   elf::ctx.script = &script;
   elf::ctx.symAux.emplace_back();
 
-  partitions.clear();
-  partitions.emplace_back();
+  elf::ctx.partitions.clear();
+  elf::ctx.partitions.emplace_back();
 
   config->progName = args[0];
 
@@ -2448,7 +2447,7 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
     return;
 
   StringRef partName = reinterpret_cast<const char *>(s->content().data());
-  for (Partition &part : partitions) {
+  for (Partition &part : ctx.partitions) {
     if (part.name == partName) {
       sym->partition = part.getNumber();
       return;
@@ -2473,11 +2472,11 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
   // Impose a limit of no more than 254 partitions. This limit comes from the
   // sizes of the Partition fields in InputSectionBase and Symbol, as well as
   // the amount of space devoted to the partition number in RankFlags.
-  if (partitions.size() == 254)
+  if (ctx.partitions.size() == 254)
     fatal("may not have more than 254 partitions");
 
-  partitions.emplace_back();
-  Partition &newPart = partitions.back();
+  ctx.partitions.emplace_back();
+  Partition &newPart = ctx.partitions.back();
   newPart.name = partName;
   sym->partition = newPart.getNumber();
 }
@@ -3097,7 +3096,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
 
   // Now that the number of partitions is fixed, save a pointer to the main
   // partition.
-  ctx.mainPart = &partitions[0];
+  ctx.mainPart = &ctx.partitions[0];
 
   // Read .note.gnu.property sections from input object files which
   // contain a hint to tweak linker's and loader's behaviors.

diff  --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp
index 92b3bbb46cc95d..14e0afc6029e9f 100644
--- a/lld/ELF/ICF.cpp
+++ b/lld/ELF/ICF.cpp
@@ -480,7 +480,7 @@ template <class ELFT> void ICF<ELFT>::run() {
   // If two .gcc_except_table have identical semantics (usually identical
   // content with PC-relative encoding), we will lose folding opportunity.
   uint32_t uniqueId = 0;
-  for (Partition &part : partitions)
+  for (Partition &part : ctx.partitions)
     part.ehFrame->iterateFDEWithLSDA<ELFT>(
         [&](InputSection &s) { s.eqClass[0] = s.eqClass[1] = ++uniqueId; });
 

diff  --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h
index 60c8d57b8db86a..f7672bbf553902 100644
--- a/lld/ELF/InputSection.h
+++ b/lld/ELF/InputSection.h
@@ -33,8 +33,6 @@ class SyntheticSection;
 template <class ELFT> class ObjFile;
 class OutputSection;
 
-LLVM_LIBRARY_VISIBILITY extern std::vector<Partition> partitions;
-
 // Returned by InputSectionBase::relsOrRelas. At most one member is empty.
 template <class ELFT> struct RelsOrRelas {
   Relocs<typename ELFT::Rel> rels;

diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 8bab26cd3b0f07..4d7ffd0b990223 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -659,7 +659,7 @@ void LinkerScript::discard(InputSectionBase &s) {
 }
 
 void LinkerScript::discardSynthetic(OutputSection &outCmd) {
-  for (Partition &part : partitions) {
+  for (Partition &part : ctx.partitions) {
     if (!part.armExidx || !part.armExidx->isLive())
       continue;
     SmallVector<InputSectionBase *, 0> secs(

diff  --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp
index 56ff53fc89bddf..f11d7f5a1053af 100644
--- a/lld/ELF/MarkLive.cpp
+++ b/lld/ELF/MarkLive.cpp
@@ -377,13 +377,13 @@ template <class ELFT> void elf::markLive() {
     sec->markDead();
 
   // Follow the graph to mark all live sections.
-  for (unsigned curPart = 1; curPart <= partitions.size(); ++curPart)
-    MarkLive<ELFT>(curPart).run();
+  for (unsigned i = 1, e = ctx.partitions.size(); i <= e; ++i)
+    MarkLive<ELFT>(i).run();
 
   // If we have multiple partitions, some sections need to live in the main
   // partition even if they were allocated to a loadable partition. Move them
   // there now.
-  if (partitions.size() != 1)
+  if (ctx.partitions.size() != 1)
     MarkLive<ELFT>(1).moveToMain();
 
   // Report garbage-collected sections.

diff  --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index e5f58f1a7dd129..565a5c2f582fce 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -1662,7 +1662,7 @@ template <class ELFT> void elf::scanRelocations() {
 
   tg.spawn([] {
     RelocationScanner scanner;
-    for (Partition &part : partitions) {
+    for (Partition &part : ctx.partitions) {
       for (EhInputSection *sec : part.ehFrame->sections)
         scanner.template scanSection<ELFT>(*sec, /*isEH=*/true);
       if (part.armExidx && part.armExidx->isLive())

diff  --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index df82e9ed0652ec..3b75863ac67b01 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -4438,26 +4438,26 @@ PartitionIndexSection::PartitionIndexSection()
     : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {}
 
 size_t PartitionIndexSection::getSize() const {
-  return 12 * (partitions.size() - 1);
+  return 12 * (ctx.partitions.size() - 1);
 }
 
 void PartitionIndexSection::finalizeContents() {
-  for (size_t i = 1; i != partitions.size(); ++i)
-    partitions[i].nameStrTab =
-        ctx.mainPart->dynStrTab->addString(partitions[i].name);
+  for (size_t i = 1; i != ctx.partitions.size(); ++i)
+    ctx.partitions[i].nameStrTab =
+        ctx.mainPart->dynStrTab->addString(ctx.partitions[i].name);
 }
 
 void PartitionIndexSection::writeTo(uint8_t *buf) {
   uint64_t va = getVA();
-  for (size_t i = 1; i != partitions.size(); ++i) {
-    write32(buf,
-            ctx.mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va);
-    write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4));
+  for (size_t i = 1; i != ctx.partitions.size(); ++i) {
+    write32(buf, ctx.mainPart->dynStrTab->getVA() +
+                     ctx.partitions[i].nameStrTab - va);
+    write32(buf + 4, ctx.partitions[i].elfHeader->getVA() - (va + 4));
 
-    SyntheticSection *next = i == partitions.size() - 1
+    SyntheticSection *next = i == ctx.partitions.size() - 1
                                  ? in.partEnd.get()
-                                 : partitions[i + 1].elfHeader.get();
-    write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA());
+                                 : ctx.partitions[i + 1].elfHeader.get();
+    write32(buf + 8, next->getVA() - ctx.partitions[i].elfHeader->getVA());
 
     va += 12;
     buf += 12;
@@ -4657,7 +4657,7 @@ template <class ELFT> void elf::createSyntheticSections() {
   // The removeUnusedSyntheticSections() function relies on the
   // SyntheticSections coming last.
   if (needsInterpSection()) {
-    for (size_t i = 1; i <= partitions.size(); ++i) {
+    for (size_t i = 1; i <= ctx.partitions.size(); ++i) {
       InputSection *sec = createInterpSection();
       sec->partition = i;
       ctx.inputSections.push_back(sec);
@@ -4707,7 +4707,7 @@ template <class ELFT> void elf::createSyntheticSections() {
   StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";
 
   const unsigned threadCount = config->threadCount;
-  for (Partition &part : partitions) {
+  for (Partition &part : ctx.partitions) {
     auto add = [&](SyntheticSection &sec) {
       sec.partition = part.getNumber();
       ctx.inputSections.push_back(&sec);
@@ -4811,7 +4811,7 @@ template <class ELFT> void elf::createSyntheticSections() {
     }
   }
 
-  if (partitions.size() != 1) {
+  if (ctx.partitions.size() != 1) {
     // Create the partition end marker. This needs to be in partition number 255
     // so that it is sorted after all other partitions. It also has other
     // special handling (see createPhdrs() and combineEhSections()).
@@ -4928,8 +4928,6 @@ template <class ELFT> void elf::createSyntheticSections() {
 
 InStruct elf::in;
 
-std::vector<Partition> elf::partitions;
-
 template void elf::splitSections<ELF32LE>();
 template void elf::splitSections<ELF32BE>();
 template void elf::splitSections<ELF64LE>();

diff  --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index 56647f46b5fc41..4bfa5cd73d35ee 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -1471,12 +1471,12 @@ struct Partition {
   std::unique_ptr<SyntheticSection> verNeed;
   std::unique_ptr<VersionTableSection> verSym;
 
-  unsigned getNumber() const { return this - &partitions[0] + 1; }
+  unsigned getNumber() const { return this - &ctx.partitions[0] + 1; }
 };
 
 inline Partition &SectionBase::getPartition() const {
   assert(isLive());
-  return partitions[partition - 1];
+  return ctx.partitions[partition - 1];
 }
 
 // Linker generated sections which can be used as inputs and are not specific to

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index bb0ae5f073ce5c..d00586bc939901 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -118,7 +118,7 @@ static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
 void elf::copySectionsIntoPartitions() {
   SmallVector<InputSectionBase *, 0> newSections;
   const size_t ehSize = ctx.ehInputSections.size();
-  for (unsigned part = 2; part != partitions.size() + 1; ++part) {
+  for (unsigned part = 2; part != ctx.partitions.size() + 1; ++part) {
     for (InputSectionBase *s : ctx.inputSections) {
       if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
         continue;
@@ -320,7 +320,7 @@ template <class ELFT> void Writer<ELFT>::run() {
   // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
   // 0 sized region. This has to be done late since only after assignAddresses
   // we know the size of the sections.
-  for (Partition &part : partitions)
+  for (Partition &part : ctx.partitions)
     removeEmptyPTLoad(part.phdrs);
 
   if (!config->oFormatBinary)
@@ -328,7 +328,7 @@ template <class ELFT> void Writer<ELFT>::run() {
   else
     assignFileOffsetsBinary();
 
-  for (Partition &part : partitions)
+  for (Partition &part : ctx.partitions)
     setPhdrs(part);
 
   // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
@@ -844,7 +844,7 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
   auto isLarge = [](OutputSection *osec) {
     return config->emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;
   };
-  for (Partition &part : partitions) {
+  for (Partition &part : ctx.partitions) {
     for (PhdrEntry *p : part.phdrs) {
       if (p->p_type != PT_LOAD)
         continue;
@@ -1443,7 +1443,7 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
   // increasing. Anything here must be repeatable, since spilling may change
   // section order.
   const auto finalizeOrderDependentContent = [this] {
-    for (Partition &part : partitions)
+    for (Partition &part : ctx.partitions)
       finalizeSynthetic(part.armExidx.get());
     resolveShfLinkOrder();
   };
@@ -1485,7 +1485,7 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
     if (in.mipsGot)
       in.mipsGot->updateAllocSize();
 
-    for (Partition &part : partitions) {
+    for (Partition &part : ctx.partitions) {
       // The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]
       // encode the signing schema. We've put relocations in .relr.auth.dyn
       // during RelocationScanner::processAux, but the target VA for some of
@@ -1777,7 +1777,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
     // earlier.
     {
       llvm::TimeTraceScope timeScope("Finalize .eh_frame");
-      for (Partition &part : partitions)
+      for (Partition &part : ctx.partitions)
         finalizeSynthetic(part.ehFrame.get());
     }
   }
@@ -1865,7 +1865,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
         in.symTab->addSymbol(sym);
 
       if (sym->includeInDynsym()) {
-        partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
+        ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
         if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
           if (file->isNeeded && !sym->isUndefined())
             addVerneed(sym);
@@ -1874,7 +1874,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
 
     // We also need to scan the dynamic relocation tables of the other
     // partitions and add any referenced symbols to the partition's dynsym.
-    for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
+    for (Partition &part :
+         MutableArrayRef<Partition>(ctx.partitions).slice(1)) {
       DenseSet<Symbol *> syms;
       for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
         syms.insert(e.sym);
@@ -1922,7 +1923,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
         symtab.addSymbol(Undefined{ctx.internalFile, "__tls_get_addr",
                                    STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
     sym->isPreemptible = true;
-    partitions[0].dynSymTab->addSymbol(sym);
+    ctx.partitions[0].dynSymTab->addSymbol(sym);
   }
 
   // This is a bit of a hack. A value of 0 means undef, so we set it
@@ -1935,7 +1936,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
   // The headers have to be created before finalize as that can influence the
   // image base and the dynamic section on mips includes the image base.
   if (!config->relocatable && !config->oFormatBinary) {
-    for (Partition &part : partitions) {
+    for (Partition &part : ctx.partitions) {
       part.phdrs = ctx.script->hasPhdrsCommands() ? ctx.script->createPhdrs()
                                                   : createPhdrs(part);
       if (config->emachine == EM_ARM) {
@@ -1993,7 +1994,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
 
     // Dynamic section must be the last one in this list and dynamic
     // symbol table section (dynSymTab) must be the first one.
-    for (Partition &part : partitions) {
+    for (Partition &part : ctx.partitions) {
       if (part.relaDyn) {
         part.relaDyn->mergeRels();
         // Compute DT_RELACOUNT to be used by part.dynamic.
@@ -2437,7 +2438,7 @@ template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
     }
   };
 
-  for (Partition &part : partitions) {
+  for (Partition &part : ctx.partitions) {
     prev = nullptr;
     for (const PhdrEntry *p : part.phdrs)
       if (p->p_type == PT_LOAD && p->firstSec) {
@@ -2503,7 +2504,7 @@ template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
   uint64_t off = ctx.out.elfHeader->size + ctx.out.programHeaders->size;
 
   PhdrEntry *lastRX = nullptr;
-  for (Partition &part : partitions)
+  for (Partition &part : ctx.partitions)
     for (PhdrEntry *p : part.phdrs)
       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
         lastRX = p;
@@ -2813,7 +2814,7 @@ static void fillTrap(uint8_t *i, uint8_t *end) {
 // We'll leave other pages in segments as-is because the rest will be
 // overwritten by output sections.
 template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
-  for (Partition &part : partitions) {
+  for (Partition &part : ctx.partitions) {
     // Fill the last page.
     for (PhdrEntry *p : part.phdrs)
       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
@@ -2890,7 +2891,7 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
     return;
 
   if (config->buildId == BuildIdKind::Hexstring) {
-    for (Partition &part : partitions)
+    for (Partition &part : ctx.partitions)
       part.buildId->writeBuildId(config->buildIdVector);
     return;
   }
@@ -2930,7 +2931,7 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
   default:
     llvm_unreachable("unknown BuildIdKind");
   }
-  for (Partition &part : partitions)
+  for (Partition &part : ctx.partitions)
     part.buildId->writeBuildId(output);
 }
 


        


More information about the llvm-commits mailing list