Index: lld/ELF/SyntheticSections.h =================================================================== --- lld/ELF/SyntheticSections.h +++ lld/ELF/SyntheticSections.h @@ -148,13 +148,13 @@ static const unsigned HeaderSize = 16; public: + const size_t HashSize; BuildIdSection(); void writeTo(uint8_t *Buf) override; size_t getSize() const override { return HeaderSize + HashSize; } void writeBuildId(llvm::ArrayRef Buf); private: - size_t HashSize; uint8_t *HashBuf; }; Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -2538,48 +2538,42 @@ HashFn(HashBuf.data(), Hashes); } -static std::vector computeBuildId(llvm::ArrayRef Buf) { - std::vector BuildId; +template void Writer::writeBuildId() { + if (!In.BuildId || !In.BuildId->getParent()) + return; + + if (Config->BuildId == BuildIdKind::Hexstring) { + In.BuildId->writeBuildId(Config->BuildIdVector); + return; + } + + // Compute a hash of all sections of the output file. + std::vector BuildId(In.BuildId->HashSize); + llvm::ArrayRef Buf{Out::BufferStart, size_t(FileSize)}; + switch (Config->BuildId) { case BuildIdKind::Fast: - BuildId.resize(8); computeHash(BuildId, Buf, [](uint8_t *Dest, ArrayRef Arr) { write64le(Dest, xxHash64(Arr)); }); break; case BuildIdKind::Md5: - BuildId.resize(16); - computeHash(BuildId, Buf, [](uint8_t *Dest, ArrayRef Arr) { - memcpy(Dest, MD5::hash(Arr).data(), 16); + computeHash(BuildId, Buf, [&](uint8_t *Dest, ArrayRef Arr) { + memcpy(Dest, MD5::hash(Arr).data(), In.BuildId->HashSize); }); break; case BuildIdKind::Sha1: - BuildId.resize(20); - computeHash(BuildId, Buf, [](uint8_t *Dest, ArrayRef Arr) { - memcpy(Dest, SHA1::hash(Arr).data(), 20); + computeHash(BuildId, Buf, [&](uint8_t *Dest, ArrayRef Arr) { + memcpy(Dest, SHA1::hash(Arr).data(), In.BuildId->HashSize); }); break; case BuildIdKind::Uuid: - BuildId.resize(16); - if (auto EC = llvm::getRandomBytes(BuildId.data(), 16)) + if (auto EC = llvm::getRandomBytes(BuildId.data(), In.BuildId->HashSize)) error("entropy source failure: " + EC.message()); break; - case BuildIdKind::Hexstring: - BuildId = Config->BuildIdVector; - break; default: llvm_unreachable("unknown BuildIdKind"); } - return BuildId; -} - -template void Writer::writeBuildId() { - if (!In.BuildId || !In.BuildId->getParent()) - return; - - // Compute a hash of all sections of the output file. - std::vector BuildId = - computeBuildId({Out::BufferStart, size_t(FileSize)}); In.BuildId->writeBuildId(BuildId); }