blob: e0dc58b2a182c42a2d38ddecc8d9d3e15bf00803 [file] [log] [blame]
Rui Ueyamae7378242015-12-04 23:11:05 +00001//===- PDB.cpp ------------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Rui Ueyama1d99ab32016-09-15 22:24:51 +000010#include "PDB.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000011#include "Chunks.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000012#include "Config.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000013#include "Error.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000014#include "SymbolTable.h"
15#include "Symbols.h"
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +000016#include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000017#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Reid Kleckner44cdb102017-06-19 17:21:45 +000018#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
19#include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000020#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000021#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Reid Klecknerd0e6e242017-06-21 17:25:56 +000022#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
Rui Ueyama52896622017-01-12 03:09:25 +000023#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
24#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000025#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000026#include "llvm/DebugInfo/MSF/MSFCommon.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000027#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
28#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
29#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
30#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
31#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
32#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000033#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000034#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000035#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000036#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
37#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000038#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000039#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000040#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000041#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000042#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000043#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000044#include <memory>
45
Rui Ueyama1d99ab32016-09-15 22:24:51 +000046using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000047using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000048using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000049using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000050
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000051using llvm::object::coff_section;
52
Rui Ueyamab28c6d42016-09-16 04:32:33 +000053static ExitOnError ExitOnErr;
54
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000055// Returns a list of all SectionChunks.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000056static void addSectionContribs(SymbolTable *Symtab, pdb::DbiStreamBuilder &DbiBuilder) {
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000057 for (Chunk *C : Symtab->getChunks())
58 if (auto *SC = dyn_cast<SectionChunk>(C))
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000059 DbiBuilder.addSectionContrib(SC->File->ModuleDBI, SC->Header);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000060}
61
Rui Ueyamabe939b32016-11-21 17:22:35 +000062static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
63 StringRef Name) {
64 for (SectionChunk *C : Sections)
65 if (C->getSectionName() == Name)
66 return C;
67 return nullptr;
68}
69
Reid Kleckner44cdb102017-06-19 17:21:45 +000070static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
71 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +000072 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +000073 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +000074 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +000075 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +000076 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +000077 return Data.slice(4);
78}
Rui Ueyamac5cb7372016-12-01 01:22:48 +000079
Reid Kleckner44cdb102017-06-19 17:21:45 +000080static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
81 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
82 return consumeDebugMagic(Sec->getContents(), SecName);
83 return {};
84}
85
Reid Kleckner5d577522017-03-24 17:26:38 +000086static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Reid Kleckner44cdb102017-06-19 17:21:45 +000087 TypeTableBuilder &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +000088 // Start the TPI or IPI stream header.
89 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +000090
Reid Kleckner5d577522017-03-24 17:26:38 +000091 // Flatten the in memory type table.
Reid Kleckner5d577522017-03-24 17:26:38 +000092 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
Reid Kleckner13fc4112017-04-04 00:56:34 +000093 // FIXME: Hash types.
94 TpiBuilder.addTypeRecord(Rec, None);
Reid Kleckner5d577522017-03-24 17:26:38 +000095 });
Reid Kleckner5d577522017-03-24 17:26:38 +000096}
97
Reid Kleckner44cdb102017-06-19 17:21:45 +000098static void mergeDebugT(ObjectFile *File,
99 TypeTableBuilder &IDTable,
100 TypeTableBuilder &TypeTable,
101 SmallVectorImpl<TypeIndex> &TypeIndexMap,
102 pdb::PDBTypeServerHandler &Handler) {
103 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
104 if (Data.empty())
105 return;
106
107 BinaryByteStream Stream(Data, support::little);
108 CVTypeArray Types;
109 BinaryStreamReader Reader(Stream);
110 Handler.addSearchPath(sys::path::parent_path(File->getName()));
111 if (auto EC = Reader.readArray(Types, Reader.getLength()))
112 fatal(EC, "Reader::readArray failed");
113 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
114 TypeIndexMap, &Handler, Types))
115 fatal(Err, "codeview::mergeTypeStreams failed");
116}
117
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000118static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
119 if (TI.isSimple())
120 return true;
121 if (TI.toArrayIndex() >= TypeIndexMap.size())
122 return false;
123 TI = TypeIndexMap[TI.toArrayIndex()];
124 return true;
125}
126
127static bool remapTypesInSymbolRecord(ObjectFile *File,
128 MutableArrayRef<uint8_t> Contents,
129 ArrayRef<TypeIndex> TypeIndexMap,
130 ArrayRef<TiReference> TypeRefs) {
131 for (const TiReference &Ref : TypeRefs) {
132 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
133 if (Contents.size() < Ref.Offset + ByteSize) {
134 log("ignoring short symbol record");
135 return false;
136 }
137 MutableArrayRef<TypeIndex> TIs(
138 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
139 for (TypeIndex &TI : TIs)
140 if (!remapTypeIndex(TI, TypeIndexMap)) {
141 log("ignoring symbol record in " + File->getName() +
142 " with bad type index 0x" + utohexstr(TI.getIndex()));
143 return false;
144 }
145 }
146 return true;
147}
148
149/// MSVC translates S_PROC_ID_END to S_END.
150uint16_t canonicalizeSymbolKind(SymbolKind Kind) {
151 if (Kind == SymbolKind::S_PROC_ID_END)
152 return SymbolKind::S_END;
153 return Kind;
154}
155
156/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
157/// The object file may not be aligned.
158static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
159 BumpPtrAllocator &Alloc) {
160 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
161 assert(Size >= 4 && "record too short");
162 assert(Size <= MaxRecordLength && "record too long");
163 void *Mem = Alloc.Allocate(Size, 4);
164
165 // Copy the symbol record and zero out any padding bytes.
166 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
167 memcpy(NewData.data(), Sym.data().data(), Sym.length());
168 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
169
170 // Update the record prefix length. It should point to the beginning of the
171 // next record. MSVC does some canonicalization of the record kind, so we do
172 // that as well.
173 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
174 Prefix->RecordKind = canonicalizeSymbolKind(Sym.kind());
175 Prefix->RecordLen = Size - 2;
176 return NewData;
177}
178
Reid Kleckner3f851922017-07-06 16:39:32 +0000179/// Return true if this symbol opens a scope. This implies that the symbol has
180/// "parent" and "end" fields, which contain the offset of the S_END or
181/// S_INLINESITE_END record.
182static bool symbolOpensScope(SymbolKind Kind) {
183 switch (Kind) {
184 case SymbolKind::S_GPROC32:
185 case SymbolKind::S_LPROC32:
186 case SymbolKind::S_LPROC32_ID:
187 case SymbolKind::S_GPROC32_ID:
188 case SymbolKind::S_BLOCK32:
189 case SymbolKind::S_SEPCODE:
190 case SymbolKind::S_THUNK32:
191 case SymbolKind::S_INLINESITE:
192 case SymbolKind::S_INLINESITE2:
193 return true;
194 default:
195 break;
196 }
197 return false;
198}
199
200static bool symbolEndsScope(SymbolKind Kind) {
201 switch (Kind) {
202 case SymbolKind::S_END:
203 case SymbolKind::S_PROC_ID_END:
204 case SymbolKind::S_INLINESITE_END:
205 return true;
206 default:
207 break;
208 }
209 return false;
210}
211
212struct ScopeRecord {
213 ulittle32_t PtrParent;
214 ulittle32_t PtrEnd;
215};
216
217struct SymbolScope {
218 ScopeRecord *OpeningRecord;
219 uint32_t ScopeOffset;
220};
221
222static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
223 uint32_t CurOffset, CVSymbol &Sym) {
224 assert(symbolOpensScope(Sym.kind()));
225 SymbolScope S;
226 S.ScopeOffset = CurOffset;
227 S.OpeningRecord = const_cast<ScopeRecord *>(
228 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
229 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
230 Stack.push_back(S);
231}
232
233static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
234 uint32_t CurOffset, ObjectFile *File) {
235 if (Stack.empty()) {
236 warn("symbol scopes are not balanced in " + File->getName());
237 return;
238 }
239 SymbolScope S = Stack.pop_back_val();
240 S.OpeningRecord->PtrEnd = CurOffset;
241}
242
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000243static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjectFile *File,
244 ArrayRef<TypeIndex> TypeIndexMap,
245 BinaryStreamRef SymData) {
246 // FIXME: Improve error recovery by warning and skipping records when
247 // possible.
248 CVSymbolArray Syms;
249 BinaryStreamReader Reader(SymData);
250 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000251 SmallVector<SymbolScope, 4> Scopes;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000252 for (const CVSymbol &Sym : Syms) {
253 // Discover type index references in the record. Skip it if we don't know
254 // where they are.
255 SmallVector<TiReference, 32> TypeRefs;
256 if (!discoverTypeIndices(Sym, TypeRefs)) {
257 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
258 continue;
259 }
260
261 // Copy the symbol record so we can mutate it.
262 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
263
264 // Re-map all the type index references.
265 MutableArrayRef<uint8_t> Contents =
266 NewData.drop_front(sizeof(RecordPrefix));
267 if (!remapTypesInSymbolRecord(File, Contents, TypeIndexMap, TypeRefs))
268 continue;
269
Reid Kleckner3f851922017-07-06 16:39:32 +0000270 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
271 CVSymbol NewSym(Sym.kind(), NewData);
272 if (symbolOpensScope(Sym.kind()))
273 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
274 else if (symbolEndsScope(Sym.kind()))
275 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000276
277 // Add the symbol to the module.
Reid Kleckner3f851922017-07-06 16:39:32 +0000278 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000279 }
280}
281
Reid Kleckner44cdb102017-06-19 17:21:45 +0000282// Allocate memory for a .debug$S section and relocate it.
283static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
284 SectionChunk *DebugChunk) {
285 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
286 assert(DebugChunk->OutputSectionOff == 0 &&
287 "debug sections should not be in output sections");
288 DebugChunk->writeTo(Buffer);
289 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
290 ".debug$S");
291}
292
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000293// Add all object files to the PDB. Merge .debug$T sections into IpiData and
294// TpiData.
Reid Kleckner44cdb102017-06-19 17:21:45 +0000295static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab,
296 pdb::PDBFileBuilder &Builder,
297 TypeTableBuilder &TypeTable,
298 TypeTableBuilder &IDTable) {
Zachary Turner2897e032017-05-25 21:16:03 +0000299 // Follow type servers. If the same type server is encountered more than
300 // once for this instance of `PDBTypeServerHandler` (for example if many
301 // object files reference the same TypeServer), the types from the
302 // TypeServer will only be visited once.
303 pdb::PDBTypeServerHandler Handler;
304
Reid Kleckner44cdb102017-06-19 17:21:45 +0000305 // PDBs use a single global string table for filenames in the file checksum
306 // table.
307 auto PDBStrTab = std::make_shared<DebugStringTableSubsection>();
308
Rui Ueyama52896622017-01-12 03:09:25 +0000309 // Visit all .debug$T sections to add them to Builder.
Rui Ueyama52896622017-01-12 03:09:25 +0000310 for (ObjectFile *File : Symtab->ObjectFiles) {
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000311 // Add a module descriptor for every object file. We need to put an absolute
312 // path to the object into the PDB. If this is a plain object, we make its
313 // path absolute. If it's an object in an archive, we make the archive path
314 // absolute.
315 bool InArchive = !File->ParentName.empty();
316 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
317 sys::fs::make_absolute(Path);
318 StringRef Name = InArchive ? File->getName() : StringRef(Path);
319 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
320 File->ModuleDBI->setObjFileName(Path);
321
Reid Kleckner44cdb102017-06-19 17:21:45 +0000322 // Before we can process symbol substreams from .debug$S, we need to process
323 // type information, file checksums, and the string table. Add type info to
324 // the PDB first, so that we can get the map from object file type and item
325 // indices to PDB type and item indices.
326 SmallVector<TypeIndex, 128> TypeIndexMap;
327 mergeDebugT(File, IDTable, TypeTable, TypeIndexMap, Handler);
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000328
Reid Kleckner44cdb102017-06-19 17:21:45 +0000329 // Now do all line info.
330 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
Reid Klecknerf5bb7382017-06-20 17:14:09 +0000331 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
Reid Kleckner44cdb102017-06-19 17:21:45 +0000332 continue;
Rui Ueyama52896622017-01-12 03:09:25 +0000333
Reid Kleckner44cdb102017-06-19 17:21:45 +0000334 ArrayRef<uint8_t> RelocatedDebugContents =
335 relocateDebugChunk(Alloc, DebugChunk);
336 if (RelocatedDebugContents.empty())
337 continue;
338
339 DebugSubsectionArray Subsections;
340 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
341 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
342
343 DebugStringTableSubsectionRef CVStrTab;
344 DebugChecksumsSubsectionRef Checksums;
345 for (const DebugSubsectionRecord &SS : Subsections) {
346 switch (SS.kind()) {
347 case DebugSubsectionKind::StringTable:
348 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
349 break;
350 case DebugSubsectionKind::FileChecksums:
351 ExitOnErr(Checksums.initialize(SS.getRecordData()));
352 break;
353 case DebugSubsectionKind::Lines:
354 // We can add the relocated line table directly to the PDB without
355 // modification because the file checksum offsets will stay the same.
356 File->ModuleDBI->addDebugSubsection(SS);
357 break;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000358 case DebugSubsectionKind::Symbols:
359 mergeSymbolRecords(Alloc, File, TypeIndexMap, SS.getRecordData());
360 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000361 default:
362 // FIXME: Process the rest of the subsections.
363 break;
364 }
365 }
366
367 if (Checksums.valid()) {
368 // Make a new file checksum table that refers to offsets in the PDB-wide
369 // string table. Generally the string table subsection appears after the
370 // checksum table, so we have to do this after looping over all the
371 // subsections.
372 if (!CVStrTab.valid())
373 fatal(".debug$S sections must have both a string table subsection "
374 "and a checksum subsection table or neither");
375 auto NewChecksums =
Reid Kleckneradea0ce2017-06-19 17:27:31 +0000376 make_unique<DebugChecksumsSubsection>(*PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000377 for (FileChecksumEntry &FC : Checksums) {
378 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
379 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(
380 *File->ModuleDBI, FileName));
381 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
382 }
383 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
384 }
385 }
Rui Ueyama52896622017-01-12 03:09:25 +0000386 }
387
Reid Kleckner44cdb102017-06-19 17:21:45 +0000388 Builder.getStringTableBuilder().setStrings(*PDBStrTab);
389
Reid Kleckner5d577522017-03-24 17:26:38 +0000390 // Construct TPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000391 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
Reid Kleckner5d577522017-03-24 17:26:38 +0000392
393 // Construct IPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000394 addTypeInfo(Builder.getIpiBuilder(), IDTable);
Rui Ueyama52896622017-01-12 03:09:25 +0000395}
396
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000397// Creates a PDB file.
398void coff::createPDB(StringRef Path, SymbolTable *Symtab,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000399 ArrayRef<uint8_t> SectionTable,
400 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000401 BumpPtrAllocator Alloc;
402 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000403 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000404
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000405 // Create streams in MSF for predefined streams, namely
406 // PDB, TPI, DBI and IPI.
407 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
408 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000409
Rui Ueyamabb542b32016-09-16 22:51:17 +0000410 // Add an Info stream.
411 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000412 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000413 InfoBuilder.setSignature(time(nullptr));
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000414
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000415 llvm::SmallString<128> NativePath(Path.begin(), Path.end());
416 llvm::sys::fs::make_absolute(NativePath);
417 llvm::sys::path::native(NativePath);
418
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000419 pdb::PDB_UniqueId uuid{};
420 if (DI)
421 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
422 InfoBuilder.setGuid(uuid);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000423 // Should be the current time, but set 0 for reproducibilty.
424 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000425 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000426
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000427 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000428 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000429 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Rui Ueyama1343fac2016-10-06 22:52:01 +0000430
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000431 // It's not entirely clear what this is, but the * Linker * module uses it.
432 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
433
Reid Kleckner44cdb102017-06-19 17:21:45 +0000434 TypeTableBuilder TypeTable(BAlloc);
435 TypeTableBuilder IDTable(BAlloc);
436 addObjectsToPDB(Alloc, Symtab, Builder, TypeTable, IDTable);
Rui Ueyamad381c982016-10-05 21:37:25 +0000437
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000438 // Add Section Contributions.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000439 addSectionContribs(Symtab, DbiBuilder);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000440
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000441 // Add Section Map stream.
442 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000443 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000444 SectionTable.size() / sizeof(object::coff_section)};
445 std::vector<pdb::SecMapEntry> SectionMap =
446 pdb::DbiStreamBuilder::createSectionMap(Sections);
447 DbiBuilder.setSectionMap(SectionMap);
448
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000449 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
450 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Rui Ueyamac91f7162016-11-16 01:10:46 +0000451
Rui Ueyama9f66f822016-10-11 19:45:07 +0000452 // Add COFF section header stream.
453 ExitOnErr(
454 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
455
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000456 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000457 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000458}