ELF: Do not instantiate InputSectionBase::Discarded.
authorRui Ueyama <ruiu@google.com>
Wed, 24 Feb 2016 18:33:35 +0000 (18:33 +0000)
committerRui Ueyama <ruiu@google.com>
Wed, 24 Feb 2016 18:33:35 +0000 (18:33 +0000)
"Discarded" section is a marker for discarded sections, and we do not
use the instance except for checking its identity. In that sense, it
is just another type of a "null" pointer for InputSectionBase. So,
it doesn't have to be a real instance of InputSectionBase class.

In this patch, we no longer instantiate Discarded section but instead
use -1 as a pointer value. This eliminates a global variable which
needed initialization at startup.

llvm-svn: 261761

lld/ELF/InputFiles.cpp
lld/ELF/InputSection.cpp
lld/ELF/InputSection.h
lld/ELF/MarkLive.cpp
lld/ELF/OutputSections.cpp
lld/ELF/Writer.cpp

index 2ba02fc..e81119d 100644 (file)
@@ -186,18 +186,18 @@ void elf2::ObjectFile<ELFT>::initializeSections(
   const ELFFile<ELFT> &Obj = this->ELFObj;
   for (const Elf_Shdr &Sec : Obj.sections()) {
     ++I;
-    if (Sections[I] == &InputSection<ELFT>::Discarded)
+    if (Sections[I] == InputSection<ELFT>::Discarded)
       continue;
 
     switch (Sec.sh_type) {
     case SHT_GROUP:
-      Sections[I] = &InputSection<ELFT>::Discarded;
+      Sections[I] = InputSection<ELFT>::Discarded;
       if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
         continue;
       for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
         if (SecIndex >= Size)
           fatal("Invalid section index in group");
-        Sections[SecIndex] = &InputSection<ELFT>::Discarded;
+        Sections[SecIndex] = InputSection<ELFT>::Discarded;
       }
       break;
     case SHT_SYMTAB:
@@ -222,7 +222,7 @@ void elf2::ObjectFile<ELFT>::initializeSections(
       // Strictly speaking, a relocation section must be included in the
       // group of the section it relocates. However, LLVM 3.3 and earlier
       // would fail to do so, so we gracefully handle that case.
-      if (RelocatedSection == &InputSection<ELFT>::Discarded)
+      if (RelocatedSection == InputSection<ELFT>::Discarded)
         continue;
       if (!RelocatedSection)
         fatal("Unsupported relocation reference");
@@ -255,7 +255,7 @@ elf2::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
   // is controlled only by the command line option (-z execstack) in LLD,
   // .note.GNU-stack is ignored.
   if (Name == ".note.GNU-stack")
-    return &InputSection<ELFT>::Discarded;
+    return InputSection<ELFT>::Discarded;
 
   // A MIPS object file has a special section that contains register
   // usage info, which needs to be handled by the linker specially.
@@ -313,7 +313,7 @@ SymbolBody *elf2::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
   case STB_WEAK:
   case STB_GNU_UNIQUE: {
     InputSectionBase<ELFT> *Sec = getSection(*Sym);
-    if (Sec == &InputSection<ELFT>::Discarded)
+    if (Sec == InputSection<ELFT>::Discarded)
       return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
     return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
   }
index a00f838..231d9f1 100644 (file)
@@ -28,14 +28,11 @@ InputSectionBase<ELFT>::InputSectionBase(ObjectFile<ELFT> *File,
     : Header(Header), File(File), SectionKind(SectionKind) {
   // The garbage collector sets sections' Live bits.
   // If GC is disabled, all sections are considered live by default.
-  // NB: "Discarded" section is initialized at start-up and when it
-  // happens Config is still null.
-  Live = Config && !Config->GcSections;
+  Live = !Config->GcSections;
 
   // The ELF spec states that a value of 0 means the section has
   // no alignment constraits.
-  if (Header)
-    Align = std::max<uintX_t>(Header->sh_addralign, 1);
+  Align = std::max<uintX_t>(Header->sh_addralign, 1);
 }
 
 template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
index 2faec9a..96e7f78 100644 (file)
@@ -50,7 +50,7 @@ public:
   // Returns the size of this section (even if this is a common or BSS.)
   size_t getSize() const { return Header->sh_size; }
 
-  static InputSectionBase<ELFT> Discarded;
+  static InputSectionBase<ELFT> *Discarded;
 
   StringRef getSectionName() const;
   const Elf_Shdr *getSectionHdr() const { return Header; }
@@ -81,9 +81,8 @@ private:
 };
 
 template <class ELFT>
-InputSectionBase<ELFT>
-    InputSectionBase<ELFT>::Discarded(nullptr, nullptr,
-                                      InputSectionBase<ELFT>::Regular);
+InputSectionBase<ELFT> *
+    InputSectionBase<ELFT>::Discarded = (InputSectionBase<ELFT> *)-1ULL;
 
 // Usually sections are copied to the output as atomic chunks of data,
 // but some special types of sections are split into small pieces of data
index 0fdf512..c545a8b 100644 (file)
@@ -118,7 +118,7 @@ template <class ELFT> void elf2::markLive(SymbolTable<ELFT> *Symtab) {
   // script KEEP command.
   for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab->getObjectFiles())
     for (InputSectionBase<ELFT> *Sec : F->getSections())
-      if (Sec && Sec != &InputSection<ELFT>::Discarded)
+      if (Sec && Sec != InputSection<ELFT>::Discarded)
         if (isReserved(Sec) || Script->shouldKeep<ELFT>(Sec))
           Enqueue(Sec);
 
index 7f225ca..879478e 100644 (file)
@@ -884,7 +884,7 @@ elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
   // the group are not allowed. Unfortunately .eh_frame breaks that rule
   // and must be treated specially. For now we just replace the symbol with
   // 0.
-  if (Section == &InputSection<ELFT>::Discarded || !Section->Live)
+  if (Section == InputSection<ELFT>::Discarded || !Section->Live)
     return Addend;
 
   uintX_t Offset = Sym->st_value;
@@ -1147,7 +1147,7 @@ void EHOutputSection<ELFT>::addSectionAux(
       if (!HasReloc)
         fatal("FDE doesn't reference another section");
       InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
-      if (Target != &InputSection<ELFT>::Discarded && Target->Live) {
+      if (Target != InputSection<ELFT>::Discarded && Target->Live) {
         uint32_t CieOffset = Offset + 4 - ID;
         auto I = OffsetToIndex.find(CieOffset);
         if (I == OffsetToIndex.end())
index 0e9a8af..0fdffe9 100644 (file)
@@ -515,7 +515,7 @@ static bool shouldKeepInSymtab(const ObjectFile<ELFT> &File, StringRef SymName,
 
   InputSectionBase<ELFT> *Sec = File.getSection(Sym);
   // If sym references a section in a discarded group, don't keep it.
-  if (Sec == &InputSection<ELFT>::Discarded)
+  if (Sec == InputSection<ELFT>::Discarded)
     return false;
 
   if (Config->DiscardNone)
@@ -752,7 +752,7 @@ void reportDiscarded(InputSectionBase<ELFT> *IS,
 
 template <class ELFT>
 bool Writer<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) const {
-  return !S || !S->Live || S == &InputSection<ELFT>::Discarded ||
+  return !S || S == InputSection<ELFT>::Discarded || !S->Live ||
          Script->isDiscarded(S);
 }