Skip to content

Commit

Permalink
[ELF] Rename SyntheticSection::empty to more appropriate isNeeded() w…
Browse files Browse the repository at this point in the history
…ith 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

llvm-svn: 357377
  • Loading branch information
MaskRay committed Apr 1, 2019
1 parent 38a8241 commit d83fb24
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
36 changes: 18 additions & 18 deletions lld/ELF/SyntheticSections.cpp
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -1320,7 +1320,7 @@ template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
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());

Expand Down Expand Up @@ -1434,7 +1434,7 @@ template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
}

// 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, [=] {
Expand Down Expand Up @@ -2081,7 +2081,7 @@ void SymtabShndxSection::writeTo(uint8_t *Buf) {
}
}

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
Expand All @@ -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() {
Expand Down Expand Up @@ -2650,7 +2650,7 @@ void GdbIndexSection::writeTo(uint8_t *Buf) {
}
}

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") {}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -2778,8 +2778,8 @@ void VersionTableSection::writeTo(uint8_t *Buf) {
}
}

bool VersionTableSection::empty() const {
return !In.VerDef && In.VerNeed->empty();
bool VersionTableSection::isNeeded() const {
return In.VerDef || In.VerNeed->isNeeded();
}

VersionNeedBaseSection::VersionNeedBaseSection()
Expand Down Expand Up @@ -2865,8 +2865,8 @@ template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
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) {
Expand Down Expand Up @@ -3305,14 +3305,14 @@ void PPC64LongBranchTargetSection::writeTo(uint8_t *Buf) {
}
}

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;
Expand Down
34 changes: 17 additions & 17 deletions lld/ELF/SyntheticSections.h
Expand Up @@ -48,7 +48,7 @@ class SyntheticSection : public InputSection {
// 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;
Expand All @@ -66,7 +66,7 @@ class EhFrameSection final : public SyntheticSection {
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);
Expand Down Expand Up @@ -111,7 +111,7 @@ class GotSection : public SyntheticSection {
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);
Expand Down Expand Up @@ -171,7 +171,7 @@ class BssSection final : public SyntheticSection {
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; }
Expand All @@ -185,7 +185,7 @@ class MipsGotSection final : public SyntheticSection {
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.
Expand Down Expand Up @@ -361,7 +361,7 @@ class GotPltSection final : public SyntheticSection {
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.
Expand All @@ -381,7 +381,7 @@ class IgotPltSection final : public SyntheticSection {
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;
Expand Down Expand Up @@ -483,7 +483,7 @@ class RelocationBaseSection : public SyntheticSection {
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;
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -602,7 +602,7 @@ class SymtabShndxSection final : public SyntheticSection {

void writeTo(uint8_t *Buf) override;
size_t getSize() const override;
bool empty() const override;
bool isNeeded() const override;
void finalizeContents() override;
};

Expand Down Expand Up @@ -659,7 +659,7 @@ class PltSection : public SyntheticSection {
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);

Expand Down Expand Up @@ -706,7 +706,7 @@ class GdbIndexSection final : public SyntheticSection {
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 {
Expand Down Expand Up @@ -746,7 +746,7 @@ class EhFrameHeader final : public SyntheticSection {
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:
Expand Down Expand Up @@ -783,7 +783,7 @@ class VersionTableSection final : public SyntheticSection {
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 {
Expand Down Expand Up @@ -817,7 +817,7 @@ class VersionNeedSection final : public VersionNeedBaseSection {
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
Expand Down Expand Up @@ -977,7 +977,7 @@ class ARMExidxSyntheticSection : public SyntheticSection {
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;
Expand Down Expand Up @@ -1043,7 +1043,7 @@ class PPC64LongBranchTargetSection final : public SyntheticSection {
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:
Expand Down
12 changes: 6 additions & 6 deletions lld/ELF/Writer.cpp
Expand Up @@ -961,7 +961,7 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
}

// .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();
Expand Down Expand Up @@ -1486,7 +1486,7 @@ template <class ELFT> void Writer<ELFT>::maybeAddThunks() {
}

static void finalizeSynthetic(SyntheticSection *Sec) {
if (Sec && !Sec->empty() && Sec->getParent())
if (Sec && Sec->isNeeded() && Sec->getParent())
Sec->finalizeContents();
}

Expand All @@ -1511,7 +1511,7 @@ static void removeUnusedSyntheticSections() {
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
Expand Down Expand Up @@ -1603,9 +1603,9 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {

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) {
Expand Down Expand Up @@ -1950,7 +1950,7 @@ template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() {
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());
Expand Down

0 comments on commit d83fb24

Please sign in to comment.