[lld] 2531b46 - [ELF] Move InStruct into Ctx. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 15 16:59:33 PDT 2024


Author: Fangrui Song
Date: 2024-09-15T16:59:28-07:00
New Revision: 2531b46264cd066d51f2571d134a63998d13710f

URL: https://github.com/llvm/llvm-project/commit/2531b46264cd066d51f2571d134a63998d13710f
DIFF: https://github.com/llvm/llvm-project/commit/2531b46264cd066d51f2571d134a63998d13710f.diff

LOG: [ELF] Move InStruct into Ctx. NFC

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

`#define in ctx.sec` is used for now to avoid migrating `in.xxx`.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 639bf9a4f22840..c9d2ec1d60cbd7 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -51,6 +51,26 @@ class TargetInfo;
 struct Partition;
 struct PhdrEntry;
 
+class BssSection;
+class GdbIndexSection;
+class GotPltSection;
+class GotSection;
+class IBTPltSection;
+class IgotPltSection;
+class InputSection;
+class IpltSection;
+class MipsGotSection;
+class MipsRldMapSection;
+class PPC32Got2Section;
+class PPC64LongBranchTargetSection;
+class PltSection;
+class RelocationBaseSection;
+class RelroPaddingSection;
+class StringTableSection;
+class SymbolTableBaseSection;
+class SymtabShndxSection;
+class SyntheticSection;
+
 enum ELFKind : uint8_t {
   ELFNoneKind,
   ELF32LEKind,
@@ -483,6 +503,42 @@ struct DuplicateSymbol {
   uint64_t value;
 };
 
+// Linker generated sections which can be used as inputs and are not specific to
+// a partition.
+struct InStruct {
+  std::unique_ptr<InputSection> attributes;
+  std::unique_ptr<SyntheticSection> riscvAttributes;
+  std::unique_ptr<BssSection> bss;
+  std::unique_ptr<BssSection> bssRelRo;
+  std::unique_ptr<GotSection> got;
+  std::unique_ptr<GotPltSection> gotPlt;
+  std::unique_ptr<IgotPltSection> igotPlt;
+  std::unique_ptr<RelroPaddingSection> relroPadding;
+  std::unique_ptr<SyntheticSection> armCmseSGSection;
+  std::unique_ptr<PPC64LongBranchTargetSection> ppc64LongBranchTarget;
+  std::unique_ptr<SyntheticSection> mipsAbiFlags;
+  std::unique_ptr<MipsGotSection> mipsGot;
+  std::unique_ptr<SyntheticSection> mipsOptions;
+  std::unique_ptr<SyntheticSection> mipsReginfo;
+  std::unique_ptr<MipsRldMapSection> mipsRldMap;
+  std::unique_ptr<SyntheticSection> partEnd;
+  std::unique_ptr<SyntheticSection> partIndex;
+  std::unique_ptr<PltSection> plt;
+  std::unique_ptr<IpltSection> iplt;
+  std::unique_ptr<PPC32Got2Section> ppc32Got2;
+  std::unique_ptr<IBTPltSection> ibtPlt;
+  std::unique_ptr<RelocationBaseSection> relaPlt;
+  // Non-SHF_ALLOC sections
+  std::unique_ptr<SyntheticSection> debugNames;
+  std::unique_ptr<GdbIndexSection> gdbIndex;
+  std::unique_ptr<StringTableSection> shStrTab;
+  std::unique_ptr<StringTableSection> strTab;
+  std::unique_ptr<SymbolTableBaseSection> symTab;
+  std::unique_ptr<SymtabShndxSection> symTabShndx;
+
+  void reset();
+};
+
 struct Ctx {
   LinkerDriver driver;
   LinkerScript *script;
@@ -504,6 +560,8 @@ struct Ctx {
   SmallVector<OutputSection *, 0> outputSections;
   std::vector<Partition> partitions;
 
+  InStruct sec;
+
   // Some linker-generated symbols need to be created as
   // Defined symbols.
   struct ElfSym {

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 54f9a7e02824f5..46670ce3db9994 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -103,6 +103,7 @@ void Ctx::reset() {
   outputSections.clear();
   partitions.clear();
 
+  in.reset();
   sym = ElfSym{};
 
   memoryBuffers.clear();
@@ -152,8 +153,6 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
     elf::ctx.partitions.emplace_back();
     symtab = SymbolTable();
 
-    in.reset();
-
     SharedFile::vernauxNum = 0;
   };
   ctx->e.logName = args::getFilenameWithoutExe(args[0]);

diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index cb17e107d6dae2..2cbd5554106113 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -22,6 +22,7 @@
 #include "llvm/Support/Parallel.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TimeProfiler.h"
+#undef in
 #if LLVM_ENABLE_ZLIB
 // Avoid introducing max as a macro from Windows headers.
 #define NOMINMAX
@@ -584,7 +585,7 @@ void OutputSection::writeTo(uint8_t *buf, parallel::TaskGroup &tg) {
 static void finalizeShtGroup(OutputSection *os, InputSection *section) {
   // sh_link field for SHT_GROUP sections should contain the section index of
   // the symbol table.
-  os->link = in.symTab->getParent()->sectionIndex;
+  os->link = ctx.sec.symTab->getParent()->sectionIndex;
 
   if (!section)
     return;
@@ -592,7 +593,7 @@ static void finalizeShtGroup(OutputSection *os, InputSection *section) {
   // sh_info then contain index of an entry in symbol table section which
   // provides signature of the section group.
   ArrayRef<Symbol *> symbols = section->file->getSymbols();
-  os->info = in.symTab->getSymbolIndex(*symbols[section->info]);
+  os->info = ctx.sec.symTab->getSymbolIndex(*symbols[section->info]);
 
   // Some group members may be combined or discarded, so we need to compute the
   // new size. The content will be rewritten in InputSection::copyShtGroup.
@@ -610,7 +611,7 @@ encodeOneCrel(raw_svector_ostream &os, Elf_Crel<sizeof(uint) == 8> &out,
               uint offset, const Symbol &sym, uint32_t type, uint addend) {
   const auto deltaOffset = static_cast<uint64_t>(offset - out.r_offset);
   out.r_offset = offset;
-  int64_t symidx = in.symTab->getSymbolIndex(sym);
+  int64_t symidx = ctx.sec.symTab->getSymbolIndex(sym);
   if (sym.type == STT_SECTION) {
     auto *d = dyn_cast<Defined>(&sym);
     if (d) {
@@ -731,7 +732,7 @@ void OutputSection::finalize() {
   if (!first || isa<SyntheticSection>(first))
     return;
 
-  link = in.symTab->getParent()->sectionIndex;
+  link = ctx.sec.symTab->getParent()->sectionIndex;
   // sh_info for SHT_REL[A] sections should contain the section header index of
   // the section to which the relocation applies.
   InputSectionBase *s = first->getRelocatedSection();
@@ -881,8 +882,8 @@ void OutputSection::checkDynRelAddends(const uint8_t *bufStart) {
       // Some targets have NOBITS synthetic sections with dynamic relocations
       // with non-zero addends. Skip such sections.
       if (is_contained({EM_PPC, EM_PPC64}, config->emachine) &&
-          (rel.inputSec == in.ppc64LongBranchTarget.get() ||
-           rel.inputSec == in.igotPlt.get()))
+          (rel.inputSec == ctx.sec.ppc64LongBranchTarget.get() ||
+           rel.inputSec == ctx.sec.igotPlt.get()))
         continue;
       const uint8_t *relocTarget =
           bufStart + relOsec->offset + rel.inputSec->getOffset(rel.offsetInSec);

diff  --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 3b75863ac67b01..7b14be8c9473ac 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -4926,8 +4926,6 @@ template <class ELFT> void elf::createSyntheticSections() {
     add(*in.strTab);
 }
 
-InStruct elf::in;
-
 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 4bfa5cd73d35ee..b1ca264bac6943 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -1479,43 +1479,7 @@ inline Partition &SectionBase::getPartition() const {
   return ctx.partitions[partition - 1];
 }
 
-// Linker generated sections which can be used as inputs and are not specific to
-// a partition.
-struct InStruct {
-  std::unique_ptr<InputSection> attributes;
-  std::unique_ptr<SyntheticSection> riscvAttributes;
-  std::unique_ptr<BssSection> bss;
-  std::unique_ptr<BssSection> bssRelRo;
-  std::unique_ptr<GotSection> got;
-  std::unique_ptr<GotPltSection> gotPlt;
-  std::unique_ptr<IgotPltSection> igotPlt;
-  std::unique_ptr<RelroPaddingSection> relroPadding;
-  std::unique_ptr<SyntheticSection> armCmseSGSection;
-  std::unique_ptr<PPC64LongBranchTargetSection> ppc64LongBranchTarget;
-  std::unique_ptr<SyntheticSection> mipsAbiFlags;
-  std::unique_ptr<MipsGotSection> mipsGot;
-  std::unique_ptr<SyntheticSection> mipsOptions;
-  std::unique_ptr<SyntheticSection> mipsReginfo;
-  std::unique_ptr<MipsRldMapSection> mipsRldMap;
-  std::unique_ptr<SyntheticSection> partEnd;
-  std::unique_ptr<SyntheticSection> partIndex;
-  std::unique_ptr<PltSection> plt;
-  std::unique_ptr<IpltSection> iplt;
-  std::unique_ptr<PPC32Got2Section> ppc32Got2;
-  std::unique_ptr<IBTPltSection> ibtPlt;
-  std::unique_ptr<RelocationBaseSection> relaPlt;
-  // Non-SHF_ALLOC sections
-  std::unique_ptr<SyntheticSection> debugNames;
-  std::unique_ptr<GdbIndexSection> gdbIndex;
-  std::unique_ptr<StringTableSection> shStrTab;
-  std::unique_ptr<StringTableSection> strTab;
-  std::unique_ptr<SymbolTableBaseSection> symTab;
-  std::unique_ptr<SymtabShndxSection> symTabShndx;
-
-  void reset();
-};
-
-LLVM_LIBRARY_VISIBILITY extern InStruct in;
+#define in ctx.sec
 
 } // namespace lld::elf
 


        


More information about the llvm-commits mailing list