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