Coverage Report

Created: 2018-06-25 02:00

/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
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "PDB.h"
11
#include "Chunks.h"
12
#include "Config.h"
13
#include "Driver.h"
14
#include "SymbolTable.h"
15
#include "Symbols.h"
16
#include "Writer.h"
17
#include "lld/Common/ErrorHandler.h"
18
#include "lld/Common/Timer.h"
19
#include "llvm/DebugInfo/CodeView/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/SymbolSerializer.h"
26
#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
27
#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
28
#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
29
#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
30
#include "llvm/DebugInfo/MSF/MSFBuilder.h"
31
#include "llvm/DebugInfo/MSF/MSFCommon.h"
32
#include "llvm/DebugInfo/PDB/GenericError.h"
33
#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
34
#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
35
#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
36
#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
37
#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
38
#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
39
#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
40
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
41
#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
42
#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
43
#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
44
#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
45
#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
46
#include "llvm/DebugInfo/PDB/PDB.h"
47
#include "llvm/Object/COFF.h"
48
#include "llvm/Object/CVDebugRecord.h"
49
#include "llvm/Support/BinaryByteStream.h"
50
#include "llvm/Support/Endian.h"
51
#include "llvm/Support/FormatVariadic.h"
52
#include "llvm/Support/JamCRC.h"
53
#include "llvm/Support/Path.h"
54
#include "llvm/Support/ScopedPrinter.h"
55
#include <memory>
56
57
using namespace lld;
58
using namespace lld::coff;
59
using namespace llvm;
60
using namespace llvm::codeview;
61
62
using llvm::object::coff_section;
63
64
static ExitOnError ExitOnErr;
65
66
static Timer TotalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root());
67
68
static Timer AddObjectsTimer("Add Objects", TotalPdbLinkTimer);
69
static Timer TypeMergingTimer("Type Merging", AddObjectsTimer);
70
static Timer SymbolMergingTimer("Symbol Merging", AddObjectsTimer);
71
static Timer GlobalsLayoutTimer("Globals Stream Layout", TotalPdbLinkTimer);
72
static Timer TpiStreamLayoutTimer("TPI Stream Layout", TotalPdbLinkTimer);
73
static Timer DiskCommitTimer("Commit to Disk", TotalPdbLinkTimer);
74
75
namespace {
76
/// Map from type index and item index in a type server PDB to the
77
/// corresponding index in the destination PDB.
78
struct CVIndexMap {
79
  SmallVector<TypeIndex, 0> TPIMap;
80
  SmallVector<TypeIndex, 0> IPIMap;
81
  bool IsTypeServerMap = false;
82
};
83
84
class PDBLinker {
85
public:
86
  PDBLinker(SymbolTable *Symtab)
87
      : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc),
88
68
        IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) {
89
68
    // This isn't strictly necessary, but link.exe usually puts an empty string
90
68
    // as the first "valid" string in the string table, so we do the same in
91
68
    // order to maintain as much byte-for-byte compatibility as possible.
92
68
    PDBStrTab.insert("");
93
68
  }
94
95
  /// Emit the basic PDB structure: initial streams, headers, etc.
96
  void initialize(const 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 CodeView from a single object file into the PDB.
105
  void addObjFile(ObjFile *File);
106
107
  /// Produce a mapping from the type and item indices used in the object
108
  /// file to those in the destination PDB.
109
  ///
110
  /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
111
  /// and IPI from the type server PDB and return a map for it. Each unique type
112
  /// server PDB is merged at most once, so this may return an existing index
113
  /// mapping.
114
  ///
115
  /// If the object does not use a type server PDB (compiled with /Z7), we merge
116
  /// all the type and item records from the .debug$S stream and fill in the
117
  /// caller-provided ObjectIndexMap.
118
  Expected<const CVIndexMap&> mergeDebugT(ObjFile *File,
119
                                          CVIndexMap &ObjectIndexMap);
120
121
  Expected<const CVIndexMap&> maybeMergeTypeServerPDB(ObjFile *File,
122
                                                      TypeServer2Record &TS);
123
124
  /// Add the section map and section contributions to the PDB.
125
  void addSections(ArrayRef<OutputSection *> OutputSections,
126
                   ArrayRef<uint8_t> SectionTable);
127
128
  /// Write the PDB to disk.
129
  void commit();
130
131
private:
132
  BumpPtrAllocator Alloc;
133
134
  SymbolTable *Symtab;
135
136
  pdb::PDBFileBuilder Builder;
137
138
  /// Type records that will go into the PDB TPI stream.
139
  MergingTypeTableBuilder TypeTable;
140
141
  /// Item records that will go into the PDB IPI stream.
142
  MergingTypeTableBuilder IDTable;
143
144
  /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH)
145
  GlobalTypeTableBuilder GlobalTypeTable;
146
147
  /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH)
148
  GlobalTypeTableBuilder GlobalIDTable;
149
150
  /// PDBs use a single global string table for filenames in the file checksum
151
  /// table.
152
  DebugStringTableSubsection PDBStrTab;
153
154
  llvm::SmallString<128> NativePath;
155
156
  /// A list of other PDBs which are loaded during the linking process and which
157
  /// we need to keep around since the linking operation may reference pointers
158
  /// inside of these PDBs.
159
  llvm::SmallVector<std::unique_ptr<pdb::NativeSession>, 2> LoadedPDBs;
160
161
  std::vector<pdb::SecMapEntry> SectionMap;
162
163
  /// Type index mappings of type server PDBs that we've loaded so far.
164
  std::map<GUID, CVIndexMap> TypeServerIndexMappings;
165
166
  /// List of TypeServer PDBs which cannot be loaded.
167
  /// Cached to prevent repeated load attempts.
168
  std::set<GUID> MissingTypeServerPDBs;
169
};
170
}
171
172
static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
173
95
                                StringRef Name) {
174
95
  for (SectionChunk *C : Sections)
175
102
    if (C->getSectionName() == Name)
176
45
      return C;
177
95
  
return nullptr50
;
178
95
}
179
180
static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
181
124
                                           StringRef SecName) {
182
124
  // First 4 bytes are section magic.
183
124
  if (Data.size() < 4)
184
0
    fatal(SecName + " too short");
185
124
  if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
186
0
    fatal(SecName + " has an invalid magic");
187
124
  return Data.slice(4);
188
124
}
189
190
91
static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
191
91
  if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
192
42
    return consumeDebugMagic(Sec->getContents(), SecName);
193
49
  return {};
194
49
}
195
196
// A COFF .debug$H section is currently a clang extension.  This function checks
197
// if a .debug$H section is in a format that we expect / understand, so that we
198
// can ignore any sections which are coincidentally also named .debug$H but do
199
// not contain a format we recognize.
200
3
static bool canUseDebugH(ArrayRef<uint8_t> DebugH) {
201
3
  if (DebugH.size() < sizeof(object::debug_h_header))
202
0
    return false;
203
3
  auto *Header =
204
3
      reinterpret_cast<const object::debug_h_header *>(DebugH.data());
205
3
  DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
206
3
  return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
207
3
         Header->Version == 0 &&
208
3
         Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) &&
209
3
         (DebugH.size() % 8 == 0);
210
3
}
211
212
4
static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) {
213
4
  SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H");
214
4
  if (!Sec)
215
1
    return llvm::None;
216
3
  ArrayRef<uint8_t> Contents = Sec->getContents();
217
3
  if (!canUseDebugH(Contents))
218
0
    return None;
219
3
  return Contents;
220
3
}
221
222
static ArrayRef<GloballyHashedType>
223
3
getHashesFromDebugH(ArrayRef<uint8_t> DebugH) {
224
3
  assert(canUseDebugH(DebugH));
225
3
226
3
  DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
227
3
  uint32_t Count = DebugH.size() / sizeof(GloballyHashedType);
228
3
  return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count};
229
3
}
230
231
static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
232
136
                        TypeCollection &TypeTable) {
233
136
  // Start the TPI or IPI stream header.
234
136
  TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
235
136
236
136
  // Flatten the in memory type table and hash each type.
237
827
  TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
238
827
    auto Hash = pdb::hashTypeRecord(Type);
239
827
    if (auto E = Hash.takeError())
240
0
      fatal("type hashing error");
241
827
    TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
242
827
  });
243
136
}
244
245
static Optional<TypeServer2Record>
246
42
maybeReadTypeServerRecord(CVTypeArray &Types) {
247
42
  auto I = Types.begin();
248
42
  if (I == Types.end())
249
0
    return None;
250
42
  const CVType &Type = *I;
251
42
  if (Type.kind() != LF_TYPESERVER2)
252
39
    return None;
253
3
  TypeServer2Record TS;
254
3
  if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
255
0
    fatal("error reading type server record: " + toString(std::move(EC)));
256
3
  return std::move(TS);
257
3
}
258
259
Expected<const CVIndexMap&> PDBLinker::mergeDebugT(ObjFile *File,
260
91
                                                   CVIndexMap &ObjectIndexMap) {
261
91
  ScopedTimer T(TypeMergingTimer);
262
91
263
91
  ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
264
91
  if (Data.empty())
265
49
    return ObjectIndexMap;
266
42
267
42
  BinaryByteStream Stream(Data, support::little);
268
42
  CVTypeArray Types;
269
42
  BinaryStreamReader Reader(Stream);
270
42
  if (auto EC = Reader.readArray(Types, Reader.getLength()))
271
0
    fatal("Reader::readArray failed: " + toString(std::move(EC)));
272
42
273
42
  // Look through type servers. If we've already seen this type server, don't
274
42
  // merge any type information.
275
42
  if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
276
3
    return maybeMergeTypeServerPDB(File, *TS);
277
39
278
39
  // This is a /Z7 object. Fill in the temporary, caller-provided
279
39
  // ObjectIndexMap.
280
39
  if (Config->DebugGHashes) {
281
4
    ArrayRef<GloballyHashedType> Hashes;
282
4
    std::vector<GloballyHashedType> OwnedHashes;
283
4
    if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File))
284
3
      Hashes = getHashesFromDebugH(*DebugH);
285
1
    else {
286
1
      OwnedHashes = GloballyHashedType::hashTypes(Types);
287
1
      Hashes = OwnedHashes;
288
1
    }
289
4
290
4
    if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable,
291
0
                                         ObjectIndexMap.TPIMap, Types, Hashes))
292
0
      fatal("codeview::mergeTypeAndIdRecords failed: " +
293
0
            toString(std::move(Err)));
294
35
  } else {
295
35
    if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
296
0
                                         ObjectIndexMap.TPIMap, Types))
297
0
      fatal("codeview::mergeTypeAndIdRecords failed: " +
298
0
            toString(std::move(Err)));
299
39
  }
300
39
  return ObjectIndexMap;
301
39
}
302
303
static Expected<std::unique_ptr<pdb::NativeSession>>
304
4
tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
305
4
  ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
306
4
      TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
307
4
  if (!MBOrErr)
308
3
    return errorCodeToError(MBOrErr.getError());
309
1
310
1
  std::unique_ptr<pdb::IPDBSession> ThisSession;
311
1
  if (auto EC = pdb::NativeSession::createFromPdb(
312
0
          MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)),
313
0
                                     /*RequiresNullTerminator=*/false),
314
0
          ThisSession))
315
0
    return std::move(EC);
316
1
317
1
  std::unique_ptr<pdb::NativeSession> NS(
318
1
      static_cast<pdb::NativeSession *>(ThisSession.release()));
319
1
  pdb::PDBFile &File = NS->getPDBFile();
320
1
  auto ExpectedInfo = File.getPDBInfoStream();
321
1
  // All PDB Files should have an Info stream.
322
1
  if (!ExpectedInfo)
323
0
    return ExpectedInfo.takeError();
324
1
325
1
  // Just because a file with a matching name was found and it was an actual
326
1
  // PDB file doesn't mean it matches.  For it to match the InfoStream's GUID
327
1
  // must match the GUID specified in the TypeServer2 record.
328
1
  if (ExpectedInfo->getGuid() != GuidFromObj)
329
0
    return make_error<pdb::GenericError>(
330
0
        pdb::generic_error_code::type_server_not_found, TSPath);
331
1
332
1
  return std::move(NS);
333
1
}
334
335
Expected<const CVIndexMap&> PDBLinker::maybeMergeTypeServerPDB(ObjFile *File,
336
3
                                                               TypeServer2Record &TS) {
337
3
  const GUID& TSId = TS.getGuid();
338
3
  StringRef TSPath = TS.getName();
339
3
340
3
  // First, check if the PDB has previously failed to load.
341
3
  if (MissingTypeServerPDBs.count(TSId))
342
0
    return make_error<pdb::GenericError>(
343
0
      pdb::generic_error_code::type_server_not_found, TSPath);
344
3
345
3
  // Second, check if we already loaded a PDB with this GUID. Return the type
346
3
  // index mapping if we have it.
347
3
  auto Insertion = TypeServerIndexMappings.insert({TSId, CVIndexMap()});
348
3
  CVIndexMap &IndexMap = Insertion.first->second;
349
3
  if (!Insertion.second)
350
1
    return IndexMap;
351
2
352
2
  // Mark this map as a type server map.
353
2
  IndexMap.IsTypeServerMap = true;
354
2
355
2
  // Check for a PDB at:
356
2
  // 1. The given file path
357
2
  // 2. Next to the object file or archive file
358
2
  auto ExpectedSession = tryToLoadPDB(TSId, TSPath);
359
2
  if (!ExpectedSession) {
360
2
    consumeError(ExpectedSession.takeError());
361
2
    StringRef LocalPath =
362
2
        !File->ParentName.empty() ? 
File->ParentName0
: File->getName();
363
2
    SmallString<128> Path = sys::path::parent_path(LocalPath);
364
2
    sys::path::append(
365
2
        Path, sys::path::filename(TSPath, sys::path::Style::windows));
366
2
    ExpectedSession = tryToLoadPDB(TSId, Path);
367
2
  }
368
2
  if (auto E = ExpectedSession.takeError()) {
369
1
    TypeServerIndexMappings.erase(TSId);
370
1
    MissingTypeServerPDBs.emplace(TSId);
371
1
    return std::move(E);
372
1
  }
373
1
374
1
  pdb::NativeSession *Session = ExpectedSession->get();
375
1
376
1
  // Keep a strong reference to this PDB, so that it's safe to hold pointers
377
1
  // into the file.
378
1
  LoadedPDBs.push_back(std::move(*ExpectedSession));
379
1
380
1
  auto ExpectedTpi = Session->getPDBFile().getPDBTpiStream();
381
1
  if (auto E = ExpectedTpi.takeError())
382
0
    fatal("Type server does not have TPI stream: " + toString(std::move(E)));
383
1
  auto ExpectedIpi = Session->getPDBFile().getPDBIpiStream();
384
1
  if (auto E = ExpectedIpi.takeError())
385
0
    fatal("Type server does not have TPI stream: " + toString(std::move(E)));
386
1
387
1
  if (Config->DebugGHashes) {
388
0
    // PDBs do not actually store global hashes, so when merging a type server
389
0
    // PDB we have to synthesize global hashes.  To do this, we first synthesize
390
0
    // global hashes for the TPI stream, since it is independent, then we
391
0
    // synthesize hashes for the IPI stream, using the hashes for the TPI stream
392
0
    // as inputs.
393
0
    auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray());
394
0
    auto IpiHashes =
395
0
        GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes);
396
0
397
0
    // Merge TPI first, because the IPI stream will reference type indices.
398
0
    if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap,
399
0
                                    ExpectedTpi->typeArray(), TpiHashes))
400
0
      fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
401
0
402
0
    // Merge IPI.
403
0
    if (auto Err =
404
0
            mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap,
405
0
                           ExpectedIpi->typeArray(), IpiHashes))
406
0
      fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
407
1
  } else {
408
1
    // Merge TPI first, because the IPI stream will reference type indices.
409
1
    if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
410
0
                                    ExpectedTpi->typeArray()))
411
0
      fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
412
1
413
1
    // Merge IPI.
414
1
    if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
415
0
                                  ExpectedIpi->typeArray()))
416
0
      fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
417
1
  }
418
1
419
1
  return IndexMap;
420
1
}
421
422
262
static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
423
262
  if (TI.isSimple())
424
42
    return true;
425
220
  if (TI.toArrayIndex() >= TypeIndexMap.size())
426
1
    return false;
427
219
  TI = TypeIndexMap[TI.toArrayIndex()];
428
219
  return true;
429
219
}
430
431
static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
432
                                     MutableArrayRef<uint8_t> Contents,
433
                                     const CVIndexMap &IndexMap,
434
491
                                     ArrayRef<TiReference> TypeRefs) {
435
491
  for (const TiReference &Ref : TypeRefs) {
436
262
    unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
437
262
    if (Contents.size() < Ref.Offset + ByteSize)
438
0
      fatal("symbol record too short");
439
262
440
262
    // This can be an item index or a type index. Choose the appropriate map.
441
262
    ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
442
262
    bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
443
262
    if (IsItemIndex && 
IndexMap.IsTypeServerMap94
)
444
4
      TypeOrItemMap = IndexMap.IPIMap;
445
262
446
262
    MutableArrayRef<TypeIndex> TIs(
447
262
        reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
448
262
    for (TypeIndex &TI : TIs) {
449
262
      if (!remapTypeIndex(TI, TypeOrItemMap)) {
450
1
        log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " +
451
1
            File->getName() + " with bad " + (IsItemIndex ? "item" : 
"type"0
) +
452
1
            " index 0x" + utohexstr(TI.getIndex()));
453
1
        TI = TypeIndex(SimpleTypeKind::NotTranslated);
454
1
        continue;
455
1
      }
456
262
    }
457
262
  }
458
491
}
459
460
static void
461
recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> Contents,
462
                                   uint32_t Offset,
463
3
                                   std::vector<ulittle32_t *> &StrTableRefs) {
464
3
  Contents =
465
3
      Contents.drop_front(Offset).take_front(sizeof(support::ulittle32_t));
466
3
  ulittle32_t *Index = reinterpret_cast<ulittle32_t *>(Contents.data());
467
3
  StrTableRefs.push_back(Index);
468
3
}
469
470
static void
471
recordStringTableReferences(SymbolKind Kind, MutableArrayRef<uint8_t> Contents,
472
491
                            std::vector<ulittle32_t *> &StrTableRefs) {
473
491
  // For now we only handle S_FILESTATIC, but we may need the same logic for
474
491
  // S_DEFRANGE and S_DEFRANGE_SUBFIELD.  However, I cannot seem to generate any
475
491
  // PDBs that contain these types of records, so because of the uncertainty
476
491
  // they are omitted here until we can prove that it's necessary.
477
491
  switch (Kind) {
478
491
  case SymbolKind::S_FILESTATIC:
479
3
    // FileStaticSym::ModFileOffset
480
3
    recordStringTableReferenceAtOffset(Contents, 4, StrTableRefs);
481
3
    break;
482
491
  case SymbolKind::S_DEFRANGE:
483
0
  case SymbolKind::S_DEFRANGE_SUBFIELD:
484
0
    log("Not fixing up string table reference in S_DEFRANGE / "
485
0
        "S_DEFRANGE_SUBFIELD record");
486
0
    break;
487
488
  default:
488
488
    break;
489
491
  }
490
491
}
491
492
982
static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
493
982
  const RecordPrefix *Prefix =
494
982
      reinterpret_cast<const RecordPrefix *>(RecordData.data());
495
982
  return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
496
982
}
497
498
/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
499
static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData,
500
491
                               TypeCollection &IDTable) {
501
491
  RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
502
491
503
491
  SymbolKind Kind = symbolKind(RecordData);
504
491
505
491
  if (Kind == SymbolKind::S_PROC_ID_END) {
506
60
    Prefix->RecordKind = SymbolKind::S_END;
507
60
    return;
508
60
  }
509
431
510
431
  // In an object file, GPROC32_ID has an embedded reference which refers to the
511
431
  // single object file type index namespace.  This has already been translated
512
431
  // to the PDB file's ID stream index space, but we need to convert this to a
513
431
  // symbol that refers to the type stream index space.  So we remap again from
514
431
  // ID index space to type index space.
515
431
  if (Kind == SymbolKind::S_GPROC32_ID || 
Kind == SymbolKind::S_LPROC32_ID373
) {
516
59
    SmallVector<TiReference, 1> Refs;
517
59
    auto Content = RecordData.drop_front(sizeof(RecordPrefix));
518
59
    CVSymbol Sym(Kind, RecordData);
519
59
    discoverTypeIndicesInSymbol(Sym, Refs);
520
59
    assert(Refs.size() == 1);
521
59
    assert(Refs.front().Count == 1);
522
59
523
59
    TypeIndex *TI =
524
59
        reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
525
59
    // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
526
59
    // the IPI stream, whose `FunctionType` member refers to the TPI stream.
527
59
    // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
528
59
    // in both cases we just need the second type index.
529
59
    if (!TI->isSimple() && 
!TI->isNoneType()57
) {
530
57
      CVType FuncIdData = IDTable.getType(*TI);
531
57
      SmallVector<TypeIndex, 2> Indices;
532
57
      discoverTypeIndices(FuncIdData, Indices);
533
57
      assert(Indices.size() == 2);
534
57
      *TI = Indices[1];
535
57
    }
536
59
537
59
    Kind = (Kind == SymbolKind::S_GPROC32_ID) ? 
SymbolKind::S_GPROC3258
538
59
                                              : 
SymbolKind::S_LPROC321
;
539
59
    Prefix->RecordKind = uint16_t(Kind);
540
59
  }
541
431
}
542
543
/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
544
/// The object file may not be aligned.
545
static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
546
491
                                                 BumpPtrAllocator &Alloc) {
547
491
  size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
548
491
  assert(Size >= 4 && "record too short");
549
491
  assert(Size <= MaxRecordLength && "record too long");
550
491
  void *Mem = Alloc.Allocate(Size, 4);
551
491
552
491
  // Copy the symbol record and zero out any padding bytes.
553
491
  MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
554
491
  memcpy(NewData.data(), Sym.data().data(), Sym.length());
555
491
  memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
556
491
557
491
  // Update the record prefix length. It should point to the beginning of the
558
491
  // next record.
559
491
  auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
560
491
  Prefix->RecordLen = Size - 2;
561
491
  return NewData;
562
491
}
563
564
/// Return true if this symbol opens a scope. This implies that the symbol has
565
/// "parent" and "end" fields, which contain the offset of the S_END or
566
/// S_INLINESITE_END record.
567
491
static bool symbolOpensScope(SymbolKind Kind) {
568
491
  switch (Kind) {
569
491
  case SymbolKind::S_GPROC32:
570
65
  case SymbolKind::S_LPROC32:
571
65
  case SymbolKind::S_LPROC32_ID:
572
65
  case SymbolKind::S_GPROC32_ID:
573
65
  case SymbolKind::S_BLOCK32:
574
65
  case SymbolKind::S_SEPCODE:
575
65
  case SymbolKind::S_THUNK32:
576
65
  case SymbolKind::S_INLINESITE:
577
65
  case SymbolKind::S_INLINESITE2:
578
65
    return true;
579
426
  default:
580
426
    break;
581
426
  }
582
426
  return false;
583
426
}
584
585
426
static bool symbolEndsScope(SymbolKind Kind) {
586
426
  switch (Kind) {
587
426
  case SymbolKind::S_END:
588
65
  case SymbolKind::S_PROC_ID_END:
589
65
  case SymbolKind::S_INLINESITE_END:
590
65
    return true;
591
361
  default:
592
361
    break;
593
361
  }
594
361
  return false;
595
361
}
596
597
struct ScopeRecord {
598
  ulittle32_t PtrParent;
599
  ulittle32_t PtrEnd;
600
};
601
602
struct SymbolScope {
603
  ScopeRecord *OpeningRecord;
604
  uint32_t ScopeOffset;
605
};
606
607
static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
608
65
                           uint32_t CurOffset, CVSymbol &Sym) {
609
65
  assert(symbolOpensScope(Sym.kind()));
610
65
  SymbolScope S;
611
65
  S.ScopeOffset = CurOffset;
612
65
  S.OpeningRecord = const_cast<ScopeRecord *>(
613
65
      reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
614
65
  S.OpeningRecord->PtrParent = Stack.empty() ? 
060
:
Stack.back().ScopeOffset5
;
615
65
  Stack.push_back(S);
616
65
}
617
618
static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
619
65
                            uint32_t CurOffset, ObjFile *File) {
620
65
  if (Stack.empty()) {
621
0
    warn("symbol scopes are not balanced in " + File->getName());
622
0
    return;
623
0
  }
624
65
  SymbolScope S = Stack.pop_back_val();
625
65
  S.OpeningRecord->PtrEnd = CurOffset;
626
65
}
627
628
491
static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
629
491
  switch (Sym.kind()) {
630
491
  case SymbolKind::S_GDATA32:
631
108
  case SymbolKind::S_CONSTANT:
632
108
  case SymbolKind::S_UDT:
633
108
  // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
634
108
  // since they are synthesized by the linker in response to S_GPROC32 and
635
108
  // S_LPROC32, but if we do see them, don't put them in the module stream I
636
108
  // guess.
637
108
  case SymbolKind::S_PROCREF:
638
108
  case SymbolKind::S_LPROCREF:
639
108
    return false;
640
108
  // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
641
108
  case SymbolKind::S_LDATA32:
642
383
  default:
643
383
    return true;
644
491
  }
645
491
}
646
647
491
static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
648
491
  switch (Sym.kind()) {
649
491
  case SymbolKind::S_CONSTANT:
650
78
  case SymbolKind::S_GDATA32:
651
78
  // S_LDATA32 goes in both the module stream and the globals stream.
652
78
  case SymbolKind::S_LDATA32:
653
78
  case SymbolKind::S_GPROC32:
654
78
  case SymbolKind::S_LPROC32:
655
78
  // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
656
78
  // since they are synthesized by the linker in response to S_GPROC32 and
657
78
  // S_LPROC32, but if we do see them, copy them straight through.
658
78
  case SymbolKind::S_PROCREF:
659
78
  case SymbolKind::S_LPROCREF:
660
78
    return true;
661
78
  // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
662
78
  // globals stream or the modules stream).  These have special handling which
663
78
  // needs more investigation before we can get right, but by putting them all
664
78
  // into the globals stream WinDbg fails to display local variables of class
665
78
  // types saying that it cannot find the type Foo *.  So as a stopgap just to
666
78
  // keep things working, we drop them.
667
92
  case SymbolKind::S_UDT:
668
413
  default:
669
413
    return false;
670
491
  }
671
491
}
672
673
static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
674
78
                            const CVSymbol &Sym) {
675
78
  switch (Sym.kind()) {
676
78
  case SymbolKind::S_CONSTANT:
677
19
  case SymbolKind::S_UDT:
678
19
  case SymbolKind::S_GDATA32:
679
19
  case SymbolKind::S_LDATA32:
680
19
  case SymbolKind::S_PROCREF:
681
19
  case SymbolKind::S_LPROCREF:
682
19
    Builder.addGlobalSymbol(Sym);
683
19
    break;
684
59
  case SymbolKind::S_GPROC32:
685
59
  case SymbolKind::S_LPROC32: {
686
59
    SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
687
59
    if (Sym.kind() == SymbolKind::S_LPROC32)
688
1
      K = SymbolRecordKind::LocalProcRef;
689
59
    ProcRefSym PS(K);
690
59
    PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
691
59
    // For some reason, MSVC seems to add one to this value.
692
59
    ++PS.Module;
693
59
    PS.Name = getSymbolName(Sym);
694
59
    PS.SumName = 0;
695
59
    PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
696
59
    Builder.addGlobalSymbol(PS);
697
59
    break;
698
59
  }
699
59
  default:
700
0
    llvm_unreachable("Invalid symbol kind!");
701
78
  }
702
78
}
703
704
static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
705
                               pdb::GSIStreamBuilder &GsiBuilder,
706
                               const CVIndexMap &IndexMap,
707
                               TypeCollection &IDTable,
708
                               std::vector<ulittle32_t *> &StringTableRefs,
709
160
                               BinaryStreamRef SymData) {
710
160
  // FIXME: Improve error recovery by warning and skipping records when
711
160
  // possible.
712
160
  ArrayRef<uint8_t> SymsBuffer;
713
160
  cantFail(SymData.readBytes(0, SymData.getLength(), SymsBuffer));
714
160
  SmallVector<SymbolScope, 4> Scopes;
715
160
716
160
  auto EC = forEachCodeViewRecord<CVSymbol>(
717
491
      SymsBuffer, [&](const CVSymbol &Sym) -> llvm::Error {
718
491
        // Discover type index references in the record. Skip it if we don't
719
491
        // know where they are.
720
491
        SmallVector<TiReference, 32> TypeRefs;
721
491
        if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) {
722
0
          log("ignoring unknown symbol record with kind 0x" +
723
0
              utohexstr(Sym.kind()));
724
0
          return Error::success();
725
0
        }
726
491
727
491
        // Copy the symbol record so we can mutate it.
728
491
        MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
729
491
730
491
        // Re-map all the type index references.
731
491
        MutableArrayRef<uint8_t> Contents =
732
491
            NewData.drop_front(sizeof(RecordPrefix));
733
491
        remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap,
734
491
                                 TypeRefs);
735
491
736
491
        // An object file may have S_xxx_ID symbols, but these get converted to
737
491
        // "real" symbols in a PDB.
738
491
        translateIdSymbols(NewData, IDTable);
739
491
740
491
        // If this record refers to an offset in the object file's string table,
741
491
        // add that item to the global PDB string table and re-write the index.
742
491
        recordStringTableReferences(Sym.kind(), Contents, StringTableRefs);
743
491
744
491
        SymbolKind NewKind = symbolKind(NewData);
745
491
746
491
        // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
747
491
        CVSymbol NewSym(NewKind, NewData);
748
491
        if (symbolOpensScope(NewKind))
749
65
          scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(),
750
65
                         NewSym);
751
426
        else if (symbolEndsScope(NewKind))
752
65
          scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
753
491
754
491
        // Add the symbol to the globals stream if necessary.  Do this before
755
491
        // adding the symbol to the module since we may need to get the next
756
491
        // symbol offset, and writing to the module's symbol stream will update
757
491
        // that offset.
758
491
        if (symbolGoesInGlobalsStream(NewSym))
759
78
          addGlobalSymbol(GsiBuilder, *File, NewSym);
760
491
761
491
        // Add the symbol to the module.
762
491
        if (symbolGoesInModuleStream(NewSym))
763
383
          File->ModuleDBI->addSymbol(NewSym);
764
491
        return Error::success();
765
491
      });
766
160
  cantFail(std::move(EC));
767
160
}
768
769
// Allocate memory for a .debug$S section and relocate it.
770
static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
771
65
                                            SectionChunk *DebugChunk) {
772
65
  uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
773
65
  assert(DebugChunk->OutputSectionOff == 0 &&
774
65
         "debug sections should not be in output sections");
775
65
  DebugChunk->writeTo(Buffer);
776
65
  return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
777
65
                           ".debug$S");
778
65
}
779
780
560
static pdb::SectionContrib createSectionContrib(const Chunk *C, uint32_t Modi) {
781
560
  OutputSection *OS = C->getOutputSection();
782
560
  pdb::SectionContrib SC;
783
560
  memset(&SC, 0, sizeof(SC));
784
560
  SC.ISect = OS->SectionIndex;
785
560
  SC.Off = C->getRVA() - OS->getRVA();
786
560
  SC.Size = C->getSize();
787
560
  if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
788
289
    SC.Characteristics = SecChunk->Header->Characteristics;
789
289
    SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
790
289
    ArrayRef<uint8_t> Contents = SecChunk->getContents();
791
289
    JamCRC CRC(0);
792
289
    ArrayRef<char> CharContents = makeArrayRef(
793
289
        reinterpret_cast<const char *>(Contents.data()), Contents.size());
794
289
    CRC.update(CharContents);
795
289
    SC.DataCrc = CRC.getCRC();
796
289
  } else {
797
271
    SC.Characteristics = OS->Header.Characteristics;
798
271
    // FIXME: When we start creating DBI for import libraries, use those here.
799
271
    SC.Imod = Modi;
800
271
  }
801
560
  SC.RelocCrc = 0; // FIXME
802
560
803
560
  return SC;
804
560
}
805
806
91
void PDBLinker::addObjFile(ObjFile *File) {
807
91
  // Add a module descriptor for every object file. We need to put an absolute
808
91
  // path to the object into the PDB. If this is a plain object, we make its
809
91
  // path absolute. If it's an object in an archive, we make the archive path
810
91
  // absolute.
811
91
  bool InArchive = !File->ParentName.empty();
812
91
  SmallString<128> Path = InArchive ? 
File->ParentName3
:
File->getName()88
;
813
91
  sys::fs::make_absolute(Path);
814
91
  sys::path::native(Path, sys::path::Style::windows);
815
91
  StringRef Name = InArchive ? 
File->getName()3
:
StringRef(Path)88
;
816
91
817
91
  File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
818
91
  File->ModuleDBI->setObjFileName(Path);
819
91
820
91
  auto Chunks = File->getChunks();
821
91
  uint32_t Modi = File->ModuleDBI->getModuleIndex();
822
93
  for (Chunk *C : Chunks) {
823
93
    auto *SecChunk = dyn_cast<SectionChunk>(C);
824
93
    if (!SecChunk || !SecChunk->isLive())
825
2
      continue;
826
91
    pdb::SectionContrib SC = createSectionContrib(SecChunk, Modi);
827
91
    File->ModuleDBI->setFirstSectionContrib(SC);
828
91
    break;
829
91
  }
830
91
831
91
  // Before we can process symbol substreams from .debug$S, we need to process
832
91
  // type information, file checksums, and the string table.  Add type info to
833
91
  // the PDB first, so that we can get the map from object file type and item
834
91
  // indices to PDB type and item indices.
835
91
  CVIndexMap ObjectIndexMap;
836
91
  auto IndexMapResult = mergeDebugT(File, ObjectIndexMap);
837
91
838
91
  // If the .debug$T sections fail to merge, assume there is no debug info.
839
91
  if (!IndexMapResult) {
840
1
    warn("Type server PDB for " + Name + " is invalid, ignoring debug info. " +
841
1
         toString(IndexMapResult.takeError()));
842
1
    return;
843
1
  }
844
90
845
90
  const CVIndexMap &IndexMap = *IndexMapResult;
846
90
847
90
  ScopedTimer T(SymbolMergingTimer);
848
90
849
90
  // Now do all live .debug$S sections.
850
90
  DebugStringTableSubsectionRef CVStrTab;
851
90
  DebugChecksumsSubsectionRef Checksums;
852
90
  std::vector<ulittle32_t *> StringTableReferences;
853
111
  for (SectionChunk *DebugChunk : File->getDebugChunks()) {
854
111
    if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
855
46
      continue;
856
65
857
65
    ArrayRef<uint8_t> RelocatedDebugContents =
858
65
        relocateDebugChunk(Alloc, DebugChunk);
859
65
    if (RelocatedDebugContents.empty())
860
0
      continue;
861
65
862
65
    DebugSubsectionArray Subsections;
863
65
    BinaryStreamReader Reader(RelocatedDebugContents, support::little);
864
65
    ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
865
65
866
316
    for (const DebugSubsectionRecord &SS : Subsections) {
867
316
      switch (SS.kind()) {
868
316
      case DebugSubsectionKind::StringTable: {
869
46
        assert(!CVStrTab.valid() &&
870
46
               "Encountered multiple string table subsections!");
871
46
        ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
872
46
        break;
873
316
      }
874
316
      case DebugSubsectionKind::FileChecksums:
875
42
        assert(!Checksums.valid() &&
876
42
               "Encountered multiple checksum subsections!");
877
42
        ExitOnErr(Checksums.initialize(SS.getRecordData()));
878
42
        break;
879
316
      case DebugSubsectionKind::Lines:
880
57
        // We can add the relocated line table directly to the PDB without
881
57
        // modification because the file checksum offsets will stay the same.
882
57
        File->ModuleDBI->addDebugSubsection(SS);
883
57
        break;
884
316
      case DebugSubsectionKind::Symbols:
885
160
        if (Config->DebugGHashes) {
886
14
          mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
887
14
                             GlobalIDTable, StringTableReferences,
888
14
                             SS.getRecordData());
889
146
        } else {
890
146
          mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
891
146
                             IDTable, StringTableReferences,
892
146
                             SS.getRecordData());
893
146
        }
894
160
        break;
895
316
      default:
896
11
        // FIXME: Process the rest of the subsections.
897
11
        break;
898
316
      }
899
316
    }
900
65
  }
901
90
902
90
  // We should have seen all debug subsections across the entire object file now
903
90
  // which means that if a StringTable subsection and Checksums subsection were
904
90
  // present, now is the time to handle them.
905
90
  if (!CVStrTab.valid()) {
906
44
    if (Checksums.valid())
907
0
      fatal(".debug$S sections with a checksums subsection must also contain a "
908
0
            "string table subsection");
909
44
910
44
    if (!StringTableReferences.empty())
911
0
      warn("No StringTable subsection was encountered, but there are string "
912
0
           "table references");
913
44
    return;
914
44
  }
915
46
916
46
  // Rewrite each string table reference based on the value that the string
917
46
  // assumes in the final PDB.
918
46
  for (ulittle32_t *Ref : StringTableReferences) {
919
3
    auto ExpectedString = CVStrTab.getString(*Ref);
920
3
    if (!ExpectedString) {
921
0
      warn("Invalid string table reference");
922
0
      consumeError(ExpectedString.takeError());
923
0
      continue;
924
0
    }
925
3
926
3
    *Ref = PDBStrTab.insert(*ExpectedString);
927
3
  }
928
46
929
46
  // Make a new file checksum table that refers to offsets in the PDB-wide
930
46
  // string table. Generally the string table subsection appears after the
931
46
  // checksum table, so we have to do this after looping over all the
932
46
  // subsections.
933
46
  auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
934
50
  for (FileChecksumEntry &FC : Checksums) {
935
50
    StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
936
50
    ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
937
50
                                                          FileName));
938
50
    NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
939
50
  }
940
46
  File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
941
46
}
942
943
193
static PublicSym32 createPublic(Defined *Def) {
944
193
  PublicSym32 Pub(SymbolKind::S_PUB32);
945
193
  Pub.Name = Def->getName();
946
193
  if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
947
177
    if (D->getCOFFSymbol().isFunctionDefinition())
948
113
      Pub.Flags = PublicSymFlags::Function;
949
177
  } else 
if (16
isa<DefinedImportThunk>(Def)16
) {
950
8
    Pub.Flags = PublicSymFlags::Function;
951
8
  }
952
193
953
193
  OutputSection *OS = Def->getChunk()->getOutputSection();
954
193
  assert(OS && "all publics should be in final image");
955
193
  Pub.Offset = Def->getRVA() - OS->getRVA();
956
193
  Pub.Segment = OS->SectionIndex;
957
193
  return Pub;
958
193
}
959
960
// Add all object files to the PDB. Merge .debug$T sections into IpiData and
961
// TpiData.
962
68
void PDBLinker::addObjectsToPDB() {
963
68
  ScopedTimer T1(AddObjectsTimer);
964
68
  for (ObjFile *File : ObjFile::Instances)
965
91
    addObjFile(File);
966
68
967
68
  Builder.getStringTableBuilder().setStrings(PDBStrTab);
968
68
  T1.stop();
969
68
970
68
  // Construct TPI and IPI stream contents.
971
68
  ScopedTimer T2(TpiStreamLayoutTimer);
972
68
  if (Config->DebugGHashes) {
973
2
    addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
974
2
    addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
975
66
  } else {
976
66
    addTypeInfo(Builder.getTpiBuilder(), TypeTable);
977
66
    addTypeInfo(Builder.getIpiBuilder(), IDTable);
978
66
  }
979
68
  T2.stop();
980
68
981
68
  ScopedTimer T3(GlobalsLayoutTimer);
982
68
  // Compute the public and global symbols.
983
68
  auto &GsiBuilder = Builder.getGsiBuilder();
984
68
  std::vector<PublicSym32> Publics;
985
859
  Symtab->forEachSymbol([&Publics](Symbol *S) {
986
859
    // Only emit defined, live symbols that have a chunk.
987
859
    auto *Def = dyn_cast<Defined>(S);
988
859
    if (Def && 
Def->isLive()840
&&
Def->getChunk()834
)
989
193
      Publics.push_back(createPublic(Def));
990
859
  });
991
68
992
68
  if (!Publics.empty()) {
993
67
    // Sort the public symbols and add them to the stream.
994
67
    std::sort(Publics.begin(), Publics.end(),
995
218
              [](const PublicSym32 &L, const PublicSym32 &R) {
996
218
                return L.Name < R.Name;
997
218
              });
998
67
    for (const PublicSym32 &Pub : Publics)
999
193
      GsiBuilder.addPublicSymbol(Pub);
1000
67
  }
1001
68
}
1002
1003
68
void PDBLinker::addNatvisFiles() {
1004
68
  for (StringRef File : Config->NatvisFiles) {
1005
0
    ErrorOr<std::unique_ptr<MemoryBuffer>> DataOrErr =
1006
0
        MemoryBuffer::getFile(File);
1007
0
    if (!DataOrErr) {
1008
0
      warn("Cannot open input file: " + File);
1009
0
      continue;
1010
0
    }
1011
0
    Builder.addInjectedSource(File, std::move(*DataOrErr));
1012
0
  }
1013
68
}
1014
1015
68
static codeview::CPUType toCodeViewMachine(COFF::MachineTypes Machine) {
1016
68
  switch (Machine) {
1017
68
  case COFF::IMAGE_FILE_MACHINE_AMD64:
1018
54
    return codeview::CPUType::X64;
1019
68
  case COFF::IMAGE_FILE_MACHINE_ARM:
1020
0
    return codeview::CPUType::ARM7;
1021
68
  case COFF::IMAGE_FILE_MACHINE_ARM64:
1022
0
    return codeview::CPUType::ARM64;
1023
68
  case COFF::IMAGE_FILE_MACHINE_ARMNT:
1024
0
    return codeview::CPUType::ARMNT;
1025
68
  case COFF::IMAGE_FILE_MACHINE_I386:
1026
14
    return codeview::CPUType::Intel80386;
1027
68
  default:
1028
0
    llvm_unreachable("Unsupported CPU Type");
1029
68
  }
1030
68
}
1031
1032
static void addCommonLinkerModuleSymbols(StringRef Path,
1033
                                         pdb::DbiModuleDescriptorBuilder &Mod,
1034
68
                                         BumpPtrAllocator &Allocator) {
1035
68
  ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
1036
68
  Compile3Sym CS(SymbolRecordKind::Compile3Sym);
1037
68
  EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
1038
68
1039
68
  ONS.Name = "* Linker *";
1040
68
  ONS.Signature = 0;
1041
68
1042
68
  CS.Machine = toCodeViewMachine(Config->Machine);
1043
68
  // Interestingly, if we set the string to 0.0.0.0, then when trying to view
1044
68
  // local variables WinDbg emits an error that private symbols are not present.
1045
68
  // By setting this to a valid MSVC linker version string, local variables are
1046
68
  // displayed properly.   As such, even though it is not representative of
1047
68
  // LLVM's version information, we need this for compatibility.
1048
68
  CS.Flags = CompileSym3Flags::None;
1049
68
  CS.VersionBackendBuild = 25019;
1050
68
  CS.VersionBackendMajor = 14;
1051
68
  CS.VersionBackendMinor = 10;
1052
68
  CS.VersionBackendQFE = 0;
1053
68
1054
68
  // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
1055
68
  // linker module (which is by definition a backend), so we don't need to do
1056
68
  // anything here.  Also, it seems we can use "LLVM Linker" for the linker name
1057
68
  // without any problems.  Only the backend version has to be hardcoded to a
1058
68
  // magic number.
1059
68
  CS.VersionFrontendBuild = 0;
1060
68
  CS.VersionFrontendMajor = 0;
1061
68
  CS.VersionFrontendMinor = 0;
1062
68
  CS.VersionFrontendQFE = 0;
1063
68
  CS.Version = "LLVM Linker";
1064
68
  CS.setLanguage(SourceLanguage::Link);
1065
68
1066
68
  ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
1067
68
  std::string ArgStr = llvm::join(Args, " ");
1068
68
  EBS.Fields.push_back("cwd");
1069
68
  SmallString<64> cwd;
1070
68
  sys::fs::current_path(cwd);
1071
68
  EBS.Fields.push_back(cwd);
1072
68
  EBS.Fields.push_back("exe");
1073
68
  SmallString<64> exe = Config->Argv[0];
1074
68
  llvm::sys::fs::make_absolute(exe);
1075
68
  EBS.Fields.push_back(exe);
1076
68
  EBS.Fields.push_back("pdb");
1077
68
  EBS.Fields.push_back(Path);
1078
68
  EBS.Fields.push_back("cmd");
1079
68
  EBS.Fields.push_back(ArgStr);
1080
68
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1081
68
      ONS, Allocator, CodeViewContainer::Pdb));
1082
68
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1083
68
      CS, Allocator, CodeViewContainer::Pdb));
1084
68
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1085
68
      EBS, Allocator, CodeViewContainer::Pdb));
1086
68
}
1087
1088
static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
1089
                                         OutputSection &OS,
1090
178
                                         BumpPtrAllocator &Allocator) {
1091
178
  SectionSym Sym(SymbolRecordKind::SectionSym);
1092
178
  Sym.Alignment = 12; // 2^12 = 4KB
1093
178
  Sym.Characteristics = OS.Header.Characteristics;
1094
178
  Sym.Length = OS.getVirtualSize();
1095
178
  Sym.Name = OS.Name;
1096
178
  Sym.Rva = OS.getRVA();
1097
178
  Sym.SectionNumber = OS.SectionIndex;
1098
178
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1099
178
      Sym, Allocator, CodeViewContainer::Pdb));
1100
178
}
1101
1102
// Creates a PDB file.
1103
void coff::createPDB(SymbolTable *Symtab,
1104
                     ArrayRef<OutputSection *> OutputSections,
1105
                     ArrayRef<uint8_t> SectionTable,
1106
68
                     const llvm::codeview::DebugInfo &BuildId) {
1107
68
  ScopedTimer T1(TotalPdbLinkTimer);
1108
68
  PDBLinker PDB(Symtab);
1109
68
1110
68
  PDB.initialize(BuildId);
1111
68
  PDB.addObjectsToPDB();
1112
68
  PDB.addSections(OutputSections, SectionTable);
1113
68
  PDB.addNatvisFiles();
1114
68
1115
68
  ScopedTimer T2(DiskCommitTimer);
1116
68
  PDB.commit();
1117
68
}
1118
1119
68
void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) {
1120
68
  ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
1121
68
1122
68
  // Create streams in MSF for predefined streams, namely
1123
68
  // PDB, TPI, DBI and IPI.
1124
408
  for (int I = 0; I < (int)pdb::kSpecialStreamCount; 
++I340
)
1125
340
    ExitOnErr(Builder.getMsfBuilder().addStream(0));
1126
68
1127
68
  // Add an Info stream.
1128
68
  auto &InfoBuilder = Builder.getInfoBuilder();
1129
68
  GUID uuid;
1130
68
  memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid));
1131
68
  InfoBuilder.setAge(BuildId.PDB70.Age);
1132
68
  InfoBuilder.setGuid(uuid);
1133
68
  InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
1134
68
1135
68
  // Add an empty DBI stream.
1136
68
  pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
1137
68
  DbiBuilder.setAge(BuildId.PDB70.Age);
1138
68
  DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
1139
68
  DbiBuilder.setMachineType(Config->Machine);
1140
68
  // Technically we are not link.exe 14.11, but there are known cases where
1141
68
  // debugging tools on Windows expect Microsoft-specific version numbers or
1142
68
  // they fail to work at all.  Since we know we produce PDBs that are
1143
68
  // compatible with LINK 14.11, we set that version number here.
1144
68
  DbiBuilder.setBuildNumber(14, 11);
1145
68
}
1146
1147
void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
1148
68
                            ArrayRef<uint8_t> SectionTable) {
1149
68
  // It's not entirely clear what this is, but the * Linker * module uses it.
1150
68
  pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
1151
68
  NativePath = Config->PDBPath;
1152
68
  sys::fs::make_absolute(NativePath);
1153
68
  sys::path::native(NativePath, sys::path::Style::windows);
1154
68
  uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
1155
68
  auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
1156
68
  LinkerModule.setPdbFilePathNI(PdbFilePathNI);
1157
68
  addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
1158
68
1159
68
  // Add section contributions. They must be ordered by ascending RVA.
1160
178
  for (OutputSection *OS : OutputSections) {
1161
178
    addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
1162
469
    for (Chunk *C : OS->getChunks()) {
1163
469
      pdb::SectionContrib SC =
1164
469
          createSectionContrib(C, LinkerModule.getModuleIndex());
1165
469
      Builder.getDbiBuilder().addSectionContrib(SC);
1166
469
    }
1167
178
  }
1168
68
1169
68
  // Add Section Map stream.
1170
68
  ArrayRef<object::coff_section> Sections = {
1171
68
      (const object::coff_section *)SectionTable.data(),
1172
68
      SectionTable.size() / sizeof(object::coff_section)};
1173
68
  SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
1174
68
  DbiBuilder.setSectionMap(SectionMap);
1175
68
1176
68
  // Add COFF section header stream.
1177
68
  ExitOnErr(
1178
68
      DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
1179
68
}
1180
1181
68
void PDBLinker::commit() {
1182
68
  // Write to a file.
1183
68
  ExitOnErr(Builder.commit(Config->PDBPath));
1184
68
}
1185
1186
static Expected<StringRef>
1187
getFileName(const DebugStringTableSubsectionRef &Strings,
1188
5
            const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID) {
1189
5
  auto Iter = Checksums.getArray().at(FileID);
1190
5
  if (Iter == Checksums.getArray().end())
1191
0
    return make_error<CodeViewError>(cv_error_code::no_records);
1192
5
  uint32_t Offset = Iter->FileNameOffset;
1193
5
  return Strings.getString(Offset);
1194
5
}
1195
1196
18
static uint32_t getSecrelReloc() {
1197
18
  switch (Config->Machine) {
1198
18
  case AMD64:
1199
18
    return COFF::IMAGE_REL_AMD64_SECREL;
1200
18
  case I386:
1201
0
    return COFF::IMAGE_REL_I386_SECREL;
1202
18
  case ARMNT:
1203
0
    return COFF::IMAGE_REL_ARM_SECREL;
1204
18
  case ARM64:
1205
0
    return COFF::IMAGE_REL_ARM64_SECREL;
1206
18
  default:
1207
0
    llvm_unreachable("unknown machine type");
1208
18
  }
1209
18
}
1210
1211
// Try to find a line table for the given offset Addr into the given chunk C.
1212
// If a line table was found, the line table, the string and checksum tables
1213
// that are used to interpret the line table, and the offset of Addr in the line
1214
// table are stored in the output arguments. Returns whether a line table was
1215
// found.
1216
static bool findLineTable(const SectionChunk *C, uint32_t Addr,
1217
                          DebugStringTableSubsectionRef &CVStrTab,
1218
                          DebugChecksumsSubsectionRef &Checksums,
1219
                          DebugLinesSubsectionRef &Lines,
1220
18
                          uint32_t &OffsetInLinetable) {
1221
18
  ExitOnError ExitOnErr;
1222
18
  uint32_t SecrelReloc = getSecrelReloc();
1223
18
1224
18
  for (SectionChunk *DbgC : C->File->getDebugChunks()) {
1225
17
    if (DbgC->getSectionName() != ".debug$S")
1226
0
      continue;
1227
17
1228
17
    // Build a mapping of SECREL relocations in DbgC that refer to C.
1229
17
    DenseMap<uint32_t, uint32_t> Secrels;
1230
22
    for (const coff_relocation &R : DbgC->Relocs) {
1231
22
      if (R.Type != SecrelReloc)
1232
11
        continue;
1233
11
1234
11
      if (auto *S = dyn_cast_or_null<DefinedRegular>(
1235
11
              C->File->getSymbols()[R.SymbolTableIndex]))
1236
11
        if (S->getChunk() == C)
1237
9
          Secrels[R.VirtualAddress] = S->getValue();
1238
11
    }
1239
17
1240
17
    ArrayRef<uint8_t> Contents =
1241
17
        consumeDebugMagic(DbgC->getContents(), ".debug$S");
1242
17
    DebugSubsectionArray Subsections;
1243
17
    BinaryStreamReader Reader(Contents, support::little);
1244
17
    ExitOnErr(Reader.readArray(Subsections, Contents.size()));
1245
17
1246
24
    for (const DebugSubsectionRecord &SS : Subsections) {
1247
24
      switch (SS.kind()) {
1248
24
      case DebugSubsectionKind::StringTable: {
1249
5
        assert(!CVStrTab.valid() &&
1250
5
               "Encountered multiple string table subsections!");
1251
5
        ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
1252
5
        break;
1253
24
      }
1254
24
      case DebugSubsectionKind::FileChecksums:
1255
5
        assert(!Checksums.valid() &&
1256
5
               "Encountered multiple checksum subsections!");
1257
5
        ExitOnErr(Checksums.initialize(SS.getRecordData()));
1258
5
        break;
1259
24
      case DebugSubsectionKind::Lines: {
1260
8
        ArrayRef<uint8_t> Bytes;
1261
8
        auto Ref = SS.getRecordData();
1262
8
        ExitOnErr(Ref.readLongestContiguousChunk(0, Bytes));
1263
8
        size_t OffsetInDbgC = Bytes.data() - DbgC->getContents().data();
1264
8
1265
8
        // Check whether this line table refers to C.
1266
8
        auto I = Secrels.find(OffsetInDbgC);
1267
8
        if (I == Secrels.end())
1268
2
          break;
1269
6
1270
6
        // Check whether this line table covers Addr in C.
1271
6
        DebugLinesSubsectionRef LinesTmp;
1272
6
        ExitOnErr(LinesTmp.initialize(BinaryStreamReader(Ref)));
1273
6
        uint32_t OffsetInC = I->second + LinesTmp.header()->RelocOffset;
1274
6
        if (Addr < OffsetInC || Addr >= OffsetInC + LinesTmp.header()->CodeSize)
1275
1
          break;
1276
5
1277
5
        assert(!Lines.header() &&
1278
5
               "Encountered multiple line tables for function!");
1279
5
        ExitOnErr(Lines.initialize(BinaryStreamReader(Ref)));
1280
5
        OffsetInLinetable = Addr - OffsetInC;
1281
5
        break;
1282
5
      }
1283
6
      default:
1284
6
        break;
1285
24
      }
1286
24
1287
24
      if (CVStrTab.valid() && 
Checksums.valid()13
&&
Lines.header()13
)
1288
5
        return true;
1289
24
    }
1290
17
  }
1291
18
1292
18
  
return false13
;
1293
18
}
1294
1295
// Use CodeView line tables to resolve a file and line number for the given
1296
// offset into the given chunk and return them, or {"", 0} if a line table was
1297
// not found.
1298
std::pair<StringRef, uint32_t> coff::getFileLine(const SectionChunk *C,
1299
18
                                                 uint32_t Addr) {
1300
18
  ExitOnError ExitOnErr;
1301
18
1302
18
  DebugStringTableSubsectionRef CVStrTab;
1303
18
  DebugChecksumsSubsectionRef Checksums;
1304
18
  DebugLinesSubsectionRef Lines;
1305
18
  uint32_t OffsetInLinetable;
1306
18
1307
18
  if (!findLineTable(C, Addr, CVStrTab, Checksums, Lines, OffsetInLinetable))
1308
13
    return {"", 0};
1309
5
1310
5
  uint32_t NameIndex;
1311
5
  uint32_t LineNumber;
1312
7
  for (LineColumnEntry &Entry : Lines) {
1313
10
    for (const LineNumberEntry &LN : Entry.LineNumbers) {
1314
10
      if (LN.Offset > OffsetInLinetable) {
1315
2
        StringRef Filename =
1316
2
            ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex));
1317
2
        return {Filename, LineNumber};
1318
2
      }
1319
8
      LineInfo LI(LN.Flags);
1320
8
      NameIndex = Entry.NameIndex;
1321
8
      LineNumber = LI.getStartLine();
1322
8
    }
1323
7
  }
1324
5
  StringRef Filename = ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex));
1325
3
  return {Filename, LineNumber};
1326
5
}