Coverage Report

Created: 2018-08-19 21:11

/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
70
        IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) {
89
70
    // This isn't strictly necessary, but link.exe usually puts an empty string
90
70
    // as the first "valid" string in the string table, so we do the same in
91
70
    // order to maintain as much byte-for-byte compatibility as possible.
92
70
    PDBStrTab.insert("");
93
70
  }
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
99
                                StringRef Name) {
174
99
  for (SectionChunk *C : Sections)
175
110
    if (C->getSectionName() == Name)
176
49
      return C;
177
99
  
return nullptr50
;
178
99
}
179
180
static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
181
133
                                           StringRef SecName) {
182
133
  // First 4 bytes are section magic.
183
133
  if (Data.size() < 4)
184
0
    fatal(SecName + " too short");
185
133
  if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
186
0
    fatal(SecName + " has an invalid magic");
187
133
  return Data.slice(4);
188
133
}
189
190
95
static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
191
95
  if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
192
46
    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
140
                        TypeCollection &TypeTable) {
233
140
  // Start the TPI or IPI stream header.
234
140
  TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
235
140
236
140
  // Flatten the in memory type table and hash each type.
237
850
  TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
238
850
    auto Hash = pdb::hashTypeRecord(Type);
239
850
    if (auto E = Hash.takeError())
240
0
      fatal("type hashing error");
241
850
    TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
242
850
  });
243
140
}
244
245
static Optional<TypeServer2Record>
246
46
maybeReadTypeServerRecord(CVTypeArray &Types) {
247
46
  auto I = Types.begin();
248
46
  if (I == Types.end())
249
0
    return None;
250
46
  const CVType &Type = *I;
251
46
  if (Type.kind() != LF_TYPESERVER2)
252
43
    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
95
                                                   CVIndexMap &ObjectIndexMap) {
261
95
  ScopedTimer T(TypeMergingTimer);
262
95
263
95
  ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
264
95
  if (Data.empty())
265
49
    return ObjectIndexMap;
266
46
267
46
  BinaryByteStream Stream(Data, support::little);
268
46
  CVTypeArray Types;
269
46
  BinaryStreamReader Reader(Stream);
270
46
  if (auto EC = Reader.readArray(Types, Reader.getLength()))
271
0
    fatal("Reader::readArray failed: " + toString(std::move(EC)));
272
46
273
46
  // Look through type servers. If we've already seen this type server, don't
274
46
  // merge any type information.
275
46
  if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
276
3
    return maybeMergeTypeServerPDB(File, *TS);
277
43
278
43
  // This is a /Z7 object. Fill in the temporary, caller-provided
279
43
  // ObjectIndexMap.
280
43
  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
39
  } else {
295
39
    if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
296
0
                                         ObjectIndexMap.TPIMap, Types))
297
0
      fatal("codeview::mergeTypeAndIdRecords failed: " +
298
0
            toString(std::move(Err)));
299
43
  }
300
43
  return ObjectIndexMap;
301
43
}
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
269
static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
423
269
  if (TI.isSimple())
424
42
    return true;
425
227
  if (TI.toArrayIndex() >= TypeIndexMap.size())
426
1
    return false;
427
226
  TI = TypeIndexMap[TI.toArrayIndex()];
428
226
  return true;
429
226
}
430
431
static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
432
                                     MutableArrayRef<uint8_t> Contents,
433
                                     const CVIndexMap &IndexMap,
434
511
                                     ArrayRef<TiReference> TypeRefs) {
435
511
  for (const TiReference &Ref : TypeRefs) {
436
269
    unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
437
269
    if (Contents.size() < Ref.Offset + ByteSize)
438
0
      fatal("symbol record too short");
439
269
440
269
    // This can be an item index or a type index. Choose the appropriate map.
441
269
    ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
442
269
    bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
443
269
    if (IsItemIndex && 
IndexMap.IsTypeServerMap101
)
444
4
      TypeOrItemMap = IndexMap.IPIMap;
445
269
446
269
    MutableArrayRef<TypeIndex> TIs(
447
269
        reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
448
269
    for (TypeIndex &TI : TIs) {
449
269
      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
269
    }
457
269
  }
458
511
}
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
511
                            std::vector<ulittle32_t *> &StrTableRefs) {
473
511
  // For now we only handle S_FILESTATIC, but we may need the same logic for
474
511
  // S_DEFRANGE and S_DEFRANGE_SUBFIELD.  However, I cannot seem to generate any
475
511
  // PDBs that contain these types of records, so because of the uncertainty
476
511
  // they are omitted here until we can prove that it's necessary.
477
511
  switch (Kind) {
478
511
  case SymbolKind::S_FILESTATIC:
479
3
    // FileStaticSym::ModFileOffset
480
3
    recordStringTableReferenceAtOffset(Contents, 4, StrTableRefs);
481
3
    break;
482
511
  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
508
  default:
488
508
    break;
489
511
  }
490
511
}
491
492
1.02k
static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
493
1.02k
  const RecordPrefix *Prefix =
494
1.02k
      reinterpret_cast<const RecordPrefix *>(RecordData.data());
495
1.02k
  return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
496
1.02k
}
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
511
                               TypeCollection &IDTable) {
501
511
  RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
502
511
503
511
  SymbolKind Kind = symbolKind(RecordData);
504
511
505
511
  if (Kind == SymbolKind::S_PROC_ID_END) {
506
65
    Prefix->RecordKind = SymbolKind::S_END;
507
65
    return;
508
65
  }
509
446
510
446
  // In an object file, GPROC32_ID has an embedded reference which refers to the
511
446
  // single object file type index namespace.  This has already been translated
512
446
  // to the PDB file's ID stream index space, but we need to convert this to a
513
446
  // symbol that refers to the type stream index space.  So we remap again from
514
446
  // ID index space to type index space.
515
446
  if (Kind == SymbolKind::S_GPROC32_ID || 
Kind == SymbolKind::S_LPROC32_ID383
) {
516
64
    SmallVector<TiReference, 1> Refs;
517
64
    auto Content = RecordData.drop_front(sizeof(RecordPrefix));
518
64
    CVSymbol Sym(Kind, RecordData);
519
64
    discoverTypeIndicesInSymbol(Sym, Refs);
520
64
    assert(Refs.size() == 1);
521
64
    assert(Refs.front().Count == 1);
522
64
523
64
    TypeIndex *TI =
524
64
        reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
525
64
    // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
526
64
    // the IPI stream, whose `FunctionType` member refers to the TPI stream.
527
64
    // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
528
64
    // in both cases we just need the second type index.
529
64
    if (!TI->isSimple() && 
!TI->isNoneType()62
) {
530
62
      CVType FuncIdData = IDTable.getType(*TI);
531
62
      SmallVector<TypeIndex, 2> Indices;
532
62
      discoverTypeIndices(FuncIdData, Indices);
533
62
      assert(Indices.size() == 2);
534
62
      *TI = Indices[1];
535
62
    }
536
64
537
64
    Kind = (Kind == SymbolKind::S_GPROC32_ID) ? 
SymbolKind::S_GPROC3263
538
64
                                              : 
SymbolKind::S_LPROC321
;
539
64
    Prefix->RecordKind = uint16_t(Kind);
540
64
  }
541
446
}
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
511
                                                 BumpPtrAllocator &Alloc) {
547
511
  size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
548
511
  assert(Size >= 4 && "record too short");
549
511
  assert(Size <= MaxRecordLength && "record too long");
550
511
  void *Mem = Alloc.Allocate(Size, 4);
551
511
552
511
  // Copy the symbol record and zero out any padding bytes.
553
511
  MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
554
511
  memcpy(NewData.data(), Sym.data().data(), Sym.length());
555
511
  memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
556
511
557
511
  // Update the record prefix length. It should point to the beginning of the
558
511
  // next record.
559
511
  auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
560
511
  Prefix->RecordLen = Size - 2;
561
511
  return NewData;
562
511
}
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
511
static bool symbolOpensScope(SymbolKind Kind) {
568
511
  switch (Kind) {
569
511
  case SymbolKind::S_GPROC32:
570
70
  case SymbolKind::S_LPROC32:
571
70
  case SymbolKind::S_LPROC32_ID:
572
70
  case SymbolKind::S_GPROC32_ID:
573
70
  case SymbolKind::S_BLOCK32:
574
70
  case SymbolKind::S_SEPCODE:
575
70
  case SymbolKind::S_THUNK32:
576
70
  case SymbolKind::S_INLINESITE:
577
70
  case SymbolKind::S_INLINESITE2:
578
70
    return true;
579
441
  default:
580
441
    break;
581
441
  }
582
441
  return false;
583
441
}
584
585
441
static bool symbolEndsScope(SymbolKind Kind) {
586
441
  switch (Kind) {
587
441
  case SymbolKind::S_END:
588
70
  case SymbolKind::S_PROC_ID_END:
589
70
  case SymbolKind::S_INLINESITE_END:
590
70
    return true;
591
371
  default:
592
371
    break;
593
371
  }
594
371
  return false;
595
371
}
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
70
                           uint32_t CurOffset, CVSymbol &Sym) {
609
70
  assert(symbolOpensScope(Sym.kind()));
610
70
  SymbolScope S;
611
70
  S.ScopeOffset = CurOffset;
612
70
  S.OpeningRecord = const_cast<ScopeRecord *>(
613
70
      reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
614
70
  S.OpeningRecord->PtrParent = Stack.empty() ? 
065
:
Stack.back().ScopeOffset5
;
615
70
  Stack.push_back(S);
616
70
}
617
618
static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
619
70
                            uint32_t CurOffset, ObjFile *File) {
620
70
  if (Stack.empty()) {
621
0
    warn("symbol scopes are not balanced in " + File->getName());
622
0
    return;
623
0
  }
624
70
  SymbolScope S = Stack.pop_back_val();
625
70
  S.OpeningRecord->PtrEnd = CurOffset;
626
70
}
627
628
511
static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
629
511
  switch (Sym.kind()) {
630
511
  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
403
  default:
643
403
    return true;
644
511
  }
645
511
}
646
647
511
static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
648
511
  switch (Sym.kind()) {
649
511
  case SymbolKind::S_CONSTANT:
650
83
  case SymbolKind::S_GDATA32:
651
83
  // S_LDATA32 goes in both the module stream and the globals stream.
652
83
  case SymbolKind::S_LDATA32:
653
83
  case SymbolKind::S_GPROC32:
654
83
  case SymbolKind::S_LPROC32:
655
83
  // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
656
83
  // since they are synthesized by the linker in response to S_GPROC32 and
657
83
  // S_LPROC32, but if we do see them, copy them straight through.
658
83
  case SymbolKind::S_PROCREF:
659
83
  case SymbolKind::S_LPROCREF:
660
83
    return true;
661
83
  // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
662
83
  // globals stream or the modules stream).  These have special handling which
663
83
  // needs more investigation before we can get right, but by putting them all
664
83
  // into the globals stream WinDbg fails to display local variables of class
665
83
  // types saying that it cannot find the type Foo *.  So as a stopgap just to
666
83
  // keep things working, we drop them.
667
92
  case SymbolKind::S_UDT:
668
428
  default:
669
428
    return false;
670
511
  }
671
511
}
672
673
static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
674
83
                            const CVSymbol &Sym) {
675
83
  switch (Sym.kind()) {
676
83
  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
64
  case SymbolKind::S_GPROC32:
685
64
  case SymbolKind::S_LPROC32: {
686
64
    SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
687
64
    if (Sym.kind() == SymbolKind::S_LPROC32)
688
1
      K = SymbolRecordKind::LocalProcRef;
689
64
    ProcRefSym PS(K);
690
64
    PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
691
64
    // For some reason, MSVC seems to add one to this value.
692
64
    ++PS.Module;
693
64
    PS.Name = getSymbolName(Sym);
694
64
    PS.SumName = 0;
695
64
    PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
696
64
    Builder.addGlobalSymbol(PS);
697
64
    break;
698
64
  }
699
64
  default:
700
0
    llvm_unreachable("Invalid symbol kind!");
701
83
  }
702
83
}
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
171
                               BinaryStreamRef SymData) {
710
171
  // FIXME: Improve error recovery by warning and skipping records when
711
171
  // possible.
712
171
  ArrayRef<uint8_t> SymsBuffer;
713
171
  cantFail(SymData.readBytes(0, SymData.getLength(), SymsBuffer));
714
171
  SmallVector<SymbolScope, 4> Scopes;
715
171
716
171
  auto EC = forEachCodeViewRecord<CVSymbol>(
717
511
      SymsBuffer, [&](const CVSymbol &Sym) -> llvm::Error {
718
511
        // Discover type index references in the record. Skip it if we don't
719
511
        // know where they are.
720
511
        SmallVector<TiReference, 32> TypeRefs;
721
511
        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
511
727
511
        // Copy the symbol record so we can mutate it.
728
511
        MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
729
511
730
511
        // Re-map all the type index references.
731
511
        MutableArrayRef<uint8_t> Contents =
732
511
            NewData.drop_front(sizeof(RecordPrefix));
733
511
        remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap,
734
511
                                 TypeRefs);
735
511
736
511
        // An object file may have S_xxx_ID symbols, but these get converted to
737
511
        // "real" symbols in a PDB.
738
511
        translateIdSymbols(NewData, IDTable);
739
511
740
511
        // If this record refers to an offset in the object file's string table,
741
511
        // add that item to the global PDB string table and re-write the index.
742
511
        recordStringTableReferences(Sym.kind(), Contents, StringTableRefs);
743
511
744
511
        SymbolKind NewKind = symbolKind(NewData);
745
511
746
511
        // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
747
511
        CVSymbol NewSym(NewKind, NewData);
748
511
        if (symbolOpensScope(NewKind))
749
70
          scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(),
750
70
                         NewSym);
751
441
        else if (symbolEndsScope(NewKind))
752
70
          scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
753
511
754
511
        // Add the symbol to the globals stream if necessary.  Do this before
755
511
        // adding the symbol to the module since we may need to get the next
756
511
        // symbol offset, and writing to the module's symbol stream will update
757
511
        // that offset.
758
511
        if (symbolGoesInGlobalsStream(NewSym))
759
83
          addGlobalSymbol(GsiBuilder, *File, NewSym);
760
511
761
511
        // Add the symbol to the module.
762
511
        if (symbolGoesInModuleStream(NewSym))
763
403
          File->ModuleDBI->addSymbol(NewSym);
764
511
        return Error::success();
765
511
      });
766
171
  cantFail(std::move(EC));
767
171
}
768
769
// Allocate memory for a .debug$S section and relocate it.
770
static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
771
70
                                            SectionChunk *DebugChunk) {
772
70
  uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
773
70
  assert(DebugChunk->OutputSectionOff == 0 &&
774
70
         "debug sections should not be in output sections");
775
70
  DebugChunk->writeTo(Buffer);
776
70
  return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
777
70
                           ".debug$S");
778
70
}
779
780
576
static pdb::SectionContrib createSectionContrib(const Chunk *C, uint32_t Modi) {
781
576
  OutputSection *OS = C->getOutputSection();
782
576
  pdb::SectionContrib SC;
783
576
  memset(&SC, 0, sizeof(SC));
784
576
  SC.ISect = OS->SectionIndex;
785
576
  SC.Off = C->getRVA() - OS->getRVA();
786
576
  SC.Size = C->getSize();
787
576
  if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
788
304
    SC.Characteristics = SecChunk->Header->Characteristics;
789
304
    SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
790
304
    ArrayRef<uint8_t> Contents = SecChunk->getContents();
791
304
    JamCRC CRC(0);
792
304
    ArrayRef<char> CharContents = makeArrayRef(
793
304
        reinterpret_cast<const char *>(Contents.data()), Contents.size());
794
304
    CRC.update(CharContents);
795
304
    SC.DataCrc = CRC.getCRC();
796
304
  } else {
797
272
    SC.Characteristics = OS->Header.Characteristics;
798
272
    // FIXME: When we start creating DBI for import libraries, use those here.
799
272
    SC.Imod = Modi;
800
272
  }
801
576
  SC.RelocCrc = 0; // FIXME
802
576
803
576
  return SC;
804
576
}
805
806
95
void PDBLinker::addObjFile(ObjFile *File) {
807
95
  // Add a module descriptor for every object file. We need to put an absolute
808
95
  // path to the object into the PDB. If this is a plain object, we make its
809
95
  // path absolute. If it's an object in an archive, we make the archive path
810
95
  // absolute.
811
95
  bool InArchive = !File->ParentName.empty();
812
95
  SmallString<128> Path = InArchive ? 
File->ParentName3
:
File->getName()92
;
813
95
  sys::fs::make_absolute(Path);
814
95
  sys::path::native(Path, sys::path::Style::windows);
815
95
  StringRef Name = InArchive ? 
File->getName()3
:
StringRef(Path)92
;
816
95
817
95
  File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
818
95
  File->ModuleDBI->setObjFileName(Path);
819
95
820
95
  auto Chunks = File->getChunks();
821
95
  uint32_t Modi = File->ModuleDBI->getModuleIndex();
822
97
  for (Chunk *C : Chunks) {
823
97
    auto *SecChunk = dyn_cast<SectionChunk>(C);
824
97
    if (!SecChunk || !SecChunk->isLive())
825
2
      continue;
826
95
    pdb::SectionContrib SC = createSectionContrib(SecChunk, Modi);
827
95
    File->ModuleDBI->setFirstSectionContrib(SC);
828
95
    break;
829
95
  }
830
95
831
95
  // Before we can process symbol substreams from .debug$S, we need to process
832
95
  // type information, file checksums, and the string table.  Add type info to
833
95
  // the PDB first, so that we can get the map from object file type and item
834
95
  // indices to PDB type and item indices.
835
95
  CVIndexMap ObjectIndexMap;
836
95
  auto IndexMapResult = mergeDebugT(File, ObjectIndexMap);
837
95
838
95
  // If the .debug$T sections fail to merge, assume there is no debug info.
839
95
  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
94
845
94
  const CVIndexMap &IndexMap = *IndexMapResult;
846
94
847
94
  ScopedTimer T(SymbolMergingTimer);
848
94
849
94
  // Now do all live .debug$S sections.
850
94
  DebugStringTableSubsectionRef CVStrTab;
851
94
  DebugChecksumsSubsectionRef Checksums;
852
94
  std::vector<ulittle32_t *> StringTableReferences;
853
120
  for (SectionChunk *DebugChunk : File->getDebugChunks()) {
854
120
    if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
855
50
      continue;
856
70
857
70
    ArrayRef<uint8_t> RelocatedDebugContents =
858
70
        relocateDebugChunk(Alloc, DebugChunk);
859
70
    if (RelocatedDebugContents.empty())
860
0
      continue;
861
70
862
70
    DebugSubsectionArray Subsections;
863
70
    BinaryStreamReader Reader(RelocatedDebugContents, support::little);
864
70
    ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
865
70
866
340
    for (const DebugSubsectionRecord &SS : Subsections) {
867
340
      switch (SS.kind()) {
868
340
      case DebugSubsectionKind::StringTable: {
869
50
        assert(!CVStrTab.valid() &&
870
50
               "Encountered multiple string table subsections!");
871
50
        ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
872
50
        break;
873
340
      }
874
340
      case DebugSubsectionKind::FileChecksums:
875
46
        assert(!Checksums.valid() &&
876
46
               "Encountered multiple checksum subsections!");
877
46
        ExitOnErr(Checksums.initialize(SS.getRecordData()));
878
46
        break;
879
340
      case DebugSubsectionKind::Lines:
880
62
        // We can add the relocated line table directly to the PDB without
881
62
        // modification because the file checksum offsets will stay the same.
882
62
        File->ModuleDBI->addDebugSubsection(SS);
883
62
        break;
884
340
      case DebugSubsectionKind::Symbols:
885
171
        if (Config->DebugGHashes) {
886
14
          mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
887
14
                             GlobalIDTable, StringTableReferences,
888
14
                             SS.getRecordData());
889
157
        } else {
890
157
          mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
891
157
                             IDTable, StringTableReferences,
892
157
                             SS.getRecordData());
893
157
        }
894
171
        break;
895
340
      default:
896
11
        // FIXME: Process the rest of the subsections.
897
11
        break;
898
340
      }
899
340
    }
900
70
  }
901
94
902
94
  // We should have seen all debug subsections across the entire object file now
903
94
  // which means that if a StringTable subsection and Checksums subsection were
904
94
  // present, now is the time to handle them.
905
94
  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
50
916
50
  // Rewrite each string table reference based on the value that the string
917
50
  // assumes in the final PDB.
918
50
  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
50
929
50
  // Make a new file checksum table that refers to offsets in the PDB-wide
930
50
  // string table. Generally the string table subsection appears after the
931
50
  // checksum table, so we have to do this after looping over all the
932
50
  // subsections.
933
50
  auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
934
55
  for (FileChecksumEntry &FC : Checksums) {
935
55
    SmallString<128> FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
936
55
    if (!sys::path::is_absolute(FileName) &&
937
55
        !Config->PDBSourcePath.empty()) {
938
3
      SmallString<128> AbsoluteFileName = Config->PDBSourcePath;
939
3
      sys::path::append(AbsoluteFileName, FileName);
940
3
      sys::path::native(AbsoluteFileName);
941
3
      sys::path::remove_dots(AbsoluteFileName, /*remove_dot_dots=*/true);
942
3
      FileName = std::move(AbsoluteFileName);
943
3
    }
944
55
    ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
945
55
                                                          FileName));
946
55
    NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
947
55
  }
948
50
  File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
949
50
}
950
951
198
static PublicSym32 createPublic(Defined *Def) {
952
198
  PublicSym32 Pub(SymbolKind::S_PUB32);
953
198
  Pub.Name = Def->getName();
954
198
  if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
955
182
    if (D->getCOFFSymbol().isFunctionDefinition())
956
118
      Pub.Flags = PublicSymFlags::Function;
957
182
  } else 
if (16
isa<DefinedImportThunk>(Def)16
) {
958
8
    Pub.Flags = PublicSymFlags::Function;
959
8
  }
960
198
961
198
  OutputSection *OS = Def->getChunk()->getOutputSection();
962
198
  assert(OS && "all publics should be in final image");
963
198
  Pub.Offset = Def->getRVA() - OS->getRVA();
964
198
  Pub.Segment = OS->SectionIndex;
965
198
  return Pub;
966
198
}
967
968
// Add all object files to the PDB. Merge .debug$T sections into IpiData and
969
// TpiData.
970
70
void PDBLinker::addObjectsToPDB() {
971
70
  ScopedTimer T1(AddObjectsTimer);
972
70
  for (ObjFile *File : ObjFile::Instances)
973
95
    addObjFile(File);
974
70
975
70
  Builder.getStringTableBuilder().setStrings(PDBStrTab);
976
70
  T1.stop();
977
70
978
70
  // Construct TPI and IPI stream contents.
979
70
  ScopedTimer T2(TpiStreamLayoutTimer);
980
70
  if (Config->DebugGHashes) {
981
2
    addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
982
2
    addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
983
68
  } else {
984
68
    addTypeInfo(Builder.getTpiBuilder(), TypeTable);
985
68
    addTypeInfo(Builder.getIpiBuilder(), IDTable);
986
68
  }
987
70
  T2.stop();
988
70
989
70
  ScopedTimer T3(GlobalsLayoutTimer);
990
70
  // Compute the public and global symbols.
991
70
  auto &GsiBuilder = Builder.getGsiBuilder();
992
70
  std::vector<PublicSym32> Publics;
993
882
  Symtab->forEachSymbol([&Publics](Symbol *S) {
994
882
    // Only emit defined, live symbols that have a chunk.
995
882
    auto *Def = dyn_cast<Defined>(S);
996
882
    if (Def && 
Def->isLive()863
&&
Def->getChunk()857
)
997
198
      Publics.push_back(createPublic(Def));
998
882
  });
999
70
1000
70
  if (!Publics.empty()) {
1001
69
    // Sort the public symbols and add them to the stream.
1002
69
    std::sort(Publics.begin(), Publics.end(),
1003
222
              [](const PublicSym32 &L, const PublicSym32 &R) {
1004
222
                return L.Name < R.Name;
1005
222
              });
1006
69
    for (const PublicSym32 &Pub : Publics)
1007
198
      GsiBuilder.addPublicSymbol(Pub);
1008
69
  }
1009
70
}
1010
1011
70
void PDBLinker::addNatvisFiles() {
1012
70
  for (StringRef File : Config->NatvisFiles) {
1013
0
    ErrorOr<std::unique_ptr<MemoryBuffer>> DataOrErr =
1014
0
        MemoryBuffer::getFile(File);
1015
0
    if (!DataOrErr) {
1016
0
      warn("Cannot open input file: " + File);
1017
0
      continue;
1018
0
    }
1019
0
    Builder.addInjectedSource(File, std::move(*DataOrErr));
1020
0
  }
1021
70
}
1022
1023
70
static codeview::CPUType toCodeViewMachine(COFF::MachineTypes Machine) {
1024
70
  switch (Machine) {
1025
70
  case COFF::IMAGE_FILE_MACHINE_AMD64:
1026
56
    return codeview::CPUType::X64;
1027
70
  case COFF::IMAGE_FILE_MACHINE_ARM:
1028
0
    return codeview::CPUType::ARM7;
1029
70
  case COFF::IMAGE_FILE_MACHINE_ARM64:
1030
0
    return codeview::CPUType::ARM64;
1031
70
  case COFF::IMAGE_FILE_MACHINE_ARMNT:
1032
0
    return codeview::CPUType::ARMNT;
1033
70
  case COFF::IMAGE_FILE_MACHINE_I386:
1034
14
    return codeview::CPUType::Intel80386;
1035
70
  default:
1036
0
    llvm_unreachable("Unsupported CPU Type");
1037
70
  }
1038
70
}
1039
1040
static void addCommonLinkerModuleSymbols(StringRef Path,
1041
                                         pdb::DbiModuleDescriptorBuilder &Mod,
1042
70
                                         BumpPtrAllocator &Allocator) {
1043
70
  ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
1044
70
  Compile3Sym CS(SymbolRecordKind::Compile3Sym);
1045
70
  EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
1046
70
1047
70
  ONS.Name = "* Linker *";
1048
70
  ONS.Signature = 0;
1049
70
1050
70
  CS.Machine = toCodeViewMachine(Config->Machine);
1051
70
  // Interestingly, if we set the string to 0.0.0.0, then when trying to view
1052
70
  // local variables WinDbg emits an error that private symbols are not present.
1053
70
  // By setting this to a valid MSVC linker version string, local variables are
1054
70
  // displayed properly.   As such, even though it is not representative of
1055
70
  // LLVM's version information, we need this for compatibility.
1056
70
  CS.Flags = CompileSym3Flags::None;
1057
70
  CS.VersionBackendBuild = 25019;
1058
70
  CS.VersionBackendMajor = 14;
1059
70
  CS.VersionBackendMinor = 10;
1060
70
  CS.VersionBackendQFE = 0;
1061
70
1062
70
  // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
1063
70
  // linker module (which is by definition a backend), so we don't need to do
1064
70
  // anything here.  Also, it seems we can use "LLVM Linker" for the linker name
1065
70
  // without any problems.  Only the backend version has to be hardcoded to a
1066
70
  // magic number.
1067
70
  CS.VersionFrontendBuild = 0;
1068
70
  CS.VersionFrontendMajor = 0;
1069
70
  CS.VersionFrontendMinor = 0;
1070
70
  CS.VersionFrontendQFE = 0;
1071
70
  CS.Version = "LLVM Linker";
1072
70
  CS.setLanguage(SourceLanguage::Link);
1073
70
1074
70
  ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
1075
70
  std::string ArgStr = llvm::join(Args, " ");
1076
70
  EBS.Fields.push_back("cwd");
1077
70
  SmallString<64> cwd;
1078
70
  sys::fs::current_path(cwd);
1079
70
  EBS.Fields.push_back(cwd);
1080
70
  EBS.Fields.push_back("exe");
1081
70
  SmallString<64> exe = Config->Argv[0];
1082
70
  llvm::sys::fs::make_absolute(exe);
1083
70
  EBS.Fields.push_back(exe);
1084
70
  EBS.Fields.push_back("pdb");
1085
70
  EBS.Fields.push_back(Path);
1086
70
  EBS.Fields.push_back("cmd");
1087
70
  EBS.Fields.push_back(ArgStr);
1088
70
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1089
70
      ONS, Allocator, CodeViewContainer::Pdb));
1090
70
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1091
70
      CS, Allocator, CodeViewContainer::Pdb));
1092
70
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1093
70
      EBS, Allocator, CodeViewContainer::Pdb));
1094
70
}
1095
1096
static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
1097
                                         OutputSection &OS,
1098
184
                                         BumpPtrAllocator &Allocator) {
1099
184
  SectionSym Sym(SymbolRecordKind::SectionSym);
1100
184
  Sym.Alignment = 12; // 2^12 = 4KB
1101
184
  Sym.Characteristics = OS.Header.Characteristics;
1102
184
  Sym.Length = OS.getVirtualSize();
1103
184
  Sym.Name = OS.Name;
1104
184
  Sym.Rva = OS.getRVA();
1105
184
  Sym.SectionNumber = OS.SectionIndex;
1106
184
  Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1107
184
      Sym, Allocator, CodeViewContainer::Pdb));
1108
184
}
1109
1110
// Creates a PDB file.
1111
void coff::createPDB(SymbolTable *Symtab,
1112
                     ArrayRef<OutputSection *> OutputSections,
1113
                     ArrayRef<uint8_t> SectionTable,
1114
70
                     const llvm::codeview::DebugInfo &BuildId) {
1115
70
  ScopedTimer T1(TotalPdbLinkTimer);
1116
70
  PDBLinker PDB(Symtab);
1117
70
1118
70
  PDB.initialize(BuildId);
1119
70
  PDB.addObjectsToPDB();
1120
70
  PDB.addSections(OutputSections, SectionTable);
1121
70
  PDB.addNatvisFiles();
1122
70
1123
70
  ScopedTimer T2(DiskCommitTimer);
1124
70
  PDB.commit();
1125
70
}
1126
1127
70
void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) {
1128
70
  ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
1129
70
1130
70
  // Create streams in MSF for predefined streams, namely
1131
70
  // PDB, TPI, DBI and IPI.
1132
420
  for (int I = 0; I < (int)pdb::kSpecialStreamCount; 
++I350
)
1133
350
    ExitOnErr(Builder.getMsfBuilder().addStream(0));
1134
70
1135
70
  // Add an Info stream.
1136
70
  auto &InfoBuilder = Builder.getInfoBuilder();
1137
70
  GUID uuid;
1138
70
  memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid));
1139
70
  InfoBuilder.setAge(BuildId.PDB70.Age);
1140
70
  InfoBuilder.setGuid(uuid);
1141
70
  InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
1142
70
1143
70
  // Add an empty DBI stream.
1144
70
  pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
1145
70
  DbiBuilder.setAge(BuildId.PDB70.Age);
1146
70
  DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
1147
70
  DbiBuilder.setMachineType(Config->Machine);
1148
70
  // Technically we are not link.exe 14.11, but there are known cases where
1149
70
  // debugging tools on Windows expect Microsoft-specific version numbers or
1150
70
  // they fail to work at all.  Since we know we produce PDBs that are
1151
70
  // compatible with LINK 14.11, we set that version number here.
1152
70
  DbiBuilder.setBuildNumber(14, 11);
1153
70
}
1154
1155
void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
1156
70
                            ArrayRef<uint8_t> SectionTable) {
1157
70
  // It's not entirely clear what this is, but the * Linker * module uses it.
1158
70
  pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
1159
70
  NativePath = Config->PDBPath;
1160
70
  sys::fs::make_absolute(NativePath);
1161
70
  sys::path::native(NativePath, sys::path::Style::windows);
1162
70
  uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
1163
70
  auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
1164
70
  LinkerModule.setPdbFilePathNI(PdbFilePathNI);
1165
70
  addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
1166
70
1167
70
  // Add section contributions. They must be ordered by ascending RVA.
1168
184
  for (OutputSection *OS : OutputSections) {
1169
184
    addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
1170
481
    for (Chunk *C : OS->getChunks()) {
1171
481
      pdb::SectionContrib SC =
1172
481
          createSectionContrib(C, LinkerModule.getModuleIndex());
1173
481
      Builder.getDbiBuilder().addSectionContrib(SC);
1174
481
    }
1175
184
  }
1176
70
1177
70
  // Add Section Map stream.
1178
70
  ArrayRef<object::coff_section> Sections = {
1179
70
      (const object::coff_section *)SectionTable.data(),
1180
70
      SectionTable.size() / sizeof(object::coff_section)};
1181
70
  SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
1182
70
  DbiBuilder.setSectionMap(SectionMap);
1183
70
1184
70
  // Add COFF section header stream.
1185
70
  ExitOnErr(
1186
70
      DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
1187
70
}
1188
1189
70
void PDBLinker::commit() {
1190
70
  // Write to a file.
1191
70
  ExitOnErr(Builder.commit(Config->PDBPath));
1192
70
}
1193
1194
static Expected<StringRef>
1195
getFileName(const DebugStringTableSubsectionRef &Strings,
1196
5
            const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID) {
1197
5
  auto Iter = Checksums.getArray().at(FileID);
1198
5
  if (Iter == Checksums.getArray().end())
1199
0
    return make_error<CodeViewError>(cv_error_code::no_records);
1200
5
  uint32_t Offset = Iter->FileNameOffset;
1201
5
  return Strings.getString(Offset);
1202
5
}
1203
1204
19
static uint32_t getSecrelReloc() {
1205
19
  switch (Config->Machine) {
1206
19
  case AMD64:
1207
19
    return COFF::IMAGE_REL_AMD64_SECREL;
1208
19
  case I386:
1209
0
    return COFF::IMAGE_REL_I386_SECREL;
1210
19
  case ARMNT:
1211
0
    return COFF::IMAGE_REL_ARM_SECREL;
1212
19
  case ARM64:
1213
0
    return COFF::IMAGE_REL_ARM64_SECREL;
1214
19
  default:
1215
0
    llvm_unreachable("unknown machine type");
1216
19
  }
1217
19
}
1218
1219
// Try to find a line table for the given offset Addr into the given chunk C.
1220
// If a line table was found, the line table, the string and checksum tables
1221
// that are used to interpret the line table, and the offset of Addr in the line
1222
// table are stored in the output arguments. Returns whether a line table was
1223
// found.
1224
static bool findLineTable(const SectionChunk *C, uint32_t Addr,
1225
                          DebugStringTableSubsectionRef &CVStrTab,
1226
                          DebugChecksumsSubsectionRef &Checksums,
1227
                          DebugLinesSubsectionRef &Lines,
1228
19
                          uint32_t &OffsetInLinetable) {
1229
19
  ExitOnError ExitOnErr;
1230
19
  uint32_t SecrelReloc = getSecrelReloc();
1231
19
1232
19
  for (SectionChunk *DbgC : C->File->getDebugChunks()) {
1233
17
    if (DbgC->getSectionName() != ".debug$S")
1234
0
      continue;
1235
17
1236
17
    // Build a mapping of SECREL relocations in DbgC that refer to C.
1237
17
    DenseMap<uint32_t, uint32_t> Secrels;
1238
22
    for (const coff_relocation &R : DbgC->Relocs) {
1239
22
      if (R.Type != SecrelReloc)
1240
11
        continue;
1241
11
1242
11
      if (auto *S = dyn_cast_or_null<DefinedRegular>(
1243
11
              C->File->getSymbols()[R.SymbolTableIndex]))
1244
11
        if (S->getChunk() == C)
1245
9
          Secrels[R.VirtualAddress] = S->getValue();
1246
11
    }
1247
17
1248
17
    ArrayRef<uint8_t> Contents =
1249
17
        consumeDebugMagic(DbgC->getContents(), ".debug$S");
1250
17
    DebugSubsectionArray Subsections;
1251
17
    BinaryStreamReader Reader(Contents, support::little);
1252
17
    ExitOnErr(Reader.readArray(Subsections, Contents.size()));
1253
17
1254
24
    for (const DebugSubsectionRecord &SS : Subsections) {
1255
24
      switch (SS.kind()) {
1256
24
      case DebugSubsectionKind::StringTable: {
1257
5
        assert(!CVStrTab.valid() &&
1258
5
               "Encountered multiple string table subsections!");
1259
5
        ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
1260
5
        break;
1261
24
      }
1262
24
      case DebugSubsectionKind::FileChecksums:
1263
5
        assert(!Checksums.valid() &&
1264
5
               "Encountered multiple checksum subsections!");
1265
5
        ExitOnErr(Checksums.initialize(SS.getRecordData()));
1266
5
        break;
1267
24
      case DebugSubsectionKind::Lines: {
1268
8
        ArrayRef<uint8_t> Bytes;
1269
8
        auto Ref = SS.getRecordData();
1270
8
        ExitOnErr(Ref.readLongestContiguousChunk(0, Bytes));
1271
8
        size_t OffsetInDbgC = Bytes.data() - DbgC->getContents().data();
1272
8
1273
8
        // Check whether this line table refers to C.
1274
8
        auto I = Secrels.find(OffsetInDbgC);
1275
8
        if (I == Secrels.end())
1276
2
          break;
1277
6
1278
6
        // Check whether this line table covers Addr in C.
1279
6
        DebugLinesSubsectionRef LinesTmp;
1280
6
        ExitOnErr(LinesTmp.initialize(BinaryStreamReader(Ref)));
1281
6
        uint32_t OffsetInC = I->second + LinesTmp.header()->RelocOffset;
1282
6
        if (Addr < OffsetInC || Addr >= OffsetInC + LinesTmp.header()->CodeSize)
1283
1
          break;
1284
5
1285
5
        assert(!Lines.header() &&
1286
5
               "Encountered multiple line tables for function!");
1287
5
        ExitOnErr(Lines.initialize(BinaryStreamReader(Ref)));
1288
5
        OffsetInLinetable = Addr - OffsetInC;
1289
5
        break;
1290
5
      }
1291
6
      default:
1292
6
        break;
1293
24
      }
1294
24
1295
24
      if (CVStrTab.valid() && 
Checksums.valid()13
&&
Lines.header()13
)
1296
5
        return true;
1297
24
    }
1298
17
  }
1299
19
1300
19
  
return false14
;
1301
19
}
1302
1303
// Use CodeView line tables to resolve a file and line number for the given
1304
// offset into the given chunk and return them, or {"", 0} if a line table was
1305
// not found.
1306
std::pair<StringRef, uint32_t> coff::getFileLine(const SectionChunk *C,
1307
19
                                                 uint32_t Addr) {
1308
19
  ExitOnError ExitOnErr;
1309
19
1310
19
  DebugStringTableSubsectionRef CVStrTab;
1311
19
  DebugChecksumsSubsectionRef Checksums;
1312
19
  DebugLinesSubsectionRef Lines;
1313
19
  uint32_t OffsetInLinetable;
1314
19
1315
19
  if (!findLineTable(C, Addr, CVStrTab, Checksums, Lines, OffsetInLinetable))
1316
14
    return {"", 0};
1317
5
1318
5
  uint32_t NameIndex;
1319
5
  uint32_t LineNumber;
1320
7
  for (LineColumnEntry &Entry : Lines) {
1321
10
    for (const LineNumberEntry &LN : Entry.LineNumbers) {
1322
10
      if (LN.Offset > OffsetInLinetable) {
1323
2
        StringRef Filename =
1324
2
            ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex));
1325
2
        return {Filename, LineNumber};
1326
2
      }
1327
8
      LineInfo LI(LN.Flags);
1328
8
      NameIndex = Entry.NameIndex;
1329
8
      LineNumber = LI.getStartLine();
1330
8
    }
1331
7
  }
1332
5
  StringRef Filename = ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex));
1333
3
  return {Filename, LineNumber};
1334
5
}