Skip to content

Commit

Permalink
[lld/mac] Extract a sha256() function
Browse files Browse the repository at this point in the history
No behavior change.

Differential Revision: https://reviews.llvm.org/D128289
  • Loading branch information
nico committed Jun 21, 2022
1 parent b19194c commit ca25bae
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lld/MachO/SyntheticSections.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit ca25bae

Please sign in to comment.