Index: ELF/ICF.cpp =================================================================== --- ELF/ICF.cpp +++ ELF/ICF.cpp @@ -233,8 +233,7 @@ } return A->getSectionHdr()->sh_flags == B->getSectionHdr()->sh_flags && - A->getSize() == B->getSize() && - A->getSectionData() == B->getSectionData(); + A->getSize() == B->getSize() && A->Data == B->Data; } template Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -42,9 +42,10 @@ // The garbage collector sets sections' Live bits. // If GC is disabled, all sections are considered live by default. - InputSectionData(Kind SectionKind, StringRef Name, bool Compressed, bool Live) + InputSectionData(Kind SectionKind, StringRef Name, ArrayRef Data, + bool Compressed, bool Live) : SectionKind(SectionKind), Live(Live), Compressed(Compressed), - Name(Name) {} + Name(Name), Data(Data) {} private: unsigned SectionKind : 3; @@ -61,9 +62,10 @@ StringRef Name; + ArrayRef Data; + // If a section is compressed, this has the uncompressed section data. std::unique_ptr UncompressedData; - size_t UncompressedDataSize = 0; std::vector Relocations; }; @@ -84,7 +86,8 @@ public: InputSectionBase() - : InputSectionData(Regular, "", false, false), Repl(this) {} + : InputSectionData(Regular, "", ArrayRef(), false, false), + Repl(this) {} InputSectionBase(ObjectFile *File, const Elf_Shdr *Header, StringRef Name, Kind SectionKind); @@ -110,8 +113,6 @@ // section. uintX_t getOffset(uintX_t Offset) const; - ArrayRef getSectionData() const; - void uncompress(); void relocate(uint8_t *Buf, uint8_t *BufEnd); Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -29,11 +29,19 @@ using namespace lld::elf; template +static ArrayRef getSectionContents(elf::ObjectFile *File, + const typename ELFT::Shdr *Hdr) { + if (!File || Hdr->sh_type == SHT_NOBITS) + return {}; + return check(File->getObj().getSectionContents(Hdr)); +} + +template InputSectionBase::InputSectionBase(elf::ObjectFile *File, const Elf_Shdr *Hdr, StringRef Name, Kind SectionKind) - : InputSectionData(SectionKind, Name, Hdr->sh_flags & SHF_COMPRESSED, - !Config->GcSections), + : InputSectionData(SectionKind, Name, getSectionContents(File, Hdr), + Hdr->sh_flags & SHF_COMPRESSED, !Config->GcSections), Header(Hdr), File(File), Repl(this) { // The ELF spec states that a value of 0 means the section has // no alignment constraits. @@ -47,14 +55,6 @@ return Header->sh_size; } -template -ArrayRef InputSectionBase::getSectionData() const { - if (Compressed) - return ArrayRef((const uint8_t *)UncompressedData.get(), - UncompressedDataSize); - return check(this->File->getObj().getSectionContents(this->Header)); -} - // Returns a string for an error message. template static std::string getName(SectionT *Sec) { return (Sec->getFile()->getName() + "(" + Sec->Name + ")").str(); @@ -94,8 +94,6 @@ // A compressed section consists of a header of Elf_Chdr type // followed by compressed data. - ArrayRef Data = - check(this->File->getObj().getSectionContents(this->Header)); if (Data.size() < sizeof(Elf_Chdr)) fatal("corrupt compressed section"); @@ -106,11 +104,13 @@ fatal(getName(this) + ": unsupported compression type"); StringRef Buf((const char *)Data.data(), Data.size()); - UncompressedDataSize = Hdr->ch_size; + size_t UncompressedDataSize = Hdr->ch_size; UncompressedData.reset(new char[UncompressedDataSize]); if (zlib::uncompress(Buf, UncompressedData.get(), UncompressedDataSize) != zlib::StatusOK) fatal(getName(this) + ": error uncompressing section"); + Data = ArrayRef((uint8_t *)UncompressedData.get(), + UncompressedDataSize); } template @@ -406,7 +406,7 @@ } // Copy section contents from source object file to output file. - ArrayRef Data = this->getSectionData(); + ArrayRef Data = this->Data; memcpy(Buf + OutSecOff, Data.data(), Data.size()); // Iterate over all relocation sections that apply to this section. @@ -490,7 +490,7 @@ template template void EhInputSection::split(ArrayRef Rels) { - ArrayRef Data = this->getSectionData(); + ArrayRef Data = this->Data; unsigned RelI = 0; for (size_t Off = 0, End = Data.size(); Off != End;) { size_t Size = readEhRecordSize(Data.slice(Off)); @@ -557,7 +557,7 @@ : InputSectionBase(F, Header, Name, InputSectionBase::Merge) {} template void MergeInputSection::splitIntoPieces() { - ArrayRef Data = this->getSectionData(); + ArrayRef Data = this->Data; uintX_t EntSize = this->Header->sh_entsize; if (this->Header->sh_flags & SHF_STRINGS) this->Pieces = splitStrings(Data, EntSize); @@ -584,9 +584,7 @@ template const SectionPiece * MergeInputSection::getSectionPiece(uintX_t Offset) const { - ArrayRef D = this->getSectionData(); - StringRef Data((const char *)D.data(), D.size()); - uintX_t Size = Data.size(); + uintX_t Size = this->Data.size(); if (Offset >= Size) fatal(getName(this) + ": entry is past the end of the section"); @@ -639,13 +637,13 @@ StringRef Name) : InputSectionBase(F, Hdr, Name, InputSectionBase::MipsReginfo) { + ArrayRef Data = this->Data; // Initialize this->Reginfo. - ArrayRef D = this->getSectionData(); - if (D.size() != sizeof(Elf_Mips_RegInfo)) { + if (Data.size() != sizeof(Elf_Mips_RegInfo)) { error(getName(this) + ": invalid size of .reginfo section"); return; } - Reginfo = reinterpret_cast *>(D.data()); + Reginfo = reinterpret_cast *>(Data.data()); } template @@ -660,7 +658,7 @@ : InputSectionBase(F, Hdr, Name, InputSectionBase::MipsOptions) { // Find ODK_REGINFO option in the section's content. - ArrayRef D = this->getSectionData(); + ArrayRef D = this->Data; while (!D.empty()) { if (D.size() < sizeof(Elf_Mips_Options)) { error(getName(this) + ": invalid size of .MIPS.options section"); @@ -686,12 +684,12 @@ : InputSectionBase(F, Hdr, Name, InputSectionBase::MipsAbiFlags) { // Initialize this->Flags. - ArrayRef D = this->getSectionData(); - if (D.size() != sizeof(Elf_Mips_ABIFlags)) { + ArrayRef Data = this->Data; + if (Data.size() != sizeof(Elf_Mips_ABIFlags)) { error("invalid size of .MIPS.abiflags section"); return; } - Flags = reinterpret_cast *>(D.data()); + Flags = reinterpret_cast *>(Data.data()); } template Index: ELF/MarkLive.cpp =================================================================== --- ELF/MarkLive.cpp +++ ELF/MarkLive.cpp @@ -54,7 +54,7 @@ template static typename ELFT::uint getAddend(InputSectionBase &Sec, const typename ELFT::Rel &Rel) { - return Target->getImplicitAddend(Sec.getSectionData().begin(), + return Target->getImplicitAddend(Sec.Data.begin(), Rel.getType(Config->Mips64EL)); } Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -534,7 +534,7 @@ }; const elf::ObjectFile &File = *C.getFile(); - ArrayRef SectionData = C.getSectionData(); + ArrayRef SectionData = C.Data; const uint8_t *Buf = SectionData.begin(); ArrayRef Pieces;