diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index ce20b5f0164b..d817fe12174f 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -482,9 +482,6 @@ template void InputSection::writeTo(uint8_t *Buf) { if (this->Type == SHT_NOBITS) return; - // Set output location. - this->OutputLoc = Buf + OutSecOff; - // If -r is given, then an InputSection may be a relocation section. if (this->Type == SHT_RELA) { copyRelocations(Buf + OutSecOff, this->template getDataAs()); @@ -867,33 +864,38 @@ BuildIdSection::BuildIdSection(size_t HashSize) } template -void BuildIdFastHash::writeBuildId(ArrayRef Buf) { +uint8_t *BuildIdSection::getOutputLoc(uint8_t *Start) const { + return Start + this->OutSec->getFileOffset() + this->OutSecOff; +} + +template +void BuildIdFastHash::writeBuildId(MutableArrayRef Buf) { const endianness E = ELFT::TargetEndianness; // 64-bit xxhash uint64_t Hash = xxHash64(toStringRef(Buf)); - write64(this->OutputLoc + 16, Hash); + write64(this->getOutputLoc(Buf.begin()) + 16, Hash); } template -void BuildIdMd5::writeBuildId(ArrayRef Buf) { +void BuildIdMd5::writeBuildId(MutableArrayRef Buf) { MD5 Hash; Hash.update(Buf); MD5::MD5Result Res; Hash.final(Res); - memcpy(this->OutputLoc + 16, Res, 16); + memcpy(this->getOutputLoc(Buf.begin()) + 16, Res, 16); } template -void BuildIdSha1::writeBuildId(ArrayRef Buf) { +void BuildIdSha1::writeBuildId(MutableArrayRef Buf) { SHA1 Hash; Hash.update(Buf); - memcpy(this->OutputLoc + 16, Hash.final().data(), 20); + memcpy(this->getOutputLoc(Buf.begin()) + 16, Hash.final().data(), 20); } template -void BuildIdUuid::writeBuildId(ArrayRef Buf) { - if (getRandomBytes(this->OutputLoc + 16, 16)) +void BuildIdUuid::writeBuildId(MutableArrayRef Buf) { + if (getRandomBytes(this->getOutputLoc(Buf.begin()) + 16, 16)) error("entropy source failure"); } @@ -902,8 +904,8 @@ BuildIdHexstring::BuildIdHexstring() : BuildIdSection(Config->BuildIdVector.size()) {} template -void BuildIdHexstring::writeBuildId(ArrayRef Buf) { - memcpy(this->OutputLoc + 16, Config->BuildIdVector.data(), +void BuildIdHexstring::writeBuildId(MutableArrayRef Buf) { + memcpy(this->getOutputLoc(Buf.begin()) + 16, Config->BuildIdVector.data(), Config->BuildIdVector.size()); } diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index e4b23dfad120..338368a9dab0 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -252,9 +252,6 @@ public: // to. The writer sets a value. uint64_t OutSecOff = 0; - // Location of this section in the output buffer - uint8_t *OutputLoc = nullptr; - // InputSection that is dependent on us (reverse dependency for GC) InputSectionBase *DependentSection = nullptr; @@ -344,9 +341,11 @@ public: template class BuildIdSection : public InputSection { public: - virtual void writeBuildId(ArrayRef Buf) = 0; + virtual void writeBuildId(llvm::MutableArrayRef Buf) = 0; virtual ~BuildIdSection() = default; + uint8_t *getOutputLoc(uint8_t *Start) const; + protected: BuildIdSection(size_t HashSize); std::vector Buf; @@ -356,32 +355,32 @@ template class BuildIdFastHash final : public BuildIdSection { public: BuildIdFastHash() : BuildIdSection(8) {} - void writeBuildId(ArrayRef Buf) override; + void writeBuildId(llvm::MutableArrayRef Buf) override; }; template class BuildIdMd5 final : public BuildIdSection { public: BuildIdMd5() : BuildIdSection(16) {} - void writeBuildId(ArrayRef Buf) override; + void writeBuildId(llvm::MutableArrayRef Buf) override; }; template class BuildIdSha1 final : public BuildIdSection { public: BuildIdSha1() : BuildIdSection(20) {} - void writeBuildId(ArrayRef Buf) override; + void writeBuildId(llvm::MutableArrayRef Buf) override; }; template class BuildIdUuid final : public BuildIdSection { public: BuildIdUuid() : BuildIdSection(16) {} - void writeBuildId(ArrayRef Buf) override; + void writeBuildId(llvm::MutableArrayRef Buf) override; }; template class BuildIdHexstring final : public BuildIdSection { public: BuildIdHexstring(); - void writeBuildId(ArrayRef) override; + void writeBuildId(llvm::MutableArrayRef) override; }; // Linker generated sections which can be used as inputs.