diff options
-rw-r--r-- | lld/ELF/SyntheticSections.h | 2 | ||||
-rw-r--r-- | lld/ELF/Writer.cpp | 42 |
2 files changed, 19 insertions, 25 deletions
diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 3e38fec9655..fead00931b7 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -148,13 +148,13 @@ class BuildIdSection : public SyntheticSection { 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<uint8_t> Buf); private: - size_t HashSize; uint8_t *HashBuf; }; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index b6a70a46327..1ca359e0443 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2541,48 +2541,42 @@ computeHash(llvm::MutableArrayRef<uint8_t> HashBuf, HashFn(HashBuf.data(), Hashes); } -static std::vector<uint8_t> computeBuildId(llvm::ArrayRef<uint8_t> Buf) { - std::vector<uint8_t> BuildId; +template <class ELFT> void Writer<ELFT>::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<uint8_t> BuildId(In.BuildId->HashSize); + llvm::ArrayRef<uint8_t> Buf{Out::BufferStart, size_t(FileSize)}; + switch (Config->BuildId) { case BuildIdKind::Fast: - BuildId.resize(8); computeHash(BuildId, Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { write64le(Dest, xxHash64(Arr)); }); break; case BuildIdKind::Md5: - BuildId.resize(16); - computeHash(BuildId, Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { - memcpy(Dest, MD5::hash(Arr).data(), 16); + computeHash(BuildId, Buf, [&](uint8_t *Dest, ArrayRef<uint8_t> Arr) { + memcpy(Dest, MD5::hash(Arr).data(), In.BuildId->HashSize); }); break; case BuildIdKind::Sha1: - BuildId.resize(20); - computeHash(BuildId, Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { - memcpy(Dest, SHA1::hash(Arr).data(), 20); + computeHash(BuildId, Buf, [&](uint8_t *Dest, ArrayRef<uint8_t> 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 <class ELFT> void Writer<ELFT>::writeBuildId() { - if (!In.BuildId || !In.BuildId->getParent()) - return; - - // Compute a hash of all sections of the output file. - std::vector<uint8_t> BuildId = - computeBuildId({Out::BufferStart, size_t(FileSize)}); In.BuildId->writeBuildId(BuildId); } |