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