]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/PDB.cpp
Merge llvm trunk r321414 to contrib/llvm.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / PDB.cpp
1 //===- 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
10 #include "PDB.h"
11 #include "Chunks.h"
12 #include "Config.h"
13 #include "Driver.h"
14 #include "SymbolTable.h"
15 #include "Symbols.h"
16 #include "Writer.h"
17 #include "lld/Common/ErrorHandler.h"
18 #include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
19 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
20 #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
21 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
22 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
23 #include "llvm/DebugInfo/CodeView/RecordName.h"
24 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
25 #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
26 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
27 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
28 #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
29 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
30 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
31 #include "llvm/DebugInfo/MSF/MSFCommon.h"
32 #include "llvm/DebugInfo/PDB/GenericError.h"
33 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
34 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
35 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
36 #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
37 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
38 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
39 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
40 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
41 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
42 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
43 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
44 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
45 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
46 #include "llvm/DebugInfo/PDB/PDB.h"
47 #include "llvm/Object/COFF.h"
48 #include "llvm/Support/BinaryByteStream.h"
49 #include "llvm/Support/Endian.h"
50 #include "llvm/Support/JamCRC.h"
51 #include "llvm/Support/Path.h"
52 #include "llvm/Support/ScopedPrinter.h"
53 #include <memory>
54
55 using namespace lld;
56 using namespace lld::coff;
57 using namespace llvm;
58 using namespace llvm::codeview;
59
60 using llvm::object::coff_section;
61
62 static ExitOnError ExitOnErr;
63
64 namespace {
65 /// Map from type index and item index in a type server PDB to the
66 /// corresponding index in the destination PDB.
67 struct CVIndexMap {
68   SmallVector<TypeIndex, 0> TPIMap;
69   SmallVector<TypeIndex, 0> IPIMap;
70   bool IsTypeServerMap = false;
71 };
72
73 class PDBLinker {
74 public:
75   PDBLinker(SymbolTable *Symtab)
76       : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc),
77         IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) {}
78
79   /// Emit the basic PDB structure: initial streams, headers, etc.
80   void initialize(const llvm::codeview::DebugInfo &BuildId);
81
82   /// Link CodeView from each object file in the symbol table into the PDB.
83   void addObjectsToPDB();
84
85   /// Link CodeView from a single object file into the PDB.
86   void addObjFile(ObjFile *File);
87
88   /// Produce a mapping from the type and item indices used in the object
89   /// file to those in the destination PDB.
90   ///
91   /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
92   /// and IPI from the type server PDB and return a map for it. Each unique type
93   /// server PDB is merged at most once, so this may return an existing index
94   /// mapping.
95   ///
96   /// If the object does not use a type server PDB (compiled with /Z7), we merge
97   /// all the type and item records from the .debug$S stream and fill in the
98   /// caller-provided ObjectIndexMap.
99   const CVIndexMap &mergeDebugT(ObjFile *File, CVIndexMap &ObjectIndexMap);
100
101   const CVIndexMap &maybeMergeTypeServerPDB(ObjFile *File,
102                                             TypeServer2Record &TS);
103
104   /// Add the section map and section contributions to the PDB.
105   void addSections(ArrayRef<OutputSection *> OutputSections,
106                    ArrayRef<uint8_t> SectionTable);
107
108   void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
109                          OutputSection *OS, Chunk *C);
110
111   /// Write the PDB to disk.
112   void commit();
113
114 private:
115   BumpPtrAllocator Alloc;
116
117   SymbolTable *Symtab;
118
119   pdb::PDBFileBuilder Builder;
120
121   /// Type records that will go into the PDB TPI stream.
122   MergingTypeTableBuilder TypeTable;
123
124   /// Item records that will go into the PDB IPI stream.
125   MergingTypeTableBuilder IDTable;
126
127   /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH)
128   GlobalTypeTableBuilder GlobalTypeTable;
129
130   /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH)
131   GlobalTypeTableBuilder GlobalIDTable;
132
133   /// PDBs use a single global string table for filenames in the file checksum
134   /// table.
135   DebugStringTableSubsection PDBStrTab;
136
137   llvm::SmallString<128> NativePath;
138
139   std::vector<pdb::SecMapEntry> SectionMap;
140
141   /// Type index mappings of type server PDBs that we've loaded so far.
142   std::map<GUID, CVIndexMap> TypeServerIndexMappings;
143 };
144 }
145
146 static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
147                                 StringRef Name) {
148   for (SectionChunk *C : Sections)
149     if (C->getSectionName() == Name)
150       return C;
151   return nullptr;
152 }
153
154 static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
155                                            StringRef SecName) {
156   // First 4 bytes are section magic.
157   if (Data.size() < 4)
158     fatal(SecName + " too short");
159   if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
160     fatal(SecName + " has an invalid magic");
161   return Data.slice(4);
162 }
163
164 static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
165   if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
166     return consumeDebugMagic(Sec->getContents(), SecName);
167   return {};
168 }
169
170 // A COFF .debug$H section is currently a clang extension.  This function checks
171 // if a .debug$H section is in a format that we expect / understand, so that we
172 // can ignore any sections which are coincidentally also named .debug$H but do
173 // not contain a format we recognize.
174 static bool canUseDebugH(ArrayRef<uint8_t> DebugH) {
175   if (DebugH.size() < sizeof(object::debug_h_header))
176     return false;
177   auto *Header =
178       reinterpret_cast<const object::debug_h_header *>(DebugH.data());
179   DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
180   return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
181          Header->Version == 0 &&
182          Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1) &&
183          (DebugH.size() % 20 == 0);
184 }
185
186 static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) {
187   SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H");
188   if (!Sec)
189     return llvm::None;
190   ArrayRef<uint8_t> Contents = Sec->getContents();
191   if (!canUseDebugH(Contents))
192     return None;
193   return Contents;
194 }
195
196 static ArrayRef<GloballyHashedType>
197 getHashesFromDebugH(ArrayRef<uint8_t> DebugH) {
198   assert(canUseDebugH(DebugH));
199
200   DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
201   uint32_t Count = DebugH.size() / sizeof(GloballyHashedType);
202   return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count};
203 }
204
205 static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
206                         TypeCollection &TypeTable) {
207   // Start the TPI or IPI stream header.
208   TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
209
210   // Flatten the in memory type table and hash each type.
211   TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
212     auto Hash = pdb::hashTypeRecord(Type);
213     if (auto E = Hash.takeError())
214       fatal("type hashing error");
215     TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
216   });
217 }
218
219 static Optional<TypeServer2Record>
220 maybeReadTypeServerRecord(CVTypeArray &Types) {
221   auto I = Types.begin();
222   if (I == Types.end())
223     return None;
224   const CVType &Type = *I;
225   if (Type.kind() != LF_TYPESERVER2)
226     return None;
227   TypeServer2Record TS;
228   if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
229     fatal("error reading type server record: " + toString(std::move(EC)));
230   return std::move(TS);
231 }
232
233 const CVIndexMap &PDBLinker::mergeDebugT(ObjFile *File,
234                                          CVIndexMap &ObjectIndexMap) {
235   ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
236   if (Data.empty())
237     return ObjectIndexMap;
238
239   BinaryByteStream Stream(Data, support::little);
240   CVTypeArray Types;
241   BinaryStreamReader Reader(Stream);
242   if (auto EC = Reader.readArray(Types, Reader.getLength()))
243     fatal("Reader::readArray failed: " + toString(std::move(EC)));
244
245   // Look through type servers. If we've already seen this type server, don't
246   // merge any type information.
247   if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
248     return maybeMergeTypeServerPDB(File, *TS);
249
250   // This is a /Z7 object. Fill in the temporary, caller-provided
251   // ObjectIndexMap.
252   if (Config->DebugGHashes) {
253     ArrayRef<GloballyHashedType> Hashes;
254     std::vector<GloballyHashedType> OwnedHashes;
255     if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File))
256       Hashes = getHashesFromDebugH(*DebugH);
257     else {
258       OwnedHashes = GloballyHashedType::hashTypes(Types);
259       Hashes = OwnedHashes;
260     }
261
262     if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable,
263                                          ObjectIndexMap.TPIMap, Types, Hashes))
264       fatal("codeview::mergeTypeAndIdRecords failed: " +
265             toString(std::move(Err)));
266   } else {
267     if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
268                                          ObjectIndexMap.TPIMap, Types))
269       fatal("codeview::mergeTypeAndIdRecords failed: " +
270             toString(std::move(Err)));
271   }
272   return ObjectIndexMap;
273 }
274
275 static Expected<std::unique_ptr<pdb::NativeSession>>
276 tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
277   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
278       TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
279   if (!MBOrErr)
280     return errorCodeToError(MBOrErr.getError());
281
282   std::unique_ptr<pdb::IPDBSession> ThisSession;
283   if (auto EC = pdb::NativeSession::createFromPdb(
284           MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)),
285                                      /*RequiresNullTerminator=*/false),
286           ThisSession))
287     return std::move(EC);
288
289   std::unique_ptr<pdb::NativeSession> NS(
290       static_cast<pdb::NativeSession *>(ThisSession.release()));
291   pdb::PDBFile &File = NS->getPDBFile();
292   auto ExpectedInfo = File.getPDBInfoStream();
293   // All PDB Files should have an Info stream.
294   if (!ExpectedInfo)
295     return ExpectedInfo.takeError();
296
297   // Just because a file with a matching name was found and it was an actual
298   // PDB file doesn't mean it matches.  For it to match the InfoStream's GUID
299   // must match the GUID specified in the TypeServer2 record.
300   if (ExpectedInfo->getGuid() != GuidFromObj)
301     return make_error<pdb::GenericError>(
302         pdb::generic_error_code::type_server_not_found, TSPath);
303
304   return std::move(NS);
305 }
306
307 const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjFile *File,
308                                                      TypeServer2Record &TS) {
309   // First, check if we already loaded a PDB with this GUID. Return the type
310   // index mapping if we have it.
311   auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()});
312   CVIndexMap &IndexMap = Insertion.first->second;
313   if (!Insertion.second)
314     return IndexMap;
315
316   // Mark this map as a type server map.
317   IndexMap.IsTypeServerMap = true;
318
319   // Check for a PDB at:
320   // 1. The given file path
321   // 2. Next to the object file or archive file
322   auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName());
323   if (!ExpectedSession) {
324     consumeError(ExpectedSession.takeError());
325     StringRef LocalPath =
326         !File->ParentName.empty() ? File->ParentName : File->getName();
327     SmallString<128> Path = sys::path::parent_path(LocalPath);
328     sys::path::append(
329         Path, sys::path::filename(TS.getName(), sys::path::Style::windows));
330     ExpectedSession = tryToLoadPDB(TS.getGuid(), Path);
331   }
332   if (auto E = ExpectedSession.takeError())
333     fatal("Type server PDB was not found: " + toString(std::move(E)));
334
335   auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream();
336   if (auto E = ExpectedTpi.takeError())
337     fatal("Type server does not have TPI stream: " + toString(std::move(E)));
338   auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream();
339   if (auto E = ExpectedIpi.takeError())
340     fatal("Type server does not have TPI stream: " + toString(std::move(E)));
341
342   if (Config->DebugGHashes) {
343     // PDBs do not actually store global hashes, so when merging a type server
344     // PDB we have to synthesize global hashes.  To do this, we first synthesize
345     // global hashes for the TPI stream, since it is independent, then we
346     // synthesize hashes for the IPI stream, using the hashes for the TPI stream
347     // as inputs.
348     auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray());
349     auto IpiHashes =
350         GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes);
351
352     // Merge TPI first, because the IPI stream will reference type indices.
353     if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap,
354                                     ExpectedTpi->typeArray(), TpiHashes))
355       fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
356
357     // Merge IPI.
358     if (auto Err =
359             mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap,
360                            ExpectedIpi->typeArray(), IpiHashes))
361       fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
362   } else {
363     // Merge TPI first, because the IPI stream will reference type indices.
364     if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
365                                     ExpectedTpi->typeArray()))
366       fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
367
368     // Merge IPI.
369     if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
370                                   ExpectedIpi->typeArray()))
371       fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
372   }
373
374   return IndexMap;
375 }
376
377 static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
378   if (TI.isSimple())
379     return true;
380   if (TI.toArrayIndex() >= TypeIndexMap.size())
381     return false;
382   TI = TypeIndexMap[TI.toArrayIndex()];
383   return true;
384 }
385
386 static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
387                                      MutableArrayRef<uint8_t> Contents,
388                                      const CVIndexMap &IndexMap,
389                                      ArrayRef<TiReference> TypeRefs) {
390   for (const TiReference &Ref : TypeRefs) {
391     unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
392     if (Contents.size() < Ref.Offset + ByteSize)
393       fatal("symbol record too short");
394
395     // This can be an item index or a type index. Choose the appropriate map.
396     ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
397     bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
398     if (IsItemIndex && IndexMap.IsTypeServerMap)
399       TypeOrItemMap = IndexMap.IPIMap;
400
401     MutableArrayRef<TypeIndex> TIs(
402         reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
403     for (TypeIndex &TI : TIs) {
404       if (!remapTypeIndex(TI, TypeOrItemMap)) {
405         log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " +
406             File->getName() + " with bad " + (IsItemIndex ? "item" : "type") +
407             " index 0x" + utohexstr(TI.getIndex()));
408         TI = TypeIndex(SimpleTypeKind::NotTranslated);
409         continue;
410       }
411     }
412   }
413 }
414
415 static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
416   const RecordPrefix *Prefix =
417       reinterpret_cast<const RecordPrefix *>(RecordData.data());
418   return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
419 }
420
421 /// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
422 static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData,
423                                TypeCollection &IDTable) {
424   RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
425
426   SymbolKind Kind = symbolKind(RecordData);
427
428   if (Kind == SymbolKind::S_PROC_ID_END) {
429     Prefix->RecordKind = SymbolKind::S_END;
430     return;
431   }
432
433   // In an object file, GPROC32_ID has an embedded reference which refers to the
434   // single object file type index namespace.  This has already been translated
435   // to the PDB file's ID stream index space, but we need to convert this to a
436   // symbol that refers to the type stream index space.  So we remap again from
437   // ID index space to type index space.
438   if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) {
439     SmallVector<TiReference, 1> Refs;
440     auto Content = RecordData.drop_front(sizeof(RecordPrefix));
441     CVSymbol Sym(Kind, RecordData);
442     discoverTypeIndicesInSymbol(Sym, Refs);
443     assert(Refs.size() == 1);
444     assert(Refs.front().Count == 1);
445
446     TypeIndex *TI =
447         reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
448     // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
449     // the IPI stream, whose `FunctionType` member refers to the TPI stream.
450     // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
451     // in both cases we just need the second type index.
452     if (!TI->isSimple() && !TI->isNoneType()) {
453       CVType FuncIdData = IDTable.getType(*TI);
454       SmallVector<TypeIndex, 2> Indices;
455       discoverTypeIndices(FuncIdData, Indices);
456       assert(Indices.size() == 2);
457       *TI = Indices[1];
458     }
459
460     Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
461                                               : SymbolKind::S_LPROC32;
462     Prefix->RecordKind = uint16_t(Kind);
463   }
464 }
465
466 /// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
467 /// The object file may not be aligned.
468 static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
469                                                  BumpPtrAllocator &Alloc) {
470   size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
471   assert(Size >= 4 && "record too short");
472   assert(Size <= MaxRecordLength && "record too long");
473   void *Mem = Alloc.Allocate(Size, 4);
474
475   // Copy the symbol record and zero out any padding bytes.
476   MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
477   memcpy(NewData.data(), Sym.data().data(), Sym.length());
478   memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
479
480   // Update the record prefix length. It should point to the beginning of the
481   // next record.
482   auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
483   Prefix->RecordLen = Size - 2;
484   return NewData;
485 }
486
487 /// Return true if this symbol opens a scope. This implies that the symbol has
488 /// "parent" and "end" fields, which contain the offset of the S_END or
489 /// S_INLINESITE_END record.
490 static bool symbolOpensScope(SymbolKind Kind) {
491   switch (Kind) {
492   case SymbolKind::S_GPROC32:
493   case SymbolKind::S_LPROC32:
494   case SymbolKind::S_LPROC32_ID:
495   case SymbolKind::S_GPROC32_ID:
496   case SymbolKind::S_BLOCK32:
497   case SymbolKind::S_SEPCODE:
498   case SymbolKind::S_THUNK32:
499   case SymbolKind::S_INLINESITE:
500   case SymbolKind::S_INLINESITE2:
501     return true;
502   default:
503     break;
504   }
505   return false;
506 }
507
508 static bool symbolEndsScope(SymbolKind Kind) {
509   switch (Kind) {
510   case SymbolKind::S_END:
511   case SymbolKind::S_PROC_ID_END:
512   case SymbolKind::S_INLINESITE_END:
513     return true;
514   default:
515     break;
516   }
517   return false;
518 }
519
520 struct ScopeRecord {
521   ulittle32_t PtrParent;
522   ulittle32_t PtrEnd;
523 };
524
525 struct SymbolScope {
526   ScopeRecord *OpeningRecord;
527   uint32_t ScopeOffset;
528 };
529
530 static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
531                            uint32_t CurOffset, CVSymbol &Sym) {
532   assert(symbolOpensScope(Sym.kind()));
533   SymbolScope S;
534   S.ScopeOffset = CurOffset;
535   S.OpeningRecord = const_cast<ScopeRecord *>(
536       reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
537   S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
538   Stack.push_back(S);
539 }
540
541 static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
542                             uint32_t CurOffset, ObjFile *File) {
543   if (Stack.empty()) {
544     warn("symbol scopes are not balanced in " + File->getName());
545     return;
546   }
547   SymbolScope S = Stack.pop_back_val();
548   S.OpeningRecord->PtrEnd = CurOffset;
549 }
550
551 static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
552   switch (Sym.kind()) {
553   case SymbolKind::S_GDATA32:
554   case SymbolKind::S_CONSTANT:
555   case SymbolKind::S_UDT:
556   // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
557   // since they are synthesized by the linker in response to S_GPROC32 and
558   // S_LPROC32, but if we do see them, don't put them in the module stream I
559   // guess.
560   case SymbolKind::S_PROCREF:
561   case SymbolKind::S_LPROCREF:
562     return false;
563   // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
564   case SymbolKind::S_LDATA32:
565   default:
566     return true;
567   }
568 }
569
570 static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
571   switch (Sym.kind()) {
572   case SymbolKind::S_CONSTANT:
573   case SymbolKind::S_GDATA32:
574   // S_LDATA32 goes in both the module stream and the globals stream.
575   case SymbolKind::S_LDATA32:
576   case SymbolKind::S_GPROC32:
577   case SymbolKind::S_LPROC32:
578   // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
579   // since they are synthesized by the linker in response to S_GPROC32 and
580   // S_LPROC32, but if we do see them, copy them straight through.
581   case SymbolKind::S_PROCREF:
582   case SymbolKind::S_LPROCREF:
583     return true;
584   // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
585   // globals stream or the modules stream).  These have special handling which
586   // needs more investigation before we can get right, but by putting them all
587   // into the globals stream WinDbg fails to display local variables of class
588   // types saying that it cannot find the type Foo *.  So as a stopgap just to
589   // keep things working, we drop them.
590   case SymbolKind::S_UDT:
591   default:
592     return false;
593   }
594 }
595
596 static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
597                             const CVSymbol &Sym) {
598   switch (Sym.kind()) {
599   case SymbolKind::S_CONSTANT:
600   case SymbolKind::S_UDT:
601   case SymbolKind::S_GDATA32:
602   case SymbolKind::S_LDATA32:
603   case SymbolKind::S_PROCREF:
604   case SymbolKind::S_LPROCREF:
605     Builder.addGlobalSymbol(Sym);
606     break;
607   case SymbolKind::S_GPROC32:
608   case SymbolKind::S_LPROC32: {
609     SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
610     if (Sym.kind() == SymbolKind::S_LPROC32)
611       K = SymbolRecordKind::LocalProcRef;
612     ProcRefSym PS(K);
613     PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
614     // For some reason, MSVC seems to add one to this value.
615     ++PS.Module;
616     PS.Name = getSymbolName(Sym);
617     PS.SumName = 0;
618     PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
619     Builder.addGlobalSymbol(PS);
620     break;
621   }
622   default:
623     llvm_unreachable("Invalid symbol kind!");
624   }
625 }
626
627 static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
628                                pdb::GSIStreamBuilder &GsiBuilder,
629                                const CVIndexMap &IndexMap,
630                                TypeCollection &IDTable,
631                                BinaryStreamRef SymData) {
632   // FIXME: Improve error recovery by warning and skipping records when
633   // possible.
634   CVSymbolArray Syms;
635   BinaryStreamReader Reader(SymData);
636   ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
637   SmallVector<SymbolScope, 4> Scopes;
638   for (CVSymbol Sym : Syms) {
639     // Discover type index references in the record. Skip it if we don't know
640     // where they are.
641     SmallVector<TiReference, 32> TypeRefs;
642     if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) {
643       log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
644       continue;
645     }
646
647     // Copy the symbol record so we can mutate it.
648     MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
649
650     // Re-map all the type index references.
651     MutableArrayRef<uint8_t> Contents =
652         NewData.drop_front(sizeof(RecordPrefix));
653     remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap, TypeRefs);
654
655     // An object file may have S_xxx_ID symbols, but these get converted to
656     // "real" symbols in a PDB.
657     translateIdSymbols(NewData, IDTable);
658
659     SymbolKind NewKind = symbolKind(NewData);
660
661     // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
662     CVSymbol NewSym(NewKind, NewData);
663     if (symbolOpensScope(NewKind))
664       scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
665     else if (symbolEndsScope(NewKind))
666       scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
667
668     // Add the symbol to the globals stream if necessary.  Do this before adding
669     // the symbol to the module since we may need to get the next symbol offset,
670     // and writing to the module's symbol stream will update that offset.
671     if (symbolGoesInGlobalsStream(NewSym))
672       addGlobalSymbol(GsiBuilder, *File, NewSym);
673
674     // Add the symbol to the module.
675     if (symbolGoesInModuleStream(NewSym))
676       File->ModuleDBI->addSymbol(NewSym);
677   }
678 }
679
680 // Allocate memory for a .debug$S section and relocate it.
681 static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
682                                             SectionChunk *DebugChunk) {
683   uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
684   assert(DebugChunk->OutputSectionOff == 0 &&
685          "debug sections should not be in output sections");
686   DebugChunk->writeTo(Buffer);
687   return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
688                            ".debug$S");
689 }
690
691 void PDBLinker::addObjFile(ObjFile *File) {
692   // Add a module descriptor for every object file. We need to put an absolute
693   // path to the object into the PDB. If this is a plain object, we make its
694   // path absolute. If it's an object in an archive, we make the archive path
695   // absolute.
696   bool InArchive = !File->ParentName.empty();
697   SmallString<128> Path = InArchive ? File->ParentName : File->getName();
698   sys::fs::make_absolute(Path);
699   sys::path::native(Path, sys::path::Style::windows);
700   StringRef Name = InArchive ? File->getName() : StringRef(Path);
701
702   File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
703   File->ModuleDBI->setObjFileName(Path);
704
705   // Before we can process symbol substreams from .debug$S, we need to process
706   // type information, file checksums, and the string table.  Add type info to
707   // the PDB first, so that we can get the map from object file type and item
708   // indices to PDB type and item indices.
709   CVIndexMap ObjectIndexMap;
710   const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap);
711
712   // Now do all live .debug$S sections.
713   for (SectionChunk *DebugChunk : File->getDebugChunks()) {
714     if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
715       continue;
716
717     ArrayRef<uint8_t> RelocatedDebugContents =
718         relocateDebugChunk(Alloc, DebugChunk);
719     if (RelocatedDebugContents.empty())
720       continue;
721
722     DebugSubsectionArray Subsections;
723     BinaryStreamReader Reader(RelocatedDebugContents, support::little);
724     ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
725
726     DebugStringTableSubsectionRef CVStrTab;
727     DebugChecksumsSubsectionRef Checksums;
728     for (const DebugSubsectionRecord &SS : Subsections) {
729       switch (SS.kind()) {
730       case DebugSubsectionKind::StringTable:
731         ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
732         break;
733       case DebugSubsectionKind::FileChecksums:
734         ExitOnErr(Checksums.initialize(SS.getRecordData()));
735         break;
736       case DebugSubsectionKind::Lines:
737         // We can add the relocated line table directly to the PDB without
738         // modification because the file checksum offsets will stay the same.
739         File->ModuleDBI->addDebugSubsection(SS);
740         break;
741       case DebugSubsectionKind::Symbols:
742         if (Config->DebugGHashes) {
743           mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
744                              GlobalIDTable, SS.getRecordData());
745         } else {
746           mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
747                              IDTable, SS.getRecordData());
748         }
749         break;
750       default:
751         // FIXME: Process the rest of the subsections.
752         break;
753       }
754     }
755
756     if (Checksums.valid()) {
757       // Make a new file checksum table that refers to offsets in the PDB-wide
758       // string table. Generally the string table subsection appears after the
759       // checksum table, so we have to do this after looping over all the
760       // subsections.
761       if (!CVStrTab.valid())
762         fatal(".debug$S sections must have both a string table subsection "
763               "and a checksum subsection table or neither");
764       auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
765       for (FileChecksumEntry &FC : Checksums) {
766         StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
767         ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
768                                                               FileName));
769         NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
770       }
771       File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
772     }
773   }
774 }
775
776 static PublicSym32 createPublic(Defined *Def) {
777   PublicSym32 Pub(SymbolKind::S_PUB32);
778   Pub.Name = Def->getName();
779   if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
780     if (D->getCOFFSymbol().isFunctionDefinition())
781       Pub.Flags = PublicSymFlags::Function;
782   } else if (isa<DefinedImportThunk>(Def)) {
783     Pub.Flags = PublicSymFlags::Function;
784   }
785
786   OutputSection *OS = Def->getChunk()->getOutputSection();
787   assert(OS && "all publics should be in final image");
788   Pub.Offset = Def->getRVA() - OS->getRVA();
789   Pub.Segment = OS->SectionIndex;
790   return Pub;
791 }
792
793 // Add all object files to the PDB. Merge .debug$T sections into IpiData and
794 // TpiData.
795 void PDBLinker::addObjectsToPDB() {
796   for (ObjFile *File : ObjFile::Instances)
797     addObjFile(File);
798
799   Builder.getStringTableBuilder().setStrings(PDBStrTab);
800
801   // Construct TPI and IPI stream contents.
802   if (Config->DebugGHashes) {
803     addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
804     addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
805   } else {
806     addTypeInfo(Builder.getTpiBuilder(), TypeTable);
807     addTypeInfo(Builder.getIpiBuilder(), IDTable);
808   }
809
810   // Compute the public and global symbols.
811   auto &GsiBuilder = Builder.getGsiBuilder();
812   std::vector<PublicSym32> Publics;
813   Symtab->forEachSymbol([&Publics](Symbol *S) {
814     // Only emit defined, live symbols that have a chunk.
815     auto *Def = dyn_cast<Defined>(S);
816     if (Def && Def->isLive() && Def->getChunk())
817       Publics.push_back(createPublic(Def));
818   });
819
820   if (!Publics.empty()) {
821     // Sort the public symbols and add them to the stream.
822     std::sort(Publics.begin(), Publics.end(),
823               [](const PublicSym32 &L, const PublicSym32 &R) {
824                 return L.Name < R.Name;
825               });
826     for (const PublicSym32 &Pub : Publics)
827       GsiBuilder.addPublicSymbol(Pub);
828   }
829 }
830
831 static void addCommonLinkerModuleSymbols(StringRef Path,
832                                          pdb::DbiModuleDescriptorBuilder &Mod,
833                                          BumpPtrAllocator &Allocator) {
834   ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
835   Compile3Sym CS(SymbolRecordKind::Compile3Sym);
836   EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
837
838   ONS.Name = "* Linker *";
839   ONS.Signature = 0;
840
841   CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
842   // Interestingly, if we set the string to 0.0.0.0, then when trying to view
843   // local variables WinDbg emits an error that private symbols are not present.
844   // By setting this to a valid MSVC linker version string, local variables are
845   // displayed properly.   As such, even though it is not representative of
846   // LLVM's version information, we need this for compatibility.
847   CS.Flags = CompileSym3Flags::None;
848   CS.VersionBackendBuild = 25019;
849   CS.VersionBackendMajor = 14;
850   CS.VersionBackendMinor = 10;
851   CS.VersionBackendQFE = 0;
852
853   // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
854   // linker module (which is by definition a backend), so we don't need to do
855   // anything here.  Also, it seems we can use "LLVM Linker" for the linker name
856   // without any problems.  Only the backend version has to be hardcoded to a
857   // magic number.
858   CS.VersionFrontendBuild = 0;
859   CS.VersionFrontendMajor = 0;
860   CS.VersionFrontendMinor = 0;
861   CS.VersionFrontendQFE = 0;
862   CS.Version = "LLVM Linker";
863   CS.setLanguage(SourceLanguage::Link);
864
865   ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
866   std::string ArgStr = llvm::join(Args, " ");
867   EBS.Fields.push_back("cwd");
868   SmallString<64> cwd;
869   sys::fs::current_path(cwd);
870   EBS.Fields.push_back(cwd);
871   EBS.Fields.push_back("exe");
872   SmallString<64> exe = Config->Argv[0];
873   llvm::sys::fs::make_absolute(exe);
874   EBS.Fields.push_back(exe);
875   EBS.Fields.push_back("pdb");
876   EBS.Fields.push_back(Path);
877   EBS.Fields.push_back("cmd");
878   EBS.Fields.push_back(ArgStr);
879   Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
880       ONS, Allocator, CodeViewContainer::Pdb));
881   Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
882       CS, Allocator, CodeViewContainer::Pdb));
883   Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
884       EBS, Allocator, CodeViewContainer::Pdb));
885 }
886
887 static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
888                                          OutputSection &OS,
889                                          BumpPtrAllocator &Allocator) {
890   SectionSym Sym(SymbolRecordKind::SectionSym);
891   Sym.Alignment = 12; // 2^12 = 4KB
892   Sym.Characteristics = OS.getCharacteristics();
893   Sym.Length = OS.getVirtualSize();
894   Sym.Name = OS.getName();
895   Sym.Rva = OS.getRVA();
896   Sym.SectionNumber = OS.SectionIndex;
897   Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
898       Sym, Allocator, CodeViewContainer::Pdb));
899 }
900
901 // Creates a PDB file.
902 void coff::createPDB(SymbolTable *Symtab,
903                      ArrayRef<OutputSection *> OutputSections,
904                      ArrayRef<uint8_t> SectionTable,
905                      const llvm::codeview::DebugInfo &BuildId) {
906   PDBLinker PDB(Symtab);
907   PDB.initialize(BuildId);
908   PDB.addObjectsToPDB();
909   PDB.addSections(OutputSections, SectionTable);
910   PDB.commit();
911 }
912
913 void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) {
914   ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
915
916   // Create streams in MSF for predefined streams, namely
917   // PDB, TPI, DBI and IPI.
918   for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
919     ExitOnErr(Builder.getMsfBuilder().addStream(0));
920
921   // Add an Info stream.
922   auto &InfoBuilder = Builder.getInfoBuilder();
923   InfoBuilder.setAge(BuildId.PDB70.Age);
924
925   GUID uuid;
926   memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid));
927   InfoBuilder.setGuid(uuid);
928   InfoBuilder.setSignature(time(nullptr));
929   InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
930
931   // Add an empty DBI stream.
932   pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
933   DbiBuilder.setAge(BuildId.PDB70.Age);
934   DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
935   ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
936 }
937
938 void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
939                                   OutputSection *OS, Chunk *C) {
940   pdb::SectionContrib SC;
941   memset(&SC, 0, sizeof(SC));
942   SC.ISect = OS->SectionIndex;
943   SC.Off = C->getRVA() - OS->getRVA();
944   SC.Size = C->getSize();
945   if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
946     SC.Characteristics = SecChunk->Header->Characteristics;
947     SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
948     ArrayRef<uint8_t> Contents = SecChunk->getContents();
949     JamCRC CRC(0);
950     ArrayRef<char> CharContents = makeArrayRef(
951         reinterpret_cast<const char *>(Contents.data()), Contents.size());
952     CRC.update(CharContents);
953     SC.DataCrc = CRC.getCRC();
954   } else {
955     SC.Characteristics = OS->getCharacteristics();
956     // FIXME: When we start creating DBI for import libraries, use those here.
957     SC.Imod = LinkerModule.getModuleIndex();
958   }
959   SC.RelocCrc = 0; // FIXME
960   Builder.getDbiBuilder().addSectionContrib(SC);
961 }
962
963 void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
964                             ArrayRef<uint8_t> SectionTable) {
965   // It's not entirely clear what this is, but the * Linker * module uses it.
966   pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
967   NativePath = Config->PDBPath;
968   sys::fs::make_absolute(NativePath);
969   sys::path::native(NativePath, sys::path::Style::windows);
970   uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
971   auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
972   LinkerModule.setPdbFilePathNI(PdbFilePathNI);
973   addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
974
975   // Add section contributions. They must be ordered by ascending RVA.
976   for (OutputSection *OS : OutputSections) {
977     addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
978     for (Chunk *C : OS->getChunks())
979       addSectionContrib(LinkerModule, OS, C);
980   }
981
982   // Add Section Map stream.
983   ArrayRef<object::coff_section> Sections = {
984       (const object::coff_section *)SectionTable.data(),
985       SectionTable.size() / sizeof(object::coff_section)};
986   SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
987   DbiBuilder.setSectionMap(SectionMap);
988
989   // Add COFF section header stream.
990   ExitOnErr(
991       DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
992 }
993
994 void PDBLinker::commit() {
995   // Write to a file.
996   ExitOnErr(Builder.commit(Config->PDBPath));
997 }