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