LLVM  5.0.0
DwarfDebug.cpp
Go to the documentation of this file.
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"
26 #include "llvm/CodeGen/DIE.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"
41 #include "llvm/Support/Debug.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"
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 
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 
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 
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 
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 
109 };
111  DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
112  cl::desc("Which DWARF linkage-name attributes to emit."),
114  "Default for platform"),
115  clEnumValN(AllLinkageNames, "All", "All"),
116  clEnumValN(AbstractLinkageNames, "Abstract",
117  "Abstract subprograms")),
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)
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 
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 
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 
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 
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.
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.
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.
282 
283 static bool isObjCClass(StringRef Name) {
284  return Name.startswith("+") || Name.startswith("-");
285 }
286 
288  if (!isObjCClass(Name))
289  return false;
290 
291  return Name.find(") ") != StringRef::npos;
292 }
293 
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 
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.
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.
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).
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 
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
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.
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 {
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.
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 
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()))
499 }
500 
501 /// Sort and unique GVEs by comparing their fragment offset.
504  std::sort(GVEs.begin(), GVEs.end(),
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(),
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.
530  return;
531 
532  const Module *M = MMI->getModule();
533 
534  unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
536  // Tell MMI whether we have debug info.
537  MMI->setDebugInfoAvailability(NumDebugCUs > 0);
538  SingleCU = NumDebugCUs == 1;
540  GVMap;
541  for (const GlobalVariable &Global : M->globals()) {
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()});
562  for (auto *GVE : CUNode->getGlobalVariables()) {
563  DIGlobalVariable *GV = GVE->getVariable();
564  if (Processed.insert(GV).second)
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.
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());
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(),
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.
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.
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 
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.
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.
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) {
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) {
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;
999  for (++Pred; Pred != MBB->rend(); ++Pred) {
1000  if (Pred->getFlag(MachineInstr::FrameSetup))
1001  break;
1002  auto PredDL = Pred->getDebugLoc();
1003  if (!PredDL || Pred->isMetaInstruction())
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  return false;
1009  auto *PredScope = LScopes.findLexicalScope(PredDL);
1010  if (!PredScope || LScope->dominates(PredScope))
1011  return false;
1012  }
1013 
1014  // If the range of the DBG_VALUE is open-ended, report success.
1015  if (!RangeEnd)
1016  return true;
1017 
1018  // Fail if there are instructions belonging to our scope in another block.
1019  const MachineInstr *LScopeEnd = LSRange.back().second;
1020  if (LScopeEnd->getParent() != MBB)
1021  return false;
1022 
1023  // Single, constant DBG_VALUEs in the prologue are promoted to be live
1024  // throughout the function. This is a hack, presumably for DWARF v2 and not
1025  // necessarily correct. It would be much better to use a dbg.declare instead
1026  // if we know the constant is live throughout the scope.
1027  if (DbgValue->getOperand(0).isImm() && MBB->pred_empty())
1028  return true;
1029 
1030  return false;
1031 }
1032 
1033 // Find variables for each lexical scope.
1034 void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
1035  const DISubprogram *SP,
1036  DenseSet<InlinedVariable> &Processed) {
1037  // Grab the variable info that was squirreled away in the MMI side-table.
1038  collectVariableInfoFromMFTable(TheCU, Processed);
1039 
1040  for (const auto &I : DbgValues) {
1041  InlinedVariable IV = I.first;
1042  if (Processed.count(IV))
1043  continue;
1044 
1045  // Instruction ranges, specifying where IV is accessible.
1046  const auto &Ranges = I.second;
1047  if (Ranges.empty())
1048  continue;
1049 
1050  LexicalScope *Scope = nullptr;
1051  if (const DILocation *IA = IV.second)
1052  Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
1053  else
1054  Scope = LScopes.findLexicalScope(IV.first->getScope());
1055  // If variable scope is not found then skip this variable.
1056  if (!Scope)
1057  continue;
1058 
1059  Processed.insert(IV);
1060  DbgVariable *RegVar = createConcreteVariable(TheCU, *Scope, IV);
1061 
1062  const MachineInstr *MInsn = Ranges.front().first;
1063  assert(MInsn->isDebugValue() && "History must begin with debug value");
1064 
1065  // Check if there is a single DBG_VALUE, valid throughout the var's scope.
1066  if (Ranges.size() == 1 &&
1067  validThroughout(LScopes, MInsn, Ranges.front().second)) {
1068  RegVar->initializeDbgValue(MInsn);
1069  continue;
1070  }
1071 
1072  // Handle multiple DBG_VALUE instructions describing one variable.
1073  DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
1074 
1075  // Build the location list for this variable.
1077  buildLocationList(Entries, Ranges);
1078 
1079  // If the variable has a DIBasicType, extract it. Basic types cannot have
1080  // unique identifiers, so don't bother resolving the type with the
1081  // identifier map.
1082  const DIBasicType *BT = dyn_cast<DIBasicType>(
1083  static_cast<const Metadata *>(IV.first->getType()));
1084 
1085  // Finalize the entry by lowering it into a DWARF bytestream.
1086  for (auto &Entry : Entries)
1087  Entry.finalize(*Asm, List, BT);
1088  }
1089 
1090  // Collect info for variables that were optimized out.
1091  for (const DILocalVariable *DV : SP->getVariables()) {
1092  if (Processed.insert(InlinedVariable(DV, nullptr)).second)
1093  if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
1094  createConcreteVariable(TheCU, *Scope, InlinedVariable(DV, nullptr));
1095  }
1096 }
1097 
1098 // Process beginning of an instruction.
1101  assert(CurMI);
1102 
1103  const auto *SP = MI->getParent()->getParent()->getFunction()->getSubprogram();
1104  if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1105  return;
1106 
1107  // Check if source location changes, but ignore DBG_VALUE and CFI locations.
1108  if (MI->isMetaInstruction())
1109  return;
1110  const DebugLoc &DL = MI->getDebugLoc();
1111  // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1112  // the last line number actually emitted, to see if it was line 0.
1113  unsigned LastAsmLine =
1114  Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
1115 
1116  if (DL == PrevInstLoc) {
1117  // If we have an ongoing unspecified location, nothing to do here.
1118  if (!DL)
1119  return;
1120  // We have an explicit location, same as the previous location.
1121  // But we might be coming back to it after a line 0 record.
1122  if (LastAsmLine == 0 && DL.getLine() != 0) {
1123  // Reinstate the source location but not marked as a statement.
1124  const MDNode *Scope = DL.getScope();
1125  recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0);
1126  }
1127  return;
1128  }
1129 
1130  if (!DL) {
1131  // We have an unspecified location, which might want to be line 0.
1132  // If we have already emitted a line-0 record, don't repeat it.
1133  if (LastAsmLine == 0)
1134  return;
1135  // If user said Don't Do That, don't do that.
1136  if (UnknownLocations == Disable)
1137  return;
1138  // See if we have a reason to emit a line-0 record now.
1139  // Reasons to emit a line-0 record include:
1140  // - User asked for it (UnknownLocations).
1141  // - Instruction has a label, so it's referenced from somewhere else,
1142  // possibly debug information; we want it to have a source location.
1143  // - Instruction is at the top of a block; we don't want to inherit the
1144  // location from the physically previous (maybe unrelated) block.
1145  if (UnknownLocations == Enable || PrevLabel ||
1146  (PrevInstBB && PrevInstBB != MI->getParent())) {
1147  // Preserve the file and column numbers, if we can, to save space in
1148  // the encoded line table.
1149  // Do not update PrevInstLoc, it remembers the last non-0 line.
1150  const MDNode *Scope = nullptr;
1151  unsigned Column = 0;
1152  if (PrevInstLoc) {
1153  Scope = PrevInstLoc.getScope();
1154  Column = PrevInstLoc.getCol();
1155  }
1156  recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
1157  }
1158  return;
1159  }
1160 
1161  // We have an explicit location, different from the previous location.
1162  // Don't repeat a line-0 record, but otherwise emit the new location.
1163  // (The new location might be an explicit line 0, which we do emit.)
1164  if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0)
1165  return;
1166  unsigned Flags = 0;
1167  if (DL == PrologEndLoc) {
1169  PrologEndLoc = DebugLoc();
1170  }
1171  // If the line changed, we call that a new statement; unless we went to
1172  // line 0 and came back, in which case it is not a new statement.
1173  unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
1174  if (DL.getLine() && DL.getLine() != OldLine)
1175  Flags |= DWARF2_FLAG_IS_STMT;
1176 
1177  const MDNode *Scope = DL.getScope();
1178  recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
1179 
1180  // If we're not at line 0, remember this location.
1181  if (DL.getLine())
1182  PrevInstLoc = DL;
1183 }
1184 
1185 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1186  // First known non-DBG_VALUE and non-frame setup location marks
1187  // the beginning of the function body.
1188  for (const auto &MBB : *MF)
1189  for (const auto &MI : MBB)
1190  if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1191  MI.getDebugLoc())
1192  return MI.getDebugLoc();
1193  return DebugLoc();
1194 }
1195 
1196 // Gather pre-function debug information. Assumes being called immediately
1197 // after the function entry point has been emitted.
1199  CurFn = MF;
1200 
1201  auto *SP = MF->getFunction()->getSubprogram();
1203  if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1204  return;
1205 
1206  DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
1207 
1208  // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1209  // belongs to so that we add to the correct per-cu line table in the
1210  // non-asm case.
1211  if (Asm->OutStreamer->hasRawTextSupport())
1212  // Use a single line table if we are generating assembly.
1213  Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1214  else
1215  Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID());
1216 
1217  // Record beginning of function.
1219  if (PrologEndLoc) {
1220  // We'd like to list the prologue as "not statements" but GDB behaves
1221  // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1222  auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
1223  recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
1224  }
1225 }
1226 
1228  // If we don't have a subprogram for this function then there will be a hole
1229  // in the range information. Keep note of this by setting the previously used
1230  // section to nullptr.
1231  PrevCU = nullptr;
1232  CurFn = nullptr;
1233 }
1234 
1235 // Gather and emit post-function debug information.
1237  const DISubprogram *SP = MF->getFunction()->getSubprogram();
1238 
1239  assert(CurFn == MF &&
1240  "endFunction should be called with the same function as beginFunction");
1241 
1242  // Set DwarfDwarfCompileUnitID in MCContext to default value.
1243  Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1244 
1246  assert(!FnScope || SP == FnScope->getScopeNode());
1247  DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit());
1248 
1249  DenseSet<InlinedVariable> ProcessedVars;
1250  collectVariableInfo(TheCU, SP, ProcessedVars);
1251 
1252  // Add the range of this function to the list of ranges for the CU.
1253  TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
1254 
1255  // Under -gmlt, skip building the subprogram if there are no inlined
1256  // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
1257  // is still needed as we need its source location.
1258  if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
1259  TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
1260  LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1261  assert(InfoHolder.getScopeVariables().empty());
1262  PrevLabel = nullptr;
1263  CurFn = nullptr;
1264  return;
1265  }
1266 
1267 #ifndef NDEBUG
1268  size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1269 #endif
1270  // Construct abstract scopes.
1271  for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
1272  auto *SP = cast<DISubprogram>(AScope->getScopeNode());
1273  // Collect info for variables that were optimized out.
1274  for (const DILocalVariable *DV : SP->getVariables()) {
1275  if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
1276  continue;
1277  ensureAbstractVariableIsCreated(TheCU, InlinedVariable(DV, nullptr),
1278  DV->getScope());
1279  assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1280  && "ensureAbstractVariableIsCreated inserted abstract scopes");
1281  }
1282  constructAbstractSubprogramScopeDIE(TheCU, AScope);
1283  }
1284 
1285  ProcessedSPNodes.insert(SP);
1286  TheCU.constructSubprogramScopeDIE(SP, FnScope);
1287  if (auto *SkelCU = TheCU.getSkeleton())
1288  if (!LScopes.getAbstractScopesList().empty() &&
1289  TheCU.getCUNode()->getSplitDebugInlining())
1290  SkelCU->constructSubprogramScopeDIE(SP, FnScope);
1291 
1292  // Clear debug info
1293  // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1294  // DbgVariables except those that are also in AbstractVariables (since they
1295  // can be used cross-function)
1296  InfoHolder.getScopeVariables().clear();
1297  PrevLabel = nullptr;
1298  CurFn = nullptr;
1299 }
1300 
1301 // Register a source line with debug info. Returns the unique label that was
1302 // emitted and which provides correspondence to the source line list.
1303 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1304  unsigned Flags) {
1305  StringRef Fn;
1306  StringRef Dir;
1307  unsigned Src = 1;
1308  unsigned Discriminator = 0;
1309  if (auto *Scope = cast_or_null<DIScope>(S)) {
1310  Fn = Scope->getFilename();
1311  Dir = Scope->getDirectory();
1312  if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1313  if (getDwarfVersion() >= 4)
1314  Discriminator = LBF->getDiscriminator();
1315 
1316  unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
1317  Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1318  .getOrCreateSourceID(Fn, Dir);
1319  }
1320  Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1321  Discriminator, Fn);
1322 }
1323 
1324 //===----------------------------------------------------------------------===//
1325 // Emit Methods
1326 //===----------------------------------------------------------------------===//
1327 
1328 // Emit the debug info section.
1329 void DwarfDebug::emitDebugInfo() {
1330  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1331  Holder.emitUnits(/* UseOffsets */ false);
1332 }
1333 
1334 // Emit the abbreviation section.
1335 void DwarfDebug::emitAbbreviations() {
1336  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1337 
1338  Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1339 }
1340 
1341 void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section,
1342  StringRef TableName) {
1343  Accel.FinalizeTable(Asm, TableName);
1344  Asm->OutStreamer->SwitchSection(Section);
1345 
1346  // Emit the full data.
1347  Accel.emit(Asm, Section->getBeginSymbol(), this);
1348 }
1349 
1350 // Emit visible names into a hashed accelerator table section.
1351 void DwarfDebug::emitAccelNames() {
1352  emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1353  "Names");
1354 }
1355 
1356 // Emit objective C classes and categories into a hashed accelerator table
1357 // section.
1358 void DwarfDebug::emitAccelObjC() {
1359  emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1360  "ObjC");
1361 }
1362 
1363 // Emit namespace dies into a hashed accelerator table.
1364 void DwarfDebug::emitAccelNamespaces() {
1365  emitAccel(AccelNamespace,
1366  Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
1367  "namespac");
1368 }
1369 
1370 // Emit type dies into a hashed accelerator table.
1371 void DwarfDebug::emitAccelTypes() {
1372  emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1373  "types");
1374 }
1375 
1376 // Public name handling.
1377 // The format for the various pubnames:
1378 //
1379 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1380 // for the DIE that is named.
1381 //
1382 // gnu pubnames - offset/index value/name tuples where the offset is the offset
1383 // into the CU and the index value is computed according to the type of value
1384 // for the DIE that is named.
1385 //
1386 // For type units the offset is the offset of the skeleton DIE. For split dwarf
1387 // it's the offset within the debug_info/debug_types dwo section, however, the
1388 // reference in the pubname header doesn't change.
1389 
1390 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
1392  const DIE *Die) {
1393  // Entities that ended up only in a Type Unit reference the CU instead (since
1394  // the pub entry has offsets within the CU there's no real offset that can be
1395  // provided anyway). As it happens all such entities (namespaces and types,
1396  // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
1397  // not to be true it would be necessary to persist this information from the
1398  // point at which the entry is added to the index data structure - since by
1399  // the time the index is built from that, the original type/namespace DIE in a
1400  // type unit has already been destroyed so it can't be queried for properties
1401  // like tag, etc.
1402  if (Die->getTag() == dwarf::DW_TAG_compile_unit)
1406 
1407  // We could have a specification DIE that has our most of our knowledge,
1408  // look for that now.
1409  if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1410  DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
1411  if (SpecDIE.findAttribute(dwarf::DW_AT_external))
1412  Linkage = dwarf::GIEL_EXTERNAL;
1413  } else if (Die->findAttribute(dwarf::DW_AT_external))
1414  Linkage = dwarf::GIEL_EXTERNAL;
1415 
1416  switch (Die->getTag()) {
1417  case dwarf::DW_TAG_class_type:
1418  case dwarf::DW_TAG_structure_type:
1419  case dwarf::DW_TAG_union_type:
1420  case dwarf::DW_TAG_enumeration_type:
1422  dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1425  case dwarf::DW_TAG_typedef:
1426  case dwarf::DW_TAG_base_type:
1427  case dwarf::DW_TAG_subrange_type:
1429  case dwarf::DW_TAG_namespace:
1430  return dwarf::GIEK_TYPE;
1431  case dwarf::DW_TAG_subprogram:
1433  case dwarf::DW_TAG_variable:
1435  case dwarf::DW_TAG_enumerator:
1438  default:
1439  return dwarf::GIEK_NONE;
1440  }
1441 }
1442 
1443 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
1444 ///
1445 void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
1446  MCSection *PSec = GnuStyle
1447  ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1448  : Asm->getObjFileLowering().getDwarfPubNamesSection();
1449 
1450  emitDebugPubSection(GnuStyle, PSec, "Names",
1452 }
1453 
1454 void DwarfDebug::emitDebugPubSection(
1455  bool GnuStyle, MCSection *PSec, StringRef Name,
1456  const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) {
1457  for (const auto &NU : CUMap) {
1458  DwarfCompileUnit *TheU = NU.second;
1459 
1460  const auto &Globals = (TheU->*Accessor)();
1461 
1463  continue;
1464 
1465  if (auto *Skeleton = TheU->getSkeleton())
1466  TheU = Skeleton;
1467 
1468  // Start the dwarf pubnames section.
1469  Asm->OutStreamer->SwitchSection(PSec);
1470 
1471  // Emit the header.
1472  Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1473  MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1474  MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1475  Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1476 
1477  Asm->OutStreamer->EmitLabel(BeginLabel);
1478 
1479  Asm->OutStreamer->AddComment("DWARF Version");
1480  Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
1481 
1482  Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
1483  Asm->emitDwarfSymbolReference(TheU->getLabelBegin());
1484 
1485  Asm->OutStreamer->AddComment("Compilation Unit Length");
1486  Asm->EmitInt32(TheU->getLength());
1487 
1488  // Emit the pubnames for this compilation unit.
1489  for (const auto &GI : Globals) {
1490  const char *Name = GI.getKeyData();
1491  const DIE *Entity = GI.second;
1492 
1493  Asm->OutStreamer->AddComment("DIE offset");
1494  Asm->EmitInt32(Entity->getOffset());
1495 
1496  if (GnuStyle) {
1498  Asm->OutStreamer->AddComment(
1499  Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1501  Asm->EmitInt8(Desc.toBits());
1502  }
1503 
1504  Asm->OutStreamer->AddComment("External Name");
1505  Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1506  }
1507 
1508  Asm->OutStreamer->AddComment("End Mark");
1509  Asm->EmitInt32(0);
1510  Asm->OutStreamer->EmitLabel(EndLabel);
1511  }
1512 }
1513 
1514 void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
1515  MCSection *PSec = GnuStyle
1516  ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1517  : Asm->getObjFileLowering().getDwarfPubTypesSection();
1518 
1519  emitDebugPubSection(GnuStyle, PSec, "Types",
1521 }
1522 
1523 /// Emit null-terminated strings into a debug str section.
1524 void DwarfDebug::emitDebugStr() {
1525  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1526  Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1527 }
1528 
1530  const DebugLocStream::Entry &Entry) {
1531  auto &&Comments = DebugLocs.getComments(Entry);
1532  auto Comment = Comments.begin();
1533  auto End = Comments.end();
1534  for (uint8_t Byte : DebugLocs.getBytes(Entry))
1535  Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
1536 }
1537 
1538 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
1539  ByteStreamer &Streamer,
1540  const DebugLocEntry::Value &Value,
1541  DwarfExpression &DwarfExpr) {
1542  auto *DIExpr = Value.getExpression();
1543  DIExpressionCursor ExprCursor(DIExpr);
1544  DwarfExpr.addFragmentOffset(DIExpr);
1545  // Regular entry.
1546  if (Value.isInt()) {
1547  if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1548  BT->getEncoding() == dwarf::DW_ATE_signed_char))
1549  DwarfExpr.addSignedConstant(Value.getInt());
1550  else
1551  DwarfExpr.addUnsignedConstant(Value.getInt());
1552  } else if (Value.isLocation()) {
1553  MachineLocation Location = Value.getLoc();
1554  if (Location.isIndirect())
1555  DwarfExpr.setMemoryLocationKind();
1557  if (Location.isIndirect() && Location.getOffset()) {
1558  Ops.push_back(dwarf::DW_OP_plus_uconst);
1559  Ops.push_back(Location.getOffset());
1560  }
1561  Ops.append(DIExpr->elements_begin(), DIExpr->elements_end());
1562  DIExpressionCursor Cursor(Ops);
1563  const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
1564  if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
1565  return;
1566  return DwarfExpr.addExpression(std::move(Cursor));
1567  } else if (Value.isConstantFP()) {
1568  APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
1569  DwarfExpr.addUnsignedConstant(RawBytes);
1570  }
1571  DwarfExpr.addExpression(std::move(ExprCursor));
1572 }
1573 
1576  const DIBasicType *BT) {
1577  DebugLocStream::EntryBuilder Entry(List, Begin, End);
1578  BufferByteStreamer Streamer = Entry.getStreamer();
1579  DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer);
1580  const DebugLocEntry::Value &Value = Values[0];
1581  if (Value.isFragment()) {
1582  // Emit all fragments that belong to the same variable and range.
1583  assert(all_of(Values, [](DebugLocEntry::Value P) {
1584  return P.isFragment();
1585  }) && "all values are expected to be fragments");
1586  assert(std::is_sorted(Values.begin(), Values.end()) &&
1587  "fragments are expected to be sorted");
1588 
1589  for (auto Fragment : Values)
1590  emitDebugLocValue(AP, BT, Streamer, Fragment, DwarfExpr);
1591 
1592  } else {
1593  assert(Values.size() == 1 && "only fragments may have >1 value");
1594  emitDebugLocValue(AP, BT, Streamer, Value, DwarfExpr);
1595  }
1596  DwarfExpr.finalize();
1597 }
1598 
1600  // Emit the size.
1601  Asm->OutStreamer->AddComment("Loc expr size");
1602  Asm->EmitInt16(DebugLocs.getBytes(Entry).size());
1603 
1604  // Emit the entry.
1605  APByteStreamer Streamer(*Asm);
1606  emitDebugLocEntry(Streamer, Entry);
1607 }
1608 
1609 // Emit locations into the debug loc section.
1610 void DwarfDebug::emitDebugLoc() {
1611  if (DebugLocs.getLists().empty())
1612  return;
1613 
1614  // Start the dwarf loc section.
1615  Asm->OutStreamer->SwitchSection(
1616  Asm->getObjFileLowering().getDwarfLocSection());
1617  unsigned char Size = Asm->MAI->getCodePointerSize();
1618  for (const auto &List : DebugLocs.getLists()) {
1619  Asm->OutStreamer->EmitLabel(List.Label);
1620  const DwarfCompileUnit *CU = List.CU;
1621  for (const auto &Entry : DebugLocs.getEntries(List)) {
1622  // Set up the range. This range is relative to the entry point of the
1623  // compile unit. This is a hard coded 0 for low_pc when we're emitting
1624  // ranges, or the DW_AT_low_pc on the compile unit otherwise.
1625  if (auto *Base = CU->getBaseAddress()) {
1626  Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1627  Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
1628  } else {
1629  Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1630  Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
1631  }
1632 
1634  }
1635  Asm->OutStreamer->EmitIntValue(0, Size);
1636  Asm->OutStreamer->EmitIntValue(0, Size);
1637  }
1638 }
1639 
1640 void DwarfDebug::emitDebugLocDWO() {
1641  Asm->OutStreamer->SwitchSection(
1642  Asm->getObjFileLowering().getDwarfLocDWOSection());
1643  for (const auto &List : DebugLocs.getLists()) {
1644  Asm->OutStreamer->EmitLabel(List.Label);
1645  for (const auto &Entry : DebugLocs.getEntries(List)) {
1646  // Just always use start_length for now - at least that's one address
1647  // rather than two. We could get fancier and try to, say, reuse an
1648  // address we know we've emitted elsewhere (the start of the function?
1649  // The start of the CU or CU subrange that encloses this range?)
1650  Asm->EmitInt8(dwarf::DW_LLE_startx_length);
1651  unsigned idx = AddrPool.getIndex(Entry.BeginSym);
1652  Asm->EmitULEB128(idx);
1653  Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
1654 
1656  }
1657  Asm->EmitInt8(dwarf::DW_LLE_end_of_list);
1658  }
1659 }
1660 
1661 struct ArangeSpan {
1662  const MCSymbol *Start, *End;
1663 };
1664 
1665 // Emit a debug aranges section, containing a CU lookup for any
1666 // address we can tie back to a CU.
1667 void DwarfDebug::emitDebugARanges() {
1668  // Provides a unique id per text section.
1670 
1671  // Filter labels by section.
1672  for (const SymbolCU &SCU : ArangeLabels) {
1673  if (SCU.Sym->isInSection()) {
1674  // Make a note of this symbol and it's section.
1675  MCSection *Section = &SCU.Sym->getSection();
1676  if (!Section->getKind().isMetadata())
1677  SectionMap[Section].push_back(SCU);
1678  } else {
1679  // Some symbols (e.g. common/bss on mach-o) can have no section but still
1680  // appear in the output. This sucks as we rely on sections to build
1681  // arange spans. We can do it without, but it's icky.
1682  SectionMap[nullptr].push_back(SCU);
1683  }
1684  }
1685 
1687 
1688  for (auto &I : SectionMap) {
1689  MCSection *Section = I.first;
1690  SmallVector<SymbolCU, 8> &List = I.second;
1691  if (List.size() < 1)
1692  continue;
1693 
1694  // If we have no section (e.g. common), just write out
1695  // individual spans for each symbol.
1696  if (!Section) {
1697  for (const SymbolCU &Cur : List) {
1698  ArangeSpan Span;
1699  Span.Start = Cur.Sym;
1700  Span.End = nullptr;
1701  assert(Cur.CU);
1702  Spans[Cur.CU].push_back(Span);
1703  }
1704  continue;
1705  }
1706 
1707  // Sort the symbols by offset within the section.
1708  std::sort(
1709  List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) {
1710  unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1711  unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
1712 
1713  // Symbols with no order assigned should be placed at the end.
1714  // (e.g. section end labels)
1715  if (IA == 0)
1716  return false;
1717  if (IB == 0)
1718  return true;
1719  return IA < IB;
1720  });
1721 
1722  // Insert a final terminator.
1723  List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
1724 
1725  // Build spans between each label.
1726  const MCSymbol *StartSym = List[0].Sym;
1727  for (size_t n = 1, e = List.size(); n < e; n++) {
1728  const SymbolCU &Prev = List[n - 1];
1729  const SymbolCU &Cur = List[n];
1730 
1731  // Try and build the longest span we can within the same CU.
1732  if (Cur.CU != Prev.CU) {
1733  ArangeSpan Span;
1734  Span.Start = StartSym;
1735  Span.End = Cur.Sym;
1736  assert(Prev.CU);
1737  Spans[Prev.CU].push_back(Span);
1738  StartSym = Cur.Sym;
1739  }
1740  }
1741  }
1742 
1743  // Start the dwarf aranges section.
1744  Asm->OutStreamer->SwitchSection(
1745  Asm->getObjFileLowering().getDwarfARangesSection());
1746 
1747  unsigned PtrSize = Asm->MAI->getCodePointerSize();
1748 
1749  // Build a list of CUs used.
1750  std::vector<DwarfCompileUnit *> CUs;
1751  for (const auto &it : Spans) {
1752  DwarfCompileUnit *CU = it.first;
1753  CUs.push_back(CU);
1754  }
1755 
1756  // Sort the CU list (again, to ensure consistent output order).
1757  std::sort(CUs.begin(), CUs.end(),
1758  [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
1759  return A->getUniqueID() < B->getUniqueID();
1760  });
1761 
1762  // Emit an arange table for each CU we used.
1763  for (DwarfCompileUnit *CU : CUs) {
1764  std::vector<ArangeSpan> &List = Spans[CU];
1765 
1766  // Describe the skeleton CU's offset and length, not the dwo file's.
1767  if (auto *Skel = CU->getSkeleton())
1768  CU = Skel;
1769 
1770  // Emit size of content not including length itself.
1771  unsigned ContentSize =
1772  sizeof(int16_t) + // DWARF ARange version number
1773  sizeof(int32_t) + // Offset of CU in the .debug_info section
1774  sizeof(int8_t) + // Pointer Size (in bytes)
1775  sizeof(int8_t); // Segment Size (in bytes)
1776 
1777  unsigned TupleSize = PtrSize * 2;
1778 
1779  // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
1780  unsigned Padding =
1781  OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
1782 
1783  ContentSize += Padding;
1784  ContentSize += (List.size() + 1) * TupleSize;
1785 
1786  // For each compile unit, write the list of spans it covers.
1787  Asm->OutStreamer->AddComment("Length of ARange Set");
1788  Asm->EmitInt32(ContentSize);
1789  Asm->OutStreamer->AddComment("DWARF Arange version number");
1790  Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
1791  Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
1792  Asm->emitDwarfSymbolReference(CU->getLabelBegin());
1793  Asm->OutStreamer->AddComment("Address Size (in bytes)");
1794  Asm->EmitInt8(PtrSize);
1795  Asm->OutStreamer->AddComment("Segment Size (in bytes)");
1796  Asm->EmitInt8(0);
1797 
1798  Asm->OutStreamer->emitFill(Padding, 0xff);
1799 
1800  for (const ArangeSpan &Span : List) {
1801  Asm->EmitLabelReference(Span.Start, PtrSize);
1802 
1803  // Calculate the size as being from the span start to it's end.
1804  if (Span.End) {
1805  Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
1806  } else {
1807  // For symbols without an end marker (e.g. common), we
1808  // write a single arange entry containing just that one symbol.
1809  uint64_t Size = SymSize[Span.Start];
1810  if (Size == 0)
1811  Size = 1;
1812 
1813  Asm->OutStreamer->EmitIntValue(Size, PtrSize);
1814  }
1815  }
1816 
1817  Asm->OutStreamer->AddComment("ARange terminator");
1818  Asm->OutStreamer->EmitIntValue(0, PtrSize);
1819  Asm->OutStreamer->EmitIntValue(0, PtrSize);
1820  }
1821 }
1822 
1823 /// Emit address ranges into a debug ranges section.
1824 void DwarfDebug::emitDebugRanges() {
1825  if (CUMap.empty())
1826  return;
1827 
1828  // Start the dwarf ranges section.
1829  Asm->OutStreamer->SwitchSection(
1830  Asm->getObjFileLowering().getDwarfRangesSection());
1831 
1832  // Size for our labels.
1833  unsigned char Size = Asm->MAI->getCodePointerSize();
1834 
1835  // Grab the specific ranges for the compile units in the module.
1836  for (const auto &I : CUMap) {
1837  DwarfCompileUnit *TheCU = I.second;
1838 
1839  if (auto *Skel = TheCU->getSkeleton())
1840  TheCU = Skel;
1841 
1842  // Iterate over the misc ranges for the compile units in the module.
1843  for (const RangeSpanList &List : TheCU->getRangeLists()) {
1844  // Emit our symbol so we can find the beginning of the range.
1845  Asm->OutStreamer->EmitLabel(List.getSym());
1846 
1847  for (const RangeSpan &Range : List.getRanges()) {
1848  const MCSymbol *Begin = Range.getStart();
1849  const MCSymbol *End = Range.getEnd();
1850  assert(Begin && "Range without a begin symbol?");
1851  assert(End && "Range without an end symbol?");
1852  if (auto *Base = TheCU->getBaseAddress()) {
1853  Asm->EmitLabelDifference(Begin, Base, Size);
1854  Asm->EmitLabelDifference(End, Base, Size);
1855  } else {
1856  Asm->OutStreamer->EmitSymbolValue(Begin, Size);
1857  Asm->OutStreamer->EmitSymbolValue(End, Size);
1858  }
1859  }
1860 
1861  // And terminate the list with two 0 values.
1862  Asm->OutStreamer->EmitIntValue(0, Size);
1863  Asm->OutStreamer->EmitIntValue(0, Size);
1864  }
1865  }
1866 }
1867 
1868 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
1869  for (auto *MN : Nodes) {
1870  if (auto *M = dyn_cast<DIMacro>(MN))
1871  emitMacro(*M);
1872  else if (auto *F = dyn_cast<DIMacroFile>(MN))
1873  emitMacroFile(*F, U);
1874  else
1875  llvm_unreachable("Unexpected DI type!");
1876  }
1877 }
1878 
1879 void DwarfDebug::emitMacro(DIMacro &M) {
1880  Asm->EmitULEB128(M.getMacinfoType());
1881  Asm->EmitULEB128(M.getLine());
1882  StringRef Name = M.getName();
1883  StringRef Value = M.getValue();
1884  Asm->OutStreamer->EmitBytes(Name);
1885  if (!Value.empty()) {
1886  // There should be one space between macro name and macro value.
1887  Asm->EmitInt8(' ');
1888  Asm->OutStreamer->EmitBytes(Value);
1889  }
1890  Asm->EmitInt8('\0');
1891 }
1892 
1893 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
1895  Asm->EmitULEB128(dwarf::DW_MACINFO_start_file);
1896  Asm->EmitULEB128(F.getLine());
1897  DIFile *File = F.getFile();
1898  unsigned FID =
1899  U.getOrCreateSourceID(File->getFilename(), File->getDirectory());
1900  Asm->EmitULEB128(FID);
1901  handleMacroNodes(F.getElements(), U);
1902  Asm->EmitULEB128(dwarf::DW_MACINFO_end_file);
1903 }
1904 
1905 /// Emit macros into a debug macinfo section.
1906 void DwarfDebug::emitDebugMacinfo() {
1907  if (CUMap.empty())
1908  return;
1909 
1910  // Start the dwarf macinfo section.
1911  Asm->OutStreamer->SwitchSection(
1912  Asm->getObjFileLowering().getDwarfMacinfoSection());
1913 
1914  for (const auto &P : CUMap) {
1915  auto &TheCU = *P.second;
1916  auto *SkCU = TheCU.getSkeleton();
1917  DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
1918  auto *CUNode = cast<DICompileUnit>(P.first);
1919  Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin());
1920  handleMacroNodes(CUNode->getMacros(), U);
1921  }
1922  Asm->OutStreamer->AddComment("End Of Macro List Mark");
1923  Asm->EmitInt8(0);
1924 }
1925 
1926 // DWARF5 Experimental Separate Dwarf emitters.
1927 
1928 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
1929  std::unique_ptr<DwarfCompileUnit> NewU) {
1930  NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
1931  Asm->TM.Options.MCOptions.SplitDwarfFile);
1932 
1933  if (!CompilationDir.empty())
1934  NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
1935 
1936  addGnuPubAttributes(*NewU, Die);
1937 
1938  SkeletonHolder.addUnit(std::move(NewU));
1939 }
1940 
1941 // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
1942 // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
1943 // DW_AT_addr_base, DW_AT_ranges_base.
1944 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
1945 
1946  auto OwnedUnit = make_unique<DwarfCompileUnit>(
1947  CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
1948  DwarfCompileUnit &NewCU = *OwnedUnit;
1949  NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
1950 
1951  NewCU.initStmtList();
1952 
1953  initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
1954 
1955  return NewCU;
1956 }
1957 
1958 // Emit the .debug_info.dwo section for separated dwarf. This contains the
1959 // compile units that would normally be in debug_info.
1960 void DwarfDebug::emitDebugInfoDWO() {
1961  assert(useSplitDwarf() && "No split dwarf debug info?");
1962  // Don't emit relocations into the dwo file.
1963  InfoHolder.emitUnits(/* UseOffsets */ true);
1964 }
1965 
1966 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
1967 // abbreviations for the .debug_info.dwo section.
1968 void DwarfDebug::emitDebugAbbrevDWO() {
1969  assert(useSplitDwarf() && "No split dwarf?");
1970  InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
1971 }
1972 
1973 void DwarfDebug::emitDebugLineDWO() {
1974  assert(useSplitDwarf() && "No split dwarf?");
1975  Asm->OutStreamer->SwitchSection(
1976  Asm->getObjFileLowering().getDwarfLineDWOSection());
1977  SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams());
1978 }
1979 
1980 // Emit the .debug_str.dwo section for separated dwarf. This contains the
1981 // string section and is identical in format to traditional .debug_str
1982 // sections.
1983 void DwarfDebug::emitDebugStrDWO() {
1984  assert(useSplitDwarf() && "No split dwarf?");
1985  MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
1986  InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
1987  OffSec);
1988 }
1989 
1990 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
1991  if (!useSplitDwarf())
1992  return nullptr;
1993  if (SingleCU)
1994  SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory());
1995  return &SplitTypeUnitFileTable;
1996 }
1997 
1999  MD5 Hash;
2000  Hash.update(Identifier);
2001  // ... take the least significant 8 bytes and return those. Our MD5
2002  // implementation always returns its results in little endian, so we actually
2003  // need the "high" word.
2004  MD5::MD5Result Result;
2005  Hash.final(Result);
2006  return Result.high();
2007 }
2008 
2010  StringRef Identifier, DIE &RefDie,
2011  const DICompositeType *CTy) {
2012  // Fast path if we're building some type units and one has already used the
2013  // address pool we know we're going to throw away all this work anyway, so
2014  // don't bother building dependent types.
2015  if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
2016  return;
2017 
2018  auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
2019  if (!Ins.second) {
2020  CU.addDIETypeSignature(RefDie, Ins.first->second);
2021  return;
2022  }
2023 
2024  bool TopLevelType = TypeUnitsUnderConstruction.empty();
2025  AddrPool.resetUsedFlag();
2026 
2027  auto OwnedUnit = make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
2028  getDwoLineTable(CU));
2029  DwarfTypeUnit &NewTU = *OwnedUnit;
2030  DIE &UnitDie = NewTU.getUnitDie();
2031  TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
2032 
2033  NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
2034  CU.getLanguage());
2035 
2036  uint64_t Signature = makeTypeSignature(Identifier);
2037  NewTU.setTypeSignature(Signature);
2038  Ins.first->second = Signature;
2039 
2040  if (useSplitDwarf())
2041  NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
2042  else {
2043  CU.applyStmtList(UnitDie);
2044  NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2045  }
2046 
2047  NewTU.setType(NewTU.createTypeDIE(CTy));
2048 
2049  if (TopLevelType) {
2050  auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2051  TypeUnitsUnderConstruction.clear();
2052 
2053  // Types referencing entries in the address table cannot be placed in type
2054  // units.
2055  if (AddrPool.hasBeenUsed()) {
2056 
2057  // Remove all the types built while building this type.
2058  // This is pessimistic as some of these types might not be dependent on
2059  // the type that used an address.
2060  for (const auto &TU : TypeUnitsToAdd)
2061  TypeSignatures.erase(TU.second);
2062 
2063  // Construct this type in the CU directly.
2064  // This is inefficient because all the dependent types will be rebuilt
2065  // from scratch, including building them in type units, discovering that
2066  // they depend on addresses, throwing them out and rebuilding them.
2067  CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
2068  return;
2069  }
2070 
2071  // If the type wasn't dependent on fission addresses, finish adding the type
2072  // and all its dependent types.
2073  for (auto &TU : TypeUnitsToAdd) {
2074  InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
2075  InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
2076  }
2077  }
2078  CU.addDIETypeSignature(RefDie, Signature);
2079 }
2080 
2081 // Accelerator table mutators - add each name along with its companion
2082 // DIE to the proper table while ensuring that the name that we're going
2083 // to reference is in the string table. We do this since the names we
2084 // add may not only be identical to the names in the DIE.
2085 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
2086  if (!useDwarfAccelTables())
2087  return;
2088  AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2089 }
2090 
2091 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2092  if (!useDwarfAccelTables())
2093  return;
2094  AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2095 }
2096 
2098  if (!useDwarfAccelTables())
2099  return;
2100  AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2101 }
2102 
2103 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2104  if (!useDwarfAccelTables())
2105  return;
2106  AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2107 }
2108 
2110  return Asm->OutStreamer->getContext().getDwarfVersion();
2111 }
MachineLocation getLoc() const
Definition: DebugLocEntry.h:74
void emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry)
Emit the location for a debug loc entry, including the size header.
ArrayRef< Entry > getEntries(const List &L) const
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
An object containing the capability of hashing and adding hash attributes onto a DIE.
Definition: DIEHash.h:28
Builder for DebugLocStream lists.
void addFlag(DIE &Die, dwarf::Attribute Attribute)
Add a flag that is true to the DIE.
Definition: DwarfUnit.cpp:205
const DILocalScope * getScopeNode() const
Definition: LexicalScopes.h:64
static cl::opt< bool > SplitDwarfCrossCuReferences("split-dwarf-cross-cu-references", cl::Hidden, cl::desc("Enable cross-cu references in DWO files"), cl::init(false))
static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, const DIE *Die)
computeIndexValue - Compute the gdb index value for the DIE and CU.
unsigned getLine() const
DIE * getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, ArrayRef< GlobalExpr > GlobalExprs)
Get or create global variable DIE.
static cl::opt< bool > GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden, cl::desc("Generate GNU-style pubnames and pubtypes"), cl::init(false))
static cl::opt< DefaultOnOff > DwarfAccelTables("dwarf-accel-tables", cl::Hidden, cl::desc("Output prototype dwarf accelerator tables."), cl::values(clEnumVal(Default,"Default for platform"), clEnumVal(Enable,"Enabled"), clEnumVal(Disable,"Disabled")), cl::init(Default))
static cl::opt< bool > DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, cl::desc("Disable debug info printing"))
This struct describes location entries emitted in the .debug_loc section.
Definition: DebugLocEntry.h:25
const DICompileUnit * getCUNode() const
Definition: DwarfUnit.h:113
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
MCSection * getDwarfAddrSection() const
#define DWARF2_FLAG_PROLOGUE_END
Definition: MCDwarf.h:71
MCTargetOptions MCOptions
Machine level options.
static SmallVectorImpl< DwarfCompileUnit::GlobalExpr > & sortGlobalExprs(SmallVectorImpl< DwarfCompileUnit::GlobalExpr > &GVEs)
Sort and unique GVEs by comparing their fragment offset.
Definition: DwarfDebug.cpp:503
MDNode * getScope() const
Definition: DebugLoc.cpp:36
bool useAppleExtensionAttributes() const
Definition: DwarfDebug.h:513
void addUnsignedConstant(uint64_t Value)
Emit an unsigned constant.
const ConstantFP * getFPImm() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
StringRef getFlags() const
void setMemoryLocationKind()
Lock this down to become a memory location description.
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:858
const ConstantFP * getConstantFP() const
Definition: DebugLocEntry.h:72
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
std::string SplitDwarfFile
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
DataLayout DL
DIMacroNodeArray getElements() const
static cl::opt< DefaultOnOff > UnknownLocations("use-unknown-locations", cl::Hidden, cl::desc("Make an absence of debug location information explicit."), cl::values(clEnumVal(Default,"At top of block or after label"), clEnumVal(Enable,"In all cases"), clEnumVal(Disable,"Never")), cl::init(Default))
ArrayRef< std::string > getComments(const Entry &E) const
bool useDwarfAccelTables() const
Returns whether or not to emit tables that dwarf consumers can use to accelerate lookup.
Definition: DwarfDebug.h:511
const MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:96
Base class containing the logic for constructing DWARF expressions independently of whether they are ...
SmallVectorImpl< InsnRange > & getRanges()
Definition: LexicalScopes.h:67
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null, or not exclusively derived from constant.
void emit(AsmPrinter *, const MCSymbol *, DwarfDebug *)
DenseMap< LexicalScope *, SmallVector< DbgVariable *, 8 > > & getScopeVariables()
Definition: DwarfFile.h:102
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:818
static const char *const DbgTimerName
Definition: DwarfDebug.cpp:122
bool getSplitDebugInlining() const
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:862
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
StringRef getName() const
Definition: DwarfDebug.h:122
debug_compile_units_iterator debug_compile_units_end() const
Definition: Module.h:747
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:45
DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Label, const MCSymbol *Sec)
Add a Dwarf section label attribute data and value.
Definition: DwarfUnit.cpp:1600
const DILocation * getInlinedAt() const
Definition: LexicalScopes.h:63
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we're processing.
bool isIndirect() const
MCSymbol * getMacroLabelBegin() const
DebuggerKind
Identify a debugger for "tuning" the debug info.
Definition: TargetOptions.h:92
DIE * getOrCreateTypeDIE(const MDNode *N)
Find existing DIE or create new DIE for the given type.
Definition: DwarfUnit.cpp:701
void addAccelNamespace(StringRef Name, const DIE &Die)
uint64_t getDWOId() const
void initializeDbgValue(const MachineInstr *DbgValue)
Initialize from a DBG_VALUE instruction.
Definition: DwarfDebug.h:98
static const char *const DbgTimerDescription
Definition: DwarfDebug.cpp:123
const DIE * getUnitDie() const
Climb up the parent chain to get the compile unit or type unit DIE that this DIE belongs to...
Definition: DIE.cpp:202
static cl::opt< DefaultOnOff > DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden, cl::desc("Generate DWARF pubnames and pubtypes sections"), cl::values(clEnumVal(Default,"Default for platform"), clEnumVal(Enable,"Enabled"), clEnumVal(Disable,"Disabled")), cl::init(Default))
const Triple & getTargetTriple() const
static cl::opt< bool > GenerateARangeSection("generate-arange-section", cl::Hidden, cl::desc("Generate dwarf aranges"), cl::init(false))
ArrayRef< FrameIndexExpr > getFrameIndexExprs() const
Get the FI entries, sorted by fragment offset.
Definition: DwarfDebug.cpp:197
const StringMap< const DIE * > & getGlobalTypes() const
virtual void EmitULEB128(uint64_t DWord, const Twine &Comment="")=0
const DIExpression * getExpression() const
Definition: DebugLocEntry.h:76
DebugLoc PrevInstLoc
Previous instruction's location information.
ArrayRef< LexicalScope * > getAbstractScopesList() const
getAbstractScopesList - Return a reference to list of abstract scopes.
#define DWARF2_FLAG_IS_STMT
Definition: MCDwarf.h:69
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
LexicalScope * getOrCreateAbstractScope(const DILocalScope *Scope)
getOrCreateAbstractScope - Find or create an abstract lexical scope.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
DbgValueHistoryMap DbgValues
History of DBG_VALUE and clobber instructions for each user variable.
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
This class is basically a combination of TimeRegion and Timer.
Definition: Timer.h:160
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void attachRangesOrLowHighPC(DIE &D, SmallVector< RangeSpan, 2 > Ranges)
const Module * getModule() const
APInt bitcastToAPInt() const
Definition: APFloat.h:1094
DwarfCompileUnit * getSkeleton() const
bool isLexicalScopeDIENull(LexicalScope *Scope)
A helper function to check whether the DIE for a given Scope is going to be null. ...
Definition: DwarfDebug.cpp:339
void finishVariableDefinition(const DbgVariable &Var)
StringRef getFilename() const
bool hasBeenUsed() const
Definition: AddressPool.h:47
void constructContainingTypeDIEs()
Construct DIEs for types that contain vtables.
Definition: DwarfUnit.cpp:1377
uint16_t getDwarfVersion() const
unsigned getMacinfoType() const
bool hasDebugInfo() const
Returns true if valid debug info is present.
virtual void EmitInt8(uint8_t Byte, const Twine &Comment="")=0
Holds a DIExpression and keeps track of how many operands have been consumed so far.
uint64_t computeCUSignature(StringRef DWOName, const DIE &Die)
Computes the CU signature.
Definition: DIEHash.cpp:383
#define clEnumVal(ENUMVAL, DESC)
Definition: CommandLine.h:594
DwarfExpression implementation for .debug_loc entries.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
LexicalScope * getCurrentFunctionScope() const
getCurrentFunctionScope - Return lexical scope for the current function.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
iterator_range< debug_compile_units_iterator > debug_compile_units() const
Return an iterator for all DICompileUnits listed in this Module's llvm.dbg.cu named metadata node and...
Definition: Module.h:755
Subprogram description.
Decsribes an entry of the various gnu_pub* debug sections.
Definition: Dwarf.h:464
#define F(x, y, z)
Definition: MD5.cpp:55
~DwarfDebug() override
Definition: DwarfDebug.cpp:281
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
static bool validThroughout(LexicalScopes &LScopes, const MachineInstr *DbgValue, const MachineInstr *RangeEnd)
Determine whether a singular DBG_VALUE is valid for the entirety of its enclosing lexical scope...
Definition: DwarfDebug.cpp:979
This class is used to track local variable information.
Definition: DwarfDebug.h:64
#define T
void setBaseAddress(const MCSymbol *Base)
void FinalizeTable(AsmPrinter *, StringRef)
bool isMetaInstruction() const
Return true if this instruction doesn't produce any output in the form of executable instructions...
Definition: MachineInstr.h:849
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void AddName(DwarfStringPoolEntryRef Name, const DIE *Die, char Flags=0)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:710
bool MergeValues(const DebugLocEntry &Next)
If this and Next are describing different pieces of the same variable, merge them by appending Next's...
Definition: DwarfDebug.cpp:832
static Optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
LexicalScope * findLexicalScope(const DILocation *DL)
findLexicalScope - Find lexical scope, either regular or inlined, for the given DebugLoc.
MCSection * getDwarfMacinfoSection() const
MCSymbol * getLabelBegin() const
const SmallVectorImpl< std::unique_ptr< DwarfCompileUnit > > & getUnits()
Definition: DwarfFile.h:66
bool shareAcrossDWOCUs() const
Definition: DwarfDebug.cpp:363
int64_t getImm() const
unsigned getLine() const
element_iterator elements_begin() const
Debug location.
DIE & addChild(DIE *Child)
Add a child to the DIE.
Definition: DIE.h:769
dwarf::Tag getTag() const
Definition: DIE.h:698
bool isFragment() const
Return whether this is a piece of an aggregate variable.
const char * Name
DIScope * getScope() const
ArrayRef< char > getBytes(const Entry &E) const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
bool includeMinimalInlineScopes() const
LexicalScope * findAbstractScope(const DILocalScope *N)
findAbstractScope - Find an abstract scope or return null.
static void forBothCUs(DwarfCompileUnit &CU, Func F)
Definition: DwarfDebug.cpp:356
bool isDebugValue() const
Definition: MachineInstr.h:785
unsigned getRuntimeVersion() const
void addUnit(std::unique_ptr< DwarfCompileUnit > U)
Add a unit to the list of CUs.
Definition: DwarfFile.cpp:24
ValuesClass values(OptsTy...Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:621
unsigned getLine() const
Definition: DebugLoc.cpp:26
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support...
Definition: DwarfDebug.h:519
DebuggerKind DebuggerTuning
Which debugger to tune for.
const MCSymbol * Start
bool isMetadata() const
Definition: SectionKind.h:117
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:68
#define P(N)
DwarfStringPool & getStringPool()
Returns the string pool.
Definition: DwarfFile.h:97
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
element_iterator elements_end() const
StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind)
Definition: Dwarf.cpp:506
void endModule() override
Emit all Dwarf sections that should come after the content.
Definition: DwarfDebug.cpp:692
const DIType * getType() const
Definition: DwarfDebug.cpp:152
DwarfCompileUnit * CU
Definition: DwarfDebug.h:199
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:298
StringRef getName() const
static bool fragmentsOverlap(const DIExpression *P1, const DIExpression *P2)
Determine whether two variable fragments overlap.
const MachineBasicBlock * PrevInstBB
LinkageNameOption
Definition: DwarfDebug.cpp:105
static void getObjCClassCategory(StringRef In, StringRef &Class, StringRef &Category)
Definition: DwarfDebug.cpp:294
DbgVariable * getExistingAbstractVariable(InlinedVariable IV, const DILocalVariable *&Cleansed)
const SmallVectorImpl< RangeSpanList > & getRangeLists() const
getRangeLists - Get the vector of range lists.
void emit(AsmPrinter &Asm, MCSection *AddrSection)
Definition: AddressPool.cpp:25
static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI)
Definition: DwarfDebug.cpp:804
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
DIEValue findAttribute(dwarf::Attribute Attribute) const
Find a value in the DIE with the attribute given.
Definition: DIE.cpp:220
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
A structured debug information entry.
Definition: DIE.h:662
AsmPrinter * Asm
Target of debug info emission.
const MCSymbol * getBaseAddress() const
#define A
Definition: LargeTest.cpp:12
DIE & getUnitDie()
Definition: DIE.h:834
unsigned getSourceLanguage() const
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:80
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:76
void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier, DIE &Die, const DICompositeType *CTy)
Add a DIE to the set of types that we're going to pull into type units.
void emitUnits(bool UseOffsets)
Emit all of the units to the section listed with the given abbreviation section.
Definition: DwarfFile.cpp:30
cl::opt< int > DwarfVersion("dwarf-version", cl::desc("Dwarf version"), cl::init(0))
void setCompilationDir(StringRef CompilationDir)
Definition: MCDwarf.h:222
static const unsigned End
void addAccelType(StringRef Name, const DIE &Die, char Flags)
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
DwarfDebug(AsmPrinter *A, Module *M)
Definition: DwarfDebug.cpp:217
void constructAbstractSubprogramScopeDIE(LexicalScope *Scope)
unsigned getEncoding() const
StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage)
Definition: Dwarf.cpp:529
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
bool isAbstractScope() const
Definition: LexicalScopes.h:65
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
GDBIndexEntryLinkage Linkage
Definition: Dwarf.h:466
bool hasDwarfPubSections(bool includeMinimalInlineScopes) const
Definition: DwarfDebug.cpp:392
A pair of GlobalVariable and DIExpression.
DIE * constructImportedEntityDIE(const DIImportedEntity *Module)
Construct import_module DIE.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
Helper used to pair up a symbol and its DWARF compile unit.
Definition: DwarfDebug.h:196
iterator erase(const_iterator CI)
Definition: SmallVector.h:446
MCSymbol * getLabelAfterInsn(const MachineInstr *MI)
Return Label immediately following the instruction.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
unsigned getDwarfVersion() const
Returns the Dwarf Version by checking module flags.
Definition: Module.cpp:453
ArrayRef< List > getLists() const
StringRef OperationEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:138
static const char *const DWARFGroupDescription
Definition: DwarfDebug.cpp:121
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
DITypeRef getType() const
An imported module (C++ using directive or similar).
unsigned first
unsigned getCol() const
Definition: DebugLoc.cpp:31
StringRef getDirectory() const
void addExpression(DIExpressionCursor &&Expr, unsigned FragmentOffsetInBits=0)
Emit all remaining operations in the DIExpressionCursor.
SmallVector< RangeSpan, 2 > takeRanges()
unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override
Look up the source ID with the given directory and source file names.
static DebugLoc findPrologueEndLoc(const MachineFunction *MF)
LexicalScope * findInlinedScope(const DILocalScope *N, const DILocation *IA)
findInlinedScope - Find an inlined scope for the given scope/inlined-at.
unsigned getTag() const
static bool hasObjCCategory(StringRef Name)
Definition: DwarfDebug.cpp:287
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Base class for types.
#define E
Definition: LargeTest.cpp:27
void setDebugInfoAvailability(bool avail)
#define B
Definition: LargeTest.cpp:24
SectionKind getKind() const
Definition: MCSection.h:102
uint16_t getLanguage() const
Definition: DwarfUnit.h:112
void createAbstractVariable(const DILocalVariable *DV, LexicalScope *Scope)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment="")=0
Module.h This file contains the declarations for the Module class.
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:46
void beginModule()
Emit all Dwarf sections that should come prior to the content.
Definition: DwarfDebug.cpp:526
const SmallVectorImpl< RangeSpan > & getRanges() const
getRanges - Get the list of ranges for this unit.
static ManagedStatic< CodeViewErrorCategory > Category
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:700
StringRef getName() const
const size_t N
MCSymbol * getBeginSymbol()
Definition: MCSection.h:106
void addAccelName(StringRef Name, const DIE &Die)
const StringMap< const DIE * > & getGlobalNames() const
void emitDebugLocEntry(ByteStreamer &Streamer, const DebugLocStream::Entry &Entry)
Emit an entry for the debug loc section.
DWARF expression.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
const ConstantInt * getCImm() const
bool isBlockByrefStruct() const
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
Class for arbitrary precision integers.
Definition: APInt.h:69
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1498
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:257
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:74
DebugLoc PrologEndLoc
This location indicates end of function prologue and beginning of function body.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:395
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:81
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:596
llvm_unreachable("Unknown RegBank!")
A single location or constant.
Definition: DebugLocEntry.h:32
void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List, const DIBasicType *BT)
Lower this entry into a DWARF expression.
DIE * getOrCreateContextDIE(const DIScope *Context)
Get context owner's DIE.
Definition: DwarfUnit.cpp:671
void addImportedEntity(const DIImportedEntity *IE)
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
Representation of each machine instruction.
Definition: MachineInstr.h:59
void Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params) const
Definition: MCDwarf.cpp:216
static StringRef getObjCMethodName(StringRef In)
Definition: DwarfDebug.cpp:306
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
void setSection(MCSection *Section)
Set the section that this DIEUnit will be emitted into.
Definition: DIE.h:815
EntryRef getEntry(AsmPrinter &Asm, StringRef Str)
Get a reference to an entry in the string pool.
static const size_t npos
Definition: StringRef.h:51
LexicalScopes - This class provides interface to collect and use lexical scoping information from mac...
TargetOptions Options
Definition: TargetMachine.h:95
uint16_t getDwarfVersion() const
Returns the Dwarf Version.
void endFunctionImpl(const MachineFunction *MF) override
Gather and emit post-function debug information.
const NodeList & List
Definition: RDFGraph.cpp:205
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
Definition: MD5.h:41
bool isPS4CPU() const
Tests whether the target is the PS4 CPU.
Definition: Triple.h:597
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
Base class for debug information backends.
static uint64_t makeTypeSignature(StringRef Identifier)
Perform an MD5 checksum of Identifier and return the lower 64 bits.
LLVMContext & getContext() const
Definition: Metadata.h:922
void skippedNonDebugFunction() override
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:77
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
void addDIETypeSignature(DIE &Die, uint64_t Signature)
Add a type's DW_AT_signature and set the declaration flag.
Definition: DwarfUnit.cpp:292
const APFloat & getValueAPF() const
Definition: Constants.h:294
void set(unsigned R)
Make this location a direct register location.
void addSubprogramNames(const DISubprogram *SP, DIE &Die)
Definition: DwarfDebug.cpp:314
std::vector< uint8_t > Unit
Definition: FuzzerDefs.h:105
const MCSymbol * End
void addFragmentOffset(const DIExpression *Expr)
If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to the fragment described by Ex...
unsigned getReg() const
getReg - Returns the register number.
MCSymbol * getLabelBeforeInsn(const MachineInstr *MI)
Return Label preceding the instruction.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char *const DWARFGroupName
Definition: DwarfDebug.cpp:120
bool isBlockByrefVariable() const
Definition: DwarfDebug.cpp:147
static int fragmentCmp(const DIExpression *P1, const DIExpression *P2)
Determine the relative position of the fragments described by P1 and P2.
LLVM Value Representation.
Definition: Value.h:73
MCSection * getDwarfRangesSection() const
static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, ByteStreamer &Streamer, const DebugLocEntry::Value &Value, DwarfExpression &DwarfExpr)
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: MathExtras.h:714
static const DwarfAccelTable::Atom TypeAtoms[]
Definition: DwarfDebug.cpp:212
GDBIndexEntryLinkage
Definition: Dwarf.h:360
#define DEBUG(X)
Definition: Debug.h:118
void addSignedConstant(int64_t Value)
Emit a signed constant.
void addUInt(DIEValueList &Die, dwarf::Attribute Attribute, Optional< dwarf::Form > Form, uint64_t Integer)
Add an unsigned integer attribute data and value.
Definition: DwarfUnit.cpp:214
std::string Hash(const Unit &U)
Definition: FuzzerSHA1.cpp:216
MachineModuleInfo * MMI
Collected machine module information.
void resetUsedFlag()
Definition: AddressPool.h:49
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
DefaultOnOff
Definition: DwarfDebug.cpp:79
iterator_range< global_iterator > globals()
Definition: Module.h:561
IRTranslator LLVM IR MI
void addAccelObjC(StringRef Name, const DIE &Die)
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Definition: Parallel.h:201
unsigned getUniqueID() const
bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
Definition: IRReader.cpp:26
static bool isObjCClass(StringRef Name)
Definition: DwarfDebug.cpp:283
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
uint64_t high() const
Definition: MD5.h:72
unsigned getReg() const
Builder for DebugLocStream entries.
void addValues(ArrayRef< DebugLocEntry::Value > Vals)
debug_compile_units_iterator debug_compile_units_begin() const
Definition: Module.h:742
int64_t getInt() const
Definition: DebugLocEntry.h:71
StringRef getProducer() const
void finishSubprogramDefinition(const DISubprogram *SP)
Root of the metadata hierarchy.
Definition: Metadata.h:58
static cl::opt< LinkageNameOption > DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, cl::desc("Which DWARF linkage-name attributes to emit."), cl::values(clEnumValN(DefaultLinkageNames,"Default","Default for platform"), clEnumValN(AllLinkageNames,"All","All"), clEnumValN(AbstractLinkageNames,"Abstract","Abstract subprograms")), cl::init(DefaultLinkageNames))
bool addMachineRegExpression(const TargetRegisterInfo &TRI, DIExpressionCursor &Expr, unsigned MachineReg, unsigned FragmentOffsetInBits=0)
Emit a machine register location.
#define D
Definition: LargeTest.cpp:26
DIFile * getFile() const
bool empty()
empty - Return true if there is any lexical scope information available.
StringRef getValue() const
Basic type, like 'int' or 'float'.
const MCSymbol * Sym
Definition: DwarfDebug.h:198
unsigned getIndex(const MCSymbol *Sym, bool TLS=false)
Returns the index into the address pool with the given label/symbol.
Definition: AddressPool.cpp:17
StringRef getSplitDebugFilename() const