[ELF] Do not omit debug sections when computing build-id
authorPetr Hosek <phosek@chromium.org>
Thu, 1 Sep 2016 22:43:03 +0000 (22:43 +0000)
committerPetr Hosek <phosek@chromium.org>
Thu, 1 Sep 2016 22:43:03 +0000 (22:43 +0000)
The primary use of build-id is in debugging, hence omitting debug
sections when computing it significantly reduces its usability as
changes in debug section content wouldn't alter the build-id.

Differential Revision: https://reviews.llvm.org/D24120

llvm-svn: 280421

lld/ELF/OutputSections.cpp
lld/ELF/OutputSections.h
lld/ELF/Writer.cpp

index 9874708..244b5a1 100644 (file)
@@ -1657,40 +1657,36 @@ template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
 }
 
 template <class ELFT>
-void BuildIdFnv1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
+void BuildIdFnv1<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
   const endianness E = ELFT::TargetEndianness;
 
   // 64-bit FNV-1 hash
   uint64_t Hash = 0xcbf29ce484222325;
-  for (ArrayRef<uint8_t> Buf : Bufs) {
-    for (uint8_t B : Buf) {
-      Hash *= 0x100000001b3;
-      Hash ^= B;
-    }
+  for (uint8_t B : Buf) {
+    Hash *= 0x100000001b3;
+    Hash ^= B;
   }
   write64<E>(this->HashBuf, Hash);
 }
 
 template <class ELFT>
-void BuildIdMd5<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
+void BuildIdMd5<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
   MD5 Hash;
-  for (ArrayRef<uint8_t> Buf : Bufs)
-    Hash.update(Buf);
+  Hash.update(Buf);
   MD5::MD5Result Res;
   Hash.final(Res);
   memcpy(this->HashBuf, Res, 16);
 }
 
 template <class ELFT>
-void BuildIdSha1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
+void BuildIdSha1<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
   SHA1 Hash;
-  for (ArrayRef<uint8_t> Buf : Bufs)
-    Hash.update(Buf);
+  Hash.update(Buf);
   memcpy(this->HashBuf, Hash.final().data(), 20);
 }
 
 template <class ELFT>
-void BuildIdUuid<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
+void BuildIdUuid<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
   if (getRandomBytes(this->HashBuf, 16))
     error("entropy source failure");
 }
@@ -1700,7 +1696,7 @@ BuildIdHexstring<ELFT>::BuildIdHexstring()
     : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {}
 
 template <class ELFT>
-void BuildIdHexstring<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) {
+void BuildIdHexstring<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
   memcpy(this->HashBuf, Config->BuildIdVector.data(),
          Config->BuildIdVector.size());
 }
index 2619e98..a5833a6 100644 (file)
@@ -700,7 +700,7 @@ template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
 
 public:
   void writeTo(uint8_t *Buf) override;
-  virtual void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) = 0;
+  virtual void writeBuildId(ArrayRef<uint8_t> Buf) = 0;
   typename Base::Kind getKind() const override { return Base::BuildId; }
   static bool classof(const Base *B) { return B->getKind() == Base::BuildId; }
 
@@ -713,32 +713,32 @@ protected:
 template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
 public:
   BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
-  void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
+  void writeBuildId(ArrayRef<uint8_t> Buf) override;
 };
 
 template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
 public:
   BuildIdMd5() : BuildIdSection<ELFT>(16) {}
-  void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
+  void writeBuildId(ArrayRef<uint8_t> Buf) override;
 };
 
 template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
 public:
   BuildIdSha1() : BuildIdSection<ELFT>(20) {}
-  void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
+  void writeBuildId(ArrayRef<uint8_t> Buf) override;
 };
 
 template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
 public:
   BuildIdUuid() : BuildIdSection<ELFT>(16) {}
-  void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
+  void writeBuildId(ArrayRef<uint8_t> Buf) override;
 };
 
 template <class ELFT>
 class BuildIdHexstring final : public BuildIdSection<ELFT> {
 public:
   BuildIdHexstring();
-  void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
+  void writeBuildId(ArrayRef<uint8_t>) override;
 };
 
 // All output sections that are hadnled by the linker specially are
index 3475de8..396a2d8 100644 (file)
@@ -1320,21 +1320,10 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
   if (!Out<ELFT>::BuildId)
     return;
 
-  // Compute a hash of all sections except .debug_* sections.
-  // We skip debug sections because they tend to be very large
-  // and their contents are very likely to be the same as long as
-  // other sections are the same.
+  // Compute a hash of all sections of the output file.
   uint8_t *Start = Buffer->getBufferStart();
-  uint8_t *Last = Start;
-  std::vector<ArrayRef<uint8_t>> Regions;
-  for (OutputSectionBase<ELFT> *Sec : OutputSections) {
-    uint8_t *End = Start + Sec->getFileOff();
-    if (!Sec->getName().startswith(".debug_"))
-      Regions.push_back({Last, End});
-    Last = End;
-  }
-  Regions.push_back({Last, Start + FileSize});
-  Out<ELFT>::BuildId->writeBuildId(Regions);
+  uint8_t *End = Start + FileSize;
+  Out<ELFT>::BuildId->writeBuildId({Start, End});
 }
 
 template void elf::writeResult<ELF32LE>();