[lld] r287603 - Merge BuildId subclasses.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 21 16:54:15 PST 2016


Author: ruiu
Date: Mon Nov 21 18:54:15 2016
New Revision: 287603

URL: http://llvm.org/viewvc/llvm-project?rev=287603&view=rev
Log:
Merge BuildId subclasses.

We had five different BuildId subclasses for five different types
of build-ids. They can simply be merged to a single class.

Modified:
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/SyntheticSections.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=287603&r1=287602&r2=287603&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Mon Nov 21 18:54:15 2016
@@ -245,10 +245,10 @@ template <class ELFT> InputSection<ELFT>
 }
 
 template <class ELFT>
-BuildIdSection<ELFT>::BuildIdSection(size_t HashSize)
+BuildIdSection<ELFT>::BuildIdSection()
     : InputSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ArrayRef<uint8_t>(),
                          ".note.gnu.build-id"),
-      HashSize(HashSize) {
+      HashSize(getHashSize()) {
   this->Live = true;
 
   Buf.resize(HeaderSize + HashSize);
@@ -262,10 +262,26 @@ BuildIdSection<ELFT>::BuildIdSection(siz
 
 // Returns the location of the build-id hash value in the output.
 template <class ELFT>
-uint8_t *BuildIdSection<ELFT>::getOutputLoc(uint8_t *Start) const {
+uint8_t *BuildIdSection<ELFT>::getOutputLoc(uint8_t *Start) {
   return Start + this->OutSec->Offset + this->OutSecOff + HeaderSize;
 }
 
+template <class ELFT> size_t BuildIdSection<ELFT>::getHashSize() {
+  switch (Config->BuildId) {
+  case BuildIdKind::Fast:
+    return 8;
+  case BuildIdKind::Md5:
+  case BuildIdKind::Uuid:
+    return 16;
+  case BuildIdKind::Sha1:
+    return 20;
+  case BuildIdKind::Hexstring:
+    return Config->BuildIdVector.size();
+  default:
+    llvm_unreachable("unknown BuildIdKind");
+  }
+}
+
 // Split one uint8 array into small pieces of uint8 arrays.
 static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
                                             size_t ChunkSize) {
@@ -300,50 +316,44 @@ void BuildIdSection<ELFT>::computeHash(
   else
     std::for_each(Chunks.begin(), Chunks.end(), Fn);
 
-  HashFn(HashList, this->getOutputLoc(Data.begin()));
-}
-
-template <class ELFT>
-void BuildIdFastHash<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  this->computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
-    write64le(Dest, xxHash64(toStringRef(Arr)));
-  });
-}
-
-template <class ELFT>
-void BuildIdMd5<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  this->computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
-    MD5 Hash;
-    Hash.update(Arr);
-    MD5::MD5Result Res;
-    Hash.final(Res);
-    memcpy(Dest, Res, 16);
-  });
-}
-
-template <class ELFT>
-void BuildIdSha1<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  this->computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
-    SHA1 Hash;
-    Hash.update(Arr);
-    memcpy(Dest, Hash.final().data(), 20);
-  });
-}
-
-template <class ELFT>
-void BuildIdUuid<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  if (getRandomBytes(this->getOutputLoc(Buf.data()), this->HashSize))
-    error("entropy source failure");
+  HashFn(HashList, getOutputLoc(Data.begin()));
 }
 
 template <class ELFT>
-BuildIdHexstring<ELFT>::BuildIdHexstring()
-    : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {}
-
-template <class ELFT>
-void BuildIdHexstring<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  memcpy(this->getOutputLoc(Buf.data()), Config->BuildIdVector.data(),
-         Config->BuildIdVector.size());
+void BuildIdSection<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
+  switch (Config->BuildId) {
+  case BuildIdKind::Fast:
+    computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
+      write64le(Dest, xxHash64(toStringRef(Arr)));
+    });
+    break;
+  case BuildIdKind::Md5:
+    computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
+      MD5 Hash;
+      Hash.update(Arr);
+      MD5::MD5Result Res;
+      Hash.final(Res);
+      memcpy(Dest, Res, 16);
+    });
+    break;
+  case BuildIdKind::Sha1:
+    computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
+      SHA1 Hash;
+      Hash.update(Arr);
+      memcpy(Dest, Hash.final().data(), 20);
+    });
+    break;
+  case BuildIdKind::Uuid:
+    if (getRandomBytes(getOutputLoc(Buf.data()), HashSize))
+      error("entropy source failure");
+    break;
+  case BuildIdKind::Hexstring:
+    memcpy(this->getOutputLoc(Buf.data()), Config->BuildIdVector.data(),
+           Config->BuildIdVector.size());
+    break;
+  default:
+    llvm_unreachable("unknown BuildIdKind");
+  }
 }
 
 template <class ELFT>
@@ -1652,31 +1662,6 @@ template class elf::BuildIdSection<ELF32
 template class elf::BuildIdSection<ELF64LE>;
 template class elf::BuildIdSection<ELF64BE>;
 
-template class elf::BuildIdFastHash<ELF32LE>;
-template class elf::BuildIdFastHash<ELF32BE>;
-template class elf::BuildIdFastHash<ELF64LE>;
-template class elf::BuildIdFastHash<ELF64BE>;
-
-template class elf::BuildIdMd5<ELF32LE>;
-template class elf::BuildIdMd5<ELF32BE>;
-template class elf::BuildIdMd5<ELF64LE>;
-template class elf::BuildIdMd5<ELF64BE>;
-
-template class elf::BuildIdSha1<ELF32LE>;
-template class elf::BuildIdSha1<ELF32BE>;
-template class elf::BuildIdSha1<ELF64LE>;
-template class elf::BuildIdSha1<ELF64BE>;
-
-template class elf::BuildIdUuid<ELF32LE>;
-template class elf::BuildIdUuid<ELF32BE>;
-template class elf::BuildIdUuid<ELF64LE>;
-template class elf::BuildIdUuid<ELF64BE>;
-
-template class elf::BuildIdHexstring<ELF32LE>;
-template class elf::BuildIdHexstring<ELF32BE>;
-template class elf::BuildIdHexstring<ELF64LE>;
-template class elf::BuildIdHexstring<ELF64BE>;
-
 template class elf::GotSection<ELF32LE>;
 template class elf::GotSection<ELF32BE>;
 template class elf::GotSection<ELF64LE>;

Modified: lld/trunk/ELF/SyntheticSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.h?rev=287603&r1=287602&r2=287603&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.h (original)
+++ lld/trunk/ELF/SyntheticSections.h Mon Nov 21 18:54:15 2016
@@ -89,54 +89,22 @@ protected:
 // .note.gnu.build-id section.
 template <class ELFT> class BuildIdSection : public InputSection<ELFT> {
 public:
-  virtual void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) = 0;
-  virtual ~BuildIdSection() = default;
+  BuildIdSection();
+  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf);
 
-  uint8_t *getOutputLoc(uint8_t *Start) const;
+private:
+  // First 16 bytes are a header.
+  static const unsigned HeaderSize = 16;
 
-protected:
-  BuildIdSection(size_t HashSize);
-  std::vector<uint8_t> Buf;
+  size_t getHashSize();
+  uint8_t *getOutputLoc(uint8_t *Start);
 
   void
   computeHash(llvm::MutableArrayRef<uint8_t> Buf,
               std::function<void(ArrayRef<uint8_t> Arr, uint8_t *Hash)> Hash);
 
+  std::vector<uint8_t> Buf;
   size_t HashSize;
-  // First 16 bytes are a header.
-  static const unsigned HeaderSize = 16;
-};
-
-template <class ELFT>
-class BuildIdFastHash final : public BuildIdSection<ELFT> {
-public:
-  BuildIdFastHash() : BuildIdSection<ELFT>(8) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
-public:
-  BuildIdMd5() : BuildIdSection<ELFT>(16) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
-public:
-  BuildIdSha1() : BuildIdSection<ELFT>(20) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
-public:
-  BuildIdUuid() : BuildIdSection<ELFT>(16) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT>
-class BuildIdHexstring final : public BuildIdSection<ELFT> {
-public:
-  BuildIdHexstring();
-  void writeBuildId(llvm::MutableArrayRef<uint8_t>) override;
 };
 
 template <class ELFT> class GotSection final : public SyntheticSection<ELFT> {

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=287603&r1=287602&r2=287603&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Nov 21 18:54:15 2016
@@ -294,21 +294,10 @@ template <class ELFT> void Writer<ELFT>:
   if (!Config->Relocatable)
     Symtab<ELFT>::X->Sections.push_back(createCommentSection<ELFT>());
 
-  if (Config->BuildId == BuildIdKind::Fast)
-    In<ELFT>::BuildId = make<BuildIdFastHash<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Md5)
-    In<ELFT>::BuildId = make<BuildIdMd5<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Sha1)
-    In<ELFT>::BuildId = make<BuildIdSha1<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Uuid)
-    In<ELFT>::BuildId = make<BuildIdUuid<ELFT>>();
-  else if (Config->BuildId == BuildIdKind::Hexstring)
-    In<ELFT>::BuildId = make<BuildIdHexstring<ELFT>>();
-  else
-    In<ELFT>::BuildId = nullptr;
-
-  if (In<ELFT>::BuildId)
+  if (Config->BuildId != BuildIdKind::None) {
+    In<ELFT>::BuildId = make<BuildIdSection<ELFT>>();
     Symtab<ELFT>::X->Sections.push_back(In<ELFT>::BuildId);
+  }
 
   InputSection<ELFT> *Common = createCommonSection<ELFT>();
   if (!Common->Data.empty()) {




More information about the llvm-commits mailing list