xref: /freebsd/contrib/llvm-project/lld/COFF/PDB.cpp (revision e17f5b1d)
1 //===- PDB.cpp ------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "PDB.h"
10 #include "Chunks.h"
11 #include "Config.h"
12 #include "DebugTypes.h"
13 #include "Driver.h"
14 #include "SymbolTable.h"
15 #include "Symbols.h"
16 #include "TypeMerger.h"
17 #include "Writer.h"
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Threads.h"
20 #include "lld/Common/Timer.h"
21 #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
22 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
23 #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
24 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
25 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
26 #include "llvm/DebugInfo/CodeView/RecordName.h"
27 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
28 #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
29 #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
30 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
31 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
32 #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
33 #include "llvm/DebugInfo/CodeView/TypeRecordHelpers.h"
34 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
35 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
36 #include "llvm/DebugInfo/MSF/MSFCommon.h"
37 #include "llvm/DebugInfo/PDB/GenericError.h"
38 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
39 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
40 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
41 #include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
42 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
43 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
44 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
45 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
46 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
47 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
48 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
49 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
50 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
51 #include "llvm/DebugInfo/PDB/PDB.h"
52 #include "llvm/Object/COFF.h"
53 #include "llvm/Object/CVDebugRecord.h"
54 #include "llvm/Support/BinaryByteStream.h"
55 #include "llvm/Support/CRC.h"
56 #include "llvm/Support/Endian.h"
57 #include "llvm/Support/Errc.h"
58 #include "llvm/Support/FormatAdapters.h"
59 #include "llvm/Support/FormatVariadic.h"
60 #include "llvm/Support/Path.h"
61 #include "llvm/Support/ScopedPrinter.h"
62 #include <memory>
63 
64 using namespace llvm;
65 using namespace llvm::codeview;
66 
67 using llvm::object::coff_section;
68 
69 namespace lld {
70 namespace coff {
71 
72 static ExitOnError exitOnErr;
73 
74 static Timer totalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root());
75 
76 static Timer addObjectsTimer("Add Objects", totalPdbLinkTimer);
77 static Timer typeMergingTimer("Type Merging", addObjectsTimer);
78 static Timer symbolMergingTimer("Symbol Merging", addObjectsTimer);
79 static Timer globalsLayoutTimer("Globals Stream Layout", totalPdbLinkTimer);
80 static Timer tpiStreamLayoutTimer("TPI Stream Layout", totalPdbLinkTimer);
81 static Timer diskCommitTimer("Commit to Disk", totalPdbLinkTimer);
82 
83 namespace {
84 class DebugSHandler;
85 
86 class PDBLinker {
87   friend DebugSHandler;
88 
89 public:
90   PDBLinker(SymbolTable *symtab)
91       : alloc(), symtab(symtab), builder(alloc), tMerger(alloc) {
92     // This isn't strictly necessary, but link.exe usually puts an empty string
93     // as the first "valid" string in the string table, so we do the same in
94     // order to maintain as much byte-for-byte compatibility as possible.
95     pdbStrTab.insert("");
96   }
97 
98   /// Emit the basic PDB structure: initial streams, headers, etc.
99   void initialize(llvm::codeview::DebugInfo *buildId);
100 
101   /// Add natvis files specified on the command line.
102   void addNatvisFiles();
103 
104   /// Link CodeView from each object file in the symbol table into the PDB.
105   void addObjectsToPDB();
106 
107   /// Link info for each import file in the symbol table into the PDB.
108   void addImportFilesToPDB(ArrayRef<OutputSection *> outputSections);
109 
110   /// Link CodeView from a single object file into the target (output) PDB.
111   /// When a precompiled headers object is linked, its TPI map might be provided
112   /// externally.
113   void addObjFile(ObjFile *file, CVIndexMap *externIndexMap = nullptr);
114 
115   /// Produce a mapping from the type and item indices used in the object
116   /// file to those in the destination PDB.
117   ///
118   /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
119   /// and IPI from the type server PDB and return a map for it. Each unique type
120   /// server PDB is merged at most once, so this may return an existing index
121   /// mapping.
122   ///
123   /// If the object does not use a type server PDB (compiled with /Z7), we merge
124   /// all the type and item records from the .debug$S stream and fill in the
125   /// caller-provided objectIndexMap.
126   Expected<const CVIndexMap &> mergeDebugT(ObjFile *file,
127                                            CVIndexMap *objectIndexMap);
128 
129   /// Reads and makes available a PDB.
130   Expected<const CVIndexMap &> maybeMergeTypeServerPDB(ObjFile *file);
131 
132   /// Merges a precompiled headers TPI map into the current TPI map. The
133   /// precompiled headers object will also be loaded and remapped in the
134   /// process.
135   Error mergeInPrecompHeaderObj(ObjFile *file, CVIndexMap *objectIndexMap);
136 
137   /// Reads and makes available a precompiled headers object.
138   ///
139   /// This is a requirement for objects compiled with cl.exe /Yu. In that
140   /// case, the referenced object (which was compiled with /Yc) has to be loaded
141   /// first. This is mainly because the current object's TPI stream has external
142   /// references to the precompiled headers object.
143   ///
144   /// If the precompiled headers object was already loaded, this function will
145   /// simply return its (remapped) TPI map.
146   Expected<const CVIndexMap &> aquirePrecompObj(ObjFile *file);
147 
148   /// Adds a precompiled headers object signature -> TPI mapping.
149   std::pair<CVIndexMap &, bool /*already there*/>
150   registerPrecompiledHeaders(uint32_t signature);
151 
152   void mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap,
153                           std::vector<ulittle32_t *> &stringTableRefs,
154                           BinaryStreamRef symData);
155 
156   /// Add the section map and section contributions to the PDB.
157   void addSections(ArrayRef<OutputSection *> outputSections,
158                    ArrayRef<uint8_t> sectionTable);
159 
160   /// Write the PDB to disk and store the Guid generated for it in *Guid.
161   void commit(codeview::GUID *guid);
162 
163   // Print statistics regarding the final PDB
164   void printStats();
165 
166 private:
167   BumpPtrAllocator alloc;
168 
169   SymbolTable *symtab;
170 
171   pdb::PDBFileBuilder builder;
172 
173   TypeMerger tMerger;
174 
175   /// PDBs use a single global string table for filenames in the file checksum
176   /// table.
177   DebugStringTableSubsection pdbStrTab;
178 
179   llvm::SmallString<128> nativePath;
180 
181   std::vector<pdb::SecMapEntry> sectionMap;
182 
183   /// Type index mappings of type server PDBs that we've loaded so far.
184   std::map<codeview::GUID, CVIndexMap> typeServerIndexMappings;
185 
186   /// Type index mappings of precompiled objects type map that we've loaded so
187   /// far.
188   std::map<uint32_t, CVIndexMap> precompTypeIndexMappings;
189 
190   // For statistics
191   uint64_t globalSymbols = 0;
192   uint64_t moduleSymbols = 0;
193   uint64_t publicSymbols = 0;
194 
195   // When showSummary is enabled, these are histograms of TPI and IPI records
196   // keyed by type index.
197   SmallVector<uint32_t, 0> tpiCounts;
198   SmallVector<uint32_t, 0> ipiCounts;
199 };
200 
201 class DebugSHandler {
202   PDBLinker &linker;
203 
204   /// The object file whose .debug$S sections we're processing.
205   ObjFile &file;
206 
207   /// The result of merging type indices.
208   const CVIndexMap &indexMap;
209 
210   /// The DEBUG_S_STRINGTABLE subsection.  These strings are referred to by
211   /// index from other records in the .debug$S section.  All of these strings
212   /// need to be added to the global PDB string table, and all references to
213   /// these strings need to have their indices re-written to refer to the
214   /// global PDB string table.
215   DebugStringTableSubsectionRef cVStrTab;
216 
217   /// The DEBUG_S_FILECHKSMS subsection.  As above, these are referred to
218   /// by other records in the .debug$S section and need to be merged into the
219   /// PDB.
220   DebugChecksumsSubsectionRef checksums;
221 
222   /// The DEBUG_S_INLINEELINES subsection. There can be only one of these per
223   /// object file.
224   DebugInlineeLinesSubsectionRef inlineeLines;
225 
226   /// The DEBUG_S_FRAMEDATA subsection(s).  There can be more than one of
227   /// these and they need not appear in any specific order.  However, they
228   /// contain string table references which need to be re-written, so we
229   /// collect them all here and re-write them after all subsections have been
230   /// discovered and processed.
231   std::vector<DebugFrameDataSubsectionRef> newFpoFrames;
232 
233   /// Pointers to raw memory that we determine have string table references
234   /// that need to be re-written.  We first process all .debug$S subsections
235   /// to ensure that we can handle subsections written in any order, building
236   /// up this list as we go.  At the end, we use the string table (which must
237   /// have been discovered by now else it is an error) to re-write these
238   /// references.
239   std::vector<ulittle32_t *> stringTableReferences;
240 
241 public:
242   DebugSHandler(PDBLinker &linker, ObjFile &file, const CVIndexMap &indexMap)
243       : linker(linker), file(file), indexMap(indexMap) {}
244 
245   void handleDebugS(lld::coff::SectionChunk &debugS);
246 
247   std::shared_ptr<DebugInlineeLinesSubsection>
248   mergeInlineeLines(DebugChecksumsSubsection *newChecksums);
249 
250   void finish();
251 };
252 }
253 
254 // Visual Studio's debugger requires absolute paths in various places in the
255 // PDB to work without additional configuration:
256 // https://docs.microsoft.com/en-us/visualstudio/debugger/debug-source-files-common-properties-solution-property-pages-dialog-box
257 static void pdbMakeAbsolute(SmallVectorImpl<char> &fileName) {
258   // The default behavior is to produce paths that are valid within the context
259   // of the machine that you perform the link on.  If the linker is running on
260   // a POSIX system, we will output absolute POSIX paths.  If the linker is
261   // running on a Windows system, we will output absolute Windows paths.  If the
262   // user desires any other kind of behavior, they should explicitly pass
263   // /pdbsourcepath, in which case we will treat the exact string the user
264   // passed in as the gospel and not normalize, canonicalize it.
265   if (sys::path::is_absolute(fileName, sys::path::Style::windows) ||
266       sys::path::is_absolute(fileName, sys::path::Style::posix))
267     return;
268 
269   // It's not absolute in any path syntax.  Relative paths necessarily refer to
270   // the local file system, so we can make it native without ending up with a
271   // nonsensical path.
272   if (config->pdbSourcePath.empty()) {
273     sys::path::native(fileName);
274     sys::fs::make_absolute(fileName);
275     return;
276   }
277 
278   // Try to guess whether /PDBSOURCEPATH is a unix path or a windows path.
279   // Since PDB's are more of a Windows thing, we make this conservative and only
280   // decide that it's a unix path if we're fairly certain.  Specifically, if
281   // it starts with a forward slash.
282   SmallString<128> absoluteFileName = config->pdbSourcePath;
283   sys::path::Style guessedStyle = absoluteFileName.startswith("/")
284                                       ? sys::path::Style::posix
285                                       : sys::path::Style::windows;
286   sys::path::append(absoluteFileName, guessedStyle, fileName);
287   sys::path::native(absoluteFileName, guessedStyle);
288   sys::path::remove_dots(absoluteFileName, true, guessedStyle);
289 
290   fileName = std::move(absoluteFileName);
291 }
292 
293 // A COFF .debug$H section is currently a clang extension.  This function checks
294 // if a .debug$H section is in a format that we expect / understand, so that we
295 // can ignore any sections which are coincidentally also named .debug$H but do
296 // not contain a format we recognize.
297 static bool canUseDebugH(ArrayRef<uint8_t> debugH) {
298   if (debugH.size() < sizeof(object::debug_h_header))
299     return false;
300   auto *header =
301       reinterpret_cast<const object::debug_h_header *>(debugH.data());
302   debugH = debugH.drop_front(sizeof(object::debug_h_header));
303   return header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
304          header->Version == 0 &&
305          header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) &&
306          (debugH.size() % 8 == 0);
307 }
308 
309 static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *file) {
310   SectionChunk *sec =
311       SectionChunk::findByName(file->getDebugChunks(), ".debug$H");
312   if (!sec)
313     return llvm::None;
314   ArrayRef<uint8_t> contents = sec->getContents();
315   if (!canUseDebugH(contents))
316     return None;
317   return contents;
318 }
319 
320 static ArrayRef<GloballyHashedType>
321 getHashesFromDebugH(ArrayRef<uint8_t> debugH) {
322   assert(canUseDebugH(debugH));
323 
324   debugH = debugH.drop_front(sizeof(object::debug_h_header));
325   uint32_t count = debugH.size() / sizeof(GloballyHashedType);
326   return {reinterpret_cast<const GloballyHashedType *>(debugH.data()), count};
327 }
328 
329 static void addTypeInfo(pdb::TpiStreamBuilder &tpiBuilder,
330                         TypeCollection &typeTable) {
331   // Start the TPI or IPI stream header.
332   tpiBuilder.setVersionHeader(pdb::PdbTpiV80);
333 
334   // Flatten the in memory type table and hash each type.
335   typeTable.ForEachRecord([&](TypeIndex ti, const CVType &type) {
336     auto hash = pdb::hashTypeRecord(type);
337     if (auto e = hash.takeError())
338       fatal("type hashing error");
339     tpiBuilder.addTypeRecord(type.RecordData, *hash);
340   });
341 }
342 
343 Expected<const CVIndexMap &>
344 PDBLinker::mergeDebugT(ObjFile *file, CVIndexMap *objectIndexMap) {
345   ScopedTimer t(typeMergingTimer);
346 
347   if (!file->debugTypesObj)
348     return *objectIndexMap; // no Types stream
349 
350   // Precompiled headers objects need to save the index map for further
351   // reference by other objects which use the precompiled headers.
352   if (file->debugTypesObj->kind == TpiSource::PCH) {
353     uint32_t pchSignature = file->pchSignature.getValueOr(0);
354     if (pchSignature == 0)
355       fatal("No signature found for the precompiled headers OBJ (" +
356             file->getName() + ")");
357 
358     // When a precompiled headers object comes first on the command-line, we
359     // update the mapping here. Otherwise, if an object referencing the
360     // precompiled headers object comes first, the mapping is created in
361     // aquirePrecompObj(), thus we would skip this block.
362     if (!objectIndexMap->isPrecompiledTypeMap) {
363       auto r = registerPrecompiledHeaders(pchSignature);
364       if (r.second)
365         fatal(
366             "A precompiled headers OBJ with the same signature was already "
367             "provided! (" +
368             file->getName() + ")");
369 
370       objectIndexMap = &r.first;
371     }
372   }
373 
374   if (file->debugTypesObj->kind == TpiSource::UsingPDB) {
375     // Look through type servers. If we've already seen this type server,
376     // don't merge any type information.
377     return maybeMergeTypeServerPDB(file);
378   }
379 
380   CVTypeArray types;
381   BinaryStreamReader reader(file->debugTypes, support::little);
382   cantFail(reader.readArray(types, reader.getLength()));
383 
384   if (file->debugTypesObj->kind == TpiSource::UsingPCH) {
385     // This object was compiled with /Yu, so process the corresponding
386     // precompiled headers object (/Yc) first. Some type indices in the current
387     // object are referencing data in the precompiled headers object, so we need
388     // both to be loaded.
389     Error e = mergeInPrecompHeaderObj(file, objectIndexMap);
390     if (e)
391       return std::move(e);
392 
393     // Drop LF_PRECOMP record from the input stream, as it has been replaced
394     // with the precompiled headers Type stream in the mergeInPrecompHeaderObj()
395     // call above. Note that we can't just call Types.drop_front(), as we
396     // explicitly want to rebase the stream.
397     CVTypeArray::Iterator firstType = types.begin();
398     types.setUnderlyingStream(
399         types.getUnderlyingStream().drop_front(firstType->RecordData.size()));
400   }
401 
402   // Fill in the temporary, caller-provided ObjectIndexMap.
403   if (config->debugGHashes) {
404     ArrayRef<GloballyHashedType> hashes;
405     std::vector<GloballyHashedType> ownedHashes;
406     if (Optional<ArrayRef<uint8_t>> debugH = getDebugH(file))
407       hashes = getHashesFromDebugH(*debugH);
408     else {
409       ownedHashes = GloballyHashedType::hashTypes(types);
410       hashes = ownedHashes;
411     }
412 
413     if (auto err = mergeTypeAndIdRecords(
414             tMerger.globalIDTable, tMerger.globalTypeTable,
415             objectIndexMap->tpiMap, types, hashes, file->pchSignature))
416       fatal("codeview::mergeTypeAndIdRecords failed: " +
417             toString(std::move(err)));
418   } else {
419     if (auto err = mergeTypeAndIdRecords(tMerger.iDTable, tMerger.typeTable,
420                                          objectIndexMap->tpiMap, types,
421                                          file->pchSignature))
422       fatal("codeview::mergeTypeAndIdRecords failed: " +
423             toString(std::move(err)));
424   }
425 
426   if (config->showSummary) {
427     // Count how many times we saw each type record in our input. This
428     // calculation requires a second pass over the type records to classify each
429     // record as a type or index. This is slow, but this code executes when
430     // collecting statistics.
431     tpiCounts.resize(tMerger.getTypeTable().size());
432     ipiCounts.resize(tMerger.getIDTable().size());
433     uint32_t srcIdx = 0;
434     for (CVType &ty : types) {
435       TypeIndex dstIdx = objectIndexMap->tpiMap[srcIdx++];
436       // Type merging may fail, so a complex source type may become the simple
437       // NotTranslated type, which cannot be used as an array index.
438       if (dstIdx.isSimple())
439         continue;
440       SmallVectorImpl<uint32_t> &counts =
441           isIdRecord(ty.kind()) ? ipiCounts : tpiCounts;
442       ++counts[dstIdx.toArrayIndex()];
443     }
444   }
445 
446   return *objectIndexMap;
447 }
448 
449 Expected<const CVIndexMap &> PDBLinker::maybeMergeTypeServerPDB(ObjFile *file) {
450   Expected<llvm::pdb::NativeSession *> pdbSession = findTypeServerSource(file);
451   if (!pdbSession)
452     return pdbSession.takeError();
453 
454   pdb::PDBFile &pdbFile = pdbSession.get()->getPDBFile();
455   pdb::InfoStream &info = cantFail(pdbFile.getPDBInfoStream());
456 
457   auto it = typeServerIndexMappings.emplace(info.getGuid(), CVIndexMap());
458   CVIndexMap &indexMap = it.first->second;
459   if (!it.second)
460     return indexMap; // already merged
461 
462   // Mark this map as a type server map.
463   indexMap.isTypeServerMap = true;
464 
465   Expected<pdb::TpiStream &> expectedTpi = pdbFile.getPDBTpiStream();
466   if (auto e = expectedTpi.takeError())
467     fatal("Type server does not have TPI stream: " + toString(std::move(e)));
468   pdb::TpiStream *maybeIpi = nullptr;
469   if (pdbFile.hasPDBIpiStream()) {
470     Expected<pdb::TpiStream &> expectedIpi = pdbFile.getPDBIpiStream();
471     if (auto e = expectedIpi.takeError())
472       fatal("Error getting type server IPI stream: " + toString(std::move(e)));
473     maybeIpi = &*expectedIpi;
474   }
475 
476   if (config->debugGHashes) {
477     // PDBs do not actually store global hashes, so when merging a type server
478     // PDB we have to synthesize global hashes.  To do this, we first synthesize
479     // global hashes for the TPI stream, since it is independent, then we
480     // synthesize hashes for the IPI stream, using the hashes for the TPI stream
481     // as inputs.
482     auto tpiHashes = GloballyHashedType::hashTypes(expectedTpi->typeArray());
483     Optional<uint32_t> endPrecomp;
484     // Merge TPI first, because the IPI stream will reference type indices.
485     if (auto err =
486             mergeTypeRecords(tMerger.globalTypeTable, indexMap.tpiMap,
487                              expectedTpi->typeArray(), tpiHashes, endPrecomp))
488       fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err)));
489 
490     // Merge IPI.
491     if (maybeIpi) {
492       auto ipiHashes =
493           GloballyHashedType::hashIds(maybeIpi->typeArray(), tpiHashes);
494       if (auto err =
495               mergeIdRecords(tMerger.globalIDTable, indexMap.tpiMap,
496                              indexMap.ipiMap, maybeIpi->typeArray(), ipiHashes))
497         fatal("codeview::mergeIdRecords failed: " + toString(std::move(err)));
498     }
499   } else {
500     // Merge TPI first, because the IPI stream will reference type indices.
501     if (auto err = mergeTypeRecords(tMerger.typeTable, indexMap.tpiMap,
502                                     expectedTpi->typeArray()))
503       fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err)));
504 
505     // Merge IPI.
506     if (maybeIpi) {
507       if (auto err = mergeIdRecords(tMerger.iDTable, indexMap.tpiMap,
508                                     indexMap.ipiMap, maybeIpi->typeArray()))
509         fatal("codeview::mergeIdRecords failed: " + toString(std::move(err)));
510     }
511   }
512 
513   if (config->showSummary) {
514     // Count how many times we saw each type record in our input. If a
515     // destination type index is present in the source to destination type index
516     // map, that means we saw it once in the input. Add it to our histogram.
517     tpiCounts.resize(tMerger.getTypeTable().size());
518     ipiCounts.resize(tMerger.getIDTable().size());
519     for (TypeIndex ti : indexMap.tpiMap)
520       if (!ti.isSimple())
521         ++tpiCounts[ti.toArrayIndex()];
522     for (TypeIndex ti : indexMap.ipiMap)
523       if (!ti.isSimple())
524         ++ipiCounts[ti.toArrayIndex()];
525   }
526 
527   return indexMap;
528 }
529 
530 Error PDBLinker::mergeInPrecompHeaderObj(ObjFile *file,
531                                          CVIndexMap *objectIndexMap) {
532   const PrecompRecord &precomp =
533       retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj);
534 
535   Expected<const CVIndexMap &> e = aquirePrecompObj(file);
536   if (!e)
537     return e.takeError();
538 
539   const CVIndexMap &precompIndexMap = *e;
540   assert(precompIndexMap.isPrecompiledTypeMap);
541 
542   if (precompIndexMap.tpiMap.empty())
543     return Error::success();
544 
545   assert(precomp.getStartTypeIndex() == TypeIndex::FirstNonSimpleIndex);
546   assert(precomp.getTypesCount() <= precompIndexMap.tpiMap.size());
547   // Use the previously remapped index map from the precompiled headers.
548   objectIndexMap->tpiMap.append(precompIndexMap.tpiMap.begin(),
549                                 precompIndexMap.tpiMap.begin() +
550                                     precomp.getTypesCount());
551   return Error::success();
552 }
553 
554 static bool equals_path(StringRef path1, StringRef path2) {
555 #if defined(_WIN32)
556   return path1.equals_lower(path2);
557 #else
558   return path1.equals(path2);
559 #endif
560 }
561 // Find by name an OBJ provided on the command line
562 static ObjFile *findObjWithPrecompSignature(StringRef fileNameOnly,
563                                             uint32_t precompSignature) {
564   for (ObjFile *f : ObjFile::instances) {
565     StringRef currentFileName = sys::path::filename(f->getName());
566 
567     if (f->pchSignature.hasValue() &&
568         f->pchSignature.getValue() == precompSignature &&
569         equals_path(fileNameOnly, currentFileName))
570       return f;
571   }
572   return nullptr;
573 }
574 
575 std::pair<CVIndexMap &, bool /*already there*/>
576 PDBLinker::registerPrecompiledHeaders(uint32_t signature) {
577   auto insertion = precompTypeIndexMappings.insert({signature, CVIndexMap()});
578   CVIndexMap &indexMap = insertion.first->second;
579   if (!insertion.second)
580     return {indexMap, true};
581   // Mark this map as a precompiled types map.
582   indexMap.isPrecompiledTypeMap = true;
583   return {indexMap, false};
584 }
585 
586 Expected<const CVIndexMap &> PDBLinker::aquirePrecompObj(ObjFile *file) {
587   const PrecompRecord &precomp =
588       retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj);
589 
590   // First, check if we already loaded the precompiled headers object with this
591   // signature. Return the type index mapping if we've already seen it.
592   auto r = registerPrecompiledHeaders(precomp.getSignature());
593   if (r.second)
594     return r.first;
595 
596   CVIndexMap &indexMap = r.first;
597 
598   // Cross-compile warning: given that Clang doesn't generate LF_PRECOMP
599   // records, we assume the OBJ comes from a Windows build of cl.exe. Thusly,
600   // the paths embedded in the OBJs are in the Windows format.
601   SmallString<128> precompFileName = sys::path::filename(
602       precomp.getPrecompFilePath(), sys::path::Style::windows);
603 
604   // link.exe requires that a precompiled headers object must always be provided
605   // on the command-line, even if that's not necessary.
606   auto precompFile =
607       findObjWithPrecompSignature(precompFileName, precomp.Signature);
608   if (!precompFile)
609     return createFileError(
610         precomp.getPrecompFilePath().str(),
611         make_error<pdb::PDBError>(pdb::pdb_error_code::no_matching_pch));
612 
613   addObjFile(precompFile, &indexMap);
614 
615   return indexMap;
616 }
617 
618 static bool remapTypeIndex(TypeIndex &ti, ArrayRef<TypeIndex> typeIndexMap) {
619   if (ti.isSimple())
620     return true;
621   if (ti.toArrayIndex() >= typeIndexMap.size())
622     return false;
623   ti = typeIndexMap[ti.toArrayIndex()];
624   return true;
625 }
626 
627 static void remapTypesInSymbolRecord(ObjFile *file, SymbolKind symKind,
628                                      MutableArrayRef<uint8_t> recordBytes,
629                                      const CVIndexMap &indexMap,
630                                      ArrayRef<TiReference> typeRefs) {
631   MutableArrayRef<uint8_t> contents =
632       recordBytes.drop_front(sizeof(RecordPrefix));
633   for (const TiReference &ref : typeRefs) {
634     unsigned byteSize = ref.Count * sizeof(TypeIndex);
635     if (contents.size() < ref.Offset + byteSize)
636       fatal("symbol record too short");
637 
638     // This can be an item index or a type index. Choose the appropriate map.
639     ArrayRef<TypeIndex> typeOrItemMap = indexMap.tpiMap;
640     bool isItemIndex = ref.Kind == TiRefKind::IndexRef;
641     if (isItemIndex && indexMap.isTypeServerMap)
642       typeOrItemMap = indexMap.ipiMap;
643 
644     MutableArrayRef<TypeIndex> tIs(
645         reinterpret_cast<TypeIndex *>(contents.data() + ref.Offset), ref.Count);
646     for (TypeIndex &ti : tIs) {
647       if (!remapTypeIndex(ti, typeOrItemMap)) {
648         log("ignoring symbol record of kind 0x" + utohexstr(symKind) + " in " +
649             file->getName() + " with bad " + (isItemIndex ? "item" : "type") +
650             " index 0x" + utohexstr(ti.getIndex()));
651         ti = TypeIndex(SimpleTypeKind::NotTranslated);
652         continue;
653       }
654     }
655   }
656 }
657 
658 static void
659 recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> contents,
660                                    uint32_t offset,
661                                    std::vector<ulittle32_t *> &strTableRefs) {
662   contents =
663       contents.drop_front(offset).take_front(sizeof(support::ulittle32_t));
664   ulittle32_t *index = reinterpret_cast<ulittle32_t *>(contents.data());
665   strTableRefs.push_back(index);
666 }
667 
668 static void
669 recordStringTableReferences(SymbolKind kind, MutableArrayRef<uint8_t> contents,
670                             std::vector<ulittle32_t *> &strTableRefs) {
671   // For now we only handle S_FILESTATIC, but we may need the same logic for
672   // S_DEFRANGE and S_DEFRANGE_SUBFIELD.  However, I cannot seem to generate any
673   // PDBs that contain these types of records, so because of the uncertainty
674   // they are omitted here until we can prove that it's necessary.
675   switch (kind) {
676   case SymbolKind::S_FILESTATIC:
677     // FileStaticSym::ModFileOffset
678     recordStringTableReferenceAtOffset(contents, 8, strTableRefs);
679     break;
680   case SymbolKind::S_DEFRANGE:
681   case SymbolKind::S_DEFRANGE_SUBFIELD:
682     log("Not fixing up string table reference in S_DEFRANGE / "
683         "S_DEFRANGE_SUBFIELD record");
684     break;
685   default:
686     break;
687   }
688 }
689 
690 static SymbolKind symbolKind(ArrayRef<uint8_t> recordData) {
691   const RecordPrefix *prefix =
692       reinterpret_cast<const RecordPrefix *>(recordData.data());
693   return static_cast<SymbolKind>(uint16_t(prefix->RecordKind));
694 }
695 
696 /// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
697 static void translateIdSymbols(MutableArrayRef<uint8_t> &recordData,
698                                TypeCollection &iDTable) {
699   RecordPrefix *prefix = reinterpret_cast<RecordPrefix *>(recordData.data());
700 
701   SymbolKind kind = symbolKind(recordData);
702 
703   if (kind == SymbolKind::S_PROC_ID_END) {
704     prefix->RecordKind = SymbolKind::S_END;
705     return;
706   }
707 
708   // In an object file, GPROC32_ID has an embedded reference which refers to the
709   // single object file type index namespace.  This has already been translated
710   // to the PDB file's ID stream index space, but we need to convert this to a
711   // symbol that refers to the type stream index space.  So we remap again from
712   // ID index space to type index space.
713   if (kind == SymbolKind::S_GPROC32_ID || kind == SymbolKind::S_LPROC32_ID) {
714     SmallVector<TiReference, 1> refs;
715     auto content = recordData.drop_front(sizeof(RecordPrefix));
716     CVSymbol sym(recordData);
717     discoverTypeIndicesInSymbol(sym, refs);
718     assert(refs.size() == 1);
719     assert(refs.front().Count == 1);
720 
721     TypeIndex *ti =
722         reinterpret_cast<TypeIndex *>(content.data() + refs[0].Offset);
723     // `ti` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
724     // the IPI stream, whose `FunctionType` member refers to the TPI stream.
725     // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
726     // in both cases we just need the second type index.
727     if (!ti->isSimple() && !ti->isNoneType()) {
728       CVType funcIdData = iDTable.getType(*ti);
729       SmallVector<TypeIndex, 2> indices;
730       discoverTypeIndices(funcIdData, indices);
731       assert(indices.size() == 2);
732       *ti = indices[1];
733     }
734 
735     kind = (kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
736                                               : SymbolKind::S_LPROC32;
737     prefix->RecordKind = uint16_t(kind);
738   }
739 }
740 
741 /// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
742 /// The object file may not be aligned.
743 static MutableArrayRef<uint8_t>
744 copyAndAlignSymbol(const CVSymbol &sym, MutableArrayRef<uint8_t> &alignedMem) {
745   size_t size = alignTo(sym.length(), alignOf(CodeViewContainer::Pdb));
746   assert(size >= 4 && "record too short");
747   assert(size <= MaxRecordLength && "record too long");
748   assert(alignedMem.size() >= size && "didn't preallocate enough");
749 
750   // Copy the symbol record and zero out any padding bytes.
751   MutableArrayRef<uint8_t> newData = alignedMem.take_front(size);
752   alignedMem = alignedMem.drop_front(size);
753   memcpy(newData.data(), sym.data().data(), sym.length());
754   memset(newData.data() + sym.length(), 0, size - sym.length());
755 
756   // Update the record prefix length. It should point to the beginning of the
757   // next record.
758   auto *prefix = reinterpret_cast<RecordPrefix *>(newData.data());
759   prefix->RecordLen = size - 2;
760   return newData;
761 }
762 
763 struct ScopeRecord {
764   ulittle32_t ptrParent;
765   ulittle32_t ptrEnd;
766 };
767 
768 struct SymbolScope {
769   ScopeRecord *openingRecord;
770   uint32_t scopeOffset;
771 };
772 
773 static void scopeStackOpen(SmallVectorImpl<SymbolScope> &stack,
774                            uint32_t curOffset, CVSymbol &sym) {
775   assert(symbolOpensScope(sym.kind()));
776   SymbolScope s;
777   s.scopeOffset = curOffset;
778   s.openingRecord = const_cast<ScopeRecord *>(
779       reinterpret_cast<const ScopeRecord *>(sym.content().data()));
780   s.openingRecord->ptrParent = stack.empty() ? 0 : stack.back().scopeOffset;
781   stack.push_back(s);
782 }
783 
784 static void scopeStackClose(SmallVectorImpl<SymbolScope> &stack,
785                             uint32_t curOffset, InputFile *file) {
786   if (stack.empty()) {
787     warn("symbol scopes are not balanced in " + file->getName());
788     return;
789   }
790   SymbolScope s = stack.pop_back_val();
791   s.openingRecord->ptrEnd = curOffset;
792 }
793 
794 static bool symbolGoesInModuleStream(const CVSymbol &sym, bool isGlobalScope) {
795   switch (sym.kind()) {
796   case SymbolKind::S_GDATA32:
797   case SymbolKind::S_CONSTANT:
798   // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
799   // since they are synthesized by the linker in response to S_GPROC32 and
800   // S_LPROC32, but if we do see them, don't put them in the module stream I
801   // guess.
802   case SymbolKind::S_PROCREF:
803   case SymbolKind::S_LPROCREF:
804     return false;
805   // S_UDT records go in the module stream if it is not a global S_UDT.
806   case SymbolKind::S_UDT:
807     return !isGlobalScope;
808   // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
809   case SymbolKind::S_LDATA32:
810   default:
811     return true;
812   }
813 }
814 
815 static bool symbolGoesInGlobalsStream(const CVSymbol &sym, bool isGlobalScope) {
816   switch (sym.kind()) {
817   case SymbolKind::S_CONSTANT:
818   case SymbolKind::S_GDATA32:
819   // S_LDATA32 goes in both the module stream and the globals stream.
820   case SymbolKind::S_LDATA32:
821   case SymbolKind::S_GPROC32:
822   case SymbolKind::S_LPROC32:
823   // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
824   // since they are synthesized by the linker in response to S_GPROC32 and
825   // S_LPROC32, but if we do see them, copy them straight through.
826   case SymbolKind::S_PROCREF:
827   case SymbolKind::S_LPROCREF:
828     return true;
829   // S_UDT records go in the globals stream if it is a global S_UDT.
830   case SymbolKind::S_UDT:
831     return isGlobalScope;
832   default:
833     return false;
834   }
835 }
836 
837 static void addGlobalSymbol(pdb::GSIStreamBuilder &builder, uint16_t modIndex,
838                             unsigned symOffset, const CVSymbol &sym) {
839   switch (sym.kind()) {
840   case SymbolKind::S_CONSTANT:
841   case SymbolKind::S_UDT:
842   case SymbolKind::S_GDATA32:
843   case SymbolKind::S_LDATA32:
844   case SymbolKind::S_PROCREF:
845   case SymbolKind::S_LPROCREF:
846     builder.addGlobalSymbol(sym);
847     break;
848   case SymbolKind::S_GPROC32:
849   case SymbolKind::S_LPROC32: {
850     SymbolRecordKind k = SymbolRecordKind::ProcRefSym;
851     if (sym.kind() == SymbolKind::S_LPROC32)
852       k = SymbolRecordKind::LocalProcRef;
853     ProcRefSym ps(k);
854     ps.Module = modIndex;
855     // For some reason, MSVC seems to add one to this value.
856     ++ps.Module;
857     ps.Name = getSymbolName(sym);
858     ps.SumName = 0;
859     ps.SymOffset = symOffset;
860     builder.addGlobalSymbol(ps);
861     break;
862   }
863   default:
864     llvm_unreachable("Invalid symbol kind!");
865   }
866 }
867 
868 void PDBLinker::mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap,
869                                    std::vector<ulittle32_t *> &stringTableRefs,
870                                    BinaryStreamRef symData) {
871   ArrayRef<uint8_t> symsBuffer;
872   cantFail(symData.readBytes(0, symData.getLength(), symsBuffer));
873   SmallVector<SymbolScope, 4> scopes;
874 
875   // Iterate every symbol to check if any need to be realigned, and if so, how
876   // much space we need to allocate for them.
877   bool needsRealignment = false;
878   unsigned totalRealignedSize = 0;
879   auto ec = forEachCodeViewRecord<CVSymbol>(
880       symsBuffer, [&](CVSymbol sym) -> llvm::Error {
881         unsigned realignedSize =
882             alignTo(sym.length(), alignOf(CodeViewContainer::Pdb));
883         needsRealignment |= realignedSize != sym.length();
884         totalRealignedSize += realignedSize;
885         return Error::success();
886       });
887 
888   // If any of the symbol record lengths was corrupt, ignore them all, warn
889   // about it, and move on.
890   if (ec) {
891     warn("corrupt symbol records in " + file->getName());
892     consumeError(std::move(ec));
893     return;
894   }
895 
896   // If any symbol needed realignment, allocate enough contiguous memory for
897   // them all. Typically symbol subsections are small enough that this will not
898   // cause fragmentation.
899   MutableArrayRef<uint8_t> alignedSymbolMem;
900   if (needsRealignment) {
901     void *alignedData =
902         alloc.Allocate(totalRealignedSize, alignOf(CodeViewContainer::Pdb));
903     alignedSymbolMem = makeMutableArrayRef(
904         reinterpret_cast<uint8_t *>(alignedData), totalRealignedSize);
905   }
906 
907   // Iterate again, this time doing the real work.
908   unsigned curSymOffset = file->moduleDBI->getNextSymbolOffset();
909   ArrayRef<uint8_t> bulkSymbols;
910   cantFail(forEachCodeViewRecord<CVSymbol>(
911       symsBuffer, [&](CVSymbol sym) -> llvm::Error {
912         // Align the record if required.
913         MutableArrayRef<uint8_t> recordBytes;
914         if (needsRealignment) {
915           recordBytes = copyAndAlignSymbol(sym, alignedSymbolMem);
916           sym = CVSymbol(recordBytes);
917         } else {
918           // Otherwise, we can actually mutate the symbol directly, since we
919           // copied it to apply relocations.
920           recordBytes = makeMutableArrayRef(
921               const_cast<uint8_t *>(sym.data().data()), sym.length());
922         }
923 
924         // Discover type index references in the record. Skip it if we don't
925         // know where they are.
926         SmallVector<TiReference, 32> typeRefs;
927         if (!discoverTypeIndicesInSymbol(sym, typeRefs)) {
928           log("ignoring unknown symbol record with kind 0x" +
929               utohexstr(sym.kind()));
930           return Error::success();
931         }
932 
933         // Re-map all the type index references.
934         remapTypesInSymbolRecord(file, sym.kind(), recordBytes, indexMap,
935                                  typeRefs);
936 
937         // An object file may have S_xxx_ID symbols, but these get converted to
938         // "real" symbols in a PDB.
939         translateIdSymbols(recordBytes, tMerger.getIDTable());
940         sym = CVSymbol(recordBytes);
941 
942         // If this record refers to an offset in the object file's string table,
943         // add that item to the global PDB string table and re-write the index.
944         recordStringTableReferences(sym.kind(), recordBytes, stringTableRefs);
945 
946         // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
947         if (symbolOpensScope(sym.kind()))
948           scopeStackOpen(scopes, curSymOffset, sym);
949         else if (symbolEndsScope(sym.kind()))
950           scopeStackClose(scopes, curSymOffset, file);
951 
952         // Add the symbol to the globals stream if necessary.  Do this before
953         // adding the symbol to the module since we may need to get the next
954         // symbol offset, and writing to the module's symbol stream will update
955         // that offset.
956         if (symbolGoesInGlobalsStream(sym, scopes.empty())) {
957           addGlobalSymbol(builder.getGsiBuilder(),
958                           file->moduleDBI->getModuleIndex(), curSymOffset, sym);
959           ++globalSymbols;
960         }
961 
962         if (symbolGoesInModuleStream(sym, scopes.empty())) {
963           // Add symbols to the module in bulk. If this symbol is contiguous
964           // with the previous run of symbols to add, combine the ranges. If
965           // not, close the previous range of symbols and start a new one.
966           if (sym.data().data() == bulkSymbols.end()) {
967             bulkSymbols = makeArrayRef(bulkSymbols.data(),
968                                        bulkSymbols.size() + sym.length());
969           } else {
970             file->moduleDBI->addSymbolsInBulk(bulkSymbols);
971             bulkSymbols = recordBytes;
972           }
973           curSymOffset += sym.length();
974           ++moduleSymbols;
975         }
976         return Error::success();
977       }));
978 
979   // Add any remaining symbols we've accumulated.
980   file->moduleDBI->addSymbolsInBulk(bulkSymbols);
981 }
982 
983 // Allocate memory for a .debug$S / .debug$F section and relocate it.
984 static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &alloc,
985                                             SectionChunk &debugChunk) {
986   uint8_t *buffer = alloc.Allocate<uint8_t>(debugChunk.getSize());
987   assert(debugChunk.getOutputSectionIdx() == 0 &&
988          "debug sections should not be in output sections");
989   debugChunk.writeTo(buffer);
990   return makeArrayRef(buffer, debugChunk.getSize());
991 }
992 
993 static pdb::SectionContrib createSectionContrib(const Chunk *c, uint32_t modi) {
994   OutputSection *os = c ? c->getOutputSection() : nullptr;
995   pdb::SectionContrib sc;
996   memset(&sc, 0, sizeof(sc));
997   sc.ISect = os ? os->sectionIndex : llvm::pdb::kInvalidStreamIndex;
998   sc.Off = c && os ? c->getRVA() - os->getRVA() : 0;
999   sc.Size = c ? c->getSize() : -1;
1000   if (auto *secChunk = dyn_cast_or_null<SectionChunk>(c)) {
1001     sc.Characteristics = secChunk->header->Characteristics;
1002     sc.Imod = secChunk->file->moduleDBI->getModuleIndex();
1003     ArrayRef<uint8_t> contents = secChunk->getContents();
1004     JamCRC crc(0);
1005     crc.update(contents);
1006     sc.DataCrc = crc.getCRC();
1007   } else {
1008     sc.Characteristics = os ? os->header.Characteristics : 0;
1009     sc.Imod = modi;
1010   }
1011   sc.RelocCrc = 0; // FIXME
1012 
1013   return sc;
1014 }
1015 
1016 static uint32_t
1017 translateStringTableIndex(uint32_t objIndex,
1018                           const DebugStringTableSubsectionRef &objStrTable,
1019                           DebugStringTableSubsection &pdbStrTable) {
1020   auto expectedString = objStrTable.getString(objIndex);
1021   if (!expectedString) {
1022     warn("Invalid string table reference");
1023     consumeError(expectedString.takeError());
1024     return 0;
1025   }
1026 
1027   return pdbStrTable.insert(*expectedString);
1028 }
1029 
1030 void DebugSHandler::handleDebugS(lld::coff::SectionChunk &debugS) {
1031   DebugSubsectionArray subsections;
1032 
1033   ArrayRef<uint8_t> relocatedDebugContents = SectionChunk::consumeDebugMagic(
1034       relocateDebugChunk(linker.alloc, debugS), debugS.getSectionName());
1035 
1036   BinaryStreamReader reader(relocatedDebugContents, support::little);
1037   exitOnErr(reader.readArray(subsections, relocatedDebugContents.size()));
1038 
1039   for (const DebugSubsectionRecord &ss : subsections) {
1040     // Ignore subsections with the 'ignore' bit. Some versions of the Visual C++
1041     // runtime have subsections with this bit set.
1042     if (uint32_t(ss.kind()) & codeview::SubsectionIgnoreFlag)
1043       continue;
1044 
1045     switch (ss.kind()) {
1046     case DebugSubsectionKind::StringTable: {
1047       assert(!cVStrTab.valid() &&
1048              "Encountered multiple string table subsections!");
1049       exitOnErr(cVStrTab.initialize(ss.getRecordData()));
1050       break;
1051     }
1052     case DebugSubsectionKind::FileChecksums:
1053       assert(!checksums.valid() &&
1054              "Encountered multiple checksum subsections!");
1055       exitOnErr(checksums.initialize(ss.getRecordData()));
1056       break;
1057     case DebugSubsectionKind::Lines:
1058       // We can add the relocated line table directly to the PDB without
1059       // modification because the file checksum offsets will stay the same.
1060       file.moduleDBI->addDebugSubsection(ss);
1061       break;
1062     case DebugSubsectionKind::InlineeLines:
1063       assert(!inlineeLines.valid() &&
1064              "Encountered multiple inlinee lines subsections!");
1065       exitOnErr(inlineeLines.initialize(ss.getRecordData()));
1066       break;
1067     case DebugSubsectionKind::FrameData: {
1068       // We need to re-write string table indices here, so save off all
1069       // frame data subsections until we've processed the entire list of
1070       // subsections so that we can be sure we have the string table.
1071       DebugFrameDataSubsectionRef fds;
1072       exitOnErr(fds.initialize(ss.getRecordData()));
1073       newFpoFrames.push_back(std::move(fds));
1074       break;
1075     }
1076     case DebugSubsectionKind::Symbols: {
1077       linker.mergeSymbolRecords(&file, indexMap, stringTableReferences,
1078                                 ss.getRecordData());
1079       break;
1080     }
1081 
1082     case DebugSubsectionKind::CrossScopeImports:
1083     case DebugSubsectionKind::CrossScopeExports:
1084       // These appear to relate to cross-module optimization, so we might use
1085       // these for ThinLTO.
1086       break;
1087 
1088     case DebugSubsectionKind::ILLines:
1089     case DebugSubsectionKind::FuncMDTokenMap:
1090     case DebugSubsectionKind::TypeMDTokenMap:
1091     case DebugSubsectionKind::MergedAssemblyInput:
1092       // These appear to relate to .Net assembly info.
1093       break;
1094 
1095     case DebugSubsectionKind::CoffSymbolRVA:
1096       // Unclear what this is for.
1097       break;
1098 
1099     default:
1100       warn("ignoring unknown debug$S subsection kind 0x" +
1101            utohexstr(uint32_t(ss.kind())) + " in file " + toString(&file));
1102       break;
1103     }
1104   }
1105 }
1106 
1107 static Expected<StringRef>
1108 getFileName(const DebugStringTableSubsectionRef &strings,
1109             const DebugChecksumsSubsectionRef &checksums, uint32_t fileID) {
1110   auto iter = checksums.getArray().at(fileID);
1111   if (iter == checksums.getArray().end())
1112     return make_error<CodeViewError>(cv_error_code::no_records);
1113   uint32_t offset = iter->FileNameOffset;
1114   return strings.getString(offset);
1115 }
1116 
1117 std::shared_ptr<DebugInlineeLinesSubsection>
1118 DebugSHandler::mergeInlineeLines(DebugChecksumsSubsection *newChecksums) {
1119   auto newInlineeLines = std::make_shared<DebugInlineeLinesSubsection>(
1120       *newChecksums, inlineeLines.hasExtraFiles());
1121 
1122   for (const InlineeSourceLine &line : inlineeLines) {
1123     TypeIndex inlinee = line.Header->Inlinee;
1124     uint32_t fileID = line.Header->FileID;
1125     uint32_t sourceLine = line.Header->SourceLineNum;
1126 
1127     ArrayRef<TypeIndex> typeOrItemMap =
1128         indexMap.isTypeServerMap ? indexMap.ipiMap : indexMap.tpiMap;
1129     if (!remapTypeIndex(inlinee, typeOrItemMap)) {
1130       log("ignoring inlinee line record in " + file.getName() +
1131           " with bad inlinee index 0x" + utohexstr(inlinee.getIndex()));
1132       continue;
1133     }
1134 
1135     SmallString<128> filename =
1136         exitOnErr(getFileName(cVStrTab, checksums, fileID));
1137     pdbMakeAbsolute(filename);
1138     newInlineeLines->addInlineSite(inlinee, filename, sourceLine);
1139 
1140     if (inlineeLines.hasExtraFiles()) {
1141       for (uint32_t extraFileId : line.ExtraFiles) {
1142         filename = exitOnErr(getFileName(cVStrTab, checksums, extraFileId));
1143         pdbMakeAbsolute(filename);
1144         newInlineeLines->addExtraFile(filename);
1145       }
1146     }
1147   }
1148 
1149   return newInlineeLines;
1150 }
1151 
1152 void DebugSHandler::finish() {
1153   pdb::DbiStreamBuilder &dbiBuilder = linker.builder.getDbiBuilder();
1154 
1155   // We should have seen all debug subsections across the entire object file now
1156   // which means that if a StringTable subsection and Checksums subsection were
1157   // present, now is the time to handle them.
1158   if (!cVStrTab.valid()) {
1159     if (checksums.valid())
1160       fatal(".debug$S sections with a checksums subsection must also contain a "
1161             "string table subsection");
1162 
1163     if (!stringTableReferences.empty())
1164       warn("No StringTable subsection was encountered, but there are string "
1165            "table references");
1166     return;
1167   }
1168 
1169   // Rewrite string table indices in the Fpo Data and symbol records to refer to
1170   // the global PDB string table instead of the object file string table.
1171   for (DebugFrameDataSubsectionRef &fds : newFpoFrames) {
1172     const ulittle32_t *reloc = fds.getRelocPtr();
1173     for (codeview::FrameData fd : fds) {
1174       fd.RvaStart += *reloc;
1175       fd.FrameFunc =
1176           translateStringTableIndex(fd.FrameFunc, cVStrTab, linker.pdbStrTab);
1177       dbiBuilder.addNewFpoData(fd);
1178     }
1179   }
1180 
1181   for (ulittle32_t *ref : stringTableReferences)
1182     *ref = translateStringTableIndex(*ref, cVStrTab, linker.pdbStrTab);
1183 
1184   // Make a new file checksum table that refers to offsets in the PDB-wide
1185   // string table. Generally the string table subsection appears after the
1186   // checksum table, so we have to do this after looping over all the
1187   // subsections.
1188   auto newChecksums = std::make_unique<DebugChecksumsSubsection>(linker.pdbStrTab);
1189   for (FileChecksumEntry &fc : checksums) {
1190     SmallString<128> filename =
1191         exitOnErr(cVStrTab.getString(fc.FileNameOffset));
1192     pdbMakeAbsolute(filename);
1193     exitOnErr(dbiBuilder.addModuleSourceFile(*file.moduleDBI, filename));
1194     newChecksums->addChecksum(filename, fc.Kind, fc.Checksum);
1195   }
1196 
1197   // Rewrite inlinee item indices if present.
1198   if (inlineeLines.valid())
1199     file.moduleDBI->addDebugSubsection(mergeInlineeLines(newChecksums.get()));
1200 
1201   file.moduleDBI->addDebugSubsection(std::move(newChecksums));
1202 }
1203 
1204 void PDBLinker::addObjFile(ObjFile *file, CVIndexMap *externIndexMap) {
1205   if (file->mergedIntoPDB)
1206     return;
1207   file->mergedIntoPDB = true;
1208 
1209   // Before we can process symbol substreams from .debug$S, we need to process
1210   // type information, file checksums, and the string table.  Add type info to
1211   // the PDB first, so that we can get the map from object file type and item
1212   // indices to PDB type and item indices.
1213   CVIndexMap objectIndexMap;
1214   auto indexMapResult =
1215       mergeDebugT(file, externIndexMap ? externIndexMap : &objectIndexMap);
1216 
1217   // If the .debug$T sections fail to merge, assume there is no debug info.
1218   if (!indexMapResult) {
1219     if (!config->warnDebugInfoUnusable) {
1220       consumeError(indexMapResult.takeError());
1221       return;
1222     }
1223     warn("Cannot use debug info for '" + toString(file) + "' [LNK4099]\n" +
1224          ">>> failed to load reference " +
1225          StringRef(toString(indexMapResult.takeError())));
1226     return;
1227   }
1228 
1229   ScopedTimer t(symbolMergingTimer);
1230 
1231   pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
1232   DebugSHandler dsh(*this, *file, *indexMapResult);
1233   // Now do all live .debug$S and .debug$F sections.
1234   for (SectionChunk *debugChunk : file->getDebugChunks()) {
1235     if (!debugChunk->live || debugChunk->getSize() == 0)
1236       continue;
1237 
1238     if (debugChunk->getSectionName() == ".debug$S") {
1239       dsh.handleDebugS(*debugChunk);
1240       continue;
1241     }
1242 
1243     if (debugChunk->getSectionName() == ".debug$F") {
1244       ArrayRef<uint8_t> relocatedDebugContents =
1245           relocateDebugChunk(alloc, *debugChunk);
1246 
1247       FixedStreamArray<object::FpoData> fpoRecords;
1248       BinaryStreamReader reader(relocatedDebugContents, support::little);
1249       uint32_t count = relocatedDebugContents.size() / sizeof(object::FpoData);
1250       exitOnErr(reader.readArray(fpoRecords, count));
1251 
1252       // These are already relocated and don't refer to the string table, so we
1253       // can just copy it.
1254       for (const object::FpoData &fd : fpoRecords)
1255         dbiBuilder.addOldFpoData(fd);
1256       continue;
1257     }
1258   }
1259 
1260   // Do any post-processing now that all .debug$S sections have been processed.
1261   dsh.finish();
1262 }
1263 
1264 // Add a module descriptor for every object file. We need to put an absolute
1265 // path to the object into the PDB. If this is a plain object, we make its
1266 // path absolute. If it's an object in an archive, we make the archive path
1267 // absolute.
1268 static void createModuleDBI(pdb::PDBFileBuilder &builder) {
1269   pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
1270   SmallString<128> objName;
1271 
1272   for (ObjFile *file : ObjFile::instances) {
1273 
1274     bool inArchive = !file->parentName.empty();
1275     objName = inArchive ? file->parentName : file->getName();
1276     pdbMakeAbsolute(objName);
1277     StringRef modName = inArchive ? file->getName() : StringRef(objName);
1278 
1279     file->moduleDBI = &exitOnErr(dbiBuilder.addModuleInfo(modName));
1280     file->moduleDBI->setObjFileName(objName);
1281 
1282     ArrayRef<Chunk *> chunks = file->getChunks();
1283     uint32_t modi = file->moduleDBI->getModuleIndex();
1284 
1285     for (Chunk *c : chunks) {
1286       auto *secChunk = dyn_cast<SectionChunk>(c);
1287       if (!secChunk || !secChunk->live)
1288         continue;
1289       pdb::SectionContrib sc = createSectionContrib(secChunk, modi);
1290       file->moduleDBI->setFirstSectionContrib(sc);
1291       break;
1292     }
1293   }
1294 }
1295 
1296 static PublicSym32 createPublic(Defined *def) {
1297   PublicSym32 pub(SymbolKind::S_PUB32);
1298   pub.Name = def->getName();
1299   if (auto *d = dyn_cast<DefinedCOFF>(def)) {
1300     if (d->getCOFFSymbol().isFunctionDefinition())
1301       pub.Flags = PublicSymFlags::Function;
1302   } else if (isa<DefinedImportThunk>(def)) {
1303     pub.Flags = PublicSymFlags::Function;
1304   }
1305 
1306   OutputSection *os = def->getChunk()->getOutputSection();
1307   assert(os && "all publics should be in final image");
1308   pub.Offset = def->getRVA() - os->getRVA();
1309   pub.Segment = os->sectionIndex;
1310   return pub;
1311 }
1312 
1313 // Add all object files to the PDB. Merge .debug$T sections into IpiData and
1314 // TpiData.
1315 void PDBLinker::addObjectsToPDB() {
1316   ScopedTimer t1(addObjectsTimer);
1317 
1318   createModuleDBI(builder);
1319 
1320   for (ObjFile *file : ObjFile::instances)
1321     addObjFile(file);
1322 
1323   builder.getStringTableBuilder().setStrings(pdbStrTab);
1324   t1.stop();
1325 
1326   // Construct TPI and IPI stream contents.
1327   ScopedTimer t2(tpiStreamLayoutTimer);
1328   addTypeInfo(builder.getTpiBuilder(), tMerger.getTypeTable());
1329   addTypeInfo(builder.getIpiBuilder(), tMerger.getIDTable());
1330   t2.stop();
1331 
1332   ScopedTimer t3(globalsLayoutTimer);
1333   // Compute the public and global symbols.
1334   auto &gsiBuilder = builder.getGsiBuilder();
1335   std::vector<PublicSym32> publics;
1336   symtab->forEachSymbol([&publics](Symbol *s) {
1337     // Only emit defined, live symbols that have a chunk.
1338     auto *def = dyn_cast<Defined>(s);
1339     if (def && def->isLive() && def->getChunk())
1340       publics.push_back(createPublic(def));
1341   });
1342 
1343   if (!publics.empty()) {
1344     publicSymbols = publics.size();
1345     // Sort the public symbols and add them to the stream.
1346     parallelSort(publics, [](const PublicSym32 &l, const PublicSym32 &r) {
1347       return l.Name < r.Name;
1348     });
1349     for (const PublicSym32 &pub : publics)
1350       gsiBuilder.addPublicSymbol(pub);
1351   }
1352 }
1353 
1354 void PDBLinker::printStats() {
1355   if (!config->showSummary)
1356     return;
1357 
1358   SmallString<256> buffer;
1359   raw_svector_ostream stream(buffer);
1360 
1361   stream << center_justify("Summary", 80) << '\n'
1362          << std::string(80, '-') << '\n';
1363 
1364   auto print = [&](uint64_t v, StringRef s) {
1365     stream << format_decimal(v, 15) << " " << s << '\n';
1366   };
1367 
1368   print(ObjFile::instances.size(),
1369         "Input OBJ files (expanded from all cmd-line inputs)");
1370   print(typeServerIndexMappings.size(), "PDB type server dependencies");
1371   print(precompTypeIndexMappings.size(), "Precomp OBJ dependencies");
1372   print(tMerger.getTypeTable().size() + tMerger.getIDTable().size(),
1373         "Merged TPI records");
1374   print(pdbStrTab.size(), "Output PDB strings");
1375   print(globalSymbols, "Global symbol records");
1376   print(moduleSymbols, "Module symbol records");
1377   print(publicSymbols, "Public symbol records");
1378 
1379   auto printLargeInputTypeRecs = [&](StringRef name,
1380                                      ArrayRef<uint32_t> recCounts,
1381                                      TypeCollection &records) {
1382     // Figure out which type indices were responsible for the most duplicate
1383     // bytes in the input files. These should be frequently emitted LF_CLASS and
1384     // LF_FIELDLIST records.
1385     struct TypeSizeInfo {
1386       uint32_t typeSize;
1387       uint32_t dupCount;
1388       TypeIndex typeIndex;
1389       uint64_t totalInputSize() const { return uint64_t(dupCount) * typeSize; }
1390       bool operator<(const TypeSizeInfo &rhs) const {
1391         return totalInputSize() < rhs.totalInputSize();
1392       }
1393     };
1394     SmallVector<TypeSizeInfo, 0> tsis;
1395     for (auto e : enumerate(recCounts)) {
1396       TypeIndex typeIndex = TypeIndex::fromArrayIndex(e.index());
1397       uint32_t typeSize = records.getType(typeIndex).length();
1398       uint32_t dupCount = e.value();
1399       tsis.push_back({typeSize, dupCount, typeIndex});
1400     }
1401 
1402     if (!tsis.empty()) {
1403       stream << "\nTop 10 types responsible for the most " << name
1404              << " input:\n";
1405       stream << "       index     total bytes   count     size\n";
1406       llvm::sort(tsis);
1407       unsigned i = 0;
1408       for (const auto &tsi : reverse(tsis)) {
1409         stream << formatv("  {0,10:X}: {1,14:N} = {2,5:N} * {3,6:N}\n",
1410                           tsi.typeIndex.getIndex(), tsi.totalInputSize(),
1411                           tsi.dupCount, tsi.typeSize);
1412         if (++i >= 10)
1413           break;
1414       }
1415       stream
1416           << "Run llvm-pdbutil to print details about a particular record:\n";
1417       stream << formatv("llvm-pdbutil dump -{0}s -{0}-index {1:X} {2}\n",
1418                         (name == "TPI" ? "type" : "id"),
1419                         tsis.back().typeIndex.getIndex(), config->pdbPath);
1420     }
1421   };
1422 
1423   printLargeInputTypeRecs("TPI", tpiCounts, tMerger.getTypeTable());
1424   printLargeInputTypeRecs("IPI", ipiCounts, tMerger.getIDTable());
1425 
1426   message(buffer);
1427 }
1428 
1429 void PDBLinker::addNatvisFiles() {
1430   for (StringRef file : config->natvisFiles) {
1431     ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr =
1432         MemoryBuffer::getFile(file);
1433     if (!dataOrErr) {
1434       warn("Cannot open input file: " + file);
1435       continue;
1436     }
1437     builder.addInjectedSource(file, std::move(*dataOrErr));
1438   }
1439 }
1440 
1441 static codeview::CPUType toCodeViewMachine(COFF::MachineTypes machine) {
1442   switch (machine) {
1443   case COFF::IMAGE_FILE_MACHINE_AMD64:
1444     return codeview::CPUType::X64;
1445   case COFF::IMAGE_FILE_MACHINE_ARM:
1446     return codeview::CPUType::ARM7;
1447   case COFF::IMAGE_FILE_MACHINE_ARM64:
1448     return codeview::CPUType::ARM64;
1449   case COFF::IMAGE_FILE_MACHINE_ARMNT:
1450     return codeview::CPUType::ARMNT;
1451   case COFF::IMAGE_FILE_MACHINE_I386:
1452     return codeview::CPUType::Intel80386;
1453   default:
1454     llvm_unreachable("Unsupported CPU Type");
1455   }
1456 }
1457 
1458 // Mimic MSVC which surrounds arguments containing whitespace with quotes.
1459 // Double double-quotes are handled, so that the resulting string can be
1460 // executed again on the cmd-line.
1461 static std::string quote(ArrayRef<StringRef> args) {
1462   std::string r;
1463   r.reserve(256);
1464   for (StringRef a : args) {
1465     if (!r.empty())
1466       r.push_back(' ');
1467     bool hasWS = a.find(' ') != StringRef::npos;
1468     bool hasQ = a.find('"') != StringRef::npos;
1469     if (hasWS || hasQ)
1470       r.push_back('"');
1471     if (hasQ) {
1472       SmallVector<StringRef, 4> s;
1473       a.split(s, '"');
1474       r.append(join(s, "\"\""));
1475     } else {
1476       r.append(a);
1477     }
1478     if (hasWS || hasQ)
1479       r.push_back('"');
1480   }
1481   return r;
1482 }
1483 
1484 static void fillLinkerVerRecord(Compile3Sym &cs) {
1485   cs.Machine = toCodeViewMachine(config->machine);
1486   // Interestingly, if we set the string to 0.0.0.0, then when trying to view
1487   // local variables WinDbg emits an error that private symbols are not present.
1488   // By setting this to a valid MSVC linker version string, local variables are
1489   // displayed properly.   As such, even though it is not representative of
1490   // LLVM's version information, we need this for compatibility.
1491   cs.Flags = CompileSym3Flags::None;
1492   cs.VersionBackendBuild = 25019;
1493   cs.VersionBackendMajor = 14;
1494   cs.VersionBackendMinor = 10;
1495   cs.VersionBackendQFE = 0;
1496 
1497   // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
1498   // linker module (which is by definition a backend), so we don't need to do
1499   // anything here.  Also, it seems we can use "LLVM Linker" for the linker name
1500   // without any problems.  Only the backend version has to be hardcoded to a
1501   // magic number.
1502   cs.VersionFrontendBuild = 0;
1503   cs.VersionFrontendMajor = 0;
1504   cs.VersionFrontendMinor = 0;
1505   cs.VersionFrontendQFE = 0;
1506   cs.Version = "LLVM Linker";
1507   cs.setLanguage(SourceLanguage::Link);
1508 }
1509 
1510 static void addCommonLinkerModuleSymbols(StringRef path,
1511                                          pdb::DbiModuleDescriptorBuilder &mod,
1512                                          BumpPtrAllocator &allocator) {
1513   ObjNameSym ons(SymbolRecordKind::ObjNameSym);
1514   EnvBlockSym ebs(SymbolRecordKind::EnvBlockSym);
1515   Compile3Sym cs(SymbolRecordKind::Compile3Sym);
1516   fillLinkerVerRecord(cs);
1517 
1518   ons.Name = "* Linker *";
1519   ons.Signature = 0;
1520 
1521   ArrayRef<StringRef> args = makeArrayRef(config->argv).drop_front();
1522   std::string argStr = quote(args);
1523   ebs.Fields.push_back("cwd");
1524   SmallString<64> cwd;
1525   if (config->pdbSourcePath.empty())
1526     sys::fs::current_path(cwd);
1527   else
1528     cwd = config->pdbSourcePath;
1529   ebs.Fields.push_back(cwd);
1530   ebs.Fields.push_back("exe");
1531   SmallString<64> exe = config->argv[0];
1532   pdbMakeAbsolute(exe);
1533   ebs.Fields.push_back(exe);
1534   ebs.Fields.push_back("pdb");
1535   ebs.Fields.push_back(path);
1536   ebs.Fields.push_back("cmd");
1537   ebs.Fields.push_back(argStr);
1538   mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1539       ons, allocator, CodeViewContainer::Pdb));
1540   mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1541       cs, allocator, CodeViewContainer::Pdb));
1542   mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1543       ebs, allocator, CodeViewContainer::Pdb));
1544 }
1545 
1546 static void addLinkerModuleCoffGroup(PartialSection *sec,
1547                                      pdb::DbiModuleDescriptorBuilder &mod,
1548                                      OutputSection &os,
1549                                      BumpPtrAllocator &allocator) {
1550   // If there's a section, there's at least one chunk
1551   assert(!sec->chunks.empty());
1552   const Chunk *firstChunk = *sec->chunks.begin();
1553   const Chunk *lastChunk = *sec->chunks.rbegin();
1554 
1555   // Emit COFF group
1556   CoffGroupSym cgs(SymbolRecordKind::CoffGroupSym);
1557   cgs.Name = sec->name;
1558   cgs.Segment = os.sectionIndex;
1559   cgs.Offset = firstChunk->getRVA() - os.getRVA();
1560   cgs.Size = lastChunk->getRVA() + lastChunk->getSize() - firstChunk->getRVA();
1561   cgs.Characteristics = sec->characteristics;
1562 
1563   // Somehow .idata sections & sections groups in the debug symbol stream have
1564   // the "write" flag set. However the section header for the corresponding
1565   // .idata section doesn't have it.
1566   if (cgs.Name.startswith(".idata"))
1567     cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE;
1568 
1569   mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1570       cgs, allocator, CodeViewContainer::Pdb));
1571 }
1572 
1573 static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &mod,
1574                                          OutputSection &os,
1575                                          BumpPtrAllocator &allocator) {
1576   SectionSym sym(SymbolRecordKind::SectionSym);
1577   sym.Alignment = 12; // 2^12 = 4KB
1578   sym.Characteristics = os.header.Characteristics;
1579   sym.Length = os.getVirtualSize();
1580   sym.Name = os.name;
1581   sym.Rva = os.getRVA();
1582   sym.SectionNumber = os.sectionIndex;
1583   mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1584       sym, allocator, CodeViewContainer::Pdb));
1585 
1586   // Skip COFF groups in MinGW because it adds a significant footprint to the
1587   // PDB, due to each function being in its own section
1588   if (config->mingw)
1589     return;
1590 
1591   // Output COFF groups for individual chunks of this section.
1592   for (PartialSection *sec : os.contribSections) {
1593     addLinkerModuleCoffGroup(sec, mod, os, allocator);
1594   }
1595 }
1596 
1597 // Add all import files as modules to the PDB.
1598 void PDBLinker::addImportFilesToPDB(ArrayRef<OutputSection *> outputSections) {
1599   if (ImportFile::instances.empty())
1600     return;
1601 
1602   std::map<std::string, llvm::pdb::DbiModuleDescriptorBuilder *> dllToModuleDbi;
1603 
1604   for (ImportFile *file : ImportFile::instances) {
1605     if (!file->live)
1606       continue;
1607 
1608     if (!file->thunkSym)
1609       continue;
1610 
1611     if (!file->thunkLive)
1612         continue;
1613 
1614     std::string dll = StringRef(file->dllName).lower();
1615     llvm::pdb::DbiModuleDescriptorBuilder *&mod = dllToModuleDbi[dll];
1616     if (!mod) {
1617       pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
1618       SmallString<128> libPath = file->parentName;
1619       pdbMakeAbsolute(libPath);
1620       sys::path::native(libPath);
1621 
1622       // Name modules similar to MSVC's link.exe.
1623       // The first module is the simple dll filename
1624       llvm::pdb::DbiModuleDescriptorBuilder &firstMod =
1625           exitOnErr(dbiBuilder.addModuleInfo(file->dllName));
1626       firstMod.setObjFileName(libPath);
1627       pdb::SectionContrib sc =
1628           createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex);
1629       firstMod.setFirstSectionContrib(sc);
1630 
1631       // The second module is where the import stream goes.
1632       mod = &exitOnErr(dbiBuilder.addModuleInfo("Import:" + file->dllName));
1633       mod->setObjFileName(libPath);
1634     }
1635 
1636     DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);
1637     Chunk *thunkChunk = thunk->getChunk();
1638     OutputSection *thunkOS = thunkChunk->getOutputSection();
1639 
1640     ObjNameSym ons(SymbolRecordKind::ObjNameSym);
1641     Compile3Sym cs(SymbolRecordKind::Compile3Sym);
1642     Thunk32Sym ts(SymbolRecordKind::Thunk32Sym);
1643     ScopeEndSym es(SymbolRecordKind::ScopeEndSym);
1644 
1645     ons.Name = file->dllName;
1646     ons.Signature = 0;
1647 
1648     fillLinkerVerRecord(cs);
1649 
1650     ts.Name = thunk->getName();
1651     ts.Parent = 0;
1652     ts.End = 0;
1653     ts.Next = 0;
1654     ts.Thunk = ThunkOrdinal::Standard;
1655     ts.Length = thunkChunk->getSize();
1656     ts.Segment = thunkOS->sectionIndex;
1657     ts.Offset = thunkChunk->getRVA() - thunkOS->getRVA();
1658 
1659     mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1660         ons, alloc, CodeViewContainer::Pdb));
1661     mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1662         cs, alloc, CodeViewContainer::Pdb));
1663 
1664     SmallVector<SymbolScope, 4> scopes;
1665     CVSymbol newSym = codeview::SymbolSerializer::writeOneSymbol(
1666         ts, alloc, CodeViewContainer::Pdb);
1667     scopeStackOpen(scopes, mod->getNextSymbolOffset(), newSym);
1668 
1669     mod->addSymbol(newSym);
1670 
1671     newSym = codeview::SymbolSerializer::writeOneSymbol(es, alloc,
1672                                                         CodeViewContainer::Pdb);
1673     scopeStackClose(scopes, mod->getNextSymbolOffset(), file);
1674 
1675     mod->addSymbol(newSym);
1676 
1677     pdb::SectionContrib sc =
1678         createSectionContrib(thunk->getChunk(), mod->getModuleIndex());
1679     mod->setFirstSectionContrib(sc);
1680   }
1681 }
1682 
1683 // Creates a PDB file.
1684 void createPDB(SymbolTable *symtab,
1685                      ArrayRef<OutputSection *> outputSections,
1686                      ArrayRef<uint8_t> sectionTable,
1687                      llvm::codeview::DebugInfo *buildId) {
1688   ScopedTimer t1(totalPdbLinkTimer);
1689   PDBLinker pdb(symtab);
1690 
1691   pdb.initialize(buildId);
1692   pdb.addObjectsToPDB();
1693   pdb.addImportFilesToPDB(outputSections);
1694   pdb.addSections(outputSections, sectionTable);
1695   pdb.addNatvisFiles();
1696 
1697   ScopedTimer t2(diskCommitTimer);
1698   codeview::GUID guid;
1699   pdb.commit(&guid);
1700   memcpy(&buildId->PDB70.Signature, &guid, 16);
1701 
1702   t2.stop();
1703   t1.stop();
1704   pdb.printStats();
1705 }
1706 
1707 void PDBLinker::initialize(llvm::codeview::DebugInfo *buildId) {
1708   exitOnErr(builder.initialize(4096)); // 4096 is blocksize
1709 
1710   buildId->Signature.CVSignature = OMF::Signature::PDB70;
1711   // Signature is set to a hash of the PDB contents when the PDB is done.
1712   memset(buildId->PDB70.Signature, 0, 16);
1713   buildId->PDB70.Age = 1;
1714 
1715   // Create streams in MSF for predefined streams, namely
1716   // PDB, TPI, DBI and IPI.
1717   for (int i = 0; i < (int)pdb::kSpecialStreamCount; ++i)
1718     exitOnErr(builder.getMsfBuilder().addStream(0));
1719 
1720   // Add an Info stream.
1721   auto &infoBuilder = builder.getInfoBuilder();
1722   infoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
1723   infoBuilder.setHashPDBContentsToGUID(true);
1724 
1725   // Add an empty DBI stream.
1726   pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
1727   dbiBuilder.setAge(buildId->PDB70.Age);
1728   dbiBuilder.setVersionHeader(pdb::PdbDbiV70);
1729   dbiBuilder.setMachineType(config->machine);
1730   // Technically we are not link.exe 14.11, but there are known cases where
1731   // debugging tools on Windows expect Microsoft-specific version numbers or
1732   // they fail to work at all.  Since we know we produce PDBs that are
1733   // compatible with LINK 14.11, we set that version number here.
1734   dbiBuilder.setBuildNumber(14, 11);
1735 }
1736 
1737 void PDBLinker::addSections(ArrayRef<OutputSection *> outputSections,
1738                             ArrayRef<uint8_t> sectionTable) {
1739   // It's not entirely clear what this is, but the * Linker * module uses it.
1740   pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
1741   nativePath = config->pdbPath;
1742   pdbMakeAbsolute(nativePath);
1743   uint32_t pdbFilePathNI = dbiBuilder.addECName(nativePath);
1744   auto &linkerModule = exitOnErr(dbiBuilder.addModuleInfo("* Linker *"));
1745   linkerModule.setPdbFilePathNI(pdbFilePathNI);
1746   addCommonLinkerModuleSymbols(nativePath, linkerModule, alloc);
1747 
1748   // Add section contributions. They must be ordered by ascending RVA.
1749   for (OutputSection *os : outputSections) {
1750     addLinkerModuleSectionSymbol(linkerModule, *os, alloc);
1751     for (Chunk *c : os->chunks) {
1752       pdb::SectionContrib sc =
1753           createSectionContrib(c, linkerModule.getModuleIndex());
1754       builder.getDbiBuilder().addSectionContrib(sc);
1755     }
1756   }
1757 
1758   // The * Linker * first section contrib is only used along with /INCREMENTAL,
1759   // to provide trampolines thunks for incremental function patching. Set this
1760   // as "unused" because LLD doesn't support /INCREMENTAL link.
1761   pdb::SectionContrib sc =
1762       createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex);
1763   linkerModule.setFirstSectionContrib(sc);
1764 
1765   // Add Section Map stream.
1766   ArrayRef<object::coff_section> sections = {
1767       (const object::coff_section *)sectionTable.data(),
1768       sectionTable.size() / sizeof(object::coff_section)};
1769   sectionMap = pdb::DbiStreamBuilder::createSectionMap(sections);
1770   dbiBuilder.setSectionMap(sectionMap);
1771 
1772   // Add COFF section header stream.
1773   exitOnErr(
1774       dbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, sectionTable));
1775 }
1776 
1777 void PDBLinker::commit(codeview::GUID *guid) {
1778   ExitOnError exitOnErr((config->pdbPath + ": ").str());
1779   // Write to a file.
1780   exitOnErr(builder.commit(config->pdbPath, guid));
1781 }
1782 
1783 static uint32_t getSecrelReloc() {
1784   switch (config->machine) {
1785   case AMD64:
1786     return COFF::IMAGE_REL_AMD64_SECREL;
1787   case I386:
1788     return COFF::IMAGE_REL_I386_SECREL;
1789   case ARMNT:
1790     return COFF::IMAGE_REL_ARM_SECREL;
1791   case ARM64:
1792     return COFF::IMAGE_REL_ARM64_SECREL;
1793   default:
1794     llvm_unreachable("unknown machine type");
1795   }
1796 }
1797 
1798 // Try to find a line table for the given offset Addr into the given chunk C.
1799 // If a line table was found, the line table, the string and checksum tables
1800 // that are used to interpret the line table, and the offset of Addr in the line
1801 // table are stored in the output arguments. Returns whether a line table was
1802 // found.
1803 static bool findLineTable(const SectionChunk *c, uint32_t addr,
1804                           DebugStringTableSubsectionRef &cVStrTab,
1805                           DebugChecksumsSubsectionRef &checksums,
1806                           DebugLinesSubsectionRef &lines,
1807                           uint32_t &offsetInLinetable) {
1808   ExitOnError exitOnErr;
1809   uint32_t secrelReloc = getSecrelReloc();
1810 
1811   for (SectionChunk *dbgC : c->file->getDebugChunks()) {
1812     if (dbgC->getSectionName() != ".debug$S")
1813       continue;
1814 
1815     // Build a mapping of SECREL relocations in dbgC that refer to `c`.
1816     DenseMap<uint32_t, uint32_t> secrels;
1817     for (const coff_relocation &r : dbgC->getRelocs()) {
1818       if (r.Type != secrelReloc)
1819         continue;
1820 
1821       if (auto *s = dyn_cast_or_null<DefinedRegular>(
1822               c->file->getSymbols()[r.SymbolTableIndex]))
1823         if (s->getChunk() == c)
1824           secrels[r.VirtualAddress] = s->getValue();
1825     }
1826 
1827     ArrayRef<uint8_t> contents =
1828         SectionChunk::consumeDebugMagic(dbgC->getContents(), ".debug$S");
1829     DebugSubsectionArray subsections;
1830     BinaryStreamReader reader(contents, support::little);
1831     exitOnErr(reader.readArray(subsections, contents.size()));
1832 
1833     for (const DebugSubsectionRecord &ss : subsections) {
1834       switch (ss.kind()) {
1835       case DebugSubsectionKind::StringTable: {
1836         assert(!cVStrTab.valid() &&
1837                "Encountered multiple string table subsections!");
1838         exitOnErr(cVStrTab.initialize(ss.getRecordData()));
1839         break;
1840       }
1841       case DebugSubsectionKind::FileChecksums:
1842         assert(!checksums.valid() &&
1843                "Encountered multiple checksum subsections!");
1844         exitOnErr(checksums.initialize(ss.getRecordData()));
1845         break;
1846       case DebugSubsectionKind::Lines: {
1847         ArrayRef<uint8_t> bytes;
1848         auto ref = ss.getRecordData();
1849         exitOnErr(ref.readLongestContiguousChunk(0, bytes));
1850         size_t offsetInDbgC = bytes.data() - dbgC->getContents().data();
1851 
1852         // Check whether this line table refers to C.
1853         auto i = secrels.find(offsetInDbgC);
1854         if (i == secrels.end())
1855           break;
1856 
1857         // Check whether this line table covers Addr in C.
1858         DebugLinesSubsectionRef linesTmp;
1859         exitOnErr(linesTmp.initialize(BinaryStreamReader(ref)));
1860         uint32_t offsetInC = i->second + linesTmp.header()->RelocOffset;
1861         if (addr < offsetInC || addr >= offsetInC + linesTmp.header()->CodeSize)
1862           break;
1863 
1864         assert(!lines.header() &&
1865                "Encountered multiple line tables for function!");
1866         exitOnErr(lines.initialize(BinaryStreamReader(ref)));
1867         offsetInLinetable = addr - offsetInC;
1868         break;
1869       }
1870       default:
1871         break;
1872       }
1873 
1874       if (cVStrTab.valid() && checksums.valid() && lines.header())
1875         return true;
1876     }
1877   }
1878 
1879   return false;
1880 }
1881 
1882 // Use CodeView line tables to resolve a file and line number for the given
1883 // offset into the given chunk and return them, or None if a line table was
1884 // not found.
1885 Optional<std::pair<StringRef, uint32_t>>
1886 getFileLineCodeView(const SectionChunk *c, uint32_t addr) {
1887   ExitOnError exitOnErr;
1888 
1889   DebugStringTableSubsectionRef cVStrTab;
1890   DebugChecksumsSubsectionRef checksums;
1891   DebugLinesSubsectionRef lines;
1892   uint32_t offsetInLinetable;
1893 
1894   if (!findLineTable(c, addr, cVStrTab, checksums, lines, offsetInLinetable))
1895     return None;
1896 
1897   Optional<uint32_t> nameIndex;
1898   Optional<uint32_t> lineNumber;
1899   for (LineColumnEntry &entry : lines) {
1900     for (const LineNumberEntry &ln : entry.LineNumbers) {
1901       LineInfo li(ln.Flags);
1902       if (ln.Offset > offsetInLinetable) {
1903         if (!nameIndex) {
1904           nameIndex = entry.NameIndex;
1905           lineNumber = li.getStartLine();
1906         }
1907         StringRef filename =
1908             exitOnErr(getFileName(cVStrTab, checksums, *nameIndex));
1909         return std::make_pair(filename, *lineNumber);
1910       }
1911       nameIndex = entry.NameIndex;
1912       lineNumber = li.getStartLine();
1913     }
1914   }
1915   if (!nameIndex)
1916     return None;
1917   StringRef filename = exitOnErr(getFileName(cVStrTab, checksums, *nameIndex));
1918   return std::make_pair(filename, *lineNumber);
1919 }
1920 
1921 } // namespace coff
1922 } // namespace lld
1923