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