From 58139d1758c116e4f86d859fe99b11dafca1ec9f Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 25 Oct 2016 16:14:25 +0000 Subject: [PATCH] Delete getSectionHdr. We were fairly inconsistent as to what information should be accessed with getSectionHdr and what information (like alignment) was stored elsewhere. Now all section info has a dedicated getter. The code is also a bit more compact. llvm-svn: 285079 --- lld/ELF/ICF.cpp | 9 ++++----- lld/ELF/InputSection.cpp | 11 +++++------ lld/ELF/InputSection.h | 7 +++++-- lld/ELF/LinkerScript.cpp | 7 +++---- lld/ELF/MarkLive.cpp | 8 ++++---- lld/ELF/OutputSections.cpp | 17 ++++++++--------- lld/ELF/Relocations.cpp | 4 ++-- lld/ELF/Writer.cpp | 4 ++-- 8 files changed, 33 insertions(+), 34 deletions(-) diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp index eb403b1..df4f763 100644 --- a/lld/ELF/ICF.cpp +++ b/lld/ELF/ICF.cpp @@ -119,7 +119,7 @@ private: // Returns a hash value for S. Note that the information about // relocation targets is not included in the hash value. template uint64_t ICF::getHash(InputSection *S) { - uint64_t Flags = S->getSectionHdr()->sh_flags; + uint64_t Flags = S->getFlags(); uint64_t H = hash_combine(Flags, S->getSize()); for (const Elf_Shdr *Rel : S->RelocSections) H = hash_combine(H, (uint64_t)Rel->sh_size); @@ -141,8 +141,7 @@ template bool ICF::isEligible(InputSectionBase *Sec) { if (Name == ".init" || Name == ".fini") return false; - const Elf_Shdr &H = *S->getSectionHdr(); - return (H.sh_flags & SHF_ALLOC) && (~H.sh_flags & SHF_WRITE); + return (S->getFlags() & SHF_ALLOC) && !(S->getFlags() & SHF_WRITE); } template @@ -231,8 +230,8 @@ bool ICF::equalsConstant(const InputSection *A, } } - return A->getSectionHdr()->sh_flags == B->getSectionHdr()->sh_flags && - A->getSize() == B->getSize() && A->Data == B->Data; + return A->getFlags() == B->getFlags() && A->getSize() == B->getSize() && + A->Data == B->Data; } template diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 09acd0f..67bbbd8 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -171,9 +171,8 @@ InputSectionBase::getOffset(const DefinedRegular &Sym) const { template InputSectionBase *InputSectionBase::getLinkOrderDep() const { - const Elf_Shdr *Hdr = getSectionHdr(); - if ((Hdr->sh_flags & SHF_LINK_ORDER) && Hdr->sh_link != 0) - return getFile()->getSections()[Hdr->sh_link]; + if ((getFlags() & SHF_LINK_ORDER) && getLink() != 0) + return getFile()->getSections()[getLink()]; return nullptr; } @@ -589,7 +588,7 @@ std::vector MergeInputSection::splitStrings(ArrayRef Data, size_t EntSize) { std::vector V; size_t Off = 0; - bool IsAlloca = this->getSectionHdr()->sh_flags & SHF_ALLOC; + bool IsAlloca = this->getFlags() & SHF_ALLOC; while (!Data.empty()) { size_t End = findNull(Data, EntSize); if (End == StringRef::npos) @@ -620,7 +619,7 @@ MergeInputSection::splitNonStrings(ArrayRef Data, std::vector V; size_t Size = Data.size(); assert((Size % EntSize) == 0); - bool IsAlloca = this->getSectionHdr()->sh_flags & SHF_ALLOC; + bool IsAlloca = this->getFlags() & SHF_ALLOC; for (unsigned I = 0, N = Size; I != N; I += EntSize) { Hashes.push_back(hash_value(toStringRef(Data.slice(I, EntSize)))); V.emplace_back(I, !IsAlloca); @@ -642,7 +641,7 @@ template void MergeInputSection::splitIntoPieces() { else this->Pieces = splitNonStrings(Data, EntSize); - if (Config->GcSections && (this->getSectionHdr()->sh_flags & SHF_ALLOC)) + if (Config->GcSections && (this->getFlags() & SHF_ALLOC)) for (uintX_t Off : LiveOffsets) this->getSectionPiece(Off)->Live = true; } diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index 1efaeaf..ec2a3f3 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -101,7 +101,10 @@ public: static InputSectionBase Discarded; - const Elf_Shdr *getSectionHdr() const { return Header; } + uintX_t getFlags() const { return Header->sh_flags; } + uint32_t getType() const { return Header->sh_type; } + uintX_t getEntsize() const { return Header->sh_entsize; } + uint32_t getLink() const { return Header->sh_link; } ObjectFile *getFile() const { return File; } uintX_t getOffset(const DefinedRegular &Sym) const; InputSectionBase *getLinkOrderDep() const; @@ -152,7 +155,7 @@ public: // Mark the piece at a given offset live. Used by GC. void markLiveAt(uintX_t Offset) { - assert(this->getSectionHdr()->sh_flags & llvm::ELF::SHF_ALLOC); + assert(this->getFlags() & llvm::ELF::SHF_ALLOC); LiveOffsets.insert(Offset); } diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index ba8bb4c..5fd49eb 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -156,7 +156,7 @@ static bool matchConstraints(ArrayRef *> Sections, return true; bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) { auto *Sec = static_cast *>(Sec2); - return Sec->getSectionHdr()->sh_flags & SHF_WRITE; + return Sec->getFlags() & SHF_WRITE; }); return (IsRW && Kind == ConstraintKind::ReadWrite) || (!IsRW && Kind == ConstraintKind::ReadOnly); @@ -268,13 +268,12 @@ static SectionKey createKey(InputSectionBase *C, // Fortunately, creating symbols in the middle of a merge section is not // supported by bfd or gold, so we can just create multiple section in that // case. - const typename ELFT::Shdr *H = C->getSectionHdr(); typedef typename ELFT::uint uintX_t; - uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS); + uintX_t Flags = C->getFlags() & (SHF_MERGE | SHF_STRINGS); uintX_t Alignment = 0; if (isa>(C)) - Alignment = std::max(H->sh_addralign, H->sh_entsize); + Alignment = std::max(C->Alignment, C->getEntsize()); return SectionKey{OutsecName, /*Type*/ 0, Flags, Alignment}; } diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index eb2b5da..4502d9d 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -135,7 +135,7 @@ scanEhFrameSection(EhInputSection &EH, ArrayRef Rels, ResolvedReloc R = resolveReloc(EH, Rels[I2]); if (!R.Sec || R.Sec == &InputSection::Discarded) continue; - if (R.Sec->getSectionHdr()->sh_flags & SHF_EXECINSTR) + if (R.Sec->getFlags() & SHF_EXECINSTR) continue; Enqueue({R.Sec, 0}); } @@ -164,14 +164,14 @@ scanEhFrameSection(EhInputSection &EH, // 1) Sections used by the loader (.init, .fini, .ctors, .dtors or .jcr) // 2) Non-allocatable sections which typically contain debugging information template static bool isReserved(InputSectionBase *Sec) { - switch (Sec->getSectionHdr()->sh_type) { + switch (Sec->getType()) { case SHT_FINI_ARRAY: case SHT_INIT_ARRAY: case SHT_NOTE: case SHT_PREINIT_ARRAY: return true; default: - if (!(Sec->getSectionHdr()->sh_flags & SHF_ALLOC)) + if (!(Sec->getFlags() & SHF_ALLOC)) return true; // We do not want to reclaim sections if they can be referred @@ -201,7 +201,7 @@ template void elf::markLive() { return; // We don't gc non alloc sections. - if (!(R.Sec->getSectionHdr()->sh_flags & SHF_ALLOC)) + if (!(R.Sec->getFlags() & SHF_ALLOC)) return; // Usually, a whole section is marked as live or dead, but in mergeable diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 27dc2be..e38de2e 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -966,8 +966,8 @@ void OutputSection::addSection(InputSectionBase *C) { this->updateAlignment(S->Alignment); // Keep sh_entsize value of the input section to be able to perform merging // later during a final linking using the generated relocatable object. - if (Config->Relocatable && (S->getSectionHdr()->sh_flags & SHF_MERGE)) - this->Header.sh_entsize = S->getSectionHdr()->sh_entsize; + if (Config->Relocatable && (S->getFlags() & SHF_MERGE)) + this->Header.sh_entsize = S->getEntsize(); } // This function is called after we sort input sections @@ -1304,7 +1304,7 @@ void MergeOutputSection::addSection(InputSectionBase *C) { auto *Sec = cast>(C); Sec->OutSec = this; this->updateAlignment(Sec->Alignment); - this->Header.sh_entsize = Sec->getSectionHdr()->sh_entsize; + this->Header.sh_entsize = Sec->getEntsize(); Sections.push_back(Sec); auto HashI = Sec->Hashes.begin(); @@ -1896,13 +1896,12 @@ void MipsAbiFlagsOutputSection::addSection(InputSectionBase *C) { template static typename ELFT::uint getOutFlags(InputSectionBase *S) { - return S->getSectionHdr()->sh_flags & ~SHF_GROUP & ~SHF_COMPRESSED; + return S->getFlags() & ~SHF_GROUP & ~SHF_COMPRESSED; } template static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) { - const typename ELFT::Shdr *H = C->getSectionHdr(); typedef typename ELFT::uint uintX_t; uintX_t Flags = getOutFlags(C); @@ -1914,10 +1913,10 @@ static SectionKey createKey(InputSectionBase *C, // output sections for them to allow merging at final linking stage. uintX_t Alignment = 0; if (isa>(C) || - (Config->Relocatable && (H->sh_flags & SHF_MERGE))) - Alignment = std::max(H->sh_addralign, H->sh_entsize); + (Config->Relocatable && (C->getFlags() & SHF_MERGE))) + Alignment = std::max(C->Alignment, C->getEntsize()); - uint32_t Type = H->sh_type; + uint32_t Type = C->getType(); return SectionKey{OutsecName, Type, Flags, Alignment}; } @@ -1940,7 +1939,7 @@ OutputSectionFactory::create(const SectionKey &Key, return {Sec, false}; } - uint32_t Type = C->getSectionHdr()->sh_type; + uint32_t Type = C->getType(); switch (C->kind()) { case InputSectionBase::Regular: Sec = new OutputSection(Key.Name, Type, Flags); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 62f81f7..a75e9db 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -132,7 +132,7 @@ static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body, InputSectionBase &C, typename ELFT::uint Offset, typename ELFT::uint Addend, RelExpr Expr) { - if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC)) + if (!(C.getFlags() & SHF_ALLOC)) return 0; if (!Body.isTls()) @@ -558,7 +558,7 @@ template static void scanRelocs(InputSectionBase &C, ArrayRef Rels) { typedef typename ELFT::uint uintX_t; - bool IsWrite = C.getSectionHdr()->sh_flags & SHF_WRITE; + bool IsWrite = C.getFlags() & SHF_WRITE; auto AddDyn = [=](const DynamicReloc &Reloc) { Out::RelaDyn->addReloc(Reloc); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 417060f..037d4d9 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -345,7 +345,7 @@ static bool shouldKeepInSymtab(InputSectionBase *Sec, StringRef SymName, if (Config->Discard == DiscardPolicy::Locals) return false; - return !Sec || !(Sec->getSectionHdr()->sh_flags & SHF_MERGE); + return !Sec || !(Sec->getFlags() & SHF_MERGE); } template static bool includeInSymtab(const SymbolBody &B) { @@ -675,7 +675,7 @@ void Writer::forEachRelSec( // creating GOT, PLT, copy relocations, etc. // Note that relocations for non-alloc sections are directly // processed by InputSection::relocateNonAlloc. - if (!(IS->getSectionHdr()->sh_flags & SHF_ALLOC)) + if (!(IS->getFlags() & SHF_ALLOC)) continue; if (auto *S = dyn_cast>(IS)) { for (const Elf_Shdr *RelSec : S->RelocSections) -- 2.7.4