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