1 //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing Microsoft CodeView debug info.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeViewDebug.h"
14 #include "DwarfExpression.h"
15 #include "llvm/ADT/APSInt.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/TinyPtrVector.h"
27 #include "llvm/ADT/Triple.h"
28 #include "llvm/ADT/Twine.h"
29 #include "llvm/BinaryFormat/COFF.h"
30 #include "llvm/BinaryFormat/Dwarf.h"
31 #include "llvm/CodeGen/AsmPrinter.h"
32 #include "llvm/CodeGen/LexicalScopes.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineFunction.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/MachineModuleInfo.h"
37 #include "llvm/CodeGen/MachineOperand.h"
38 #include "llvm/CodeGen/TargetFrameLowering.h"
39 #include "llvm/CodeGen/TargetRegisterInfo.h"
40 #include "llvm/CodeGen/TargetSubtargetInfo.h"
41 #include "llvm/Config/llvm-config.h"
42 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
43 #include "llvm/DebugInfo/CodeView/CodeView.h"
44 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
45 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
46 #include "llvm/DebugInfo/CodeView/EnumTables.h"
47 #include "llvm/DebugInfo/CodeView/Line.h"
48 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
49 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
50 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
51 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
52 #include "llvm/DebugInfo/CodeView/TypeTableCollection.h"
53 #include "llvm/IR/Constants.h"
54 #include "llvm/IR/DataLayout.h"
55 #include "llvm/IR/DebugInfoMetadata.h"
56 #include "llvm/IR/DebugLoc.h"
57 #include "llvm/IR/Function.h"
58 #include "llvm/IR/GlobalValue.h"
59 #include "llvm/IR/GlobalVariable.h"
60 #include "llvm/IR/Metadata.h"
61 #include "llvm/IR/Module.h"
62 #include "llvm/MC/MCAsmInfo.h"
63 #include "llvm/MC/MCContext.h"
64 #include "llvm/MC/MCSectionCOFF.h"
65 #include "llvm/MC/MCStreamer.h"
66 #include "llvm/MC/MCSymbol.h"
67 #include "llvm/Support/BinaryByteStream.h"
68 #include "llvm/Support/BinaryStreamReader.h"
69 #include "llvm/Support/Casting.h"
70 #include "llvm/Support/CommandLine.h"
71 #include "llvm/Support/Compiler.h"
72 #include "llvm/Support/Endian.h"
73 #include "llvm/Support/Error.h"
74 #include "llvm/Support/ErrorHandling.h"
75 #include "llvm/Support/FormatVariadic.h"
76 #include "llvm/Support/Path.h"
77 #include "llvm/Support/SMLoc.h"
78 #include "llvm/Support/ScopedPrinter.h"
79 #include "llvm/Target/TargetLoweringObjectFile.h"
80 #include "llvm/Target/TargetMachine.h"
81 #include <algorithm>
82 #include <cassert>
83 #include <cctype>
84 #include <cstddef>
85 #include <cstdint>
86 #include <iterator>
87 #include <limits>
88 #include <string>
89 #include <utility>
90 #include <vector>
91 
92 using namespace llvm;
93 using namespace llvm::codeview;
94 
95 static CPUType mapArchToCVCPUType(Triple::ArchType Type) {
96   switch (Type) {
97   case Triple::ArchType::x86:
98     return CPUType::Pentium3;
99   case Triple::ArchType::x86_64:
100     return CPUType::X64;
101   case Triple::ArchType::thumb:
102     return CPUType::Thumb;
103   case Triple::ArchType::aarch64:
104     return CPUType::ARM64;
105   default:
106     report_fatal_error("target architecture doesn't map to a CodeView CPUType");
107   }
108 }
109 
110 CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
111     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {
112   // If module doesn't have named metadata anchors or COFF debug section
113   // is not available, skip any debug info related stuff.
114   if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
115       !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {
116     Asm = nullptr;
117     MMI->setDebugInfoAvailability(false);
118     return;
119   }
120   // Tell MMI that we have debug info.
121   MMI->setDebugInfoAvailability(true);
122 
123   TheCPU =
124       mapArchToCVCPUType(Triple(MMI->getModule()->getTargetTriple()).getArch());
125 
126   collectGlobalVariableInfo();
127 
128   // Check if we should emit type record hashes.
129   ConstantInt *GH = mdconst::extract_or_null<ConstantInt>(
130       MMI->getModule()->getModuleFlag("CodeViewGHash"));
131   EmitDebugGlobalHashes = GH && !GH->isZero();
132 }
133 
134 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
135   std::string &Filepath = FileToFilepathMap[File];
136   if (!Filepath.empty())
137     return Filepath;
138 
139   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
140 
141   // If this is a Unix-style path, just use it as is. Don't try to canonicalize
142   // it textually because one of the path components could be a symlink.
143   if (Dir.startswith("/") || Filename.startswith("/")) {
144     if (llvm::sys::path::is_absolute(Filename, llvm::sys::path::Style::posix))
145       return Filename;
146     Filepath = Dir;
147     if (Dir.back() != '/')
148       Filepath += '/';
149     Filepath += Filename;
150     return Filepath;
151   }
152 
153   // Clang emits directory and relative filename info into the IR, but CodeView
154   // operates on full paths.  We could change Clang to emit full paths too, but
155   // that would increase the IR size and probably not needed for other users.
156   // For now, just concatenate and canonicalize the path here.
157   if (Filename.find(':') == 1)
158     Filepath = Filename;
159   else
160     Filepath = (Dir + "\\" + Filename).str();
161 
162   // Canonicalize the path.  We have to do it textually because we may no longer
163   // have access the file in the filesystem.
164   // First, replace all slashes with backslashes.
165   std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
166 
167   // Remove all "\.\" with "\".
168   size_t Cursor = 0;
169   while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
170     Filepath.erase(Cursor, 2);
171 
172   // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
173   // path should be well-formatted, e.g. start with a drive letter, etc.
174   Cursor = 0;
175   while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
176     // Something's wrong if the path starts with "\..\", abort.
177     if (Cursor == 0)
178       break;
179 
180     size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
181     if (PrevSlash == std::string::npos)
182       // Something's wrong, abort.
183       break;
184 
185     Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
186     // The next ".." might be following the one we've just erased.
187     Cursor = PrevSlash;
188   }
189 
190   // Remove all duplicate backslashes.
191   Cursor = 0;
192   while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
193     Filepath.erase(Cursor, 1);
194 
195   return Filepath;
196 }
197 
198 unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
199   StringRef FullPath = getFullFilepath(F);
200   unsigned NextId = FileIdMap.size() + 1;
201   auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));
202   if (Insertion.second) {
203     // We have to compute the full filepath and emit a .cv_file directive.
204     ArrayRef<uint8_t> ChecksumAsBytes;
205     FileChecksumKind CSKind = FileChecksumKind::None;
206     if (F->getChecksum()) {
207       std::string Checksum = fromHex(F->getChecksum()->Value);
208       void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
209       memcpy(CKMem, Checksum.data(), Checksum.size());
210       ChecksumAsBytes = ArrayRef<uint8_t>(
211           reinterpret_cast<const uint8_t *>(CKMem), Checksum.size());
212       switch (F->getChecksum()->Kind) {
213       case DIFile::CSK_MD5:  CSKind = FileChecksumKind::MD5; break;
214       case DIFile::CSK_SHA1: CSKind = FileChecksumKind::SHA1; break;
215       }
216     }
217     bool Success = OS.EmitCVFileDirective(NextId, FullPath, ChecksumAsBytes,
218                                           static_cast<unsigned>(CSKind));
219     (void)Success;
220     assert(Success && ".cv_file directive failed");
221   }
222   return Insertion.first->second;
223 }
224 
225 CodeViewDebug::InlineSite &
226 CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
227                              const DISubprogram *Inlinee) {
228   auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
229   InlineSite *Site = &SiteInsertion.first->second;
230   if (SiteInsertion.second) {
231     unsigned ParentFuncId = CurFn->FuncId;
232     if (const DILocation *OuterIA = InlinedAt->getInlinedAt())
233       ParentFuncId =
234           getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram())
235               .SiteFuncId;
236 
237     Site->SiteFuncId = NextFuncId++;
238     OS.EmitCVInlineSiteIdDirective(
239         Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()),
240         InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc());
241     Site->Inlinee = Inlinee;
242     InlinedSubprograms.insert(Inlinee);
243     getFuncIdForSubprogram(Inlinee);
244   }
245   return *Site;
246 }
247 
248 static StringRef getPrettyScopeName(const DIScope *Scope) {
249   StringRef ScopeName = Scope->getName();
250   if (!ScopeName.empty())
251     return ScopeName;
252 
253   switch (Scope->getTag()) {
254   case dwarf::DW_TAG_enumeration_type:
255   case dwarf::DW_TAG_class_type:
256   case dwarf::DW_TAG_structure_type:
257   case dwarf::DW_TAG_union_type:
258     return "<unnamed-tag>";
259   case dwarf::DW_TAG_namespace:
260     return "`anonymous namespace'";
261   }
262 
263   return StringRef();
264 }
265 
266 static const DISubprogram *getQualifiedNameComponents(
267     const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
268   const DISubprogram *ClosestSubprogram = nullptr;
269   while (Scope != nullptr) {
270     if (ClosestSubprogram == nullptr)
271       ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
272     StringRef ScopeName = getPrettyScopeName(Scope);
273     if (!ScopeName.empty())
274       QualifiedNameComponents.push_back(ScopeName);
275     Scope = Scope->getScope().resolve();
276   }
277   return ClosestSubprogram;
278 }
279 
280 static std::string getQualifiedName(ArrayRef<StringRef> QualifiedNameComponents,
281                                     StringRef TypeName) {
282   std::string FullyQualifiedName;
283   for (StringRef QualifiedNameComponent :
284        llvm::reverse(QualifiedNameComponents)) {
285     FullyQualifiedName.append(QualifiedNameComponent);
286     FullyQualifiedName.append("::");
287   }
288   FullyQualifiedName.append(TypeName);
289   return FullyQualifiedName;
290 }
291 
292 static std::string getFullyQualifiedName(const DIScope *Scope, StringRef Name) {
293   SmallVector<StringRef, 5> QualifiedNameComponents;
294   getQualifiedNameComponents(Scope, QualifiedNameComponents);
295   return getQualifiedName(QualifiedNameComponents, Name);
296 }
297 
298 struct CodeViewDebug::TypeLoweringScope {
299   TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }
300   ~TypeLoweringScope() {
301     // Don't decrement TypeEmissionLevel until after emitting deferred types, so
302     // inner TypeLoweringScopes don't attempt to emit deferred types.
303     if (CVD.TypeEmissionLevel == 1)
304       CVD.emitDeferredCompleteTypes();
305     --CVD.TypeEmissionLevel;
306   }
307   CodeViewDebug &CVD;
308 };
309 
310 static std::string getFullyQualifiedName(const DIScope *Ty) {
311   const DIScope *Scope = Ty->getScope().resolve();
312   return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));
313 }
314 
315 TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {
316   // No scope means global scope and that uses the zero index.
317   if (!Scope || isa<DIFile>(Scope))
318     return TypeIndex();
319 
320   assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");
321 
322   // Check if we've already translated this scope.
323   auto I = TypeIndices.find({Scope, nullptr});
324   if (I != TypeIndices.end())
325     return I->second;
326 
327   // Build the fully qualified name of the scope.
328   std::string ScopeName = getFullyQualifiedName(Scope);
329   StringIdRecord SID(TypeIndex(), ScopeName);
330   auto TI = TypeTable.writeLeafType(SID);
331   return recordTypeIndexForDINode(Scope, TI);
332 }
333 
334 TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
335   assert(SP);
336 
337   // Check if we've already translated this subprogram.
338   auto I = TypeIndices.find({SP, nullptr});
339   if (I != TypeIndices.end())
340     return I->second;
341 
342   // The display name includes function template arguments. Drop them to match
343   // MSVC.
344   StringRef DisplayName = SP->getName().split('<').first;
345 
346   const DIScope *Scope = SP->getScope().resolve();
347   TypeIndex TI;
348   if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {
349     // If the scope is a DICompositeType, then this must be a method. Member
350     // function types take some special handling, and require access to the
351     // subprogram.
352     TypeIndex ClassType = getTypeIndex(Class);
353     MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),
354                                DisplayName);
355     TI = TypeTable.writeLeafType(MFuncId);
356   } else {
357     // Otherwise, this must be a free function.
358     TypeIndex ParentScope = getScopeIndex(Scope);
359     FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
360     TI = TypeTable.writeLeafType(FuncId);
361   }
362 
363   return recordTypeIndexForDINode(SP, TI);
364 }
365 
366 static bool isTrivial(const DICompositeType *DCTy) {
367   return ((DCTy->getFlags() & DINode::FlagTrivial) == DINode::FlagTrivial);
368 }
369 
370 static FunctionOptions
371 getFunctionOptions(const DISubroutineType *Ty,
372                    const DICompositeType *ClassTy = nullptr,
373                    StringRef SPName = StringRef("")) {
374   FunctionOptions FO = FunctionOptions::None;
375   const DIType *ReturnTy = nullptr;
376   if (auto TypeArray = Ty->getTypeArray()) {
377     if (TypeArray.size())
378       ReturnTy = TypeArray[0].resolve();
379   }
380 
381   if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(ReturnTy)) {
382     if (!isTrivial(ReturnDCTy))
383       FO |= FunctionOptions::CxxReturnUdt;
384   }
385 
386   // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison.
387   if (ClassTy && !isTrivial(ClassTy) && SPName == ClassTy->getName()) {
388     FO |= FunctionOptions::Constructor;
389 
390   // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag.
391 
392   }
393   return FO;
394 }
395 
396 TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,
397                                                const DICompositeType *Class) {
398   // Always use the method declaration as the key for the function type. The
399   // method declaration contains the this adjustment.
400   if (SP->getDeclaration())
401     SP = SP->getDeclaration();
402   assert(!SP->getDeclaration() && "should use declaration as key");
403 
404   // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide
405   // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.
406   auto I = TypeIndices.find({SP, Class});
407   if (I != TypeIndices.end())
408     return I->second;
409 
410   // Make sure complete type info for the class is emitted *after* the member
411   // function type, as the complete class type is likely to reference this
412   // member function type.
413   TypeLoweringScope S(*this);
414   const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0;
415 
416   FunctionOptions FO = getFunctionOptions(SP->getType(), Class, SP->getName());
417   TypeIndex TI = lowerTypeMemberFunction(
418       SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod, FO);
419   return recordTypeIndexForDINode(SP, TI, Class);
420 }
421 
422 TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,
423                                                   TypeIndex TI,
424                                                   const DIType *ClassTy) {
425   auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});
426   (void)InsertResult;
427   assert(InsertResult.second && "DINode was already assigned a type index");
428   return TI;
429 }
430 
431 unsigned CodeViewDebug::getPointerSizeInBytes() {
432   return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8;
433 }
434 
435 void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
436                                         const LexicalScope *LS) {
437   if (const DILocation *InlinedAt = LS->getInlinedAt()) {
438     // This variable was inlined. Associate it with the InlineSite.
439     const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
440     InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
441     Site.InlinedLocals.emplace_back(Var);
442   } else {
443     // This variable goes into the corresponding lexical scope.
444     ScopeVariables[LS].emplace_back(Var);
445   }
446 }
447 
448 static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
449                                const DILocation *Loc) {
450   auto B = Locs.begin(), E = Locs.end();
451   if (std::find(B, E, Loc) == E)
452     Locs.push_back(Loc);
453 }
454 
455 void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
456                                         const MachineFunction *MF) {
457   // Skip this instruction if it has the same location as the previous one.
458   if (!DL || DL == PrevInstLoc)
459     return;
460 
461   const DIScope *Scope = DL.get()->getScope();
462   if (!Scope)
463     return;
464 
465   // Skip this line if it is longer than the maximum we can record.
466   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
467   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
468       LI.isNeverStepInto())
469     return;
470 
471   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
472   if (CI.getStartColumn() != DL.getCol())
473     return;
474 
475   if (!CurFn->HaveLineInfo)
476     CurFn->HaveLineInfo = true;
477   unsigned FileId = 0;
478   if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile())
479     FileId = CurFn->LastFileId;
480   else
481     FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
482   PrevInstLoc = DL;
483 
484   unsigned FuncId = CurFn->FuncId;
485   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
486     const DILocation *Loc = DL.get();
487 
488     // If this location was actually inlined from somewhere else, give it the ID
489     // of the inline call site.
490     FuncId =
491         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
492 
493     // Ensure we have links in the tree of inline call sites.
494     bool FirstLoc = true;
495     while ((SiteLoc = Loc->getInlinedAt())) {
496       InlineSite &Site =
497           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
498       if (!FirstLoc)
499         addLocIfNotPresent(Site.ChildSites, Loc);
500       FirstLoc = false;
501       Loc = SiteLoc;
502     }
503     addLocIfNotPresent(CurFn->ChildSites, Loc);
504   }
505 
506   OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
507                         /*PrologueEnd=*/false, /*IsStmt=*/false,
508                         DL->getFilename(), SMLoc());
509 }
510 
511 void CodeViewDebug::emitCodeViewMagicVersion() {
512   OS.EmitValueToAlignment(4);
513   OS.AddComment("Debug section magic");
514   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
515 }
516 
517 void CodeViewDebug::endModule() {
518   if (!Asm || !MMI->hasDebugInfo())
519     return;
520 
521   assert(Asm != nullptr);
522 
523   // The COFF .debug$S section consists of several subsections, each starting
524   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
525   // of the payload followed by the payload itself.  The subsections are 4-byte
526   // aligned.
527 
528   // Use the generic .debug$S section, and make a subsection for all the inlined
529   // subprograms.
530   switchToDebugSectionForSymbol(nullptr);
531 
532   MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols);
533   emitCompilerInformation();
534   endCVSubsection(CompilerInfo);
535 
536   emitInlineeLinesSubsection();
537 
538   // Emit per-function debug information.
539   for (auto &P : FnDebugInfo)
540     if (!P.first->isDeclarationForLinker())
541       emitDebugInfoForFunction(P.first, *P.second);
542 
543   // Emit global variable debug information.
544   setCurrentSubprogram(nullptr);
545   emitDebugInfoForGlobals();
546 
547   // Emit retained types.
548   emitDebugInfoForRetainedTypes();
549 
550   // Switch back to the generic .debug$S section after potentially processing
551   // comdat symbol sections.
552   switchToDebugSectionForSymbol(nullptr);
553 
554   // Emit UDT records for any types used by global variables.
555   if (!GlobalUDTs.empty()) {
556     MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
557     emitDebugInfoForUDTs(GlobalUDTs);
558     endCVSubsection(SymbolsEnd);
559   }
560 
561   // This subsection holds a file index to offset in string table table.
562   OS.AddComment("File index to string table offset subsection");
563   OS.EmitCVFileChecksumsDirective();
564 
565   // This subsection holds the string table.
566   OS.AddComment("String table");
567   OS.EmitCVStringTableDirective();
568 
569   // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol
570   // subsection in the generic .debug$S section at the end. There is no
571   // particular reason for this ordering other than to match MSVC.
572   emitBuildInfo();
573 
574   // Emit type information and hashes last, so that any types we translate while
575   // emitting function info are included.
576   emitTypeInformation();
577 
578   if (EmitDebugGlobalHashes)
579     emitTypeGlobalHashes();
580 
581   clear();
582 }
583 
584 static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S,
585     unsigned MaxFixedRecordLength = 0xF00) {
586   // The maximum CV record length is 0xFF00. Most of the strings we emit appear
587   // after a fixed length portion of the record. The fixed length portion should
588   // always be less than 0xF00 (3840) bytes, so truncate the string so that the
589   // overall record size is less than the maximum allowed.
590   SmallString<32> NullTerminatedString(
591       S.take_front(MaxRecordLength - MaxFixedRecordLength - 1));
592   NullTerminatedString.push_back('\0');
593   OS.EmitBytes(NullTerminatedString);
594 }
595 
596 void CodeViewDebug::emitTypeInformation() {
597   if (TypeTable.empty())
598     return;
599 
600   // Start the .debug$T or .debug$P section with 0x4.
601   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
602   emitCodeViewMagicVersion();
603 
604   SmallString<8> CommentPrefix;
605   if (OS.isVerboseAsm()) {
606     CommentPrefix += '\t';
607     CommentPrefix += Asm->MAI->getCommentString();
608     CommentPrefix += ' ';
609   }
610 
611   TypeTableCollection Table(TypeTable.records());
612   Optional<TypeIndex> B = Table.getFirst();
613   while (B) {
614     // This will fail if the record data is invalid.
615     CVType Record = Table.getType(*B);
616 
617     if (OS.isVerboseAsm()) {
618       // Emit a block comment describing the type record for readability.
619       SmallString<512> CommentBlock;
620       raw_svector_ostream CommentOS(CommentBlock);
621       ScopedPrinter SP(CommentOS);
622       SP.setPrefix(CommentPrefix);
623       TypeDumpVisitor TDV(Table, &SP, false);
624 
625       Error E = codeview::visitTypeRecord(Record, *B, TDV);
626       if (E) {
627         logAllUnhandledErrors(std::move(E), errs(), "error: ");
628         llvm_unreachable("produced malformed type record");
629       }
630       // emitRawComment will insert its own tab and comment string before
631       // the first line, so strip off our first one. It also prints its own
632       // newline.
633       OS.emitRawComment(
634           CommentOS.str().drop_front(CommentPrefix.size() - 1).rtrim());
635     }
636     OS.EmitBinaryData(Record.str_data());
637     B = Table.getNext(*B);
638   }
639 }
640 
641 void CodeViewDebug::emitTypeGlobalHashes() {
642   if (TypeTable.empty())
643     return;
644 
645   // Start the .debug$H section with the version and hash algorithm, currently
646   // hardcoded to version 0, SHA1.
647   OS.SwitchSection(Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection());
648 
649   OS.EmitValueToAlignment(4);
650   OS.AddComment("Magic");
651   OS.EmitIntValue(COFF::DEBUG_HASHES_SECTION_MAGIC, 4);
652   OS.AddComment("Section Version");
653   OS.EmitIntValue(0, 2);
654   OS.AddComment("Hash Algorithm");
655   OS.EmitIntValue(uint16_t(GlobalTypeHashAlg::SHA1_8), 2);
656 
657   TypeIndex TI(TypeIndex::FirstNonSimpleIndex);
658   for (const auto &GHR : TypeTable.hashes()) {
659     if (OS.isVerboseAsm()) {
660       // Emit an EOL-comment describing which TypeIndex this hash corresponds
661       // to, as well as the stringified SHA1 hash.
662       SmallString<32> Comment;
663       raw_svector_ostream CommentOS(Comment);
664       CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR);
665       OS.AddComment(Comment);
666       ++TI;
667     }
668     assert(GHR.Hash.size() == 8);
669     StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()),
670                 GHR.Hash.size());
671     OS.EmitBinaryData(S);
672   }
673 }
674 
675 static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
676   switch (DWLang) {
677   case dwarf::DW_LANG_C:
678   case dwarf::DW_LANG_C89:
679   case dwarf::DW_LANG_C99:
680   case dwarf::DW_LANG_C11:
681   case dwarf::DW_LANG_ObjC:
682     return SourceLanguage::C;
683   case dwarf::DW_LANG_C_plus_plus:
684   case dwarf::DW_LANG_C_plus_plus_03:
685   case dwarf::DW_LANG_C_plus_plus_11:
686   case dwarf::DW_LANG_C_plus_plus_14:
687     return SourceLanguage::Cpp;
688   case dwarf::DW_LANG_Fortran77:
689   case dwarf::DW_LANG_Fortran90:
690   case dwarf::DW_LANG_Fortran03:
691   case dwarf::DW_LANG_Fortran08:
692     return SourceLanguage::Fortran;
693   case dwarf::DW_LANG_Pascal83:
694     return SourceLanguage::Pascal;
695   case dwarf::DW_LANG_Cobol74:
696   case dwarf::DW_LANG_Cobol85:
697     return SourceLanguage::Cobol;
698   case dwarf::DW_LANG_Java:
699     return SourceLanguage::Java;
700   case dwarf::DW_LANG_D:
701     return SourceLanguage::D;
702   default:
703     // There's no CodeView representation for this language, and CV doesn't
704     // have an "unknown" option for the language field, so we'll use MASM,
705     // as it's very low level.
706     return SourceLanguage::Masm;
707   }
708 }
709 
710 namespace {
711 struct Version {
712   int Part[4];
713 };
714 } // end anonymous namespace
715 
716 // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out
717 // the version number.
718 static Version parseVersion(StringRef Name) {
719   Version V = {{0}};
720   int N = 0;
721   for (const char C : Name) {
722     if (isdigit(C)) {
723       V.Part[N] *= 10;
724       V.Part[N] += C - '0';
725     } else if (C == '.') {
726       ++N;
727       if (N >= 4)
728         return V;
729     } else if (N > 0)
730       return V;
731   }
732   return V;
733 }
734 
735 void CodeViewDebug::emitCompilerInformation() {
736   MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3);
737   uint32_t Flags = 0;
738 
739   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
740   const MDNode *Node = *CUs->operands().begin();
741   const auto *CU = cast<DICompileUnit>(Node);
742 
743   // The low byte of the flags indicates the source language.
744   Flags = MapDWLangToCVLang(CU->getSourceLanguage());
745   // TODO:  Figure out which other flags need to be set.
746 
747   OS.AddComment("Flags and language");
748   OS.EmitIntValue(Flags, 4);
749 
750   OS.AddComment("CPUType");
751   OS.EmitIntValue(static_cast<uint64_t>(TheCPU), 2);
752 
753   StringRef CompilerVersion = CU->getProducer();
754   Version FrontVer = parseVersion(CompilerVersion);
755   OS.AddComment("Frontend version");
756   for (int N = 0; N < 4; ++N)
757     OS.EmitIntValue(FrontVer.Part[N], 2);
758 
759   // Some Microsoft tools, like Binscope, expect a backend version number of at
760   // least 8.something, so we'll coerce the LLVM version into a form that
761   // guarantees it'll be big enough without really lying about the version.
762   int Major = 1000 * LLVM_VERSION_MAJOR +
763               10 * LLVM_VERSION_MINOR +
764               LLVM_VERSION_PATCH;
765   // Clamp it for builds that use unusually large version numbers.
766   Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max());
767   Version BackVer = {{ Major, 0, 0, 0 }};
768   OS.AddComment("Backend version");
769   for (int N = 0; N < 4; ++N)
770     OS.EmitIntValue(BackVer.Part[N], 2);
771 
772   OS.AddComment("Null-terminated compiler version string");
773   emitNullTerminatedSymbolName(OS, CompilerVersion);
774 
775   endSymbolRecord(CompilerEnd);
776 }
777 
778 static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
779                                     StringRef S) {
780   StringIdRecord SIR(TypeIndex(0x0), S);
781   return TypeTable.writeLeafType(SIR);
782 }
783 
784 void CodeViewDebug::emitBuildInfo() {
785   // First, make LF_BUILDINFO. It's a sequence of strings with various bits of
786   // build info. The known prefix is:
787   // - Absolute path of current directory
788   // - Compiler path
789   // - Main source file path, relative to CWD or absolute
790   // - Type server PDB file
791   // - Canonical compiler command line
792   // If frontend and backend compilation are separated (think llc or LTO), it's
793   // not clear if the compiler path should refer to the executable for the
794   // frontend or the backend. Leave it blank for now.
795   TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};
796   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
797   const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.
798   const auto *CU = cast<DICompileUnit>(Node);
799   const DIFile *MainSourceFile = CU->getFile();
800   BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =
801       getStringIdTypeIdx(TypeTable, MainSourceFile->getDirectory());
802   BuildInfoArgs[BuildInfoRecord::SourceFile] =
803       getStringIdTypeIdx(TypeTable, MainSourceFile->getFilename());
804   // FIXME: Path to compiler and command line. PDB is intentionally blank unless
805   // we implement /Zi type servers.
806   BuildInfoRecord BIR(BuildInfoArgs);
807   TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
808 
809   // Make a new .debug$S subsection for the S_BUILDINFO record, which points
810   // from the module symbols into the type stream.
811   MCSymbol *BISubsecEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
812   MCSymbol *BIEnd = beginSymbolRecord(SymbolKind::S_BUILDINFO);
813   OS.AddComment("LF_BUILDINFO index");
814   OS.EmitIntValue(BuildInfoIndex.getIndex(), 4);
815   endSymbolRecord(BIEnd);
816   endCVSubsection(BISubsecEnd);
817 }
818 
819 void CodeViewDebug::emitInlineeLinesSubsection() {
820   if (InlinedSubprograms.empty())
821     return;
822 
823   OS.AddComment("Inlinee lines subsection");
824   MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines);
825 
826   // We emit the checksum info for files.  This is used by debuggers to
827   // determine if a pdb matches the source before loading it.  Visual Studio,
828   // for instance, will display a warning that the breakpoints are not valid if
829   // the pdb does not match the source.
830   OS.AddComment("Inlinee lines signature");
831   OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
832 
833   for (const DISubprogram *SP : InlinedSubprograms) {
834     assert(TypeIndices.count({SP, nullptr}));
835     TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];
836 
837     OS.AddBlankLine();
838     unsigned FileId = maybeRecordFile(SP->getFile());
839     OS.AddComment("Inlined function " + SP->getName() + " starts at " +
840                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
841     OS.AddBlankLine();
842     OS.AddComment("Type index of inlined function");
843     OS.EmitIntValue(InlineeIdx.getIndex(), 4);
844     OS.AddComment("Offset into filechecksum table");
845     OS.EmitCVFileChecksumOffsetDirective(FileId);
846     OS.AddComment("Starting line number");
847     OS.EmitIntValue(SP->getLine(), 4);
848   }
849 
850   endCVSubsection(InlineEnd);
851 }
852 
853 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
854                                         const DILocation *InlinedAt,
855                                         const InlineSite &Site) {
856   assert(TypeIndices.count({Site.Inlinee, nullptr}));
857   TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];
858 
859   // SymbolRecord
860   MCSymbol *InlineEnd = beginSymbolRecord(SymbolKind::S_INLINESITE);
861 
862   OS.AddComment("PtrParent");
863   OS.EmitIntValue(0, 4);
864   OS.AddComment("PtrEnd");
865   OS.EmitIntValue(0, 4);
866   OS.AddComment("Inlinee type index");
867   OS.EmitIntValue(InlineeIdx.getIndex(), 4);
868 
869   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
870   unsigned StartLineNum = Site.Inlinee->getLine();
871 
872   OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
873                                     FI.Begin, FI.End);
874 
875   endSymbolRecord(InlineEnd);
876 
877   emitLocalVariableList(FI, Site.InlinedLocals);
878 
879   // Recurse on child inlined call sites before closing the scope.
880   for (const DILocation *ChildSite : Site.ChildSites) {
881     auto I = FI.InlineSites.find(ChildSite);
882     assert(I != FI.InlineSites.end() &&
883            "child site not in function inline site map");
884     emitInlinedCallSite(FI, ChildSite, I->second);
885   }
886 
887   // Close the scope.
888   emitEndSymbolRecord(SymbolKind::S_INLINESITE_END);
889 }
890 
891 void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
892   // If we have a symbol, it may be in a section that is COMDAT. If so, find the
893   // comdat key. A section may be comdat because of -ffunction-sections or
894   // because it is comdat in the IR.
895   MCSectionCOFF *GVSec =
896       GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
897   const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
898 
899   MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
900       Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
901   DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
902 
903   OS.SwitchSection(DebugSec);
904 
905   // Emit the magic version number if this is the first time we've switched to
906   // this section.
907   if (ComdatDebugSections.insert(DebugSec).second)
908     emitCodeViewMagicVersion();
909 }
910 
911 // Emit an S_THUNK32/S_END symbol pair for a thunk routine.
912 // The only supported thunk ordinal is currently the standard type.
913 void CodeViewDebug::emitDebugInfoForThunk(const Function *GV,
914                                           FunctionInfo &FI,
915                                           const MCSymbol *Fn) {
916   std::string FuncName = GlobalValue::dropLLVMManglingEscape(GV->getName());
917   const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind.
918 
919   OS.AddComment("Symbol subsection for " + Twine(FuncName));
920   MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
921 
922   // Emit S_THUNK32
923   MCSymbol *ThunkRecordEnd = beginSymbolRecord(SymbolKind::S_THUNK32);
924   OS.AddComment("PtrParent");
925   OS.EmitIntValue(0, 4);
926   OS.AddComment("PtrEnd");
927   OS.EmitIntValue(0, 4);
928   OS.AddComment("PtrNext");
929   OS.EmitIntValue(0, 4);
930   OS.AddComment("Thunk section relative address");
931   OS.EmitCOFFSecRel32(Fn, /*Offset=*/0);
932   OS.AddComment("Thunk section index");
933   OS.EmitCOFFSectionIndex(Fn);
934   OS.AddComment("Code size");
935   OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2);
936   OS.AddComment("Ordinal");
937   OS.EmitIntValue(unsigned(ordinal), 1);
938   OS.AddComment("Function name");
939   emitNullTerminatedSymbolName(OS, FuncName);
940   // Additional fields specific to the thunk ordinal would go here.
941   endSymbolRecord(ThunkRecordEnd);
942 
943   // Local variables/inlined routines are purposely omitted here.  The point of
944   // marking this as a thunk is so Visual Studio will NOT stop in this routine.
945 
946   // Emit S_PROC_ID_END
947   emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
948 
949   endCVSubsection(SymbolsEnd);
950 }
951 
952 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
953                                              FunctionInfo &FI) {
954   // For each function there is a separate subsection which holds the PC to
955   // file:line table.
956   const MCSymbol *Fn = Asm->getSymbol(GV);
957   assert(Fn);
958 
959   // Switch to the to a comdat section, if appropriate.
960   switchToDebugSectionForSymbol(Fn);
961 
962   std::string FuncName;
963   auto *SP = GV->getSubprogram();
964   assert(SP);
965   setCurrentSubprogram(SP);
966 
967   if (SP->isThunk()) {
968     emitDebugInfoForThunk(GV, FI, Fn);
969     return;
970   }
971 
972   // If we have a display name, build the fully qualified name by walking the
973   // chain of scopes.
974   if (!SP->getName().empty())
975     FuncName =
976         getFullyQualifiedName(SP->getScope().resolve(), SP->getName());
977 
978   // If our DISubprogram name is empty, use the mangled name.
979   if (FuncName.empty())
980     FuncName = GlobalValue::dropLLVMManglingEscape(GV->getName());
981 
982   // Emit FPO data, but only on 32-bit x86. No other platforms use it.
983   if (Triple(MMI->getModule()->getTargetTriple()).getArch() == Triple::x86)
984     OS.EmitCVFPOData(Fn);
985 
986   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
987   OS.AddComment("Symbol subsection for " + Twine(FuncName));
988   MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
989   {
990     SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID
991                                                 : SymbolKind::S_GPROC32_ID;
992     MCSymbol *ProcRecordEnd = beginSymbolRecord(ProcKind);
993 
994     // These fields are filled in by tools like CVPACK which run after the fact.
995     OS.AddComment("PtrParent");
996     OS.EmitIntValue(0, 4);
997     OS.AddComment("PtrEnd");
998     OS.EmitIntValue(0, 4);
999     OS.AddComment("PtrNext");
1000     OS.EmitIntValue(0, 4);
1001     // This is the important bit that tells the debugger where the function
1002     // code is located and what's its size:
1003     OS.AddComment("Code size");
1004     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
1005     OS.AddComment("Offset after prologue");
1006     OS.EmitIntValue(0, 4);
1007     OS.AddComment("Offset before epilogue");
1008     OS.EmitIntValue(0, 4);
1009     OS.AddComment("Function type index");
1010     OS.EmitIntValue(getFuncIdForSubprogram(GV->getSubprogram()).getIndex(), 4);
1011     OS.AddComment("Function section relative address");
1012     OS.EmitCOFFSecRel32(Fn, /*Offset=*/0);
1013     OS.AddComment("Function section index");
1014     OS.EmitCOFFSectionIndex(Fn);
1015     OS.AddComment("Flags");
1016     OS.EmitIntValue(0, 1);
1017     // Emit the function display name as a null-terminated string.
1018     OS.AddComment("Function name");
1019     // Truncate the name so we won't overflow the record length field.
1020     emitNullTerminatedSymbolName(OS, FuncName);
1021     endSymbolRecord(ProcRecordEnd);
1022 
1023     MCSymbol *FrameProcEnd = beginSymbolRecord(SymbolKind::S_FRAMEPROC);
1024     // Subtract out the CSR size since MSVC excludes that and we include it.
1025     OS.AddComment("FrameSize");
1026     OS.EmitIntValue(FI.FrameSize - FI.CSRSize, 4);
1027     OS.AddComment("Padding");
1028     OS.EmitIntValue(0, 4);
1029     OS.AddComment("Offset of padding");
1030     OS.EmitIntValue(0, 4);
1031     OS.AddComment("Bytes of callee saved registers");
1032     OS.EmitIntValue(FI.CSRSize, 4);
1033     OS.AddComment("Exception handler offset");
1034     OS.EmitIntValue(0, 4);
1035     OS.AddComment("Exception handler section");
1036     OS.EmitIntValue(0, 2);
1037     OS.AddComment("Flags (defines frame register)");
1038     OS.EmitIntValue(uint32_t(FI.FrameProcOpts), 4);
1039     endSymbolRecord(FrameProcEnd);
1040 
1041     emitLocalVariableList(FI, FI.Locals);
1042     emitGlobalVariableList(FI.Globals);
1043     emitLexicalBlockList(FI.ChildBlocks, FI);
1044 
1045     // Emit inlined call site information. Only emit functions inlined directly
1046     // into the parent function. We'll emit the other sites recursively as part
1047     // of their parent inline site.
1048     for (const DILocation *InlinedAt : FI.ChildSites) {
1049       auto I = FI.InlineSites.find(InlinedAt);
1050       assert(I != FI.InlineSites.end() &&
1051              "child site not in function inline site map");
1052       emitInlinedCallSite(FI, InlinedAt, I->second);
1053     }
1054 
1055     for (auto Annot : FI.Annotations) {
1056       MCSymbol *Label = Annot.first;
1057       MDTuple *Strs = cast<MDTuple>(Annot.second);
1058       MCSymbol *AnnotEnd = beginSymbolRecord(SymbolKind::S_ANNOTATION);
1059       OS.EmitCOFFSecRel32(Label, /*Offset=*/0);
1060       // FIXME: Make sure we don't overflow the max record size.
1061       OS.EmitCOFFSectionIndex(Label);
1062       OS.EmitIntValue(Strs->getNumOperands(), 2);
1063       for (Metadata *MD : Strs->operands()) {
1064         // MDStrings are null terminated, so we can do EmitBytes and get the
1065         // nice .asciz directive.
1066         StringRef Str = cast<MDString>(MD)->getString();
1067         assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString");
1068         OS.EmitBytes(StringRef(Str.data(), Str.size() + 1));
1069       }
1070       endSymbolRecord(AnnotEnd);
1071     }
1072 
1073     if (SP != nullptr)
1074       emitDebugInfoForUDTs(LocalUDTs);
1075 
1076     // We're done with this function.
1077     emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
1078   }
1079   endCVSubsection(SymbolsEnd);
1080 
1081   // We have an assembler directive that takes care of the whole line table.
1082   OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
1083 }
1084 
1085 CodeViewDebug::LocalVarDefRange
1086 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
1087   LocalVarDefRange DR;
1088   DR.InMemory = -1;
1089   DR.DataOffset = Offset;
1090   assert(DR.DataOffset == Offset && "truncation");
1091   DR.IsSubfield = 0;
1092   DR.StructOffset = 0;
1093   DR.CVRegister = CVRegister;
1094   return DR;
1095 }
1096 
1097 void CodeViewDebug::collectVariableInfoFromMFTable(
1098     DenseSet<InlinedEntity> &Processed) {
1099   const MachineFunction &MF = *Asm->MF;
1100   const TargetSubtargetInfo &TSI = MF.getSubtarget();
1101   const TargetFrameLowering *TFI = TSI.getFrameLowering();
1102   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1103 
1104   for (const MachineFunction::VariableDbgInfo &VI : MF.getVariableDbgInfo()) {
1105     if (!VI.Var)
1106       continue;
1107     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1108            "Expected inlined-at fields to agree");
1109 
1110     Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt()));
1111     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
1112 
1113     // If variable scope is not found then skip this variable.
1114     if (!Scope)
1115       continue;
1116 
1117     // If the variable has an attached offset expression, extract it.
1118     // FIXME: Try to handle DW_OP_deref as well.
1119     int64_t ExprOffset = 0;
1120     if (VI.Expr)
1121       if (!VI.Expr->extractIfOffset(ExprOffset))
1122         continue;
1123 
1124     // Get the frame register used and the offset.
1125     unsigned FrameReg = 0;
1126     int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
1127     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
1128 
1129     // Calculate the label ranges.
1130     LocalVarDefRange DefRange =
1131         createDefRangeMem(CVReg, FrameOffset + ExprOffset);
1132     for (const InsnRange &Range : Scope->getRanges()) {
1133       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
1134       const MCSymbol *End = getLabelAfterInsn(Range.second);
1135       End = End ? End : Asm->getFunctionEnd();
1136       DefRange.Ranges.emplace_back(Begin, End);
1137     }
1138 
1139     LocalVariable Var;
1140     Var.DIVar = VI.Var;
1141     Var.DefRanges.emplace_back(std::move(DefRange));
1142     recordLocalVariable(std::move(Var), Scope);
1143   }
1144 }
1145 
1146 static bool canUseReferenceType(const DbgVariableLocation &Loc) {
1147   return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0;
1148 }
1149 
1150 static bool needsReferenceType(const DbgVariableLocation &Loc) {
1151   return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0;
1152 }
1153 
1154 void CodeViewDebug::calculateRanges(
1155     LocalVariable &Var, const DbgValueHistoryMap::InstrRanges &Ranges) {
1156   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
1157 
1158   // Calculate the definition ranges.
1159   for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1160     const InsnRange &Range = *I;
1161     const MachineInstr *DVInst = Range.first;
1162     assert(DVInst->isDebugValue() && "Invalid History entry");
1163     // FIXME: Find a way to represent constant variables, since they are
1164     // relatively common.
1165     Optional<DbgVariableLocation> Location =
1166         DbgVariableLocation::extractFromMachineInstruction(*DVInst);
1167     if (!Location)
1168       continue;
1169 
1170     // CodeView can only express variables in register and variables in memory
1171     // at a constant offset from a register. However, for variables passed
1172     // indirectly by pointer, it is common for that pointer to be spilled to a
1173     // stack location. For the special case of one offseted load followed by a
1174     // zero offset load (a pointer spilled to the stack), we change the type of
1175     // the local variable from a value type to a reference type. This tricks the
1176     // debugger into doing the load for us.
1177     if (Var.UseReferenceType) {
1178       // We're using a reference type. Drop the last zero offset load.
1179       if (canUseReferenceType(*Location))
1180         Location->LoadChain.pop_back();
1181       else
1182         continue;
1183     } else if (needsReferenceType(*Location)) {
1184       // This location can't be expressed without switching to a reference type.
1185       // Start over using that.
1186       Var.UseReferenceType = true;
1187       Var.DefRanges.clear();
1188       calculateRanges(Var, Ranges);
1189       return;
1190     }
1191 
1192     // We can only handle a register or an offseted load of a register.
1193     if (Location->Register == 0 || Location->LoadChain.size() > 1)
1194       continue;
1195     {
1196       LocalVarDefRange DR;
1197       DR.CVRegister = TRI->getCodeViewRegNum(Location->Register);
1198       DR.InMemory = !Location->LoadChain.empty();
1199       DR.DataOffset =
1200           !Location->LoadChain.empty() ? Location->LoadChain.back() : 0;
1201       if (Location->FragmentInfo) {
1202         DR.IsSubfield = true;
1203         DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8;
1204       } else {
1205         DR.IsSubfield = false;
1206         DR.StructOffset = 0;
1207       }
1208 
1209       if (Var.DefRanges.empty() ||
1210           Var.DefRanges.back().isDifferentLocation(DR)) {
1211         Var.DefRanges.emplace_back(std::move(DR));
1212       }
1213     }
1214 
1215     // Compute the label range.
1216     const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
1217     const MCSymbol *End = getLabelAfterInsn(Range.second);
1218     if (!End) {
1219       // This range is valid until the next overlapping bitpiece. In the
1220       // common case, ranges will not be bitpieces, so they will overlap.
1221       auto J = std::next(I);
1222       const DIExpression *DIExpr = DVInst->getDebugExpression();
1223       while (J != E &&
1224              !DIExpr->fragmentsOverlap(J->first->getDebugExpression()))
1225         ++J;
1226       if (J != E)
1227         End = getLabelBeforeInsn(J->first);
1228       else
1229         End = Asm->getFunctionEnd();
1230     }
1231 
1232     // If the last range end is our begin, just extend the last range.
1233     // Otherwise make a new range.
1234     SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R =
1235         Var.DefRanges.back().Ranges;
1236     if (!R.empty() && R.back().second == Begin)
1237       R.back().second = End;
1238     else
1239       R.emplace_back(Begin, End);
1240 
1241     // FIXME: Do more range combining.
1242   }
1243 }
1244 
1245 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
1246   DenseSet<InlinedEntity> Processed;
1247   // Grab the variable info that was squirreled away in the MMI side-table.
1248   collectVariableInfoFromMFTable(Processed);
1249 
1250   for (const auto &I : DbgValues) {
1251     InlinedEntity IV = I.first;
1252     if (Processed.count(IV))
1253       continue;
1254     const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first);
1255     const DILocation *InlinedAt = IV.second;
1256 
1257     // Instruction ranges, specifying where IV is accessible.
1258     const auto &Ranges = I.second;
1259 
1260     LexicalScope *Scope = nullptr;
1261     if (InlinedAt)
1262       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
1263     else
1264       Scope = LScopes.findLexicalScope(DIVar->getScope());
1265     // If variable scope is not found then skip this variable.
1266     if (!Scope)
1267       continue;
1268 
1269     LocalVariable Var;
1270     Var.DIVar = DIVar;
1271 
1272     calculateRanges(Var, Ranges);
1273     recordLocalVariable(std::move(Var), Scope);
1274   }
1275 }
1276 
1277 void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {
1278   const TargetSubtargetInfo &TSI = MF->getSubtarget();
1279   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1280   const MachineFrameInfo &MFI = MF->getFrameInfo();
1281   const Function &GV = MF->getFunction();
1282   auto Insertion = FnDebugInfo.insert({&GV, llvm::make_unique<FunctionInfo>()});
1283   assert(Insertion.second && "function already has info");
1284   CurFn = Insertion.first->second.get();
1285   CurFn->FuncId = NextFuncId++;
1286   CurFn->Begin = Asm->getFunctionBegin();
1287 
1288   // The S_FRAMEPROC record reports the stack size, and how many bytes of
1289   // callee-saved registers were used. For targets that don't use a PUSH
1290   // instruction (AArch64), this will be zero.
1291   CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters();
1292   CurFn->FrameSize = MFI.getStackSize();
1293   CurFn->OffsetAdjustment = MFI.getOffsetAdjustment();
1294   CurFn->HasStackRealignment = TRI->needsStackRealignment(*MF);
1295 
1296   // For this function S_FRAMEPROC record, figure out which codeview register
1297   // will be the frame pointer.
1298   CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None.
1299   CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None.
1300   if (CurFn->FrameSize > 0) {
1301     if (!TSI.getFrameLowering()->hasFP(*MF)) {
1302       CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1303       CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr;
1304     } else {
1305       // If there is an FP, parameters are always relative to it.
1306       CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr;
1307       if (CurFn->HasStackRealignment) {
1308         // If the stack needs realignment, locals are relative to SP or VFRAME.
1309         CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1310       } else {
1311         // Otherwise, locals are relative to EBP, and we probably have VLAs or
1312         // other stack adjustments.
1313         CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr;
1314       }
1315     }
1316   }
1317 
1318   // Compute other frame procedure options.
1319   FrameProcedureOptions FPO = FrameProcedureOptions::None;
1320   if (MFI.hasVarSizedObjects())
1321     FPO |= FrameProcedureOptions::HasAlloca;
1322   if (MF->exposesReturnsTwice())
1323     FPO |= FrameProcedureOptions::HasSetJmp;
1324   // FIXME: Set HasLongJmp if we ever track that info.
1325   if (MF->hasInlineAsm())
1326     FPO |= FrameProcedureOptions::HasInlineAssembly;
1327   if (GV.hasPersonalityFn()) {
1328     if (isAsynchronousEHPersonality(
1329             classifyEHPersonality(GV.getPersonalityFn())))
1330       FPO |= FrameProcedureOptions::HasStructuredExceptionHandling;
1331     else
1332       FPO |= FrameProcedureOptions::HasExceptionHandling;
1333   }
1334   if (GV.hasFnAttribute(Attribute::InlineHint))
1335     FPO |= FrameProcedureOptions::MarkedInline;
1336   if (GV.hasFnAttribute(Attribute::Naked))
1337     FPO |= FrameProcedureOptions::Naked;
1338   if (MFI.hasStackProtectorIndex())
1339     FPO |= FrameProcedureOptions::SecurityChecks;
1340   FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U);
1341   FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U);
1342   if (Asm->TM.getOptLevel() != CodeGenOpt::None && !GV.optForSize() &&
1343       !GV.hasFnAttribute(Attribute::OptimizeNone))
1344     FPO |= FrameProcedureOptions::OptimizedForSpeed;
1345   // FIXME: Set GuardCfg when it is implemented.
1346   CurFn->FrameProcOpts = FPO;
1347 
1348   OS.EmitCVFuncIdDirective(CurFn->FuncId);
1349 
1350   // Find the end of the function prolog.  First known non-DBG_VALUE and
1351   // non-frame setup location marks the beginning of the function body.
1352   // FIXME: is there a simpler a way to do this? Can we just search
1353   // for the first instruction of the function, not the last of the prolog?
1354   DebugLoc PrologEndLoc;
1355   bool EmptyPrologue = true;
1356   for (const auto &MBB : *MF) {
1357     for (const auto &MI : MBB) {
1358       if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1359           MI.getDebugLoc()) {
1360         PrologEndLoc = MI.getDebugLoc();
1361         break;
1362       } else if (!MI.isMetaInstruction()) {
1363         EmptyPrologue = false;
1364       }
1365     }
1366   }
1367 
1368   // Record beginning of function if we have a non-empty prologue.
1369   if (PrologEndLoc && !EmptyPrologue) {
1370     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
1371     maybeRecordLocation(FnStartDL, MF);
1372   }
1373 }
1374 
1375 static bool shouldEmitUdt(const DIType *T) {
1376   if (!T)
1377     return false;
1378 
1379   // MSVC does not emit UDTs for typedefs that are scoped to classes.
1380   if (T->getTag() == dwarf::DW_TAG_typedef) {
1381     if (DIScope *Scope = T->getScope().resolve()) {
1382       switch (Scope->getTag()) {
1383       case dwarf::DW_TAG_structure_type:
1384       case dwarf::DW_TAG_class_type:
1385       case dwarf::DW_TAG_union_type:
1386         return false;
1387       }
1388     }
1389   }
1390 
1391   while (true) {
1392     if (!T || T->isForwardDecl())
1393       return false;
1394 
1395     const DIDerivedType *DT = dyn_cast<DIDerivedType>(T);
1396     if (!DT)
1397       return true;
1398     T = DT->getBaseType().resolve();
1399   }
1400   return true;
1401 }
1402 
1403 void CodeViewDebug::addToUDTs(const DIType *Ty) {
1404   // Don't record empty UDTs.
1405   if (Ty->getName().empty())
1406     return;
1407   if (!shouldEmitUdt(Ty))
1408     return;
1409 
1410   SmallVector<StringRef, 5> QualifiedNameComponents;
1411   const DISubprogram *ClosestSubprogram = getQualifiedNameComponents(
1412       Ty->getScope().resolve(), QualifiedNameComponents);
1413 
1414   std::string FullyQualifiedName =
1415       getQualifiedName(QualifiedNameComponents, getPrettyScopeName(Ty));
1416 
1417   if (ClosestSubprogram == nullptr) {
1418     GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1419   } else if (ClosestSubprogram == CurrentSubprogram) {
1420     LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1421   }
1422 
1423   // TODO: What if the ClosestSubprogram is neither null or the current
1424   // subprogram?  Currently, the UDT just gets dropped on the floor.
1425   //
1426   // The current behavior is not desirable.  To get maximal fidelity, we would
1427   // need to perform all type translation before beginning emission of .debug$S
1428   // and then make LocalUDTs a member of FunctionInfo
1429 }
1430 
1431 TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
1432   // Generic dispatch for lowering an unknown type.
1433   switch (Ty->getTag()) {
1434   case dwarf::DW_TAG_array_type:
1435     return lowerTypeArray(cast<DICompositeType>(Ty));
1436   case dwarf::DW_TAG_typedef:
1437     return lowerTypeAlias(cast<DIDerivedType>(Ty));
1438   case dwarf::DW_TAG_base_type:
1439     return lowerTypeBasic(cast<DIBasicType>(Ty));
1440   case dwarf::DW_TAG_pointer_type:
1441     if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type")
1442       return lowerTypeVFTableShape(cast<DIDerivedType>(Ty));
1443     LLVM_FALLTHROUGH;
1444   case dwarf::DW_TAG_reference_type:
1445   case dwarf::DW_TAG_rvalue_reference_type:
1446     return lowerTypePointer(cast<DIDerivedType>(Ty));
1447   case dwarf::DW_TAG_ptr_to_member_type:
1448     return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
1449   case dwarf::DW_TAG_restrict_type:
1450   case dwarf::DW_TAG_const_type:
1451   case dwarf::DW_TAG_volatile_type:
1452   // TODO: add support for DW_TAG_atomic_type here
1453     return lowerTypeModifier(cast<DIDerivedType>(Ty));
1454   case dwarf::DW_TAG_subroutine_type:
1455     if (ClassTy) {
1456       // The member function type of a member function pointer has no
1457       // ThisAdjustment.
1458       return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,
1459                                      /*ThisAdjustment=*/0,
1460                                      /*IsStaticMethod=*/false);
1461     }
1462     return lowerTypeFunction(cast<DISubroutineType>(Ty));
1463   case dwarf::DW_TAG_enumeration_type:
1464     return lowerTypeEnum(cast<DICompositeType>(Ty));
1465   case dwarf::DW_TAG_class_type:
1466   case dwarf::DW_TAG_structure_type:
1467     return lowerTypeClass(cast<DICompositeType>(Ty));
1468   case dwarf::DW_TAG_union_type:
1469     return lowerTypeUnion(cast<DICompositeType>(Ty));
1470   case dwarf::DW_TAG_unspecified_type:
1471     if (Ty->getName() == "decltype(nullptr)")
1472       return TypeIndex::NullptrT();
1473     return TypeIndex::None();
1474   default:
1475     // Use the null type index.
1476     return TypeIndex();
1477   }
1478 }
1479 
1480 TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
1481   DITypeRef UnderlyingTypeRef = Ty->getBaseType();
1482   TypeIndex UnderlyingTypeIndex = getTypeIndex(UnderlyingTypeRef);
1483   StringRef TypeName = Ty->getName();
1484 
1485   addToUDTs(Ty);
1486 
1487   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
1488       TypeName == "HRESULT")
1489     return TypeIndex(SimpleTypeKind::HResult);
1490   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
1491       TypeName == "wchar_t")
1492     return TypeIndex(SimpleTypeKind::WideCharacter);
1493 
1494   return UnderlyingTypeIndex;
1495 }
1496 
1497 TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
1498   DITypeRef ElementTypeRef = Ty->getBaseType();
1499   TypeIndex ElementTypeIndex = getTypeIndex(ElementTypeRef);
1500   // IndexType is size_t, which depends on the bitness of the target.
1501   TypeIndex IndexType = getPointerSizeInBytes() == 8
1502                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
1503                             : TypeIndex(SimpleTypeKind::UInt32Long);
1504 
1505   uint64_t ElementSize = getBaseTypeSize(ElementTypeRef) / 8;
1506 
1507   // Add subranges to array type.
1508   DINodeArray Elements = Ty->getElements();
1509   for (int i = Elements.size() - 1; i >= 0; --i) {
1510     const DINode *Element = Elements[i];
1511     assert(Element->getTag() == dwarf::DW_TAG_subrange_type);
1512 
1513     const DISubrange *Subrange = cast<DISubrange>(Element);
1514     assert(Subrange->getLowerBound() == 0 &&
1515            "codeview doesn't support subranges with lower bounds");
1516     int64_t Count = -1;
1517     if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt*>())
1518       Count = CI->getSExtValue();
1519 
1520     // Forward declarations of arrays without a size and VLAs use a count of -1.
1521     // Emit a count of zero in these cases to match what MSVC does for arrays
1522     // without a size. MSVC doesn't support VLAs, so it's not clear what we
1523     // should do for them even if we could distinguish them.
1524     if (Count == -1)
1525       Count = 0;
1526 
1527     // Update the element size and element type index for subsequent subranges.
1528     ElementSize *= Count;
1529 
1530     // If this is the outermost array, use the size from the array. It will be
1531     // more accurate if we had a VLA or an incomplete element type size.
1532     uint64_t ArraySize =
1533         (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;
1534 
1535     StringRef Name = (i == 0) ? Ty->getName() : "";
1536     ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name);
1537     ElementTypeIndex = TypeTable.writeLeafType(AR);
1538   }
1539 
1540   return ElementTypeIndex;
1541 }
1542 
1543 TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
1544   TypeIndex Index;
1545   dwarf::TypeKind Kind;
1546   uint32_t ByteSize;
1547 
1548   Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
1549   ByteSize = Ty->getSizeInBits() / 8;
1550 
1551   SimpleTypeKind STK = SimpleTypeKind::None;
1552   switch (Kind) {
1553   case dwarf::DW_ATE_address:
1554     // FIXME: Translate
1555     break;
1556   case dwarf::DW_ATE_boolean:
1557     switch (ByteSize) {
1558     case 1:  STK = SimpleTypeKind::Boolean8;   break;
1559     case 2:  STK = SimpleTypeKind::Boolean16;  break;
1560     case 4:  STK = SimpleTypeKind::Boolean32;  break;
1561     case 8:  STK = SimpleTypeKind::Boolean64;  break;
1562     case 16: STK = SimpleTypeKind::Boolean128; break;
1563     }
1564     break;
1565   case dwarf::DW_ATE_complex_float:
1566     switch (ByteSize) {
1567     case 2:  STK = SimpleTypeKind::Complex16;  break;
1568     case 4:  STK = SimpleTypeKind::Complex32;  break;
1569     case 8:  STK = SimpleTypeKind::Complex64;  break;
1570     case 10: STK = SimpleTypeKind::Complex80;  break;
1571     case 16: STK = SimpleTypeKind::Complex128; break;
1572     }
1573     break;
1574   case dwarf::DW_ATE_float:
1575     switch (ByteSize) {
1576     case 2:  STK = SimpleTypeKind::Float16;  break;
1577     case 4:  STK = SimpleTypeKind::Float32;  break;
1578     case 6:  STK = SimpleTypeKind::Float48;  break;
1579     case 8:  STK = SimpleTypeKind::Float64;  break;
1580     case 10: STK = SimpleTypeKind::Float80;  break;
1581     case 16: STK = SimpleTypeKind::Float128; break;
1582     }
1583     break;
1584   case dwarf::DW_ATE_signed:
1585     switch (ByteSize) {
1586     case 1:  STK = SimpleTypeKind::SignedCharacter; break;
1587     case 2:  STK = SimpleTypeKind::Int16Short;      break;
1588     case 4:  STK = SimpleTypeKind::Int32;           break;
1589     case 8:  STK = SimpleTypeKind::Int64Quad;       break;
1590     case 16: STK = SimpleTypeKind::Int128Oct;       break;
1591     }
1592     break;
1593   case dwarf::DW_ATE_unsigned:
1594     switch (ByteSize) {
1595     case 1:  STK = SimpleTypeKind::UnsignedCharacter; break;
1596     case 2:  STK = SimpleTypeKind::UInt16Short;       break;
1597     case 4:  STK = SimpleTypeKind::UInt32;            break;
1598     case 8:  STK = SimpleTypeKind::UInt64Quad;        break;
1599     case 16: STK = SimpleTypeKind::UInt128Oct;        break;
1600     }
1601     break;
1602   case dwarf::DW_ATE_UTF:
1603     switch (ByteSize) {
1604     case 2: STK = SimpleTypeKind::Character16; break;
1605     case 4: STK = SimpleTypeKind::Character32; break;
1606     }
1607     break;
1608   case dwarf::DW_ATE_signed_char:
1609     if (ByteSize == 1)
1610       STK = SimpleTypeKind::SignedCharacter;
1611     break;
1612   case dwarf::DW_ATE_unsigned_char:
1613     if (ByteSize == 1)
1614       STK = SimpleTypeKind::UnsignedCharacter;
1615     break;
1616   default:
1617     break;
1618   }
1619 
1620   // Apply some fixups based on the source-level type name.
1621   if (STK == SimpleTypeKind::Int32 && Ty->getName() == "long int")
1622     STK = SimpleTypeKind::Int32Long;
1623   if (STK == SimpleTypeKind::UInt32 && Ty->getName() == "long unsigned int")
1624     STK = SimpleTypeKind::UInt32Long;
1625   if (STK == SimpleTypeKind::UInt16Short &&
1626       (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
1627     STK = SimpleTypeKind::WideCharacter;
1628   if ((STK == SimpleTypeKind::SignedCharacter ||
1629        STK == SimpleTypeKind::UnsignedCharacter) &&
1630       Ty->getName() == "char")
1631     STK = SimpleTypeKind::NarrowCharacter;
1632 
1633   return TypeIndex(STK);
1634 }
1635 
1636 TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty,
1637                                           PointerOptions PO) {
1638   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
1639 
1640   // Pointers to simple types without any options can use SimpleTypeMode, rather
1641   // than having a dedicated pointer type record.
1642   if (PointeeTI.isSimple() && PO == PointerOptions::None &&
1643       PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
1644       Ty->getTag() == dwarf::DW_TAG_pointer_type) {
1645     SimpleTypeMode Mode = Ty->getSizeInBits() == 64
1646                               ? SimpleTypeMode::NearPointer64
1647                               : SimpleTypeMode::NearPointer32;
1648     return TypeIndex(PointeeTI.getSimpleKind(), Mode);
1649   }
1650 
1651   PointerKind PK =
1652       Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
1653   PointerMode PM = PointerMode::Pointer;
1654   switch (Ty->getTag()) {
1655   default: llvm_unreachable("not a pointer tag type");
1656   case dwarf::DW_TAG_pointer_type:
1657     PM = PointerMode::Pointer;
1658     break;
1659   case dwarf::DW_TAG_reference_type:
1660     PM = PointerMode::LValueReference;
1661     break;
1662   case dwarf::DW_TAG_rvalue_reference_type:
1663     PM = PointerMode::RValueReference;
1664     break;
1665   }
1666 
1667   if (Ty->isObjectPointer())
1668     PO |= PointerOptions::Const;
1669 
1670   PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
1671   return TypeTable.writeLeafType(PR);
1672 }
1673 
1674 static PointerToMemberRepresentation
1675 translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
1676   // SizeInBytes being zero generally implies that the member pointer type was
1677   // incomplete, which can happen if it is part of a function prototype. In this
1678   // case, use the unknown model instead of the general model.
1679   if (IsPMF) {
1680     switch (Flags & DINode::FlagPtrToMemberRep) {
1681     case 0:
1682       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1683                               : PointerToMemberRepresentation::GeneralFunction;
1684     case DINode::FlagSingleInheritance:
1685       return PointerToMemberRepresentation::SingleInheritanceFunction;
1686     case DINode::FlagMultipleInheritance:
1687       return PointerToMemberRepresentation::MultipleInheritanceFunction;
1688     case DINode::FlagVirtualInheritance:
1689       return PointerToMemberRepresentation::VirtualInheritanceFunction;
1690     }
1691   } else {
1692     switch (Flags & DINode::FlagPtrToMemberRep) {
1693     case 0:
1694       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1695                               : PointerToMemberRepresentation::GeneralData;
1696     case DINode::FlagSingleInheritance:
1697       return PointerToMemberRepresentation::SingleInheritanceData;
1698     case DINode::FlagMultipleInheritance:
1699       return PointerToMemberRepresentation::MultipleInheritanceData;
1700     case DINode::FlagVirtualInheritance:
1701       return PointerToMemberRepresentation::VirtualInheritanceData;
1702     }
1703   }
1704   llvm_unreachable("invalid ptr to member representation");
1705 }
1706 
1707 TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty,
1708                                                 PointerOptions PO) {
1709   assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
1710   TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
1711   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType(), Ty->getClassType());
1712   PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
1713                                                 : PointerKind::Near32;
1714   bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
1715   PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
1716                          : PointerMode::PointerToDataMember;
1717 
1718   assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
1719   uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
1720   MemberPointerInfo MPI(
1721       ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
1722   PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
1723   return TypeTable.writeLeafType(PR);
1724 }
1725 
1726 /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1727 /// have a translation, use the NearC convention.
1728 static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1729   switch (DwarfCC) {
1730   case dwarf::DW_CC_normal:             return CallingConvention::NearC;
1731   case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1732   case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;
1733   case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;
1734   case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;
1735   case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;
1736   }
1737   return CallingConvention::NearC;
1738 }
1739 
1740 TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
1741   ModifierOptions Mods = ModifierOptions::None;
1742   PointerOptions PO = PointerOptions::None;
1743   bool IsModifier = true;
1744   const DIType *BaseTy = Ty;
1745   while (IsModifier && BaseTy) {
1746     // FIXME: Need to add DWARF tags for __unaligned and _Atomic
1747     switch (BaseTy->getTag()) {
1748     case dwarf::DW_TAG_const_type:
1749       Mods |= ModifierOptions::Const;
1750       PO |= PointerOptions::Const;
1751       break;
1752     case dwarf::DW_TAG_volatile_type:
1753       Mods |= ModifierOptions::Volatile;
1754       PO |= PointerOptions::Volatile;
1755       break;
1756     case dwarf::DW_TAG_restrict_type:
1757       // Only pointer types be marked with __restrict. There is no known flag
1758       // for __restrict in LF_MODIFIER records.
1759       PO |= PointerOptions::Restrict;
1760       break;
1761     default:
1762       IsModifier = false;
1763       break;
1764     }
1765     if (IsModifier)
1766       BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve();
1767   }
1768 
1769   // Check if the inner type will use an LF_POINTER record. If so, the
1770   // qualifiers will go in the LF_POINTER record. This comes up for types like
1771   // 'int *const' and 'int *__restrict', not the more common cases like 'const
1772   // char *'.
1773   if (BaseTy) {
1774     switch (BaseTy->getTag()) {
1775     case dwarf::DW_TAG_pointer_type:
1776     case dwarf::DW_TAG_reference_type:
1777     case dwarf::DW_TAG_rvalue_reference_type:
1778       return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO);
1779     case dwarf::DW_TAG_ptr_to_member_type:
1780       return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO);
1781     default:
1782       break;
1783     }
1784   }
1785 
1786   TypeIndex ModifiedTI = getTypeIndex(BaseTy);
1787 
1788   // Return the base type index if there aren't any modifiers. For example, the
1789   // metadata could contain restrict wrappers around non-pointer types.
1790   if (Mods == ModifierOptions::None)
1791     return ModifiedTI;
1792 
1793   ModifierRecord MR(ModifiedTI, Mods);
1794   return TypeTable.writeLeafType(MR);
1795 }
1796 
1797 TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
1798   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
1799   for (DITypeRef ArgTypeRef : Ty->getTypeArray())
1800     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef));
1801 
1802   // MSVC uses type none for variadic argument.
1803   if (ReturnAndArgTypeIndices.size() > 1 &&
1804       ReturnAndArgTypeIndices.back() == TypeIndex::Void()) {
1805     ReturnAndArgTypeIndices.back() = TypeIndex::None();
1806   }
1807   TypeIndex ReturnTypeIndex = TypeIndex::Void();
1808   ArrayRef<TypeIndex> ArgTypeIndices = None;
1809   if (!ReturnAndArgTypeIndices.empty()) {
1810     auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
1811     ReturnTypeIndex = ReturnAndArgTypesRef.front();
1812     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
1813   }
1814 
1815   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1816   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
1817 
1818   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1819 
1820   FunctionOptions FO = getFunctionOptions(Ty);
1821   ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(),
1822                             ArgListIndex);
1823   return TypeTable.writeLeafType(Procedure);
1824 }
1825 
1826 TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,
1827                                                  const DIType *ClassTy,
1828                                                  int ThisAdjustment,
1829                                                  bool IsStaticMethod,
1830                                                  FunctionOptions FO) {
1831   // Lower the containing class type.
1832   TypeIndex ClassType = getTypeIndex(ClassTy);
1833 
1834   DITypeRefArray ReturnAndArgs = Ty->getTypeArray();
1835 
1836   unsigned Index = 0;
1837   SmallVector<TypeIndex, 8> ArgTypeIndices;
1838   TypeIndex ReturnTypeIndex = getTypeIndex(ReturnAndArgs[Index++]);
1839 
1840   // If the first argument is a pointer type and this isn't a static method,
1841   // treat it as the special 'this' parameter, which is encoded separately from
1842   // the arguments.
1843   TypeIndex ThisTypeIndex;
1844   if (!IsStaticMethod && ReturnAndArgs.size() > Index) {
1845     if (const DIDerivedType *PtrTy =
1846             dyn_cast_or_null<DIDerivedType>(ReturnAndArgs[Index].resolve())) {
1847       if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) {
1848         ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, Ty);
1849         Index++;
1850       }
1851     }
1852   }
1853 
1854   while (Index < ReturnAndArgs.size())
1855     ArgTypeIndices.push_back(getTypeIndex(ReturnAndArgs[Index++]));
1856 
1857   // MSVC uses type none for variadic argument.
1858   if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void())
1859     ArgTypeIndices.back() = TypeIndex::None();
1860 
1861   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1862   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
1863 
1864   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1865 
1866   MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO,
1867                            ArgTypeIndices.size(), ArgListIndex, ThisAdjustment);
1868   return TypeTable.writeLeafType(MFR);
1869 }
1870 
1871 TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) {
1872   unsigned VSlotCount =
1873       Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize());
1874   SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near);
1875 
1876   VFTableShapeRecord VFTSR(Slots);
1877   return TypeTable.writeLeafType(VFTSR);
1878 }
1879 
1880 static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {
1881   switch (Flags & DINode::FlagAccessibility) {
1882   case DINode::FlagPrivate:   return MemberAccess::Private;
1883   case DINode::FlagPublic:    return MemberAccess::Public;
1884   case DINode::FlagProtected: return MemberAccess::Protected;
1885   case 0:
1886     // If there was no explicit access control, provide the default for the tag.
1887     return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
1888                                                  : MemberAccess::Public;
1889   }
1890   llvm_unreachable("access flags are exclusive");
1891 }
1892 
1893 static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) {
1894   if (SP->isArtificial())
1895     return MethodOptions::CompilerGenerated;
1896 
1897   // FIXME: Handle other MethodOptions.
1898 
1899   return MethodOptions::None;
1900 }
1901 
1902 static MethodKind translateMethodKindFlags(const DISubprogram *SP,
1903                                            bool Introduced) {
1904   if (SP->getFlags() & DINode::FlagStaticMember)
1905     return MethodKind::Static;
1906 
1907   switch (SP->getVirtuality()) {
1908   case dwarf::DW_VIRTUALITY_none:
1909     break;
1910   case dwarf::DW_VIRTUALITY_virtual:
1911     return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;
1912   case dwarf::DW_VIRTUALITY_pure_virtual:
1913     return Introduced ? MethodKind::PureIntroducingVirtual
1914                       : MethodKind::PureVirtual;
1915   default:
1916     llvm_unreachable("unhandled virtuality case");
1917   }
1918 
1919   return MethodKind::Vanilla;
1920 }
1921 
1922 static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
1923   switch (Ty->getTag()) {
1924   case dwarf::DW_TAG_class_type:     return TypeRecordKind::Class;
1925   case dwarf::DW_TAG_structure_type: return TypeRecordKind::Struct;
1926   }
1927   llvm_unreachable("unexpected tag");
1928 }
1929 
1930 /// Return ClassOptions that should be present on both the forward declaration
1931 /// and the defintion of a tag type.
1932 static ClassOptions getCommonClassOptions(const DICompositeType *Ty) {
1933   ClassOptions CO = ClassOptions::None;
1934 
1935   // MSVC always sets this flag, even for local types. Clang doesn't always
1936   // appear to give every type a linkage name, which may be problematic for us.
1937   // FIXME: Investigate the consequences of not following them here.
1938   if (!Ty->getIdentifier().empty())
1939     CO |= ClassOptions::HasUniqueName;
1940 
1941   // Put the Nested flag on a type if it appears immediately inside a tag type.
1942   // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass
1943   // here. That flag is only set on definitions, and not forward declarations.
1944   const DIScope *ImmediateScope = Ty->getScope().resolve();
1945   if (ImmediateScope && isa<DICompositeType>(ImmediateScope))
1946     CO |= ClassOptions::Nested;
1947 
1948   // Put the Scoped flag on function-local types. MSVC puts this flag for enum
1949   // type only when it has an immediate function scope. Clang never puts enums
1950   // inside DILexicalBlock scopes. Enum types, as generated by clang, are
1951   // always in function, class, or file scopes.
1952   if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) {
1953     if (ImmediateScope && isa<DISubprogram>(ImmediateScope))
1954       CO |= ClassOptions::Scoped;
1955   } else {
1956     for (const DIScope *Scope = ImmediateScope; Scope != nullptr;
1957          Scope = Scope->getScope().resolve()) {
1958       if (isa<DISubprogram>(Scope)) {
1959         CO |= ClassOptions::Scoped;
1960         break;
1961       }
1962     }
1963   }
1964 
1965   return CO;
1966 }
1967 
1968 void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) {
1969   switch (Ty->getTag()) {
1970   case dwarf::DW_TAG_class_type:
1971   case dwarf::DW_TAG_structure_type:
1972   case dwarf::DW_TAG_union_type:
1973   case dwarf::DW_TAG_enumeration_type:
1974     break;
1975   default:
1976     return;
1977   }
1978 
1979   if (const auto *File = Ty->getFile()) {
1980     StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File));
1981     TypeIndex SIDI = TypeTable.writeLeafType(SIDR);
1982 
1983     UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine());
1984     TypeTable.writeLeafType(USLR);
1985   }
1986 }
1987 
1988 TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
1989   ClassOptions CO = getCommonClassOptions(Ty);
1990   TypeIndex FTI;
1991   unsigned EnumeratorCount = 0;
1992 
1993   if (Ty->isForwardDecl()) {
1994     CO |= ClassOptions::ForwardReference;
1995   } else {
1996     ContinuationRecordBuilder ContinuationBuilder;
1997     ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
1998     for (const DINode *Element : Ty->getElements()) {
1999       // We assume that the frontend provides all members in source declaration
2000       // order, which is what MSVC does.
2001       if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
2002         EnumeratorRecord ER(MemberAccess::Public,
2003                             APSInt::getUnsigned(Enumerator->getValue()),
2004                             Enumerator->getName());
2005         ContinuationBuilder.writeMemberType(ER);
2006         EnumeratorCount++;
2007       }
2008     }
2009     FTI = TypeTable.insertRecord(ContinuationBuilder);
2010   }
2011 
2012   std::string FullName = getFullyQualifiedName(Ty);
2013 
2014   EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(),
2015                 getTypeIndex(Ty->getBaseType()));
2016   TypeIndex EnumTI = TypeTable.writeLeafType(ER);
2017 
2018   addUDTSrcLine(Ty, EnumTI);
2019 
2020   return EnumTI;
2021 }
2022 
2023 //===----------------------------------------------------------------------===//
2024 // ClassInfo
2025 //===----------------------------------------------------------------------===//
2026 
2027 struct llvm::ClassInfo {
2028   struct MemberInfo {
2029     const DIDerivedType *MemberTypeNode;
2030     uint64_t BaseOffset;
2031   };
2032   // [MemberInfo]
2033   using MemberList = std::vector<MemberInfo>;
2034 
2035   using MethodsList = TinyPtrVector<const DISubprogram *>;
2036   // MethodName -> MethodsList
2037   using MethodsMap = MapVector<MDString *, MethodsList>;
2038 
2039   /// Base classes.
2040   std::vector<const DIDerivedType *> Inheritance;
2041 
2042   /// Direct members.
2043   MemberList Members;
2044   // Direct overloaded methods gathered by name.
2045   MethodsMap Methods;
2046 
2047   TypeIndex VShapeTI;
2048 
2049   std::vector<const DIType *> NestedTypes;
2050 };
2051 
2052 void CodeViewDebug::clear() {
2053   assert(CurFn == nullptr);
2054   FileIdMap.clear();
2055   FnDebugInfo.clear();
2056   FileToFilepathMap.clear();
2057   LocalUDTs.clear();
2058   GlobalUDTs.clear();
2059   TypeIndices.clear();
2060   CompleteTypeIndices.clear();
2061   ScopeGlobals.clear();
2062 }
2063 
2064 void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
2065                                       const DIDerivedType *DDTy) {
2066   if (!DDTy->getName().empty()) {
2067     Info.Members.push_back({DDTy, 0});
2068     return;
2069   }
2070 
2071   // An unnamed member may represent a nested struct or union. Attempt to
2072   // interpret the unnamed member as a DICompositeType possibly wrapped in
2073   // qualifier types. Add all the indirect fields to the current record if that
2074   // succeeds, and drop the member if that fails.
2075   assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");
2076   uint64_t Offset = DDTy->getOffsetInBits();
2077   const DIType *Ty = DDTy->getBaseType().resolve();
2078   bool FullyResolved = false;
2079   while (!FullyResolved) {
2080     switch (Ty->getTag()) {
2081     case dwarf::DW_TAG_const_type:
2082     case dwarf::DW_TAG_volatile_type:
2083       // FIXME: we should apply the qualifier types to the indirect fields
2084       // rather than dropping them.
2085       Ty = cast<DIDerivedType>(Ty)->getBaseType().resolve();
2086       break;
2087     default:
2088       FullyResolved = true;
2089       break;
2090     }
2091   }
2092 
2093   const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty);
2094   if (!DCTy)
2095     return;
2096 
2097   ClassInfo NestedInfo = collectClassInfo(DCTy);
2098   for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)
2099     Info.Members.push_back(
2100         {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});
2101 }
2102 
2103 ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {
2104   ClassInfo Info;
2105   // Add elements to structure type.
2106   DINodeArray Elements = Ty->getElements();
2107   for (auto *Element : Elements) {
2108     // We assume that the frontend provides all members in source declaration
2109     // order, which is what MSVC does.
2110     if (!Element)
2111       continue;
2112     if (auto *SP = dyn_cast<DISubprogram>(Element)) {
2113       Info.Methods[SP->getRawName()].push_back(SP);
2114     } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
2115       if (DDTy->getTag() == dwarf::DW_TAG_member) {
2116         collectMemberInfo(Info, DDTy);
2117       } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {
2118         Info.Inheritance.push_back(DDTy);
2119       } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type &&
2120                  DDTy->getName() == "__vtbl_ptr_type") {
2121         Info.VShapeTI = getTypeIndex(DDTy);
2122       } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) {
2123         Info.NestedTypes.push_back(DDTy);
2124       } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {
2125         // Ignore friend members. It appears that MSVC emitted info about
2126         // friends in the past, but modern versions do not.
2127       }
2128     } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
2129       Info.NestedTypes.push_back(Composite);
2130     }
2131     // Skip other unrecognized kinds of elements.
2132   }
2133   return Info;
2134 }
2135 
2136 static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) {
2137   // This routine is used by lowerTypeClass and lowerTypeUnion to determine
2138   // if a complete type should be emitted instead of a forward reference.
2139   return Ty->getName().empty() && Ty->getIdentifier().empty() &&
2140       !Ty->isForwardDecl();
2141 }
2142 
2143 TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
2144   // Emit the complete type for unnamed structs.  C++ classes with methods
2145   // which have a circular reference back to the class type are expected to
2146   // be named by the front-end and should not be "unnamed".  C unnamed
2147   // structs should not have circular references.
2148   if (shouldAlwaysEmitCompleteClassType(Ty)) {
2149     // If this unnamed complete type is already in the process of being defined
2150     // then the description of the type is malformed and cannot be emitted
2151     // into CodeView correctly so report a fatal error.
2152     auto I = CompleteTypeIndices.find(Ty);
2153     if (I != CompleteTypeIndices.end() && I->second == TypeIndex())
2154       report_fatal_error("cannot debug circular reference to unnamed type");
2155     return getCompleteTypeIndex(Ty);
2156   }
2157 
2158   // First, construct the forward decl.  Don't look into Ty to compute the
2159   // forward decl options, since it might not be available in all TUs.
2160   TypeRecordKind Kind = getRecordKind(Ty);
2161   ClassOptions CO =
2162       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2163   std::string FullName = getFullyQualifiedName(Ty);
2164   ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0,
2165                  FullName, Ty->getIdentifier());
2166   TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR);
2167   if (!Ty->isForwardDecl())
2168     DeferredCompleteTypes.push_back(Ty);
2169   return FwdDeclTI;
2170 }
2171 
2172 TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
2173   // Construct the field list and complete type record.
2174   TypeRecordKind Kind = getRecordKind(Ty);
2175   ClassOptions CO = getCommonClassOptions(Ty);
2176   TypeIndex FieldTI;
2177   TypeIndex VShapeTI;
2178   unsigned FieldCount;
2179   bool ContainsNestedClass;
2180   std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =
2181       lowerRecordFieldList(Ty);
2182 
2183   if (ContainsNestedClass)
2184     CO |= ClassOptions::ContainsNestedClass;
2185 
2186   std::string FullName = getFullyQualifiedName(Ty);
2187 
2188   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2189 
2190   ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI,
2191                  SizeInBytes, FullName, Ty->getIdentifier());
2192   TypeIndex ClassTI = TypeTable.writeLeafType(CR);
2193 
2194   addUDTSrcLine(Ty, ClassTI);
2195 
2196   addToUDTs(Ty);
2197 
2198   return ClassTI;
2199 }
2200 
2201 TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
2202   // Emit the complete type for unnamed unions.
2203   if (shouldAlwaysEmitCompleteClassType(Ty))
2204     return getCompleteTypeIndex(Ty);
2205 
2206   ClassOptions CO =
2207       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2208   std::string FullName = getFullyQualifiedName(Ty);
2209   UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier());
2210   TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR);
2211   if (!Ty->isForwardDecl())
2212     DeferredCompleteTypes.push_back(Ty);
2213   return FwdDeclTI;
2214 }
2215 
2216 TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
2217   ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);
2218   TypeIndex FieldTI;
2219   unsigned FieldCount;
2220   bool ContainsNestedClass;
2221   std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =
2222       lowerRecordFieldList(Ty);
2223 
2224   if (ContainsNestedClass)
2225     CO |= ClassOptions::ContainsNestedClass;
2226 
2227   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2228   std::string FullName = getFullyQualifiedName(Ty);
2229 
2230   UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName,
2231                  Ty->getIdentifier());
2232   TypeIndex UnionTI = TypeTable.writeLeafType(UR);
2233 
2234   addUDTSrcLine(Ty, UnionTI);
2235 
2236   addToUDTs(Ty);
2237 
2238   return UnionTI;
2239 }
2240 
2241 std::tuple<TypeIndex, TypeIndex, unsigned, bool>
2242 CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
2243   // Manually count members. MSVC appears to count everything that generates a
2244   // field list record. Each individual overload in a method overload group
2245   // contributes to this count, even though the overload group is a single field
2246   // list record.
2247   unsigned MemberCount = 0;
2248   ClassInfo Info = collectClassInfo(Ty);
2249   ContinuationRecordBuilder ContinuationBuilder;
2250   ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
2251 
2252   // Create base classes.
2253   for (const DIDerivedType *I : Info.Inheritance) {
2254     if (I->getFlags() & DINode::FlagVirtual) {
2255       // Virtual base.
2256       unsigned VBPtrOffset = I->getVBPtrOffset();
2257       // FIXME: Despite the accessor name, the offset is really in bytes.
2258       unsigned VBTableIndex = I->getOffsetInBits() / 4;
2259       auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase
2260                             ? TypeRecordKind::IndirectVirtualBaseClass
2261                             : TypeRecordKind::VirtualBaseClass;
2262       VirtualBaseClassRecord VBCR(
2263           RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()),
2264           getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,
2265           VBTableIndex);
2266 
2267       ContinuationBuilder.writeMemberType(VBCR);
2268       MemberCount++;
2269     } else {
2270       assert(I->getOffsetInBits() % 8 == 0 &&
2271              "bases must be on byte boundaries");
2272       BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()),
2273                           getTypeIndex(I->getBaseType()),
2274                           I->getOffsetInBits() / 8);
2275       ContinuationBuilder.writeMemberType(BCR);
2276       MemberCount++;
2277     }
2278   }
2279 
2280   // Create members.
2281   for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {
2282     const DIDerivedType *Member = MemberInfo.MemberTypeNode;
2283     TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());
2284     StringRef MemberName = Member->getName();
2285     MemberAccess Access =
2286         translateAccessFlags(Ty->getTag(), Member->getFlags());
2287 
2288     if (Member->isStaticMember()) {
2289       StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName);
2290       ContinuationBuilder.writeMemberType(SDMR);
2291       MemberCount++;
2292       continue;
2293     }
2294 
2295     // Virtual function pointer member.
2296     if ((Member->getFlags() & DINode::FlagArtificial) &&
2297         Member->getName().startswith("_vptr$")) {
2298       VFPtrRecord VFPR(getTypeIndex(Member->getBaseType()));
2299       ContinuationBuilder.writeMemberType(VFPR);
2300       MemberCount++;
2301       continue;
2302     }
2303 
2304     // Data member.
2305     uint64_t MemberOffsetInBits =
2306         Member->getOffsetInBits() + MemberInfo.BaseOffset;
2307     if (Member->isBitField()) {
2308       uint64_t StartBitOffset = MemberOffsetInBits;
2309       if (const auto *CI =
2310               dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {
2311         MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;
2312       }
2313       StartBitOffset -= MemberOffsetInBits;
2314       BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(),
2315                          StartBitOffset);
2316       MemberBaseType = TypeTable.writeLeafType(BFR);
2317     }
2318     uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;
2319     DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes,
2320                          MemberName);
2321     ContinuationBuilder.writeMemberType(DMR);
2322     MemberCount++;
2323   }
2324 
2325   // Create methods
2326   for (auto &MethodItr : Info.Methods) {
2327     StringRef Name = MethodItr.first->getString();
2328 
2329     std::vector<OneMethodRecord> Methods;
2330     for (const DISubprogram *SP : MethodItr.second) {
2331       TypeIndex MethodType = getMemberFunctionType(SP, Ty);
2332       bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;
2333 
2334       unsigned VFTableOffset = -1;
2335       if (Introduced)
2336         VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();
2337 
2338       Methods.push_back(OneMethodRecord(
2339           MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()),
2340           translateMethodKindFlags(SP, Introduced),
2341           translateMethodOptionFlags(SP), VFTableOffset, Name));
2342       MemberCount++;
2343     }
2344     assert(!Methods.empty() && "Empty methods map entry");
2345     if (Methods.size() == 1)
2346       ContinuationBuilder.writeMemberType(Methods[0]);
2347     else {
2348       // FIXME: Make this use its own ContinuationBuilder so that
2349       // MethodOverloadList can be split correctly.
2350       MethodOverloadListRecord MOLR(Methods);
2351       TypeIndex MethodList = TypeTable.writeLeafType(MOLR);
2352 
2353       OverloadedMethodRecord OMR(Methods.size(), MethodList, Name);
2354       ContinuationBuilder.writeMemberType(OMR);
2355     }
2356   }
2357 
2358   // Create nested classes.
2359   for (const DIType *Nested : Info.NestedTypes) {
2360     NestedTypeRecord R(getTypeIndex(DITypeRef(Nested)), Nested->getName());
2361     ContinuationBuilder.writeMemberType(R);
2362     MemberCount++;
2363   }
2364 
2365   TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder);
2366   return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount,
2367                          !Info.NestedTypes.empty());
2368 }
2369 
2370 TypeIndex CodeViewDebug::getVBPTypeIndex() {
2371   if (!VBPType.getIndex()) {
2372     // Make a 'const int *' type.
2373     ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);
2374     TypeIndex ModifiedTI = TypeTable.writeLeafType(MR);
2375 
2376     PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
2377                                                   : PointerKind::Near32;
2378     PointerMode PM = PointerMode::Pointer;
2379     PointerOptions PO = PointerOptions::None;
2380     PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());
2381     VBPType = TypeTable.writeLeafType(PR);
2382   }
2383 
2384   return VBPType;
2385 }
2386 
2387 TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef, DITypeRef ClassTyRef) {
2388   const DIType *Ty = TypeRef.resolve();
2389   const DIType *ClassTy = ClassTyRef.resolve();
2390 
2391   // The null DIType is the void type. Don't try to hash it.
2392   if (!Ty)
2393     return TypeIndex::Void();
2394 
2395   // Check if we've already translated this type. Don't try to do a
2396   // get-or-create style insertion that caches the hash lookup across the
2397   // lowerType call. It will update the TypeIndices map.
2398   auto I = TypeIndices.find({Ty, ClassTy});
2399   if (I != TypeIndices.end())
2400     return I->second;
2401 
2402   TypeLoweringScope S(*this);
2403   TypeIndex TI = lowerType(Ty, ClassTy);
2404   return recordTypeIndexForDINode(Ty, TI, ClassTy);
2405 }
2406 
2407 codeview::TypeIndex
2408 CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
2409                                       const DISubroutineType *SubroutineTy) {
2410   assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type &&
2411          "this type must be a pointer type");
2412 
2413   PointerOptions Options = PointerOptions::None;
2414   if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference)
2415     Options = PointerOptions::LValueRefThisPointer;
2416   else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference)
2417     Options = PointerOptions::RValueRefThisPointer;
2418 
2419   // Check if we've already translated this type.  If there is no ref qualifier
2420   // on the function then we look up this pointer type with no associated class
2421   // so that the TypeIndex for the this pointer can be shared with the type
2422   // index for other pointers to this class type.  If there is a ref qualifier
2423   // then we lookup the pointer using the subroutine as the parent type.
2424   auto I = TypeIndices.find({PtrTy, SubroutineTy});
2425   if (I != TypeIndices.end())
2426     return I->second;
2427 
2428   TypeLoweringScope S(*this);
2429   TypeIndex TI = lowerTypePointer(PtrTy, Options);
2430   return recordTypeIndexForDINode(PtrTy, TI, SubroutineTy);
2431 }
2432 
2433 TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(DITypeRef TypeRef) {
2434   DIType *Ty = TypeRef.resolve();
2435   PointerRecord PR(getTypeIndex(Ty),
2436                    getPointerSizeInBytes() == 8 ? PointerKind::Near64
2437                                                 : PointerKind::Near32,
2438                    PointerMode::LValueReference, PointerOptions::None,
2439                    Ty->getSizeInBits() / 8);
2440   return TypeTable.writeLeafType(PR);
2441 }
2442 
2443 TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) {
2444   const DIType *Ty = TypeRef.resolve();
2445 
2446   // The null DIType is the void type. Don't try to hash it.
2447   if (!Ty)
2448     return TypeIndex::Void();
2449 
2450   // Look through typedefs when getting the complete type index. Call
2451   // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are
2452   // emitted only once.
2453   if (Ty->getTag() == dwarf::DW_TAG_typedef)
2454     (void)getTypeIndex(Ty);
2455   while (Ty->getTag() == dwarf::DW_TAG_typedef)
2456     Ty = cast<DIDerivedType>(Ty)->getBaseType().resolve();
2457 
2458   // If this is a non-record type, the complete type index is the same as the
2459   // normal type index. Just call getTypeIndex.
2460   switch (Ty->getTag()) {
2461   case dwarf::DW_TAG_class_type:
2462   case dwarf::DW_TAG_structure_type:
2463   case dwarf::DW_TAG_union_type:
2464     break;
2465   default:
2466     return getTypeIndex(Ty);
2467   }
2468 
2469   // Check if we've already translated the complete record type.
2470   const auto *CTy = cast<DICompositeType>(Ty);
2471   auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
2472   if (!InsertResult.second)
2473     return InsertResult.first->second;
2474 
2475   TypeLoweringScope S(*this);
2476 
2477   // Make sure the forward declaration is emitted first. It's unclear if this
2478   // is necessary, but MSVC does it, and we should follow suit until we can show
2479   // otherwise.
2480   // We only emit a forward declaration for named types.
2481   if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) {
2482     TypeIndex FwdDeclTI = getTypeIndex(CTy);
2483 
2484     // Just use the forward decl if we don't have complete type info. This
2485     // might happen if the frontend is using modules and expects the complete
2486     // definition to be emitted elsewhere.
2487     if (CTy->isForwardDecl())
2488       return FwdDeclTI;
2489   }
2490 
2491   TypeIndex TI;
2492   switch (CTy->getTag()) {
2493   case dwarf::DW_TAG_class_type:
2494   case dwarf::DW_TAG_structure_type:
2495     TI = lowerCompleteTypeClass(CTy);
2496     break;
2497   case dwarf::DW_TAG_union_type:
2498     TI = lowerCompleteTypeUnion(CTy);
2499     break;
2500   default:
2501     llvm_unreachable("not a record");
2502   }
2503 
2504   // Update the type index associated with this CompositeType.  This cannot
2505   // use the 'InsertResult' iterator above because it is potentially
2506   // invalidated by map insertions which can occur while lowering the class
2507   // type above.
2508   CompleteTypeIndices[CTy] = TI;
2509   return TI;
2510 }
2511 
2512 /// Emit all the deferred complete record types. Try to do this in FIFO order,
2513 /// and do this until fixpoint, as each complete record type typically
2514 /// references
2515 /// many other record types.
2516 void CodeViewDebug::emitDeferredCompleteTypes() {
2517   SmallVector<const DICompositeType *, 4> TypesToEmit;
2518   while (!DeferredCompleteTypes.empty()) {
2519     std::swap(DeferredCompleteTypes, TypesToEmit);
2520     for (const DICompositeType *RecordTy : TypesToEmit)
2521       getCompleteTypeIndex(RecordTy);
2522     TypesToEmit.clear();
2523   }
2524 }
2525 
2526 void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI,
2527                                           ArrayRef<LocalVariable> Locals) {
2528   // Get the sorted list of parameters and emit them first.
2529   SmallVector<const LocalVariable *, 6> Params;
2530   for (const LocalVariable &L : Locals)
2531     if (L.DIVar->isParameter())
2532       Params.push_back(&L);
2533   llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) {
2534     return L->DIVar->getArg() < R->DIVar->getArg();
2535   });
2536   for (const LocalVariable *L : Params)
2537     emitLocalVariable(FI, *L);
2538 
2539   // Next emit all non-parameters in the order that we found them.
2540   for (const LocalVariable &L : Locals)
2541     if (!L.DIVar->isParameter())
2542       emitLocalVariable(FI, L);
2543 }
2544 
2545 /// Only call this on endian-specific types like ulittle16_t and little32_t, or
2546 /// structs composed of them.
2547 template <typename T>
2548 static void copyBytesForDefRange(SmallString<20> &BytePrefix,
2549                                  SymbolKind SymKind, const T &DefRangeHeader) {
2550   BytePrefix.resize(2 + sizeof(T));
2551   ulittle16_t SymKindLE = ulittle16_t(SymKind);
2552   memcpy(&BytePrefix[0], &SymKindLE, 2);
2553   memcpy(&BytePrefix[2], &DefRangeHeader, sizeof(T));
2554 }
2555 
2556 void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI,
2557                                       const LocalVariable &Var) {
2558   // LocalSym record, see SymbolRecord.h for more info.
2559   MCSymbol *LocalEnd = beginSymbolRecord(SymbolKind::S_LOCAL);
2560 
2561   LocalSymFlags Flags = LocalSymFlags::None;
2562   if (Var.DIVar->isParameter())
2563     Flags |= LocalSymFlags::IsParameter;
2564   if (Var.DefRanges.empty())
2565     Flags |= LocalSymFlags::IsOptimizedOut;
2566 
2567   OS.AddComment("TypeIndex");
2568   TypeIndex TI = Var.UseReferenceType
2569                      ? getTypeIndexForReferenceTo(Var.DIVar->getType())
2570                      : getCompleteTypeIndex(Var.DIVar->getType());
2571   OS.EmitIntValue(TI.getIndex(), 4);
2572   OS.AddComment("Flags");
2573   OS.EmitIntValue(static_cast<uint16_t>(Flags), 2);
2574   // Truncate the name so we won't overflow the record length field.
2575   emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
2576   endSymbolRecord(LocalEnd);
2577 
2578   // Calculate the on disk prefix of the appropriate def range record. The
2579   // records and on disk formats are described in SymbolRecords.h. BytePrefix
2580   // should be big enough to hold all forms without memory allocation.
2581   SmallString<20> BytePrefix;
2582   for (const LocalVarDefRange &DefRange : Var.DefRanges) {
2583     BytePrefix.clear();
2584     if (DefRange.InMemory) {
2585       int Offset = DefRange.DataOffset;
2586       unsigned Reg = DefRange.CVRegister;
2587 
2588       // 32-bit x86 call sequences often use PUSH instructions, which disrupt
2589       // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0,
2590       // instead. In frames without stack realignment, $T0 will be the CFA.
2591       if (RegisterId(Reg) == RegisterId::ESP) {
2592         Reg = unsigned(RegisterId::VFRAME);
2593         Offset += FI.OffsetAdjustment;
2594       }
2595 
2596       // If we can use the chosen frame pointer for the frame and this isn't a
2597       // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record.
2598       // Otherwise, use S_DEFRANGE_REGISTER_REL.
2599       EncodedFramePtrReg EncFP = encodeFramePtrReg(RegisterId(Reg), TheCPU);
2600       if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None &&
2601           (bool(Flags & LocalSymFlags::IsParameter)
2602                ? (EncFP == FI.EncodedParamFramePtrReg)
2603                : (EncFP == FI.EncodedLocalFramePtrReg))) {
2604         little32_t FPOffset = little32_t(Offset);
2605         copyBytesForDefRange(BytePrefix, S_DEFRANGE_FRAMEPOINTER_REL, FPOffset);
2606       } else {
2607         uint16_t RegRelFlags = 0;
2608         if (DefRange.IsSubfield) {
2609           RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag |
2610                         (DefRange.StructOffset
2611                          << DefRangeRegisterRelSym::OffsetInParentShift);
2612         }
2613         DefRangeRegisterRelSym::Header DRHdr;
2614         DRHdr.Register = Reg;
2615         DRHdr.Flags = RegRelFlags;
2616         DRHdr.BasePointerOffset = Offset;
2617         copyBytesForDefRange(BytePrefix, S_DEFRANGE_REGISTER_REL, DRHdr);
2618       }
2619     } else {
2620       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
2621       if (DefRange.IsSubfield) {
2622         DefRangeSubfieldRegisterSym::Header DRHdr;
2623         DRHdr.Register = DefRange.CVRegister;
2624         DRHdr.MayHaveNoName = 0;
2625         DRHdr.OffsetInParent = DefRange.StructOffset;
2626         copyBytesForDefRange(BytePrefix, S_DEFRANGE_SUBFIELD_REGISTER, DRHdr);
2627       } else {
2628         DefRangeRegisterSym::Header DRHdr;
2629         DRHdr.Register = DefRange.CVRegister;
2630         DRHdr.MayHaveNoName = 0;
2631         copyBytesForDefRange(BytePrefix, S_DEFRANGE_REGISTER, DRHdr);
2632       }
2633     }
2634     OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
2635   }
2636 }
2637 
2638 void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
2639                                          const FunctionInfo& FI) {
2640   for (LexicalBlock *Block : Blocks)
2641     emitLexicalBlock(*Block, FI);
2642 }
2643 
2644 /// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a
2645 /// lexical block scope.
2646 void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block,
2647                                      const FunctionInfo& FI) {
2648   MCSymbol *RecordEnd = beginSymbolRecord(SymbolKind::S_BLOCK32);
2649   OS.AddComment("PtrParent");
2650   OS.EmitIntValue(0, 4);                                  // PtrParent
2651   OS.AddComment("PtrEnd");
2652   OS.EmitIntValue(0, 4);                                  // PtrEnd
2653   OS.AddComment("Code size");
2654   OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4);   // Code Size
2655   OS.AddComment("Function section relative address");
2656   OS.EmitCOFFSecRel32(Block.Begin, /*Offset=*/0);         // Func Offset
2657   OS.AddComment("Function section index");
2658   OS.EmitCOFFSectionIndex(FI.Begin);                      // Func Symbol
2659   OS.AddComment("Lexical block name");
2660   emitNullTerminatedSymbolName(OS, Block.Name);           // Name
2661   endSymbolRecord(RecordEnd);
2662 
2663   // Emit variables local to this lexical block.
2664   emitLocalVariableList(FI, Block.Locals);
2665   emitGlobalVariableList(Block.Globals);
2666 
2667   // Emit lexical blocks contained within this block.
2668   emitLexicalBlockList(Block.Children, FI);
2669 
2670   // Close the lexical block scope.
2671   emitEndSymbolRecord(SymbolKind::S_END);
2672 }
2673 
2674 /// Convenience routine for collecting lexical block information for a list
2675 /// of lexical scopes.
2676 void CodeViewDebug::collectLexicalBlockInfo(
2677         SmallVectorImpl<LexicalScope *> &Scopes,
2678         SmallVectorImpl<LexicalBlock *> &Blocks,
2679         SmallVectorImpl<LocalVariable> &Locals,
2680         SmallVectorImpl<CVGlobalVariable> &Globals) {
2681   for (LexicalScope *Scope : Scopes)
2682     collectLexicalBlockInfo(*Scope, Blocks, Locals, Globals);
2683 }
2684 
2685 /// Populate the lexical blocks and local variable lists of the parent with
2686 /// information about the specified lexical scope.
2687 void CodeViewDebug::collectLexicalBlockInfo(
2688     LexicalScope &Scope,
2689     SmallVectorImpl<LexicalBlock *> &ParentBlocks,
2690     SmallVectorImpl<LocalVariable> &ParentLocals,
2691     SmallVectorImpl<CVGlobalVariable> &ParentGlobals) {
2692   if (Scope.isAbstractScope())
2693     return;
2694 
2695   // Gather information about the lexical scope including local variables,
2696   // global variables, and address ranges.
2697   bool IgnoreScope = false;
2698   auto LI = ScopeVariables.find(&Scope);
2699   SmallVectorImpl<LocalVariable> *Locals =
2700       LI != ScopeVariables.end() ? &LI->second : nullptr;
2701   auto GI = ScopeGlobals.find(Scope.getScopeNode());
2702   SmallVectorImpl<CVGlobalVariable> *Globals =
2703       GI != ScopeGlobals.end() ? GI->second.get() : nullptr;
2704   const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode());
2705   const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges();
2706 
2707   // Ignore lexical scopes which do not contain variables.
2708   if (!Locals && !Globals)
2709     IgnoreScope = true;
2710 
2711   // Ignore lexical scopes which are not lexical blocks.
2712   if (!DILB)
2713     IgnoreScope = true;
2714 
2715   // Ignore scopes which have too many address ranges to represent in the
2716   // current CodeView format or do not have a valid address range.
2717   //
2718   // For lexical scopes with multiple address ranges you may be tempted to
2719   // construct a single range covering every instruction where the block is
2720   // live and everything in between.  Unfortunately, Visual Studio only
2721   // displays variables from the first matching lexical block scope.  If the
2722   // first lexical block contains exception handling code or cold code which
2723   // is moved to the bottom of the routine creating a single range covering
2724   // nearly the entire routine, then it will hide all other lexical blocks
2725   // and the variables they contain.
2726   if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second))
2727     IgnoreScope = true;
2728 
2729   if (IgnoreScope) {
2730     // This scope can be safely ignored and eliminating it will reduce the
2731     // size of the debug information. Be sure to collect any variable and scope
2732     // information from the this scope or any of its children and collapse them
2733     // into the parent scope.
2734     if (Locals)
2735       ParentLocals.append(Locals->begin(), Locals->end());
2736     if (Globals)
2737       ParentGlobals.append(Globals->begin(), Globals->end());
2738     collectLexicalBlockInfo(Scope.getChildren(),
2739                             ParentBlocks,
2740                             ParentLocals,
2741                             ParentGlobals);
2742     return;
2743   }
2744 
2745   // Create a new CodeView lexical block for this lexical scope.  If we've
2746   // seen this DILexicalBlock before then the scope tree is malformed and
2747   // we can handle this gracefully by not processing it a second time.
2748   auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()});
2749   if (!BlockInsertion.second)
2750     return;
2751 
2752   // Create a lexical block containing the variables and collect the the
2753   // lexical block information for the children.
2754   const InsnRange &Range = Ranges.front();
2755   assert(Range.first && Range.second);
2756   LexicalBlock &Block = BlockInsertion.first->second;
2757   Block.Begin = getLabelBeforeInsn(Range.first);
2758   Block.End = getLabelAfterInsn(Range.second);
2759   assert(Block.Begin && "missing label for scope begin");
2760   assert(Block.End && "missing label for scope end");
2761   Block.Name = DILB->getName();
2762   if (Locals)
2763     Block.Locals = std::move(*Locals);
2764   if (Globals)
2765     Block.Globals = std::move(*Globals);
2766   ParentBlocks.push_back(&Block);
2767   collectLexicalBlockInfo(Scope.getChildren(),
2768                           Block.Children,
2769                           Block.Locals,
2770                           Block.Globals);
2771 }
2772 
2773 void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) {
2774   const Function &GV = MF->getFunction();
2775   assert(FnDebugInfo.count(&GV));
2776   assert(CurFn == FnDebugInfo[&GV].get());
2777 
2778   collectVariableInfo(GV.getSubprogram());
2779 
2780   // Build the lexical block structure to emit for this routine.
2781   if (LexicalScope *CFS = LScopes.getCurrentFunctionScope())
2782     collectLexicalBlockInfo(*CFS,
2783                             CurFn->ChildBlocks,
2784                             CurFn->Locals,
2785                             CurFn->Globals);
2786 
2787   // Clear the scope and variable information from the map which will not be
2788   // valid after we have finished processing this routine.  This also prepares
2789   // the map for the subsequent routine.
2790   ScopeVariables.clear();
2791 
2792   // Don't emit anything if we don't have any line tables.
2793   // Thunks are compiler-generated and probably won't have source correlation.
2794   if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) {
2795     FnDebugInfo.erase(&GV);
2796     CurFn = nullptr;
2797     return;
2798   }
2799 
2800   CurFn->Annotations = MF->getCodeViewAnnotations();
2801 
2802   CurFn->End = Asm->getFunctionEnd();
2803 
2804   CurFn = nullptr;
2805 }
2806 
2807 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
2808   DebugHandlerBase::beginInstruction(MI);
2809 
2810   // Ignore DBG_VALUE and DBG_LABEL locations and function prologue.
2811   if (!Asm || !CurFn || MI->isDebugInstr() ||
2812       MI->getFlag(MachineInstr::FrameSetup))
2813     return;
2814 
2815   // If the first instruction of a new MBB has no location, find the first
2816   // instruction with a location and use that.
2817   DebugLoc DL = MI->getDebugLoc();
2818   if (!DL && MI->getParent() != PrevInstBB) {
2819     for (const auto &NextMI : *MI->getParent()) {
2820       if (NextMI.isDebugInstr())
2821         continue;
2822       DL = NextMI.getDebugLoc();
2823       if (DL)
2824         break;
2825     }
2826   }
2827   PrevInstBB = MI->getParent();
2828 
2829   // If we still don't have a debug location, don't record a location.
2830   if (!DL)
2831     return;
2832 
2833   maybeRecordLocation(DL, Asm->MF);
2834 }
2835 
2836 MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) {
2837   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
2838            *EndLabel = MMI->getContext().createTempSymbol();
2839   OS.EmitIntValue(unsigned(Kind), 4);
2840   OS.AddComment("Subsection size");
2841   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
2842   OS.EmitLabel(BeginLabel);
2843   return EndLabel;
2844 }
2845 
2846 void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
2847   OS.EmitLabel(EndLabel);
2848   // Every subsection must be aligned to a 4-byte boundary.
2849   OS.EmitValueToAlignment(4);
2850 }
2851 
2852 static StringRef getSymbolName(SymbolKind SymKind) {
2853   for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames())
2854     if (EE.Value == SymKind)
2855       return EE.Name;
2856   return "";
2857 }
2858 
2859 MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) {
2860   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
2861            *EndLabel = MMI->getContext().createTempSymbol();
2862   OS.AddComment("Record length");
2863   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
2864   OS.EmitLabel(BeginLabel);
2865   if (OS.isVerboseAsm())
2866     OS.AddComment("Record kind: " + getSymbolName(SymKind));
2867   OS.EmitIntValue(unsigned(SymKind), 2);
2868   return EndLabel;
2869 }
2870 
2871 void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) {
2872   // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid
2873   // an extra copy of every symbol record in LLD. This increases object file
2874   // size by less than 1% in the clang build, and is compatible with the Visual
2875   // C++ linker.
2876   OS.EmitValueToAlignment(4);
2877   OS.EmitLabel(SymEnd);
2878 }
2879 
2880 void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) {
2881   OS.AddComment("Record length");
2882   OS.EmitIntValue(2, 2);
2883   if (OS.isVerboseAsm())
2884     OS.AddComment("Record kind: " + getSymbolName(EndKind));
2885   OS.EmitIntValue(unsigned(EndKind), 2); // Record Kind
2886 }
2887 
2888 void CodeViewDebug::emitDebugInfoForUDTs(
2889     ArrayRef<std::pair<std::string, const DIType *>> UDTs) {
2890   for (const auto &UDT : UDTs) {
2891     const DIType *T = UDT.second;
2892     assert(shouldEmitUdt(T));
2893 
2894     MCSymbol *UDTRecordEnd = beginSymbolRecord(SymbolKind::S_UDT);
2895     OS.AddComment("Type");
2896     OS.EmitIntValue(getCompleteTypeIndex(T).getIndex(), 4);
2897     emitNullTerminatedSymbolName(OS, UDT.first);
2898     endSymbolRecord(UDTRecordEnd);
2899   }
2900 }
2901 
2902 void CodeViewDebug::collectGlobalVariableInfo() {
2903   DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>
2904       GlobalMap;
2905   for (const GlobalVariable &GV : MMI->getModule()->globals()) {
2906     SmallVector<DIGlobalVariableExpression *, 1> GVEs;
2907     GV.getDebugInfo(GVEs);
2908     for (const auto *GVE : GVEs)
2909       GlobalMap[GVE] = &GV;
2910   }
2911 
2912   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
2913   for (const MDNode *Node : CUs->operands()) {
2914     const auto *CU = cast<DICompileUnit>(Node);
2915     for (const auto *GVE : CU->getGlobalVariables()) {
2916       const auto *GV = GlobalMap.lookup(GVE);
2917       if (!GV || GV->isDeclarationForLinker())
2918         continue;
2919       const DIGlobalVariable *DIGV = GVE->getVariable();
2920       DIScope *Scope = DIGV->getScope();
2921       SmallVector<CVGlobalVariable, 1> *VariableList;
2922       if (Scope && isa<DILocalScope>(Scope)) {
2923         // Locate a global variable list for this scope, creating one if
2924         // necessary.
2925         auto Insertion = ScopeGlobals.insert(
2926             {Scope, std::unique_ptr<GlobalVariableList>()});
2927         if (Insertion.second)
2928           Insertion.first->second = llvm::make_unique<GlobalVariableList>();
2929         VariableList = Insertion.first->second.get();
2930       } else if (GV->hasComdat())
2931         // Emit this global variable into a COMDAT section.
2932         VariableList = &ComdatVariables;
2933       else
2934         // Emit this globla variable in a single global symbol section.
2935         VariableList = &GlobalVariables;
2936       CVGlobalVariable CVGV = {DIGV, GV};
2937       VariableList->emplace_back(std::move(CVGV));
2938     }
2939   }
2940 }
2941 
2942 void CodeViewDebug::emitDebugInfoForGlobals() {
2943   // First, emit all globals that are not in a comdat in a single symbol
2944   // substream. MSVC doesn't like it if the substream is empty, so only open
2945   // it if we have at least one global to emit.
2946   switchToDebugSectionForSymbol(nullptr);
2947   if (!GlobalVariables.empty()) {
2948     OS.AddComment("Symbol subsection for globals");
2949     MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
2950     emitGlobalVariableList(GlobalVariables);
2951     endCVSubsection(EndLabel);
2952   }
2953 
2954   // Second, emit each global that is in a comdat into its own .debug$S
2955   // section along with its own symbol substream.
2956   for (const CVGlobalVariable &CVGV : ComdatVariables) {
2957     MCSymbol *GVSym = Asm->getSymbol(CVGV.GV);
2958     OS.AddComment("Symbol subsection for " +
2959             Twine(GlobalValue::dropLLVMManglingEscape(CVGV.GV->getName())));
2960     switchToDebugSectionForSymbol(GVSym);
2961     MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
2962     // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
2963     emitDebugInfoForGlobal(CVGV.DIGV, CVGV.GV, GVSym);
2964     endCVSubsection(EndLabel);
2965   }
2966 }
2967 
2968 void CodeViewDebug::emitDebugInfoForRetainedTypes() {
2969   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
2970   for (const MDNode *Node : CUs->operands()) {
2971     for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {
2972       if (DIType *RT = dyn_cast<DIType>(Ty)) {
2973         getTypeIndex(RT);
2974         // FIXME: Add to global/local DTU list.
2975       }
2976     }
2977   }
2978 }
2979 
2980 // Emit each global variable in the specified array.
2981 void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) {
2982   for (const CVGlobalVariable &CVGV : Globals) {
2983     MCSymbol *GVSym = Asm->getSymbol(CVGV.GV);
2984     // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
2985     emitDebugInfoForGlobal(CVGV.DIGV, CVGV.GV, GVSym);
2986   }
2987 }
2988 
2989 void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
2990                                            const GlobalVariable *GV,
2991                                            MCSymbol *GVSym) {
2992   // DataSym record, see SymbolRecord.h for more info. Thread local data
2993   // happens to have the same format as global data.
2994   SymbolKind DataSym = GV->isThreadLocal()
2995                            ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD32
2996                                                     : SymbolKind::S_GTHREAD32)
2997                            : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA32
2998                                                     : SymbolKind::S_GDATA32);
2999   MCSymbol *DataEnd = beginSymbolRecord(DataSym);
3000   OS.AddComment("Type");
3001   OS.EmitIntValue(getCompleteTypeIndex(DIGV->getType()).getIndex(), 4);
3002   OS.AddComment("DataOffset");
3003   OS.EmitCOFFSecRel32(GVSym, /*Offset=*/0);
3004   OS.AddComment("Segment");
3005   OS.EmitCOFFSectionIndex(GVSym);
3006   OS.AddComment("Name");
3007   const unsigned LengthOfDataRecord = 12;
3008   emitNullTerminatedSymbolName(OS, DIGV->getName(), LengthOfDataRecord);
3009   endSymbolRecord(DataEnd);
3010 }
3011