1 //===- PDBFileBuilder.cpp - PDB File Creation -------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
10 #include "llvm/ADT/BitVector.h"
11 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
12 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
13 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
14 #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
15 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
16 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
17 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
18 #include "llvm/DebugInfo/PDB/Native/RawError.h"
19 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
20 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
21 #include "llvm/Support/BinaryStream.h"
22 #include "llvm/Support/BinaryStreamWriter.h"
23 #include "llvm/Support/CRC.h"
24 #include "llvm/Support/Chrono.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/xxhash.h"
29 using namespace llvm::codeview;
30 using namespace llvm::msf;
31 using namespace llvm::pdb;
32 using namespace llvm::support;
34 PDBFileBuilder::PDBFileBuilder(BumpPtrAllocator &Allocator)
35 : Allocator(Allocator), InjectedSourceHashTraits(Strings),
36 InjectedSourceTable(2) {}
38 PDBFileBuilder::~PDBFileBuilder() {}
40 Error PDBFileBuilder::initialize(uint32_t BlockSize) {
41 auto ExpectedMsf = MSFBuilder::create(Allocator, BlockSize);
43 return ExpectedMsf.takeError();
44 Msf = std::make_unique<MSFBuilder>(std::move(*ExpectedMsf));
45 return Error::success();
48 MSFBuilder &PDBFileBuilder::getMsfBuilder() { return *Msf; }
50 InfoStreamBuilder &PDBFileBuilder::getInfoBuilder() {
52 Info = std::make_unique<InfoStreamBuilder>(*Msf, NamedStreams);
56 DbiStreamBuilder &PDBFileBuilder::getDbiBuilder() {
58 Dbi = std::make_unique<DbiStreamBuilder>(*Msf);
62 TpiStreamBuilder &PDBFileBuilder::getTpiBuilder() {
64 Tpi = std::make_unique<TpiStreamBuilder>(*Msf, StreamTPI);
68 TpiStreamBuilder &PDBFileBuilder::getIpiBuilder() {
70 Ipi = std::make_unique<TpiStreamBuilder>(*Msf, StreamIPI);
74 PDBStringTableBuilder &PDBFileBuilder::getStringTableBuilder() {
78 GSIStreamBuilder &PDBFileBuilder::getGsiBuilder() {
80 Gsi = std::make_unique<GSIStreamBuilder>(*Msf);
84 Expected<uint32_t> PDBFileBuilder::allocateNamedStream(StringRef Name,
86 auto ExpectedStream = Msf->addStream(Size);
88 NamedStreams.set(Name, *ExpectedStream);
89 return ExpectedStream;
92 Error PDBFileBuilder::addNamedStream(StringRef Name, StringRef Data) {
93 Expected<uint32_t> ExpectedIndex = allocateNamedStream(Name, Data.size());
95 return ExpectedIndex.takeError();
96 assert(NamedStreamData.count(*ExpectedIndex) == 0);
97 NamedStreamData[*ExpectedIndex] = std::string(Data);
98 return Error::success();
101 void PDBFileBuilder::addInjectedSource(StringRef Name,
102 std::unique_ptr<MemoryBuffer> Buffer) {
103 // Stream names must be exact matches, since they get looked up in a hash
104 // table and the hash value is dependent on the exact contents of the string.
105 // link.exe lowercases a path and converts / to \, so we must do the same.
106 SmallString<64> VName;
107 sys::path::native(Name.lower(), VName);
109 uint32_t NI = getStringTableBuilder().insert(Name);
110 uint32_t VNI = getStringTableBuilder().insert(VName);
112 InjectedSourceDescriptor Desc;
113 Desc.Content = std::move(Buffer);
115 Desc.VNameIndex = VNI;
116 Desc.StreamName = "/src/files/";
118 Desc.StreamName += VName;
120 InjectedSources.push_back(std::move(Desc));
123 Error PDBFileBuilder::finalizeMsfLayout() {
125 if (Ipi && Ipi->getRecordCount() > 0) {
126 // In theory newer PDBs always have an ID stream, but by saying that we're
127 // only going to *really* have an ID stream if there is at least one ID
128 // record, we leave open the opportunity to test older PDBs such as those
129 // that don't have an ID stream.
130 auto &Info = getInfoBuilder();
131 Info.addFeature(PdbRaw_FeatureSig::VC140);
134 uint32_t StringsLen = Strings.calculateSerializedSize();
136 Expected<uint32_t> SN = allocateNamedStream("/LinkInfo", 0);
138 return SN.takeError();
141 if (auto EC = Gsi->finalizeMsfLayout())
144 Dbi->setPublicsStreamIndex(Gsi->getPublicsStreamIndex());
145 Dbi->setGlobalsStreamIndex(Gsi->getGlobalsStreamIndex());
146 Dbi->setSymbolRecordStreamIndex(Gsi->getRecordStreamIndex());
150 if (auto EC = Tpi->finalizeMsfLayout())
154 if (auto EC = Dbi->finalizeMsfLayout())
157 SN = allocateNamedStream("/names", StringsLen);
159 return SN.takeError();
162 if (auto EC = Ipi->finalizeMsfLayout())
166 // Do this last, since it relies on the named stream map being complete, and
167 // that can be updated by previous steps in the finalization.
169 if (auto EC = Info->finalizeMsfLayout())
173 if (!InjectedSources.empty()) {
174 for (const auto &IS : InjectedSources) {
176 CRC.update(arrayRefFromStringRef(IS.Content->getBuffer()));
178 SrcHeaderBlockEntry Entry;
179 ::memset(&Entry, 0, sizeof(SrcHeaderBlockEntry));
180 Entry.Size = sizeof(SrcHeaderBlockEntry);
181 Entry.FileSize = IS.Content->getBufferSize();
182 Entry.FileNI = IS.NameIndex;
183 Entry.VFileNI = IS.VNameIndex;
187 static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne);
188 Entry.CRC = CRC.getCRC();
189 StringRef VName = getStringTableBuilder().getStringForId(IS.VNameIndex);
190 InjectedSourceTable.set_as(VName, std::move(Entry),
191 InjectedSourceHashTraits);
194 uint32_t SrcHeaderBlockSize =
195 sizeof(SrcHeaderBlockHeader) +
196 InjectedSourceTable.calculateSerializedLength();
197 SN = allocateNamedStream("/src/headerblock", SrcHeaderBlockSize);
199 return SN.takeError();
200 for (const auto &IS : InjectedSources) {
201 SN = allocateNamedStream(IS.StreamName, IS.Content->getBufferSize());
203 return SN.takeError();
207 // Do this last, since it relies on the named stream map being complete, and
208 // that can be updated by previous steps in the finalization.
210 if (auto EC = Info->finalizeMsfLayout())
214 return Error::success();
217 Expected<uint32_t> PDBFileBuilder::getNamedStreamIndex(StringRef Name) const {
219 if (!NamedStreams.get(Name, SN))
220 return llvm::make_error<pdb::RawError>(raw_error_code::no_stream);
224 void PDBFileBuilder::commitSrcHeaderBlock(WritableBinaryStream &MsfBuffer,
225 const msf::MSFLayout &Layout) {
226 assert(!InjectedSourceTable.empty());
228 uint32_t SN = cantFail(getNamedStreamIndex("/src/headerblock"));
229 auto Stream = WritableMappedBlockStream::createIndexedStream(
230 Layout, MsfBuffer, SN, Allocator);
231 BinaryStreamWriter Writer(*Stream);
233 SrcHeaderBlockHeader Header;
234 ::memset(&Header, 0, sizeof(Header));
235 Header.Version = static_cast<uint32_t>(PdbRaw_SrcHeaderBlockVer::SrcVerOne);
236 Header.Size = Writer.bytesRemaining();
238 cantFail(Writer.writeObject(Header));
239 cantFail(InjectedSourceTable.commit(Writer));
241 assert(Writer.bytesRemaining() == 0);
244 void PDBFileBuilder::commitInjectedSources(WritableBinaryStream &MsfBuffer,
245 const msf::MSFLayout &Layout) {
246 if (InjectedSourceTable.empty())
249 commitSrcHeaderBlock(MsfBuffer, Layout);
251 for (const auto &IS : InjectedSources) {
252 uint32_t SN = cantFail(getNamedStreamIndex(IS.StreamName));
254 auto SourceStream = WritableMappedBlockStream::createIndexedStream(
255 Layout, MsfBuffer, SN, Allocator);
256 BinaryStreamWriter SourceWriter(*SourceStream);
257 assert(SourceWriter.bytesRemaining() == IS.Content->getBufferSize());
258 cantFail(SourceWriter.writeBytes(
259 arrayRefFromStringRef(IS.Content->getBuffer())));
263 Error PDBFileBuilder::commit(StringRef Filename, codeview::GUID *Guid) {
264 assert(!Filename.empty());
265 if (auto EC = finalizeMsfLayout())
269 Expected<FileBufferByteStream> ExpectedMsfBuffer =
270 Msf->commit(Filename, Layout);
271 if (!ExpectedMsfBuffer)
272 return ExpectedMsfBuffer.takeError();
273 FileBufferByteStream Buffer = std::move(*ExpectedMsfBuffer);
275 auto ExpectedSN = getNamedStreamIndex("/names");
277 return ExpectedSN.takeError();
279 auto NS = WritableMappedBlockStream::createIndexedStream(
280 Layout, Buffer, *ExpectedSN, Allocator);
281 BinaryStreamWriter NSWriter(*NS);
282 if (auto EC = Strings.commit(NSWriter))
285 for (const auto &NSE : NamedStreamData) {
286 if (NSE.second.empty())
289 auto NS = WritableMappedBlockStream::createIndexedStream(
290 Layout, Buffer, NSE.first, Allocator);
291 BinaryStreamWriter NSW(*NS);
292 if (auto EC = NSW.writeBytes(arrayRefFromStringRef(NSE.second)))
297 if (auto EC = Info->commit(Layout, Buffer))
302 if (auto EC = Dbi->commit(Layout, Buffer))
307 if (auto EC = Tpi->commit(Layout, Buffer))
312 if (auto EC = Ipi->commit(Layout, Buffer))
317 if (auto EC = Gsi->commit(Layout, Buffer))
321 auto InfoStreamBlocks = Layout.StreamMap[StreamPDB];
322 assert(!InfoStreamBlocks.empty());
323 uint64_t InfoStreamFileOffset =
324 blockToOffset(InfoStreamBlocks.front(), Layout.SB->BlockSize);
325 InfoStreamHeader *H = reinterpret_cast<InfoStreamHeader *>(
326 Buffer.getBufferStart() + InfoStreamFileOffset);
328 commitInjectedSources(Buffer, Layout);
330 // Set the build id at the very end, after every other byte of the PDB
332 if (Info->hashPDBContentsToGUID()) {
333 // Compute a hash of all sections of the output file.
335 xxHash64({Buffer.getBufferStart(), Buffer.getBufferEnd()});
339 memcpy(H->Guid.Guid, &Digest, 8);
340 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
341 memcpy(H->Guid.Guid + 8, "LLD PDB.", 8);
343 // Put the hash in the Signature field too.
344 H->Signature = static_cast<uint32_t>(Digest);
346 // Return GUID to caller.
347 memcpy(Guid, H->Guid.Guid, 16);
349 H->Age = Info->getAge();
350 H->Guid = Info->getGuid();
351 Optional<uint32_t> Sig = Info->getSignature();
352 H->Signature = Sig.hasValue() ? *Sig : time(nullptr);
355 return Buffer.commit();