LLVM  7.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 "DebugLocStream.h"
19 #include "DwarfCompileUnit.h"
20 #include "DwarfExpression.h"
21 #include "DwarfFile.h"
22 #include "DwarfUnit.h"
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/MapVector.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/ADT/Twine.h"
35 #include "llvm/CodeGen/DIE.h"
44 #include "llvm/IR/Constants.h"
46 #include "llvm/IR/DebugLoc.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/GlobalVariable.h"
49 #include "llvm/IR/Module.h"
50 #include "llvm/MC/MCAsmInfo.h"
51 #include "llvm/MC/MCContext.h"
52 #include "llvm/MC/MCDwarf.h"
53 #include "llvm/MC/MCSection.h"
54 #include "llvm/MC/MCStreamer.h"
55 #include "llvm/MC/MCSymbol.h"
58 #include "llvm/MC/SectionKind.h"
59 #include "llvm/Pass.h"
60 #include "llvm/Support/Casting.h"
62 #include "llvm/Support/Debug.h"
64 #include "llvm/Support/MD5.h"
66 #include "llvm/Support/Timer.h"
71 #include <algorithm>
72 #include <cassert>
73 #include <cstddef>
74 #include <cstdint>
75 #include <iterator>
76 #include <string>
77 #include <utility>
78 #include <vector>
79 
80 using namespace llvm;
81 
82 #define DEBUG_TYPE "dwarfdebug"
83 
84 static cl::opt<bool>
85 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
86  cl::desc("Disable debug info printing"));
87 
89  "use-dwarf-ranges-base-address-specifier", cl::Hidden,
90  cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
91 
92 static cl::opt<bool> GenerateARangeSection("generate-arange-section",
93  cl::Hidden,
94  cl::desc("Generate dwarf aranges"),
95  cl::init(false));
96 
97 static cl::opt<bool>
98  GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
99  cl::desc("Generate DWARF4 type units."),
100  cl::init(false));
101 
103  "split-dwarf-cross-cu-references", cl::Hidden,
104  cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
105 
106 enum DefaultOnOff { Default, Enable, Disable };
107 
109  "use-unknown-locations", cl::Hidden,
110  cl::desc("Make an absence of debug location information explicit."),
111  cl::values(clEnumVal(Default, "At top of block or after label"),
112  clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
113  cl::init(Default));
114 
116  "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."),
118  "Default for platform"),
119  clEnumValN(AccelTableKind::None, "Disable", "Disabled."),
120  clEnumValN(AccelTableKind::Apple, "Apple", "Apple"),
121  clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")),
123 
125 DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden,
126  cl::desc("Use inlined strings rather than string section."),
127  cl::values(clEnumVal(Default, "Default for platform"),
128  clEnumVal(Enable, "Enabled"),
129  clEnumVal(Disable, "Disabled")),
130  cl::init(Default));
131 
132 static cl::opt<bool>
133  NoDwarfPubSections("no-dwarf-pub-sections", cl::Hidden,
134  cl::desc("Disable emission of DWARF pub sections."),
135  cl::init(false));
136 
137 static cl::opt<bool>
138  NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden,
139  cl::desc("Disable emission .debug_ranges section."),
140  cl::init(false));
141 
143  "dwarf-sections-as-references", cl::Hidden,
144  cl::desc("Use sections+offset as references rather than labels."),
145  cl::values(clEnumVal(Default, "Default for platform"),
146  clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
147  cl::init(Default));
148 
153 };
154 
156  DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
157  cl::desc("Which DWARF linkage-name attributes to emit."),
159  "Default for platform"),
160  clEnumValN(AllLinkageNames, "All", "All"),
161  clEnumValN(AbstractLinkageNames, "Abstract",
162  "Abstract subprograms")),
164 
165 static const char *const DWARFGroupName = "dwarf";
166 static const char *const DWARFGroupDescription = "DWARF Emission";
167 static const char *const DbgTimerName = "writer";
168 static const char *const DbgTimerDescription = "DWARF Debug Writer";
169 
170 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
171  BS.EmitInt8(
172  Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
174 }
175 
176 void DebugLocDwarfExpression::emitSigned(int64_t Value) {
177  BS.EmitSLEB128(Value, Twine(Value));
178 }
179 
180 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
181  BS.EmitULEB128(Value, Twine(Value));
182 }
183 
184 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
185  unsigned MachineReg) {
186  // This information is not available while emitting .debug_loc entries.
187  return false;
188 }
189 
191  assert(Var && "Invalid complex DbgVariable!");
192  return Var->getType().resolve()->isBlockByrefStruct();
193 }
194 
195 const DIType *DbgVariable::getType() const {
196  DIType *Ty = Var->getType().resolve();
197  // FIXME: isBlockByrefVariable should be reformulated in terms of complex
198  // addresses instead.
199  if (Ty->isBlockByrefStruct()) {
200  /* Byref variables, in Blocks, are declared by the programmer as
201  "SomeType VarName;", but the compiler creates a
202  __Block_byref_x_VarName struct, and gives the variable VarName
203  either the struct, or a pointer to the struct, as its type. This
204  is necessary for various behind-the-scenes things the compiler
205  needs to do with by-reference variables in blocks.
206 
207  However, as far as the original *programmer* is concerned, the
208  variable should still have type 'SomeType', as originally declared.
209 
210  The following function dives into the __Block_byref_x_VarName
211  struct to find the original type of the variable. This will be
212  passed back to the code generating the type for the Debug
213  Information Entry for the variable 'VarName'. 'VarName' will then
214  have the original type 'SomeType' in its debug information.
215 
216  The original type 'SomeType' will be the type of the field named
217  'VarName' inside the __Block_byref_x_VarName struct.
218 
219  NOTE: In order for this to not completely fail on the debugger
220  side, the Debug Information Entry for the variable VarName needs to
221  have a DW_AT_location that tells the debugger how to unwind through
222  the pointers and __Block_byref_x_VarName struct to find the actual
223  value of the variable. The function addBlockByrefType does this. */
224  DIType *subType = Ty;
225  uint16_t tag = Ty->getTag();
226 
227  if (tag == dwarf::DW_TAG_pointer_type)
228  subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
229 
230  auto Elements = cast<DICompositeType>(subType)->getElements();
231  for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
232  auto *DT = cast<DIDerivedType>(Elements[i]);
233  if (getName() == DT->getName())
234  return resolve(DT->getBaseType());
235  }
236  }
237  return Ty;
238 }
239 
241  if (FrameIndexExprs.size() == 1)
242  return FrameIndexExprs;
243 
244  assert(llvm::all_of(FrameIndexExprs,
245  [](const FrameIndexExpr &A) {
246  return A.Expr->isFragment();
247  }) &&
248  "multiple FI expressions without DW_OP_LLVM_fragment");
249  llvm::sort(FrameIndexExprs.begin(), FrameIndexExprs.end(),
250  [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
251  return A.Expr->getFragmentInfo()->OffsetInBits <
252  B.Expr->getFragmentInfo()->OffsetInBits;
253  });
254 
255  return FrameIndexExprs;
256 }
257 
259  assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry");
260  assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry");
261  assert(V.Var == Var && "conflicting variable");
262  assert(V.IA == IA && "conflicting inlined-at location");
263 
264  assert(!FrameIndexExprs.empty() && "Expected an MMI entry");
265  assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry");
266 
267  // FIXME: This logic should not be necessary anymore, as we now have proper
268  // deduplication. However, without it, we currently run into the assertion
269  // below, which means that we are likely dealing with broken input, i.e. two
270  // non-fragment entries for the same variable at different frame indices.
271  if (FrameIndexExprs.size()) {
272  auto *Expr = FrameIndexExprs.back().Expr;
273  if (!Expr || !Expr->isFragment())
274  return;
275  }
276 
277  for (const auto &FIE : V.FrameIndexExprs)
278  // Ignore duplicate entries.
279  if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) {
280  return FIE.FI == Other.FI && FIE.Expr == Other.Expr;
281  }))
282  FrameIndexExprs.push_back(FIE);
283 
284  assert((FrameIndexExprs.size() == 1 ||
285  llvm::all_of(FrameIndexExprs,
286  [](FrameIndexExpr &FIE) {
287  return FIE.Expr && FIE.Expr->isFragment();
288  })) &&
289  "conflicting locations for variable");
290 }
291 
293  bool GenerateTypeUnits,
294  DebuggerKind Tuning,
295  const Triple &TT) {
296  // Honor an explicit request.
297  if (AccelTables != AccelTableKind::Default)
298  return AccelTables;
299 
300  // Accelerator tables with type units are currently not supported.
301  if (GenerateTypeUnits)
302  return AccelTableKind::None;
303 
304  // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5
305  // always implies debug_names. For lower standard versions we use apple
306  // accelerator tables on apple platforms and debug_names elsewhere.
307  if (DwarfVersion >= 5)
308  return AccelTableKind::Dwarf;
309  if (Tuning == DebuggerKind::LLDB)
312  return AccelTableKind::None;
313 }
314 
316  : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
317  InfoHolder(A, "info_string", DIEValueAllocator),
318  SkeletonHolder(A, "skel_string", DIEValueAllocator),
319  IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
320  const Triple &TT = Asm->TM.getTargetTriple();
321 
322  // Make sure we know our "debugger tuning." The target option takes
323  // precedence; fall back to triple-based defaults.
325  DebuggerTuning = Asm->TM.Options.DebuggerTuning;
326  else if (IsDarwin)
327  DebuggerTuning = DebuggerKind::LLDB;
328  else if (TT.isPS4CPU())
329  DebuggerTuning = DebuggerKind::SCE;
330  else
331  DebuggerTuning = DebuggerKind::GDB;
332 
333  if (DwarfInlinedStrings == Default)
334  UseInlineStrings = TT.isNVPTX();
335  else
336  UseInlineStrings = DwarfInlinedStrings == Enable;
337 
338  UseLocSection = !TT.isNVPTX();
339 
340  HasAppleExtensionAttributes = tuneForLLDB();
341 
342  // Handle split DWARF.
343  HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty();
344 
345  // SCE defaults to linkage names only for abstract subprograms.
347  UseAllLinkageNames = !tuneForSCE();
348  else
349  UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
350 
351  unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
352  unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
353  : MMI->getModule()->getDwarfVersion();
354  // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2.
355  DwarfVersion =
356  TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION);
357 
358  UsePubSections = !NoDwarfPubSections && !TT.isNVPTX();
359  UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX();
360 
361  // Use sections as references. Force for NVPTX.
362  if (DwarfSectionsAsReferences == Default)
363  UseSectionsAsReferences = TT.isNVPTX();
364  else
365  UseSectionsAsReferences = DwarfSectionsAsReferences == Enable;
366 
367  // Don't generate type units for unsupported object file formats.
368  GenerateTypeUnits =
370 
371  TheAccelTableKind = computeAccelTableKind(
372  DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple());
373 
374  // Work around a GDB bug. GDB doesn't support the standard opcode;
375  // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
376  // is defined as of DWARF 3.
377  // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
378  // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
379  UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
380 
381  // GDB does not fully support the DWARF 4 representation for bitfields.
382  UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB();
383 
384  // The DWARF v5 string offsets table has - possibly shared - contributions
385  // from each compile and type unit each preceded by a header. The string
386  // offsets table used by the pre-DWARF v5 split-DWARF implementation uses
387  // a monolithic string offsets table without any header.
388  UseSegmentedStringOffsetsTable = DwarfVersion >= 5;
389 
390  Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
391 }
392 
393 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
394 DwarfDebug::~DwarfDebug() = default;
395 
396 static bool isObjCClass(StringRef Name) {
397  return Name.startswith("+") || Name.startswith("-");
398 }
399 
401  if (!isObjCClass(Name))
402  return false;
403 
404  return Name.find(") ") != StringRef::npos;
405 }
406 
408  StringRef &Category) {
409  if (!hasObjCCategory(In)) {
410  Class = In.slice(In.find('[') + 1, In.find(' '));
411  Category = "";
412  return;
413  }
414 
415  Class = In.slice(In.find('[') + 1, In.find('('));
416  Category = In.slice(In.find('[') + 1, In.find(' '));
417 }
418 
420  return In.slice(In.find(' ') + 1, In.find(']'));
421 }
422 
423 // Add the various names to the Dwarf accelerator table names.
425  if (!SP->isDefinition())
426  return;
427 
428  if (SP->getName() != "")
429  addAccelName(SP->getName(), Die);
430 
431  // If the linkage name is different than the name, go ahead and output that as
432  // well into the name table. Only do that if we are going to actually emit
433  // that name.
434  if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() &&
435  (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP)))
436  addAccelName(SP->getLinkageName(), Die);
437 
438  // If this is an Objective-C selector name add it to the ObjC accelerator
439  // too.
440  if (isObjCClass(SP->getName())) {
441  StringRef Class, Category;
442  getObjCClassCategory(SP->getName(), Class, Category);
443  addAccelObjC(Class, Die);
444  if (Category != "")
445  addAccelObjC(Category, Die);
446  // Also add the base method name to the name table.
448  }
449 }
450 
451 /// Check whether we should create a DIE for the given Scope, return true
452 /// if we don't create a DIE (the corresponding DIE is null).
454  if (Scope->isAbstractScope())
455  return false;
456 
457  // We don't create a DIE if there is no Range.
458  const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
459  if (Ranges.empty())
460  return true;
461 
462  if (Ranges.size() > 1)
463  return false;
464 
465  // We don't create a DIE if we have a single Range and the end label
466  // is null.
467  return !getLabelAfterInsn(Ranges.front().second);
468 }
469 
470 template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
471  F(CU);
472  if (auto *SkelCU = CU.getSkeleton())
473  if (CU.getCUNode()->getSplitDebugInlining())
474  F(*SkelCU);
475 }
476 
479 }
480 
481 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU,
482  LexicalScope *Scope) {
483  assert(Scope && Scope->getScopeNode());
484  assert(Scope->isAbstractScope());
485  assert(!Scope->getInlinedAt());
486 
487  auto *SP = cast<DISubprogram>(Scope->getScopeNode());
488 
489  // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
490  // was inlined from another compile unit.
491  if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining())
492  // Avoid building the original CU if it won't be used
494  else {
495  auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
496  if (auto *SkelCU = CU.getSkeleton()) {
497  (shareAcrossDWOCUs() ? CU : SrcCU)
498  .constructAbstractSubprogramScopeDIE(Scope);
499  if (CU.getCUNode()->getSplitDebugInlining())
500  SkelCU->constructAbstractSubprogramScopeDIE(Scope);
501  } else
502  CU.constructAbstractSubprogramScopeDIE(Scope);
503  }
504 }
505 
506 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const {
507  if (!U.hasDwarfPubSections())
508  return;
509 
510  U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
511 }
512 
513 // Create new DwarfCompileUnit for the given metadata node with tag
514 // DW_TAG_compile_unit.
516 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) {
517  if (auto *CU = CUMap.lookup(DIUnit))
518  return *CU;
519  StringRef FN = DIUnit->getFilename();
520  CompilationDir = DIUnit->getDirectory();
521 
522  auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
523  InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
524  DwarfCompileUnit &NewCU = *OwnedUnit;
525  DIE &Die = NewCU.getUnitDie();
526  InfoHolder.addUnit(std::move(OwnedUnit));
527  if (useSplitDwarf()) {
528  NewCU.setSkeleton(constructSkeletonCU(NewCU));
529  NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
530  Asm->TM.Options.MCOptions.SplitDwarfFile);
531  }
532 
533  for (auto *IE : DIUnit->getImportedEntities())
534  NewCU.addImportedEntity(IE);
535 
536  // LTO with assembly output shares a single line table amongst multiple CUs.
537  // To avoid the compilation directory being ambiguous, let the line table
538  // explicitly describe the directory of all files, never relying on the
539  // compilation directory.
540  if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
541  Asm->OutStreamer->emitDwarfFile0Directive(
542  CompilationDir, FN, NewCU.getMD5AsBytes(DIUnit->getFile()),
543  DIUnit->getSource(), NewCU.getUniqueID());
544 
545  StringRef Producer = DIUnit->getProducer();
546  StringRef Flags = DIUnit->getFlags();
547  if (!Flags.empty()) {
548  std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
549  NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
550  } else
551  NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
552 
553  NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
554  DIUnit->getSourceLanguage());
555  NewCU.addString(Die, dwarf::DW_AT_name, FN);
556 
557  // Add DW_str_offsets_base to the unit DIE, except for split units.
559  NewCU.addStringOffsetsStart();
560 
561  if (!useSplitDwarf()) {
562  NewCU.initStmtList();
563 
564  // If we're using split dwarf the compilation dir is going to be in the
565  // skeleton CU and so we don't need to duplicate it here.
566  if (!CompilationDir.empty())
567  NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
568 
569  addGnuPubAttributes(NewCU, Die);
570  }
571 
573  if (DIUnit->isOptimized())
574  NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
575 
576  StringRef Flags = DIUnit->getFlags();
577  if (!Flags.empty())
578  NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
579 
580  if (unsigned RVer = DIUnit->getRuntimeVersion())
581  NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
582  dwarf::DW_FORM_data1, RVer);
583  }
584 
585  if (useSplitDwarf())
586  NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
587  else
588  NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
589 
590  if (DIUnit->getDWOId()) {
591  // This CU is either a clang module DWO or a skeleton CU.
592  NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
593  DIUnit->getDWOId());
594  if (!DIUnit->getSplitDebugFilename().empty())
595  // This is a prefabricated skeleton CU.
596  NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
597  DIUnit->getSplitDebugFilename());
598  }
599 
600  CUMap.insert({DIUnit, &NewCU});
601  CUDieMap.insert({&Die, &NewCU});
602  return NewCU;
603 }
604 
605 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
606  const DIImportedEntity *N) {
607  if (isa<DILocalScope>(N->getScope()))
608  return;
609  if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
611 }
612 
613 /// Sort and unique GVEs by comparing their fragment offset.
616  llvm::sort(GVEs.begin(), GVEs.end(),
619  // Sort order: first null exprs, then exprs without fragment
620  // info, then sort by fragment offset in bits.
621  // FIXME: Come up with a more comprehensive comparator so
622  // the sorting isn't non-deterministic, and so the following
623  // std::unique call works correctly.
624  if (!A.Expr || !B.Expr)
625  return !!B.Expr;
626  auto FragmentA = A.Expr->getFragmentInfo();
627  auto FragmentB = B.Expr->getFragmentInfo();
628  if (!FragmentA || !FragmentB)
629  return !!FragmentB;
630  return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
631  });
632  GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
635  return A.Expr == B.Expr;
636  }),
637  GVEs.end());
638  return GVEs;
639 }
640 
641 // Emit all Dwarf sections that should come prior to the content. Create
642 // global DIEs and emit initial debug info sections. This is invoked by
643 // the target AsmPrinter.
645  NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName,
646  DWARFGroupDescription, TimePassesIsEnabled);
648  return;
649 
650  const Module *M = MMI->getModule();
651 
652  unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
654  // Tell MMI whether we have debug info.
655  MMI->setDebugInfoAvailability(NumDebugCUs > 0);
656  SingleCU = NumDebugCUs == 1;
658  GVMap;
659  for (const GlobalVariable &Global : M->globals()) {
661  Global.getDebugInfo(GVs);
662  for (auto *GVE : GVs)
663  GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
664  }
665 
666  // Create the symbol that designates the start of the unit's contribution
667  // to the string offsets table. In a split DWARF scenario, only the skeleton
668  // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol).
670  (useSplitDwarf() ? SkeletonHolder : InfoHolder)
671  .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base"));
672 
673  // Create the symbol that designates the start of the DWARF v5 range list
674  // table. It is located past the header and before the offsets table.
675  if (getDwarfVersion() >= 5)
676  (useSplitDwarf() ? SkeletonHolder : InfoHolder)
677  .setRnglistsTableBaseSym(Asm->createTempSymbol("rnglists_table_base"));
678 
679  for (DICompileUnit *CUNode : M->debug_compile_units()) {
680  // FIXME: Move local imported entities into a list attached to the
681  // subprogram, then this search won't be needed and a
682  // getImportedEntities().empty() test should go below with the rest.
683  bool HasNonLocalImportedEntities = llvm::any_of(
684  CUNode->getImportedEntities(), [](const DIImportedEntity *IE) {
685  return !isa<DILocalScope>(IE->getScope());
686  });
687 
688  if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() &&
689  CUNode->getRetainedTypes().empty() &&
690  CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
691  continue;
692 
693  DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
694 
695  // Global Variables.
696  for (auto *GVE : CUNode->getGlobalVariables()) {
697  // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
698  // already know about the variable and it isn't adding a constant
699  // expression.
700  auto &GVMapEntry = GVMap[GVE->getVariable()];
701  auto *Expr = GVE->getExpression();
702  if (!GVMapEntry.size() || (Expr && Expr->isConstant()))
703  GVMapEntry.push_back({nullptr, Expr});
704  }
706  for (auto *GVE : CUNode->getGlobalVariables()) {
707  DIGlobalVariable *GV = GVE->getVariable();
708  if (Processed.insert(GV).second)
710  }
711 
712  for (auto *Ty : CUNode->getEnumTypes()) {
713  // The enum types array by design contains pointers to
714  // MDNodes rather than DIRefs. Unique them here.
715  CU.getOrCreateTypeDIE(cast<DIType>(Ty));
716  }
717  for (auto *Ty : CUNode->getRetainedTypes()) {
718  // The retained types array by design contains pointers to
719  // MDNodes rather than DIRefs. Unique them here.
720  if (DIType *RT = dyn_cast<DIType>(Ty))
721  // There is no point in force-emitting a forward declaration.
722  CU.getOrCreateTypeDIE(RT);
723  }
724  // Emit imported_modules last so that the relevant context is already
725  // available.
726  for (auto *IE : CUNode->getImportedEntities())
727  constructAndAddImportedEntityDIE(CU, IE);
728  }
729 }
730 
731 void DwarfDebug::finishVariableDefinitions() {
732  for (const auto &Var : ConcreteVariables) {
733  DIE *VariableDie = Var->getDIE();
734  assert(VariableDie);
735  // FIXME: Consider the time-space tradeoff of just storing the unit pointer
736  // in the ConcreteVariables list, rather than looking it up again here.
737  // DIE::getUnit isn't simple - it walks parent pointers, etc.
738  DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie());
739  assert(Unit);
740  Unit->finishVariableDefinition(*Var);
741  }
742 }
743 
744 void DwarfDebug::finishSubprogramDefinitions() {
745  for (const DISubprogram *SP : ProcessedSPNodes) {
746  assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
747  forBothCUs(
748  getOrCreateDwarfCompileUnit(SP->getUnit()),
750  }
751 }
752 
753 void DwarfDebug::finalizeModuleInfo() {
755 
756  finishSubprogramDefinitions();
757 
758  finishVariableDefinitions();
759 
760  // Include the DWO file name in the hash if there's more than one CU.
761  // This handles ThinLTO's situation where imported CUs may very easily be
762  // duplicate with the same CU partially imported into another ThinLTO unit.
763  StringRef DWOName;
764  if (CUMap.size() > 1)
766 
767  // Handle anything that needs to be done on a per-unit basis after
768  // all other generation.
769  for (const auto &P : CUMap) {
770  auto &TheCU = *P.second;
771  // Emit DW_AT_containing_type attribute to connect types with their
772  // vtable holding type.
774 
775  // Add CU specific attributes if we need to add any.
776  // If we're splitting the dwarf out now that we've got the entire
777  // CU then add the dwo id to it.
778  auto *SkCU = TheCU.getSkeleton();
779  if (useSplitDwarf()) {
780  // Emit a unique identifier for this CU.
781  uint64_t ID =
782  DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie());
783  if (getDwarfVersion() >= 5) {
784  TheCU.setDWOId(ID);
785  SkCU->setDWOId(ID);
786  } else {
787  TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
788  dwarf::DW_FORM_data8, ID);
789  SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
790  dwarf::DW_FORM_data8, ID);
791  }
792  // We don't keep track of which addresses are used in which CU so this
793  // is a bit pessimistic under LTO.
794  if (!AddrPool.isEmpty()) {
795  const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol();
796  SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
797  Sym, Sym);
798  }
799  if (getDwarfVersion() < 5 && !SkCU->getRangeLists().empty()) {
800  const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
801  SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
802  Sym, Sym);
803  }
804  }
805 
806  // If we have code split among multiple sections or non-contiguous
807  // ranges of code then emit a DW_AT_ranges attribute on the unit that will
808  // remain in the .o file, otherwise add a DW_AT_low_pc.
809  // FIXME: We should use ranges allow reordering of code ala
810  // .subsections_via_symbols in mach-o. This would mean turning on
811  // ranges for all subprogram DIEs for mach-o.
812  DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
813  if (unsigned NumRanges = TheCU.getRanges().size()) {
814  if (NumRanges > 1 && useRangesSection())
815  // A DW_AT_low_pc attribute may also be specified in combination with
816  // DW_AT_ranges to specify the default base address for use in
817  // location lists (see Section 2.6.2) and range lists (see Section
818  // 2.17.3).
819  U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
820  else
821  U.setBaseAddress(TheCU.getRanges().front().getStart());
823  }
824 
825  if (getDwarfVersion() >= 5 && !useSplitDwarf() &&
826  !U.getRangeLists().empty())
827  U.addRnglistsBase();
828 
829  auto *CUNode = cast<DICompileUnit>(P.first);
830  // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
831  if (CUNode->getMacros())
832  U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
833  U.getMacroLabelBegin(),
835  }
836 
837  // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
838  for (auto *CUNode : MMI->getModule()->debug_compile_units())
839  if (CUNode->getDWOId())
840  getOrCreateDwarfCompileUnit(CUNode);
841 
842  // Compute DIE offsets and sizes.
843  InfoHolder.computeSizeAndOffsets();
844  if (useSplitDwarf())
845  SkeletonHolder.computeSizeAndOffsets();
846 }
847 
848 // Emit all Dwarf sections that should come after the content.
850  assert(CurFn == nullptr);
851  assert(CurMI == nullptr);
852 
853  // If we aren't actually generating debug info (check beginModule -
854  // conditionalized on !DisableDebugInfoPrinting and the presence of the
855  // llvm.dbg.cu metadata node)
856  if (!MMI->hasDebugInfo())
857  return;
858 
859  // Finalize the debug info for the module.
860  finalizeModuleInfo();
861 
862  emitDebugStr();
863 
864  if (useSplitDwarf())
865  emitDebugLocDWO();
866  else
867  // Emit info into a debug loc section.
868  emitDebugLoc();
869 
870  // Corresponding abbreviations into a abbrev section.
871  emitAbbreviations();
872 
873  // Emit all the DIEs into a debug info section.
874  emitDebugInfo();
875 
876  // Emit info into a debug aranges section.
878  emitDebugARanges();
879 
880  // Emit info into a debug ranges section.
881  emitDebugRanges();
882 
883  // Emit info into a debug macinfo section.
884  emitDebugMacinfo();
885 
886  if (useSplitDwarf()) {
887  emitDebugStrDWO();
888  emitDebugInfoDWO();
889  emitDebugAbbrevDWO();
890  emitDebugLineDWO();
891  emitDebugAddr();
892  }
893 
894  // Emit info into the dwarf accelerator table sections.
895  switch (getAccelTableKind()) {
897  emitAccelNames();
898  emitAccelObjC();
899  emitAccelNamespaces();
900  emitAccelTypes();
901  break;
903  emitAccelDebugNames();
904  break;
906  break;
908  llvm_unreachable("Default should have already been resolved.");
909  }
910 
911  // Emit the pubnames and pubtypes sections if requested.
912  emitDebugPubSections();
913 
914  // clean up.
915  // FIXME: AbstractVariables.clear();
916 }
917 
918 void DwarfDebug::ensureAbstractVariableIsCreated(DwarfCompileUnit &CU, InlinedVariable IV,
919  const MDNode *ScopeNode) {
920  const DILocalVariable *Cleansed = nullptr;
921  if (CU.getExistingAbstractVariable(IV, Cleansed))
922  return;
923 
925  cast<DILocalScope>(ScopeNode)));
926 }
927 
928 void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit &CU,
929  InlinedVariable IV, const MDNode *ScopeNode) {
930  const DILocalVariable *Cleansed = nullptr;
931  if (CU.getExistingAbstractVariable(IV, Cleansed))
932  return;
933 
934  if (LexicalScope *Scope =
935  LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
936  CU.createAbstractVariable(Cleansed, Scope);
937 }
938 
939 // Collect variable information from side table maintained by MF.
940 void DwarfDebug::collectVariableInfoFromMFTable(
941  DwarfCompileUnit &TheCU, DenseSet<InlinedVariable> &Processed) {
943  for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
944  if (!VI.Var)
945  continue;
946  assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
947  "Expected inlined-at fields to agree");
948 
949  InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt());
950  Processed.insert(Var);
951  LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
952 
953  // If variable scope is not found then skip this variable.
954  if (!Scope)
955  continue;
956 
957  ensureAbstractVariableIsCreatedIfScoped(TheCU, Var, Scope->getScopeNode());
958  auto RegVar = llvm::make_unique<DbgVariable>(Var.first, Var.second);
959  RegVar->initializeMMI(VI.Expr, VI.Slot);
960  if (DbgVariable *DbgVar = MFVars.lookup(Var))
961  DbgVar->addMMIEntry(*RegVar);
962  else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) {
963  MFVars.insert({Var, RegVar.get()});
964  ConcreteVariables.push_back(std::move(RegVar));
965  }
966  }
967 }
968 
969 // Get .debug_loc entry for the instruction range starting at MI.
971  const DIExpression *Expr = MI->getDebugExpression();
972  assert(MI->getNumOperands() == 4);
973  if (MI->getOperand(0).isReg()) {
974  auto RegOp = MI->getOperand(0);
975  auto Op1 = MI->getOperand(1);
976  // If the second operand is an immediate, this is a
977  // register-indirect address.
978  assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset");
979  MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
980  return DebugLocEntry::Value(Expr, MLoc);
981  }
982  if (MI->getOperand(0).isImm())
983  return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
984  if (MI->getOperand(0).isFPImm())
985  return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
986  if (MI->getOperand(0).isCImm())
987  return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
988 
989  llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
990 }
991 
992 /// If this and Next are describing different fragments of the same
993 /// variable, merge them by appending Next's values to the current
994 /// list of values.
995 /// Return true if the merge was successful.
997  if (Begin == Next.Begin) {
998  auto *FirstExpr = cast<DIExpression>(Values[0].Expression);
999  auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression);
1000  if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment())
1001  return false;
1002 
1003  // We can only merge entries if none of the fragments overlap any others.
1004  // In doing so, we can take advantage of the fact that both lists are
1005  // sorted.
1006  for (unsigned i = 0, j = 0; i < Values.size(); ++i) {
1007  for (; j < Next.Values.size(); ++j) {
1008  int res = cast<DIExpression>(Values[i].Expression)->fragmentCmp(
1009  cast<DIExpression>(Next.Values[j].Expression));
1010  if (res == 0) // The two expressions overlap, we can't merge.
1011  return false;
1012  // Values[i] is entirely before Next.Values[j],
1013  // so go back to the next entry of Values.
1014  else if (res == -1)
1015  break;
1016  // Next.Values[j] is entirely before Values[i], so go on to the
1017  // next entry of Next.Values.
1018  }
1019  }
1020 
1021  addValues(Next.Values);
1022  End = Next.End;
1023  return true;
1024  }
1025  return false;
1026 }
1027 
1028 /// Build the location list for all DBG_VALUEs in the function that
1029 /// describe the same variable. If the ranges of several independent
1030 /// fragments of the same variable overlap partially, split them up and
1031 /// combine the ranges. The resulting DebugLocEntries are will have
1032 /// strict monotonically increasing begin addresses and will never
1033 /// overlap.
1034 //
1035 // Input:
1036 //
1037 // Ranges History [var, loc, fragment ofs size]
1038 // 0 | [x, (reg0, fragment 0, 32)]
1039 // 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry
1040 // 2 | | ...
1041 // 3 | [clobber reg0]
1042 // 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of
1043 // x.
1044 //
1045 // Output:
1046 //
1047 // [0-1] [x, (reg0, fragment 0, 32)]
1048 // [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)]
1049 // [3-4] [x, (reg1, fragment 32, 32)]
1050 // [4- ] [x, (mem, fragment 0, 64)]
1051 void
1052 DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
1053  const DbgValueHistoryMap::InstrRanges &Ranges) {
1055 
1056  for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1057  const MachineInstr *Begin = I->first;
1058  const MachineInstr *End = I->second;
1059  assert(Begin->isDebugValue() && "Invalid History entry");
1060 
1061  // Check if a variable is inaccessible in this range.
1062  if (Begin->getNumOperands() > 1 &&
1063  Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
1064  OpenRanges.clear();
1065  continue;
1066  }
1067 
1068  // If this fragment overlaps with any open ranges, truncate them.
1069  const DIExpression *DIExpr = Begin->getDebugExpression();
1070  auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) {
1071  return DIExpr->fragmentsOverlap(R.getExpression());
1072  });
1073  OpenRanges.erase(Last, OpenRanges.end());
1074 
1075  const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
1076  assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
1077 
1078  const MCSymbol *EndLabel;
1079  if (End != nullptr)
1080  EndLabel = getLabelAfterInsn(End);
1081  else if (std::next(I) == Ranges.end())
1082  EndLabel = Asm->getFunctionEnd();
1083  else
1084  EndLabel = getLabelBeforeInsn(std::next(I)->first);
1085  assert(EndLabel && "Forgot label after instruction ending a range!");
1086 
1087  LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
1088 
1089  auto Value = getDebugLocValue(Begin);
1090  DebugLocEntry Loc(StartLabel, EndLabel, Value);
1091  bool couldMerge = false;
1092 
1093  // If this is a fragment, it may belong to the current DebugLocEntry.
1094  if (DIExpr->isFragment()) {
1095  // Add this value to the list of open ranges.
1096  OpenRanges.push_back(Value);
1097 
1098  // Attempt to add the fragment to the last entry.
1099  if (!DebugLoc.empty())
1100  if (DebugLoc.back().MergeValues(Loc))
1101  couldMerge = true;
1102  }
1103 
1104  if (!couldMerge) {
1105  // Need to add a new DebugLocEntry. Add all values from still
1106  // valid non-overlapping fragments.
1107  if (OpenRanges.size())
1108  Loc.addValues(OpenRanges);
1109 
1110  DebugLoc.push_back(std::move(Loc));
1111  }
1112 
1113  // Attempt to coalesce the ranges of two otherwise identical
1114  // DebugLocEntries.
1115  auto CurEntry = DebugLoc.rbegin();
1116  LLVM_DEBUG({
1117  dbgs() << CurEntry->getValues().size() << " Values:\n";
1118  for (auto &Value : CurEntry->getValues())
1119  Value.dump();
1120  dbgs() << "-----\n";
1121  });
1122 
1123  auto PrevEntry = std::next(CurEntry);
1124  if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
1125  DebugLoc.pop_back();
1126  }
1127 }
1128 
1129 DbgVariable *DwarfDebug::createConcreteVariable(DwarfCompileUnit &TheCU,
1130  LexicalScope &Scope,
1131  InlinedVariable IV) {
1132  ensureAbstractVariableIsCreatedIfScoped(TheCU, IV, Scope.getScopeNode());
1133  ConcreteVariables.push_back(
1134  llvm::make_unique<DbgVariable>(IV.first, IV.second));
1135  InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get());
1136  return ConcreteVariables.back().get();
1137 }
1138 
1139 /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1140 /// enclosing lexical scope. The check ensures there are no other instructions
1141 /// in the same lexical scope preceding the DBG_VALUE and that its range is
1142 /// either open or otherwise rolls off the end of the scope.
1144  const MachineInstr *DbgValue,
1145  const MachineInstr *RangeEnd) {
1146  assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
1147  auto MBB = DbgValue->getParent();
1148  auto DL = DbgValue->getDebugLoc();
1149  auto *LScope = LScopes.findLexicalScope(DL);
1150  // Scope doesn't exist; this is a dead DBG_VALUE.
1151  if (!LScope)
1152  return false;
1153  auto &LSRange = LScope->getRanges();
1154  if (LSRange.size() == 0)
1155  return false;
1156 
1157  // Determine if the DBG_VALUE is valid at the beginning of its lexical block.
1158  const MachineInstr *LScopeBegin = LSRange.front().first;
1159  // Early exit if the lexical scope begins outside of the current block.
1160  if (LScopeBegin->getParent() != MBB)
1161  return false;
1163  for (++Pred; Pred != MBB->rend(); ++Pred) {
1164  if (Pred->getFlag(MachineInstr::FrameSetup))
1165  break;
1166  auto PredDL = Pred->getDebugLoc();
1167  if (!PredDL || Pred->isMetaInstruction())
1168  continue;
1169  // Check whether the instruction preceding the DBG_VALUE is in the same
1170  // (sub)scope as the DBG_VALUE.
1171  if (DL->getScope() == PredDL->getScope())
1172  return false;
1173  auto *PredScope = LScopes.findLexicalScope(PredDL);
1174  if (!PredScope || LScope->dominates(PredScope))
1175  return false;
1176  }
1177 
1178  // If the range of the DBG_VALUE is open-ended, report success.
1179  if (!RangeEnd)
1180  return true;
1181 
1182  // Fail if there are instructions belonging to our scope in another block.
1183  const MachineInstr *LScopeEnd = LSRange.back().second;
1184  if (LScopeEnd->getParent() != MBB)
1185  return false;
1186 
1187  // Single, constant DBG_VALUEs in the prologue are promoted to be live
1188  // throughout the function. This is a hack, presumably for DWARF v2 and not
1189  // necessarily correct. It would be much better to use a dbg.declare instead
1190  // if we know the constant is live throughout the scope.
1191  if (DbgValue->getOperand(0).isImm() && MBB->pred_empty())
1192  return true;
1193 
1194  return false;
1195 }
1196 
1197 // Find variables for each lexical scope.
1198 void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
1199  const DISubprogram *SP,
1200  DenseSet<InlinedVariable> &Processed) {
1201  // Grab the variable info that was squirreled away in the MMI side-table.
1202  collectVariableInfoFromMFTable(TheCU, Processed);
1203 
1204  for (const auto &I : DbgValues) {
1205  InlinedVariable IV = I.first;
1206  if (Processed.count(IV))
1207  continue;
1208 
1209  // Instruction ranges, specifying where IV is accessible.
1210  const auto &Ranges = I.second;
1211  if (Ranges.empty())
1212  continue;
1213 
1214  LexicalScope *Scope = nullptr;
1215  if (const DILocation *IA = IV.second)
1216  Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
1217  else
1218  Scope = LScopes.findLexicalScope(IV.first->getScope());
1219  // If variable scope is not found then skip this variable.
1220  if (!Scope)
1221  continue;
1222 
1223  Processed.insert(IV);
1224  DbgVariable *RegVar = createConcreteVariable(TheCU, *Scope, IV);
1225 
1226  const MachineInstr *MInsn = Ranges.front().first;
1227  assert(MInsn->isDebugValue() && "History must begin with debug value");
1228 
1229  // Check if there is a single DBG_VALUE, valid throughout the var's scope.
1230  if (Ranges.size() == 1 &&
1231  validThroughout(LScopes, MInsn, Ranges.front().second)) {
1232  RegVar->initializeDbgValue(MInsn);
1233  continue;
1234  }
1235  // Do not emit location lists if .debug_loc secton is disabled.
1236  if (!useLocSection())
1237  continue;
1238 
1239  // Handle multiple DBG_VALUE instructions describing one variable.
1240  DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
1241 
1242  // Build the location list for this variable.
1244  buildLocationList(Entries, Ranges);
1245 
1246  // If the variable has a DIBasicType, extract it. Basic types cannot have
1247  // unique identifiers, so don't bother resolving the type with the
1248  // identifier map.
1249  const DIBasicType *BT = dyn_cast<DIBasicType>(
1250  static_cast<const Metadata *>(IV.first->getType()));
1251 
1252  // Finalize the entry by lowering it into a DWARF bytestream.
1253  for (auto &Entry : Entries)
1254  Entry.finalize(*Asm, List, BT);
1255  }
1256 
1257  // Collect info for variables that were optimized out.
1258  for (const DINode *DN : SP->getRetainedNodes()) {
1259  if (auto *DV = dyn_cast<DILocalVariable>(DN)) {
1260  if (Processed.insert(InlinedVariable(DV, nullptr)).second)
1261  if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
1262  createConcreteVariable(TheCU, *Scope, InlinedVariable(DV, nullptr));
1263  }
1264  }
1265 }
1266 
1267 // Process beginning of an instruction.
1270  assert(CurMI);
1271 
1272  const auto *SP = MI->getMF()->getFunction().getSubprogram();
1273  if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1274  return;
1275 
1276  // Check if source location changes, but ignore DBG_VALUE and CFI locations.
1277  // If the instruction is part of the function frame setup code, do not emit
1278  // any line record, as there is no correspondence with any user code.
1280  return;
1281  const DebugLoc &DL = MI->getDebugLoc();
1282  // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1283  // the last line number actually emitted, to see if it was line 0.
1284  unsigned LastAsmLine =
1285  Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
1286 
1287  if (DL == PrevInstLoc) {
1288  // If we have an ongoing unspecified location, nothing to do here.
1289  if (!DL)
1290  return;
1291  // We have an explicit location, same as the previous location.
1292  // But we might be coming back to it after a line 0 record.
1293  if (LastAsmLine == 0 && DL.getLine() != 0) {
1294  // Reinstate the source location but not marked as a statement.
1295  const MDNode *Scope = DL.getScope();
1296  recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0);
1297  }
1298  return;
1299  }
1300 
1301  if (!DL) {
1302  // We have an unspecified location, which might want to be line 0.
1303  // If we have already emitted a line-0 record, don't repeat it.
1304  if (LastAsmLine == 0)
1305  return;
1306  // If user said Don't Do That, don't do that.
1307  if (UnknownLocations == Disable)
1308  return;
1309  // See if we have a reason to emit a line-0 record now.
1310  // Reasons to emit a line-0 record include:
1311  // - User asked for it (UnknownLocations).
1312  // - Instruction has a label, so it's referenced from somewhere else,
1313  // possibly debug information; we want it to have a source location.
1314  // - Instruction is at the top of a block; we don't want to inherit the
1315  // location from the physically previous (maybe unrelated) block.
1316  if (UnknownLocations == Enable || PrevLabel ||
1317  (PrevInstBB && PrevInstBB != MI->getParent())) {
1318  // Preserve the file and column numbers, if we can, to save space in
1319  // the encoded line table.
1320  // Do not update PrevInstLoc, it remembers the last non-0 line.
1321  const MDNode *Scope = nullptr;
1322  unsigned Column = 0;
1323  if (PrevInstLoc) {
1324  Scope = PrevInstLoc.getScope();
1325  Column = PrevInstLoc.getCol();
1326  }
1327  recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
1328  }
1329  return;
1330  }
1331 
1332  // We have an explicit location, different from the previous location.
1333  // Don't repeat a line-0 record, but otherwise emit the new location.
1334  // (The new location might be an explicit line 0, which we do emit.)
1335  if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0)
1336  return;
1337  unsigned Flags = 0;
1338  if (DL == PrologEndLoc) {
1340  PrologEndLoc = DebugLoc();
1341  }
1342  // If the line changed, we call that a new statement; unless we went to
1343  // line 0 and came back, in which case it is not a new statement.
1344  unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
1345  if (DL.getLine() && DL.getLine() != OldLine)
1346  Flags |= DWARF2_FLAG_IS_STMT;
1347 
1348  const MDNode *Scope = DL.getScope();
1349  recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
1350 
1351  // If we're not at line 0, remember this location.
1352  if (DL.getLine())
1353  PrevInstLoc = DL;
1354 }
1355 
1356 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1357  // First known non-DBG_VALUE and non-frame setup location marks
1358  // the beginning of the function body.
1359  for (const auto &MBB : *MF)
1360  for (const auto &MI : MBB)
1361  if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1362  MI.getDebugLoc())
1363  return MI.getDebugLoc();
1364  return DebugLoc();
1365 }
1366 
1367 // Gather pre-function debug information. Assumes being called immediately
1368 // after the function entry point has been emitted.
1370  CurFn = MF;
1371 
1372  auto *SP = MF->getFunction().getSubprogram();
1374  if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1375  return;
1376 
1377  DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
1378 
1379  // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1380  // belongs to so that we add to the correct per-cu line table in the
1381  // non-asm case.
1382  if (Asm->OutStreamer->hasRawTextSupport())
1383  // Use a single line table if we are generating assembly.
1384  Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1385  else
1386  Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID());
1387 
1388  // Record beginning of function.
1390  if (PrologEndLoc) {
1391  // We'd like to list the prologue as "not statements" but GDB behaves
1392  // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1393  auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
1394  recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
1395  }
1396 }
1397 
1399  // If we don't have a subprogram for this function then there will be a hole
1400  // in the range information. Keep note of this by setting the previously used
1401  // section to nullptr.
1402  PrevCU = nullptr;
1403  CurFn = nullptr;
1404 }
1405 
1406 // Gather and emit post-function debug information.
1408  const DISubprogram *SP = MF->getFunction().getSubprogram();
1409 
1410  assert(CurFn == MF &&
1411  "endFunction should be called with the same function as beginFunction");
1412 
1413  // Set DwarfDwarfCompileUnitID in MCContext to default value.
1414  Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1415 
1417  assert(!FnScope || SP == FnScope->getScopeNode());
1418  DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit());
1419 
1420  DenseSet<InlinedVariable> ProcessedVars;
1421  collectVariableInfo(TheCU, SP, ProcessedVars);
1422 
1423  // Add the range of this function to the list of ranges for the CU.
1424  TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
1425 
1426  // Under -gmlt, skip building the subprogram if there are no inlined
1427  // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
1428  // is still needed as we need its source location.
1429  if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
1430  TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
1431  LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1432  assert(InfoHolder.getScopeVariables().empty());
1433  PrevLabel = nullptr;
1434  CurFn = nullptr;
1435  return;
1436  }
1437 
1438 #ifndef NDEBUG
1439  size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1440 #endif
1441  // Construct abstract scopes.
1442  for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
1443  auto *SP = cast<DISubprogram>(AScope->getScopeNode());
1444  for (const DINode *DN : SP->getRetainedNodes()) {
1445  if (auto *DV = dyn_cast<DILocalVariable>(DN)) {
1446  // Collect info for variables that were optimized out.
1447  if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
1448  continue;
1449  ensureAbstractVariableIsCreated(TheCU, InlinedVariable(DV, nullptr),
1450  DV->getScope());
1451  assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1452  && "ensureAbstractVariableIsCreated inserted abstract scopes");
1453  }
1454  }
1455  constructAbstractSubprogramScopeDIE(TheCU, AScope);
1456  }
1457 
1458  ProcessedSPNodes.insert(SP);
1459  TheCU.constructSubprogramScopeDIE(SP, FnScope);
1460  if (auto *SkelCU = TheCU.getSkeleton())
1461  if (!LScopes.getAbstractScopesList().empty() &&
1462  TheCU.getCUNode()->getSplitDebugInlining())
1463  SkelCU->constructSubprogramScopeDIE(SP, FnScope);
1464 
1465  // Clear debug info
1466  // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1467  // DbgVariables except those that are also in AbstractVariables (since they
1468  // can be used cross-function)
1469  InfoHolder.getScopeVariables().clear();
1470  PrevLabel = nullptr;
1471  CurFn = nullptr;
1472 }
1473 
1474 // Register a source line with debug info. Returns the unique label that was
1475 // emitted and which provides correspondence to the source line list.
1476 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1477  unsigned Flags) {
1478  StringRef Fn;
1479  unsigned FileNo = 1;
1480  unsigned Discriminator = 0;
1481  if (auto *Scope = cast_or_null<DIScope>(S)) {
1482  Fn = Scope->getFilename();
1483  if (Line != 0 && getDwarfVersion() >= 4)
1484  if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1485  Discriminator = LBF->getDiscriminator();
1486 
1487  unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
1488  FileNo = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1489  .getOrCreateSourceID(Scope->getFile());
1490  }
1491  Asm->OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
1492  Discriminator, Fn);
1493 }
1494 
1495 //===----------------------------------------------------------------------===//
1496 // Emit Methods
1497 //===----------------------------------------------------------------------===//
1498 
1499 // Emit the debug info section.
1500 void DwarfDebug::emitDebugInfo() {
1501  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1502  Holder.emitUnits(/* UseOffsets */ false);
1503 }
1504 
1505 // Emit the abbreviation section.
1506 void DwarfDebug::emitAbbreviations() {
1507  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1508 
1510 }
1511 
1512 void DwarfDebug::emitStringOffsetsTableHeader() {
1513  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1516  Holder.getStringOffsetsStartSym());
1517 }
1518 
1519 template <typename AccelTableT>
1520 void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section,
1521  StringRef TableName) {
1522  Asm->OutStreamer->SwitchSection(Section);
1523 
1524  // Emit the full data.
1525  emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol());
1526 }
1527 
1528 void DwarfDebug::emitAccelDebugNames() {
1529  // Don't emit anything if we have no compilation units to index.
1530  if (getUnits().empty())
1531  return;
1532 
1533  Asm->OutStreamer->SwitchSection(
1535  emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits());
1536 }
1537 
1538 // Emit visible names into a hashed accelerator table section.
1539 void DwarfDebug::emitAccelNames() {
1540  emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1541  "Names");
1542 }
1543 
1544 // Emit objective C classes and categories into a hashed accelerator table
1545 // section.
1546 void DwarfDebug::emitAccelObjC() {
1547  emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1548  "ObjC");
1549 }
1550 
1551 // Emit namespace dies into a hashed accelerator table.
1552 void DwarfDebug::emitAccelNamespaces() {
1553  emitAccel(AccelNamespace,
1555  "namespac");
1556 }
1557 
1558 // Emit type dies into a hashed accelerator table.
1559 void DwarfDebug::emitAccelTypes() {
1560  emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1561  "types");
1562 }
1563 
1564 // Public name handling.
1565 // The format for the various pubnames:
1566 //
1567 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1568 // for the DIE that is named.
1569 //
1570 // gnu pubnames - offset/index value/name tuples where the offset is the offset
1571 // into the CU and the index value is computed according to the type of value
1572 // for the DIE that is named.
1573 //
1574 // For type units the offset is the offset of the skeleton DIE. For split dwarf
1575 // it's the offset within the debug_info/debug_types dwo section, however, the
1576 // reference in the pubname header doesn't change.
1577 
1578 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
1580  const DIE *Die) {
1581  // Entities that ended up only in a Type Unit reference the CU instead (since
1582  // the pub entry has offsets within the CU there's no real offset that can be
1583  // provided anyway). As it happens all such entities (namespaces and types,
1584  // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
1585  // not to be true it would be necessary to persist this information from the
1586  // point at which the entry is added to the index data structure - since by
1587  // the time the index is built from that, the original type/namespace DIE in a
1588  // type unit has already been destroyed so it can't be queried for properties
1589  // like tag, etc.
1590  if (Die->getTag() == dwarf::DW_TAG_compile_unit)
1594 
1595  // We could have a specification DIE that has our most of our knowledge,
1596  // look for that now.
1597  if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1598  DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
1599  if (SpecDIE.findAttribute(dwarf::DW_AT_external))
1600  Linkage = dwarf::GIEL_EXTERNAL;
1601  } else if (Die->findAttribute(dwarf::DW_AT_external))
1602  Linkage = dwarf::GIEL_EXTERNAL;
1603 
1604  switch (Die->getTag()) {
1605  case dwarf::DW_TAG_class_type:
1606  case dwarf::DW_TAG_structure_type:
1607  case dwarf::DW_TAG_union_type:
1608  case dwarf::DW_TAG_enumeration_type:
1610  dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1613  case dwarf::DW_TAG_typedef:
1614  case dwarf::DW_TAG_base_type:
1615  case dwarf::DW_TAG_subrange_type:
1617  case dwarf::DW_TAG_namespace:
1618  return dwarf::GIEK_TYPE;
1619  case dwarf::DW_TAG_subprogram:
1621  case dwarf::DW_TAG_variable:
1623  case dwarf::DW_TAG_enumerator:
1626  default:
1627  return dwarf::GIEK_NONE;
1628  }
1629 }
1630 
1631 /// emitDebugPubSections - Emit visible names and types into debug pubnames and
1632 /// pubtypes sections.
1633 void DwarfDebug::emitDebugPubSections() {
1634  for (const auto &NU : CUMap) {
1635  DwarfCompileUnit *TheU = NU.second;
1636  if (!TheU->hasDwarfPubSections())
1637  continue;
1638 
1639  bool GnuStyle = TheU->getCUNode()->getGnuPubnames();
1640 
1641  Asm->OutStreamer->SwitchSection(
1644  emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames());
1645 
1646  Asm->OutStreamer->SwitchSection(
1649  emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes());
1650  }
1651 }
1652 
1653 void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) {
1656  CU.getDebugSectionOffset());
1657  else
1659 }
1660 
1661 void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
1662  DwarfCompileUnit *TheU,
1663  const StringMap<const DIE *> &Globals) {
1664  if (auto *Skeleton = TheU->getSkeleton())
1665  TheU = Skeleton;
1666 
1667  // Emit the header.
1668  Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1669  MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1670  MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1671  Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1672 
1673  Asm->OutStreamer->EmitLabel(BeginLabel);
1674 
1675  Asm->OutStreamer->AddComment("DWARF Version");
1677 
1678  Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
1679  emitSectionReference(*TheU);
1680 
1681  Asm->OutStreamer->AddComment("Compilation Unit Length");
1682  Asm->emitInt32(TheU->getLength());
1683 
1684  // Emit the pubnames for this compilation unit.
1685  for (const auto &GI : Globals) {
1686  const char *Name = GI.getKeyData();
1687  const DIE *Entity = GI.second;
1688 
1689  Asm->OutStreamer->AddComment("DIE offset");
1690  Asm->emitInt32(Entity->getOffset());
1691 
1692  if (GnuStyle) {
1694  Asm->OutStreamer->AddComment(
1695  Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1697  Asm->emitInt8(Desc.toBits());
1698  }
1699 
1700  Asm->OutStreamer->AddComment("External Name");
1701  Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1702  }
1703 
1704  Asm->OutStreamer->AddComment("End Mark");
1705  Asm->emitInt32(0);
1706  Asm->OutStreamer->EmitLabel(EndLabel);
1707 }
1708 
1709 /// Emit null-terminated strings into a debug str section.
1710 void DwarfDebug::emitDebugStr() {
1711  MCSection *StringOffsetsSection = nullptr;
1713  emitStringOffsetsTableHeader();
1714  StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection();
1715  }
1716  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1718  StringOffsetsSection, /* UseRelativeOffsets = */ true);
1719 }
1720 
1722  const DebugLocStream::Entry &Entry) {
1723  auto &&Comments = DebugLocs.getComments(Entry);
1724  auto Comment = Comments.begin();
1725  auto End = Comments.end();
1726  for (uint8_t Byte : DebugLocs.getBytes(Entry))
1727  Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
1728 }
1729 
1730 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
1731  const DebugLocEntry::Value &Value,
1732  DwarfExpression &DwarfExpr) {
1733  auto *DIExpr = Value.getExpression();
1734  DIExpressionCursor ExprCursor(DIExpr);
1735  DwarfExpr.addFragmentOffset(DIExpr);
1736  // Regular entry.
1737  if (Value.isInt()) {
1738  if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1739  BT->getEncoding() == dwarf::DW_ATE_signed_char))
1740  DwarfExpr.addSignedConstant(Value.getInt());
1741  else
1742  DwarfExpr.addUnsignedConstant(Value.getInt());
1743  } else if (Value.isLocation()) {
1744  MachineLocation Location = Value.getLoc();
1745  if (Location.isIndirect())
1746  DwarfExpr.setMemoryLocationKind();
1747  DIExpressionCursor Cursor(DIExpr);
1748  const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
1749  if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
1750  return;
1751  return DwarfExpr.addExpression(std::move(Cursor));
1752  } else if (Value.isConstantFP()) {
1753  APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
1754  DwarfExpr.addUnsignedConstant(RawBytes);
1755  }
1756  DwarfExpr.addExpression(std::move(ExprCursor));
1757 }
1758 
1761  const DIBasicType *BT) {
1762  DebugLocStream::EntryBuilder Entry(List, Begin, End);
1763  BufferByteStreamer Streamer = Entry.getStreamer();
1764  DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer);
1765  const DebugLocEntry::Value &Value = Values[0];
1766  if (Value.isFragment()) {
1767  // Emit all fragments that belong to the same variable and range.
1769  return P.isFragment();
1770  }) && "all values are expected to be fragments");
1771  assert(std::is_sorted(Values.begin(), Values.end()) &&
1772  "fragments are expected to be sorted");
1773 
1774  for (auto Fragment : Values)
1775  emitDebugLocValue(AP, BT, Fragment, DwarfExpr);
1776 
1777  } else {
1778  assert(Values.size() == 1 && "only fragments may have >1 value");
1779  emitDebugLocValue(AP, BT, Value, DwarfExpr);
1780  }
1781  DwarfExpr.finalize();
1782 }
1783 
1785  // Emit the size.
1786  Asm->OutStreamer->AddComment("Loc expr size");
1787  Asm->emitInt16(DebugLocs.getBytes(Entry).size());
1788 
1789  // Emit the entry.
1790  APByteStreamer Streamer(*Asm);
1791  emitDebugLocEntry(Streamer, Entry);
1792 }
1793 
1794 // Emit locations into the debug loc section.
1795 void DwarfDebug::emitDebugLoc() {
1796  if (DebugLocs.getLists().empty())
1797  return;
1798 
1799  // Start the dwarf loc section.
1800  Asm->OutStreamer->SwitchSection(
1802  unsigned char Size = Asm->MAI->getCodePointerSize();
1803  for (const auto &List : DebugLocs.getLists()) {
1804  Asm->OutStreamer->EmitLabel(List.Label);
1805  const DwarfCompileUnit *CU = List.CU;
1806  for (const auto &Entry : DebugLocs.getEntries(List)) {
1807  // Set up the range. This range is relative to the entry point of the
1808  // compile unit. This is a hard coded 0 for low_pc when we're emitting
1809  // ranges, or the DW_AT_low_pc on the compile unit otherwise.
1810  if (auto *Base = CU->getBaseAddress()) {
1811  Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1812  Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
1813  } else {
1814  Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1815  Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
1816  }
1817 
1819  }
1820  Asm->OutStreamer->EmitIntValue(0, Size);
1821  Asm->OutStreamer->EmitIntValue(0, Size);
1822  }
1823 }
1824 
1825 void DwarfDebug::emitDebugLocDWO() {
1826  Asm->OutStreamer->SwitchSection(
1828  for (const auto &List : DebugLocs.getLists()) {
1829  Asm->OutStreamer->EmitLabel(List.Label);
1830  for (const auto &Entry : DebugLocs.getEntries(List)) {
1831  // Just always use start_length for now - at least that's one address
1832  // rather than two. We could get fancier and try to, say, reuse an
1833  // address we know we've emitted elsewhere (the start of the function?
1834  // The start of the CU or CU subrange that encloses this range?)
1836  unsigned idx = AddrPool.getIndex(Entry.BeginSym);
1837  Asm->EmitULEB128(idx);
1838  Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
1839 
1841  }
1843  }
1844 }
1845 
1846 struct ArangeSpan {
1847  const MCSymbol *Start, *End;
1848 };
1849 
1850 // Emit a debug aranges section, containing a CU lookup for any
1851 // address we can tie back to a CU.
1852 void DwarfDebug::emitDebugARanges() {
1853  // Provides a unique id per text section.
1855 
1856  // Filter labels by section.
1857  for (const SymbolCU &SCU : ArangeLabels) {
1858  if (SCU.Sym->isInSection()) {
1859  // Make a note of this symbol and it's section.
1860  MCSection *Section = &SCU.Sym->getSection();
1861  if (!Section->getKind().isMetadata())
1862  SectionMap[Section].push_back(SCU);
1863  } else {
1864  // Some symbols (e.g. common/bss on mach-o) can have no section but still
1865  // appear in the output. This sucks as we rely on sections to build
1866  // arange spans. We can do it without, but it's icky.
1867  SectionMap[nullptr].push_back(SCU);
1868  }
1869  }
1870 
1872 
1873  for (auto &I : SectionMap) {
1874  MCSection *Section = I.first;
1875  SmallVector<SymbolCU, 8> &List = I.second;
1876  if (List.size() < 1)
1877  continue;
1878 
1879  // If we have no section (e.g. common), just write out
1880  // individual spans for each symbol.
1881  if (!Section) {
1882  for (const SymbolCU &Cur : List) {
1883  ArangeSpan Span;
1884  Span.Start = Cur.Sym;
1885  Span.End = nullptr;
1886  assert(Cur.CU);
1887  Spans[Cur.CU].push_back(Span);
1888  }
1889  continue;
1890  }
1891 
1892  // Sort the symbols by offset within the section.
1893  std::stable_sort(
1894  List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) {
1895  unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1896  unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
1897 
1898  // Symbols with no order assigned should be placed at the end.
1899  // (e.g. section end labels)
1900  if (IA == 0)
1901  return false;
1902  if (IB == 0)
1903  return true;
1904  return IA < IB;
1905  });
1906 
1907  // Insert a final terminator.
1908  List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
1909 
1910  // Build spans between each label.
1911  const MCSymbol *StartSym = List[0].Sym;
1912  for (size_t n = 1, e = List.size(); n < e; n++) {
1913  const SymbolCU &Prev = List[n - 1];
1914  const SymbolCU &Cur = List[n];
1915 
1916  // Try and build the longest span we can within the same CU.
1917  if (Cur.CU != Prev.CU) {
1918  ArangeSpan Span;
1919  Span.Start = StartSym;
1920  Span.End = Cur.Sym;
1921  assert(Prev.CU);
1922  Spans[Prev.CU].push_back(Span);
1923  StartSym = Cur.Sym;
1924  }
1925  }
1926  }
1927 
1928  // Start the dwarf aranges section.
1929  Asm->OutStreamer->SwitchSection(
1931 
1932  unsigned PtrSize = Asm->MAI->getCodePointerSize();
1933 
1934  // Build a list of CUs used.
1935  std::vector<DwarfCompileUnit *> CUs;
1936  for (const auto &it : Spans) {
1937  DwarfCompileUnit *CU = it.first;
1938  CUs.push_back(CU);
1939  }
1940 
1941  // Sort the CU list (again, to ensure consistent output order).
1942  llvm::sort(CUs.begin(), CUs.end(),
1943  [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
1944  return A->getUniqueID() < B->getUniqueID();
1945  });
1946 
1947  // Emit an arange table for each CU we used.
1948  for (DwarfCompileUnit *CU : CUs) {
1949  std::vector<ArangeSpan> &List = Spans[CU];
1950 
1951  // Describe the skeleton CU's offset and length, not the dwo file's.
1952  if (auto *Skel = CU->getSkeleton())
1953  CU = Skel;
1954 
1955  // Emit size of content not including length itself.
1956  unsigned ContentSize =
1957  sizeof(int16_t) + // DWARF ARange version number
1958  sizeof(int32_t) + // Offset of CU in the .debug_info section
1959  sizeof(int8_t) + // Pointer Size (in bytes)
1960  sizeof(int8_t); // Segment Size (in bytes)
1961 
1962  unsigned TupleSize = PtrSize * 2;
1963 
1964  // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
1965  unsigned Padding =
1966  OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
1967 
1968  ContentSize += Padding;
1969  ContentSize += (List.size() + 1) * TupleSize;
1970 
1971  // For each compile unit, write the list of spans it covers.
1972  Asm->OutStreamer->AddComment("Length of ARange Set");
1973  Asm->emitInt32(ContentSize);
1974  Asm->OutStreamer->AddComment("DWARF Arange version number");
1976  Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
1977  emitSectionReference(*CU);
1978  Asm->OutStreamer->AddComment("Address Size (in bytes)");
1979  Asm->emitInt8(PtrSize);
1980  Asm->OutStreamer->AddComment("Segment Size (in bytes)");
1981  Asm->emitInt8(0);
1982 
1983  Asm->OutStreamer->emitFill(Padding, 0xff);
1984 
1985  for (const ArangeSpan &Span : List) {
1986  Asm->EmitLabelReference(Span.Start, PtrSize);
1987 
1988  // Calculate the size as being from the span start to it's end.
1989  if (Span.End) {
1990  Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
1991  } else {
1992  // For symbols without an end marker (e.g. common), we
1993  // write a single arange entry containing just that one symbol.
1994  uint64_t Size = SymSize[Span.Start];
1995  if (Size == 0)
1996  Size = 1;
1997 
1998  Asm->OutStreamer->EmitIntValue(Size, PtrSize);
1999  }
2000  }
2001 
2002  Asm->OutStreamer->AddComment("ARange terminator");
2003  Asm->OutStreamer->EmitIntValue(0, PtrSize);
2004  Asm->OutStreamer->EmitIntValue(0, PtrSize);
2005  }
2006 }
2007 
2008 /// Emit a single range list. We handle both DWARF v5 and earlier.
2010  const RangeSpanList &List) {
2011 
2012  auto DwarfVersion = CU->getDwarfVersion();
2013  // Emit our symbol so we can find the beginning of the range.
2014  Asm->OutStreamer->EmitLabel(List.getSym());
2015  // Gather all the ranges that apply to the same section so they can share
2016  // a base address entry.
2018  // Size for our labels.
2019  auto Size = Asm->MAI->getCodePointerSize();
2020 
2021  for (const RangeSpan &Range : List.getRanges())
2022  SectionRanges[&Range.getStart()->getSection()].push_back(&Range);
2023 
2024  auto *CUBase = CU->getBaseAddress();
2025  bool BaseIsSet = false;
2026  for (const auto &P : SectionRanges) {
2027  // Don't bother with a base address entry if there's only one range in
2028  // this section in this range list - for example ranges for a CU will
2029  // usually consist of single regions from each of many sections
2030  // (-ffunction-sections, or just C++ inline functions) except under LTO
2031  // or optnone where there may be holes in a single CU's section
2032  // contributions.
2033  auto *Base = CUBase;
2034  if (!Base && P.second.size() > 1 &&
2035  (UseDwarfRangesBaseAddressSpecifier || DwarfVersion >= 5)) {
2036  BaseIsSet = true;
2037  // FIXME/use care: This may not be a useful base address if it's not
2038  // the lowest address/range in this object.
2039  Base = P.second.front()->getStart();
2040  if (DwarfVersion >= 5) {
2041  Asm->OutStreamer->AddComment("DW_RLE_base_address");
2042  Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_base_address, 1);
2043  } else
2044  Asm->OutStreamer->EmitIntValue(-1, Size);
2045  Asm->OutStreamer->AddComment(" base address");
2046  Asm->OutStreamer->EmitSymbolValue(Base, Size);
2047  } else if (BaseIsSet && DwarfVersion < 5) {
2048  BaseIsSet = false;
2049  assert(!Base);
2050  Asm->OutStreamer->EmitIntValue(-1, Size);
2051  Asm->OutStreamer->EmitIntValue(0, Size);
2052  }
2053 
2054  for (const auto *RS : P.second) {
2055  const MCSymbol *Begin = RS->getStart();
2056  const MCSymbol *End = RS->getEnd();
2057  assert(Begin && "Range without a begin symbol?");
2058  assert(End && "Range without an end symbol?");
2059  if (Base) {
2060  if (DwarfVersion >= 5) {
2061  // Emit DW_RLE_offset_pair when we have a base.
2062  Asm->OutStreamer->AddComment("DW_RLE_offset_pair");
2063  Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_offset_pair, 1);
2064  Asm->OutStreamer->AddComment(" starting offset");
2065  Asm->EmitLabelDifferenceAsULEB128(Begin, Base);
2066  Asm->OutStreamer->AddComment(" ending offset");
2068  } else {
2069  Asm->EmitLabelDifference(Begin, Base, Size);
2070  Asm->EmitLabelDifference(End, Base, Size);
2071  }
2072  } else if (DwarfVersion >= 5) {
2073  Asm->OutStreamer->AddComment("DW_RLE_start_length");
2074  Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_start_length, 1);
2075  Asm->OutStreamer->AddComment(" start");
2076  Asm->OutStreamer->EmitSymbolValue(Begin, Size);
2077  Asm->OutStreamer->AddComment(" length");
2078  Asm->EmitLabelDifferenceAsULEB128(End, Begin);
2079  } else {
2080  Asm->OutStreamer->EmitSymbolValue(Begin, Size);
2081  Asm->OutStreamer->EmitSymbolValue(End, Size);
2082  }
2083  }
2084  }
2085  if (DwarfVersion >= 5) {
2086  Asm->OutStreamer->AddComment("DW_RLE_end_of_list");
2087  Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_end_of_list, 1);
2088  } else {
2089  // Terminate the list with two 0 values.
2090  Asm->OutStreamer->EmitIntValue(0, Size);
2091  Asm->OutStreamer->EmitIntValue(0, Size);
2092  }
2093 }
2094 
2095 // Emit the header of a DWARF 5 range list table. Returns the symbol that
2096 // designates the end of the table for the caller to emit when the table is
2097 // complete.
2099  // The length is described by a starting label right after the length field
2100  // and an end label.
2101  MCSymbol *TableStart = Asm->createTempSymbol("debug_rnglist_table_start");
2102  MCSymbol *TableEnd = Asm->createTempSymbol("debug_rnglist_table_end");
2103  // Build the range table header, which starts with the length field.
2104  Asm->EmitLabelDifference(TableEnd, TableStart, 4);
2105  Asm->OutStreamer->EmitLabel(TableStart);
2106  // Version number (DWARF v5 and later).
2107  Asm->emitInt16(Asm->OutStreamer->getContext().getDwarfVersion());
2108  // Address size.
2109  Asm->emitInt8(Asm->MAI->getCodePointerSize());
2110  // Segment selector size.
2111  Asm->emitInt8(0);
2112 
2113  MCSymbol *RnglistTableBaseSym = Holder.getRnglistsTableBaseSym();
2114 
2115  // FIXME: Generate the offsets table and use DW_FORM_rnglistx with the
2116  // DW_AT_ranges attribute. Until then set the number of offsets to 0.
2117  Asm->emitInt32(0);
2118  Asm->OutStreamer->EmitLabel(RnglistTableBaseSym);
2119  return TableEnd;
2120 }
2121 
2122 /// Emit address ranges into the .debug_ranges section or into the DWARF v5
2123 /// .debug_rnglists section.
2124 void DwarfDebug::emitDebugRanges() {
2125  if (CUMap.empty())
2126  return;
2127 
2128  auto NoRangesPresent = [this]() {
2129  return llvm::all_of(
2130  CUMap, [](const decltype(CUMap)::const_iterator::value_type &Pair) {
2131  return Pair.second->getRangeLists().empty();
2132  });
2133  };
2134 
2135  if (!useRangesSection()) {
2136  assert(NoRangesPresent() && "No debug ranges expected.");
2137  return;
2138  }
2139 
2140  if (NoRangesPresent())
2141  return;
2142 
2143  // Start the dwarf ranges section.
2144  MCSymbol *TableEnd = nullptr;
2145  if (getDwarfVersion() >= 5) {
2146  Asm->OutStreamer->SwitchSection(
2148  TableEnd = emitRnglistsTableHeader(Asm, useSplitDwarf() ? SkeletonHolder
2149  : InfoHolder);
2150  } else
2151  Asm->OutStreamer->SwitchSection(
2153 
2154  // Grab the specific ranges for the compile units in the module.
2155  for (const auto &I : CUMap) {
2156  DwarfCompileUnit *TheCU = I.second;
2157 
2158  if (auto *Skel = TheCU->getSkeleton())
2159  TheCU = Skel;
2160 
2161  // Iterate over the misc ranges for the compile units in the module.
2162  for (const RangeSpanList &List : TheCU->getRangeLists())
2163  emitRangeList(Asm, TheCU, List);
2164  }
2165 
2166  if (TableEnd)
2167  Asm->OutStreamer->EmitLabel(TableEnd);
2168 }
2169 
2170 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
2171  for (auto *MN : Nodes) {
2172  if (auto *M = dyn_cast<DIMacro>(MN))
2173  emitMacro(*M);
2174  else if (auto *F = dyn_cast<DIMacroFile>(MN))
2175  emitMacroFile(*F, U);
2176  else
2177  llvm_unreachable("Unexpected DI type!");
2178  }
2179 }
2180 
2181 void DwarfDebug::emitMacro(DIMacro &M) {
2183  Asm->EmitULEB128(M.getLine());
2184  StringRef Name = M.getName();
2185  StringRef Value = M.getValue();
2186  Asm->OutStreamer->EmitBytes(Name);
2187  if (!Value.empty()) {
2188  // There should be one space between macro name and macro value.
2189  Asm->emitInt8(' ');
2190  Asm->OutStreamer->EmitBytes(Value);
2191  }
2192  Asm->emitInt8('\0');
2193 }
2194 
2195 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
2198  Asm->EmitULEB128(F.getLine());
2200  handleMacroNodes(F.getElements(), U);
2202 }
2203 
2204 /// Emit macros into a debug macinfo section.
2205 void DwarfDebug::emitDebugMacinfo() {
2206  if (CUMap.empty())
2207  return;
2208 
2209  // Start the dwarf macinfo section.
2210  Asm->OutStreamer->SwitchSection(
2212 
2213  for (const auto &P : CUMap) {
2214  auto &TheCU = *P.second;
2215  auto *SkCU = TheCU.getSkeleton();
2216  DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
2217  auto *CUNode = cast<DICompileUnit>(P.first);
2218  DIMacroNodeArray Macros = CUNode->getMacros();
2219  if (!Macros.empty()) {
2220  Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin());
2221  handleMacroNodes(Macros, U);
2222  }
2223  }
2224  Asm->OutStreamer->AddComment("End Of Macro List Mark");
2225  Asm->emitInt8(0);
2226 }
2227 
2228 // DWARF5 Experimental Separate Dwarf emitters.
2229 
2230 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
2231  std::unique_ptr<DwarfCompileUnit> NewU) {
2232  NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
2234 
2235  if (!CompilationDir.empty())
2236  NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2237 
2238  addGnuPubAttributes(*NewU, Die);
2239 
2240  SkeletonHolder.addUnit(std::move(NewU));
2241 }
2242 
2243 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
2244 
2245  auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
2246  CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
2247  DwarfCompileUnit &NewCU = *OwnedUnit;
2249 
2250  NewCU.initStmtList();
2251 
2253  NewCU.addStringOffsetsStart();
2254 
2255  initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
2256 
2257  return NewCU;
2258 }
2259 
2260 // Emit the .debug_info.dwo section for separated dwarf. This contains the
2261 // compile units that would normally be in debug_info.
2262 void DwarfDebug::emitDebugInfoDWO() {
2263  assert(useSplitDwarf() && "No split dwarf debug info?");
2264  // Don't emit relocations into the dwo file.
2265  InfoHolder.emitUnits(/* UseOffsets */ true);
2266 }
2267 
2268 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2269 // abbreviations for the .debug_info.dwo section.
2270 void DwarfDebug::emitDebugAbbrevDWO() {
2271  assert(useSplitDwarf() && "No split dwarf?");
2272  InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
2273 }
2274 
2275 void DwarfDebug::emitDebugLineDWO() {
2276  assert(useSplitDwarf() && "No split dwarf?");
2277  SplitTypeUnitFileTable.Emit(
2280 }
2281 
2282 void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
2283  assert(useSplitDwarf() && "No split dwarf?");
2284  InfoHolder.getStringPool().emitStringOffsetsTableHeader(
2286  InfoHolder.getStringOffsetsStartSym());
2287 }
2288 
2289 // Emit the .debug_str.dwo section for separated dwarf. This contains the
2290 // string section and is identical in format to traditional .debug_str
2291 // sections.
2292 void DwarfDebug::emitDebugStrDWO() {
2294  emitStringOffsetsTableHeaderDWO();
2295  assert(useSplitDwarf() && "No split dwarf?");
2297  InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2298  OffSec, /* UseRelativeOffsets = */ false);
2299 }
2300 
2301 // Emit DWO addresses.
2302 void DwarfDebug::emitDebugAddr() {
2303  assert(useSplitDwarf() && "No split dwarf?");
2304  AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
2305 }
2306 
2307 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
2308  if (!useSplitDwarf())
2309  return nullptr;
2310  const DICompileUnit *DIUnit = CU.getCUNode();
2311  SplitTypeUnitFileTable.maybeSetRootFile(
2312  DIUnit->getDirectory(), DIUnit->getFilename(),
2313  CU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource());
2314  return &SplitTypeUnitFileTable;
2315 }
2316 
2318  MD5 Hash;
2319  Hash.update(Identifier);
2320  // ... take the least significant 8 bytes and return those. Our MD5
2321  // implementation always returns its results in little endian, so we actually
2322  // need the "high" word.
2324  Hash.final(Result);
2325  return Result.high();
2326 }
2327 
2329  StringRef Identifier, DIE &RefDie,
2330  const DICompositeType *CTy) {
2331  // Fast path if we're building some type units and one has already used the
2332  // address pool we know we're going to throw away all this work anyway, so
2333  // don't bother building dependent types.
2334  if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
2335  return;
2336 
2337  auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
2338  if (!Ins.second) {
2339  CU.addDIETypeSignature(RefDie, Ins.first->second);
2340  return;
2341  }
2342 
2343  bool TopLevelType = TypeUnitsUnderConstruction.empty();
2344  AddrPool.resetUsedFlag();
2345 
2346  auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
2347  getDwoLineTable(CU));
2348  DwarfTypeUnit &NewTU = *OwnedUnit;
2349  DIE &UnitDie = NewTU.getUnitDie();
2350  TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
2351 
2352  NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
2353  CU.getLanguage());
2354 
2355  uint64_t Signature = makeTypeSignature(Identifier);
2356  NewTU.setTypeSignature(Signature);
2357  Ins.first->second = Signature;
2358 
2359  if (useSplitDwarf())
2360  NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
2361  else {
2362  NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2363  // Non-split type units reuse the compile unit's line table.
2364  CU.applyStmtList(UnitDie);
2365  }
2366 
2367  // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
2368  // units.
2370  NewTU.addStringOffsetsStart();
2371 
2372  NewTU.setType(NewTU.createTypeDIE(CTy));
2373 
2374  if (TopLevelType) {
2375  auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2376  TypeUnitsUnderConstruction.clear();
2377 
2378  // Types referencing entries in the address table cannot be placed in type
2379  // units.
2380  if (AddrPool.hasBeenUsed()) {
2381 
2382  // Remove all the types built while building this type.
2383  // This is pessimistic as some of these types might not be dependent on
2384  // the type that used an address.
2385  for (const auto &TU : TypeUnitsToAdd)
2386  TypeSignatures.erase(TU.second);
2387 
2388  // Construct this type in the CU directly.
2389  // This is inefficient because all the dependent types will be rebuilt
2390  // from scratch, including building them in type units, discovering that
2391  // they depend on addresses, throwing them out and rebuilding them.
2392  CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
2393  return;
2394  }
2395 
2396  // If the type wasn't dependent on fission addresses, finish adding the type
2397  // and all its dependent types.
2398  for (auto &TU : TypeUnitsToAdd) {
2399  InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
2400  InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
2401  }
2402  }
2403  CU.addDIETypeSignature(RefDie, Signature);
2404 }
2405 
2406 // Add the Name along with its companion DIE to the appropriate accelerator
2407 // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
2408 // AccelTableKind::Apple, we use the table we got as an argument). If
2409 // accelerator tables are disabled, this function does nothing.
2410 template <typename DataT>
2411 void DwarfDebug::addAccelNameImpl(AccelTable<DataT> &AppleAccel, StringRef Name,
2412  const DIE &Die) {
2414  return;
2415 
2416  DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2418  Holder.getStringPool().getEntry(*Asm, Name);
2419 
2420  switch (getAccelTableKind()) {
2421  case AccelTableKind::Apple:
2422  AppleAccel.addName(Ref, Die);
2423  break;
2424  case AccelTableKind::Dwarf:
2425  AccelDebugNames.addName(Ref, Die);
2426  break;
2428  llvm_unreachable("Default should have already been resolved.");
2429  case AccelTableKind::None:
2430  llvm_unreachable("None handled above");
2431  }
2432 }
2433 
2434 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
2435  addAccelNameImpl(AccelNames, Name, Die);
2436 }
2437 
2438 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2439  // ObjC names go only into the Apple accelerator tables.
2441  addAccelNameImpl(AccelObjC, Name, Die);
2442 }
2443 
2445  addAccelNameImpl(AccelNamespace, Name, Die);
2446 }
2447 
2448 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2449  addAccelNameImpl(AccelTypes, Name, Die);
2450 }
2451 
2453  return Asm->OutStreamer->getContext().getDwarfVersion();
2454 }
const StringMap< const DIE * > & getGlobalTypes() const
const DICompileUnit * getCUNode() const
Definition: DwarfUnit.h:113
void emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry)
Emit the location for a debug loc entry, including the size header.
debug_compile_units_iterator debug_compile_units_end() const
Definition: Module.h:759
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:209
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
const SmallVectorImpl< RangeSpanList > & getRangeLists() const
getRangeLists - Get the vector of range lists.
An object containing the capability of hashing and adding hash attributes onto a DIE.
Definition: DIEHash.h:28
const DILocalScope * getScopeNode() const
Definition: LexicalScopes.h:64
Builder for DebugLocStream lists.
MCSection * getDwarfStrOffSection() const
void addFlag(DIE &Die, dwarf::Attribute Attribute)
Add a flag that is true to the DIE.
Definition: DwarfUnit.cpp:200
SectionKind getKind() const
Definition: MCSection.h:102
static cl::opt< bool > SplitDwarfCrossCuReferences("split-dwarf-cross-cu-references", cl::Hidden, cl::desc("Enable cross-cu references in DWO files"), cl::init(false))
void addUnit(std::unique_ptr< DwarfCompileUnit > U)
Add a unit to the list of CUs.
Definition: DwarfFile.cpp:27
static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, const DIE *Die)
computeIndexValue - Compute the gdb index value for the DIE and CU.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
void emitAppleAccelTable(AsmPrinter *Asm, AccelTable< DataT > &Contents, StringRef Prefix, const MCSymbol *SecBegin)
Emit an Apple Accelerator Table consisting of entries in the specified AccelTable.
Definition: AccelTable.h:305
void addMMIEntry(const DbgVariable &V)
Definition: DwarfDebug.cpp:258
static cl::opt< bool > DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, cl::desc("Disable debug info printing"))
MCSection * getDwarfAccelObjCSection() const
This struct describes location entries emitted in the .debug_loc section.
Definition: DebugLocEntry.h:26
const MCSymbol * getBaseAddress() const
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
bool hasDebugInfo() const
Returns true if valid debug info is present.
#define DWARF2_FLAG_PROLOGUE_END
Definition: MCDwarf.h:82
MCSection * getDwarfStrSection() const
MCTargetOptions MCOptions
Machine level options.
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
static SmallVectorImpl< DwarfCompileUnit::GlobalExpr > & sortGlobalExprs(SmallVectorImpl< DwarfCompileUnit::GlobalExpr > &GVEs)
Sort and unique GVEs by comparing their fragment offset.
Definition: DwarfDebug.cpp:615
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool isAbstractScope() const
Definition: LexicalScopes.h:65
void addUnsignedConstant(uint64_t Value)
Emit an unsigned constant.
unsigned getRuntimeVersion() const
MCSection * getDwarfLocSection() 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
void setMemoryLocationKind()
Lock this down to become a memory location description.
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:588
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
DWARF v5 .debug_names.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
const DIType * getType() const
Definition: DwarfDebug.cpp:195
AccelTableKind getAccelTableKind() const
Returns what kind (if any) of accelerator tables to emit.
Definition: DwarfDebug.h:566
DIFile * getFile() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:285
std::string SplitDwarfFile
uint16_t getDwarfVersion() const
unsigned getReg() const
unsigned getReg() const
getReg - Returns the register number.
MCSection * getDwarfAccelNamesSection() const
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:199
static cl::opt< AccelTableKind > AccelTables("accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."), cl::values(clEnumValN(AccelTableKind::Default, "Default", "Default for platform"), clEnumValN(AccelTableKind::None, "Disable", "Disabled."), clEnumValN(AccelTableKind::Apple, "Apple", "Apple"), clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")), cl::init(AccelTableKind::Default))
Base class containing the logic for constructing DWARF expressions independently of whether they are ...
MD5::MD5Result * getMD5AsBytes(const DIFile *File) const
If the File has an MD5 checksum, return it as an MD5Result allocated in the MCContext.
Definition: DwarfUnit.cpp:280
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.
unsigned getDebugSectionOffset() const
Definition: DIE.h:829
static cl::opt< bool > NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden, cl::desc("Disable emission .debug_ranges section."), cl::init(false))
void emitDwarfSymbolReference(const MCSymbol *Label, bool ForceOffset=false) const
Emit a reference to a symbol for use in dwarf.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
unsigned getLine() const
Definition: DebugLoc.cpp:26
uint64_t getDWOId() const
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:921
void addName(DwarfStringPoolEntryRef Name, Types &&... Args)
Definition: AccelTable.h:209
static const char *const DbgTimerName
Definition: DwarfDebug.cpp:167
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:862
F(f)
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:96
MCSection * getDwarfARangesSection() const
StringRef getProducer() const
bool isMetaInstruction() const
Return true if this instruction doesn&#39;t produce any output in the form of executable instructions...
Definition: MachineInstr.h:918
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:45
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
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:1715
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we&#39;re processing.
VariableDbgInfoMapTy & getVariableDbgInfo()
Tagged DWARF-like metadata node.
DebuggerKind
Identify a debugger for "tuning" the debug info.
Definition: TargetOptions.h:92
void addAccelNamespace(StringRef Name, const DIE &Die)
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support...
Definition: DwarfDebug.h:574
void initializeDbgValue(const MachineInstr *DbgValue)
Initialize from a DBG_VALUE instruction.
Definition: DwarfDebug.h:110
static const char *const DbgTimerDescription
Definition: DwarfDebug.cpp:168
static cl::opt< bool > GenerateARangeSection("generate-arange-section", cl::Hidden, cl::desc("Generate dwarf aranges"), cl::init(false))
MCSection * getDwarfRnglistsSection() const
MCSection * getDwarfPubNamesSection() const
virtual void EmitULEB128(uint64_t DWord, const Twine &Comment="")=0
void attachRangesOrLowHighPC(DIE &D, SmallVector< RangeSpan, 2 > Ranges)
DIScope * getScope() const
static MCSymbol * emitRnglistsTableHeader(AsmPrinter *Asm, DwarfFile &Holder)
MCSymbol * getRnglistsTableBaseSym() const
Definition: DwarfFile.h:118
DebugLoc PrevInstLoc
Previous instruction&#39;s location information.
StringRef getSplitDebugFilename() const
#define DWARF2_FLAG_IS_STMT
Definition: MCDwarf.h:80
static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, const DebugLocEntry::Value &Value, DwarfExpression &DwarfExpr)
StringRef getFlags() const
MCSection * getDwarfMacinfoSection() const
LexicalScope * getOrCreateAbstractScope(const DILocalScope *Scope)
getOrCreateAbstractScope - Find or create an abstract lexical scope.
MCSection * getDwarfLocDWOSection() const
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.
unsigned getTag() const
const ConstantFP * getFPImm() const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:314
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
This class is basically a combination of TimeRegion and Timer.
Definition: Timer.h:161
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:204
String pool entry reference.
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
The access may reference the value stored in memory.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:935
static StringRef getName(Value *V)
bool isLexicalScopeDIENull(LexicalScope *Scope)
A helper function to check whether the DIE for a given Scope is going to be null. ...
Definition: DwarfDebug.cpp:453
void constructContainingTypeDIEs()
Construct DIEs for types that contain vtables.
Definition: DwarfUnit.cpp:1489
void setDWOId(uint64_t DwoId)
virtual void EmitInt8(uint8_t Byte, const Twine &Comment="")=0
DenseMap< LexicalScope *, ScopeVars > & getScopeVariables()
Definition: DwarfFile.h:125
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
AccelTableKind
The kind of accelerator tables we should emit.
Definition: DwarfDebug.h:196
#define clEnumVal(ENUMVAL, DESC)
Definition: CommandLine.h:603
StringRef getFilename() const
DwarfExpression implementation for .debug_loc entries.
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:773
This file implements a class to represent arbitrary precision integral constant values and operations...
DIE * getOrCreateTypeDIE(const MDNode *TyNode)
Find existing DIE or create new DIE for the given type.
Definition: DwarfUnit.cpp:733
const SmallVectorImpl< RangeSpan > & getRanges() const
Definition: DwarfUnit.h:61
Subprogram description.
Describes an entry of the various gnu_pub* debug sections.
Definition: Dwarf.h:557
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
unsigned getUniqueID() const
~DwarfDebug() override
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:208
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...
DenseMap< const MDNode *, DIE * > & getAbstractSPDies()
Definition: DwarfFile.h:129
This class is used to track local variable information.
Definition: DwarfDebug.h:76
#define T
void setBaseAddress(const MCSymbol *Base)
static AccelTableKind computeAccelTableKind(unsigned DwarfVersion, bool GenerateTypeUnits, DebuggerKind Tuning, const Triple &TT)
Definition: DwarfDebug.cpp:292
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
DIMacroNodeArray getElements() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
.apple_names, .apple_namespaces, .apple_types, .apple_objc.
bool MergeValues(const DebugLocEntry &Next)
If this and Next are describing different pieces of the same variable, merge them by appending Next&#39;s...
Definition: DwarfDebug.cpp:996
static Optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
debug_compile_units_iterator debug_compile_units_begin() const
Definition: Module.h:754
LexicalScope * findLexicalScope(const DILocation *DL)
findLexicalScope - Find lexical scope, either regular or inlined, for the given DebugLoc.
const SmallVectorImpl< std::unique_ptr< DwarfCompileUnit > > & getUnits()
Definition: DwarfFile.h:77
static cl::opt< DefaultOnOff > DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden, cl::desc("Use inlined strings rather than string section."), cl::values(clEnumVal(Default, "Default for platform"), clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), cl::init(Default))
MCSection * getDwarfAccelTypesSection() const
Debug location.
DIE & addChild(DIE *Child)
Add a child to the DIE.
Definition: DIE.h:769
static cl::opt< bool > GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, cl::desc("Generate DWARF4 type units."), cl::init(false))
void EmitLabelDifferenceAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) const
Emit something like ".uleb128 Hi-Lo".
static cl::opt< bool > UseDwarfRangesBaseAddressSpecifier("use-dwarf-ranges-base-address-specifier", cl::Hidden, cl::desc("Use base address specifiers in debug_ranges"), cl::init(false))
MCSection * getDwarfAbbrevSection() const
void resolve()
Resolve a unique, unresolved node.
Definition: Metadata.cpp:576
MCSection * getDwarfAbbrevDWOSection() const
LexicalScope * findAbstractScope(const DILocalScope *N)
findAbstractScope - Find an abstract scope or return null.
static void forBothCUs(DwarfCompileUnit &CU, Func F)
Definition: DwarfDebug.cpp:470
MCSymbol * getStringOffsetsStartSym() const
Definition: DwarfFile.h:114
MCSection * getDwarfGnuPubTypesSection() const
DebuggerKind DebuggerTuning
Which debugger to tune for.
const MCSymbol * Start
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:112
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind)
Definition: Dwarf.cpp:510
void endModule() override
Emit all Dwarf sections that should come after the content.
Definition: DwarfDebug.cpp:849
DwarfCompileUnit * CU
Definition: DwarfDebug.h:192
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MCSymbol * getSym() const
Definition: DwarfUnit.h:60
bool tuneForLLDB() const
Definition: DwarfDebug.h:640
const MachineBasicBlock * PrevInstBB
LinkageNameOption
Definition: DwarfDebug.cpp:149
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:84
static void getObjCClassCategory(StringRef In, StringRef &Class, StringRef &Category)
Definition: DwarfDebug.cpp:407
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))
MCSection * getDwarfAddrSection() const
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1508
const DILocation * getInlinedAt() const
Definition: LexicalScopes.h:63
static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI)
Definition: DwarfDebug.cpp:970
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:630
const DIExpression * getExpression() const
Definition: DebugLocEntry.h:77
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
This file contains the declarations for the subclasses of Constant, which represent the different fla...
A structured debug information entry.
Definition: DIE.h:662
AsmPrinter * Asm
Target of debug info emission.
DIE & getUnitDie()
Definition: DIE.h:834
ArrayRef< char > getBytes(const Entry &E) const
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:81
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier, DIE &Die, const DICompositeType *CTy)
Add a DIE to the set of types that we&#39;re going to pull into type units.
unsigned getOrCreateSourceID(const DIFile *File) override
Look up the source ID for the given file.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:928
DIImportedEntityArray getImportedEntities() const
MCSection * getDwarfLineDWOSection() const
bool isPS4CPU() const
Tests whether the target is the PS4 CPU.
Definition: Triple.h:608
void addAccelType(StringRef Name, const DIE &Die, char Flags)
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))
DwarfDebug(AsmPrinter *A, Module *M)
Definition: DwarfDebug.cpp:315
void EmitLabelReference(const MCSymbol *Label, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label" where the size in bytes of the directive is specified by Size and L...
Definition: AsmPrinter.h:491
const Triple & getTargetTriple() const
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:49
bool useSegmentedStringOffsetsTable() const
Returns whether to generate a string offsets table with (possibly shared) contributions from each CU ...
Definition: DwarfDebug.h:582
ArrayRef< Entry > getEntries(const List &L) const
StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage)
Definition: Dwarf.cpp:533
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:85
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:961
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:598
GDBIndexEntryLinkage Linkage
Definition: Dwarf.h:559
MDNode * getScope() const
Definition: DebugLoc.cpp:36
A pair of GlobalVariable and DIExpression.
void emitDWARF5AccelTable(AsmPrinter *Asm, AccelTable< DWARF5AccelTableData > &Contents, const DwarfDebug &DD, ArrayRef< std::unique_ptr< DwarfCompileUnit >> CUs)
Definition: AccelTable.cpp:552
Helper used to pair up a symbol and its DWARF compile unit.
Definition: DwarfDebug.h:188
StringRef getDirectory() const
iterator erase(const_iterator CI)
Definition: SmallVector.h:445
bool useAllLinkageNames() const
Returns whether we should emit all DW_AT_[MIPS_]linkage_name.
Definition: DwarfDebug.h:533
unsigned getLine() const
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...
size_t size() const
Definition: SmallVector.h:53
unsigned getDwarfVersion() const
Returns the Dwarf Version by checking module flags.
Definition: Module.cpp:453
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
StringRef OperationEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:138
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
static const char *const DWARFGroupDescription
Definition: DwarfDebug.cpp:166
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ArrayRef< FrameIndexExpr > getFrameIndexExprs() const
Get the FI entries, sorted by fragment offset.
Definition: DwarfDebug.cpp:240
An imported module (C++ using directive or similar).
unsigned first
bool useLocSection() const
Returns whether .debug_loc section should be emitted.
Definition: DwarfDebug.h:558
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:872
DIE * constructImportedEntityDIE(const DIImportedEntity *Module)
Construct import_module DIE.
const APFloat & getValueAPF() const
Definition: Constants.h:299
void addExpression(DIExpressionCursor &&Expr, unsigned FragmentOffsetInBits=0)
Emit all remaining operations in the DIExpressionCursor.
uint16_t getLanguage() const
Definition: DwarfUnit.h:112
bool tuneForGDB() const
Definition: DwarfDebug.h:639
SmallVector< RangeSpan, 2 > takeRanges()
void constructAbstractSubprogramScopeDIE(LexicalScope *Scope)
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.
bool useSectionsAsReferences() const
Returns whether to use sections as labels rather than temp symbols.
Definition: DwarfDebug.h:553
MCSymbol * getMacroLabelBegin() const
static bool hasObjCCategory(StringRef Name)
Definition: DwarfDebug.cpp:400
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
const StringMap< const DIE * > & getGlobalNames() const
Base class for types.
void setDebugInfoAvailability(bool avail)
StringRef getName() const
unsigned getLine() const
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:827
DIFile * getFile() const
bool isDebugValue() const
Definition: MachineInstr.h:849
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment="")=0
Module.h This file contains the declarations for the Module class.
void beginModule()
Emit all Dwarf sections that should come prior to the content.
Definition: DwarfDebug.cpp:644
static ManagedStatic< CodeViewErrorCategory > Category
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
MCSymbol * getBeginSymbol()
Definition: MCSection.h:106
bool isMetadata() const
Definition: SectionKind.h:117
void finishSubprogramDefinition(const DISubprogram *SP)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
MCSection * getDwarfInfoSection() const
bool useAppleExtensionAttributes() const
Definition: DwarfDebug.h:568
void emitInt32(int Value) const
Emit a long directive and value.
MCSection * getDwarfDebugNamesSection() const
MCSection * getDwarfStrOffDWOSection() const
void addAccelName(StringRef Name, const DIE &Die)
MCSymbol * getFunctionEnd() const
Definition: AsmPrinter.h:209
int64_t getImm() const
void emitDebugLocEntry(ByteStreamer &Streamer, const DebugLocStream::Entry &Entry)
Emit an entry for the debug loc section.
DWARF expression.
void addRnglistsBase()
Add the DW_AT_rnglists_base attribute to the unit DIE.
Definition: DwarfUnit.cpp:1756
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
static void emitRangeList(AsmPrinter *Asm, DwarfCompileUnit *CU, const RangeSpanList &List)
Emit a single range list. We handle both DWARF v5 and earlier.
unsigned getSourceLanguage() const
static cl::opt< DefaultOnOff > DwarfSectionsAsReferences("dwarf-sections-as-references", cl::Hidden, cl::desc("Use sections+offset as references rather than labels."), cl::values(clEnumVal(Default, "Default for platform"), clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), cl::init(Default))
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
This file contains constants used for implementing Dwarf debug support.
Class for arbitrary precision integers.
Definition: APInt.h:69
ArrayRef< std::string > getComments(const Entry &E) const
bool fragmentsOverlap(const DIExpression *Other) const
Check if fragments overlap between this DIExpression and Other.
MCSection * getDwarfPubTypesSection() const
bool isBlockByrefStruct() const
DebugLoc PrologEndLoc
This location indicates end of function prologue and beginning of function body.
const SmallVectorImpl< RangeSpan > & getRanges() const
getRanges - Get the list of ranges for this unit.
dwarf::Tag getTag() const
Definition: DIE.h:698
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:605
A single location or constant.
Definition: DebugLocEntry.h:33
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:156
void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List, const DIBasicType *BT)
Lower this entry into a DWARF expression.
void emitStringOffsetsTableHeader(AsmPrinter &Asm, MCSection *OffsetSection, MCSymbol *StartSym)
DIE * getOrCreateContextDIE(const DIScope *Context)
Get context owner&#39;s DIE.
Definition: DwarfUnit.cpp:703
MCSection * getDwarfStrDWOSection() const
Representation of each machine instruction.
Definition: MachineInstr.h:60
static StringRef getObjCMethodName(StringRef In)
Definition: DwarfDebug.cpp:419
bool getSplitDebugInlining() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
void setSection(MCSection *Section)
Set the section that this DIEUnit will be emitted into.
Definition: DIE.h:815
unsigned getEncoding() const
EntryRef getEntry(AsmPrinter &Asm, StringRef Str)
Get a reference to an entry in the string pool.
void EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const
Emit something like ".long Label + Offset".
static const size_t npos
Definition: StringRef.h:51
DIE * getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, ArrayRef< GlobalExpr > GlobalExprs)
Get or create global variable DIE.
LexicalScopes - This class provides interface to collect and use lexical scoping information from mac...
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
bool useRangesSection() const
Returns whether ranges section should be emitted.
Definition: DwarfDebug.h:550
bool isBlockByrefVariable() const
Definition: DwarfDebug.cpp:190
TargetOptions Options
Definition: TargetMachine.h:98
void endFunctionImpl(const MachineFunction *MF) override
Gather and emit post-function debug information.
const NodeList & List
Definition: RDFGraph.cpp:210
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
iterator_range< debug_compile_units_iterator > debug_compile_units() const
Return an iterator for all DICompileUnits listed in this Module&#39;s llvm.dbg.cu named metadata node and...
Definition: Module.h:767
bool shareAcrossDWOCUs() const
Definition: DwarfDebug.cpp:477
MCSection * getDwarfGnuPubNamesSection() const
Definition: MD5.h:41
Optional< StringRef > getSource() const
void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) const
Emit something like ".long Hi-Lo" where the size in bytes of the directive is specified by Size and H...
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.
unsigned getMacinfoType() const
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
StringRef getValue() const
DwarfCompileUnit * getSkeleton() const
void skippedNonDebugFunction() override
const Module * getModule() const
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:80
static cl::opt< bool > NoDwarfPubSections("no-dwarf-pub-sections", cl::Hidden, cl::desc("Disable emission of DWARF pub sections."), cl::init(false))
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:181
bool isReg() const
isReg - Tests if this is a MO_Register operand.
uint16_t getDwarfVersion() const
Definition: DwarfUnit.h:115
void addDIETypeSignature(DIE &Die, uint64_t Signature)
Add a type&#39;s DW_AT_signature and set the declaration flag.
Definition: DwarfUnit.cpp:330
DIEValue findAttribute(dwarf::Attribute Attribute) const
Find a value in the DIE with the attribute given.
Definition: DIE.cpp:222
ArrayRef< LexicalScope * > getAbstractScopesList() const
getAbstractScopesList - Return a reference to list of abstract scopes.
unsigned getCol() const
Definition: DebugLoc.cpp:31
void addSubprogramNames(const DISubprogram *SP, DIE &Die)
Definition: DwarfDebug.cpp:424
const MCSymbol * End
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition: MCAsmInfo.h:396
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...
MCSymbol * getLabelBeforeInsn(const MachineInstr *MI)
Return Label preceding the instruction.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSection * getDwarfAccelNamespaceSection() const
static const char *const DWARFGroupName
Definition: DwarfDebug.cpp:165
void emitInt8(int Value) const
Emit a byte directive and value.
void finishVariableDefinition(const DbgVariable &Var)
LLVM Value Representation.
Definition: Value.h:73
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
void EmitULEB128(uint64_t Value, const char *Desc=nullptr) const
Emit the specified unsigned leb128 value.
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:727
DbgVariable * getExistingAbstractVariable(InlinedVariable IV, const DILocalVariable *&Cleansed)
uint16_t getDwarfVersion() const
Returns the Dwarf Version.
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:77
GDBIndexEntryLinkage
Definition: Dwarf.h:405
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:209
MachineModuleInfo * MMI
Collected machine module information.
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:700
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
DefaultOnOff
Definition: DwarfDebug.cpp:106
bool isNVPTX() const
Tests whether the target is NVPTX (32- or 64-bit).
Definition: Triple.h:644
iterator_range< global_iterator > globals()
Definition: Module.h:573
IRTranslator LLVM IR MI
void addAccelObjC(StringRef Name, const DIE &Die)
MachineLocation getLoc() const
Definition: DebugLocEntry.h:75
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
APInt bitcastToAPInt() const
Definition: APFloat.h:1094
void emitUnits(bool UseOffsets)
Emit all of the units to the section listed with the given abbreviation section.
Definition: DwarfFile.cpp:33
MCSection * getDwarfRangesSection() const
bool tuneForSCE() const
Definition: DwarfDebug.h:641
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:25
uint64_t high() const
Definition: MD5.h:72
static bool isObjCClass(StringRef Name)
Definition: DwarfDebug.cpp:396
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
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
void createAbstractVariable(const DILocalVariable *Var, LexicalScope *Scope)
Builder for DebugLocStream entries.
#define LLVM_DEBUG(X)
Definition: Debug.h:119
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:316
bool isFragment() const
Return whether this is a piece of an aggregate variable.
const ConstantInt * getCImm() const
MCSymbol * getLabelBegin() const
void addValues(ArrayRef< DebugLocEntry::Value > Vals)
Root of the metadata hierarchy.
Definition: Metadata.h:58
MCSymbol * createTempSymbol(const Twine &Name) const
bool addMachineRegExpression(const TargetRegisterInfo &TRI, DIExpressionCursor &Expr, unsigned MachineReg, unsigned FragmentOffsetInBits=0)
Emit a machine register location.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
Definition: MachineInstr.h:197
const ConstantFP * getConstantFP() const
Definition: DebugLocEntry.h:73
ArrayRef< List > getLists() const
bool empty()
empty - Return true if there is any lexical scope information available.
LexicalScope * getCurrentFunctionScope() const
getCurrentFunctionScope - Return lexical scope for the current function.
void emitInt16(int Value) const
Emit a short directive and value.
StringRef getName() const
Basic type, like &#39;int&#39; or &#39;float&#39;.
const MCSymbol * Sym
Definition: DwarfDebug.h:191
unsigned getIndex(const MCSymbol *Sym, bool TLS=false)
Returns the index into the address pool with the given label/symbol.
Definition: AddressPool.cpp:20