blob: 8dd30018028e7a29271fbb407aabee0ed3149f91 [file] [log] [blame]
Eugene Zelenko570e39a2016-11-23 23:16:32 +00001//===- TpiStreamBuilder.cpp - -------------------------------------------===//
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//===----------------------------------------------------------------------===//
Zachary Turnerc6d54da2016-09-09 17:46:17 +00009
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/STLExtras.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000013#include "llvm/DebugInfo/CodeView/TypeIndex.h"
14#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Zachary Turner620961d2016-09-14 23:00:02 +000015#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000016#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000017#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
18#include "llvm/DebugInfo/PDB/Native/RawError.h"
19#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000020#include "llvm/Support/Allocator.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000021#include "llvm/Support/BinaryByteStream.h"
22#include "llvm/Support/BinaryStreamArray.h"
23#include "llvm/Support/BinaryStreamReader.h"
24#include "llvm/Support/BinaryStreamWriter.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/Error.h"
27#include <algorithm>
28#include <cstdint>
Zachary Turnerc6d54da2016-09-09 17:46:17 +000029
30using namespace llvm;
31using namespace llvm::msf;
32using namespace llvm::pdb;
33using namespace llvm::support;
34
Zachary Turnerde9ba152016-09-15 18:22:31 +000035TpiStreamBuilder::TpiStreamBuilder(MSFBuilder &Msf, uint32_t StreamIdx)
Reid Kleckner13fc4112017-04-04 00:56:34 +000036 : Msf(Msf), Allocator(Msf.getAllocator()), Header(nullptr), Idx(StreamIdx) {
Zachary Turnerde9ba152016-09-15 18:22:31 +000037}
Zachary Turnerc6d54da2016-09-09 17:46:17 +000038
Eugene Zelenko570e39a2016-11-23 23:16:32 +000039TpiStreamBuilder::~TpiStreamBuilder() = default;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000040
41void TpiStreamBuilder::setVersionHeader(PdbRaw_TpiVer Version) {
42 VerHeader = Version;
43}
44
Reid Kleckner13fc4112017-04-04 00:56:34 +000045void TpiStreamBuilder::addTypeRecord(ArrayRef<uint8_t> Record,
46 Optional<uint32_t> Hash) {
Reid Kleckner6e545ff2017-04-11 16:26:15 +000047 // If we just crossed an 8KB threshold, add a type index offset.
48 size_t NewSize = TypeRecordBytes + Record.size();
49 constexpr size_t EightKB = 8 * 1024;
50 if (NewSize / EightKB > TypeRecordBytes / EightKB || TypeRecords.empty()) {
51 TypeIndexOffsets.push_back(
52 {codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex +
53 TypeRecords.size()),
54 ulittle32_t(TypeRecordBytes)});
55 }
56 TypeRecordBytes = NewSize;
57
Zachary Turnerc6d54da2016-09-09 17:46:17 +000058 TypeRecords.push_back(Record);
Reid Kleckner13fc4112017-04-04 00:56:34 +000059 if (Hash)
60 TypeHashes.push_back(*Hash);
Zachary Turnerc6d54da2016-09-09 17:46:17 +000061}
62
63Error TpiStreamBuilder::finalize() {
64 if (Header)
65 return Error::success();
66
67 TpiStreamHeader *H = Allocator.Allocate<TpiStreamHeader>();
68
69 uint32_t Count = TypeRecords.size();
70
Zachary Turner8fb441a2017-05-18 23:03:41 +000071 H->Version = VerHeader;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000072 H->HeaderSize = sizeof(TpiStreamHeader);
73 H->TypeIndexBegin = codeview::TypeIndex::FirstNonSimpleIndex;
74 H->TypeIndexEnd = H->TypeIndexBegin + Count;
Reid Kleckner13fc4112017-04-04 00:56:34 +000075 H->TypeRecordBytes = TypeRecordBytes;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000076
Zachary Turner620961d2016-09-14 23:00:02 +000077 H->HashStreamIndex = HashStreamIndex;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000078 H->HashAuxStreamIndex = kInvalidStreamIndex;
79 H->HashKeySize = sizeof(ulittle32_t);
80 H->NumHashBuckets = MinTpiHashBuckets;
81
Zachary Turner620961d2016-09-14 23:00:02 +000082 // Recall that hash values go into a completely different stream identified by
83 // the `HashStreamIndex` field of the `TpiStreamHeader`. Therefore, the data
84 // begins at offset 0 of this independent stream.
85 H->HashValueBuffer.Off = 0;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000086 H->HashValueBuffer.Length = calculateHashBufferSize();
87
88 // We never write any adjustments into our PDBs, so this is usually some
89 // offset with zero length.
Zachary Turner620961d2016-09-14 23:00:02 +000090 H->HashAdjBuffer.Off = H->HashValueBuffer.Off + H->HashValueBuffer.Length;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000091 H->HashAdjBuffer.Length = 0;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000092
Zachary Turner620961d2016-09-14 23:00:02 +000093 H->IndexOffsetBuffer.Off = H->HashAdjBuffer.Off + H->HashAdjBuffer.Length;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000094 H->IndexOffsetBuffer.Length = calculateIndexOffsetSize();
Zachary Turnerc6d54da2016-09-09 17:46:17 +000095
96 Header = H;
97 return Error::success();
98}
99
Zachary Turner120faca2017-02-27 22:11:43 +0000100uint32_t TpiStreamBuilder::calculateSerializedLength() {
Reid Kleckner13fc4112017-04-04 00:56:34 +0000101 return sizeof(TpiStreamHeader) + TypeRecordBytes;
Zachary Turner620961d2016-09-14 23:00:02 +0000102}
103
104uint32_t TpiStreamBuilder::calculateHashBufferSize() const {
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000105 assert((TypeRecords.size() == TypeHashes.size() || TypeHashes.empty()) &&
Reid Kleckner13fc4112017-04-04 00:56:34 +0000106 "either all or no type records should have hashes");
107 return TypeHashes.size() * sizeof(ulittle32_t);
Zachary Turner620961d2016-09-14 23:00:02 +0000108}
109
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000110uint32_t TpiStreamBuilder::calculateIndexOffsetSize() const {
Zachary Turnerdd3a7392017-05-12 19:18:12 +0000111 return TypeIndexOffsets.size() * sizeof(codeview::TypeIndexOffset);
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000112}
113
Zachary Turner620961d2016-09-14 23:00:02 +0000114Error TpiStreamBuilder::finalizeMsfLayout() {
115 uint32_t Length = calculateSerializedLength();
Zachary Turnerde9ba152016-09-15 18:22:31 +0000116 if (auto EC = Msf.setStreamSize(Idx, Length))
Zachary Turner620961d2016-09-14 23:00:02 +0000117 return EC;
118
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000119 uint32_t HashStreamSize =
120 calculateHashBufferSize() + calculateIndexOffsetSize();
Zachary Turner620961d2016-09-14 23:00:02 +0000121
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000122 if (HashStreamSize == 0)
Zachary Turner620961d2016-09-14 23:00:02 +0000123 return Error::success();
124
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000125 auto ExpectedIndex = Msf.addStream(HashStreamSize);
Zachary Turner620961d2016-09-14 23:00:02 +0000126 if (!ExpectedIndex)
127 return ExpectedIndex.takeError();
128 HashStreamIndex = *ExpectedIndex;
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000129 if (!TypeHashes.empty()) {
130 ulittle32_t *H = Allocator.Allocate<ulittle32_t>(TypeHashes.size());
131 MutableArrayRef<ulittle32_t> HashBuffer(H, TypeHashes.size());
132 for (uint32_t I = 0; I < TypeHashes.size(); ++I) {
133 HashBuffer[I] = TypeHashes[I] % MinTpiHashBuckets;
134 }
135 ArrayRef<uint8_t> Bytes(
136 reinterpret_cast<const uint8_t *>(HashBuffer.data()),
137 calculateHashBufferSize());
138 HashValueStream =
139 llvm::make_unique<BinaryByteStream>(Bytes, llvm::support::little);
Zachary Turner620961d2016-09-14 23:00:02 +0000140 }
Zachary Turner620961d2016-09-14 23:00:02 +0000141 return Error::success();
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000142}
143
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000144Error TpiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000145 WritableBinaryStreamRef Buffer) {
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000146 if (auto EC = finalize())
147 return EC;
148
Zachary Turner5b74ff32017-06-03 00:33:35 +0000149 auto InfoS = WritableMappedBlockStream::createIndexedStream(Layout, Buffer,
150 Idx, Allocator);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000151
Zachary Turner120faca2017-02-27 22:11:43 +0000152 BinaryStreamWriter Writer(*InfoS);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000153 if (auto EC = Writer.writeObject(*Header))
154 return EC;
155
Reid Kleckner13fc4112017-04-04 00:56:34 +0000156 for (auto Rec : TypeRecords)
157 if (auto EC = Writer.writeBytes(Rec))
158 return EC;
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000159
Zachary Turner620961d2016-09-14 23:00:02 +0000160 if (HashStreamIndex != kInvalidStreamIndex) {
Zachary Turner5b74ff32017-06-03 00:33:35 +0000161 auto HVS = WritableMappedBlockStream::createIndexedStream(
162 Layout, Buffer, HashStreamIndex, Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000163 BinaryStreamWriter HW(*HVS);
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000164 if (HashValueStream) {
165 if (auto EC = HW.writeStreamRef(*HashValueStream))
166 return EC;
167 }
168
169 for (auto &IndexOffset : TypeIndexOffsets) {
170 if (auto EC = HW.writeObject(IndexOffset))
171 return EC;
172 }
Zachary Turner620961d2016-09-14 23:00:02 +0000173 }
174
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000175 return Error::success();
176}