[lld] r357377 - [ELF] Rename SyntheticSection::empty to more appropriate isNeeded() with opposite meaning

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 1 01:16:08 PDT 2019


Author: maskray
Date: Mon Apr  1 01:16:08 2019
New Revision: 357377

URL: http://llvm.org/viewvc/llvm-project?rev=357377&view=rev
Log:
[ELF] Rename SyntheticSection::empty to more appropriate isNeeded() with opposite meaning

Summary:
Some synthetic sections can be empty while still being needed, thus they
can't be removed by removeUnusedSyntheticSections(). Rename this member
function to more appropriate isNeeded() with the opposite meaning.

No functional change intended.

Reviewers: ruiu, espindola

Reviewed By: ruiu

Subscribers: jhenderson, grimar, emaste, arichardson, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D59982

Modified:
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/SyntheticSections.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=357377&r1=357376&r2=357377&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Mon Apr  1 01:16:08 2019
@@ -648,10 +648,10 @@ void GotSection::finalizeContents() {
   Size = NumEntries * Config->Wordsize;
 }
 
-bool GotSection::empty() const {
+bool GotSection::isNeeded() const {
   // We need to emit a GOT even if it's empty if there's a relocation that is
   // relative to GOT(such as GOTOFFREL).
-  return NumEntries == 0 && !HasGotOffRel;
+  return NumEntries || HasGotOffRel;
 }
 
 void GotSection::writeTo(uint8_t *Buf) {
@@ -1004,10 +1004,10 @@ void MipsGotSection::build() {
   }
 }
 
-bool MipsGotSection::empty() const {
+bool MipsGotSection::isNeeded() const {
   // We add the .got section to the result for dynamic MIPS target because
   // its address and properties are mentioned in the .dynamic section.
-  return Config->Relocatable;
+  return !Config->Relocatable;
 }
 
 uint64_t MipsGotSection::getGp(const InputFile *F) const {
@@ -1111,10 +1111,10 @@ void GotPltSection::writeTo(uint8_t *Buf
   }
 }
 
-bool GotPltSection::empty() const {
+bool GotPltSection::isNeeded() const {
   // We need to emit GOTPLT even if it's empty if there's a relocation relative
   // to it.
-  return Entries.empty() && !HasGotPltOffRel;
+  return !Entries.empty() || HasGotPltOffRel;
 }
 
 static StringRef getIgotPltName() {
@@ -1320,7 +1320,7 @@ template <class ELFT> void DynamicSectio
   if (OutputSection *Sec = In.DynStrTab->getParent())
     this->Link = Sec->SectionIndex;
 
-  if (!In.RelaDyn->empty()) {
+  if (In.RelaDyn->isNeeded()) {
     addInSec(In.RelaDyn->DynamicTag, In.RelaDyn);
     addSize(In.RelaDyn->SizeDynamicTag, In.RelaDyn->getParent());
 
@@ -1434,7 +1434,7 @@ template <class ELFT> void DynamicSectio
   }
 
   // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
-  if (Config->EMachine == EM_PPC64 && !In.Plt->empty()) {
+  if (Config->EMachine == EM_PPC64 && In.Plt->isNeeded()) {
     // The Glink tag points to 32 bytes before the first lazy symbol resolution
     // stub, which starts directly after the header.
     Entries.push_back({DT_PPC64_GLINK, [=] {
@@ -2081,7 +2081,7 @@ void SymtabShndxSection::writeTo(uint8_t
   }
 }
 
-bool SymtabShndxSection::empty() const {
+bool SymtabShndxSection::isNeeded() const {
   // SHT_SYMTAB can hold symbols with section indices values up to
   // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
   // section. Problem is that we reveal the final section indices a bit too
@@ -2091,7 +2091,7 @@ bool SymtabShndxSection::empty() const {
   for (BaseCommand *Base : Script->SectionCommands)
     if (isa<OutputSection>(Base))
       ++Size;
-  return Size < SHN_LORESERVE;
+  return Size >= SHN_LORESERVE;
 }
 
 void SymtabShndxSection::finalizeContents() {
@@ -2650,7 +2650,7 @@ void GdbIndexSection::writeTo(uint8_t *B
   }
 }
 
-bool GdbIndexSection::empty() const { return Chunks.empty(); }
+bool GdbIndexSection::isNeeded() const { return !Chunks.empty(); }
 
 EhFrameHeader::EhFrameHeader()
     : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {}
@@ -2693,7 +2693,7 @@ size_t EhFrameHeader::getSize() const {
   return 12 + In.EhFrame->NumFdes * 8;
 }
 
-bool EhFrameHeader::empty() const { return In.EhFrame->empty(); }
+bool EhFrameHeader::isNeeded() const { return In.EhFrame->isNeeded(); }
 
 VersionDefinitionSection::VersionDefinitionSection()
     : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
@@ -2778,8 +2778,8 @@ void VersionTableSection::writeTo(uint8_
   }
 }
 
-bool VersionTableSection::empty() const {
-  return !In.VerDef && In.VerNeed->empty();
+bool VersionTableSection::isNeeded() const {
+  return In.VerDef || In.VerNeed->isNeeded();
 }
 
 VersionNeedBaseSection::VersionNeedBaseSection()
@@ -2865,8 +2865,8 @@ template <class ELFT> size_t VersionNeed
   return Size;
 }
 
-template <class ELFT> bool VersionNeedSection<ELFT>::empty() const {
-  return getNeedNum() == 0;
+template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const {
+  return getNeedNum() != 0;
 }
 
 void MergeSyntheticSection::addSection(MergeInputSection *MS) {
@@ -3305,14 +3305,14 @@ void PPC64LongBranchTargetSection::write
   }
 }
 
-bool PPC64LongBranchTargetSection::empty() const {
+bool PPC64LongBranchTargetSection::isNeeded() const {
   // `removeUnusedSyntheticSections()` is called before thunk allocation which
   // is too early to determine if this section will be empty or not. We need
   // Finalized to keep the section alive until after thunk creation. Finalized
   // only gets set to true once `finalizeSections()` is called after thunk
   // creation. Becuase of this, if we don't create any long-branch thunks we end
   // up with an empty .branch_lt section in the binary.
-  return Finalized && Entries.empty();
+  return !Finalized || !Entries.empty();
 }
 
 InStruct elf::In;

Modified: lld/trunk/ELF/SyntheticSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.h?rev=357377&r1=357376&r2=357377&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.h (original)
+++ lld/trunk/ELF/SyntheticSections.h Mon Apr  1 01:16:08 2019
@@ -48,7 +48,7 @@ public:
   // If the section has the SHF_ALLOC flag and the size may be changed if
   // thunks are added, update the section size.
   virtual bool updateAllocSize() { return false; }
-  virtual bool empty() const { return false; }
+  virtual bool isNeeded() const { return true; }
 
   static bool classof(const SectionBase *D) {
     return D->kind() == InputSectionBase::Synthetic;
@@ -66,7 +66,7 @@ public:
   EhFrameSection();
   void writeTo(uint8_t *Buf) override;
   void finalizeContents() override;
-  bool empty() const override { return Sections.empty(); }
+  bool isNeeded() const override { return !Sections.empty(); }
   size_t getSize() const override { return Size; }
 
   template <class ELFT> void addSection(InputSectionBase *S);
@@ -111,7 +111,7 @@ public:
   GotSection();
   size_t getSize() const override { return Size; }
   void finalizeContents() override;
-  bool empty() const override;
+  bool isNeeded() const override;
   void writeTo(uint8_t *Buf) override;
 
   void addEntry(Symbol &Sym);
@@ -171,7 +171,7 @@ public:
   void writeTo(uint8_t *) override {
     llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
   }
-  bool empty() const override { return getSize() == 0; }
+  bool isNeeded() const override { return Size != 0; }
   size_t getSize() const override { return Size; }
 
   static bool classof(const SectionBase *S) { return S->Bss; }
@@ -185,7 +185,7 @@ public:
   size_t getSize() const override { return Size; }
   bool updateAllocSize() override;
   void finalizeContents() override;
-  bool empty() const override;
+  bool isNeeded() const override;
 
   // Join separate GOTs built for each input file to generate
   // primary and optional multiple secondary GOTs.
@@ -361,7 +361,7 @@ public:
   void addEntry(Symbol &Sym);
   size_t getSize() const override;
   void writeTo(uint8_t *Buf) override;
-  bool empty() const override;
+  bool isNeeded() const override;
 
   // Flag to force GotPlt to be in output if we have relocations
   // that relies on its address.
@@ -381,7 +381,7 @@ public:
   void addEntry(Symbol &Sym);
   size_t getSize() const override;
   void writeTo(uint8_t *Buf) override;
-  bool empty() const override { return Entries.empty(); }
+  bool isNeeded() const override { return !Entries.empty(); }
 
 private:
   std::vector<const Symbol *> Entries;
@@ -483,7 +483,7 @@ public:
                 uint64_t OffsetInSec, Symbol *Sym, int64_t Addend, RelExpr Expr,
                 RelType Type);
   void addReloc(const DynamicReloc &Reloc);
-  bool empty() const override { return Relocs.empty(); }
+  bool isNeeded() const override { return !Relocs.empty(); }
   size_t getSize() const override { return Relocs.size() * this->Entsize; }
   size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
   void finalizeContents() override;
@@ -535,7 +535,7 @@ struct RelativeReloc {
 class RelrBaseSection : public SyntheticSection {
 public:
   RelrBaseSection();
-  bool empty() const override { return Relocs.empty(); }
+  bool isNeeded() const override { return !Relocs.empty(); }
   std::vector<RelativeReloc> Relocs;
 };
 
@@ -602,7 +602,7 @@ public:
 
   void writeTo(uint8_t *Buf) override;
   size_t getSize() const override;
-  bool empty() const override;
+  bool isNeeded() const override;
   void finalizeContents() override;
 };
 
@@ -659,7 +659,7 @@ public:
   PltSection(bool IsIplt);
   void writeTo(uint8_t *Buf) override;
   size_t getSize() const override;
-  bool empty() const override { return Entries.empty(); }
+  bool isNeeded() const override { return !Entries.empty(); }
   void addSymbols();
   template <class ELFT> void addEntry(Symbol &Sym);
 
@@ -706,7 +706,7 @@ public:
   template <typename ELFT> static GdbIndexSection *create();
   void writeTo(uint8_t *Buf) override;
   size_t getSize() const override { return Size; }
-  bool empty() const override;
+  bool isNeeded() const override;
 
 private:
   struct GdbIndexHeader {
@@ -746,7 +746,7 @@ public:
   void write();
   void writeTo(uint8_t *Buf) override;
   size_t getSize() const override;
-  bool empty() const override;
+  bool isNeeded() const override;
 };
 
 // For more information about .gnu.version and .gnu.version_r see:
@@ -783,7 +783,7 @@ public:
   void finalizeContents() override;
   size_t getSize() const override;
   void writeTo(uint8_t *Buf) override;
-  bool empty() const override;
+  bool isNeeded() const override;
 };
 
 class VersionNeedBaseSection : public SyntheticSection {
@@ -817,7 +817,7 @@ public:
   void writeTo(uint8_t *Buf) override;
   size_t getSize() const override;
   size_t getNeedNum() const override { return Needed.size(); }
-  bool empty() const override;
+  bool isNeeded() const override;
 };
 
 // MergeSyntheticSection is a class that allows us to put mergeable sections
@@ -977,7 +977,7 @@ public:
   ARMExidxSyntheticSection();
   size_t getSize() const override { return Size; }
   void writeTo(uint8_t *Buf) override;
-  bool empty() const override { return Empty; }
+  bool isNeeded() const override { return !Empty; }
   // Sort and remove duplicate entries.
   void finalizeContents() override;
   InputSection *getLinkOrderDep() const;
@@ -1043,7 +1043,7 @@ public:
   void addEntry(Symbol &Sym);
   size_t getSize() const override;
   void writeTo(uint8_t *Buf) override;
-  bool empty() const override;
+  bool isNeeded() const override;
   void finalizeContents() override { Finalized = true; }
 
 private:

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=357377&r1=357376&r2=357377&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Apr  1 01:16:08 2019
@@ -961,7 +961,7 @@ template <class ELFT> void Writer<ELFT>:
   }
 
   // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
-  if (ElfSym::RelaIpltStart && !In.RelaIplt->empty()) {
+  if (ElfSym::RelaIpltStart && In.RelaIplt->isNeeded()) {
     ElfSym::RelaIpltStart->Section = In.RelaIplt;
     ElfSym::RelaIpltEnd->Section = In.RelaIplt;
     ElfSym::RelaIpltEnd->Value = In.RelaIplt->getSize();
@@ -1486,7 +1486,7 @@ template <class ELFT> void Writer<ELFT>:
 }
 
 static void finalizeSynthetic(SyntheticSection *Sec) {
-  if (Sec && !Sec->empty() && Sec->getParent())
+  if (Sec && Sec->isNeeded() && Sec->getParent())
     Sec->finalizeContents();
 }
 
@@ -1511,7 +1511,7 @@ static void removeUnusedSyntheticSection
     if (!SS)
       return;
     OutputSection *OS = SS->getParent();
-    if (!OS || !SS->empty())
+    if (!OS || SS->isNeeded())
       continue;
 
     // If we reach here, then SS is an unused synthetic section and we want to
@@ -1603,9 +1603,9 @@ template <class ELFT> void Writer<ELFT>:
 
   addIRelativeRelocs();
 
-  if (In.Plt && !In.Plt->empty())
+  if (In.Plt && In.Plt->isNeeded())
     In.Plt->addSymbols();
-  if (In.Iplt && !In.Iplt->empty())
+  if (In.Iplt && In.Iplt->isNeeded())
     In.Iplt->addSymbols();
 
   if (!Config->AllowShlibUndefined) {
@@ -1950,7 +1950,7 @@ template <class ELFT> std::vector<PhdrEn
     Ret.push_back(RelRo);
 
   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
-  if (!In.EhFrame->empty() && In.EhFrameHdr && In.EhFrame->getParent() &&
+  if (In.EhFrame->isNeeded() && In.EhFrameHdr && In.EhFrame->getParent() &&
       In.EhFrameHdr->getParent())
     AddHdr(PT_GNU_EH_FRAME, In.EhFrameHdr->getParent()->getPhdrFlags())
         ->add(In.EhFrameHdr->getParent());




More information about the llvm-commits mailing list