Coverage Report

Created: 2019-05-22 02:55

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