[PATCH] D61078: [LLD][NFC] Refactor: BuildID hash size calculation is now done in a single place.

ben via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 24 10:14:48 PDT 2019


bd1976llvm created this revision.
bd1976llvm added reviewers: ruiu, grimar, MaskRay, peter.smith, pcc.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

https://reviews.llvm.org/D61078

Files:
  lld/ELF/SyntheticSections.h
  lld/ELF/Writer.cpp


Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -2538,48 +2538,42 @@
   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.
+  size_t HashSize = In.BuildId->HashSize;
+  std::vector<uint8_t> 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, [HashSize](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
+      memcpy(Dest, MD5::hash(Arr).data(), 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, [HashSize](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
+      memcpy(Dest, SHA1::hash(Arr).data(), HashSize);
     });
     break;
   case BuildIdKind::Uuid:
-    BuildId.resize(16);
-    if (auto EC = llvm::getRandomBytes(BuildId.data(), 16))
+    if (auto EC = llvm::getRandomBytes(BuildId.data(), 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);
 }
 
Index: lld/ELF/SyntheticSections.h
===================================================================
--- lld/ELF/SyntheticSections.h
+++ lld/ELF/SyntheticSections.h
@@ -148,13 +148,13 @@
   static const unsigned HeaderSize = 16;
 
 public:
+  size_t const 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;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61078.196479.patch
Type: text/x-patch
Size: 2892 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190424/3f79b2f8/attachment.bin>


More information about the llvm-commits mailing list