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