Index: lld/trunk/ELF/SyntheticSections.h =================================================================== --- lld/trunk/ELF/SyntheticSections.h +++ lld/trunk/ELF/SyntheticSections.h @@ -48,7 +48,7 @@ // 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 @@ 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 void addSection(InputSectionBase *S); @@ -111,7 +111,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 Entries; @@ -483,7 +483,7 @@ 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 @@ class RelrBaseSection : public SyntheticSection { public: RelrBaseSection(); - bool empty() const override { return Relocs.empty(); } + bool isNeeded() const override { return !Relocs.empty(); } std::vector Relocs; }; @@ -602,7 +602,7 @@ 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 @@ 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 void addEntry(Symbol &Sym); @@ -706,7 +706,7 @@ template 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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: Index: lld/trunk/ELF/SyntheticSections.cpp =================================================================== --- lld/trunk/ELF/SyntheticSections.cpp +++ lld/trunk/ELF/SyntheticSections.cpp @@ -648,10 +648,10 @@ 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 @@ } } -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 @@ } } -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 @@ 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 @@ } // 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 @@ } } -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 @@ for (BaseCommand *Base : Script->SectionCommands) if (isa(Base)) ++Size; - return Size < SHN_LORESERVE; + return Size >= SHN_LORESERVE; } void SymtabShndxSection::finalizeContents() { @@ -2650,7 +2650,7 @@ } } -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 @@ 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 @@ } } -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 @@ return Size; } -template bool VersionNeedSection::empty() const { - return getNeedNum() == 0; +template bool VersionNeedSection::isNeeded() const { + return getNeedNum() != 0; } void MergeSyntheticSection::addSection(MergeInputSection *MS) { @@ -3305,14 +3305,14 @@ } } -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; Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -961,7 +961,7 @@ } // .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 @@ } static void finalizeSynthetic(SyntheticSection *Sec) { - if (Sec && !Sec->empty() && Sec->getParent()) + if (Sec && Sec->isNeeded() && Sec->getParent()) Sec->finalizeContents(); } @@ -1511,7 +1511,7 @@ 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 @@ 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 @@ 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());