1 //===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf debug info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "DwarfDebug.h"
15 #include "ByteStreamer.h"
16 #include "DIEHash.h"
17 #include "DebugLocEntry.h"
18 #include "DwarfCompileUnit.h"
19 #include "DwarfExpression.h"
20 #include "DwarfUnit.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/CodeGen/DIE.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DebugInfo.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/ValueHandle.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCDwarf.h"
36 #include "llvm/MC/MCSection.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Dwarf.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/FormattedStream.h"
44 #include "llvm/Support/LEB128.h"
45 #include "llvm/Support/MD5.h"
46 #include "llvm/Support/Path.h"
47 #include "llvm/Support/Timer.h"
48 #include "llvm/Support/raw_ostream.h"
49 #include "llvm/Target/TargetFrameLowering.h"
50 #include "llvm/Target/TargetLoweringObjectFile.h"
51 #include "llvm/Target/TargetMachine.h"
52 #include "llvm/Target/TargetOptions.h"
53 #include "llvm/Target/TargetRegisterInfo.h"
54 #include "llvm/Target/TargetSubtargetInfo.h"
55 
56 using namespace llvm;
57 
58 #define DEBUG_TYPE "dwarfdebug"
59 
60 static cl::opt<bool>
61 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
62                          cl::desc("Disable debug info printing"));
63 
64 static cl::opt<bool>
65 GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
66                        cl::desc("Generate GNU-style pubnames and pubtypes"),
67                        cl::init(false));
68 
69 static cl::opt<bool> GenerateARangeSection("generate-arange-section",
70                                            cl::Hidden,
71                                            cl::desc("Generate dwarf aranges"),
72                                            cl::init(false));
73 
74 namespace {
75 enum DefaultOnOff { Default, Enable, Disable };
76 }
77 
78 static cl::opt<DefaultOnOff> UnknownLocations(
79     "use-unknown-locations", cl::Hidden,
80     cl::desc("Make an absence of debug location information explicit."),
81     cl::values(clEnumVal(Default, "At top of block or after label"),
82                clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
83     cl::init(Default));
84 
85 static cl::opt<DefaultOnOff>
86 DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
87                  cl::desc("Output prototype dwarf accelerator tables."),
88                  cl::values(clEnumVal(Default, "Default for platform"),
89                             clEnumVal(Enable, "Enabled"),
90                             clEnumVal(Disable, "Disabled")),
91                  cl::init(Default));
92 
93 static cl::opt<DefaultOnOff>
94 SplitDwarf("split-dwarf", cl::Hidden,
95            cl::desc("Output DWARF5 split debug info."),
96            cl::values(clEnumVal(Default, "Default for platform"),
97                       clEnumVal(Enable, "Enabled"),
98                       clEnumVal(Disable, "Disabled")),
99            cl::init(Default));
100 
101 static cl::opt<DefaultOnOff>
102 DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
103                  cl::desc("Generate DWARF pubnames and pubtypes sections"),
104                  cl::values(clEnumVal(Default, "Default for platform"),
105                             clEnumVal(Enable, "Enabled"),
106                             clEnumVal(Disable, "Disabled")),
107                  cl::init(Default));
108 
109 enum LinkageNameOption {
110   DefaultLinkageNames,
111   AllLinkageNames,
112   AbstractLinkageNames
113 };
114 static cl::opt<LinkageNameOption>
115     DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
116                       cl::desc("Which DWARF linkage-name attributes to emit."),
117                       cl::values(clEnumValN(DefaultLinkageNames, "Default",
118                                             "Default for platform"),
119                                  clEnumValN(AllLinkageNames, "All", "All"),
120                                  clEnumValN(AbstractLinkageNames, "Abstract",
121                                             "Abstract subprograms")),
122                       cl::init(DefaultLinkageNames));
123 
124 static const char *const DWARFGroupName = "dwarf";
125 static const char *const DWARFGroupDescription = "DWARF Emission";
126 static const char *const DbgTimerName = "writer";
127 static const char *const DbgTimerDescription = "DWARF Debug Writer";
128 
129 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
130   BS.EmitInt8(
131       Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
132                   : dwarf::OperationEncodingString(Op));
133 }
134 
135 void DebugLocDwarfExpression::emitSigned(int64_t Value) {
136   BS.EmitSLEB128(Value, Twine(Value));
137 }
138 
139 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
140   BS.EmitULEB128(Value, Twine(Value));
141 }
142 
143 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
144                                               unsigned MachineReg) {
145   // This information is not available while emitting .debug_loc entries.
146   return false;
147 }
148 
149 //===----------------------------------------------------------------------===//
150 
151 bool DbgVariable::isBlockByrefVariable() const {
152   assert(Var && "Invalid complex DbgVariable!");
153   return Var->getType().resolve()->isBlockByrefStruct();
154 }
155 
156 const DIType *DbgVariable::getType() const {
157   DIType *Ty = Var->getType().resolve();
158   // FIXME: isBlockByrefVariable should be reformulated in terms of complex
159   // addresses instead.
160   if (Ty->isBlockByrefStruct()) {
161     /* Byref variables, in Blocks, are declared by the programmer as
162        "SomeType VarName;", but the compiler creates a
163        __Block_byref_x_VarName struct, and gives the variable VarName
164        either the struct, or a pointer to the struct, as its type.  This
165        is necessary for various behind-the-scenes things the compiler
166        needs to do with by-reference variables in blocks.
167 
168        However, as far as the original *programmer* is concerned, the
169        variable should still have type 'SomeType', as originally declared.
170 
171        The following function dives into the __Block_byref_x_VarName
172        struct to find the original type of the variable.  This will be
173        passed back to the code generating the type for the Debug
174        Information Entry for the variable 'VarName'.  'VarName' will then
175        have the original type 'SomeType' in its debug information.
176 
177        The original type 'SomeType' will be the type of the field named
178        'VarName' inside the __Block_byref_x_VarName struct.
179 
180        NOTE: In order for this to not completely fail on the debugger
181        side, the Debug Information Entry for the variable VarName needs to
182        have a DW_AT_location that tells the debugger how to unwind through
183        the pointers and __Block_byref_x_VarName struct to find the actual
184        value of the variable.  The function addBlockByrefType does this.  */
185     DIType *subType = Ty;
186     uint16_t tag = Ty->getTag();
187 
188     if (tag == dwarf::DW_TAG_pointer_type)
189       subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
190 
191     auto Elements = cast<DICompositeType>(subType)->getElements();
192     for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
193       auto *DT = cast<DIDerivedType>(Elements[i]);
194       if (getName() == DT->getName())
195         return resolve(DT->getBaseType());
196     }
197   }
198   return Ty;
199 }
200 
201 ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
202   if (FrameIndexExprs.size() == 1)
203     return FrameIndexExprs;
204 
205   assert(all_of(FrameIndexExprs,
206                 [](const FrameIndexExpr &A) { return A.Expr->isFragment(); }) &&
207          "multiple FI expressions without DW_OP_LLVM_fragment");
208   std::sort(FrameIndexExprs.begin(), FrameIndexExprs.end(),
209             [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
210               return A.Expr->getFragmentInfo()->OffsetInBits <
211                      B.Expr->getFragmentInfo()->OffsetInBits;
212             });
213   return FrameIndexExprs;
214 }
215 
216 static const DwarfAccelTable::Atom TypeAtoms[] = {
217     DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
218     DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
219     DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
220 
221 DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
222     : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
223       InfoHolder(A, "info_string", DIEValueAllocator),
224       SkeletonHolder(A, "skel_string", DIEValueAllocator),
225       IsDarwin(A->TM.getTargetTriple().isOSDarwin()),
226       AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
227                                        dwarf::DW_FORM_data4)),
228       AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
229                                       dwarf::DW_FORM_data4)),
230       AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
231                                            dwarf::DW_FORM_data4)),
232       AccelTypes(TypeAtoms), DebuggerTuning(DebuggerKind::Default) {
233 
234   CurFn = nullptr;
235   const Triple &TT = Asm->TM.getTargetTriple();
236 
237   // Make sure we know our "debugger tuning."  The target option takes
238   // precedence; fall back to triple-based defaults.
239   if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
240     DebuggerTuning = Asm->TM.Options.DebuggerTuning;
241   else if (IsDarwin)
242     DebuggerTuning = DebuggerKind::LLDB;
243   else if (TT.isPS4CPU())
244     DebuggerTuning = DebuggerKind::SCE;
245   else
246     DebuggerTuning = DebuggerKind::GDB;
247 
248   // Turn on accelerator tables for LLDB by default.
249   if (DwarfAccelTables == Default)
250     HasDwarfAccelTables = tuneForLLDB();
251   else
252     HasDwarfAccelTables = DwarfAccelTables == Enable;
253 
254   HasAppleExtensionAttributes = tuneForLLDB();
255 
256   // Handle split DWARF. Off by default for now.
257   if (SplitDwarf == Default)
258     HasSplitDwarf = false;
259   else
260     HasSplitDwarf = SplitDwarf == Enable;
261 
262   // Pubnames/pubtypes on by default for GDB.
263   if (DwarfPubSections == Default)
264     HasDwarfPubSections = tuneForGDB();
265   else
266     HasDwarfPubSections = DwarfPubSections == Enable;
267 
268   // SCE defaults to linkage names only for abstract subprograms.
269   if (DwarfLinkageNames == DefaultLinkageNames)
270     UseAllLinkageNames = !tuneForSCE();
271   else
272     UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
273 
274   unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
275   unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
276                                     : MMI->getModule()->getDwarfVersion();
277   // Use dwarf 4 by default if nothing is requested.
278   DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION;
279 
280   // Work around a GDB bug. GDB doesn't support the standard opcode;
281   // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
282   // is defined as of DWARF 3.
283   // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
284   // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
285   UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
286 
287   // GDB does not fully support the DWARF 4 representation for bitfields.
288   UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB();
289 
290   Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
291 }
292 
293 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
294 DwarfDebug::~DwarfDebug() { }
295 
296 static bool isObjCClass(StringRef Name) {
297   return Name.startswith("+") || Name.startswith("-");
298 }
299 
300 static bool hasObjCCategory(StringRef Name) {
301   if (!isObjCClass(Name))
302     return false;
303 
304   return Name.find(") ") != StringRef::npos;
305 }
306 
307 static void getObjCClassCategory(StringRef In, StringRef &Class,
308                                  StringRef &Category) {
309   if (!hasObjCCategory(In)) {
310     Class = In.slice(In.find('[') + 1, In.find(' '));
311     Category = "";
312     return;
313   }
314 
315   Class = In.slice(In.find('[') + 1, In.find('('));
316   Category = In.slice(In.find('[') + 1, In.find(' '));
317 }
318 
319 static StringRef getObjCMethodName(StringRef In) {
320   return In.slice(In.find(' ') + 1, In.find(']'));
321 }
322 
323 // Add the various names to the Dwarf accelerator table names.
324 // TODO: Determine whether or not we should add names for programs
325 // that do not have a DW_AT_name or DW_AT_linkage_name field - this
326 // is only slightly different than the lookup of non-standard ObjC names.
327 void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) {
328   if (!SP->isDefinition())
329     return;
330   addAccelName(SP->getName(), Die);
331 
332   // If the linkage name is different than the name, go ahead and output
333   // that as well into the name table.
334   if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName())
335     addAccelName(SP->getLinkageName(), Die);
336 
337   // If this is an Objective-C selector name add it to the ObjC accelerator
338   // too.
339   if (isObjCClass(SP->getName())) {
340     StringRef Class, Category;
341     getObjCClassCategory(SP->getName(), Class, Category);
342     addAccelObjC(Class, Die);
343     if (Category != "")
344       addAccelObjC(Category, Die);
345     // Also add the base method name to the name table.
346     addAccelName(getObjCMethodName(SP->getName()), Die);
347   }
348 }
349 
350 /// Check whether we should create a DIE for the given Scope, return true
351 /// if we don't create a DIE (the corresponding DIE is null).
352 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
353   if (Scope->isAbstractScope())
354     return false;
355 
356   // We don't create a DIE if there is no Range.
357   const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
358   if (Ranges.empty())
359     return true;
360 
361   if (Ranges.size() > 1)
362     return false;
363 
364   // We don't create a DIE if we have a single Range and the end label
365   // is null.
366   return !getLabelAfterInsn(Ranges.front().second);
367 }
368 
369 template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
370   F(CU);
371   if (auto *SkelCU = CU.getSkeleton())
372     if (CU.getCUNode()->getSplitDebugInlining())
373       F(*SkelCU);
374 }
375 
376 void DwarfDebug::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) {
377   assert(Scope && Scope->getScopeNode());
378   assert(Scope->isAbstractScope());
379   assert(!Scope->getInlinedAt());
380 
381   auto *SP = cast<DISubprogram>(Scope->getScopeNode());
382 
383   ProcessedSPNodes.insert(SP);
384 
385   // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
386   // was inlined from another compile unit.
387   auto &CU = *CUMap.lookup(SP->getUnit());
388   forBothCUs(CU, [&](DwarfCompileUnit &CU) {
389     CU.constructAbstractSubprogramScopeDIE(Scope);
390   });
391 }
392 
393 void DwarfDebug::addGnuPubAttributes(DwarfUnit &U, DIE &D) const {
394   if (!GenerateGnuPubSections)
395     return;
396 
397   U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
398 }
399 
400 // Create new DwarfCompileUnit for the given metadata node with tag
401 // DW_TAG_compile_unit.
402 DwarfCompileUnit &
403 DwarfDebug::constructDwarfCompileUnit(const DICompileUnit *DIUnit) {
404   StringRef FN = DIUnit->getFilename();
405   CompilationDir = DIUnit->getDirectory();
406 
407   auto OwnedUnit = make_unique<DwarfCompileUnit>(
408       InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
409   DwarfCompileUnit &NewCU = *OwnedUnit;
410   DIE &Die = NewCU.getUnitDie();
411   InfoHolder.addUnit(std::move(OwnedUnit));
412   if (useSplitDwarf()) {
413     NewCU.setSkeleton(constructSkeletonCU(NewCU));
414     NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
415                     DIUnit->getSplitDebugFilename());
416   }
417 
418   // LTO with assembly output shares a single line table amongst multiple CUs.
419   // To avoid the compilation directory being ambiguous, let the line table
420   // explicitly describe the directory of all files, never relying on the
421   // compilation directory.
422   if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
423     Asm->OutStreamer->getContext().setMCLineTableCompilationDir(
424         NewCU.getUniqueID(), CompilationDir);
425 
426   StringRef Producer = DIUnit->getProducer();
427   StringRef Flags = DIUnit->getFlags();
428   if (!Flags.empty()) {
429     std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
430     NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
431   } else
432     NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
433 
434   NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
435                 DIUnit->getSourceLanguage());
436   NewCU.addString(Die, dwarf::DW_AT_name, FN);
437 
438   if (!useSplitDwarf()) {
439     NewCU.initStmtList();
440 
441     // If we're using split dwarf the compilation dir is going to be in the
442     // skeleton CU and so we don't need to duplicate it here.
443     if (!CompilationDir.empty())
444       NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
445 
446     addGnuPubAttributes(NewCU, Die);
447   }
448 
449   if (useAppleExtensionAttributes()) {
450     if (DIUnit->isOptimized())
451       NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
452 
453     StringRef Flags = DIUnit->getFlags();
454     if (!Flags.empty())
455       NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
456 
457     if (unsigned RVer = DIUnit->getRuntimeVersion())
458       NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
459                     dwarf::DW_FORM_data1, RVer);
460   }
461 
462   if (useSplitDwarf())
463     NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
464   else
465     NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
466 
467   if (DIUnit->getDWOId()) {
468     // This CU is either a clang module DWO or a skeleton CU.
469     NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
470                   DIUnit->getDWOId());
471     if (!DIUnit->getSplitDebugFilename().empty())
472       // This is a prefabricated skeleton CU.
473       NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
474                       DIUnit->getSplitDebugFilename());
475   }
476 
477   CUMap.insert({DIUnit, &NewCU});
478   CUDieMap.insert({&Die, &NewCU});
479   return NewCU;
480 }
481 
482 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
483                                                   const DIImportedEntity *N) {
484   if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
485     D->addChild(TheCU.constructImportedEntityDIE(N));
486 }
487 
488 /// Sort and unique GVEs by comparing their fragment offset.
489 static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
490 sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
491   std::sort(GVEs.begin(), GVEs.end(),
492             [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
493               if (A.Expr != B.Expr && A.Expr && B.Expr) {
494 		auto FragmentA = A.Expr->getFragmentInfo();
495 		auto FragmentB = B.Expr->getFragmentInfo();
496 		if (FragmentA && FragmentB)
497 		  return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
498 	      }
499               return false;
500             });
501   GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
502                          [](DwarfCompileUnit::GlobalExpr A,
503                             DwarfCompileUnit::GlobalExpr B) {
504                            return A.Expr == B.Expr;
505                          }),
506              GVEs.end());
507   return GVEs;
508 }
509 
510 // Emit all Dwarf sections that should come prior to the content. Create
511 // global DIEs and emit initial debug info sections. This is invoked by
512 // the target AsmPrinter.
513 void DwarfDebug::beginModule() {
514   NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName,
515                      DWARFGroupDescription, TimePassesIsEnabled);
516   if (DisableDebugInfoPrinting)
517     return;
518 
519   const Module *M = MMI->getModule();
520 
521   unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
522                                        M->debug_compile_units_end());
523   // Tell MMI whether we have debug info.
524   MMI->setDebugInfoAvailability(NumDebugCUs > 0);
525   SingleCU = NumDebugCUs == 1;
526   DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
527       GVMap;
528   for (const GlobalVariable &Global : M->globals()) {
529     SmallVector<DIGlobalVariableExpression *, 1> GVs;
530     Global.getDebugInfo(GVs);
531     for (auto *GVE : GVs)
532       GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
533   }
534 
535   for (DICompileUnit *CUNode : M->debug_compile_units()) {
536     DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
537     for (auto *IE : CUNode->getImportedEntities())
538       CU.addImportedEntity(IE);
539 
540     // Global Variables.
541     for (auto *GVE : CUNode->getGlobalVariables())
542       GVMap[GVE->getVariable()].push_back({nullptr, GVE->getExpression()});
543     DenseSet<DIGlobalVariable *> Processed;
544     for (auto *GVE : CUNode->getGlobalVariables()) {
545       DIGlobalVariable *GV = GVE->getVariable();
546       if (Processed.insert(GV).second)
547         CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
548     }
549 
550     for (auto *Ty : CUNode->getEnumTypes()) {
551       // The enum types array by design contains pointers to
552       // MDNodes rather than DIRefs. Unique them here.
553       CU.getOrCreateTypeDIE(cast<DIType>(Ty));
554     }
555     for (auto *Ty : CUNode->getRetainedTypes()) {
556       // The retained types array by design contains pointers to
557       // MDNodes rather than DIRefs. Unique them here.
558       if (DIType *RT = dyn_cast<DIType>(Ty))
559           // There is no point in force-emitting a forward declaration.
560           CU.getOrCreateTypeDIE(RT);
561     }
562     // Emit imported_modules last so that the relevant context is already
563     // available.
564     for (auto *IE : CUNode->getImportedEntities())
565       constructAndAddImportedEntityDIE(CU, IE);
566   }
567 }
568 
569 void DwarfDebug::finishVariableDefinitions() {
570   for (const auto &Var : ConcreteVariables) {
571     DIE *VariableDie = Var->getDIE();
572     assert(VariableDie);
573     // FIXME: Consider the time-space tradeoff of just storing the unit pointer
574     // in the ConcreteVariables list, rather than looking it up again here.
575     // DIE::getUnit isn't simple - it walks parent pointers, etc.
576     DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie());
577     assert(Unit);
578     DbgVariable *AbsVar = getExistingAbstractVariable(
579         InlinedVariable(Var->getVariable(), Var->getInlinedAt()));
580     if (AbsVar && AbsVar->getDIE()) {
581       Unit->addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin,
582                         *AbsVar->getDIE());
583     } else
584       Unit->applyVariableAttributes(*Var, *VariableDie);
585   }
586 }
587 
588 void DwarfDebug::finishSubprogramDefinitions() {
589   for (const DISubprogram *SP : ProcessedSPNodes)
590     if (SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug)
591       forBothCUs(*CUMap.lookup(SP->getUnit()), [&](DwarfCompileUnit &CU) {
592         CU.finishSubprogramDefinition(SP);
593       });
594 }
595 
596 void DwarfDebug::finalizeModuleInfo() {
597   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
598 
599   finishSubprogramDefinitions();
600 
601   finishVariableDefinitions();
602 
603   // Handle anything that needs to be done on a per-unit basis after
604   // all other generation.
605   for (const auto &P : CUMap) {
606     auto &TheCU = *P.second;
607     // Emit DW_AT_containing_type attribute to connect types with their
608     // vtable holding type.
609     TheCU.constructContainingTypeDIEs();
610 
611     // Add CU specific attributes if we need to add any.
612     // If we're splitting the dwarf out now that we've got the entire
613     // CU then add the dwo id to it.
614     auto *SkCU = TheCU.getSkeleton();
615     if (useSplitDwarf()) {
616       // Emit a unique identifier for this CU.
617       uint64_t ID = DIEHash(Asm).computeCUSignature(TheCU.getUnitDie());
618       TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
619                     dwarf::DW_FORM_data8, ID);
620       SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
621                     dwarf::DW_FORM_data8, ID);
622 
623       // We don't keep track of which addresses are used in which CU so this
624       // is a bit pessimistic under LTO.
625       if (!AddrPool.isEmpty()) {
626         const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol();
627         SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
628                               Sym, Sym);
629       }
630       if (!SkCU->getRangeLists().empty()) {
631         const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
632         SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
633                               Sym, Sym);
634       }
635     }
636 
637     // If we have code split among multiple sections or non-contiguous
638     // ranges of code then emit a DW_AT_ranges attribute on the unit that will
639     // remain in the .o file, otherwise add a DW_AT_low_pc.
640     // FIXME: We should use ranges allow reordering of code ala
641     // .subsections_via_symbols in mach-o. This would mean turning on
642     // ranges for all subprogram DIEs for mach-o.
643     DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
644     if (unsigned NumRanges = TheCU.getRanges().size()) {
645       if (NumRanges > 1)
646         // A DW_AT_low_pc attribute may also be specified in combination with
647         // DW_AT_ranges to specify the default base address for use in
648         // location lists (see Section 2.6.2) and range lists (see Section
649         // 2.17.3).
650         U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
651       else
652         U.setBaseAddress(TheCU.getRanges().front().getStart());
653       U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
654     }
655 
656     auto *CUNode = cast<DICompileUnit>(P.first);
657     // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
658     if (CUNode->getMacros())
659       U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
660                         U.getMacroLabelBegin(),
661                         TLOF.getDwarfMacinfoSection()->getBeginSymbol());
662   }
663 
664   // Compute DIE offsets and sizes.
665   InfoHolder.computeSizeAndOffsets();
666   if (useSplitDwarf())
667     SkeletonHolder.computeSizeAndOffsets();
668 }
669 
670 // Emit all Dwarf sections that should come after the content.
671 void DwarfDebug::endModule() {
672   assert(CurFn == nullptr);
673   assert(CurMI == nullptr);
674 
675   // If we aren't actually generating debug info (check beginModule -
676   // conditionalized on !DisableDebugInfoPrinting and the presence of the
677   // llvm.dbg.cu metadata node)
678   if (!MMI->hasDebugInfo())
679     return;
680 
681   // Finalize the debug info for the module.
682   finalizeModuleInfo();
683 
684   emitDebugStr();
685 
686   if (useSplitDwarf())
687     emitDebugLocDWO();
688   else
689     // Emit info into a debug loc section.
690     emitDebugLoc();
691 
692   // Corresponding abbreviations into a abbrev section.
693   emitAbbreviations();
694 
695   // Emit all the DIEs into a debug info section.
696   emitDebugInfo();
697 
698   // Emit info into a debug aranges section.
699   if (GenerateARangeSection)
700     emitDebugARanges();
701 
702   // Emit info into a debug ranges section.
703   emitDebugRanges();
704 
705   // Emit info into a debug macinfo section.
706   emitDebugMacinfo();
707 
708   if (useSplitDwarf()) {
709     emitDebugStrDWO();
710     emitDebugInfoDWO();
711     emitDebugAbbrevDWO();
712     emitDebugLineDWO();
713     // Emit DWO addresses.
714     AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
715   }
716 
717   // Emit info into the dwarf accelerator table sections.
718   if (useDwarfAccelTables()) {
719     emitAccelNames();
720     emitAccelObjC();
721     emitAccelNamespaces();
722     emitAccelTypes();
723   }
724 
725   // Emit the pubnames and pubtypes sections if requested.
726   if (HasDwarfPubSections) {
727     emitDebugPubNames(GenerateGnuPubSections);
728     emitDebugPubTypes(GenerateGnuPubSections);
729   }
730 
731   // clean up.
732   AbstractVariables.clear();
733 }
734 
735 // Find abstract variable, if any, associated with Var.
736 DbgVariable *
737 DwarfDebug::getExistingAbstractVariable(InlinedVariable IV,
738                                         const DILocalVariable *&Cleansed) {
739   // More then one inlined variable corresponds to one abstract variable.
740   Cleansed = IV.first;
741   auto I = AbstractVariables.find(Cleansed);
742   if (I != AbstractVariables.end())
743     return I->second.get();
744   return nullptr;
745 }
746 
747 DbgVariable *DwarfDebug::getExistingAbstractVariable(InlinedVariable IV) {
748   const DILocalVariable *Cleansed;
749   return getExistingAbstractVariable(IV, Cleansed);
750 }
751 
752 void DwarfDebug::createAbstractVariable(const DILocalVariable *Var,
753                                         LexicalScope *Scope) {
754   assert(Scope && Scope->isAbstractScope());
755   auto AbsDbgVariable = make_unique<DbgVariable>(Var, /* IA */ nullptr);
756   InfoHolder.addScopeVariable(Scope, AbsDbgVariable.get());
757   AbstractVariables[Var] = std::move(AbsDbgVariable);
758 }
759 
760 void DwarfDebug::ensureAbstractVariableIsCreated(InlinedVariable IV,
761                                                  const MDNode *ScopeNode) {
762   const DILocalVariable *Cleansed = nullptr;
763   if (getExistingAbstractVariable(IV, Cleansed))
764     return;
765 
766   createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(
767                                        cast<DILocalScope>(ScopeNode)));
768 }
769 
770 void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(
771     InlinedVariable IV, const MDNode *ScopeNode) {
772   const DILocalVariable *Cleansed = nullptr;
773   if (getExistingAbstractVariable(IV, Cleansed))
774     return;
775 
776   if (LexicalScope *Scope =
777           LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
778     createAbstractVariable(Cleansed, Scope);
779 }
780 
781 // Collect variable information from side table maintained by MF.
782 void DwarfDebug::collectVariableInfoFromMFTable(
783     DenseSet<InlinedVariable> &Processed) {
784   for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
785     if (!VI.Var)
786       continue;
787     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
788            "Expected inlined-at fields to agree");
789 
790     InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt());
791     Processed.insert(Var);
792     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
793 
794     // If variable scope is not found then skip this variable.
795     if (!Scope)
796       continue;
797 
798     ensureAbstractVariableIsCreatedIfScoped(Var, Scope->getScopeNode());
799     auto RegVar = make_unique<DbgVariable>(Var.first, Var.second);
800     RegVar->initializeMMI(VI.Expr, VI.Slot);
801     if (InfoHolder.addScopeVariable(Scope, RegVar.get()))
802       ConcreteVariables.push_back(std::move(RegVar));
803   }
804 }
805 
806 // Get .debug_loc entry for the instruction range starting at MI.
807 static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
808   const DIExpression *Expr = MI->getDebugExpression();
809 
810   assert(MI->getNumOperands() == 4);
811   if (MI->getOperand(0).isReg()) {
812     MachineLocation MLoc;
813     // If the second operand is an immediate, this is a
814     // register-indirect address.
815     if (!MI->getOperand(1).isImm())
816       MLoc.set(MI->getOperand(0).getReg());
817     else
818       MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
819     return DebugLocEntry::Value(Expr, MLoc);
820   }
821   if (MI->getOperand(0).isImm())
822     return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
823   if (MI->getOperand(0).isFPImm())
824     return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
825   if (MI->getOperand(0).isCImm())
826     return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
827 
828   llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
829 }
830 
831 /// \brief If this and Next are describing different fragments of the same
832 /// variable, merge them by appending Next's values to the current
833 /// list of values.
834 /// Return true if the merge was successful.
835 bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) {
836   if (Begin == Next.Begin) {
837     auto *FirstExpr = cast<DIExpression>(Values[0].Expression);
838     auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression);
839     if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment())
840       return false;
841 
842     // We can only merge entries if none of the fragments overlap any others.
843     // In doing so, we can take advantage of the fact that both lists are
844     // sorted.
845     for (unsigned i = 0, j = 0; i < Values.size(); ++i) {
846       for (; j < Next.Values.size(); ++j) {
847         int res = DebugHandlerBase::fragmentCmp(
848             cast<DIExpression>(Values[i].Expression),
849             cast<DIExpression>(Next.Values[j].Expression));
850         if (res == 0) // The two expressions overlap, we can't merge.
851           return false;
852         // Values[i] is entirely before Next.Values[j],
853         // so go back to the next entry of Values.
854         else if (res == -1)
855           break;
856         // Next.Values[j] is entirely before Values[i], so go on to the
857         // next entry of Next.Values.
858       }
859     }
860 
861     addValues(Next.Values);
862     End = Next.End;
863     return true;
864   }
865   return false;
866 }
867 
868 /// Build the location list for all DBG_VALUEs in the function that
869 /// describe the same variable.  If the ranges of several independent
870 /// fragments of the same variable overlap partially, split them up and
871 /// combine the ranges. The resulting DebugLocEntries are will have
872 /// strict monotonically increasing begin addresses and will never
873 /// overlap.
874 //
875 // Input:
876 //
877 //   Ranges History [var, loc, fragment ofs size]
878 // 0 |      [x, (reg0, fragment 0, 32)]
879 // 1 | |    [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry
880 // 2 | |    ...
881 // 3   |    [clobber reg0]
882 // 4        [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of
883 //                                     x.
884 //
885 // Output:
886 //
887 // [0-1]    [x, (reg0, fragment  0, 32)]
888 // [1-3]    [x, (reg0, fragment  0, 32), (reg1, fragment 32, 32)]
889 // [3-4]    [x, (reg1, fragment 32, 32)]
890 // [4- ]    [x, (mem,  fragment  0, 64)]
891 void
892 DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
893                               const DbgValueHistoryMap::InstrRanges &Ranges) {
894   SmallVector<DebugLocEntry::Value, 4> OpenRanges;
895 
896   for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
897     const MachineInstr *Begin = I->first;
898     const MachineInstr *End = I->second;
899     assert(Begin->isDebugValue() && "Invalid History entry");
900 
901     // Check if a variable is inaccessible in this range.
902     if (Begin->getNumOperands() > 1 &&
903         Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
904       OpenRanges.clear();
905       continue;
906     }
907 
908     // If this fragment overlaps with any open ranges, truncate them.
909     const DIExpression *DIExpr = Begin->getDebugExpression();
910     auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) {
911       return fragmentsOverlap(DIExpr, R.getExpression());
912     });
913     OpenRanges.erase(Last, OpenRanges.end());
914 
915     const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
916     assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
917 
918     const MCSymbol *EndLabel;
919     if (End != nullptr)
920       EndLabel = getLabelAfterInsn(End);
921     else if (std::next(I) == Ranges.end())
922       EndLabel = Asm->getFunctionEnd();
923     else
924       EndLabel = getLabelBeforeInsn(std::next(I)->first);
925     assert(EndLabel && "Forgot label after instruction ending a range!");
926 
927     DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
928 
929     auto Value = getDebugLocValue(Begin);
930     DebugLocEntry Loc(StartLabel, EndLabel, Value);
931     bool couldMerge = false;
932 
933     // If this is a fragment, it may belong to the current DebugLocEntry.
934     if (DIExpr->isFragment()) {
935       // Add this value to the list of open ranges.
936       OpenRanges.push_back(Value);
937 
938       // Attempt to add the fragment to the last entry.
939       if (!DebugLoc.empty())
940         if (DebugLoc.back().MergeValues(Loc))
941           couldMerge = true;
942     }
943 
944     if (!couldMerge) {
945       // Need to add a new DebugLocEntry. Add all values from still
946       // valid non-overlapping fragments.
947       if (OpenRanges.size())
948         Loc.addValues(OpenRanges);
949 
950       DebugLoc.push_back(std::move(Loc));
951     }
952 
953     // Attempt to coalesce the ranges of two otherwise identical
954     // DebugLocEntries.
955     auto CurEntry = DebugLoc.rbegin();
956     DEBUG({
957       dbgs() << CurEntry->getValues().size() << " Values:\n";
958       for (auto &Value : CurEntry->getValues())
959         Value.dump();
960       dbgs() << "-----\n";
961     });
962 
963     auto PrevEntry = std::next(CurEntry);
964     if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
965       DebugLoc.pop_back();
966   }
967 }
968 
969 DbgVariable *DwarfDebug::createConcreteVariable(LexicalScope &Scope,
970                                                 InlinedVariable IV) {
971   ensureAbstractVariableIsCreatedIfScoped(IV, Scope.getScopeNode());
972   ConcreteVariables.push_back(make_unique<DbgVariable>(IV.first, IV.second));
973   InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get());
974   return ConcreteVariables.back().get();
975 }
976 
977 // Determine whether this DBG_VALUE is valid at the beginning of the function.
978 static bool validAtEntry(const MachineInstr *MInsn) {
979   auto MBB = MInsn->getParent();
980   // Is it in the entry basic block?
981   if (!MBB->pred_empty())
982     return false;
983   for (MachineBasicBlock::const_reverse_iterator I(MInsn); I != MBB->rend(); ++I)
984     if (!(I->isDebugValue() || I->getFlag(MachineInstr::FrameSetup)))
985       return false;
986   return true;
987 }
988 
989 // Find variables for each lexical scope.
990 void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
991                                      const DISubprogram *SP,
992                                      DenseSet<InlinedVariable> &Processed) {
993   // Grab the variable info that was squirreled away in the MMI side-table.
994   collectVariableInfoFromMFTable(Processed);
995 
996   for (const auto &I : DbgValues) {
997     InlinedVariable IV = I.first;
998     if (Processed.count(IV))
999       continue;
1000 
1001     // Instruction ranges, specifying where IV is accessible.
1002     const auto &Ranges = I.second;
1003     if (Ranges.empty())
1004       continue;
1005 
1006     LexicalScope *Scope = nullptr;
1007     if (const DILocation *IA = IV.second)
1008       Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
1009     else
1010       Scope = LScopes.findLexicalScope(IV.first->getScope());
1011     // If variable scope is not found then skip this variable.
1012     if (!Scope)
1013       continue;
1014 
1015     Processed.insert(IV);
1016     DbgVariable *RegVar = createConcreteVariable(*Scope, IV);
1017 
1018     const MachineInstr *MInsn = Ranges.front().first;
1019     assert(MInsn->isDebugValue() && "History must begin with debug value");
1020 
1021     // Check if there is a single DBG_VALUE, valid throughout the function.
1022     // A single constant is also considered valid for the entire function.
1023     if (Ranges.size() == 1 &&
1024         (MInsn->getOperand(0).isImm() ||
1025          (validAtEntry(MInsn) && Ranges.front().second == nullptr))) {
1026       RegVar->initializeDbgValue(MInsn);
1027       continue;
1028     }
1029 
1030     // Handle multiple DBG_VALUE instructions describing one variable.
1031     DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
1032 
1033     // Build the location list for this variable.
1034     SmallVector<DebugLocEntry, 8> Entries;
1035     buildLocationList(Entries, Ranges);
1036 
1037     // If the variable has a DIBasicType, extract it.  Basic types cannot have
1038     // unique identifiers, so don't bother resolving the type with the
1039     // identifier map.
1040     const DIBasicType *BT = dyn_cast<DIBasicType>(
1041         static_cast<const Metadata *>(IV.first->getType()));
1042 
1043     // Finalize the entry by lowering it into a DWARF bytestream.
1044     for (auto &Entry : Entries)
1045       Entry.finalize(*Asm, List, BT);
1046   }
1047 
1048   // Collect info for variables that were optimized out.
1049   for (const DILocalVariable *DV : SP->getVariables()) {
1050     if (Processed.insert(InlinedVariable(DV, nullptr)).second)
1051       if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
1052         createConcreteVariable(*Scope, InlinedVariable(DV, nullptr));
1053   }
1054 }
1055 
1056 // Process beginning of an instruction.
1057 void DwarfDebug::beginInstruction(const MachineInstr *MI) {
1058   DebugHandlerBase::beginInstruction(MI);
1059   assert(CurMI);
1060 
1061   // Check if source location changes, but ignore DBG_VALUE and CFI locations.
1062   if (MI->isDebugValue() || MI->isCFIInstruction())
1063     return;
1064   const DebugLoc &DL = MI->getDebugLoc();
1065   // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1066   // the last line number actually emitted, to see if it was line 0.
1067   unsigned LastAsmLine =
1068       Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
1069 
1070   if (DL == PrevInstLoc) {
1071     // If we have an ongoing unspecified location, nothing to do here.
1072     if (!DL)
1073       return;
1074     // We have an explicit location, same as the previous location.
1075     // But we might be coming back to it after a line 0 record.
1076     if (LastAsmLine == 0 && DL.getLine() != 0) {
1077       // Reinstate the source location but not marked as a statement.
1078       const MDNode *Scope = DL.getScope();
1079       recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0);
1080     }
1081     return;
1082   }
1083 
1084   if (!DL) {
1085     // We have an unspecified location, which might want to be line 0.
1086     // If we have already emitted a line-0 record, don't repeat it.
1087     if (LastAsmLine == 0)
1088       return;
1089     // If user said Don't Do That, don't do that.
1090     if (UnknownLocations == Disable)
1091       return;
1092     // See if we have a reason to emit a line-0 record now.
1093     // Reasons to emit a line-0 record include:
1094     // - User asked for it (UnknownLocations).
1095     // - Instruction has a label, so it's referenced from somewhere else,
1096     //   possibly debug information; we want it to have a source location.
1097     // - Instruction is at the top of a block; we don't want to inherit the
1098     //   location from the physically previous (maybe unrelated) block.
1099     if (UnknownLocations == Enable || PrevLabel ||
1100         (PrevInstBB && PrevInstBB != MI->getParent())) {
1101       // Preserve the file and column numbers, if we can, to save space in
1102       // the encoded line table.
1103       // Do not update PrevInstLoc, it remembers the last non-0 line.
1104       const MDNode *Scope = nullptr;
1105       unsigned Column = 0;
1106       if (PrevInstLoc) {
1107         Scope = PrevInstLoc.getScope();
1108         Column = PrevInstLoc.getCol();
1109       }
1110       recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
1111     }
1112     return;
1113   }
1114 
1115   // We have an explicit location, different from the previous location.
1116   // Don't repeat a line-0 record, but otherwise emit the new location.
1117   // (The new location might be an explicit line 0, which we do emit.)
1118   if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0)
1119     return;
1120   unsigned Flags = 0;
1121   if (DL == PrologEndLoc) {
1122     Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
1123     PrologEndLoc = DebugLoc();
1124   }
1125   // If the line changed, we call that a new statement; unless we went to
1126   // line 0 and came back, in which case it is not a new statement.
1127   unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
1128   if (DL.getLine() && DL.getLine() != OldLine)
1129     Flags |= DWARF2_FLAG_IS_STMT;
1130 
1131   const MDNode *Scope = DL.getScope();
1132   recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
1133 
1134   // If we're not at line 0, remember this location.
1135   if (DL.getLine())
1136     PrevInstLoc = DL;
1137 }
1138 
1139 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1140   // First known non-DBG_VALUE and non-frame setup location marks
1141   // the beginning of the function body.
1142   for (const auto &MBB : *MF)
1143     for (const auto &MI : MBB)
1144       if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
1145           MI.getDebugLoc())
1146         return MI.getDebugLoc();
1147   return DebugLoc();
1148 }
1149 
1150 // Gather pre-function debug information.  Assumes being called immediately
1151 // after the function entry point has been emitted.
1152 void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
1153   CurFn = MF;
1154 
1155   if (LScopes.empty())
1156     return;
1157 
1158   // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1159   // belongs to so that we add to the correct per-cu line table in the
1160   // non-asm case.
1161   LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1162   // FnScope->getScopeNode() and DI->second should represent the same function,
1163   // though they may not be the same MDNode due to inline functions merged in
1164   // LTO where the debug info metadata still differs (either due to distinct
1165   // written differences - two versions of a linkonce_odr function
1166   // written/copied into two separate files, or some sub-optimal metadata that
1167   // isn't structurally identical (see: file path/name info from clang, which
1168   // includes the directory of the cpp file being built, even when the file name
1169   // is absolute (such as an <> lookup header)))
1170   auto *SP = cast<DISubprogram>(FnScope->getScopeNode());
1171   DwarfCompileUnit *TheCU = CUMap.lookup(SP->getUnit());
1172   if (!TheCU) {
1173     assert(SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug &&
1174            "DICompileUnit missing from llvm.dbg.cu?");
1175     return;
1176   }
1177   if (Asm->OutStreamer->hasRawTextSupport())
1178     // Use a single line table if we are generating assembly.
1179     Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1180   else
1181     Asm->OutStreamer->getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
1182 
1183   // Record beginning of function.
1184   PrologEndLoc = findPrologueEndLoc(MF);
1185   if (DILocation *L = PrologEndLoc) {
1186     // We'd like to list the prologue as "not statements" but GDB behaves
1187     // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1188     auto *SP = L->getInlinedAtScope()->getSubprogram();
1189     recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
1190   }
1191 }
1192 
1193 void DwarfDebug::skippedNonDebugFunction() {
1194   // If we don't have a subprogram for this function then there will be a hole
1195   // in the range information. Keep note of this by setting the previously used
1196   // section to nullptr.
1197   PrevCU = nullptr;
1198   CurFn = nullptr;
1199 }
1200 
1201 // Gather and emit post-function debug information.
1202 void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
1203   const DISubprogram *SP = MF->getFunction()->getSubprogram();
1204 
1205   assert(CurFn == MF &&
1206       "endFunction should be called with the same function as beginFunction");
1207 
1208   // Set DwarfDwarfCompileUnitID in MCContext to default value.
1209   Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1210 
1211   LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1212   assert(!FnScope || SP == FnScope->getScopeNode());
1213   DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit());
1214 
1215   DenseSet<InlinedVariable> ProcessedVars;
1216   collectVariableInfo(TheCU, SP, ProcessedVars);
1217 
1218   // Add the range of this function to the list of ranges for the CU.
1219   TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
1220 
1221   // Under -gmlt, skip building the subprogram if there are no inlined
1222   // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
1223   // is still needed as we need its source location.
1224   if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
1225       TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
1226       LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1227     assert(InfoHolder.getScopeVariables().empty());
1228     PrevLabel = nullptr;
1229     CurFn = nullptr;
1230     return;
1231   }
1232 
1233 #ifndef NDEBUG
1234   size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1235 #endif
1236   // Construct abstract scopes.
1237   for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
1238     auto *SP = cast<DISubprogram>(AScope->getScopeNode());
1239     // Collect info for variables that were optimized out.
1240     for (const DILocalVariable *DV : SP->getVariables()) {
1241       if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
1242         continue;
1243       ensureAbstractVariableIsCreated(InlinedVariable(DV, nullptr),
1244                                       DV->getScope());
1245       assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1246              && "ensureAbstractVariableIsCreated inserted abstract scopes");
1247     }
1248     constructAbstractSubprogramScopeDIE(AScope);
1249   }
1250 
1251   ProcessedSPNodes.insert(SP);
1252   TheCU.constructSubprogramScopeDIE(SP, FnScope);
1253   if (auto *SkelCU = TheCU.getSkeleton())
1254     if (!LScopes.getAbstractScopesList().empty() &&
1255         TheCU.getCUNode()->getSplitDebugInlining())
1256       SkelCU->constructSubprogramScopeDIE(SP, FnScope);
1257 
1258   // Clear debug info
1259   // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1260   // DbgVariables except those that are also in AbstractVariables (since they
1261   // can be used cross-function)
1262   InfoHolder.getScopeVariables().clear();
1263   PrevLabel = nullptr;
1264   CurFn = nullptr;
1265 }
1266 
1267 // Register a source line with debug info. Returns the  unique label that was
1268 // emitted and which provides correspondence to the source line list.
1269 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1270                                   unsigned Flags) {
1271   StringRef Fn;
1272   StringRef Dir;
1273   unsigned Src = 1;
1274   unsigned Discriminator = 0;
1275   if (auto *Scope = cast_or_null<DIScope>(S)) {
1276     Fn = Scope->getFilename();
1277     Dir = Scope->getDirectory();
1278     if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1279       if (getDwarfVersion() >= 4)
1280         Discriminator = LBF->getDiscriminator();
1281 
1282     unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
1283     Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1284               .getOrCreateSourceID(Fn, Dir);
1285   }
1286   Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1287                                           Discriminator, Fn);
1288 }
1289 
1290 //===----------------------------------------------------------------------===//
1291 // Emit Methods
1292 //===----------------------------------------------------------------------===//
1293 
1294 // Emit the debug info section.
1295 void DwarfDebug::emitDebugInfo() {
1296   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1297   Holder.emitUnits(/* UseOffsets */ false);
1298 }
1299 
1300 // Emit the abbreviation section.
1301 void DwarfDebug::emitAbbreviations() {
1302   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1303 
1304   Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1305 }
1306 
1307 void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section,
1308                            StringRef TableName) {
1309   Accel.FinalizeTable(Asm, TableName);
1310   Asm->OutStreamer->SwitchSection(Section);
1311 
1312   // Emit the full data.
1313   Accel.emit(Asm, Section->getBeginSymbol(), this);
1314 }
1315 
1316 // Emit visible names into a hashed accelerator table section.
1317 void DwarfDebug::emitAccelNames() {
1318   emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1319             "Names");
1320 }
1321 
1322 // Emit objective C classes and categories into a hashed accelerator table
1323 // section.
1324 void DwarfDebug::emitAccelObjC() {
1325   emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1326             "ObjC");
1327 }
1328 
1329 // Emit namespace dies into a hashed accelerator table.
1330 void DwarfDebug::emitAccelNamespaces() {
1331   emitAccel(AccelNamespace,
1332             Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
1333             "namespac");
1334 }
1335 
1336 // Emit type dies into a hashed accelerator table.
1337 void DwarfDebug::emitAccelTypes() {
1338   emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1339             "types");
1340 }
1341 
1342 // Public name handling.
1343 // The format for the various pubnames:
1344 //
1345 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1346 // for the DIE that is named.
1347 //
1348 // gnu pubnames - offset/index value/name tuples where the offset is the offset
1349 // into the CU and the index value is computed according to the type of value
1350 // for the DIE that is named.
1351 //
1352 // For type units the offset is the offset of the skeleton DIE. For split dwarf
1353 // it's the offset within the debug_info/debug_types dwo section, however, the
1354 // reference in the pubname header doesn't change.
1355 
1356 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
1357 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
1358                                                         const DIE *Die) {
1359   // Entities that ended up only in a Type Unit reference the CU instead (since
1360   // the pub entry has offsets within the CU there's no real offset that can be
1361   // provided anyway). As it happens all such entities (namespaces and types,
1362   // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
1363   // not to be true it would be necessary to persist this information from the
1364   // point at which the entry is added to the index data structure - since by
1365   // the time the index is built from that, the original type/namespace DIE in a
1366   // type unit has already been destroyed so it can't be queried for properties
1367   // like tag, etc.
1368   if (Die->getTag() == dwarf::DW_TAG_compile_unit)
1369     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
1370                                           dwarf::GIEL_EXTERNAL);
1371   dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1372 
1373   // We could have a specification DIE that has our most of our knowledge,
1374   // look for that now.
1375   if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1376     DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
1377     if (SpecDIE.findAttribute(dwarf::DW_AT_external))
1378       Linkage = dwarf::GIEL_EXTERNAL;
1379   } else if (Die->findAttribute(dwarf::DW_AT_external))
1380     Linkage = dwarf::GIEL_EXTERNAL;
1381 
1382   switch (Die->getTag()) {
1383   case dwarf::DW_TAG_class_type:
1384   case dwarf::DW_TAG_structure_type:
1385   case dwarf::DW_TAG_union_type:
1386   case dwarf::DW_TAG_enumeration_type:
1387     return dwarf::PubIndexEntryDescriptor(
1388         dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1389                               ? dwarf::GIEL_STATIC
1390                               : dwarf::GIEL_EXTERNAL);
1391   case dwarf::DW_TAG_typedef:
1392   case dwarf::DW_TAG_base_type:
1393   case dwarf::DW_TAG_subrange_type:
1394     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
1395   case dwarf::DW_TAG_namespace:
1396     return dwarf::GIEK_TYPE;
1397   case dwarf::DW_TAG_subprogram:
1398     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
1399   case dwarf::DW_TAG_variable:
1400     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
1401   case dwarf::DW_TAG_enumerator:
1402     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1403                                           dwarf::GIEL_STATIC);
1404   default:
1405     return dwarf::GIEK_NONE;
1406   }
1407 }
1408 
1409 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
1410 ///
1411 void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
1412   MCSection *PSec = GnuStyle
1413                         ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1414                         : Asm->getObjFileLowering().getDwarfPubNamesSection();
1415 
1416   emitDebugPubSection(GnuStyle, PSec, "Names",
1417                       &DwarfCompileUnit::getGlobalNames);
1418 }
1419 
1420 void DwarfDebug::emitDebugPubSection(
1421     bool GnuStyle, MCSection *PSec, StringRef Name,
1422     const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) {
1423   for (const auto &NU : CUMap) {
1424     DwarfCompileUnit *TheU = NU.second;
1425 
1426     const auto &Globals = (TheU->*Accessor)();
1427 
1428     if (Globals.empty())
1429       continue;
1430 
1431     if (auto *Skeleton = TheU->getSkeleton())
1432       TheU = Skeleton;
1433 
1434     // Start the dwarf pubnames section.
1435     Asm->OutStreamer->SwitchSection(PSec);
1436 
1437     // Emit the header.
1438     Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1439     MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1440     MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1441     Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1442 
1443     Asm->OutStreamer->EmitLabel(BeginLabel);
1444 
1445     Asm->OutStreamer->AddComment("DWARF Version");
1446     Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
1447 
1448     Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
1449     Asm->emitDwarfSymbolReference(TheU->getLabelBegin());
1450 
1451     Asm->OutStreamer->AddComment("Compilation Unit Length");
1452     Asm->EmitInt32(TheU->getLength());
1453 
1454     // Emit the pubnames for this compilation unit.
1455     for (const auto &GI : Globals) {
1456       const char *Name = GI.getKeyData();
1457       const DIE *Entity = GI.second;
1458 
1459       Asm->OutStreamer->AddComment("DIE offset");
1460       Asm->EmitInt32(Entity->getOffset());
1461 
1462       if (GnuStyle) {
1463         dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
1464         Asm->OutStreamer->AddComment(
1465             Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1466             dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
1467         Asm->EmitInt8(Desc.toBits());
1468       }
1469 
1470       Asm->OutStreamer->AddComment("External Name");
1471       Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1472     }
1473 
1474     Asm->OutStreamer->AddComment("End Mark");
1475     Asm->EmitInt32(0);
1476     Asm->OutStreamer->EmitLabel(EndLabel);
1477   }
1478 }
1479 
1480 void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
1481   MCSection *PSec = GnuStyle
1482                         ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1483                         : Asm->getObjFileLowering().getDwarfPubTypesSection();
1484 
1485   emitDebugPubSection(GnuStyle, PSec, "Types",
1486                       &DwarfCompileUnit::getGlobalTypes);
1487 }
1488 
1489 /// Emit null-terminated strings into a debug str section.
1490 void DwarfDebug::emitDebugStr() {
1491   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1492   Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1493 }
1494 
1495 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
1496                                    const DebugLocStream::Entry &Entry) {
1497   auto &&Comments = DebugLocs.getComments(Entry);
1498   auto Comment = Comments.begin();
1499   auto End = Comments.end();
1500   for (uint8_t Byte : DebugLocs.getBytes(Entry))
1501     Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
1502 }
1503 
1504 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
1505                               ByteStreamer &Streamer,
1506                               const DebugLocEntry::Value &Value,
1507                               DwarfExpression &DwarfExpr) {
1508   auto *DIExpr = Value.getExpression();
1509   DIExpressionCursor ExprCursor(DIExpr);
1510   DwarfExpr.addFragmentOffset(DIExpr);
1511   // Regular entry.
1512   if (Value.isInt()) {
1513     if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1514                BT->getEncoding() == dwarf::DW_ATE_signed_char))
1515       DwarfExpr.addSignedConstant(Value.getInt());
1516     else
1517       DwarfExpr.addUnsignedConstant(Value.getInt());
1518   } else if (Value.isLocation()) {
1519     MachineLocation Location = Value.getLoc();
1520     SmallVector<uint64_t, 8> Ops;
1521     if (Location.isIndirect() && Location.getOffset()) {
1522       Ops.push_back(dwarf::DW_OP_plus);
1523       Ops.push_back(Location.getOffset());
1524     }
1525     Ops.append(DIExpr->elements_begin(), DIExpr->elements_end());
1526     DIExpressionCursor Cursor(Ops);
1527     const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
1528     if (!DwarfExpr.addMachineLocExpression(TRI, Cursor, Location))
1529       return;
1530     return DwarfExpr.addExpression(std::move(Cursor));
1531   } else if (Value.isConstantFP()) {
1532     APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
1533     DwarfExpr.addUnsignedConstant(RawBytes);
1534   }
1535   DwarfExpr.addExpression(std::move(ExprCursor));
1536 }
1537 
1538 void DebugLocEntry::finalize(const AsmPrinter &AP,
1539                              DebugLocStream::ListBuilder &List,
1540                              const DIBasicType *BT) {
1541   DebugLocStream::EntryBuilder Entry(List, Begin, End);
1542   BufferByteStreamer Streamer = Entry.getStreamer();
1543   DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer);
1544   const DebugLocEntry::Value &Value = Values[0];
1545   if (Value.isFragment()) {
1546     // Emit all fragments that belong to the same variable and range.
1547     assert(all_of(Values, [](DebugLocEntry::Value P) {
1548           return P.isFragment();
1549         }) && "all values are expected to be fragments");
1550     assert(std::is_sorted(Values.begin(), Values.end()) &&
1551            "fragments are expected to be sorted");
1552 
1553     for (auto Fragment : Values)
1554       emitDebugLocValue(AP, BT, Streamer, Fragment, DwarfExpr);
1555 
1556   } else {
1557     assert(Values.size() == 1 && "only fragments may have >1 value");
1558     emitDebugLocValue(AP, BT, Streamer, Value, DwarfExpr);
1559   }
1560   DwarfExpr.finalize();
1561 }
1562 
1563 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
1564   // Emit the size.
1565   Asm->OutStreamer->AddComment("Loc expr size");
1566   Asm->EmitInt16(DebugLocs.getBytes(Entry).size());
1567 
1568   // Emit the entry.
1569   APByteStreamer Streamer(*Asm);
1570   emitDebugLocEntry(Streamer, Entry);
1571 }
1572 
1573 // Emit locations into the debug loc section.
1574 void DwarfDebug::emitDebugLoc() {
1575   // Start the dwarf loc section.
1576   Asm->OutStreamer->SwitchSection(
1577       Asm->getObjFileLowering().getDwarfLocSection());
1578   unsigned char Size = Asm->MAI->getCodePointerSize();
1579   for (const auto &List : DebugLocs.getLists()) {
1580     Asm->OutStreamer->EmitLabel(List.Label);
1581     const DwarfCompileUnit *CU = List.CU;
1582     for (const auto &Entry : DebugLocs.getEntries(List)) {
1583       // Set up the range. This range is relative to the entry point of the
1584       // compile unit. This is a hard coded 0 for low_pc when we're emitting
1585       // ranges, or the DW_AT_low_pc on the compile unit otherwise.
1586       if (auto *Base = CU->getBaseAddress()) {
1587         Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1588         Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
1589       } else {
1590         Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1591         Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
1592       }
1593 
1594       emitDebugLocEntryLocation(Entry);
1595     }
1596     Asm->OutStreamer->EmitIntValue(0, Size);
1597     Asm->OutStreamer->EmitIntValue(0, Size);
1598   }
1599 }
1600 
1601 void DwarfDebug::emitDebugLocDWO() {
1602   Asm->OutStreamer->SwitchSection(
1603       Asm->getObjFileLowering().getDwarfLocDWOSection());
1604   for (const auto &List : DebugLocs.getLists()) {
1605     Asm->OutStreamer->EmitLabel(List.Label);
1606     for (const auto &Entry : DebugLocs.getEntries(List)) {
1607       // Just always use start_length for now - at least that's one address
1608       // rather than two. We could get fancier and try to, say, reuse an
1609       // address we know we've emitted elsewhere (the start of the function?
1610       // The start of the CU or CU subrange that encloses this range?)
1611       Asm->EmitInt8(dwarf::DW_LLE_startx_length);
1612       unsigned idx = AddrPool.getIndex(Entry.BeginSym);
1613       Asm->EmitULEB128(idx);
1614       Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
1615 
1616       emitDebugLocEntryLocation(Entry);
1617     }
1618     Asm->EmitInt8(dwarf::DW_LLE_end_of_list);
1619   }
1620 }
1621 
1622 struct ArangeSpan {
1623   const MCSymbol *Start, *End;
1624 };
1625 
1626 // Emit a debug aranges section, containing a CU lookup for any
1627 // address we can tie back to a CU.
1628 void DwarfDebug::emitDebugARanges() {
1629   // Provides a unique id per text section.
1630   MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
1631 
1632   // Filter labels by section.
1633   for (const SymbolCU &SCU : ArangeLabels) {
1634     if (SCU.Sym->isInSection()) {
1635       // Make a note of this symbol and it's section.
1636       MCSection *Section = &SCU.Sym->getSection();
1637       if (!Section->getKind().isMetadata())
1638         SectionMap[Section].push_back(SCU);
1639     } else {
1640       // Some symbols (e.g. common/bss on mach-o) can have no section but still
1641       // appear in the output. This sucks as we rely on sections to build
1642       // arange spans. We can do it without, but it's icky.
1643       SectionMap[nullptr].push_back(SCU);
1644     }
1645   }
1646 
1647   DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
1648 
1649   for (auto &I : SectionMap) {
1650     MCSection *Section = I.first;
1651     SmallVector<SymbolCU, 8> &List = I.second;
1652     if (List.size() < 1)
1653       continue;
1654 
1655     // If we have no section (e.g. common), just write out
1656     // individual spans for each symbol.
1657     if (!Section) {
1658       for (const SymbolCU &Cur : List) {
1659         ArangeSpan Span;
1660         Span.Start = Cur.Sym;
1661         Span.End = nullptr;
1662         assert(Cur.CU);
1663         Spans[Cur.CU].push_back(Span);
1664       }
1665       continue;
1666     }
1667 
1668     // Sort the symbols by offset within the section.
1669     std::sort(
1670         List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) {
1671           unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1672           unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
1673 
1674           // Symbols with no order assigned should be placed at the end.
1675           // (e.g. section end labels)
1676           if (IA == 0)
1677             return false;
1678           if (IB == 0)
1679             return true;
1680           return IA < IB;
1681         });
1682 
1683     // Insert a final terminator.
1684     List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
1685 
1686     // Build spans between each label.
1687     const MCSymbol *StartSym = List[0].Sym;
1688     for (size_t n = 1, e = List.size(); n < e; n++) {
1689       const SymbolCU &Prev = List[n - 1];
1690       const SymbolCU &Cur = List[n];
1691 
1692       // Try and build the longest span we can within the same CU.
1693       if (Cur.CU != Prev.CU) {
1694         ArangeSpan Span;
1695         Span.Start = StartSym;
1696         Span.End = Cur.Sym;
1697         assert(Prev.CU);
1698         Spans[Prev.CU].push_back(Span);
1699         StartSym = Cur.Sym;
1700       }
1701     }
1702   }
1703 
1704   // Start the dwarf aranges section.
1705   Asm->OutStreamer->SwitchSection(
1706       Asm->getObjFileLowering().getDwarfARangesSection());
1707 
1708   unsigned PtrSize = Asm->MAI->getCodePointerSize();
1709 
1710   // Build a list of CUs used.
1711   std::vector<DwarfCompileUnit *> CUs;
1712   for (const auto &it : Spans) {
1713     DwarfCompileUnit *CU = it.first;
1714     CUs.push_back(CU);
1715   }
1716 
1717   // Sort the CU list (again, to ensure consistent output order).
1718   std::sort(CUs.begin(), CUs.end(),
1719             [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
1720               return A->getUniqueID() < B->getUniqueID();
1721             });
1722 
1723   // Emit an arange table for each CU we used.
1724   for (DwarfCompileUnit *CU : CUs) {
1725     std::vector<ArangeSpan> &List = Spans[CU];
1726 
1727     // Describe the skeleton CU's offset and length, not the dwo file's.
1728     if (auto *Skel = CU->getSkeleton())
1729       CU = Skel;
1730 
1731     // Emit size of content not including length itself.
1732     unsigned ContentSize =
1733         sizeof(int16_t) + // DWARF ARange version number
1734         sizeof(int32_t) + // Offset of CU in the .debug_info section
1735         sizeof(int8_t) +  // Pointer Size (in bytes)
1736         sizeof(int8_t);   // Segment Size (in bytes)
1737 
1738     unsigned TupleSize = PtrSize * 2;
1739 
1740     // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
1741     unsigned Padding =
1742         OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
1743 
1744     ContentSize += Padding;
1745     ContentSize += (List.size() + 1) * TupleSize;
1746 
1747     // For each compile unit, write the list of spans it covers.
1748     Asm->OutStreamer->AddComment("Length of ARange Set");
1749     Asm->EmitInt32(ContentSize);
1750     Asm->OutStreamer->AddComment("DWARF Arange version number");
1751     Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
1752     Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
1753     Asm->emitDwarfSymbolReference(CU->getLabelBegin());
1754     Asm->OutStreamer->AddComment("Address Size (in bytes)");
1755     Asm->EmitInt8(PtrSize);
1756     Asm->OutStreamer->AddComment("Segment Size (in bytes)");
1757     Asm->EmitInt8(0);
1758 
1759     Asm->OutStreamer->emitFill(Padding, 0xff);
1760 
1761     for (const ArangeSpan &Span : List) {
1762       Asm->EmitLabelReference(Span.Start, PtrSize);
1763 
1764       // Calculate the size as being from the span start to it's end.
1765       if (Span.End) {
1766         Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
1767       } else {
1768         // For symbols without an end marker (e.g. common), we
1769         // write a single arange entry containing just that one symbol.
1770         uint64_t Size = SymSize[Span.Start];
1771         if (Size == 0)
1772           Size = 1;
1773 
1774         Asm->OutStreamer->EmitIntValue(Size, PtrSize);
1775       }
1776     }
1777 
1778     Asm->OutStreamer->AddComment("ARange terminator");
1779     Asm->OutStreamer->EmitIntValue(0, PtrSize);
1780     Asm->OutStreamer->EmitIntValue(0, PtrSize);
1781   }
1782 }
1783 
1784 /// Emit address ranges into a debug ranges section.
1785 void DwarfDebug::emitDebugRanges() {
1786   // Start the dwarf ranges section.
1787   Asm->OutStreamer->SwitchSection(
1788       Asm->getObjFileLowering().getDwarfRangesSection());
1789 
1790   // Size for our labels.
1791   unsigned char Size = Asm->MAI->getCodePointerSize();
1792 
1793   // Grab the specific ranges for the compile units in the module.
1794   for (const auto &I : CUMap) {
1795     DwarfCompileUnit *TheCU = I.second;
1796 
1797     if (auto *Skel = TheCU->getSkeleton())
1798       TheCU = Skel;
1799 
1800     // Iterate over the misc ranges for the compile units in the module.
1801     for (const RangeSpanList &List : TheCU->getRangeLists()) {
1802       // Emit our symbol so we can find the beginning of the range.
1803       Asm->OutStreamer->EmitLabel(List.getSym());
1804 
1805       for (const RangeSpan &Range : List.getRanges()) {
1806         const MCSymbol *Begin = Range.getStart();
1807         const MCSymbol *End = Range.getEnd();
1808         assert(Begin && "Range without a begin symbol?");
1809         assert(End && "Range without an end symbol?");
1810         if (auto *Base = TheCU->getBaseAddress()) {
1811           Asm->EmitLabelDifference(Begin, Base, Size);
1812           Asm->EmitLabelDifference(End, Base, Size);
1813         } else {
1814           Asm->OutStreamer->EmitSymbolValue(Begin, Size);
1815           Asm->OutStreamer->EmitSymbolValue(End, Size);
1816         }
1817       }
1818 
1819       // And terminate the list with two 0 values.
1820       Asm->OutStreamer->EmitIntValue(0, Size);
1821       Asm->OutStreamer->EmitIntValue(0, Size);
1822     }
1823   }
1824 }
1825 
1826 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
1827   for (auto *MN : Nodes) {
1828     if (auto *M = dyn_cast<DIMacro>(MN))
1829       emitMacro(*M);
1830     else if (auto *F = dyn_cast<DIMacroFile>(MN))
1831       emitMacroFile(*F, U);
1832     else
1833       llvm_unreachable("Unexpected DI type!");
1834   }
1835 }
1836 
1837 void DwarfDebug::emitMacro(DIMacro &M) {
1838   Asm->EmitULEB128(M.getMacinfoType());
1839   Asm->EmitULEB128(M.getLine());
1840   StringRef Name = M.getName();
1841   StringRef Value = M.getValue();
1842   Asm->OutStreamer->EmitBytes(Name);
1843   if (!Value.empty()) {
1844     // There should be one space between macro name and macro value.
1845     Asm->EmitInt8(' ');
1846     Asm->OutStreamer->EmitBytes(Value);
1847   }
1848   Asm->EmitInt8('\0');
1849 }
1850 
1851 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
1852   assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
1853   Asm->EmitULEB128(dwarf::DW_MACINFO_start_file);
1854   Asm->EmitULEB128(F.getLine());
1855   DIFile *File = F.getFile();
1856   unsigned FID =
1857       U.getOrCreateSourceID(File->getFilename(), File->getDirectory());
1858   Asm->EmitULEB128(FID);
1859   handleMacroNodes(F.getElements(), U);
1860   Asm->EmitULEB128(dwarf::DW_MACINFO_end_file);
1861 }
1862 
1863 /// Emit macros into a debug macinfo section.
1864 void DwarfDebug::emitDebugMacinfo() {
1865   // Start the dwarf macinfo section.
1866   Asm->OutStreamer->SwitchSection(
1867       Asm->getObjFileLowering().getDwarfMacinfoSection());
1868 
1869   for (const auto &P : CUMap) {
1870     auto &TheCU = *P.second;
1871     auto *SkCU = TheCU.getSkeleton();
1872     DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
1873     auto *CUNode = cast<DICompileUnit>(P.first);
1874     Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin());
1875     handleMacroNodes(CUNode->getMacros(), U);
1876   }
1877   Asm->OutStreamer->AddComment("End Of Macro List Mark");
1878   Asm->EmitInt8(0);
1879 }
1880 
1881 // DWARF5 Experimental Separate Dwarf emitters.
1882 
1883 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
1884                                   std::unique_ptr<DwarfCompileUnit> NewU) {
1885   NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
1886                   U.getCUNode()->getSplitDebugFilename());
1887 
1888   if (!CompilationDir.empty())
1889     NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
1890 
1891   addGnuPubAttributes(*NewU, Die);
1892 
1893   SkeletonHolder.addUnit(std::move(NewU));
1894 }
1895 
1896 // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
1897 // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
1898 // DW_AT_addr_base, DW_AT_ranges_base.
1899 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
1900 
1901   auto OwnedUnit = make_unique<DwarfCompileUnit>(
1902       CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
1903   DwarfCompileUnit &NewCU = *OwnedUnit;
1904   NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
1905 
1906   NewCU.initStmtList();
1907 
1908   initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
1909 
1910   return NewCU;
1911 }
1912 
1913 // Emit the .debug_info.dwo section for separated dwarf. This contains the
1914 // compile units that would normally be in debug_info.
1915 void DwarfDebug::emitDebugInfoDWO() {
1916   assert(useSplitDwarf() && "No split dwarf debug info?");
1917   // Don't emit relocations into the dwo file.
1918   InfoHolder.emitUnits(/* UseOffsets */ true);
1919 }
1920 
1921 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
1922 // abbreviations for the .debug_info.dwo section.
1923 void DwarfDebug::emitDebugAbbrevDWO() {
1924   assert(useSplitDwarf() && "No split dwarf?");
1925   InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
1926 }
1927 
1928 void DwarfDebug::emitDebugLineDWO() {
1929   assert(useSplitDwarf() && "No split dwarf?");
1930   Asm->OutStreamer->SwitchSection(
1931       Asm->getObjFileLowering().getDwarfLineDWOSection());
1932   SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams());
1933 }
1934 
1935 // Emit the .debug_str.dwo section for separated dwarf. This contains the
1936 // string section and is identical in format to traditional .debug_str
1937 // sections.
1938 void DwarfDebug::emitDebugStrDWO() {
1939   assert(useSplitDwarf() && "No split dwarf?");
1940   MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
1941   InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
1942                          OffSec);
1943 }
1944 
1945 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
1946   if (!useSplitDwarf())
1947     return nullptr;
1948   if (SingleCU)
1949     SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory());
1950   return &SplitTypeUnitFileTable;
1951 }
1952 
1953 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
1954   MD5 Hash;
1955   Hash.update(Identifier);
1956   // ... take the least significant 8 bytes and return those. Our MD5
1957   // implementation always returns its results in little endian, so we actually
1958   // need the "high" word.
1959   MD5::MD5Result Result;
1960   Hash.final(Result);
1961   return Result.high();
1962 }
1963 
1964 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
1965                                       StringRef Identifier, DIE &RefDie,
1966                                       const DICompositeType *CTy) {
1967   // Fast path if we're building some type units and one has already used the
1968   // address pool we know we're going to throw away all this work anyway, so
1969   // don't bother building dependent types.
1970   if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
1971     return;
1972 
1973   auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
1974   if (!Ins.second) {
1975     CU.addDIETypeSignature(RefDie, Ins.first->second);
1976     return;
1977   }
1978 
1979   bool TopLevelType = TypeUnitsUnderConstruction.empty();
1980   AddrPool.resetUsedFlag();
1981 
1982   auto OwnedUnit = make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
1983                                               getDwoLineTable(CU));
1984   DwarfTypeUnit &NewTU = *OwnedUnit;
1985   DIE &UnitDie = NewTU.getUnitDie();
1986   TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
1987 
1988   NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
1989                 CU.getLanguage());
1990 
1991   uint64_t Signature = makeTypeSignature(Identifier);
1992   NewTU.setTypeSignature(Signature);
1993   Ins.first->second = Signature;
1994 
1995   if (useSplitDwarf())
1996     NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
1997   else {
1998     CU.applyStmtList(UnitDie);
1999     NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2000   }
2001 
2002   NewTU.setType(NewTU.createTypeDIE(CTy));
2003 
2004   if (TopLevelType) {
2005     auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2006     TypeUnitsUnderConstruction.clear();
2007 
2008     // Types referencing entries in the address table cannot be placed in type
2009     // units.
2010     if (AddrPool.hasBeenUsed()) {
2011 
2012       // Remove all the types built while building this type.
2013       // This is pessimistic as some of these types might not be dependent on
2014       // the type that used an address.
2015       for (const auto &TU : TypeUnitsToAdd)
2016         TypeSignatures.erase(TU.second);
2017 
2018       // Construct this type in the CU directly.
2019       // This is inefficient because all the dependent types will be rebuilt
2020       // from scratch, including building them in type units, discovering that
2021       // they depend on addresses, throwing them out and rebuilding them.
2022       CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
2023       return;
2024     }
2025 
2026     // If the type wasn't dependent on fission addresses, finish adding the type
2027     // and all its dependent types.
2028     for (auto &TU : TypeUnitsToAdd) {
2029       InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
2030       InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
2031     }
2032   }
2033   CU.addDIETypeSignature(RefDie, Signature);
2034 }
2035 
2036 // Accelerator table mutators - add each name along with its companion
2037 // DIE to the proper table while ensuring that the name that we're going
2038 // to reference is in the string table. We do this since the names we
2039 // add may not only be identical to the names in the DIE.
2040 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
2041   if (!useDwarfAccelTables())
2042     return;
2043   AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2044 }
2045 
2046 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2047   if (!useDwarfAccelTables())
2048     return;
2049   AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2050 }
2051 
2052 void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
2053   if (!useDwarfAccelTables())
2054     return;
2055   AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2056 }
2057 
2058 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2059   if (!useDwarfAccelTables())
2060     return;
2061   AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2062 }
2063 
2064 uint16_t DwarfDebug::getDwarfVersion() const {
2065   return Asm->OutStreamer->getContext().getDwarfVersion();
2066 }
2067