Skip to content

Commit

Permalink
[ELF] --build-id: replace vector<uint8_t> with unique_ptr<uint8_t[]>.…
Browse files Browse the repository at this point in the history
… NFC

We can't use C++20 make_unique_for_overwrite yet.
  • Loading branch information
MaskRay committed Jan 26, 2022
1 parent 7438dbe commit 07bd467
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lld/ELF/Writer.cpp
Expand Up @@ -2902,15 +2902,16 @@ computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
llvm::ArrayRef<uint8_t> data,
std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);
std::vector<uint8_t> hashes(chunks.size() * hashBuf.size());
const size_t hashesSize = chunks.size() * hashBuf.size();
std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]);

// Compute hash values.
parallelForEachN(0, chunks.size(), [&](size_t i) {
hashFn(hashes.data() + i * hashBuf.size(), chunks[i]);
hashFn(hashes.get() + i * hashBuf.size(), chunks[i]);
});

// Write to the final output buffer.
hashFn(hashBuf.data(), hashes);
hashFn(hashBuf.data(), makeArrayRef(hashes.get(), hashesSize));
}

template <class ELFT> void Writer<ELFT>::writeBuildId() {
Expand All @@ -2925,34 +2926,35 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {

// Compute a hash of all sections of the output file.
size_t hashSize = mainPart->buildId->hashSize;
std::vector<uint8_t> buildId(hashSize);
llvm::ArrayRef<uint8_t> buf{Out::bufferStart, size_t(fileSize)};
std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]);
MutableArrayRef<uint8_t> output(buildId.get(), hashSize);
llvm::ArrayRef<uint8_t> input{Out::bufferStart, size_t(fileSize)};

switch (config->buildId) {
case BuildIdKind::Fast:
computeHash(buildId, buf, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
write64le(dest, xxHash64(arr));
});
break;
case BuildIdKind::Md5:
computeHash(buildId, buf, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
memcpy(dest, MD5::hash(arr).data(), hashSize);
});
break;
case BuildIdKind::Sha1:
computeHash(buildId, buf, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
memcpy(dest, SHA1::hash(arr).data(), hashSize);
});
break;
case BuildIdKind::Uuid:
if (auto ec = llvm::getRandomBytes(buildId.data(), hashSize))
if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize))
error("entropy source failure: " + ec.message());
break;
default:
llvm_unreachable("unknown BuildIdKind");
}
for (Partition &part : partitions)
part.buildId->writeBuildId(buildId);
part.buildId->writeBuildId(output);
}

template void elf::createSyntheticSections<ELF32LE>();
Expand Down

0 comments on commit 07bd467

Please sign in to comment.