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