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