blob: 6d0f064381ecf88b5bd8939a18c48d2af6d4975b [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 Turner6708e0b2017-07-10 21:01:37 +000021#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000022#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Reid Klecknerd0e6e242017-06-21 17:25:56 +000023#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
Rui Ueyama52896622017-01-12 03:09:25 +000024#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
25#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000026#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000027#include "llvm/DebugInfo/MSF/MSFCommon.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000028#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000029#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
30#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
31#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
32#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
33#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
34#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000035#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000036#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000037#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
38#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000039#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000040#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000041#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000042#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000043#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000044#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000045#include <memory>
46
Rui Ueyama1d99ab32016-09-15 22:24:51 +000047using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000048using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000049using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000050using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000051
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000052using llvm::object::coff_section;
53
Rui Ueyamab28c6d42016-09-16 04:32:33 +000054static ExitOnError ExitOnErr;
55
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000056// Returns a list of all SectionChunks.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000057static void addSectionContribs(SymbolTable *Symtab, pdb::DbiStreamBuilder &DbiBuilder) {
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000058 for (Chunk *C : Symtab->getChunks())
59 if (auto *SC = dyn_cast<SectionChunk>(C))
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000060 DbiBuilder.addSectionContrib(SC->File->ModuleDBI, SC->Header);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000061}
62
Rui Ueyamabe939b32016-11-21 17:22:35 +000063static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
64 StringRef Name) {
65 for (SectionChunk *C : Sections)
66 if (C->getSectionName() == Name)
67 return C;
68 return nullptr;
69}
70
Reid Kleckner44cdb102017-06-19 17:21:45 +000071static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
72 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +000073 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +000074 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +000075 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +000076 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +000077 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +000078 return Data.slice(4);
79}
Rui Ueyamac5cb7372016-12-01 01:22:48 +000080
Reid Kleckner44cdb102017-06-19 17:21:45 +000081static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
82 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
83 return consumeDebugMagic(Sec->getContents(), SecName);
84 return {};
85}
86
Reid Kleckner5d577522017-03-24 17:26:38 +000087static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Reid Kleckner44cdb102017-06-19 17:21:45 +000088 TypeTableBuilder &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +000089 // Start the TPI or IPI stream header.
90 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +000091
Reid Kleckner5d577522017-03-24 17:26:38 +000092 // Flatten the in memory type table.
Reid Kleckner5d577522017-03-24 17:26:38 +000093 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
Reid Kleckner13fc4112017-04-04 00:56:34 +000094 // FIXME: Hash types.
95 TpiBuilder.addTypeRecord(Rec, None);
Reid Kleckner5d577522017-03-24 17:26:38 +000096 });
Reid Kleckner5d577522017-03-24 17:26:38 +000097}
98
Reid Kleckner44cdb102017-06-19 17:21:45 +000099static void mergeDebugT(ObjectFile *File,
100 TypeTableBuilder &IDTable,
101 TypeTableBuilder &TypeTable,
102 SmallVectorImpl<TypeIndex> &TypeIndexMap,
103 pdb::PDBTypeServerHandler &Handler) {
104 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
105 if (Data.empty())
106 return;
107
108 BinaryByteStream Stream(Data, support::little);
109 CVTypeArray Types;
110 BinaryStreamReader Reader(Stream);
111 Handler.addSearchPath(sys::path::parent_path(File->getName()));
112 if (auto EC = Reader.readArray(Types, Reader.getLength()))
113 fatal(EC, "Reader::readArray failed");
114 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
115 TypeIndexMap, &Handler, Types))
116 fatal(Err, "codeview::mergeTypeStreams failed");
117}
118
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000119static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
120 if (TI.isSimple())
121 return true;
122 if (TI.toArrayIndex() >= TypeIndexMap.size())
123 return false;
124 TI = TypeIndexMap[TI.toArrayIndex()];
125 return true;
126}
127
128static bool remapTypesInSymbolRecord(ObjectFile *File,
129 MutableArrayRef<uint8_t> Contents,
130 ArrayRef<TypeIndex> TypeIndexMap,
131 ArrayRef<TiReference> TypeRefs) {
132 for (const TiReference &Ref : TypeRefs) {
133 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
134 if (Contents.size() < Ref.Offset + ByteSize) {
135 log("ignoring short symbol record");
136 return false;
137 }
138 MutableArrayRef<TypeIndex> TIs(
139 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
140 for (TypeIndex &TI : TIs)
141 if (!remapTypeIndex(TI, TypeIndexMap)) {
142 log("ignoring symbol record in " + File->getName() +
143 " with bad type index 0x" + utohexstr(TI.getIndex()));
144 return false;
145 }
146 }
147 return true;
148}
149
150/// MSVC translates S_PROC_ID_END to S_END.
151uint16_t canonicalizeSymbolKind(SymbolKind Kind) {
152 if (Kind == SymbolKind::S_PROC_ID_END)
153 return SymbolKind::S_END;
154 return Kind;
155}
156
157/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
158/// The object file may not be aligned.
159static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
160 BumpPtrAllocator &Alloc) {
161 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
162 assert(Size >= 4 && "record too short");
163 assert(Size <= MaxRecordLength && "record too long");
164 void *Mem = Alloc.Allocate(Size, 4);
165
166 // Copy the symbol record and zero out any padding bytes.
167 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
168 memcpy(NewData.data(), Sym.data().data(), Sym.length());
169 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
170
171 // Update the record prefix length. It should point to the beginning of the
172 // next record. MSVC does some canonicalization of the record kind, so we do
173 // that as well.
174 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
175 Prefix->RecordKind = canonicalizeSymbolKind(Sym.kind());
176 Prefix->RecordLen = Size - 2;
177 return NewData;
178}
179
Reid Kleckner3f851922017-07-06 16:39:32 +0000180/// Return true if this symbol opens a scope. This implies that the symbol has
181/// "parent" and "end" fields, which contain the offset of the S_END or
182/// S_INLINESITE_END record.
183static bool symbolOpensScope(SymbolKind Kind) {
184 switch (Kind) {
185 case SymbolKind::S_GPROC32:
186 case SymbolKind::S_LPROC32:
187 case SymbolKind::S_LPROC32_ID:
188 case SymbolKind::S_GPROC32_ID:
189 case SymbolKind::S_BLOCK32:
190 case SymbolKind::S_SEPCODE:
191 case SymbolKind::S_THUNK32:
192 case SymbolKind::S_INLINESITE:
193 case SymbolKind::S_INLINESITE2:
194 return true;
195 default:
196 break;
197 }
198 return false;
199}
200
201static bool symbolEndsScope(SymbolKind Kind) {
202 switch (Kind) {
203 case SymbolKind::S_END:
204 case SymbolKind::S_PROC_ID_END:
205 case SymbolKind::S_INLINESITE_END:
206 return true;
207 default:
208 break;
209 }
210 return false;
211}
212
213struct ScopeRecord {
214 ulittle32_t PtrParent;
215 ulittle32_t PtrEnd;
216};
217
218struct SymbolScope {
219 ScopeRecord *OpeningRecord;
220 uint32_t ScopeOffset;
221};
222
223static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
224 uint32_t CurOffset, CVSymbol &Sym) {
225 assert(symbolOpensScope(Sym.kind()));
226 SymbolScope S;
227 S.ScopeOffset = CurOffset;
228 S.OpeningRecord = const_cast<ScopeRecord *>(
229 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
230 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
231 Stack.push_back(S);
232}
233
234static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
235 uint32_t CurOffset, ObjectFile *File) {
236 if (Stack.empty()) {
237 warn("symbol scopes are not balanced in " + File->getName());
238 return;
239 }
240 SymbolScope S = Stack.pop_back_val();
241 S.OpeningRecord->PtrEnd = CurOffset;
242}
243
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000244static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjectFile *File,
245 ArrayRef<TypeIndex> TypeIndexMap,
246 BinaryStreamRef SymData) {
247 // FIXME: Improve error recovery by warning and skipping records when
248 // possible.
249 CVSymbolArray Syms;
250 BinaryStreamReader Reader(SymData);
251 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000252 SmallVector<SymbolScope, 4> Scopes;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000253 for (const CVSymbol &Sym : Syms) {
254 // Discover type index references in the record. Skip it if we don't know
255 // where they are.
256 SmallVector<TiReference, 32> TypeRefs;
257 if (!discoverTypeIndices(Sym, TypeRefs)) {
258 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
259 continue;
260 }
261
262 // Copy the symbol record so we can mutate it.
263 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
264
265 // Re-map all the type index references.
266 MutableArrayRef<uint8_t> Contents =
267 NewData.drop_front(sizeof(RecordPrefix));
268 if (!remapTypesInSymbolRecord(File, Contents, TypeIndexMap, TypeRefs))
269 continue;
270
Reid Kleckner3f851922017-07-06 16:39:32 +0000271 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
272 CVSymbol NewSym(Sym.kind(), NewData);
273 if (symbolOpensScope(Sym.kind()))
274 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
275 else if (symbolEndsScope(Sym.kind()))
276 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000277
278 // Add the symbol to the module.
Reid Kleckner3f851922017-07-06 16:39:32 +0000279 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000280 }
281}
282
Reid Kleckner44cdb102017-06-19 17:21:45 +0000283// Allocate memory for a .debug$S section and relocate it.
284static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
285 SectionChunk *DebugChunk) {
286 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
287 assert(DebugChunk->OutputSectionOff == 0 &&
288 "debug sections should not be in output sections");
289 DebugChunk->writeTo(Buffer);
290 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
291 ".debug$S");
292}
293
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000294// Add all object files to the PDB. Merge .debug$T sections into IpiData and
295// TpiData.
Reid Kleckner44cdb102017-06-19 17:21:45 +0000296static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab,
297 pdb::PDBFileBuilder &Builder,
298 TypeTableBuilder &TypeTable,
299 TypeTableBuilder &IDTable) {
Zachary Turner2897e032017-05-25 21:16:03 +0000300 // Follow type servers. If the same type server is encountered more than
301 // once for this instance of `PDBTypeServerHandler` (for example if many
302 // object files reference the same TypeServer), the types from the
303 // TypeServer will only be visited once.
304 pdb::PDBTypeServerHandler Handler;
305
Reid Kleckner44cdb102017-06-19 17:21:45 +0000306 // PDBs use a single global string table for filenames in the file checksum
307 // table.
308 auto PDBStrTab = std::make_shared<DebugStringTableSubsection>();
309
Rui Ueyama52896622017-01-12 03:09:25 +0000310 // Visit all .debug$T sections to add them to Builder.
Rui Ueyama52896622017-01-12 03:09:25 +0000311 for (ObjectFile *File : Symtab->ObjectFiles) {
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000312 // Add a module descriptor for every object file. We need to put an absolute
313 // path to the object into the PDB. If this is a plain object, we make its
314 // path absolute. If it's an object in an archive, we make the archive path
315 // absolute.
316 bool InArchive = !File->ParentName.empty();
317 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
318 sys::fs::make_absolute(Path);
Zachary Turner448dea42017-07-07 18:46:14 +0000319 sys::path::native(Path, llvm::sys::path::Style::windows);
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000320 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner448dea42017-07-07 18:46:14 +0000321
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000322 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
323 File->ModuleDBI->setObjFileName(Path);
324
Reid Kleckner44cdb102017-06-19 17:21:45 +0000325 // Before we can process symbol substreams from .debug$S, we need to process
326 // type information, file checksums, and the string table. Add type info to
327 // the PDB first, so that we can get the map from object file type and item
328 // indices to PDB type and item indices.
329 SmallVector<TypeIndex, 128> TypeIndexMap;
330 mergeDebugT(File, IDTable, TypeTable, TypeIndexMap, Handler);
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000331
Reid Kleckner44cdb102017-06-19 17:21:45 +0000332 // Now do all line info.
333 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
Reid Klecknerf5bb7382017-06-20 17:14:09 +0000334 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
Reid Kleckner44cdb102017-06-19 17:21:45 +0000335 continue;
Rui Ueyama52896622017-01-12 03:09:25 +0000336
Reid Kleckner44cdb102017-06-19 17:21:45 +0000337 ArrayRef<uint8_t> RelocatedDebugContents =
338 relocateDebugChunk(Alloc, DebugChunk);
339 if (RelocatedDebugContents.empty())
340 continue;
341
342 DebugSubsectionArray Subsections;
343 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
344 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
345
346 DebugStringTableSubsectionRef CVStrTab;
347 DebugChecksumsSubsectionRef Checksums;
348 for (const DebugSubsectionRecord &SS : Subsections) {
349 switch (SS.kind()) {
350 case DebugSubsectionKind::StringTable:
351 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
352 break;
353 case DebugSubsectionKind::FileChecksums:
354 ExitOnErr(Checksums.initialize(SS.getRecordData()));
355 break;
356 case DebugSubsectionKind::Lines:
357 // We can add the relocated line table directly to the PDB without
358 // modification because the file checksum offsets will stay the same.
359 File->ModuleDBI->addDebugSubsection(SS);
360 break;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000361 case DebugSubsectionKind::Symbols:
362 mergeSymbolRecords(Alloc, File, TypeIndexMap, SS.getRecordData());
363 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000364 default:
365 // FIXME: Process the rest of the subsections.
366 break;
367 }
368 }
369
370 if (Checksums.valid()) {
371 // Make a new file checksum table that refers to offsets in the PDB-wide
372 // string table. Generally the string table subsection appears after the
373 // checksum table, so we have to do this after looping over all the
374 // subsections.
375 if (!CVStrTab.valid())
376 fatal(".debug$S sections must have both a string table subsection "
377 "and a checksum subsection table or neither");
378 auto NewChecksums =
Reid Kleckneradea0ce2017-06-19 17:27:31 +0000379 make_unique<DebugChecksumsSubsection>(*PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000380 for (FileChecksumEntry &FC : Checksums) {
381 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
382 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(
383 *File->ModuleDBI, FileName));
384 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
385 }
386 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
387 }
388 }
Rui Ueyama52896622017-01-12 03:09:25 +0000389 }
390
Reid Kleckner44cdb102017-06-19 17:21:45 +0000391 Builder.getStringTableBuilder().setStrings(*PDBStrTab);
392
Reid Kleckner5d577522017-03-24 17:26:38 +0000393 // Construct TPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000394 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
Reid Kleckner5d577522017-03-24 17:26:38 +0000395
396 // Construct IPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000397 addTypeInfo(Builder.getIpiBuilder(), IDTable);
Rui Ueyama52896622017-01-12 03:09:25 +0000398}
399
Zachary Turner6708e0b2017-07-10 21:01:37 +0000400static void addLinkerModuleSymbols(StringRef Path,
401 pdb::DbiModuleDescriptorBuilder &Mod,
402 BumpPtrAllocator &Allocator) {
403 codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb);
404 codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
405 codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym);
406 codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
407
408 ONS.Name = "* Linker *";
409 ONS.Signature = 0;
410
411 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
412 CS.Flags = CompileSym3Flags::None;
413 CS.VersionBackendBuild = 0;
414 CS.VersionBackendMajor = 0;
415 CS.VersionBackendMinor = 0;
416 CS.VersionBackendQFE = 0;
417 CS.VersionFrontendBuild = 0;
418 CS.VersionFrontendMajor = 0;
419 CS.VersionFrontendMinor = 0;
420 CS.VersionFrontendQFE = 0;
421 CS.Version = "LLVM Linker";
422 CS.setLanguage(SourceLanguage::Link);
423
424 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
425 std::string ArgStr = llvm::join(Args, " ");
426 EBS.Fields.push_back("cwd");
427 SmallString<64> cwd;
428 llvm::sys::fs::current_path(cwd);
429 EBS.Fields.push_back(cwd);
430 EBS.Fields.push_back("exe");
431 std::string Exe =
432 llvm::sys::fs::getMainExecutable(Config->Argv[0].data(), nullptr);
433 EBS.Fields.push_back(Exe);
434 EBS.Fields.push_back("pdb");
435 EBS.Fields.push_back(Path);
436 EBS.Fields.push_back("cmd");
437 EBS.Fields.push_back(ArgStr);
438 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
439 ONS, Allocator, CodeViewContainer::Pdb));
440 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
441 CS, Allocator, CodeViewContainer::Pdb));
442 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
443 EBS, Allocator, CodeViewContainer::Pdb));
444}
445
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000446// Creates a PDB file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000447void coff::createPDB(SymbolTable *Symtab, ArrayRef<uint8_t> SectionTable,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000448 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000449 BumpPtrAllocator Alloc;
450 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000451 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000452
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000453 // Create streams in MSF for predefined streams, namely
454 // PDB, TPI, DBI and IPI.
455 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
456 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000457
Rui Ueyamabb542b32016-09-16 22:51:17 +0000458 // Add an Info stream.
459 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000460 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
461
Zachary Turner6708e0b2017-07-10 21:01:37 +0000462 llvm::SmallString<128> NativePath(Config->PDBPath.begin(),
463 Config->PDBPath.end());
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000464 llvm::sys::fs::make_absolute(NativePath);
Zachary Turner448dea42017-07-07 18:46:14 +0000465 llvm::sys::path::native(NativePath, llvm::sys::path::Style::windows);
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000466
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000467 pdb::PDB_UniqueId uuid{};
468 if (DI)
469 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
470 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000471 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000472 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000473
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000474 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000475 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000476 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000477 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Rui Ueyama1343fac2016-10-06 22:52:01 +0000478
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000479 // It's not entirely clear what this is, but the * Linker * module uses it.
480 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
481
Reid Kleckner44cdb102017-06-19 17:21:45 +0000482 TypeTableBuilder TypeTable(BAlloc);
483 TypeTableBuilder IDTable(BAlloc);
484 addObjectsToPDB(Alloc, Symtab, Builder, TypeTable, IDTable);
Rui Ueyamad381c982016-10-05 21:37:25 +0000485
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000486 // Add Section Contributions.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000487 addSectionContribs(Symtab, DbiBuilder);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000488
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000489 // Add Section Map stream.
490 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000491 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000492 SectionTable.size() / sizeof(object::coff_section)};
493 std::vector<pdb::SecMapEntry> SectionMap =
494 pdb::DbiStreamBuilder::createSectionMap(Sections);
495 DbiBuilder.setSectionMap(SectionMap);
496
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000497 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
498 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000499 addLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +0000500
Rui Ueyama9f66f822016-10-11 19:45:07 +0000501 // Add COFF section header stream.
502 ExitOnErr(
503 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
504
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000505 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000506 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +0000507}