blob: 9e19b4dab1470543e10f7ae3a5742f9cc7882e23 [file] [log] [blame]
Zachary Turner8c099fe2017-05-30 16:36:15 +00001//===- DebugChecksumsSubsection.cpp ----------------------*- C++ -*-===//
Zachary Turnerc37cb0c2017-04-27 16:12:16 +00002//
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
Zachary Turner8c099fe2017-05-30 16:36:15 +000010#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000011
12#include "llvm/DebugInfo/CodeView/CodeViewError.h"
Zachary Turnercf468d82017-05-03 17:11:40 +000013#include "llvm/DebugInfo/CodeView/StringTable.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000014#include "llvm/Support/BinaryStreamReader.h"
15
16using namespace llvm;
17using namespace llvm::codeview;
18
19struct 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
28Error llvm::VarStreamArrayExtractor<FileChecksumEntry>::extract(
Zachary Turner59e83892017-05-03 05:34:00 +000029 BinaryStreamRef Stream, uint32_t &Len, FileChecksumEntry &Item) {
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000030 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
Zachary Turner8c099fe2017-05-30 16:36:15 +000045Error DebugChecksumsSubsectionRef::initialize(BinaryStreamReader Reader) {
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000046 if (auto EC = Reader.readArray(Checksums, Reader.bytesRemaining()))
47 return EC;
48
49 return Error::success();
50}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000051
Zachary Turner8c099fe2017-05-30 16:36:15 +000052DebugChecksumsSubsection::DebugChecksumsSubsection(StringTable &Strings)
53 : DebugSubsection(DebugSubsectionKind::FileChecksums), Strings(Strings) {}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000054
Zachary Turner8c099fe2017-05-30 16:36:15 +000055void DebugChecksumsSubsection::addChecksum(StringRef FileName,
56 FileChecksumKind Kind,
57 ArrayRef<uint8_t> Bytes) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000058 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 }
Zachary Turnercf468d82017-05-03 17:11:40 +000064
65 Entry.FileNameOffset = Strings.insert(FileName);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000066 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.
Zachary Turnercf468d82017-05-03 17:11:40 +000071 OffsetMap[Entry.FileNameOffset] = SerializedSize;
Zachary Turneredef1452017-05-02 16:56:09 +000072 assert(SerializedSize % 4 == 0);
73
74 uint32_t Len = alignTo(sizeof(FileChecksumEntryHeader) + Bytes.size(), 4);
75 SerializedSize += Len;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000076}
77
Zachary Turner8c099fe2017-05-30 16:36:15 +000078uint32_t DebugChecksumsSubsection::calculateSerializedLength() {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000079 return SerializedSize;
80}
81
Zachary Turner8c099fe2017-05-30 16:36:15 +000082Error DebugChecksumsSubsection::commit(BinaryStreamWriter &Writer) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000083 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;
Zachary Turneredef1452017-05-02 16:56:09 +000092 if (auto EC = Writer.padToAlignment(4))
93 return EC;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000094 }
95 return Error::success();
96}
97
Zachary Turner8c099fe2017-05-30 16:36:15 +000098uint32_t DebugChecksumsSubsection::mapChecksumOffset(StringRef FileName) const {
Zachary Turnercf468d82017-05-03 17:11:40 +000099 uint32_t Offset = Strings.getStringId(FileName);
100 auto Iter = OffsetMap.find(Offset);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000101 assert(Iter != OffsetMap.end());
102 return Iter->second;
103}