Coverage Report

Created: 2017-10-03 07:32

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