xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/DebugChecksumsSubsection.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- DebugChecksumsSubsection.cpp ---------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
130b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
140b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
150b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
160b57cec5SDimitry Andric #include "llvm/Support/Error.h"
170b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
180b57cec5SDimitry Andric #include <cassert>
190b57cec5SDimitry Andric #include <cstdint>
200b57cec5SDimitry Andric #include <cstring>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric using namespace llvm::codeview;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric struct FileChecksumEntryHeader {
260b57cec5SDimitry Andric   using ulittle32_t = support::ulittle32_t;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric   ulittle32_t FileNameOffset; // Byte offset of filename in global string table.
290b57cec5SDimitry Andric   uint8_t ChecksumSize;       // Number of bytes of checksum.
300b57cec5SDimitry Andric   uint8_t ChecksumKind;       // FileChecksumKind
310b57cec5SDimitry Andric                               // Checksum bytes follow.
320b57cec5SDimitry Andric };
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric Error VarStreamArrayExtractor<FileChecksumEntry>::
operator ()(BinaryStreamRef Stream,uint32_t & Len,FileChecksumEntry & Item)350b57cec5SDimitry Andric operator()(BinaryStreamRef Stream, uint32_t &Len, FileChecksumEntry &Item) {
360b57cec5SDimitry Andric   BinaryStreamReader Reader(Stream);
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   const FileChecksumEntryHeader *Header;
390b57cec5SDimitry Andric   if (auto EC = Reader.readObject(Header))
400b57cec5SDimitry Andric     return EC;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   Item.FileNameOffset = Header->FileNameOffset;
430b57cec5SDimitry Andric   Item.Kind = static_cast<FileChecksumKind>(Header->ChecksumKind);
440b57cec5SDimitry Andric   if (auto EC = Reader.readBytes(Item.Checksum, Header->ChecksumSize))
450b57cec5SDimitry Andric     return EC;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   Len = alignTo(Header->ChecksumSize + sizeof(FileChecksumEntryHeader), 4);
480b57cec5SDimitry Andric   return Error::success();
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
initialize(BinaryStreamReader Reader)510b57cec5SDimitry Andric Error DebugChecksumsSubsectionRef::initialize(BinaryStreamReader Reader) {
520b57cec5SDimitry Andric   if (auto EC = Reader.readArray(Checksums, Reader.bytesRemaining()))
530b57cec5SDimitry Andric     return EC;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   return Error::success();
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
initialize(BinaryStreamRef Section)580b57cec5SDimitry Andric Error DebugChecksumsSubsectionRef::initialize(BinaryStreamRef Section) {
590b57cec5SDimitry Andric   BinaryStreamReader Reader(Section);
600b57cec5SDimitry Andric   return initialize(Reader);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
DebugChecksumsSubsection(DebugStringTableSubsection & Strings)630b57cec5SDimitry Andric DebugChecksumsSubsection::DebugChecksumsSubsection(
640b57cec5SDimitry Andric     DebugStringTableSubsection &Strings)
650b57cec5SDimitry Andric     : DebugSubsection(DebugSubsectionKind::FileChecksums), Strings(Strings) {}
660b57cec5SDimitry Andric 
addChecksum(StringRef FileName,FileChecksumKind Kind,ArrayRef<uint8_t> Bytes)670b57cec5SDimitry Andric void DebugChecksumsSubsection::addChecksum(StringRef FileName,
680b57cec5SDimitry Andric                                            FileChecksumKind Kind,
690b57cec5SDimitry Andric                                            ArrayRef<uint8_t> Bytes) {
700b57cec5SDimitry Andric   FileChecksumEntry Entry;
710b57cec5SDimitry Andric   if (!Bytes.empty()) {
720b57cec5SDimitry Andric     uint8_t *Copy = Storage.Allocate<uint8_t>(Bytes.size());
730b57cec5SDimitry Andric     ::memcpy(Copy, Bytes.data(), Bytes.size());
74*bdd1243dSDimitry Andric     Entry.Checksum = ArrayRef(Copy, Bytes.size());
750b57cec5SDimitry Andric   }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   Entry.FileNameOffset = Strings.insert(FileName);
780b57cec5SDimitry Andric   Entry.Kind = Kind;
790b57cec5SDimitry Andric   Checksums.push_back(Entry);
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   // This maps the offset of this string in the string table to the offset
820b57cec5SDimitry Andric   // of this checksum entry in the checksum buffer.
830b57cec5SDimitry Andric   OffsetMap[Entry.FileNameOffset] = SerializedSize;
840b57cec5SDimitry Andric   assert(SerializedSize % 4 == 0);
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   uint32_t Len = alignTo(sizeof(FileChecksumEntryHeader) + Bytes.size(), 4);
870b57cec5SDimitry Andric   SerializedSize += Len;
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
calculateSerializedSize() const900b57cec5SDimitry Andric uint32_t DebugChecksumsSubsection::calculateSerializedSize() const {
910b57cec5SDimitry Andric   return SerializedSize;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
commit(BinaryStreamWriter & Writer) const940b57cec5SDimitry Andric Error DebugChecksumsSubsection::commit(BinaryStreamWriter &Writer) const {
950b57cec5SDimitry Andric   for (const auto &FC : Checksums) {
960b57cec5SDimitry Andric     FileChecksumEntryHeader Header;
970b57cec5SDimitry Andric     Header.ChecksumKind = uint8_t(FC.Kind);
980b57cec5SDimitry Andric     Header.ChecksumSize = FC.Checksum.size();
990b57cec5SDimitry Andric     Header.FileNameOffset = FC.FileNameOffset;
1000b57cec5SDimitry Andric     if (auto EC = Writer.writeObject(Header))
1010b57cec5SDimitry Andric       return EC;
102*bdd1243dSDimitry Andric     if (auto EC = Writer.writeArray(ArrayRef(FC.Checksum)))
1030b57cec5SDimitry Andric       return EC;
1040b57cec5SDimitry Andric     if (auto EC = Writer.padToAlignment(4))
1050b57cec5SDimitry Andric       return EC;
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric   return Error::success();
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
mapChecksumOffset(StringRef FileName) const1100b57cec5SDimitry Andric uint32_t DebugChecksumsSubsection::mapChecksumOffset(StringRef FileName) const {
1110b57cec5SDimitry Andric   uint32_t Offset = Strings.getIdForString(FileName);
1120b57cec5SDimitry Andric   auto Iter = OffsetMap.find(Offset);
1130b57cec5SDimitry Andric   assert(Iter != OffsetMap.end());
1140b57cec5SDimitry Andric   return Iter->second;
1150b57cec5SDimitry Andric }
116