Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -12,6 +12,7 @@ #include "Config.h" #include "lld/Core/LLVM.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/Object/ELF.h" @@ -143,10 +144,6 @@ SplitInputSection(ObjectFile *File, const Elf_Shdr *Header, typename InputSectionBase::Kind SectionKind); - // For each piece of data, we maintain the offsets in the input section and - // in the output section. - std::vector> Offsets; - // Merge input sections may use the following special values as the output // section offset: enum { @@ -161,6 +158,18 @@ std::pair *, uintX_t> getRangeAndSize(uintX_t Offset); + + std::vector> &getOffsets() { + ensureOffsets(); + return Offsets; + } + +private: + void ensureOffsets(); + + // For each piece of data, we maintain the offsets in the input section and + // in the output section. + std::vector> Offsets; }; // This corresponds to a SHF_MERGE section of an input file. Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -413,11 +413,20 @@ template MergeInputSection::MergeInputSection(elf::ObjectFile *F, const Elf_Shdr *Header) - : SplitInputSection(F, Header, InputSectionBase::Merge) { + : SplitInputSection(F, Header, InputSectionBase::Merge) {} + +template +bool MergeInputSection::classof(const InputSectionBase *S) { + return S->SectionKind == InputSectionBase::Merge; +} + +template void SplitInputSection::ensureOffsets() { + if (this->SectionKind != InputSectionBase::Merge || !Offsets.empty()) + return; + uintX_t EntSize = Header->sh_entsize; ArrayRef D = this->getSectionData(); StringRef Data((const char *)D.data(), D.size()); - std::vector> &Offsets = this->Offsets; uintX_t V = Config->GcSections ? MergeInputSection::PieceDead : MergeInputSection::PieceLive; @@ -443,14 +452,10 @@ } template -bool MergeInputSection::classof(const InputSectionBase *S) { - return S->SectionKind == InputSectionBase::Merge; -} - -template std::pair *, typename ELFT::uint> SplitInputSection::getRangeAndSize(uintX_t Offset) { + MutableArrayRef> Offsets = getOffsets(); ArrayRef D = this->getSectionData(); StringRef Data((const char *)D.data(), D.size()); uintX_t Size = Data.size(); Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -983,7 +983,7 @@ template StringRef EHRegion::data() const { ArrayRef SecData = S->getSectionData(); - ArrayRef> Offsets = S->Offsets; + ArrayRef> Offsets = S->getOffsets(); size_t Start = Offsets[Index].first; size_t End = Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first; @@ -1137,8 +1137,8 @@ DenseMap OffsetToIndex; while (!D.empty()) { - unsigned Index = S->Offsets.size(); - S->Offsets.push_back(std::make_pair(Offset, -1)); + unsigned Index = S->getOffsets().size(); + S->getOffsets().push_back(std::make_pair(Offset, -1)); uintX_t Length = readEntryLength(D); // If CIE/FDE data length is zero then Length is 4, this @@ -1225,11 +1225,11 @@ size_t Offset = 0; for (const Cie &C : Cies) { - C.S->Offsets[C.Index].second = Offset; + C.S->getOffsets()[C.Index].second = Offset; Offset += alignTo(C.data().size(), sizeof(uintX_t)); for (const EHRegion &F : C.Fdes) { - F.S->Offsets[F.Index].second = Offset; + F.S->getOffsets()[F.Index].second = Offset; Offset += alignTo(F.data().size(), sizeof(uintX_t)); } } @@ -1238,11 +1238,11 @@ template void EHOutputSection::writeTo(uint8_t *Buf) { const endianness E = ELFT::TargetEndianness; for (const Cie &C : Cies) { - size_t CieOffset = C.S->Offsets[C.Index].second; + size_t CieOffset = C.S->getOffsets()[C.Index].second; writeAlignedCieOrFde(C.data(), Buf + CieOffset); for (const EHRegion &F : C.Fdes) { - size_t Offset = F.S->Offsets[F.Index].second; + size_t Offset = F.S->getOffsets()[F.Index].second; writeAlignedCieOrFde(F.data(), Buf + Offset); write32(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer @@ -1282,7 +1282,7 @@ StringRef Data((const char *)D.data(), D.size()); uintX_t EntSize = S->getSectionHdr()->sh_entsize; this->Header.sh_entsize = EntSize; - MutableArrayRef> Offsets = S->Offsets; + MutableArrayRef> Offsets = S->getOffsets(); // If this is of type string, the contents are null-terminated strings. if (this->Header.sh_flags & SHF_STRINGS) {