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