Coverage Report

Created: 2018-01-17 17:22

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