diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 42db014b6e9d..d1c96fd0065f 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -79,6 +79,13 @@ InputSectionBase::InputSectionBase(elf::ObjectFile *File, this->Offset = Hdr->sh_offset; } +template size_t InputSectionBase::getSize() const { + if (auto *D = dyn_cast>(this)) + if (D->getThunksSize() > 0) + return D->getThunkOff() + D->getThunksSize(); + return Data.size(); +} + // Returns a string for an error message. template static std::string getName(SectionT *Sec) { return (Sec->getFile()->getName() + "(" + Sec->Name + ")").str(); @@ -200,11 +207,6 @@ bool InputSection::classof(const InputSectionData *S) { return S->kind() == Base::Regular; } -template size_t InputSection::getSize() const { - return getThunksSize() > 0 ? getThunkOff() + getThunksSize() - : this->Data.size(); -} - template InputSectionBase *InputSection::getRelocatedSection() { assert(this->Type == SHT_RELA || this->Type == SHT_REL); diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index 78eeb9f134a4..62fc5e6a0f21 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -52,8 +52,6 @@ private: unsigned SectionKind : 3; public: - virtual ~InputSectionData() = default; - InputSectionData(InputSectionData &&) = default; Kind kind() const { return (Kind)SectionKind; } unsigned Live : 1; // for garbage collection @@ -68,9 +66,6 @@ public: return llvm::makeArrayRef((const T *)Data.data(), S / sizeof(T)); } - virtual void writeTo(uint8_t *Buf) {} - virtual size_t getSize() const { return Data.size(); } - // If a section is compressed, this has the uncompressed section data. std::unique_ptr UncompressedData; @@ -118,6 +113,9 @@ public: // this but instead this->Repl. InputSectionBase *Repl; + // Returns the size of this section (even if this is a common or BSS.) + size_t getSize() const; + static InputSectionBase Discarded; ObjectFile *getFile() const { return File; } @@ -245,7 +243,7 @@ public: // Write this section to a mmap'ed file, assuming Buf is pointing to // beginning of the output section. - void writeTo(uint8_t *Buf) override; + void writeTo(uint8_t *Buf); // Relocation sections that refer to this one. llvm::TinyPtrVector RelocSections; @@ -272,9 +270,6 @@ public: // Size of chunk with thunks code. uint64_t getThunksSize() const; - // Size of section in bytes. - size_t getSize() const override; - template void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef Rels); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index f1d6331531fe..b61221557c88 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -95,14 +95,14 @@ BuildIdSection::BuildIdSection(size_t HashSize) ".note.gnu.build-id"), HashSize(HashSize) { this->Live = true; -} -template void BuildIdSection::writeTo(uint8_t *Buf) { + Buf.resize(16 + HashSize); const endianness E = ELFT::TargetEndianness; - write32(Buf, 4); // Name size - write32(Buf + 4, HashSize); // Content size - write32(Buf + 8, NT_GNU_BUILD_ID); // Type - memcpy(Buf + 12, "GNU", 4); // Name string + write32(Buf.data(), 4); // Name size + write32(Buf.data() + 4, HashSize); // Content size + write32(Buf.data() + 8, NT_GNU_BUILD_ID); // Type + memcpy(Buf.data() + 12, "GNU", 4); // Name string + this->Data = ArrayRef(Buf); } template diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 4e62f30c56cf..29388193a8e6 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -24,8 +24,6 @@ public: // .note.gnu.build-id section. template class BuildIdSection : public InputSection { public: - void writeTo(uint8_t *Buf) override; - size_t getSize() const override { return 16 + HashSize; } virtual void writeBuildId(llvm::MutableArrayRef Buf) = 0; virtual ~BuildIdSection() = default; @@ -33,6 +31,7 @@ public: protected: BuildIdSection(size_t HashSize); + std::vector Buf; void computeHash(llvm::MutableArrayRef Buf,