Coverage Report

Created: 2019-01-21 03:01

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