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

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 21 10:51:51 PDT 2022


thakis created this revision.
thakis added a reviewer: lld-macho.
Herald added projects: lld-macho, All.
thakis requested review of this revision.

No behavior change.


https://reviews.llvm.org/D128289

Files:
  lld/MachO/SyntheticSections.cpp


Index: lld/MachO/SyntheticSections.cpp
===================================================================
--- lld/MachO/SyntheticSections.cpp
+++ lld/MachO/SyntheticSections.cpp
@@ -43,6 +43,14 @@
 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 @@
   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;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128289.438760.patch
Type: text/x-patch
Size: 1290 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220621/a71e251b/attachment.bin>


More information about the llvm-commits mailing list