blob: 44d17fffbb9a48439385736a4fa981b2d278351e [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
Reid Kleckner6597c282017-07-13 20:12:23 +0000108 // Look for type server PDBs next to the input file. If this file has a parent
109 // archive, look next to the archive path.
110 StringRef LocalPath =
111 !File->ParentName.empty() ? File->ParentName : File->getName();
112 Handler.addSearchPath(sys::path::parent_path(LocalPath));
113
Reid Kleckner44cdb102017-06-19 17:21:45 +0000114 BinaryByteStream Stream(Data, support::little);
115 CVTypeArray Types;
116 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000117 if (auto EC = Reader.readArray(Types, Reader.getLength()))
118 fatal(EC, "Reader::readArray failed");
Reid Kleckner6597c282017-07-13 20:12:23 +0000119 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable, TypeIndexMap,
120 &Handler, Types))
Reid Kleckner44cdb102017-06-19 17:21:45 +0000121 fatal(Err, "codeview::mergeTypeStreams failed");
122}
123
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000124static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
125 if (TI.isSimple())
126 return true;
127 if (TI.toArrayIndex() >= TypeIndexMap.size())
128 return false;
129 TI = TypeIndexMap[TI.toArrayIndex()];
130 return true;
131}
132
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000133static void remapTypesInSymbolRecord(ObjectFile *File,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000134 MutableArrayRef<uint8_t> Contents,
135 ArrayRef<TypeIndex> TypeIndexMap,
136 ArrayRef<TiReference> TypeRefs) {
137 for (const TiReference &Ref : TypeRefs) {
138 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000139 if (Contents.size() < Ref.Offset + ByteSize)
140 fatal("symbol record too short");
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000141 MutableArrayRef<TypeIndex> TIs(
142 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000143 for (TypeIndex &TI : TIs) {
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000144 if (!remapTypeIndex(TI, TypeIndexMap)) {
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000145 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000146 log("ignoring symbol record in " + File->getName() +
147 " with bad type index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000148 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000149 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000150 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000151 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000152}
153
154/// MSVC translates S_PROC_ID_END to S_END.
155uint16_t canonicalizeSymbolKind(SymbolKind Kind) {
156 if (Kind == SymbolKind::S_PROC_ID_END)
157 return SymbolKind::S_END;
158 return Kind;
159}
160
161/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
162/// The object file may not be aligned.
163static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
164 BumpPtrAllocator &Alloc) {
165 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
166 assert(Size >= 4 && "record too short");
167 assert(Size <= MaxRecordLength && "record too long");
168 void *Mem = Alloc.Allocate(Size, 4);
169
170 // Copy the symbol record and zero out any padding bytes.
171 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
172 memcpy(NewData.data(), Sym.data().data(), Sym.length());
173 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
174
175 // Update the record prefix length. It should point to the beginning of the
176 // next record. MSVC does some canonicalization of the record kind, so we do
177 // that as well.
178 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
179 Prefix->RecordKind = canonicalizeSymbolKind(Sym.kind());
180 Prefix->RecordLen = Size - 2;
181 return NewData;
182}
183
Reid Kleckner3f851922017-07-06 16:39:32 +0000184/// Return true if this symbol opens a scope. This implies that the symbol has
185/// "parent" and "end" fields, which contain the offset of the S_END or
186/// S_INLINESITE_END record.
187static bool symbolOpensScope(SymbolKind Kind) {
188 switch (Kind) {
189 case SymbolKind::S_GPROC32:
190 case SymbolKind::S_LPROC32:
191 case SymbolKind::S_LPROC32_ID:
192 case SymbolKind::S_GPROC32_ID:
193 case SymbolKind::S_BLOCK32:
194 case SymbolKind::S_SEPCODE:
195 case SymbolKind::S_THUNK32:
196 case SymbolKind::S_INLINESITE:
197 case SymbolKind::S_INLINESITE2:
198 return true;
199 default:
200 break;
201 }
202 return false;
203}
204
205static bool symbolEndsScope(SymbolKind Kind) {
206 switch (Kind) {
207 case SymbolKind::S_END:
208 case SymbolKind::S_PROC_ID_END:
209 case SymbolKind::S_INLINESITE_END:
210 return true;
211 default:
212 break;
213 }
214 return false;
215}
216
217struct ScopeRecord {
218 ulittle32_t PtrParent;
219 ulittle32_t PtrEnd;
220};
221
222struct SymbolScope {
223 ScopeRecord *OpeningRecord;
224 uint32_t ScopeOffset;
225};
226
227static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
228 uint32_t CurOffset, CVSymbol &Sym) {
229 assert(symbolOpensScope(Sym.kind()));
230 SymbolScope S;
231 S.ScopeOffset = CurOffset;
232 S.OpeningRecord = const_cast<ScopeRecord *>(
233 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
234 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
235 Stack.push_back(S);
236}
237
238static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
239 uint32_t CurOffset, ObjectFile *File) {
240 if (Stack.empty()) {
241 warn("symbol scopes are not balanced in " + File->getName());
242 return;
243 }
244 SymbolScope S = Stack.pop_back_val();
245 S.OpeningRecord->PtrEnd = CurOffset;
246}
247
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000248static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjectFile *File,
249 ArrayRef<TypeIndex> TypeIndexMap,
250 BinaryStreamRef SymData) {
251 // FIXME: Improve error recovery by warning and skipping records when
252 // possible.
253 CVSymbolArray Syms;
254 BinaryStreamReader Reader(SymData);
255 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000256 SmallVector<SymbolScope, 4> Scopes;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000257 for (const CVSymbol &Sym : Syms) {
258 // Discover type index references in the record. Skip it if we don't know
259 // where they are.
260 SmallVector<TiReference, 32> TypeRefs;
261 if (!discoverTypeIndices(Sym, TypeRefs)) {
262 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
263 continue;
264 }
265
266 // Copy the symbol record so we can mutate it.
267 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
268
269 // Re-map all the type index references.
270 MutableArrayRef<uint8_t> Contents =
271 NewData.drop_front(sizeof(RecordPrefix));
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000272 remapTypesInSymbolRecord(File, Contents, TypeIndexMap, TypeRefs);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000273
Reid Kleckner3f851922017-07-06 16:39:32 +0000274 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
275 CVSymbol NewSym(Sym.kind(), NewData);
276 if (symbolOpensScope(Sym.kind()))
277 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
278 else if (symbolEndsScope(Sym.kind()))
279 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000280
281 // Add the symbol to the module.
Reid Kleckner3f851922017-07-06 16:39:32 +0000282 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000283 }
284}
285
Reid Kleckner44cdb102017-06-19 17:21:45 +0000286// Allocate memory for a .debug$S section and relocate it.
287static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
288 SectionChunk *DebugChunk) {
289 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
290 assert(DebugChunk->OutputSectionOff == 0 &&
291 "debug sections should not be in output sections");
292 DebugChunk->writeTo(Buffer);
293 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
294 ".debug$S");
295}
296
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000297// Add all object files to the PDB. Merge .debug$T sections into IpiData and
298// TpiData.
Reid Kleckner44cdb102017-06-19 17:21:45 +0000299static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab,
300 pdb::PDBFileBuilder &Builder,
301 TypeTableBuilder &TypeTable,
302 TypeTableBuilder &IDTable) {
Zachary Turner2897e032017-05-25 21:16:03 +0000303 // Follow type servers. If the same type server is encountered more than
304 // once for this instance of `PDBTypeServerHandler` (for example if many
305 // object files reference the same TypeServer), the types from the
306 // TypeServer will only be visited once.
307 pdb::PDBTypeServerHandler Handler;
308
Reid Kleckner44cdb102017-06-19 17:21:45 +0000309 // PDBs use a single global string table for filenames in the file checksum
310 // table.
311 auto PDBStrTab = std::make_shared<DebugStringTableSubsection>();
312
Rui Ueyama52896622017-01-12 03:09:25 +0000313 // Visit all .debug$T sections to add them to Builder.
Rui Ueyama52896622017-01-12 03:09:25 +0000314 for (ObjectFile *File : Symtab->ObjectFiles) {
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000315 // Add a module descriptor for every object file. We need to put an absolute
316 // path to the object into the PDB. If this is a plain object, we make its
317 // path absolute. If it's an object in an archive, we make the archive path
318 // absolute.
319 bool InArchive = !File->ParentName.empty();
320 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
321 sys::fs::make_absolute(Path);
Zachary Turner448dea42017-07-07 18:46:14 +0000322 sys::path::native(Path, llvm::sys::path::Style::windows);
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000323 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner448dea42017-07-07 18:46:14 +0000324
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000325 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
326 File->ModuleDBI->setObjFileName(Path);
327
Reid Kleckner44cdb102017-06-19 17:21:45 +0000328 // Before we can process symbol substreams from .debug$S, we need to process
329 // type information, file checksums, and the string table. Add type info to
330 // the PDB first, so that we can get the map from object file type and item
331 // indices to PDB type and item indices.
332 SmallVector<TypeIndex, 128> TypeIndexMap;
333 mergeDebugT(File, IDTable, TypeTable, TypeIndexMap, Handler);
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000334
Reid Kleckner44cdb102017-06-19 17:21:45 +0000335 // Now do all line info.
336 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
Reid Klecknerf5bb7382017-06-20 17:14:09 +0000337 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
Reid Kleckner44cdb102017-06-19 17:21:45 +0000338 continue;
Rui Ueyama52896622017-01-12 03:09:25 +0000339
Reid Kleckner44cdb102017-06-19 17:21:45 +0000340 ArrayRef<uint8_t> RelocatedDebugContents =
341 relocateDebugChunk(Alloc, DebugChunk);
342 if (RelocatedDebugContents.empty())
343 continue;
344
345 DebugSubsectionArray Subsections;
346 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
347 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
348
349 DebugStringTableSubsectionRef CVStrTab;
350 DebugChecksumsSubsectionRef Checksums;
351 for (const DebugSubsectionRecord &SS : Subsections) {
352 switch (SS.kind()) {
353 case DebugSubsectionKind::StringTable:
354 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
355 break;
356 case DebugSubsectionKind::FileChecksums:
357 ExitOnErr(Checksums.initialize(SS.getRecordData()));
358 break;
359 case DebugSubsectionKind::Lines:
360 // We can add the relocated line table directly to the PDB without
361 // modification because the file checksum offsets will stay the same.
362 File->ModuleDBI->addDebugSubsection(SS);
363 break;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000364 case DebugSubsectionKind::Symbols:
365 mergeSymbolRecords(Alloc, File, TypeIndexMap, SS.getRecordData());
366 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000367 default:
368 // FIXME: Process the rest of the subsections.
369 break;
370 }
371 }
372
373 if (Checksums.valid()) {
374 // Make a new file checksum table that refers to offsets in the PDB-wide
375 // string table. Generally the string table subsection appears after the
376 // checksum table, so we have to do this after looping over all the
377 // subsections.
378 if (!CVStrTab.valid())
379 fatal(".debug$S sections must have both a string table subsection "
380 "and a checksum subsection table or neither");
381 auto NewChecksums =
Reid Kleckneradea0ce2017-06-19 17:27:31 +0000382 make_unique<DebugChecksumsSubsection>(*PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000383 for (FileChecksumEntry &FC : Checksums) {
384 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
385 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(
386 *File->ModuleDBI, FileName));
387 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
388 }
389 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
390 }
391 }
Rui Ueyama52896622017-01-12 03:09:25 +0000392 }
393
Reid Kleckner44cdb102017-06-19 17:21:45 +0000394 Builder.getStringTableBuilder().setStrings(*PDBStrTab);
395
Reid Kleckner5d577522017-03-24 17:26:38 +0000396 // Construct TPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000397 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
Reid Kleckner5d577522017-03-24 17:26:38 +0000398
399 // Construct IPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000400 addTypeInfo(Builder.getIpiBuilder(), IDTable);
Rui Ueyama52896622017-01-12 03:09:25 +0000401}
402
Zachary Turner6708e0b2017-07-10 21:01:37 +0000403static void addLinkerModuleSymbols(StringRef Path,
404 pdb::DbiModuleDescriptorBuilder &Mod,
405 BumpPtrAllocator &Allocator) {
406 codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb);
407 codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
408 codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym);
409 codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
410
411 ONS.Name = "* Linker *";
412 ONS.Signature = 0;
413
414 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
415 CS.Flags = CompileSym3Flags::None;
416 CS.VersionBackendBuild = 0;
417 CS.VersionBackendMajor = 0;
418 CS.VersionBackendMinor = 0;
419 CS.VersionBackendQFE = 0;
420 CS.VersionFrontendBuild = 0;
421 CS.VersionFrontendMajor = 0;
422 CS.VersionFrontendMinor = 0;
423 CS.VersionFrontendQFE = 0;
424 CS.Version = "LLVM Linker";
425 CS.setLanguage(SourceLanguage::Link);
426
427 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
428 std::string ArgStr = llvm::join(Args, " ");
429 EBS.Fields.push_back("cwd");
430 SmallString<64> cwd;
431 llvm::sys::fs::current_path(cwd);
432 EBS.Fields.push_back(cwd);
433 EBS.Fields.push_back("exe");
Zachary Turner77f23b92017-07-10 21:09:11 +0000434 EBS.Fields.push_back(Config->Argv[0]);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000435 EBS.Fields.push_back("pdb");
436 EBS.Fields.push_back(Path);
437 EBS.Fields.push_back("cmd");
438 EBS.Fields.push_back(ArgStr);
439 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
440 ONS, Allocator, CodeViewContainer::Pdb));
441 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
442 CS, Allocator, CodeViewContainer::Pdb));
443 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
444 EBS, Allocator, CodeViewContainer::Pdb));
445}
446
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000447// Creates a PDB file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000448void coff::createPDB(SymbolTable *Symtab, ArrayRef<uint8_t> SectionTable,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000449 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000450 BumpPtrAllocator Alloc;
451 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000452 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000453
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000454 // Create streams in MSF for predefined streams, namely
455 // PDB, TPI, DBI and IPI.
456 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
457 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000458
Rui Ueyamabb542b32016-09-16 22:51:17 +0000459 // Add an Info stream.
460 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000461 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
462
Zachary Turner6708e0b2017-07-10 21:01:37 +0000463 llvm::SmallString<128> NativePath(Config->PDBPath.begin(),
464 Config->PDBPath.end());
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000465 llvm::sys::fs::make_absolute(NativePath);
Zachary Turner448dea42017-07-07 18:46:14 +0000466 llvm::sys::path::native(NativePath, llvm::sys::path::Style::windows);
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000467
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000468 pdb::PDB_UniqueId uuid{};
469 if (DI)
470 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
471 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000472 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000473 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000474
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000475 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000476 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000477 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000478 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Rui Ueyama1343fac2016-10-06 22:52:01 +0000479
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000480 // It's not entirely clear what this is, but the * Linker * module uses it.
481 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
482
Reid Kleckner44cdb102017-06-19 17:21:45 +0000483 TypeTableBuilder TypeTable(BAlloc);
484 TypeTableBuilder IDTable(BAlloc);
485 addObjectsToPDB(Alloc, Symtab, Builder, TypeTable, IDTable);
Rui Ueyamad381c982016-10-05 21:37:25 +0000486
Zachary Turner7eaf1d92017-07-10 22:40:20 +0000487 // Add public and symbol records stream.
488
489 // For now we don't actually write any thing useful to the publics stream, but
490 // the act of "getting" it also creates it lazily so that we write an empty
491 // stream.
492 (void)Builder.getPublicsBuilder();
493
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000494 // Add Section Contributions.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000495 addSectionContribs(Symtab, DbiBuilder);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000496
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000497 // Add Section Map stream.
498 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000499 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000500 SectionTable.size() / sizeof(object::coff_section)};
501 std::vector<pdb::SecMapEntry> SectionMap =
502 pdb::DbiStreamBuilder::createSectionMap(Sections);
503 DbiBuilder.setSectionMap(SectionMap);
504
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000505 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
506 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000507 addLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +0000508
Rui Ueyama9f66f822016-10-11 19:45:07 +0000509 // Add COFF section header stream.
510 ExitOnErr(
511 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
512
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000513 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000514 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +0000515}