Add output filename to UUID hash
authorLeonard Grey <lgrey@chromium.org>
Thu, 31 Mar 2022 18:49:12 +0000 (14:49 -0400)
committerLeonard Grey <lgrey@chromium.org>
Thu, 31 Mar 2022 22:50:05 +0000 (18:50 -0400)
Differential Revision: https://reviews.llvm.org/D122843

lld/MachO/Writer.cpp
lld/test/MachO/uuid.s

index 957042a769506a69e53d29db7963ccbfc1f15ec3..fdb4cafd5cb95fa55aaa73af2c2dd406942b326a 100644 (file)
@@ -1075,7 +1075,8 @@ void Writer::writeUuid() {
   // Round-up integer division
   size_t chunkSize = (data.size() + chunkCount - 1) / chunkCount;
   std::vector<ArrayRef<uint8_t>> chunks = split(data, chunkSize);
-  std::vector<uint64_t> hashes(chunks.size());
+  // Leave one slot for filename
+  std::vector<uint64_t> hashes(chunks.size() + 1);
   SmallVector<std::shared_future<void>> threadFutures;
   threadFutures.reserve(chunks.size());
   for (size_t i = 0; i < chunks.size(); ++i)
@@ -1083,7 +1084,9 @@ void Writer::writeUuid() {
         [&](size_t j) { hashes[j] = xxHash64(chunks[j]); }, i));
   for (std::shared_future<void> &future : threadFutures)
     future.wait();
-
+  // Append the output filename so that identical binaries with different names
+  // don't get the same UUID.
+  hashes[chunks.size()] = xxHash64(sys::path::filename(config->finalOutput));
   uint64_t digest = xxHash64({reinterpret_cast<uint8_t *>(hashes.data()),
                               hashes.size() * sizeof(uint64_t)});
   uuidCommand->writeUuid(digest);
index b4486fe63ecfb7215ab3907411a62e082275df70..ff38f4bda524ca38bcf9fc793bc4e1714f53a762 100644 (file)
@@ -1,8 +1,22 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
-# RUN: %lld -lSystem %t.o -o %t
-# RUN: llvm-dwarfdump --uuid %t | FileCheck %s
-# CHECK: 4C4C44{{([[:xdigit:]]{2})}}-5555-{{([[:xdigit:]]{4})}}-A1{{([[:xdigit:]]{2})}}-{{([[:xdigit:]]{12})}} (x86_64)
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
+# RUN: %lld -lSystem %t/test.o -o %t/a
+# RUN: %lld -lSystem %t/test.o -o %t/b
+# RUN: llvm-dwarfdump --uuid %t/a | awk '{print $2}' > %t/uuida
+# RUN: llvm-dwarfdump --uuid %t/b | awk '{print $2}' > %t/uuidb
+# RUN: FileCheck %s < %t/uuida
+# RUN: FileCheck %s < %t/uuidb
+# RUN: not cmp %t/uuida %t/uuidb
+
+## Ensure -final_output is used for universal binaries, which may be linked with
+## temporary output file names
+# RUN: %lld -lSystem %t/test.o -o %t/c -final_output %t/a
+# RUN: llvm-dwarfdump --uuid %t/c | awk '{print $2}' > %t/uuidc
+# RUN: cmp %t/uuida %t/uuidc
+
+
+# CHECK: 4C4C44{{([[:xdigit:]]{2})}}-5555-{{([[:xdigit:]]{4})}}-A1{{([[:xdigit:]]{2})}}-{{([[:xdigit:]]{12})}}
 
 .globl _main
 _main: