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