blob: e0ceb7499ee53d8fac8cd87d27c5065fd804705b [file] [log] [blame]
Zachary Turnerdbeaea72016-07-11 21:45:26 +00001//===- PDBFileBuilder.cpp - PDB File Creation -------------------*- 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
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000011
Zachary Turnerfaa554b2016-07-15 22:16:56 +000012#include "llvm/ADT/BitVector.h"
13
Zachary Turnera3225b02016-07-29 20:56:36 +000014#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000015#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
16#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
Zachary Turner946204c2017-08-09 04:23:25 +000017#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000018#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
19#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
Zachary Turnere204a6c2017-05-02 18:00:13 +000020#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000021#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000022#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
23#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000024#include "llvm/Support/BinaryStream.h"
25#include "llvm/Support/BinaryStreamWriter.h"
Zachary Turnerf2282762018-03-23 19:57:25 +000026#include "llvm/Support/JamCRC.h"
27#include "llvm/Support/Path.h"
Nico Weber205ca682018-09-15 18:35:51 +000028#include "llvm/Support/xxhash.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000029
30using namespace llvm;
31using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000032using namespace llvm::msf;
Zachary Turnerdbeaea72016-07-11 21:45:26 +000033using namespace llvm::pdb;
Zachary Turnerfaa554b2016-07-15 22:16:56 +000034using namespace llvm::support;
Zachary Turnerdbeaea72016-07-11 21:45:26 +000035
Zachary Turnere109dc62016-07-22 19:56:26 +000036PDBFileBuilder::PDBFileBuilder(BumpPtrAllocator &Allocator)
Zachary Turnerf2282762018-03-23 19:57:25 +000037 : Allocator(Allocator), InjectedSourceHashTraits(Strings),
38 InjectedSourceTable(2, InjectedSourceHashTraits) {}
Zachary Turnerdbeaea72016-07-11 21:45:26 +000039
Zachary Turner7eaf1d92017-07-10 22:40:20 +000040PDBFileBuilder::~PDBFileBuilder() {}
41
Rui Ueyama5d6714e2016-09-30 20:52:12 +000042Error PDBFileBuilder::initialize(uint32_t BlockSize) {
43 auto ExpectedMsf = MSFBuilder::create(Allocator, BlockSize);
Zachary Turnerfaa554b2016-07-15 22:16:56 +000044 if (!ExpectedMsf)
45 return ExpectedMsf.takeError();
Rui Ueyama5d6714e2016-09-30 20:52:12 +000046 Msf = llvm::make_unique<MSFBuilder>(std::move(*ExpectedMsf));
Zachary Turnerdbeaea72016-07-11 21:45:26 +000047 return Error::success();
48}
49
Zachary Turnera3225b02016-07-29 20:56:36 +000050MSFBuilder &PDBFileBuilder::getMsfBuilder() { return *Msf; }
Zachary Turnerfaa554b2016-07-15 22:16:56 +000051
Zachary Turnerdbeaea72016-07-11 21:45:26 +000052InfoStreamBuilder &PDBFileBuilder::getInfoBuilder() {
53 if (!Info)
Zachary Turner760ad4d2017-01-20 22:42:09 +000054 Info = llvm::make_unique<InfoStreamBuilder>(*Msf, NamedStreams);
Zachary Turnerdbeaea72016-07-11 21:45:26 +000055 return *Info;
56}
57
58DbiStreamBuilder &PDBFileBuilder::getDbiBuilder() {
59 if (!Dbi)
Zachary Turner620961d2016-09-14 23:00:02 +000060 Dbi = llvm::make_unique<DbiStreamBuilder>(*Msf);
Zachary Turnerdbeaea72016-07-11 21:45:26 +000061 return *Dbi;
62}
63
Zachary Turnerc6d54da2016-09-09 17:46:17 +000064TpiStreamBuilder &PDBFileBuilder::getTpiBuilder() {
65 if (!Tpi)
Zachary Turnerde9ba152016-09-15 18:22:31 +000066 Tpi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamTPI);
Zachary Turnerc6d54da2016-09-09 17:46:17 +000067 return *Tpi;
68}
69
Zachary Turnerde9ba152016-09-15 18:22:31 +000070TpiStreamBuilder &PDBFileBuilder::getIpiBuilder() {
71 if (!Ipi)
72 Ipi = llvm::make_unique<TpiStreamBuilder>(*Msf, StreamIPI);
73 return *Ipi;
74}
75
Zachary Turnere204a6c2017-05-02 18:00:13 +000076PDBStringTableBuilder &PDBFileBuilder::getStringTableBuilder() {
77 return Strings;
78}
Zachary Turner760ad4d2017-01-20 22:42:09 +000079
Zachary Turner946204c2017-08-09 04:23:25 +000080GSIStreamBuilder &PDBFileBuilder::getGsiBuilder() {
81 if (!Gsi)
82 Gsi = llvm::make_unique<GSIStreamBuilder>(*Msf);
83 return *Gsi;
Zachary Turner8d927b62017-07-31 19:36:08 +000084}
85
Zachary Turnera6fb5362018-03-23 18:43:39 +000086Expected<uint32_t> PDBFileBuilder::allocateNamedStream(StringRef Name,
87 uint32_t Size) {
Zachary Turner760ad4d2017-01-20 22:42:09 +000088 auto ExpectedStream = Msf->addStream(Size);
Zachary Turnera6fb5362018-03-23 18:43:39 +000089 if (ExpectedStream)
90 NamedStreams.set(Name, *ExpectedStream);
91 return ExpectedStream;
92}
93
94Error PDBFileBuilder::addNamedStream(StringRef Name, StringRef Data) {
95 Expected<uint32_t> ExpectedIndex = allocateNamedStream(Name, Data.size());
96 if (!ExpectedIndex)
97 return ExpectedIndex.takeError();
98 assert(NamedStreamData.count(*ExpectedIndex) == 0);
99 NamedStreamData[*ExpectedIndex] = Data;
Zachary Turner760ad4d2017-01-20 22:42:09 +0000100 return Error::success();
101}
102
Zachary Turnerf2282762018-03-23 19:57:25 +0000103void PDBFileBuilder::addInjectedSource(StringRef Name,
104 std::unique_ptr<MemoryBuffer> Buffer) {
105 // Stream names must be exact matches, since they get looked up in a hash
106 // table and the hash value is dependent on the exact contents of the string.
107 // link.exe lowercases a path and converts / to \, so we must do the same.
108 SmallString<64> VName;
109 sys::path::native(Name.lower(), VName);
110
111 uint32_t NI = getStringTableBuilder().insert(Name);
112 uint32_t VNI = getStringTableBuilder().insert(VName);
113
114 InjectedSourceDescriptor Desc;
115 Desc.Content = std::move(Buffer);
116 Desc.NameIndex = NI;
117 Desc.VNameIndex = VNI;
118 Desc.StreamName = "/src/files/";
119
120 Desc.StreamName += VName;
121
122 InjectedSources.push_back(std::move(Desc));
123}
124
Zachary Turneree8010a2018-06-27 21:18:15 +0000125Error PDBFileBuilder::finalizeMsfLayout() {
Zachary Turner68ea80d2017-06-12 21:46:51 +0000126
127 if (Ipi && Ipi->getRecordCount() > 0) {
128 // In theory newer PDBs always have an ID stream, but by saying that we're
129 // only going to *really* have an ID stream if there is at least one ID
130 // record, we leave open the opportunity to test older PDBs such as those
131 // that don't have an ID stream.
132 auto &Info = getInfoBuilder();
133 Info.addFeature(PdbRaw_FeatureSig::VC140);
134 }
135
Zachary Turnerc504ae32017-05-03 15:58:37 +0000136 uint32_t StringsLen = Strings.calculateSerializedSize();
Zachary Turner760ad4d2017-01-20 22:42:09 +0000137
Zachary Turnera6fb5362018-03-23 18:43:39 +0000138 Expected<uint32_t> SN = allocateNamedStream("/LinkInfo", 0);
139 if (!SN)
140 return SN.takeError();
Zachary Turner760ad4d2017-01-20 22:42:09 +0000141
Zachary Turner946204c2017-08-09 04:23:25 +0000142 if (Gsi) {
143 if (auto EC = Gsi->finalizeMsfLayout())
Zachary Turneree8010a2018-06-27 21:18:15 +0000144 return EC;
Zachary Turner7eaf1d92017-07-10 22:40:20 +0000145 if (Dbi) {
Zachary Turner946204c2017-08-09 04:23:25 +0000146 Dbi->setPublicsStreamIndex(Gsi->getPublicsStreamIndex());
147 Dbi->setGlobalsStreamIndex(Gsi->getGlobalsStreamIndex());
148 Dbi->setSymbolRecordStreamIndex(Gsi->getRecordStreamIdx());
Zachary Turner7eaf1d92017-07-10 22:40:20 +0000149 }
150 }
Zachary Turnera6fb5362018-03-23 18:43:39 +0000151 if (Tpi) {
152 if (auto EC = Tpi->finalizeMsfLayout())
Zachary Turneree8010a2018-06-27 21:18:15 +0000153 return EC;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000154 }
155 if (Dbi) {
156 if (auto EC = Dbi->finalizeMsfLayout())
Zachary Turneree8010a2018-06-27 21:18:15 +0000157 return EC;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000158 }
159 SN = allocateNamedStream("/names", StringsLen);
160 if (!SN)
161 return SN.takeError();
162
163 if (Ipi) {
164 if (auto EC = Ipi->finalizeMsfLayout())
Zachary Turneree8010a2018-06-27 21:18:15 +0000165 return EC;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000166 }
167
168 // Do this last, since it relies on the named stream map being complete, and
169 // that can be updated by previous steps in the finalization.
170 if (Info) {
171 if (auto EC = Info->finalizeMsfLayout())
Zachary Turneree8010a2018-06-27 21:18:15 +0000172 return EC;
Zachary Turnera6fb5362018-03-23 18:43:39 +0000173 }
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000174
Zachary Turnerf2282762018-03-23 19:57:25 +0000175 if (!InjectedSources.empty()) {
176 for (const auto &IS : InjectedSources) {
177 JamCRC CRC(0);
178 CRC.update(makeArrayRef(IS.Content->getBufferStart(),
179 IS.Content->getBufferSize()));
180
181 SrcHeaderBlockEntry Entry;
182 ::memset(&Entry, 0, sizeof(SrcHeaderBlockEntry));
183 Entry.Size = sizeof(SrcHeaderBlockEntry);
184 Entry.FileSize = IS.Content->getBufferSize();
185 Entry.FileNI = IS.NameIndex;
186 Entry.VFileNI = IS.VNameIndex;
187 Entry.ObjNI = 1;
188 Entry.IsVirtual = 0;
189 Entry.Version =
190 static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne);
191 Entry.CRC = CRC.getCRC();
192 StringRef VName = getStringTableBuilder().getStringForId(IS.VNameIndex);
193 InjectedSourceTable.set_as(VName, std::move(Entry));
194 }
195
196 uint32_t SrcHeaderBlockSize =
197 sizeof(SrcHeaderBlockHeader) +
198 InjectedSourceTable.calculateSerializedLength();
199 SN = allocateNamedStream("/src/headerblock", SrcHeaderBlockSize);
200 if (!SN)
201 return SN.takeError();
202 for (const auto &IS : InjectedSources) {
203 SN = allocateNamedStream(IS.StreamName, IS.Content->getBufferSize());
204 if (!SN)
205 return SN.takeError();
206 }
207 }
208
209 // Do this last, since it relies on the named stream map being complete, and
210 // that can be updated by previous steps in the finalization.
211 if (Info) {
212 if (auto EC = Info->finalizeMsfLayout())
Zachary Turneree8010a2018-06-27 21:18:15 +0000213 return EC;
Zachary Turnerf2282762018-03-23 19:57:25 +0000214 }
215
Zachary Turneree8010a2018-06-27 21:18:15 +0000216 return Error::success();
Zachary Turner199f48a2016-07-28 19:11:09 +0000217}
218
Zachary Turnerc504ae32017-05-03 15:58:37 +0000219Expected<uint32_t> PDBFileBuilder::getNamedStreamIndex(StringRef Name) const {
220 uint32_t SN = 0;
221 if (!NamedStreams.get(Name, SN))
222 return llvm::make_error<pdb::RawError>(raw_error_code::no_stream);
223 return SN;
224}
225
Zachary Turnerf2282762018-03-23 19:57:25 +0000226void PDBFileBuilder::commitSrcHeaderBlock(WritableBinaryStream &MsfBuffer,
227 const msf::MSFLayout &Layout) {
228 assert(!InjectedSourceTable.empty());
229
230 uint32_t SN = cantFail(getNamedStreamIndex("/src/headerblock"));
231 auto Stream = WritableMappedBlockStream::createIndexedStream(
232 Layout, MsfBuffer, SN, Allocator);
233 BinaryStreamWriter Writer(*Stream);
234
235 SrcHeaderBlockHeader Header;
236 ::memset(&Header, 0, sizeof(Header));
237 Header.Version = static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne);
238 Header.Size = Writer.bytesRemaining();
239
240 cantFail(Writer.writeObject(Header));
241 cantFail(InjectedSourceTable.commit(Writer));
242
243 assert(Writer.bytesRemaining() == 0);
244}
245
246void PDBFileBuilder::commitInjectedSources(WritableBinaryStream &MsfBuffer,
247 const msf::MSFLayout &Layout) {
248 if (InjectedSourceTable.empty())
249 return;
250
251 commitSrcHeaderBlock(MsfBuffer, Layout);
252
253 for (const auto &IS : InjectedSources) {
254 uint32_t SN = cantFail(getNamedStreamIndex(IS.StreamName));
255
256 auto SourceStream = WritableMappedBlockStream::createIndexedStream(
257 Layout, MsfBuffer, SN, Allocator);
258 BinaryStreamWriter SourceWriter(*SourceStream);
259 assert(SourceWriter.bytesRemaining() == IS.Content->getBufferSize());
260 cantFail(SourceWriter.writeBytes(
261 arrayRefFromStringRef(IS.Content->getBuffer())));
262 }
263}
264
Nico Weber205ca682018-09-15 18:35:51 +0000265Error PDBFileBuilder::commit(StringRef Filename, codeview::GUID *Guid) {
Bob Haarmande33a6372017-05-17 20:46:48 +0000266 assert(!Filename.empty());
Zachary Turneree8010a2018-06-27 21:18:15 +0000267 if (auto EC = finalizeMsfLayout())
Zachary Turnerd66889c2016-07-28 19:12:28 +0000268 return EC;
Zachary Turner9fb9d712017-08-02 22:31:39 +0000269
Zachary Turneree8010a2018-06-27 21:18:15 +0000270 MSFLayout Layout;
Nico Weber205ca682018-09-15 18:35:51 +0000271 Expected<FileBufferByteStream> ExpectedMsfBuffer =
272 Msf->commit(Filename, Layout);
Zachary Turneree8010a2018-06-27 21:18:15 +0000273 if (!ExpectedMsfBuffer)
274 return ExpectedMsfBuffer.takeError();
275 FileBufferByteStream Buffer = std::move(*ExpectedMsfBuffer);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000276
Zachary Turnerc504ae32017-05-03 15:58:37 +0000277 auto ExpectedSN = getNamedStreamIndex("/names");
278 if (!ExpectedSN)
279 return ExpectedSN.takeError();
Zachary Turner760ad4d2017-01-20 22:42:09 +0000280
Zachary Turner5b74ff32017-06-03 00:33:35 +0000281 auto NS = WritableMappedBlockStream::createIndexedStream(
282 Layout, Buffer, *ExpectedSN, Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000283 BinaryStreamWriter NSWriter(*NS);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000284 if (auto EC = Strings.commit(NSWriter))
285 return EC;
286
Zachary Turnera6fb5362018-03-23 18:43:39 +0000287 for (const auto &NSE : NamedStreamData) {
288 if (NSE.second.empty())
289 continue;
290
291 auto NS = WritableMappedBlockStream::createIndexedStream(
292 Layout, Buffer, NSE.first, Allocator);
293 BinaryStreamWriter NSW(*NS);
294 if (auto EC = NSW.writeBytes(arrayRefFromStringRef(NSE.second)))
295 return EC;
296 }
297
Zachary Turnerd66889c2016-07-28 19:12:28 +0000298 if (Info) {
299 if (auto EC = Info->commit(Layout, Buffer))
300 return EC;
301 }
302
303 if (Dbi) {
304 if (auto EC = Dbi->commit(Layout, Buffer))
305 return EC;
306 }
307
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000308 if (Tpi) {
309 if (auto EC = Tpi->commit(Layout, Buffer))
310 return EC;
311 }
312
Zachary Turnerde9ba152016-09-15 18:22:31 +0000313 if (Ipi) {
314 if (auto EC = Ipi->commit(Layout, Buffer))
315 return EC;
316 }
317
Zachary Turner946204c2017-08-09 04:23:25 +0000318 if (Gsi) {
319 if (auto EC = Gsi->commit(Layout, Buffer))
Zachary Turner8d927b62017-07-31 19:36:08 +0000320 return EC;
321 }
322
Zachary Turnerc6a75a62018-03-01 18:00:29 +0000323 auto InfoStreamBlocks = Layout.StreamMap[StreamPDB];
324 assert(!InfoStreamBlocks.empty());
325 uint64_t InfoStreamFileOffset =
326 blockToOffset(InfoStreamBlocks.front(), Layout.SB->BlockSize);
327 InfoStreamHeader *H = reinterpret_cast<InfoStreamHeader *>(
Zachary Turneree8010a2018-06-27 21:18:15 +0000328 Buffer.getBufferStart() + InfoStreamFileOffset);
Zachary Turnerc6a75a62018-03-01 18:00:29 +0000329
Zachary Turnerf2282762018-03-23 19:57:25 +0000330 commitInjectedSources(Buffer, Layout);
331
Zachary Turnerc6a75a62018-03-01 18:00:29 +0000332 // Set the build id at the very end, after every other byte of the PDB
333 // has been written.
Nico Weber205ca682018-09-15 18:35:51 +0000334 if (Info->hashPDBContentsToGUID()) {
335 // Compute a hash of all sections of the output file.
336 uint64_t Digest =
337 xxHash64({Buffer.getBufferStart(), Buffer.getBufferEnd()});
338
339 H->Age = 1;
340
341 memcpy(H->Guid.Guid, &Digest, 8);
342 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
343 memcpy(H->Guid.Guid + 8, "LLD PDB.", 8);
344
345 // Put the hash in the Signature field too.
346 H->Signature = static_cast<uint32_t>(Digest);
347
348 // Return GUID to caller.
349 memcpy(Guid, H->Guid.Guid, 16);
350 } else {
351 H->Age = Info->getAge();
352 H->Guid = Info->getGuid();
353 Optional<uint32_t> Sig = Info->getSignature();
354 H->Signature = Sig.hasValue() ? *Sig : time(nullptr);
355 }
Zachary Turnerc6a75a62018-03-01 18:00:29 +0000356
Zachary Turnerd66889c2016-07-28 19:12:28 +0000357 return Buffer.commit();
Rui Ueyamafc22cef2016-09-30 20:34:44 +0000358}