1 //===- DebugChecksumsSubsection.cpp ----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" 11 12 #include "llvm/DebugInfo/CodeView/CodeViewError.h" 13 #include "llvm/DebugInfo/CodeView/StringTable.h" 14 #include "llvm/Support/BinaryStreamReader.h" 15 16 using namespace llvm; 17 using namespace llvm::codeview; 18 19 struct FileChecksumEntryHeader { 20 using ulittle32_t = support::ulittle32_t; 21 22 ulittle32_t FileNameOffset; // Byte offset of filename in global string table. 23 uint8_t ChecksumSize; // Number of bytes of checksum. 24 uint8_t ChecksumKind; // FileChecksumKind 25 // Checksum bytes follow. 26 }; 27 28 Error llvm::VarStreamArrayExtractor<FileChecksumEntry>::extract( 29 BinaryStreamRef Stream, uint32_t &Len, FileChecksumEntry &Item) { 30 BinaryStreamReader Reader(Stream); 31 32 const FileChecksumEntryHeader *Header; 33 if (auto EC = Reader.readObject(Header)) 34 return EC; 35 36 Item.FileNameOffset = Header->FileNameOffset; 37 Item.Kind = static_cast<FileChecksumKind>(Header->ChecksumKind); 38 if (auto EC = Reader.readBytes(Item.Checksum, Header->ChecksumSize)) 39 return EC; 40 41 Len = alignTo(Header->ChecksumSize + sizeof(FileChecksumEntryHeader), 4); 42 return Error::success(); 43 } 44 45 Error DebugChecksumsSubsectionRef::initialize(BinaryStreamReader Reader) { 46 if (auto EC = Reader.readArray(Checksums, Reader.bytesRemaining())) 47 return EC; 48 49 return Error::success(); 50 } 51 52 DebugChecksumsSubsection::DebugChecksumsSubsection(StringTable &Strings) 53 : DebugSubsection(DebugSubsectionKind::FileChecksums), Strings(Strings) {} 54 55 void DebugChecksumsSubsection::addChecksum(StringRef FileName, 56 FileChecksumKind Kind, 57 ArrayRef<uint8_t> Bytes) { 58 FileChecksumEntry Entry; 59 if (!Bytes.empty()) { 60 uint8_t *Copy = Storage.Allocate<uint8_t>(Bytes.size()); 61 ::memcpy(Copy, Bytes.data(), Bytes.size()); 62 Entry.Checksum = makeArrayRef(Copy, Bytes.size()); 63 } 64 65 Entry.FileNameOffset = Strings.insert(FileName); 66 Entry.Kind = Kind; 67 Checksums.push_back(Entry); 68 69 // This maps the offset of this string in the string table to the offset 70 // of this checksum entry in the checksum buffer. 71 OffsetMap[Entry.FileNameOffset] = SerializedSize; 72 assert(SerializedSize % 4 == 0); 73 74 uint32_t Len = alignTo(sizeof(FileChecksumEntryHeader) + Bytes.size(), 4); 75 SerializedSize += Len; 76 } 77 78 uint32_t DebugChecksumsSubsection::calculateSerializedLength() { 79 return SerializedSize; 80 } 81 82 Error DebugChecksumsSubsection::commit(BinaryStreamWriter &Writer) { 83 for (const auto &FC : Checksums) { 84 FileChecksumEntryHeader Header; 85 Header.ChecksumKind = uint8_t(FC.Kind); 86 Header.ChecksumSize = FC.Checksum.size(); 87 Header.FileNameOffset = FC.FileNameOffset; 88 if (auto EC = Writer.writeObject(Header)) 89 return EC; 90 if (auto EC = Writer.writeArray(makeArrayRef(FC.Checksum))) 91 return EC; 92 if (auto EC = Writer.padToAlignment(4)) 93 return EC; 94 } 95 return Error::success(); 96 } 97 98 uint32_t DebugChecksumsSubsection::mapChecksumOffset(StringRef FileName) const { 99 uint32_t Offset = Strings.getStringId(FileName); 100 auto Iter = OffsetMap.find(Offset); 101 assert(Iter != OffsetMap.end()); 102 return Iter->second; 103 } 104