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