blob: 701a318511b8fe1d13d5e79f66b9f5a7f2e56132 [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"
20#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000021#include "llvm/Support/Allocator.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000022#include "llvm/Support/BinaryByteStream.h"
23#include "llvm/Support/BinaryStreamArray.h"
24#include "llvm/Support/BinaryStreamReader.h"
25#include "llvm/Support/BinaryStreamWriter.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000026#include "llvm/Support/Endian.h"
27#include "llvm/Support/Error.h"
28#include <algorithm>
29#include <cstdint>
Zachary Turnerc6d54da2016-09-09 17:46:17 +000030
31using namespace llvm;
32using namespace llvm::msf;
33using namespace llvm::pdb;
34using namespace llvm::support;
35
Zachary Turnerde9ba152016-09-15 18:22:31 +000036TpiStreamBuilder::TpiStreamBuilder(MSFBuilder &Msf, uint32_t StreamIdx)
Reid Kleckner13fc4112017-04-04 00:56:34 +000037 : Msf(Msf), Allocator(Msf.getAllocator()), Header(nullptr), Idx(StreamIdx) {
Zachary Turnerde9ba152016-09-15 18:22:31 +000038}
Zachary Turnerc6d54da2016-09-09 17:46:17 +000039
Eugene Zelenko570e39a2016-11-23 23:16:32 +000040TpiStreamBuilder::~TpiStreamBuilder() = default;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000041
42void TpiStreamBuilder::setVersionHeader(PdbRaw_TpiVer Version) {
43 VerHeader = Version;
44}
45
Reid Kleckner13fc4112017-04-04 00:56:34 +000046void TpiStreamBuilder::addTypeRecord(ArrayRef<uint8_t> Record,
47 Optional<uint32_t> Hash) {
Reid Kleckner6e545ff2017-04-11 16:26:15 +000048 // If we just crossed an 8KB threshold, add a type index offset.
49 size_t NewSize = TypeRecordBytes + Record.size();
50 constexpr size_t EightKB = 8 * 1024;
51 if (NewSize / EightKB > TypeRecordBytes / EightKB || TypeRecords.empty()) {
52 TypeIndexOffsets.push_back(
53 {codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex +
54 TypeRecords.size()),
55 ulittle32_t(TypeRecordBytes)});
56 }
57 TypeRecordBytes = NewSize;
58
Zachary Turnerc6d54da2016-09-09 17:46:17 +000059 TypeRecords.push_back(Record);
Reid Kleckner13fc4112017-04-04 00:56:34 +000060 if (Hash)
61 TypeHashes.push_back(*Hash);
Zachary Turnerc6d54da2016-09-09 17:46:17 +000062}
63
64Error TpiStreamBuilder::finalize() {
65 if (Header)
66 return Error::success();
67
68 TpiStreamHeader *H = Allocator.Allocate<TpiStreamHeader>();
69
70 uint32_t Count = TypeRecords.size();
71
72 H->Version = *VerHeader;
73 H->HeaderSize = sizeof(TpiStreamHeader);
74 H->TypeIndexBegin = codeview::TypeIndex::FirstNonSimpleIndex;
75 H->TypeIndexEnd = H->TypeIndexBegin + Count;
Reid Kleckner13fc4112017-04-04 00:56:34 +000076 H->TypeRecordBytes = TypeRecordBytes;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000077
Zachary Turner620961d2016-09-14 23:00:02 +000078 H->HashStreamIndex = HashStreamIndex;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000079 H->HashAuxStreamIndex = kInvalidStreamIndex;
80 H->HashKeySize = sizeof(ulittle32_t);
81 H->NumHashBuckets = MinTpiHashBuckets;
82
Zachary Turner620961d2016-09-14 23:00:02 +000083 // Recall that hash values go into a completely different stream identified by
84 // the `HashStreamIndex` field of the `TpiStreamHeader`. Therefore, the data
85 // begins at offset 0 of this independent stream.
86 H->HashValueBuffer.Off = 0;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000087 H->HashValueBuffer.Length = calculateHashBufferSize();
88
89 // We never write any adjustments into our PDBs, so this is usually some
90 // offset with zero length.
Zachary Turner620961d2016-09-14 23:00:02 +000091 H->HashAdjBuffer.Off = H->HashValueBuffer.Off + H->HashValueBuffer.Length;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000092 H->HashAdjBuffer.Length = 0;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000093
Zachary Turner620961d2016-09-14 23:00:02 +000094 H->IndexOffsetBuffer.Off = H->HashAdjBuffer.Off + H->HashAdjBuffer.Length;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000095 H->IndexOffsetBuffer.Length = calculateIndexOffsetSize();
Zachary Turnerc6d54da2016-09-09 17:46:17 +000096
97 Header = H;
98 return Error::success();
99}
100
Zachary Turner120faca2017-02-27 22:11:43 +0000101uint32_t TpiStreamBuilder::calculateSerializedLength() {
Reid Kleckner13fc4112017-04-04 00:56:34 +0000102 return sizeof(TpiStreamHeader) + TypeRecordBytes;
Zachary Turner620961d2016-09-14 23:00:02 +0000103}
104
105uint32_t TpiStreamBuilder::calculateHashBufferSize() const {
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000106 assert((TypeRecords.size() == TypeHashes.size() || TypeHashes.empty()) &&
Reid Kleckner13fc4112017-04-04 00:56:34 +0000107 "either all or no type records should have hashes");
108 return TypeHashes.size() * sizeof(ulittle32_t);
Zachary Turner620961d2016-09-14 23:00:02 +0000109}
110
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000111uint32_t TpiStreamBuilder::calculateIndexOffsetSize() const {
Zachary Turnerdd3a7392017-05-12 19:18:12 +0000112 return TypeIndexOffsets.size() * sizeof(codeview::TypeIndexOffset);
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000113}
114
Zachary Turner620961d2016-09-14 23:00:02 +0000115Error TpiStreamBuilder::finalizeMsfLayout() {
116 uint32_t Length = calculateSerializedLength();
Zachary Turnerde9ba152016-09-15 18:22:31 +0000117 if (auto EC = Msf.setStreamSize(Idx, Length))
Zachary Turner620961d2016-09-14 23:00:02 +0000118 return EC;
119
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000120 uint32_t HashStreamSize =
121 calculateHashBufferSize() + calculateIndexOffsetSize();
Zachary Turner620961d2016-09-14 23:00:02 +0000122
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000123 if (HashStreamSize == 0)
Zachary Turner620961d2016-09-14 23:00:02 +0000124 return Error::success();
125
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000126 auto ExpectedIndex = Msf.addStream(HashStreamSize);
Zachary Turner620961d2016-09-14 23:00:02 +0000127 if (!ExpectedIndex)
128 return ExpectedIndex.takeError();
129 HashStreamIndex = *ExpectedIndex;
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000130 if (!TypeHashes.empty()) {
131 ulittle32_t *H = Allocator.Allocate<ulittle32_t>(TypeHashes.size());
132 MutableArrayRef<ulittle32_t> HashBuffer(H, TypeHashes.size());
133 for (uint32_t I = 0; I < TypeHashes.size(); ++I) {
134 HashBuffer[I] = TypeHashes[I] % MinTpiHashBuckets;
135 }
136 ArrayRef<uint8_t> Bytes(
137 reinterpret_cast<const uint8_t *>(HashBuffer.data()),
138 calculateHashBufferSize());
139 HashValueStream =
140 llvm::make_unique<BinaryByteStream>(Bytes, llvm::support::little);
Zachary Turner620961d2016-09-14 23:00:02 +0000141 }
Zachary Turner620961d2016-09-14 23:00:02 +0000142 return Error::success();
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000143}
144
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000145Error TpiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000146 WritableBinaryStreamRef Buffer) {
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000147 if (auto EC = finalize())
148 return EC;
149
150 auto InfoS =
Zachary Turnerde9ba152016-09-15 18:22:31 +0000151 WritableMappedBlockStream::createIndexedStream(Layout, Buffer, Idx);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000152
Zachary Turner120faca2017-02-27 22:11:43 +0000153 BinaryStreamWriter Writer(*InfoS);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000154 if (auto EC = Writer.writeObject(*Header))
155 return EC;
156
Reid Kleckner13fc4112017-04-04 00:56:34 +0000157 for (auto Rec : TypeRecords)
158 if (auto EC = Writer.writeBytes(Rec))
159 return EC;
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000160
Zachary Turner620961d2016-09-14 23:00:02 +0000161 if (HashStreamIndex != kInvalidStreamIndex) {
162 auto HVS = WritableMappedBlockStream::createIndexedStream(Layout, Buffer,
163 HashStreamIndex);
Zachary Turner120faca2017-02-27 22:11:43 +0000164 BinaryStreamWriter HW(*HVS);
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000165 if (HashValueStream) {
166 if (auto EC = HW.writeStreamRef(*HashValueStream))
167 return EC;
168 }
169
170 for (auto &IndexOffset : TypeIndexOffsets) {
171 if (auto EC = HW.writeObject(IndexOffset))
172 return EC;
173 }
Zachary Turner620961d2016-09-14 23:00:02 +0000174 }
175
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000176 return Error::success();
177}