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