From 90fa3722d29ea753f3543d8b957aa1939e1f1e8e Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 22 May 2016 00:41:38 +0000 Subject: [PATCH] Simplify SplitInputSection::getRangeAndSize. This patch adds Size member to SectionPiece so that getRangeAndSize can just return a SectionPiece instead of a std::pair. Also renamed the function. llvm-svn: 270346 --- lld/ELF/InputSection.cpp | 18 +++++++----------- lld/ELF/InputSection.h | 6 ++++-- lld/ELF/MarkLive.cpp | 5 ++--- lld/ELF/OutputSections.cpp | 6 +++--- lld/ELF/Writer.cpp | 2 +- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 58e8e63bf6dc..592f15221d0c 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -414,7 +414,7 @@ typename ELFT::uint EHInputSection::getOffset(uintX_t Offset) { // identify the start of the output .eh_frame. Handle this special case. if (this->getSectionHdr()->sh_size == 0) return Offset; - SectionPiece *Piece = this->getRangeAndSize(Offset).first; + SectionPiece *Piece = this->getSectionPiece(Offset); if (Piece->OutputOff == size_t(-1)) return -1; // Not in the output @@ -449,8 +449,8 @@ MergeInputSection::MergeInputSection(elf::ObjectFile *F, size_t End = findNull(Data, EntSize); if (End == StringRef::npos) fatal("string is not null terminated"); - this->Pieces.emplace_back(Offset); uintX_t Size = End + EntSize; + this->Pieces.emplace_back(Offset, Size); Data = Data.substr(Size); Offset += Size; } @@ -461,7 +461,7 @@ MergeInputSection::MergeInputSection(elf::ObjectFile *F, size_t Size = Data.size(); assert((Size % EntSize) == 0); for (unsigned I = 0, N = Size; I != N; I += EntSize) - this->Pieces.emplace_back(I); + this->Pieces.emplace_back(I, EntSize); } template @@ -470,8 +470,7 @@ bool MergeInputSection::classof(const InputSectionBase *S) { } template -std::pair -SplitInputSection::getRangeAndSize(uintX_t Offset) { +SectionPiece *SplitInputSection::getSectionPiece(uintX_t Offset) { ArrayRef D = this->getSectionData(); StringRef Data((const char *)D.data(), D.size()); uintX_t Size = Data.size(); @@ -482,16 +481,13 @@ SplitInputSection::getRangeAndSize(uintX_t Offset) { auto I = std::upper_bound( Pieces.begin(), Pieces.end(), Offset, [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; }); - uintX_t End = (I == Pieces.end()) ? Data.size() : I->InputOff; --I; - return {&*I, End}; + return &*I; } template typename ELFT::uint MergeInputSection::getOffset(uintX_t Offset) { - std::pair T = this->getRangeAndSize(Offset); - SectionPiece &Piece = *T.first; - uintX_t End = T.second; + SectionPiece &Piece = *this->getSectionPiece(Offset); assert(Piece.Live); // Compute the Addend and if the Base is cached, return. @@ -502,7 +498,7 @@ typename ELFT::uint MergeInputSection::getOffset(uintX_t Offset) { // Map the base to the offset in the output section and cache it. ArrayRef D = this->getSectionData(); StringRef Data((const char *)D.data(), D.size()); - StringRef Entry = Data.substr(Piece.InputOff, End - Piece.InputOff); + StringRef Entry = Data.substr(Piece.InputOff, Piece.Size); auto *MOS = static_cast *>(this->OutSec); Piece.OutputOff = MOS->getOffset(Entry); return Piece.OutputOff + Addend; diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index f2458592b69d..aec4c38d2cae 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -132,8 +132,10 @@ template InputSectionBase InputSectionBase::Discarded; // SectionPiece represents a piece of splittable section contents. struct SectionPiece { - SectionPiece(size_t I) : InputOff(I), Live(!Config->GcSections) {} + SectionPiece(size_t Off, size_t Size) + : InputOff(Off), Size(Size), Live(!Config->GcSections) {} size_t InputOff; + size_t Size; size_t OutputOff = -1; bool Live; }; @@ -154,7 +156,7 @@ public: // rather than a single large blob of data. std::vector Pieces; - std::pair getRangeAndSize(uintX_t Offset); + SectionPiece *getSectionPiece(uintX_t Offset); }; // This corresponds to a SHF_MERGE section of an input file. diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index 6680f6950412..87ff847041e5 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -136,15 +136,14 @@ template static bool isReserved(InputSectionBase *Sec) { // Starting from GC-root sections, this function visits all reachable // sections to set their "Live" bits. template void elf::markLive() { - typedef typename ELFT::uint uintX_t; SmallVector *, 256> Q; auto Enqueue = [&](ResolvedReloc R) { if (!R.Sec) return; if (auto *MS = dyn_cast>(R.Sec)) { - std::pair T = MS->getRangeAndSize(R.Offset); - T.first->Live = true; + SectionPiece *Piece = MS->getSectionPiece(R.Offset); + Piece->Live = true; } if (R.Sec->Live) return; diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index d54b9c4d0c43..c235583ef14a 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -1142,9 +1142,6 @@ void EHOutputSection::addSectionAux(EHInputSection *Sec, DenseMap OffsetToIndex; while (!D.empty()) { - unsigned Index = Sec->Pieces.size(); - Sec->Pieces.emplace_back(Offset); - uintX_t Length = readEntryLength(D); // If CIE/FDE data length is zero then Length is 4, this // shall be considered a terminator and processing shall end. @@ -1152,6 +1149,9 @@ void EHOutputSection::addSectionAux(EHInputSection *Sec, break; StringRef Entry((const char *)D.data(), Length); + unsigned Index = Sec->Pieces.size(); + Sec->Pieces.emplace_back(Offset, Length); + uint32_t ID = read32(D.data() + 4); if (ID == 0) { // CIE diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 0105991f8fe4..1f84102eacbf 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -855,7 +855,7 @@ template static bool includeInSymtab(const SymbolBody &B) { if (!D->Section->Live) return false; if (auto *S = dyn_cast>(D->Section)) - if (!S->getRangeAndSize(D->Value).first->Live) + if (!S->getSectionPiece(D->Value)->Live) return false; } return true;