[lld] ca25bae - [lld/mac] Extract a sha256() function

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 21 11:03:04 PDT 2022


Author: Nico Weber
Date: 2022-06-21T14:02:42-04:00
New Revision: ca25baee7ecc351e9e9f82828f7316126ac490ec

URL: https://github.com/llvm/llvm-project/commit/ca25baee7ecc351e9e9f82828f7316126ac490ec
DIFF: https://github.com/llvm/llvm-project/commit/ca25baee7ecc351e9e9f82828f7316126ac490ec.diff

LOG: [lld/mac] Extract a sha256() function

No behavior change.

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

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index db0b1e794488d..6de4c308a4af8 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -43,6 +43,14 @@ using namespace llvm::support::endian;
 using namespace lld;
 using namespace lld::macho;
 
+// Reads `len` bytes at data and writes the 32-byte SHA256 checksum to `output`.
+static void sha256(const uint8_t *data, size_t len, uint8_t *output) {
+  ArrayRef<uint8_t> block(data, len);
+  std::array<uint8_t, 32> hash = SHA256::hash(block);
+  assert(hash.size() == CodeSignatureSection::hashSize);
+  memcpy(output, hash.data(), hash.size());
+}
+
 InStruct macho::in;
 std::vector<SyntheticSection *> macho::syntheticSections;
 
@@ -1239,13 +1247,8 @@ void CodeSignatureSection::writeHashes(uint8_t *buf) const {
   uint8_t *codeEnd = buf + fileOff;
   uint8_t *hashes = codeEnd + allHeadersSize;
   while (code < codeEnd) {
-    StringRef block(reinterpret_cast<char *>(code),
-                    std::min(codeEnd - code, static_cast<ssize_t>(blockSize)));
-    SHA256 hasher;
-    hasher.update(block);
-    std::array<uint8_t, 32> hash = hasher.final();
-    assert(hash.size() == hashSize);
-    memcpy(hashes, hash.data(), hashSize);
+    sha256(code, std::min(static_cast<size_t>(codeEnd - code), blockSize),
+           hashes);
     code += blockSize;
     hashes += hashSize;
   }


        


More information about the llvm-commits mailing list