1 //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains support for writing dwarf debug info into asm files.
11 //===----------------------------------------------------------------------===//
13 #include "DwarfDebug.h"
14 #include "ByteStreamer.h"
16 #include "DwarfCompileUnit.h"
17 #include "DwarfExpression.h"
18 #include "DwarfUnit.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DIE.h"
25 #include "llvm/CodeGen/LexicalScopes.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/TargetInstrInfo.h"
31 #include "llvm/CodeGen/TargetLowering.h"
32 #include "llvm/CodeGen/TargetRegisterInfo.h"
33 #include "llvm/CodeGen/TargetSubtargetInfo.h"
34 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
35 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/Function.h"
38 #include "llvm/IR/GlobalVariable.h"
39 #include "llvm/IR/Module.h"
40 #include "llvm/MC/MCAsmInfo.h"
41 #include "llvm/MC/MCContext.h"
42 #include "llvm/MC/MCSection.h"
43 #include "llvm/MC/MCStreamer.h"
44 #include "llvm/MC/MCSymbol.h"
45 #include "llvm/MC/MCTargetOptions.h"
46 #include "llvm/MC/MachineLocation.h"
47 #include "llvm/MC/SectionKind.h"
48 #include "llvm/Support/Casting.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Debug.h"
51 #include "llvm/Support/ErrorHandling.h"
52 #include "llvm/Support/MD5.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include "llvm/Target/TargetLoweringObjectFile.h"
55 #include "llvm/Target/TargetMachine.h"
56 #include "llvm/TargetParser/Triple.h"
65 #define DEBUG_TYPE "dwarfdebug"
67 STATISTIC(NumCSParams, "Number of dbg call site params created");
69 static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier(
70 "use-dwarf-ranges-base-address-specifier", cl::Hidden,
71 cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
73 static cl::opt<bool> GenerateARangeSection("generate-arange-section",
75 cl::desc("Generate dwarf aranges"),
79 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
80 cl::desc("Generate DWARF4 type units."),
83 static cl::opt<bool> SplitDwarfCrossCuReferences(
84 "split-dwarf-cross-cu-references", cl::Hidden,
85 cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
87 enum DefaultOnOff { Default, Enable, Disable };
89 static cl::opt<DefaultOnOff> UnknownLocations(
90 "use-unknown-locations", cl::Hidden,
91 cl::desc("Make an absence of debug location information explicit."),
92 cl::values(clEnumVal(Default, "At top of block or after label"),
93 clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
96 static cl::opt<AccelTableKind> AccelTables(
97 "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."),
98 cl::values(clEnumValN(AccelTableKind::Default, "Default",
99 "Default for platform"),
100 clEnumValN(AccelTableKind::None, "Disable", "Disabled."),
101 clEnumValN(AccelTableKind::Apple, "Apple", "Apple"),
102 clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")),
103 cl::init(AccelTableKind::Default));
105 static cl::opt<DefaultOnOff>
106 DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden,
107 cl::desc("Use inlined strings rather than string section."),
108 cl::values(clEnumVal(Default, "Default for platform"),
109 clEnumVal(Enable, "Enabled"),
110 clEnumVal(Disable, "Disabled")),
114 NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden,
115 cl::desc("Disable emission .debug_ranges section."),
118 static cl::opt<DefaultOnOff> DwarfSectionsAsReferences(
119 "dwarf-sections-as-references", cl::Hidden,
120 cl::desc("Use sections+offset as references rather than labels."),
121 cl::values(clEnumVal(Default, "Default for platform"),
122 clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
126 UseGNUDebugMacro("use-gnu-debug-macro", cl::Hidden,
127 cl::desc("Emit the GNU .debug_macro format with DWARF <5"),
130 static cl::opt<DefaultOnOff> DwarfOpConvert(
131 "dwarf-op-convert", cl::Hidden,
132 cl::desc("Enable use of the DWARFv5 DW_OP_convert operator"),
133 cl::values(clEnumVal(Default, "Default for platform"),
134 clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
137 enum LinkageNameOption {
143 static cl::opt<LinkageNameOption>
144 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
145 cl::desc("Which DWARF linkage-name attributes to emit."),
146 cl::values(clEnumValN(DefaultLinkageNames, "Default",
147 "Default for platform"),
148 clEnumValN(AllLinkageNames, "All", "All"),
149 clEnumValN(AbstractLinkageNames, "Abstract",
150 "Abstract subprograms")),
151 cl::init(DefaultLinkageNames));
153 static cl::opt<DwarfDebug::MinimizeAddrInV5> MinimizeAddrInV5Option(
154 "minimize-addr-in-v5", cl::Hidden,
155 cl::desc("Always use DW_AT_ranges in DWARFv5 whenever it could allow more "
156 "address pool entry sharing to reduce relocations/object size"),
157 cl::values(clEnumValN(DwarfDebug::MinimizeAddrInV5::Default, "Default",
158 "Default address minimization strategy"),
159 clEnumValN(DwarfDebug::MinimizeAddrInV5::Ranges, "Ranges",
160 "Use rnglists for contiguous ranges if that allows "
161 "using a pre-existing base address"),
162 clEnumValN(DwarfDebug::MinimizeAddrInV5::Expressions,
164 "Use exprloc addrx+offset expressions for any "
165 "address with a prior base address"),
166 clEnumValN(DwarfDebug::MinimizeAddrInV5::Form, "Form",
167 "Use addrx+offset extension form for any address "
168 "with a prior base address"),
169 clEnumValN(DwarfDebug::MinimizeAddrInV5::Disabled, "Disabled",
171 cl::init(DwarfDebug::MinimizeAddrInV5::Default));
173 static constexpr unsigned ULEB128PadSize = 4;
175 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
176 getActiveStreamer().emitInt8(
177 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
178 : dwarf::OperationEncodingString(Op));
181 void DebugLocDwarfExpression::emitSigned(int64_t Value) {
182 getActiveStreamer().emitSLEB128(Value, Twine(Value));
185 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
186 getActiveStreamer().emitULEB128(Value, Twine(Value));
189 void DebugLocDwarfExpression::emitData1(uint8_t Value) {
190 getActiveStreamer().emitInt8(Value, Twine(Value));
193 void DebugLocDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
194 assert(Idx < (1ULL << (ULEB128PadSize * 7)) && "Idx wont fit");
195 getActiveStreamer().emitULEB128(Idx, Twine(Idx), ULEB128PadSize);
198 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
199 llvm::Register MachineReg) {
200 // This information is not available while emitting .debug_loc entries.
204 void DebugLocDwarfExpression::enableTemporaryBuffer() {
205 assert(!IsBuffering && "Already buffering?");
207 TmpBuf = std::make_unique<TempBuffer>(OutBS.GenerateComments);
211 void DebugLocDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
213 unsigned DebugLocDwarfExpression::getTemporaryBufferSize() {
214 return TmpBuf ? TmpBuf->Bytes.size() : 0;
217 void DebugLocDwarfExpression::commitTemporaryBuffer() {
220 for (auto Byte : enumerate(TmpBuf->Bytes)) {
221 const char *Comment = (Byte.index() < TmpBuf->Comments.size())
222 ? TmpBuf->Comments[Byte.index()].c_str()
224 OutBS.emitInt8(Byte.value(), Comment);
226 TmpBuf->Bytes.clear();
227 TmpBuf->Comments.clear();
230 const DIType *DbgVariable::getType() const {
231 return getVariable()->getType();
234 /// Get .debug_loc entry for the instruction range starting at MI.
235 static DbgValueLoc getDebugLocValue(const MachineInstr *MI) {
236 const DIExpression *Expr = MI->getDebugExpression();
237 const bool IsVariadic = MI->isDebugValueList();
238 assert(MI->getNumOperands() >= 3);
239 SmallVector<DbgValueLocEntry, 4> DbgValueLocEntries;
240 for (const MachineOperand &Op : MI->debug_operands()) {
242 MachineLocation MLoc(Op.getReg(),
243 MI->isNonListDebugValue() && MI->isDebugOffsetImm());
244 DbgValueLocEntries.push_back(DbgValueLocEntry(MLoc));
245 } else if (Op.isTargetIndex()) {
246 DbgValueLocEntries.push_back(
247 DbgValueLocEntry(TargetIndexLocation(Op.getIndex(), Op.getOffset())));
248 } else if (Op.isImm())
249 DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getImm()));
250 else if (Op.isFPImm())
251 DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getFPImm()));
252 else if (Op.isCImm())
253 DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getCImm()));
255 llvm_unreachable("Unexpected debug operand in DBG_VALUE* instruction!");
257 return DbgValueLoc(Expr, DbgValueLocEntries, IsVariadic);
260 void DbgVariable::initializeDbgValue(const MachineInstr *DbgValue) {
261 assert(FrameIndexExprs.empty() && "Already initialized?");
262 assert(!ValueLoc.get() && "Already initialized?");
264 assert(getVariable() == DbgValue->getDebugVariable() && "Wrong variable");
265 assert(getInlinedAt() == DbgValue->getDebugLoc()->getInlinedAt() &&
268 ValueLoc = std::make_unique<DbgValueLoc>(getDebugLocValue(DbgValue));
269 if (auto *E = DbgValue->getDebugExpression())
270 if (E->getNumElements())
271 FrameIndexExprs.push_back({0, E});
274 ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
275 if (FrameIndexExprs.size() == 1)
276 return FrameIndexExprs;
278 assert(llvm::all_of(FrameIndexExprs,
279 [](const FrameIndexExpr &A) {
280 return A.Expr->isFragment();
282 "multiple FI expressions without DW_OP_LLVM_fragment");
283 llvm::sort(FrameIndexExprs,
284 [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
285 return A.Expr->getFragmentInfo()->OffsetInBits <
286 B.Expr->getFragmentInfo()->OffsetInBits;
289 return FrameIndexExprs;
292 void DbgVariable::addMMIEntry(const DbgVariable &V) {
293 assert(DebugLocListIndex == ~0U && !ValueLoc.get() && "not an MMI entry");
294 assert(V.DebugLocListIndex == ~0U && !V.ValueLoc.get() && "not an MMI entry");
295 assert(V.getVariable() == getVariable() && "conflicting variable");
296 assert(V.getInlinedAt() == getInlinedAt() && "conflicting inlined-at location");
298 assert(!FrameIndexExprs.empty() && "Expected an MMI entry");
299 assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry");
301 // FIXME: This logic should not be necessary anymore, as we now have proper
302 // deduplication. However, without it, we currently run into the assertion
303 // below, which means that we are likely dealing with broken input, i.e. two
304 // non-fragment entries for the same variable at different frame indices.
305 if (FrameIndexExprs.size()) {
306 auto *Expr = FrameIndexExprs.back().Expr;
307 if (!Expr || !Expr->isFragment())
311 for (const auto &FIE : V.FrameIndexExprs)
312 // Ignore duplicate entries.
313 if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) {
314 return FIE.FI == Other.FI && FIE.Expr == Other.Expr;
316 FrameIndexExprs.push_back(FIE);
318 assert((FrameIndexExprs.size() == 1 ||
319 llvm::all_of(FrameIndexExprs,
320 [](FrameIndexExpr &FIE) {
321 return FIE.Expr && FIE.Expr->isFragment();
323 "conflicting locations for variable");
326 static AccelTableKind computeAccelTableKind(unsigned DwarfVersion,
327 bool GenerateTypeUnits,
330 // Honor an explicit request.
331 if (AccelTables != AccelTableKind::Default)
334 // Accelerator tables with type units are currently not supported.
335 if (GenerateTypeUnits)
336 return AccelTableKind::None;
338 // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5
339 // always implies debug_names. For lower standard versions we use apple
340 // accelerator tables on apple platforms and debug_names elsewhere.
341 if (DwarfVersion >= 5)
342 return AccelTableKind::Dwarf;
343 if (Tuning == DebuggerKind::LLDB)
344 return TT.isOSBinFormatMachO() ? AccelTableKind::Apple
345 : AccelTableKind::Dwarf;
346 return AccelTableKind::None;
349 DwarfDebug::DwarfDebug(AsmPrinter *A)
350 : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
351 InfoHolder(A, "info_string", DIEValueAllocator),
352 SkeletonHolder(A, "skel_string", DIEValueAllocator),
353 IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
354 const Triple &TT = Asm->TM.getTargetTriple();
356 // Make sure we know our "debugger tuning". The target option takes
357 // precedence; fall back to triple-based defaults.
358 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
359 DebuggerTuning = Asm->TM.Options.DebuggerTuning;
361 DebuggerTuning = DebuggerKind::LLDB;
363 DebuggerTuning = DebuggerKind::SCE;
364 else if (TT.isOSAIX())
365 DebuggerTuning = DebuggerKind::DBX;
367 DebuggerTuning = DebuggerKind::GDB;
369 if (DwarfInlinedStrings == Default)
370 UseInlineStrings = TT.isNVPTX() || tuneForDBX();
372 UseInlineStrings = DwarfInlinedStrings == Enable;
374 UseLocSection = !TT.isNVPTX();
376 HasAppleExtensionAttributes = tuneForLLDB();
378 // Handle split DWARF.
379 HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty();
381 // SCE defaults to linkage names only for abstract subprograms.
382 if (DwarfLinkageNames == DefaultLinkageNames)
383 UseAllLinkageNames = !tuneForSCE();
385 UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
387 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
388 unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
389 : MMI->getModule()->getDwarfVersion();
390 // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2.
392 TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION);
394 bool Dwarf64 = DwarfVersion >= 3 && // DWARF64 was introduced in DWARFv3.
395 TT.isArch64Bit(); // DWARF64 requires 64-bit relocations.
398 // 1: For ELF when requested.
399 // 2: For XCOFF64: the AIX assembler will fill in debug section lengths
400 // according to the DWARF64 format for 64-bit assembly, so we must use
401 // DWARF64 in the compiler too for 64-bit mode.
403 ((Asm->TM.Options.MCOptions.Dwarf64 || MMI->getModule()->isDwarf64()) &&
404 TT.isOSBinFormatELF()) ||
405 TT.isOSBinFormatXCOFF();
407 if (!Dwarf64 && TT.isArch64Bit() && TT.isOSBinFormatXCOFF())
408 report_fatal_error("XCOFF requires DWARF64 for 64-bit mode!");
410 UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX();
412 // Use sections as references. Force for NVPTX.
413 if (DwarfSectionsAsReferences == Default)
414 UseSectionsAsReferences = TT.isNVPTX();
416 UseSectionsAsReferences = DwarfSectionsAsReferences == Enable;
418 // Don't generate type units for unsupported object file formats.
419 GenerateTypeUnits = (A->TM.getTargetTriple().isOSBinFormatELF() ||
420 A->TM.getTargetTriple().isOSBinFormatWasm()) &&
421 GenerateDwarfTypeUnits;
423 TheAccelTableKind = computeAccelTableKind(
424 DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple());
426 // Work around a GDB bug. GDB doesn't support the standard opcode;
427 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
428 // is defined as of DWARF 3.
429 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
430 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
431 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
433 UseDWARF2Bitfields = DwarfVersion < 4;
435 // The DWARF v5 string offsets table has - possibly shared - contributions
436 // from each compile and type unit each preceded by a header. The string
437 // offsets table used by the pre-DWARF v5 split-DWARF implementation uses
438 // a monolithic string offsets table without any header.
439 UseSegmentedStringOffsetsTable = DwarfVersion >= 5;
441 // Emit call-site-param debug info for GDB and LLDB, if the target supports
442 // the debug entry values feature. It can also be enabled explicitly.
443 EmitDebugEntryValues = Asm->TM.Options.ShouldEmitDebugEntryValues();
445 // It is unclear if the GCC .debug_macro extension is well-specified
446 // for split DWARF. For now, do not allow LLVM to emit it.
447 UseDebugMacroSection =
448 DwarfVersion >= 5 || (UseGNUDebugMacro && !useSplitDwarf());
449 if (DwarfOpConvert == Default)
450 EnableOpConvert = !((tuneForGDB() && useSplitDwarf()) || (tuneForLLDB() && !TT.isOSBinFormatMachO()));
452 EnableOpConvert = (DwarfOpConvert == Enable);
454 // Split DWARF would benefit object size significantly by trading reductions
455 // in address pool usage for slightly increased range list encodings.
456 if (DwarfVersion >= 5)
457 MinimizeAddr = MinimizeAddrInV5Option;
459 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
460 Asm->OutStreamer->getContext().setDwarfFormat(Dwarf64 ? dwarf::DWARF64
464 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
465 DwarfDebug::~DwarfDebug() = default;
467 static bool isObjCClass(StringRef Name) {
468 return Name.startswith("+") || Name.startswith("-");
471 static bool hasObjCCategory(StringRef Name) {
472 if (!isObjCClass(Name))
475 return Name.contains(") ");
478 static void getObjCClassCategory(StringRef In, StringRef &Class,
479 StringRef &Category) {
480 if (!hasObjCCategory(In)) {
481 Class = In.slice(In.find('[') + 1, In.find(' '));
486 Class = In.slice(In.find('[') + 1, In.find('('));
487 Category = In.slice(In.find('[') + 1, In.find(' '));
490 static StringRef getObjCMethodName(StringRef In) {
491 return In.slice(In.find(' ') + 1, In.find(']'));
494 // Add the various names to the Dwarf accelerator table names.
495 void DwarfDebug::addSubprogramNames(const DICompileUnit &CU,
496 const DISubprogram *SP, DIE &Die) {
497 if (getAccelTableKind() != AccelTableKind::Apple &&
498 CU.getNameTableKind() != DICompileUnit::DebugNameTableKind::Apple &&
499 CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None)
502 if (!SP->isDefinition())
505 if (SP->getName() != "")
506 addAccelName(CU, SP->getName(), Die);
508 // If the linkage name is different than the name, go ahead and output that as
509 // well into the name table. Only do that if we are going to actually emit
511 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() &&
512 (useAllLinkageNames() || InfoHolder.getAbstractScopeDIEs().lookup(SP)))
513 addAccelName(CU, SP->getLinkageName(), Die);
515 // If this is an Objective-C selector name add it to the ObjC accelerator
517 if (isObjCClass(SP->getName())) {
518 StringRef Class, Category;
519 getObjCClassCategory(SP->getName(), Class, Category);
520 addAccelObjC(CU, Class, Die);
522 addAccelObjC(CU, Category, Die);
523 // Also add the base method name to the name table.
524 addAccelName(CU, getObjCMethodName(SP->getName()), Die);
528 /// Check whether we should create a DIE for the given Scope, return true
529 /// if we don't create a DIE (the corresponding DIE is null).
530 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
531 if (Scope->isAbstractScope())
534 // We don't create a DIE if there is no Range.
535 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
539 if (Ranges.size() > 1)
542 // We don't create a DIE if we have a single Range and the end label
544 return !getLabelAfterInsn(Ranges.front().second);
547 template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
549 if (auto *SkelCU = CU.getSkeleton())
550 if (CU.getCUNode()->getSplitDebugInlining())
554 bool DwarfDebug::shareAcrossDWOCUs() const {
555 return SplitDwarfCrossCuReferences;
558 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU,
559 LexicalScope *Scope) {
560 assert(Scope && Scope->getScopeNode());
561 assert(Scope->isAbstractScope());
562 assert(!Scope->getInlinedAt());
564 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
566 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
567 // was inlined from another compile unit.
568 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining())
569 // Avoid building the original CU if it won't be used
570 SrcCU.constructAbstractSubprogramScopeDIE(Scope);
572 auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
573 if (auto *SkelCU = CU.getSkeleton()) {
574 (shareAcrossDWOCUs() ? CU : SrcCU)
575 .constructAbstractSubprogramScopeDIE(Scope);
576 if (CU.getCUNode()->getSplitDebugInlining())
577 SkelCU->constructAbstractSubprogramScopeDIE(Scope);
579 CU.constructAbstractSubprogramScopeDIE(Scope);
583 /// Represents a parameter whose call site value can be described by applying a
584 /// debug expression to a register in the forwarded register worklist.
585 struct FwdRegParamInfo {
586 /// The described parameter register.
589 /// Debug expression that has been built up when walking through the
590 /// instruction chain that produces the parameter's value.
591 const DIExpression *Expr;
594 /// Register worklist for finding call site values.
595 using FwdRegWorklist = MapVector<unsigned, SmallVector<FwdRegParamInfo, 2>>;
596 /// Container for the set of registers known to be clobbered on the path to a
598 using ClobberedRegSet = SmallSet<Register, 16>;
600 /// Append the expression \p Addition to \p Original and return the result.
601 static const DIExpression *combineDIExpressions(const DIExpression *Original,
602 const DIExpression *Addition) {
603 std::vector<uint64_t> Elts = Addition->getElements().vec();
604 // Avoid multiple DW_OP_stack_values.
605 if (Original->isImplicit() && Addition->isImplicit())
606 erase_value(Elts, dwarf::DW_OP_stack_value);
607 const DIExpression *CombinedExpr =
608 (Elts.size() > 0) ? DIExpression::append(Original, Elts) : Original;
612 /// Emit call site parameter entries that are described by the given value and
613 /// debug expression.
614 template <typename ValT>
615 static void finishCallSiteParams(ValT Val, const DIExpression *Expr,
616 ArrayRef<FwdRegParamInfo> DescribedParams,
618 for (auto Param : DescribedParams) {
619 bool ShouldCombineExpressions = Expr && Param.Expr->getNumElements() > 0;
621 // TODO: Entry value operations can currently not be combined with any
622 // other expressions, so we can't emit call site entries in those cases.
623 if (ShouldCombineExpressions && Expr->isEntryValue())
626 // If a parameter's call site value is produced by a chain of
627 // instructions we may have already created an expression for the
628 // parameter when walking through the instructions. Append that to the
630 const DIExpression *CombinedExpr =
631 ShouldCombineExpressions ? combineDIExpressions(Expr, Param.Expr)
633 assert((!CombinedExpr || CombinedExpr->isValid()) &&
634 "Combined debug expression is invalid");
636 DbgValueLoc DbgLocVal(CombinedExpr, DbgValueLocEntry(Val));
637 DbgCallSiteParam CSParm(Param.ParamReg, DbgLocVal);
638 Params.push_back(CSParm);
643 /// Add \p Reg to the worklist, if it's not already present, and mark that the
644 /// given parameter registers' values can (potentially) be described using
645 /// that register and an debug expression.
646 static void addToFwdRegWorklist(FwdRegWorklist &Worklist, unsigned Reg,
647 const DIExpression *Expr,
648 ArrayRef<FwdRegParamInfo> ParamsToAdd) {
649 auto I = Worklist.insert({Reg, {}});
650 auto &ParamsForFwdReg = I.first->second;
651 for (auto Param : ParamsToAdd) {
652 assert(none_of(ParamsForFwdReg,
653 [Param](const FwdRegParamInfo &D) {
654 return D.ParamReg == Param.ParamReg;
656 "Same parameter described twice by forwarding reg");
658 // If a parameter's call site value is produced by a chain of
659 // instructions we may have already created an expression for the
660 // parameter when walking through the instructions. Append that to the
662 const DIExpression *CombinedExpr = combineDIExpressions(Expr, Param.Expr);
663 ParamsForFwdReg.push_back({Param.ParamReg, CombinedExpr});
667 /// Interpret values loaded into registers by \p CurMI.
668 static void interpretValues(const MachineInstr *CurMI,
669 FwdRegWorklist &ForwardedRegWorklist,
671 ClobberedRegSet &ClobberedRegUnits) {
673 const MachineFunction *MF = CurMI->getMF();
674 const DIExpression *EmptyExpr =
675 DIExpression::get(MF->getFunction().getContext(), {});
676 const auto &TRI = *MF->getSubtarget().getRegisterInfo();
677 const auto &TII = *MF->getSubtarget().getInstrInfo();
678 const auto &TLI = *MF->getSubtarget().getTargetLowering();
680 // If an instruction defines more than one item in the worklist, we may run
681 // into situations where a worklist register's value is (potentially)
682 // described by the previous value of another register that is also defined
683 // by that instruction.
685 // This can for example occur in cases like this:
688 // $r0, $r1 = mvrr $r1, 456
689 // call @foo, $r0, $r1
691 // When describing $r1's value for the mvrr instruction, we need to make sure
692 // that we don't finalize an entry value for $r0, as that is dependent on the
693 // previous value of $r1 (123 rather than 456).
695 // In order to not have to distinguish between those cases when finalizing
696 // entry values, we simply postpone adding new parameter registers to the
697 // worklist, by first keeping them in this temporary container until the
698 // instruction has been handled.
699 FwdRegWorklist TmpWorklistItems;
701 // If the MI is an instruction defining one or more parameters' forwarding
702 // registers, add those defines.
703 ClobberedRegSet NewClobberedRegUnits;
704 auto getForwardingRegsDefinedByMI = [&](const MachineInstr &MI,
705 SmallSetVector<unsigned, 4> &Defs) {
706 if (MI.isDebugInstr())
709 for (const MachineOperand &MO : MI.all_defs()) {
710 if (MO.getReg().isPhysical()) {
711 for (auto &FwdReg : ForwardedRegWorklist)
712 if (TRI.regsOverlap(FwdReg.first, MO.getReg()))
713 Defs.insert(FwdReg.first);
714 for (MCRegUnit Unit : TRI.regunits(MO.getReg()))
715 NewClobberedRegUnits.insert(Unit);
720 // Set of worklist registers that are defined by this instruction.
721 SmallSetVector<unsigned, 4> FwdRegDefs;
723 getForwardingRegsDefinedByMI(*CurMI, FwdRegDefs);
724 if (FwdRegDefs.empty()) {
725 // Any definitions by this instruction will clobber earlier reg movements.
726 ClobberedRegUnits.insert(NewClobberedRegUnits.begin(),
727 NewClobberedRegUnits.end());
731 // It's possible that we find a copy from a non-volatile register to the param
732 // register, which is clobbered in the meantime. Test for clobbered reg unit
733 // overlaps before completing.
734 auto IsRegClobberedInMeantime = [&](Register Reg) -> bool {
735 for (auto &RegUnit : ClobberedRegUnits)
736 if (TRI.hasRegUnit(Reg, RegUnit))
741 for (auto ParamFwdReg : FwdRegDefs) {
742 if (auto ParamValue = TII.describeLoadedValue(*CurMI, ParamFwdReg)) {
743 if (ParamValue->first.isImm()) {
744 int64_t Val = ParamValue->first.getImm();
745 finishCallSiteParams(Val, ParamValue->second,
746 ForwardedRegWorklist[ParamFwdReg], Params);
747 } else if (ParamValue->first.isReg()) {
748 Register RegLoc = ParamValue->first.getReg();
749 Register SP = TLI.getStackPointerRegisterToSaveRestore();
750 Register FP = TRI.getFrameRegister(*MF);
751 bool IsSPorFP = (RegLoc == SP) || (RegLoc == FP);
752 if (!IsRegClobberedInMeantime(RegLoc) &&
753 (TRI.isCalleeSavedPhysReg(RegLoc, *MF) || IsSPorFP)) {
754 MachineLocation MLoc(RegLoc, /*Indirect=*/IsSPorFP);
755 finishCallSiteParams(MLoc, ParamValue->second,
756 ForwardedRegWorklist[ParamFwdReg], Params);
758 // ParamFwdReg was described by the non-callee saved register
759 // RegLoc. Mark that the call site values for the parameters are
760 // dependent on that register instead of ParamFwdReg. Since RegLoc
761 // may be a register that will be handled in this iteration, we
762 // postpone adding the items to the worklist, and instead keep them
763 // in a temporary container.
764 addToFwdRegWorklist(TmpWorklistItems, RegLoc, ParamValue->second,
765 ForwardedRegWorklist[ParamFwdReg]);
771 // Remove all registers that this instruction defines from the worklist.
772 for (auto ParamFwdReg : FwdRegDefs)
773 ForwardedRegWorklist.erase(ParamFwdReg);
775 // Any definitions by this instruction will clobber earlier reg movements.
776 ClobberedRegUnits.insert(NewClobberedRegUnits.begin(),
777 NewClobberedRegUnits.end());
779 // Now that we are done handling this instruction, add items from the
780 // temporary worklist to the real one.
781 for (auto &New : TmpWorklistItems)
782 addToFwdRegWorklist(ForwardedRegWorklist, New.first, EmptyExpr, New.second);
783 TmpWorklistItems.clear();
786 static bool interpretNextInstr(const MachineInstr *CurMI,
787 FwdRegWorklist &ForwardedRegWorklist,
789 ClobberedRegSet &ClobberedRegUnits) {
790 // Skip bundle headers.
791 if (CurMI->isBundle())
794 // If the next instruction is a call we can not interpret parameter's
795 // forwarding registers or we finished the interpretation of all
800 if (ForwardedRegWorklist.empty())
803 // Avoid NOP description.
804 if (CurMI->getNumOperands() == 0)
807 interpretValues(CurMI, ForwardedRegWorklist, Params, ClobberedRegUnits);
812 /// Try to interpret values loaded into registers that forward parameters
813 /// for \p CallMI. Store parameters with interpreted value into \p Params.
814 static void collectCallSiteParameters(const MachineInstr *CallMI,
816 const MachineFunction *MF = CallMI->getMF();
817 const auto &CalleesMap = MF->getCallSitesInfo();
818 auto CallFwdRegsInfo = CalleesMap.find(CallMI);
820 // There is no information for the call instruction.
821 if (CallFwdRegsInfo == CalleesMap.end())
824 const MachineBasicBlock *MBB = CallMI->getParent();
826 // Skip the call instruction.
827 auto I = std::next(CallMI->getReverseIterator());
829 FwdRegWorklist ForwardedRegWorklist;
831 const DIExpression *EmptyExpr =
832 DIExpression::get(MF->getFunction().getContext(), {});
834 // Add all the forwarding registers into the ForwardedRegWorklist.
835 for (const auto &ArgReg : CallFwdRegsInfo->second) {
837 ForwardedRegWorklist.insert({ArgReg.Reg, {{ArgReg.Reg, EmptyExpr}}})
839 assert(InsertedReg && "Single register used to forward two arguments?");
843 // Do not emit CSInfo for undef forwarding registers.
844 for (const auto &MO : CallMI->uses())
845 if (MO.isReg() && MO.isUndef())
846 ForwardedRegWorklist.erase(MO.getReg());
848 // We erase, from the ForwardedRegWorklist, those forwarding registers for
849 // which we successfully describe a loaded value (by using
850 // the describeLoadedValue()). For those remaining arguments in the working
851 // list, for which we do not describe a loaded value by
852 // the describeLoadedValue(), we try to generate an entry value expression
853 // for their call site value description, if the call is within the entry MBB.
854 // TODO: Handle situations when call site parameter value can be described
855 // as the entry value within basic blocks other than the first one.
856 bool ShouldTryEmitEntryVals = MBB->getIterator() == MF->begin();
858 // Search for a loading value in forwarding registers inside call delay slot.
859 ClobberedRegSet ClobberedRegUnits;
860 if (CallMI->hasDelaySlot()) {
861 auto Suc = std::next(CallMI->getIterator());
862 // Only one-instruction delay slot is supported.
863 auto BundleEnd = llvm::getBundleEnd(CallMI->getIterator());
865 assert(std::next(Suc) == BundleEnd &&
866 "More than one instruction in call delay slot");
867 // Try to interpret value loaded by instruction.
868 if (!interpretNextInstr(&*Suc, ForwardedRegWorklist, Params, ClobberedRegUnits))
872 // Search for a loading value in forwarding registers.
873 for (; I != MBB->rend(); ++I) {
874 // Try to interpret values loaded by instruction.
875 if (!interpretNextInstr(&*I, ForwardedRegWorklist, Params, ClobberedRegUnits))
879 // Emit the call site parameter's value as an entry value.
880 if (ShouldTryEmitEntryVals) {
881 // Create an expression where the register's entry value is used.
882 DIExpression *EntryExpr = DIExpression::get(
883 MF->getFunction().getContext(), {dwarf::DW_OP_LLVM_entry_value, 1});
884 for (auto &RegEntry : ForwardedRegWorklist) {
885 MachineLocation MLoc(RegEntry.first);
886 finishCallSiteParams(MLoc, EntryExpr, RegEntry.second, Params);
891 void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
892 DwarfCompileUnit &CU, DIE &ScopeDIE,
893 const MachineFunction &MF) {
894 // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if
895 // the subprogram is required to have one.
896 if (!SP.areAllCallsDescribed() || !SP.isDefinition())
899 // Use DW_AT_call_all_calls to express that call site entries are present
900 // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls
901 // because one of its requirements is not met: call site entries for
902 // optimized-out calls are elided.
903 CU.addFlag(ScopeDIE, CU.getDwarf5OrGNUAttr(dwarf::DW_AT_call_all_calls));
905 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
906 assert(TII && "TargetInstrInfo not found: cannot label tail calls");
908 // Delay slot support check.
909 auto delaySlotSupported = [&](const MachineInstr &MI) {
910 if (!MI.isBundledWithSucc())
912 auto Suc = std::next(MI.getIterator());
913 auto CallInstrBundle = getBundleStart(MI.getIterator());
914 (void)CallInstrBundle;
915 auto DelaySlotBundle = getBundleStart(Suc);
916 (void)DelaySlotBundle;
917 // Ensure that label after call is following delay slot instruction.
918 // Ex. CALL_INSTRUCTION {
919 // DELAY_SLOT_INSTRUCTION }
921 assert(getLabelAfterInsn(&*CallInstrBundle) ==
922 getLabelAfterInsn(&*DelaySlotBundle) &&
923 "Call and its successor instruction don't have same label after.");
927 // Emit call site entries for each call or tail call in the function.
928 for (const MachineBasicBlock &MBB : MF) {
929 for (const MachineInstr &MI : MBB.instrs()) {
930 // Bundles with call in them will pass the isCall() test below but do not
931 // have callee operand information so skip them here. Iterator will
932 // eventually reach the call MI.
936 // Skip instructions which aren't calls. Both calls and tail-calling jump
937 // instructions (e.g TAILJMPd64) are classified correctly here.
938 if (!MI.isCandidateForCallSiteEntry())
941 // Skip instructions marked as frame setup, as they are not interesting to
943 if (MI.getFlag(MachineInstr::FrameSetup))
946 // Check if delay slot support is enabled.
947 if (MI.hasDelaySlot() && !delaySlotSupported(*&MI))
950 // If this is a direct call, find the callee's subprogram.
951 // In the case of an indirect call find the register that holds
953 const MachineOperand &CalleeOp = TII->getCalleeOperand(MI);
954 if (!CalleeOp.isGlobal() &&
955 (!CalleeOp.isReg() || !CalleeOp.getReg().isPhysical()))
958 unsigned CallReg = 0;
959 const DISubprogram *CalleeSP = nullptr;
960 const Function *CalleeDecl = nullptr;
961 if (CalleeOp.isReg()) {
962 CallReg = CalleeOp.getReg();
966 CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal());
967 if (!CalleeDecl || !CalleeDecl->getSubprogram())
969 CalleeSP = CalleeDecl->getSubprogram();
972 // TODO: Omit call site entries for runtime calls (objc_msgSend, etc).
974 bool IsTail = TII->isTailCall(MI);
976 // If MI is in a bundle, the label was created after the bundle since
977 // EmitFunctionBody iterates over top-level MIs. Get that top-level MI
978 // to search for that label below.
979 const MachineInstr *TopLevelCallMI =
980 MI.isInsideBundle() ? &*getBundleStart(MI.getIterator()) : &MI;
982 // For non-tail calls, the return PC is needed to disambiguate paths in
983 // the call graph which could lead to some target function. For tail
984 // calls, no return PC information is needed, unless tuning for GDB in
985 // DWARF4 mode in which case we fake a return PC for compatibility.
986 const MCSymbol *PCAddr =
987 (!IsTail || CU.useGNUAnalogForDwarf5Feature())
988 ? const_cast<MCSymbol *>(getLabelAfterInsn(TopLevelCallMI))
991 // For tail calls, it's necessary to record the address of the branch
992 // instruction so that the debugger can show where the tail call occurred.
993 const MCSymbol *CallAddr =
994 IsTail ? getLabelBeforeInsn(TopLevelCallMI) : nullptr;
996 assert((IsTail || PCAddr) && "Non-tail call without return PC");
998 LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> "
999 << (CalleeDecl ? CalleeDecl->getName()
1000 : StringRef(MF.getSubtarget()
1002 ->getName(CallReg)))
1003 << (IsTail ? " [IsTail]" : "") << "\n");
1005 DIE &CallSiteDIE = CU.constructCallSiteEntryDIE(
1006 ScopeDIE, CalleeSP, IsTail, PCAddr, CallAddr, CallReg);
1008 // Optionally emit call-site-param debug info.
1009 if (emitDebugEntryValues()) {
1011 // Try to interpret values of call site parameters.
1012 collectCallSiteParameters(&MI, Params);
1013 CU.constructCallSiteParmEntryDIEs(CallSiteDIE, Params);
1019 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const {
1020 if (!U.hasDwarfPubSections())
1023 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
1026 void DwarfDebug::finishUnitAttributes(const DICompileUnit *DIUnit,
1027 DwarfCompileUnit &NewCU) {
1028 DIE &Die = NewCU.getUnitDie();
1029 StringRef FN = DIUnit->getFilename();
1031 StringRef Producer = DIUnit->getProducer();
1032 StringRef Flags = DIUnit->getFlags();
1033 if (!Flags.empty() && !useAppleExtensionAttributes()) {
1034 std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
1035 NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
1037 NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
1039 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
1040 DIUnit->getSourceLanguage());
1041 NewCU.addString(Die, dwarf::DW_AT_name, FN);
1042 StringRef SysRoot = DIUnit->getSysRoot();
1043 if (!SysRoot.empty())
1044 NewCU.addString(Die, dwarf::DW_AT_LLVM_sysroot, SysRoot);
1045 StringRef SDK = DIUnit->getSDK();
1047 NewCU.addString(Die, dwarf::DW_AT_APPLE_sdk, SDK);
1049 if (!useSplitDwarf()) {
1050 // Add DW_str_offsets_base to the unit DIE, except for split units.
1051 if (useSegmentedStringOffsetsTable())
1052 NewCU.addStringOffsetsStart();
1054 NewCU.initStmtList();
1056 // If we're using split dwarf the compilation dir is going to be in the
1057 // skeleton CU and so we don't need to duplicate it here.
1058 if (!CompilationDir.empty())
1059 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
1060 addGnuPubAttributes(NewCU, Die);
1063 if (useAppleExtensionAttributes()) {
1064 if (DIUnit->isOptimized())
1065 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
1067 StringRef Flags = DIUnit->getFlags();
1069 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
1071 if (unsigned RVer = DIUnit->getRuntimeVersion())
1072 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1073 dwarf::DW_FORM_data1, RVer);
1076 if (DIUnit->getDWOId()) {
1077 // This CU is either a clang module DWO or a skeleton CU.
1078 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
1079 DIUnit->getDWOId());
1080 if (!DIUnit->getSplitDebugFilename().empty()) {
1081 // This is a prefabricated skeleton CU.
1082 dwarf::Attribute attrDWOName = getDwarfVersion() >= 5
1083 ? dwarf::DW_AT_dwo_name
1084 : dwarf::DW_AT_GNU_dwo_name;
1085 NewCU.addString(Die, attrDWOName, DIUnit->getSplitDebugFilename());
1089 // Create new DwarfCompileUnit for the given metadata node with tag
1090 // DW_TAG_compile_unit.
1092 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) {
1093 if (auto *CU = CUMap.lookup(DIUnit))
1096 if (useSplitDwarf() &&
1097 !shareAcrossDWOCUs() &&
1098 (!DIUnit->getSplitDebugInlining() ||
1099 DIUnit->getEmissionKind() == DICompileUnit::FullDebug) &&
1101 return *CUMap.begin()->second;
1103 CompilationDir = DIUnit->getDirectory();
1105 auto OwnedUnit = std::make_unique<DwarfCompileUnit>(
1106 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
1107 DwarfCompileUnit &NewCU = *OwnedUnit;
1108 InfoHolder.addUnit(std::move(OwnedUnit));
1110 // LTO with assembly output shares a single line table amongst multiple CUs.
1111 // To avoid the compilation directory being ambiguous, let the line table
1112 // explicitly describe the directory of all files, never relying on the
1113 // compilation directory.
1114 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
1115 Asm->OutStreamer->emitDwarfFile0Directive(
1116 CompilationDir, DIUnit->getFilename(), getMD5AsBytes(DIUnit->getFile()),
1117 DIUnit->getSource(), NewCU.getUniqueID());
1119 if (useSplitDwarf()) {
1120 NewCU.setSkeleton(constructSkeletonCU(NewCU));
1121 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
1123 finishUnitAttributes(DIUnit, NewCU);
1124 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
1127 CUMap.insert({DIUnit, &NewCU});
1128 CUDieMap.insert({&NewCU.getUnitDie(), &NewCU});
1132 /// Sort and unique GVEs by comparing their fragment offset.
1133 static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
1134 sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
1136 GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
1137 // Sort order: first null exprs, then exprs without fragment
1138 // info, then sort by fragment offset in bits.
1139 // FIXME: Come up with a more comprehensive comparator so
1140 // the sorting isn't non-deterministic, and so the following
1141 // std::unique call works correctly.
1142 if (!A.Expr || !B.Expr)
1144 auto FragmentA = A.Expr->getFragmentInfo();
1145 auto FragmentB = B.Expr->getFragmentInfo();
1146 if (!FragmentA || !FragmentB)
1148 return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
1150 GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
1151 [](DwarfCompileUnit::GlobalExpr A,
1152 DwarfCompileUnit::GlobalExpr B) {
1153 return A.Expr == B.Expr;
1159 // Emit all Dwarf sections that should come prior to the content. Create
1160 // global DIEs and emit initial debug info sections. This is invoked by
1161 // the target AsmPrinter.
1162 void DwarfDebug::beginModule(Module *M) {
1163 DebugHandlerBase::beginModule(M);
1165 if (!Asm || !MMI->hasDebugInfo())
1168 unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
1169 M->debug_compile_units_end());
1170 assert(NumDebugCUs > 0 && "Asm unexpectedly initialized");
1171 assert(MMI->hasDebugInfo() &&
1172 "DebugInfoAvailabilty unexpectedly not initialized");
1173 SingleCU = NumDebugCUs == 1;
1174 DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
1176 for (const GlobalVariable &Global : M->globals()) {
1177 SmallVector<DIGlobalVariableExpression *, 1> GVs;
1178 Global.getDebugInfo(GVs);
1179 for (auto *GVE : GVs)
1180 GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
1183 // Create the symbol that designates the start of the unit's contribution
1184 // to the string offsets table. In a split DWARF scenario, only the skeleton
1185 // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol).
1186 if (useSegmentedStringOffsetsTable())
1187 (useSplitDwarf() ? SkeletonHolder : InfoHolder)
1188 .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base"));
1191 // Create the symbols that designates the start of the DWARF v5 range list
1192 // and locations list tables. They are located past the table headers.
1193 if (getDwarfVersion() >= 5) {
1194 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1195 Holder.setRnglistsTableBaseSym(
1196 Asm->createTempSymbol("rnglists_table_base"));
1198 if (useSplitDwarf())
1199 InfoHolder.setRnglistsTableBaseSym(
1200 Asm->createTempSymbol("rnglists_dwo_table_base"));
1203 // Create the symbol that points to the first entry following the debug
1204 // address table (.debug_addr) header.
1205 AddrPool.setLabel(Asm->createTempSymbol("addr_table_base"));
1206 DebugLocs.setSym(Asm->createTempSymbol("loclists_table_base"));
1208 for (DICompileUnit *CUNode : M->debug_compile_units()) {
1209 if (CUNode->getImportedEntities().empty() &&
1210 CUNode->getEnumTypes().empty() && CUNode->getRetainedTypes().empty() &&
1211 CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
1214 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
1216 // Global Variables.
1217 for (auto *GVE : CUNode->getGlobalVariables()) {
1218 // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
1219 // already know about the variable and it isn't adding a constant
1221 auto &GVMapEntry = GVMap[GVE->getVariable()];
1222 auto *Expr = GVE->getExpression();
1223 if (!GVMapEntry.size() || (Expr && Expr->isConstant()))
1224 GVMapEntry.push_back({nullptr, Expr});
1227 DenseSet<DIGlobalVariable *> Processed;
1228 for (auto *GVE : CUNode->getGlobalVariables()) {
1229 DIGlobalVariable *GV = GVE->getVariable();
1230 if (Processed.insert(GV).second)
1231 CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
1234 for (auto *Ty : CUNode->getEnumTypes())
1235 CU.getOrCreateTypeDIE(cast<DIType>(Ty));
1237 for (auto *Ty : CUNode->getRetainedTypes()) {
1238 // The retained types array by design contains pointers to
1239 // MDNodes rather than DIRefs. Unique them here.
1240 if (DIType *RT = dyn_cast<DIType>(Ty))
1241 // There is no point in force-emitting a forward declaration.
1242 CU.getOrCreateTypeDIE(RT);
1247 void DwarfDebug::finishEntityDefinitions() {
1248 for (const auto &Entity : ConcreteEntities) {
1249 DIE *Die = Entity->getDIE();
1251 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
1252 // in the ConcreteEntities list, rather than looking it up again here.
1253 // DIE::getUnit isn't simple - it walks parent pointers, etc.
1254 DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie());
1256 Unit->finishEntityDefinition(Entity.get());
1260 void DwarfDebug::finishSubprogramDefinitions() {
1261 for (const DISubprogram *SP : ProcessedSPNodes) {
1262 assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
1264 getOrCreateDwarfCompileUnit(SP->getUnit()),
1265 [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); });
1269 void DwarfDebug::finalizeModuleInfo() {
1270 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1272 finishSubprogramDefinitions();
1274 finishEntityDefinitions();
1276 // Include the DWO file name in the hash if there's more than one CU.
1277 // This handles ThinLTO's situation where imported CUs may very easily be
1278 // duplicate with the same CU partially imported into another ThinLTO unit.
1280 if (CUMap.size() > 1)
1281 DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile;
1283 bool HasEmittedSplitCU = false;
1285 // Handle anything that needs to be done on a per-unit basis after
1286 // all other generation.
1287 for (const auto &P : CUMap) {
1288 auto &TheCU = *P.second;
1289 if (TheCU.getCUNode()->isDebugDirectivesOnly())
1291 // Emit DW_AT_containing_type attribute to connect types with their
1292 // vtable holding type.
1293 TheCU.constructContainingTypeDIEs();
1295 // Add CU specific attributes if we need to add any.
1296 // If we're splitting the dwarf out now that we've got the entire
1297 // CU then add the dwo id to it.
1298 auto *SkCU = TheCU.getSkeleton();
1300 bool HasSplitUnit = SkCU && !TheCU.getUnitDie().children().empty();
1303 (void)HasEmittedSplitCU;
1304 assert((shareAcrossDWOCUs() || !HasEmittedSplitCU) &&
1305 "Multiple CUs emitted into a single dwo file");
1306 HasEmittedSplitCU = true;
1307 dwarf::Attribute attrDWOName = getDwarfVersion() >= 5
1308 ? dwarf::DW_AT_dwo_name
1309 : dwarf::DW_AT_GNU_dwo_name;
1310 finishUnitAttributes(TheCU.getCUNode(), TheCU);
1311 TheCU.addString(TheCU.getUnitDie(), attrDWOName,
1312 Asm->TM.Options.MCOptions.SplitDwarfFile);
1313 SkCU->addString(SkCU->getUnitDie(), attrDWOName,
1314 Asm->TM.Options.MCOptions.SplitDwarfFile);
1315 // Emit a unique identifier for this CU.
1317 DIEHash(Asm, &TheCU).computeCUSignature(DWOName, TheCU.getUnitDie());
1318 if (getDwarfVersion() >= 5) {
1322 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1323 dwarf::DW_FORM_data8, ID);
1324 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1325 dwarf::DW_FORM_data8, ID);
1328 if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) {
1329 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
1330 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
1334 finishUnitAttributes(SkCU->getCUNode(), *SkCU);
1337 // If we have code split among multiple sections or non-contiguous
1338 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
1339 // remain in the .o file, otherwise add a DW_AT_low_pc.
1340 // FIXME: We should use ranges allow reordering of code ala
1341 // .subsections_via_symbols in mach-o. This would mean turning on
1342 // ranges for all subprogram DIEs for mach-o.
1343 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
1345 if (unsigned NumRanges = TheCU.getRanges().size()) {
1346 if (NumRanges > 1 && useRangesSection())
1347 // A DW_AT_low_pc attribute may also be specified in combination with
1348 // DW_AT_ranges to specify the default base address for use in
1349 // location lists (see Section 2.6.2) and range lists (see Section
1351 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
1353 U.setBaseAddress(TheCU.getRanges().front().Begin);
1354 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
1357 // We don't keep track of which addresses are used in which CU so this
1358 // is a bit pessimistic under LTO.
1359 if ((HasSplitUnit || getDwarfVersion() >= 5) && !AddrPool.isEmpty())
1360 U.addAddrTableBase();
1362 if (getDwarfVersion() >= 5) {
1363 if (U.hasRangeLists())
1364 U.addRnglistsBase();
1366 if (!DebugLocs.getLists().empty() && !useSplitDwarf()) {
1367 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_loclists_base,
1369 TLOF.getDwarfLoclistsSection()->getBeginSymbol());
1373 auto *CUNode = cast<DICompileUnit>(P.first);
1374 // If compile Unit has macros, emit "DW_AT_macro_info/DW_AT_macros"
1376 if (CUNode->getMacros()) {
1377 if (UseDebugMacroSection) {
1378 if (useSplitDwarf())
1379 TheCU.addSectionDelta(
1380 TheCU.getUnitDie(), dwarf::DW_AT_macros, U.getMacroLabelBegin(),
1381 TLOF.getDwarfMacroDWOSection()->getBeginSymbol());
1383 dwarf::Attribute MacrosAttr = getDwarfVersion() >= 5
1384 ? dwarf::DW_AT_macros
1385 : dwarf::DW_AT_GNU_macros;
1386 U.addSectionLabel(U.getUnitDie(), MacrosAttr, U.getMacroLabelBegin(),
1387 TLOF.getDwarfMacroSection()->getBeginSymbol());
1390 if (useSplitDwarf())
1391 TheCU.addSectionDelta(
1392 TheCU.getUnitDie(), dwarf::DW_AT_macro_info,
1393 U.getMacroLabelBegin(),
1394 TLOF.getDwarfMacinfoDWOSection()->getBeginSymbol());
1396 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
1397 U.getMacroLabelBegin(),
1398 TLOF.getDwarfMacinfoSection()->getBeginSymbol());
1403 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
1404 for (auto *CUNode : MMI->getModule()->debug_compile_units())
1405 if (CUNode->getDWOId())
1406 getOrCreateDwarfCompileUnit(CUNode);
1408 // Compute DIE offsets and sizes.
1409 InfoHolder.computeSizeAndOffsets();
1410 if (useSplitDwarf())
1411 SkeletonHolder.computeSizeAndOffsets();
1414 // Emit all Dwarf sections that should come after the content.
1415 void DwarfDebug::endModule() {
1416 // Terminate the pending line table.
1418 terminateLineTable(PrevCU);
1420 assert(CurFn == nullptr);
1421 assert(CurMI == nullptr);
1423 for (const auto &P : CUMap) {
1424 const auto *CUNode = cast<DICompileUnit>(P.first);
1425 DwarfCompileUnit *CU = &*P.second;
1427 // Emit imported entities.
1428 for (auto *IE : CUNode->getImportedEntities()) {
1429 assert(!isa_and_nonnull<DILocalScope>(IE->getScope()) &&
1430 "Unexpected function-local entity in 'imports' CU field.");
1431 CU->getOrCreateImportedEntityDIE(IE);
1433 for (const auto *D : CU->getDeferredLocalDecls()) {
1434 if (auto *IE = dyn_cast<DIImportedEntity>(D))
1435 CU->getOrCreateImportedEntityDIE(IE);
1437 llvm_unreachable("Unexpected local retained node!");
1441 CU->createBaseTypeDIEs();
1444 // If we aren't actually generating debug info (check beginModule -
1445 // conditionalized on the presence of the llvm.dbg.cu metadata node)
1446 if (!Asm || !MMI->hasDebugInfo())
1449 // Finalize the debug info for the module.
1450 finalizeModuleInfo();
1452 if (useSplitDwarf())
1453 // Emit debug_loc.dwo/debug_loclists.dwo section.
1456 // Emit debug_loc/debug_loclists section.
1459 // Corresponding abbreviations into a abbrev section.
1460 emitAbbreviations();
1462 // Emit all the DIEs into a debug info section.
1465 // Emit info into a debug aranges section.
1466 if (GenerateARangeSection)
1469 // Emit info into a debug ranges section.
1472 if (useSplitDwarf())
1473 // Emit info into a debug macinfo.dwo section.
1474 emitDebugMacinfoDWO();
1476 // Emit info into a debug macinfo/macro section.
1481 if (useSplitDwarf()) {
1484 emitDebugAbbrevDWO();
1486 emitDebugRangesDWO();
1491 // Emit info into the dwarf accelerator table sections.
1492 switch (getAccelTableKind()) {
1493 case AccelTableKind::Apple:
1496 emitAccelNamespaces();
1499 case AccelTableKind::Dwarf:
1500 emitAccelDebugNames();
1502 case AccelTableKind::None:
1504 case AccelTableKind::Default:
1505 llvm_unreachable("Default should have already been resolved.");
1508 // Emit the pubnames and pubtypes sections if requested.
1509 emitDebugPubSections();
1512 // FIXME: AbstractVariables.clear();
1515 void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
1516 const DINode *Node, const MDNode *ScopeNode) {
1517 if (CU.getExistingAbstractEntity(Node))
1520 if (LexicalScope *Scope =
1521 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
1522 CU.createAbstractEntity(Node, Scope);
1525 static const DILocalScope *getRetainedNodeScope(const MDNode *N) {
1527 if (const auto *LV = dyn_cast<DILocalVariable>(N))
1529 else if (const auto *L = dyn_cast<DILabel>(N))
1531 else if (const auto *IE = dyn_cast<DIImportedEntity>(N))
1534 llvm_unreachable("Unexpected retained node!");
1536 // Ensure the scope is not a DILexicalBlockFile.
1537 return cast<DILocalScope>(S)->getNonLexicalBlockFileScope();
1540 // Collect variable information from side table maintained by MF.
1541 void DwarfDebug::collectVariableInfoFromMFTable(
1542 DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) {
1543 SmallDenseMap<InlinedEntity, DbgVariable *> MFVars;
1544 LLVM_DEBUG(dbgs() << "DwarfDebug: collecting variables from MF side table\n");
1545 for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
1548 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1549 "Expected inlined-at fields to agree");
1551 InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt());
1552 Processed.insert(Var);
1553 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
1555 // If variable scope is not found then skip this variable.
1557 LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI.Var->getName()
1558 << ", no variable scope found\n");
1562 ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode());
1563 auto RegVar = std::make_unique<DbgVariable>(
1564 cast<DILocalVariable>(Var.first), Var.second);
1565 if (VI.inStackSlot())
1566 RegVar->initializeMMI(VI.Expr, VI.getStackSlot());
1568 MachineLocation MLoc(VI.getEntryValueRegister(), /*IsIndirect*/ true);
1569 auto LocEntry = DbgValueLocEntry(MLoc);
1570 RegVar->initializeDbgValue(DbgValueLoc(VI.Expr, LocEntry));
1572 LLVM_DEBUG(dbgs() << "Created DbgVariable for " << VI.Var->getName()
1575 if (DbgVariable *DbgVar = MFVars.lookup(Var)) {
1576 if (DbgVar->getValueLoc())
1577 LLVM_DEBUG(dbgs() << "Dropping repeated entry value debug info for "
1579 << VI.Var->getName() << "\n");
1581 DbgVar->addMMIEntry(*RegVar);
1582 } else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) {
1583 MFVars.insert({Var, RegVar.get()});
1584 ConcreteEntities.push_back(std::move(RegVar));
1589 /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1590 /// enclosing lexical scope. The check ensures there are no other instructions
1591 /// in the same lexical scope preceding the DBG_VALUE and that its range is
1592 /// either open or otherwise rolls off the end of the scope.
1593 static bool validThroughout(LexicalScopes &LScopes,
1594 const MachineInstr *DbgValue,
1595 const MachineInstr *RangeEnd,
1596 const InstructionOrdering &Ordering) {
1597 assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
1598 auto MBB = DbgValue->getParent();
1599 auto DL = DbgValue->getDebugLoc();
1600 auto *LScope = LScopes.findLexicalScope(DL);
1601 // Scope doesn't exist; this is a dead DBG_VALUE.
1604 auto &LSRange = LScope->getRanges();
1605 if (LSRange.size() == 0)
1608 const MachineInstr *LScopeBegin = LSRange.front().first;
1609 // If the scope starts before the DBG_VALUE then we may have a negative
1610 // result. Otherwise the location is live coming into the scope and we
1611 // can skip the following checks.
1612 if (!Ordering.isBefore(DbgValue, LScopeBegin)) {
1613 // Exit if the lexical scope begins outside of the current block.
1614 if (LScopeBegin->getParent() != MBB)
1617 MachineBasicBlock::const_reverse_iterator Pred(DbgValue);
1618 for (++Pred; Pred != MBB->rend(); ++Pred) {
1619 if (Pred->getFlag(MachineInstr::FrameSetup))
1621 auto PredDL = Pred->getDebugLoc();
1622 if (!PredDL || Pred->isMetaInstruction())
1624 // Check whether the instruction preceding the DBG_VALUE is in the same
1625 // (sub)scope as the DBG_VALUE.
1626 if (DL->getScope() == PredDL->getScope())
1628 auto *PredScope = LScopes.findLexicalScope(PredDL);
1629 if (!PredScope || LScope->dominates(PredScope))
1634 // If the range of the DBG_VALUE is open-ended, report success.
1638 // Single, constant DBG_VALUEs in the prologue are promoted to be live
1639 // throughout the function. This is a hack, presumably for DWARF v2 and not
1640 // necessarily correct. It would be much better to use a dbg.declare instead
1641 // if we know the constant is live throughout the scope.
1642 if (MBB->pred_empty() &&
1643 all_of(DbgValue->debug_operands(),
1644 [](const MachineOperand &Op) { return Op.isImm(); }))
1647 // Test if the location terminates before the end of the scope.
1648 const MachineInstr *LScopeEnd = LSRange.back().second;
1649 if (Ordering.isBefore(RangeEnd, LScopeEnd))
1652 // There's a single location which starts at the scope start, and ends at or
1653 // after the scope end.
1657 /// Build the location list for all DBG_VALUEs in the function that
1658 /// describe the same variable. The resulting DebugLocEntries will have
1659 /// strict monotonically increasing begin addresses and will never
1660 /// overlap. If the resulting list has only one entry that is valid
1661 /// throughout variable's scope return true.
1663 // See the definition of DbgValueHistoryMap::Entry for an explanation of the
1664 // different kinds of history map entries. One thing to be aware of is that if
1665 // a debug value is ended by another entry (rather than being valid until the
1666 // end of the function), that entry's instruction may or may not be included in
1667 // the range, depending on if the entry is a clobbering entry (it has an
1668 // instruction that clobbers one or more preceding locations), or if it is an
1669 // (overlapping) debug value entry. This distinction can be seen in the example
1670 // below. The first debug value is ended by the clobbering entry 2, and the
1671 // second and third debug values are ended by the overlapping debug value entry
1676 // History map entries [type, end index, mi]
1678 // 0 | [DbgValue, 2, DBG_VALUE $reg0, [...] (fragment 0, 32)]
1679 // 1 | | [DbgValue, 4, DBG_VALUE $reg1, [...] (fragment 32, 32)]
1680 // 2 | | [Clobber, $reg0 = [...], -, -]
1681 // 3 | | [DbgValue, 4, DBG_VALUE 123, [...] (fragment 64, 32)]
1682 // 4 [DbgValue, ~0, DBG_VALUE @g, [...] (fragment 0, 96)]
1684 // Output [start, end) [Value...]:
1686 // [0-1) [(reg0, fragment 0, 32)]
1687 // [1-3) [(reg0, fragment 0, 32), (reg1, fragment 32, 32)]
1688 // [3-4) [(reg1, fragment 32, 32), (123, fragment 64, 32)]
1689 // [4-) [(@g, fragment 0, 96)]
1690 bool DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
1691 const DbgValueHistoryMap::Entries &Entries) {
1693 std::pair<DbgValueHistoryMap::EntryIndex, DbgValueLoc>;
1694 SmallVector<OpenRange, 4> OpenRanges;
1695 bool isSafeForSingleLocation = true;
1696 const MachineInstr *StartDebugMI = nullptr;
1697 const MachineInstr *EndMI = nullptr;
1699 for (auto EB = Entries.begin(), EI = EB, EE = Entries.end(); EI != EE; ++EI) {
1700 const MachineInstr *Instr = EI->getInstr();
1702 // Remove all values that are no longer live.
1703 size_t Index = std::distance(EB, EI);
1704 erase_if(OpenRanges, [&](OpenRange &R) { return R.first <= Index; });
1706 // If we are dealing with a clobbering entry, this iteration will result in
1707 // a location list entry starting after the clobbering instruction.
1708 const MCSymbol *StartLabel =
1709 EI->isClobber() ? getLabelAfterInsn(Instr) : getLabelBeforeInsn(Instr);
1710 assert(StartLabel &&
1711 "Forgot label before/after instruction starting a range!");
1713 const MCSymbol *EndLabel;
1714 if (std::next(EI) == Entries.end()) {
1715 const MachineBasicBlock &EndMBB = Asm->MF->back();
1716 EndLabel = Asm->MBBSectionRanges[EndMBB.getSectionIDNum()].EndLabel;
1717 if (EI->isClobber())
1718 EndMI = EI->getInstr();
1720 else if (std::next(EI)->isClobber())
1721 EndLabel = getLabelAfterInsn(std::next(EI)->getInstr());
1723 EndLabel = getLabelBeforeInsn(std::next(EI)->getInstr());
1724 assert(EndLabel && "Forgot label after instruction ending a range!");
1726 if (EI->isDbgValue())
1727 LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Instr << "\n");
1729 // If this history map entry has a debug value, add that to the list of
1730 // open ranges and check if its location is valid for a single value
1732 if (EI->isDbgValue()) {
1733 // Do not add undef debug values, as they are redundant information in
1734 // the location list entries. An undef debug results in an empty location
1735 // description. If there are any non-undef fragments then padding pieces
1736 // with empty location descriptions will automatically be inserted, and if
1737 // all fragments are undef then the whole location list entry is
1739 if (!Instr->isUndefDebugValue()) {
1740 auto Value = getDebugLocValue(Instr);
1741 OpenRanges.emplace_back(EI->getEndIndex(), Value);
1743 // TODO: Add support for single value fragment locations.
1744 if (Instr->getDebugExpression()->isFragment())
1745 isSafeForSingleLocation = false;
1748 StartDebugMI = Instr;
1750 isSafeForSingleLocation = false;
1754 // Location list entries with empty location descriptions are redundant
1755 // information in DWARF, so do not emit those.
1756 if (OpenRanges.empty())
1759 // Omit entries with empty ranges as they do not have any effect in DWARF.
1760 if (StartLabel == EndLabel) {
1761 LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n");
1765 SmallVector<DbgValueLoc, 4> Values;
1766 for (auto &R : OpenRanges)
1767 Values.push_back(R.second);
1769 // With Basic block sections, it is posssible that the StartLabel and the
1770 // Instr are not in the same section. This happens when the StartLabel is
1771 // the function begin label and the dbg value appears in a basic block
1772 // that is not the entry. In this case, the range needs to be split to
1773 // span each individual section in the range from StartLabel to EndLabel.
1774 if (Asm->MF->hasBBSections() && StartLabel == Asm->getFunctionBegin() &&
1775 !Instr->getParent()->sameSection(&Asm->MF->front())) {
1776 const MCSymbol *BeginSectionLabel = StartLabel;
1778 for (const MachineBasicBlock &MBB : *Asm->MF) {
1779 if (MBB.isBeginSection() && &MBB != &Asm->MF->front())
1780 BeginSectionLabel = MBB.getSymbol();
1782 if (MBB.sameSection(Instr->getParent())) {
1783 DebugLoc.emplace_back(BeginSectionLabel, EndLabel, Values);
1786 if (MBB.isEndSection())
1787 DebugLoc.emplace_back(BeginSectionLabel, MBB.getEndSymbol(), Values);
1790 DebugLoc.emplace_back(StartLabel, EndLabel, Values);
1793 // Attempt to coalesce the ranges of two otherwise identical
1795 auto CurEntry = DebugLoc.rbegin();
1797 dbgs() << CurEntry->getValues().size() << " Values:\n";
1798 for (auto &Value : CurEntry->getValues())
1800 dbgs() << "-----\n";
1803 auto PrevEntry = std::next(CurEntry);
1804 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
1805 DebugLoc.pop_back();
1808 if (!isSafeForSingleLocation ||
1809 !validThroughout(LScopes, StartDebugMI, EndMI, getInstOrdering()))
1812 if (DebugLoc.size() == 1)
1815 if (!Asm->MF->hasBBSections())
1818 // Check here to see if loclist can be merged into a single range. If not,
1819 // we must keep the split loclists per section. This does exactly what
1820 // MergeRanges does without sections. We don't actually merge the ranges
1821 // as the split ranges must be kept intact if this cannot be collapsed
1822 // into a single range.
1823 const MachineBasicBlock *RangeMBB = nullptr;
1824 if (DebugLoc[0].getBeginSym() == Asm->getFunctionBegin())
1825 RangeMBB = &Asm->MF->front();
1827 RangeMBB = Entries.begin()->getInstr()->getParent();
1828 auto *CurEntry = DebugLoc.begin();
1829 auto *NextEntry = std::next(CurEntry);
1830 while (NextEntry != DebugLoc.end()) {
1831 // Get the last machine basic block of this section.
1832 while (!RangeMBB->isEndSection())
1833 RangeMBB = RangeMBB->getNextNode();
1834 if (!RangeMBB->getNextNode())
1836 // CurEntry should end the current section and NextEntry should start
1837 // the next section and the Values must match for these two ranges to be
1839 if (CurEntry->getEndSym() != RangeMBB->getEndSymbol() ||
1840 NextEntry->getBeginSym() != RangeMBB->getNextNode()->getSymbol() ||
1841 CurEntry->getValues() != NextEntry->getValues())
1843 RangeMBB = RangeMBB->getNextNode();
1844 CurEntry = NextEntry;
1845 NextEntry = std::next(CurEntry);
1850 DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU,
1851 LexicalScope &Scope,
1853 const DILocation *Location,
1854 const MCSymbol *Sym) {
1855 ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode());
1856 if (isa<const DILocalVariable>(Node)) {
1857 ConcreteEntities.push_back(
1858 std::make_unique<DbgVariable>(cast<const DILocalVariable>(Node),
1860 InfoHolder.addScopeVariable(&Scope,
1861 cast<DbgVariable>(ConcreteEntities.back().get()));
1862 } else if (isa<const DILabel>(Node)) {
1863 ConcreteEntities.push_back(
1864 std::make_unique<DbgLabel>(cast<const DILabel>(Node),
1866 InfoHolder.addScopeLabel(&Scope,
1867 cast<DbgLabel>(ConcreteEntities.back().get()));
1869 return ConcreteEntities.back().get();
1872 // Find variables for each lexical scope.
1873 void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
1874 const DISubprogram *SP,
1875 DenseSet<InlinedEntity> &Processed) {
1876 // Grab the variable info that was squirreled away in the MMI side-table.
1877 collectVariableInfoFromMFTable(TheCU, Processed);
1879 for (const auto &I : DbgValues) {
1880 InlinedEntity IV = I.first;
1881 if (Processed.count(IV))
1884 // Instruction ranges, specifying where IV is accessible.
1885 const auto &HistoryMapEntries = I.second;
1887 // Try to find any non-empty variable location. Do not create a concrete
1888 // entity if there are no locations.
1889 if (!DbgValues.hasNonEmptyLocation(HistoryMapEntries))
1892 LexicalScope *Scope = nullptr;
1893 const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first);
1894 if (const DILocation *IA = IV.second)
1895 Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA);
1897 Scope = LScopes.findLexicalScope(LocalVar->getScope());
1898 // If variable scope is not found then skip this variable.
1902 Processed.insert(IV);
1903 DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU,
1904 *Scope, LocalVar, IV.second));
1906 const MachineInstr *MInsn = HistoryMapEntries.front().getInstr();
1907 assert(MInsn->isDebugValue() && "History must begin with debug value");
1909 // Check if there is a single DBG_VALUE, valid throughout the var's scope.
1910 // If the history map contains a single debug value, there may be an
1911 // additional entry which clobbers the debug value.
1912 size_t HistSize = HistoryMapEntries.size();
1913 bool SingleValueWithClobber =
1914 HistSize == 2 && HistoryMapEntries[1].isClobber();
1915 if (HistSize == 1 || SingleValueWithClobber) {
1917 SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr;
1918 if (validThroughout(LScopes, MInsn, End, getInstOrdering())) {
1919 RegVar->initializeDbgValue(MInsn);
1924 // Do not emit location lists if .debug_loc secton is disabled.
1925 if (!useLocSection())
1928 // Handle multiple DBG_VALUE instructions describing one variable.
1929 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
1931 // Build the location list for this variable.
1932 SmallVector<DebugLocEntry, 8> Entries;
1933 bool isValidSingleLocation = buildLocationList(Entries, HistoryMapEntries);
1935 // Check whether buildLocationList managed to merge all locations to one
1936 // that is valid throughout the variable's scope. If so, produce single
1938 if (isValidSingleLocation) {
1939 RegVar->initializeDbgValue(Entries[0].getValues()[0]);
1943 // If the variable has a DIBasicType, extract it. Basic types cannot have
1944 // unique identifiers, so don't bother resolving the type with the
1946 const DIBasicType *BT = dyn_cast<DIBasicType>(
1947 static_cast<const Metadata *>(LocalVar->getType()));
1949 // Finalize the entry by lowering it into a DWARF bytestream.
1950 for (auto &Entry : Entries)
1951 Entry.finalize(*Asm, List, BT, TheCU);
1954 // For each InlinedEntity collected from DBG_LABEL instructions, convert to
1955 // DWARF-related DbgLabel.
1956 for (const auto &I : DbgLabels) {
1957 InlinedEntity IL = I.first;
1958 const MachineInstr *MI = I.second;
1962 LexicalScope *Scope = nullptr;
1963 const DILabel *Label = cast<DILabel>(IL.first);
1964 // The scope could have an extra lexical block file.
1965 const DILocalScope *LocalScope =
1966 Label->getScope()->getNonLexicalBlockFileScope();
1967 // Get inlined DILocation if it is inlined label.
1968 if (const DILocation *IA = IL.second)
1969 Scope = LScopes.findInlinedScope(LocalScope, IA);
1971 Scope = LScopes.findLexicalScope(LocalScope);
1972 // If label scope is not found then skip this label.
1976 Processed.insert(IL);
1977 /// At this point, the temporary label is created.
1978 /// Save the temporary label to DbgLabel entity to get the
1979 /// actually address when generating Dwarf DIE.
1980 MCSymbol *Sym = getLabelBeforeInsn(MI);
1981 createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym);
1984 // Collect info for retained nodes.
1985 for (const DINode *DN : SP->getRetainedNodes()) {
1986 const auto *LS = getRetainedNodeScope(DN);
1987 if (isa<DILocalVariable>(DN) || isa<DILabel>(DN)) {
1988 if (!Processed.insert(InlinedEntity(DN, nullptr)).second)
1990 LexicalScope *LexS = LScopes.findLexicalScope(LS);
1992 createConcreteEntity(TheCU, *LexS, DN, nullptr);
1994 LocalDeclsPerLS[LS].insert(DN);
1999 // Process beginning of an instruction.
2000 void DwarfDebug::beginInstruction(const MachineInstr *MI) {
2001 const MachineFunction &MF = *MI->getMF();
2002 const auto *SP = MF.getFunction().getSubprogram();
2004 !SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug;
2006 // Delay slot support check.
2007 auto delaySlotSupported = [](const MachineInstr &MI) {
2008 if (!MI.isBundledWithSucc())
2010 auto Suc = std::next(MI.getIterator());
2012 // Ensure that delay slot instruction is successor of the call instruction.
2013 // Ex. CALL_INSTRUCTION {
2014 // DELAY_SLOT_INSTRUCTION }
2015 assert(Suc->isBundledWithPred() &&
2016 "Call bundle instructions are out of order");
2020 // When describing calls, we need a label for the call instruction.
2021 if (!NoDebug && SP->areAllCallsDescribed() &&
2022 MI->isCandidateForCallSiteEntry(MachineInstr::AnyInBundle) &&
2023 (!MI->hasDelaySlot() || delaySlotSupported(*MI))) {
2024 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2025 bool IsTail = TII->isTailCall(*MI);
2026 // For tail calls, we need the address of the branch instruction for
2029 requestLabelBeforeInsn(MI);
2030 // For non-tail calls, we need the return address for the call for
2031 // DW_AT_call_return_pc. Under GDB tuning, this information is needed for
2032 // tail calls as well.
2033 requestLabelAfterInsn(MI);
2036 DebugHandlerBase::beginInstruction(MI);
2043 // Check if source location changes, but ignore DBG_VALUE and CFI locations.
2044 // If the instruction is part of the function frame setup code, do not emit
2045 // any line record, as there is no correspondence with any user code.
2046 if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup))
2048 const DebugLoc &DL = MI->getDebugLoc();
2051 if (MI->getFlag(MachineInstr::FrameDestroy) && DL) {
2052 const MachineBasicBlock *MBB = MI->getParent();
2053 if (MBB && (MBB != EpilogBeginBlock)) {
2054 // First time FrameDestroy has been seen in this basic block
2055 EpilogBeginBlock = MBB;
2056 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2060 // When we emit a line-0 record, we don't update PrevInstLoc; so look at
2061 // the last line number actually emitted, to see if it was line 0.
2062 unsigned LastAsmLine =
2063 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
2065 bool PrevInstInSameSection =
2067 PrevInstBB->getSectionIDNum() == MI->getParent()->getSectionIDNum());
2068 if (DL == PrevInstLoc && PrevInstInSameSection) {
2069 // If we have an ongoing unspecified location, nothing to do here.
2072 // We have an explicit location, same as the previous location.
2073 // But we might be coming back to it after a line 0 record.
2074 if ((LastAsmLine == 0 && DL.getLine() != 0) || Flags) {
2075 // Reinstate the source location but not marked as a statement.
2076 const MDNode *Scope = DL.getScope();
2077 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
2083 // We have an unspecified location, which might want to be line 0.
2084 // If we have already emitted a line-0 record, don't repeat it.
2085 if (LastAsmLine == 0)
2087 // If user said Don't Do That, don't do that.
2088 if (UnknownLocations == Disable)
2090 // See if we have a reason to emit a line-0 record now.
2091 // Reasons to emit a line-0 record include:
2092 // - User asked for it (UnknownLocations).
2093 // - Instruction has a label, so it's referenced from somewhere else,
2094 // possibly debug information; we want it to have a source location.
2095 // - Instruction is at the top of a block; we don't want to inherit the
2096 // location from the physically previous (maybe unrelated) block.
2097 if (UnknownLocations == Enable || PrevLabel ||
2098 (PrevInstBB && PrevInstBB != MI->getParent())) {
2099 // Preserve the file and column numbers, if we can, to save space in
2100 // the encoded line table.
2101 // Do not update PrevInstLoc, it remembers the last non-0 line.
2102 const MDNode *Scope = nullptr;
2103 unsigned Column = 0;
2105 Scope = PrevInstLoc.getScope();
2106 Column = PrevInstLoc.getCol();
2108 recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
2113 // We have an explicit location, different from the previous location.
2114 // Don't repeat a line-0 record, but otherwise emit the new location.
2115 // (The new location might be an explicit line 0, which we do emit.)
2116 if (DL.getLine() == 0 && LastAsmLine == 0)
2118 if (DL == PrologEndLoc) {
2119 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
2120 PrologEndLoc = DebugLoc();
2122 // If the line changed, we call that a new statement; unless we went to
2123 // line 0 and came back, in which case it is not a new statement.
2124 unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
2125 if (DL.getLine() && DL.getLine() != OldLine)
2126 Flags |= DWARF2_FLAG_IS_STMT;
2128 const MDNode *Scope = DL.getScope();
2129 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
2131 // If we're not at line 0, remember this location.
2136 static std::pair<DebugLoc, bool> findPrologueEndLoc(const MachineFunction *MF) {
2137 // First known non-DBG_VALUE and non-frame setup location marks
2138 // the beginning of the function body.
2139 DebugLoc LineZeroLoc;
2140 const Function &F = MF->getFunction();
2142 // Some instructions may be inserted into prologue after this function. Must
2143 // keep prologue for these cases.
2144 bool IsEmptyPrologue =
2145 !(F.hasPrologueData() || F.getMetadata(LLVMContext::MD_func_sanitize));
2146 for (const auto &MBB : *MF) {
2147 for (const auto &MI : MBB) {
2148 if (!MI.isMetaInstruction()) {
2149 if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
2150 // Scan forward to try to find a non-zero line number. The
2151 // prologue_end marks the first breakpoint in the function after the
2152 // frame setup, and a compiler-generated line 0 location is not a
2153 // meaningful breakpoint. If none is found, return the first
2154 // location after the frame setup.
2155 if (MI.getDebugLoc().getLine())
2156 return std::make_pair(MI.getDebugLoc(), IsEmptyPrologue);
2158 LineZeroLoc = MI.getDebugLoc();
2160 IsEmptyPrologue = false;
2164 return std::make_pair(LineZeroLoc, IsEmptyPrologue);
2167 /// Register a source line with debug info. Returns the unique label that was
2168 /// emitted and which provides correspondence to the source line list.
2169 static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col,
2170 const MDNode *S, unsigned Flags, unsigned CUID,
2171 uint16_t DwarfVersion,
2172 ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs) {
2174 unsigned FileNo = 1;
2175 unsigned Discriminator = 0;
2176 if (auto *Scope = cast_or_null<DIScope>(S)) {
2177 Fn = Scope->getFilename();
2178 if (Line != 0 && DwarfVersion >= 4)
2179 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
2180 Discriminator = LBF->getDiscriminator();
2182 FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID])
2183 .getOrCreateSourceID(Scope->getFile());
2185 Asm.OutStreamer->emitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
2189 DebugLoc DwarfDebug::emitInitialLocDirective(const MachineFunction &MF,
2191 std::pair<DebugLoc, bool> PrologEnd = findPrologueEndLoc(&MF);
2192 DebugLoc PrologEndLoc = PrologEnd.first;
2193 bool IsEmptyPrologue = PrologEnd.second;
2195 // Get beginning of function.
2197 // If the prolog is empty, no need to generate scope line for the proc.
2198 if (IsEmptyPrologue)
2199 return PrologEndLoc;
2201 // Ensure the compile unit is created if the function is called before
2203 (void)getOrCreateDwarfCompileUnit(
2204 MF.getFunction().getSubprogram()->getUnit());
2205 // We'd like to list the prologue as "not statements" but GDB behaves
2206 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
2207 const DISubprogram *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
2208 ::recordSourceLine(*Asm, SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT,
2209 CUID, getDwarfVersion(), getUnits());
2210 return PrologEndLoc;
2215 // Gather pre-function debug information. Assumes being called immediately
2216 // after the function entry point has been emitted.
2217 void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
2220 auto *SP = MF->getFunction().getSubprogram();
2221 assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode());
2222 if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
2225 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
2227 Asm->OutStreamer->getContext().setDwarfCompileUnitID(
2228 getDwarfCompileUnitIDForLineTable(CU));
2230 // Record beginning of function.
2231 PrologEndLoc = emitInitialLocDirective(
2232 *MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID());
2236 DwarfDebug::getDwarfCompileUnitIDForLineTable(const DwarfCompileUnit &CU) {
2237 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
2238 // belongs to so that we add to the correct per-cu line table in the
2240 if (Asm->OutStreamer->hasRawTextSupport())
2241 // Use a single line table if we are generating assembly.
2244 return CU.getUniqueID();
2247 void DwarfDebug::terminateLineTable(const DwarfCompileUnit *CU) {
2248 const auto &CURanges = CU->getRanges();
2249 auto &LineTable = Asm->OutStreamer->getContext().getMCDwarfLineTable(
2250 getDwarfCompileUnitIDForLineTable(*CU));
2251 // Add the last range label for the given CU.
2252 LineTable.getMCLineSections().addEndEntry(
2253 const_cast<MCSymbol *>(CURanges.back().End));
2256 void DwarfDebug::skippedNonDebugFunction() {
2257 // If we don't have a subprogram for this function then there will be a hole
2258 // in the range information. Keep note of this by setting the previously used
2259 // section to nullptr.
2260 // Terminate the pending line table.
2262 terminateLineTable(PrevCU);
2267 // Gather and emit post-function debug information.
2268 void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
2269 const DISubprogram *SP = MF->getFunction().getSubprogram();
2271 assert(CurFn == MF &&
2272 "endFunction should be called with the same function as beginFunction");
2274 // Set DwarfDwarfCompileUnitID in MCContext to default value.
2275 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
2277 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
2278 assert(!FnScope || SP == FnScope->getScopeNode());
2279 DwarfCompileUnit &TheCU = getOrCreateDwarfCompileUnit(SP->getUnit());
2280 if (TheCU.getCUNode()->isDebugDirectivesOnly()) {
2281 PrevLabel = nullptr;
2286 DenseSet<InlinedEntity> Processed;
2287 collectEntityInfo(TheCU, SP, Processed);
2289 // Add the range of this function to the list of ranges for the CU.
2290 // With basic block sections, add ranges for all basic block sections.
2291 for (const auto &R : Asm->MBBSectionRanges)
2292 TheCU.addRange({R.second.BeginLabel, R.second.EndLabel});
2294 // Under -gmlt, skip building the subprogram if there are no inlined
2295 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
2296 // is still needed as we need its source location.
2297 if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
2298 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
2299 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
2300 for (const auto &R : Asm->MBBSectionRanges)
2301 addArangeLabel(SymbolCU(&TheCU, R.second.BeginLabel));
2303 assert(InfoHolder.getScopeVariables().empty());
2304 PrevLabel = nullptr;
2310 size_t NumAbstractSubprograms = LScopes.getAbstractScopesList().size();
2312 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
2313 const auto *SP = cast<DISubprogram>(AScope->getScopeNode());
2314 for (const DINode *DN : SP->getRetainedNodes()) {
2315 const auto *LS = getRetainedNodeScope(DN);
2316 // Ensure LexicalScope is created for the scope of this node.
2317 auto *LexS = LScopes.getOrCreateAbstractScope(LS);
2318 assert(LexS && "Expected the LexicalScope to be created.");
2319 if (isa<DILocalVariable>(DN) || isa<DILabel>(DN)) {
2320 // Collect info for variables/labels that were optimized out.
2321 if (!Processed.insert(InlinedEntity(DN, nullptr)).second ||
2322 TheCU.getExistingAbstractEntity(DN))
2324 TheCU.createAbstractEntity(DN, LexS);
2326 // Remember the node if this is a local declarations.
2327 LocalDeclsPerLS[LS].insert(DN);
2330 LScopes.getAbstractScopesList().size() == NumAbstractSubprograms &&
2331 "getOrCreateAbstractScope() inserted an abstract subprogram scope");
2333 constructAbstractSubprogramScopeDIE(TheCU, AScope);
2336 ProcessedSPNodes.insert(SP);
2337 DIE &ScopeDIE = TheCU.constructSubprogramScopeDIE(SP, FnScope);
2338 if (auto *SkelCU = TheCU.getSkeleton())
2339 if (!LScopes.getAbstractScopesList().empty() &&
2340 TheCU.getCUNode()->getSplitDebugInlining())
2341 SkelCU->constructSubprogramScopeDIE(SP, FnScope);
2343 // Construct call site entries.
2344 constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF);
2347 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
2348 // DbgVariables except those that are also in AbstractVariables (since they
2349 // can be used cross-function)
2350 InfoHolder.getScopeVariables().clear();
2351 InfoHolder.getScopeLabels().clear();
2352 LocalDeclsPerLS.clear();
2353 PrevLabel = nullptr;
2357 // Register a source line with debug info. Returns the unique label that was
2358 // emitted and which provides correspondence to the source line list.
2359 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
2361 ::recordSourceLine(*Asm, Line, Col, S, Flags,
2362 Asm->OutStreamer->getContext().getDwarfCompileUnitID(),
2363 getDwarfVersion(), getUnits());
2366 //===----------------------------------------------------------------------===//
2368 //===----------------------------------------------------------------------===//
2370 // Emit the debug info section.
2371 void DwarfDebug::emitDebugInfo() {
2372 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2373 Holder.emitUnits(/* UseOffsets */ false);
2376 // Emit the abbreviation section.
2377 void DwarfDebug::emitAbbreviations() {
2378 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2380 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2383 void DwarfDebug::emitStringOffsetsTableHeader() {
2384 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2385 Holder.getStringPool().emitStringOffsetsTableHeader(
2386 *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(),
2387 Holder.getStringOffsetsStartSym());
2390 template <typename AccelTableT>
2391 void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section,
2392 StringRef TableName) {
2393 Asm->OutStreamer->switchSection(Section);
2395 // Emit the full data.
2396 emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol());
2399 void DwarfDebug::emitAccelDebugNames() {
2400 // Don't emit anything if we have no compilation units to index.
2401 if (getUnits().empty())
2404 emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits());
2407 // Emit visible names into a hashed accelerator table section.
2408 void DwarfDebug::emitAccelNames() {
2409 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
2413 // Emit objective C classes and categories into a hashed accelerator table
2415 void DwarfDebug::emitAccelObjC() {
2416 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
2420 // Emit namespace dies into a hashed accelerator table.
2421 void DwarfDebug::emitAccelNamespaces() {
2422 emitAccel(AccelNamespace,
2423 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
2427 // Emit type dies into a hashed accelerator table.
2428 void DwarfDebug::emitAccelTypes() {
2429 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
2433 // Public name handling.
2434 // The format for the various pubnames:
2436 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2437 // for the DIE that is named.
2439 // gnu pubnames - offset/index value/name tuples where the offset is the offset
2440 // into the CU and the index value is computed according to the type of value
2441 // for the DIE that is named.
2443 // For type units the offset is the offset of the skeleton DIE. For split dwarf
2444 // it's the offset within the debug_info/debug_types dwo section, however, the
2445 // reference in the pubname header doesn't change.
2447 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
2448 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
2450 // Entities that ended up only in a Type Unit reference the CU instead (since
2451 // the pub entry has offsets within the CU there's no real offset that can be
2452 // provided anyway). As it happens all such entities (namespaces and types,
2453 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
2454 // not to be true it would be necessary to persist this information from the
2455 // point at which the entry is added to the index data structure - since by
2456 // the time the index is built from that, the original type/namespace DIE in a
2457 // type unit has already been destroyed so it can't be queried for properties
2459 if (Die->getTag() == dwarf::DW_TAG_compile_unit)
2460 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
2461 dwarf::GIEL_EXTERNAL);
2462 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2464 // We could have a specification DIE that has our most of our knowledge,
2465 // look for that now.
2466 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
2467 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
2468 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
2469 Linkage = dwarf::GIEL_EXTERNAL;
2470 } else if (Die->findAttribute(dwarf::DW_AT_external))
2471 Linkage = dwarf::GIEL_EXTERNAL;
2473 switch (Die->getTag()) {
2474 case dwarf::DW_TAG_class_type:
2475 case dwarf::DW_TAG_structure_type:
2476 case dwarf::DW_TAG_union_type:
2477 case dwarf::DW_TAG_enumeration_type:
2478 return dwarf::PubIndexEntryDescriptor(
2480 dwarf::isCPlusPlus((dwarf::SourceLanguage)CU->getLanguage())
2481 ? dwarf::GIEL_EXTERNAL
2482 : dwarf::GIEL_STATIC);
2483 case dwarf::DW_TAG_typedef:
2484 case dwarf::DW_TAG_base_type:
2485 case dwarf::DW_TAG_subrange_type:
2486 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
2487 case dwarf::DW_TAG_namespace:
2488 return dwarf::GIEK_TYPE;
2489 case dwarf::DW_TAG_subprogram:
2490 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
2491 case dwarf::DW_TAG_variable:
2492 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
2493 case dwarf::DW_TAG_enumerator:
2494 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2495 dwarf::GIEL_STATIC);
2497 return dwarf::GIEK_NONE;
2501 /// emitDebugPubSections - Emit visible names and types into debug pubnames and
2502 /// pubtypes sections.
2503 void DwarfDebug::emitDebugPubSections() {
2504 for (const auto &NU : CUMap) {
2505 DwarfCompileUnit *TheU = NU.second;
2506 if (!TheU->hasDwarfPubSections())
2509 bool GnuStyle = TheU->getCUNode()->getNameTableKind() ==
2510 DICompileUnit::DebugNameTableKind::GNU;
2512 Asm->OutStreamer->switchSection(
2513 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2514 : Asm->getObjFileLowering().getDwarfPubNamesSection());
2515 emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames());
2517 Asm->OutStreamer->switchSection(
2518 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2519 : Asm->getObjFileLowering().getDwarfPubTypesSection());
2520 emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes());
2524 void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) {
2525 if (useSectionsAsReferences())
2526 Asm->emitDwarfOffset(CU.getSection()->getBeginSymbol(),
2527 CU.getDebugSectionOffset());
2529 Asm->emitDwarfSymbolReference(CU.getLabelBegin());
2532 void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
2533 DwarfCompileUnit *TheU,
2534 const StringMap<const DIE *> &Globals) {
2535 if (auto *Skeleton = TheU->getSkeleton())
2539 MCSymbol *EndLabel = Asm->emitDwarfUnitLength(
2540 "pub" + Name, "Length of Public " + Name + " Info");
2542 Asm->OutStreamer->AddComment("DWARF Version");
2543 Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION);
2545 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
2546 emitSectionReference(*TheU);
2548 Asm->OutStreamer->AddComment("Compilation Unit Length");
2549 Asm->emitDwarfLengthOrOffset(TheU->getLength());
2551 // Emit the pubnames for this compilation unit.
2552 SmallVector<std::pair<StringRef, const DIE *>, 0> Vec;
2553 for (const auto &GI : Globals)
2554 Vec.emplace_back(GI.first(), GI.second);
2555 llvm::sort(Vec, [](auto &A, auto &B) {
2556 return A.second->getOffset() < B.second->getOffset();
2558 for (const auto &[Name, Entity] : Vec) {
2559 Asm->OutStreamer->AddComment("DIE offset");
2560 Asm->emitDwarfLengthOrOffset(Entity->getOffset());
2563 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
2564 Asm->OutStreamer->AddComment(
2565 Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) +
2566 ", " + dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
2567 Asm->emitInt8(Desc.toBits());
2570 Asm->OutStreamer->AddComment("External Name");
2571 Asm->OutStreamer->emitBytes(StringRef(Name.data(), Name.size() + 1));
2574 Asm->OutStreamer->AddComment("End Mark");
2575 Asm->emitDwarfLengthOrOffset(0);
2576 Asm->OutStreamer->emitLabel(EndLabel);
2579 /// Emit null-terminated strings into a debug str section.
2580 void DwarfDebug::emitDebugStr() {
2581 MCSection *StringOffsetsSection = nullptr;
2582 if (useSegmentedStringOffsetsTable()) {
2583 emitStringOffsetsTableHeader();
2584 StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection();
2586 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2587 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(),
2588 StringOffsetsSection, /* UseRelativeOffsets = */ true);
2591 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
2592 const DebugLocStream::Entry &Entry,
2593 const DwarfCompileUnit *CU) {
2594 auto &&Comments = DebugLocs.getComments(Entry);
2595 auto Comment = Comments.begin();
2596 auto End = Comments.end();
2598 // The expressions are inserted into a byte stream rather early (see
2599 // DwarfExpression::addExpression) so for those ops (e.g. DW_OP_convert) that
2600 // need to reference a base_type DIE the offset of that DIE is not yet known.
2601 // To deal with this we instead insert a placeholder early and then extract
2602 // it here and replace it with the real reference.
2603 unsigned PtrSize = Asm->MAI->getCodePointerSize();
2604 DWARFDataExtractor Data(StringRef(DebugLocs.getBytes(Entry).data(),
2605 DebugLocs.getBytes(Entry).size()),
2606 Asm->getDataLayout().isLittleEndian(), PtrSize);
2607 DWARFExpression Expr(Data, PtrSize, Asm->OutContext.getDwarfFormat());
2609 using Encoding = DWARFExpression::Operation::Encoding;
2610 uint64_t Offset = 0;
2611 for (const auto &Op : Expr) {
2612 assert(Op.getCode() != dwarf::DW_OP_const_type &&
2613 "3 operand ops not yet supported");
2614 assert(!Op.getSubCode() && "SubOps not yet supported");
2615 Streamer.emitInt8(Op.getCode(), Comment != End ? *(Comment++) : "");
2617 for (unsigned I = 0; I < Op.getDescription().Op.size(); ++I) {
2618 if (Op.getDescription().Op[I] == Encoding::BaseTypeRef) {
2620 Streamer.emitDIERef(*CU->ExprRefedBaseTypes[Op.getRawOperand(I)].Die);
2621 // Make sure comments stay aligned.
2622 for (unsigned J = 0; J < Length; ++J)
2626 for (uint64_t J = Offset; J < Op.getOperandEndOffset(I); ++J)
2627 Streamer.emitInt8(Data.getData()[J], Comment != End ? *(Comment++) : "");
2629 Offset = Op.getOperandEndOffset(I);
2631 assert(Offset == Op.getEndOffset());
2635 void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
2636 const DbgValueLoc &Value,
2637 DwarfExpression &DwarfExpr) {
2638 auto *DIExpr = Value.getExpression();
2639 DIExpressionCursor ExprCursor(DIExpr);
2640 DwarfExpr.addFragmentOffset(DIExpr);
2642 // If the DIExpr is is an Entry Value, we want to follow the same code path
2643 // regardless of whether the DBG_VALUE is variadic or not.
2644 if (DIExpr && DIExpr->isEntryValue()) {
2645 // Entry values can only be a single register with no additional DIExpr,
2646 // so just add it directly.
2647 assert(Value.getLocEntries().size() == 1);
2648 assert(Value.getLocEntries()[0].isLocation());
2649 MachineLocation Location = Value.getLocEntries()[0].getLoc();
2650 DwarfExpr.setLocation(Location, DIExpr);
2652 DwarfExpr.beginEntryValueExpression(ExprCursor);
2654 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
2655 if (!DwarfExpr.addMachineRegExpression(TRI, ExprCursor, Location.getReg()))
2657 return DwarfExpr.addExpression(std::move(ExprCursor));
2661 auto EmitValueLocEntry = [&DwarfExpr, &BT,
2662 &AP](const DbgValueLocEntry &Entry,
2663 DIExpressionCursor &Cursor) -> bool {
2664 if (Entry.isInt()) {
2665 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
2666 BT->getEncoding() == dwarf::DW_ATE_signed_char))
2667 DwarfExpr.addSignedConstant(Entry.getInt());
2669 DwarfExpr.addUnsignedConstant(Entry.getInt());
2670 } else if (Entry.isLocation()) {
2671 MachineLocation Location = Entry.getLoc();
2672 if (Location.isIndirect())
2673 DwarfExpr.setMemoryLocationKind();
2675 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
2676 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
2678 } else if (Entry.isTargetIndexLocation()) {
2679 TargetIndexLocation Loc = Entry.getTargetIndexLocation();
2680 // TODO TargetIndexLocation is a target-independent. Currently only the
2681 // WebAssembly-specific encoding is supported.
2682 assert(AP.TM.getTargetTriple().isWasm());
2683 DwarfExpr.addWasmLocation(Loc.Index, static_cast<uint64_t>(Loc.Offset));
2684 } else if (Entry.isConstantFP()) {
2685 if (AP.getDwarfVersion() >= 4 && !AP.getDwarfDebug()->tuneForSCE() &&
2687 DwarfExpr.addConstantFP(Entry.getConstantFP()->getValueAPF(), AP);
2688 } else if (Entry.getConstantFP()
2691 .getBitWidth() <= 64 /*bits*/) {
2692 DwarfExpr.addUnsignedConstant(
2693 Entry.getConstantFP()->getValueAPF().bitcastToAPInt());
2696 dbgs() << "Skipped DwarfExpression creation for ConstantFP of size"
2697 << Entry.getConstantFP()
2708 if (!Value.isVariadic()) {
2709 if (!EmitValueLocEntry(Value.getLocEntries()[0], ExprCursor))
2711 DwarfExpr.addExpression(std::move(ExprCursor));
2715 // If any of the location entries are registers with the value 0, then the
2716 // location is undefined.
2717 if (any_of(Value.getLocEntries(), [](const DbgValueLocEntry &Entry) {
2718 return Entry.isLocation() && !Entry.getLoc().getReg();
2722 DwarfExpr.addExpression(
2723 std::move(ExprCursor),
2724 [EmitValueLocEntry, &Value](unsigned Idx,
2725 DIExpressionCursor &Cursor) -> bool {
2726 return EmitValueLocEntry(Value.getLocEntries()[Idx], Cursor);
2730 void DebugLocEntry::finalize(const AsmPrinter &AP,
2731 DebugLocStream::ListBuilder &List,
2732 const DIBasicType *BT,
2733 DwarfCompileUnit &TheCU) {
2734 assert(!Values.empty() &&
2735 "location list entries without values are redundant");
2736 assert(Begin != End && "unexpected location list entry with empty range");
2737 DebugLocStream::EntryBuilder Entry(List, Begin, End);
2738 BufferByteStreamer Streamer = Entry.getStreamer();
2739 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer, TheCU);
2740 const DbgValueLoc &Value = Values[0];
2741 if (Value.isFragment()) {
2742 // Emit all fragments that belong to the same variable and range.
2743 assert(llvm::all_of(Values, [](DbgValueLoc P) {
2744 return P.isFragment();
2745 }) && "all values are expected to be fragments");
2746 assert(llvm::is_sorted(Values) && "fragments are expected to be sorted");
2748 for (const auto &Fragment : Values)
2749 DwarfDebug::emitDebugLocValue(AP, BT, Fragment, DwarfExpr);
2752 assert(Values.size() == 1 && "only fragments may have >1 value");
2753 DwarfDebug::emitDebugLocValue(AP, BT, Value, DwarfExpr);
2755 DwarfExpr.finalize();
2756 if (DwarfExpr.TagOffset)
2757 List.setTagOffset(*DwarfExpr.TagOffset);
2760 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry,
2761 const DwarfCompileUnit *CU) {
2763 Asm->OutStreamer->AddComment("Loc expr size");
2764 if (getDwarfVersion() >= 5)
2765 Asm->emitULEB128(DebugLocs.getBytes(Entry).size());
2766 else if (DebugLocs.getBytes(Entry).size() <= std::numeric_limits<uint16_t>::max())
2767 Asm->emitInt16(DebugLocs.getBytes(Entry).size());
2769 // The entry is too big to fit into 16 bit, drop it as there is nothing we
2775 APByteStreamer Streamer(*Asm);
2776 emitDebugLocEntry(Streamer, Entry, CU);
2779 // Emit the header of a DWARF 5 range list table list table. Returns the symbol
2780 // that designates the end of the table for the caller to emit when the table is
2782 static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm,
2783 const DwarfFile &Holder) {
2784 MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer);
2786 Asm->OutStreamer->AddComment("Offset entry count");
2787 Asm->emitInt32(Holder.getRangeLists().size());
2788 Asm->OutStreamer->emitLabel(Holder.getRnglistsTableBaseSym());
2790 for (const RangeSpanList &List : Holder.getRangeLists())
2791 Asm->emitLabelDifference(List.Label, Holder.getRnglistsTableBaseSym(),
2792 Asm->getDwarfOffsetByteSize());
2797 // Emit the header of a DWARF 5 locations list table. Returns the symbol that
2798 // designates the end of the table for the caller to emit when the table is
2800 static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm,
2801 const DwarfDebug &DD) {
2802 MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer);
2804 const auto &DebugLocs = DD.getDebugLocs();
2806 Asm->OutStreamer->AddComment("Offset entry count");
2807 Asm->emitInt32(DebugLocs.getLists().size());
2808 Asm->OutStreamer->emitLabel(DebugLocs.getSym());
2810 for (const auto &List : DebugLocs.getLists())
2811 Asm->emitLabelDifference(List.Label, DebugLocs.getSym(),
2812 Asm->getDwarfOffsetByteSize());
2817 template <typename Ranges, typename PayloadEmitter>
2818 static void emitRangeList(
2819 DwarfDebug &DD, AsmPrinter *Asm, MCSymbol *Sym, const Ranges &R,
2820 const DwarfCompileUnit &CU, unsigned BaseAddressx, unsigned OffsetPair,
2821 unsigned StartxLength, unsigned EndOfList,
2822 StringRef (*StringifyEnum)(unsigned),
2823 bool ShouldUseBaseAddress,
2824 PayloadEmitter EmitPayload) {
2826 auto Size = Asm->MAI->getCodePointerSize();
2827 bool UseDwarf5 = DD.getDwarfVersion() >= 5;
2829 // Emit our symbol so we can find the beginning of the range.
2830 Asm->OutStreamer->emitLabel(Sym);
2832 // Gather all the ranges that apply to the same section so they can share
2833 // a base address entry.
2834 MapVector<const MCSection *, std::vector<decltype(&*R.begin())>> SectionRanges;
2836 for (const auto &Range : R)
2837 SectionRanges[&Range.Begin->getSection()].push_back(&Range);
2839 const MCSymbol *CUBase = CU.getBaseAddress();
2840 bool BaseIsSet = false;
2841 for (const auto &P : SectionRanges) {
2842 auto *Base = CUBase;
2843 if (!Base && ShouldUseBaseAddress) {
2844 const MCSymbol *Begin = P.second.front()->Begin;
2845 const MCSymbol *NewBase = DD.getSectionLabel(&Begin->getSection());
2849 Asm->OutStreamer->emitIntValue(-1, Size);
2850 Asm->OutStreamer->AddComment(" base address");
2851 Asm->OutStreamer->emitSymbolValue(Base, Size);
2852 } else if (NewBase != Begin || P.second.size() > 1) {
2853 // Only use a base address if
2854 // * the existing pool address doesn't match (NewBase != Begin)
2855 // * or, there's more than one entry to share the base address
2858 Asm->OutStreamer->AddComment(StringifyEnum(BaseAddressx));
2859 Asm->emitInt8(BaseAddressx);
2860 Asm->OutStreamer->AddComment(" base address index");
2861 Asm->emitULEB128(DD.getAddressPool().getIndex(Base));
2863 } else if (BaseIsSet && !UseDwarf5) {
2866 Asm->OutStreamer->emitIntValue(-1, Size);
2867 Asm->OutStreamer->emitIntValue(0, Size);
2870 for (const auto *RS : P.second) {
2871 const MCSymbol *Begin = RS->Begin;
2872 const MCSymbol *End = RS->End;
2873 assert(Begin && "Range without a begin symbol?");
2874 assert(End && "Range without an end symbol?");
2877 // Emit offset_pair when we have a base.
2878 Asm->OutStreamer->AddComment(StringifyEnum(OffsetPair));
2879 Asm->emitInt8(OffsetPair);
2880 Asm->OutStreamer->AddComment(" starting offset");
2881 Asm->emitLabelDifferenceAsULEB128(Begin, Base);
2882 Asm->OutStreamer->AddComment(" ending offset");
2883 Asm->emitLabelDifferenceAsULEB128(End, Base);
2885 Asm->emitLabelDifference(Begin, Base, Size);
2886 Asm->emitLabelDifference(End, Base, Size);
2888 } else if (UseDwarf5) {
2889 Asm->OutStreamer->AddComment(StringifyEnum(StartxLength));
2890 Asm->emitInt8(StartxLength);
2891 Asm->OutStreamer->AddComment(" start index");
2892 Asm->emitULEB128(DD.getAddressPool().getIndex(Begin));
2893 Asm->OutStreamer->AddComment(" length");
2894 Asm->emitLabelDifferenceAsULEB128(End, Begin);
2896 Asm->OutStreamer->emitSymbolValue(Begin, Size);
2897 Asm->OutStreamer->emitSymbolValue(End, Size);
2904 Asm->OutStreamer->AddComment(StringifyEnum(EndOfList));
2905 Asm->emitInt8(EndOfList);
2907 // Terminate the list with two 0 values.
2908 Asm->OutStreamer->emitIntValue(0, Size);
2909 Asm->OutStreamer->emitIntValue(0, Size);
2913 // Handles emission of both debug_loclist / debug_loclist.dwo
2914 static void emitLocList(DwarfDebug &DD, AsmPrinter *Asm, const DebugLocStream::List &List) {
2915 emitRangeList(DD, Asm, List.Label, DD.getDebugLocs().getEntries(List),
2916 *List.CU, dwarf::DW_LLE_base_addressx,
2917 dwarf::DW_LLE_offset_pair, dwarf::DW_LLE_startx_length,
2918 dwarf::DW_LLE_end_of_list, llvm::dwarf::LocListEncodingString,
2919 /* ShouldUseBaseAddress */ true,
2920 [&](const DebugLocStream::Entry &E) {
2921 DD.emitDebugLocEntryLocation(E, List.CU);
2925 void DwarfDebug::emitDebugLocImpl(MCSection *Sec) {
2926 if (DebugLocs.getLists().empty())
2929 Asm->OutStreamer->switchSection(Sec);
2931 MCSymbol *TableEnd = nullptr;
2932 if (getDwarfVersion() >= 5)
2933 TableEnd = emitLoclistsTableHeader(Asm, *this);
2935 for (const auto &List : DebugLocs.getLists())
2936 emitLocList(*this, Asm, List);
2939 Asm->OutStreamer->emitLabel(TableEnd);
2942 // Emit locations into the .debug_loc/.debug_loclists section.
2943 void DwarfDebug::emitDebugLoc() {
2945 getDwarfVersion() >= 5
2946 ? Asm->getObjFileLowering().getDwarfLoclistsSection()
2947 : Asm->getObjFileLowering().getDwarfLocSection());
2950 // Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section.
2951 void DwarfDebug::emitDebugLocDWO() {
2952 if (getDwarfVersion() >= 5) {
2954 Asm->getObjFileLowering().getDwarfLoclistsDWOSection());
2959 for (const auto &List : DebugLocs.getLists()) {
2960 Asm->OutStreamer->switchSection(
2961 Asm->getObjFileLowering().getDwarfLocDWOSection());
2962 Asm->OutStreamer->emitLabel(List.Label);
2964 for (const auto &Entry : DebugLocs.getEntries(List)) {
2965 // GDB only supports startx_length in pre-standard split-DWARF.
2966 // (in v5 standard loclists, it currently* /only/ supports base_address +
2967 // offset_pair, so the implementations can't really share much since they
2968 // need to use different representations)
2969 // * as of October 2018, at least
2971 // In v5 (see emitLocList), this uses SectionLabels to reuse existing
2972 // addresses in the address pool to minimize object size/relocations.
2973 Asm->emitInt8(dwarf::DW_LLE_startx_length);
2974 unsigned idx = AddrPool.getIndex(Entry.Begin);
2975 Asm->emitULEB128(idx);
2976 // Also the pre-standard encoding is slightly different, emitting this as
2977 // an address-length entry here, but its a ULEB128 in DWARFv5 loclists.
2978 Asm->emitLabelDifference(Entry.End, Entry.Begin, 4);
2979 emitDebugLocEntryLocation(Entry, List.CU);
2981 Asm->emitInt8(dwarf::DW_LLE_end_of_list);
2986 const MCSymbol *Start, *End;
2989 // Emit a debug aranges section, containing a CU lookup for any
2990 // address we can tie back to a CU.
2991 void DwarfDebug::emitDebugARanges() {
2992 // Provides a unique id per text section.
2993 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
2995 // Filter labels by section.
2996 for (const SymbolCU &SCU : ArangeLabels) {
2997 if (SCU.Sym->isInSection()) {
2998 // Make a note of this symbol and it's section.
2999 MCSection *Section = &SCU.Sym->getSection();
3000 if (!Section->getKind().isMetadata())
3001 SectionMap[Section].push_back(SCU);
3003 // Some symbols (e.g. common/bss on mach-o) can have no section but still
3004 // appear in the output. This sucks as we rely on sections to build
3005 // arange spans. We can do it without, but it's icky.
3006 SectionMap[nullptr].push_back(SCU);
3010 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
3012 for (auto &I : SectionMap) {
3013 MCSection *Section = I.first;
3014 SmallVector<SymbolCU, 8> &List = I.second;
3015 if (List.size() < 1)
3018 // If we have no section (e.g. common), just write out
3019 // individual spans for each symbol.
3021 for (const SymbolCU &Cur : List) {
3023 Span.Start = Cur.Sym;
3026 Spans[Cur.CU].push_back(Span);
3031 // Sort the symbols by offset within the section.
3032 llvm::stable_sort(List, [&](const SymbolCU &A, const SymbolCU &B) {
3033 unsigned IA = A.Sym ? Asm->OutStreamer->getSymbolOrder(A.Sym) : 0;
3034 unsigned IB = B.Sym ? Asm->OutStreamer->getSymbolOrder(B.Sym) : 0;
3036 // Symbols with no order assigned should be placed at the end.
3037 // (e.g. section end labels)
3045 // Insert a final terminator.
3046 List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
3048 // Build spans between each label.
3049 const MCSymbol *StartSym = List[0].Sym;
3050 for (size_t n = 1, e = List.size(); n < e; n++) {
3051 const SymbolCU &Prev = List[n - 1];
3052 const SymbolCU &Cur = List[n];
3054 // Try and build the longest span we can within the same CU.
3055 if (Cur.CU != Prev.CU) {
3057 Span.Start = StartSym;
3060 Spans[Prev.CU].push_back(Span);
3066 // Start the dwarf aranges section.
3067 Asm->OutStreamer->switchSection(
3068 Asm->getObjFileLowering().getDwarfARangesSection());
3070 unsigned PtrSize = Asm->MAI->getCodePointerSize();
3072 // Build a list of CUs used.
3073 std::vector<DwarfCompileUnit *> CUs;
3074 for (const auto &it : Spans) {
3075 DwarfCompileUnit *CU = it.first;
3079 // Sort the CU list (again, to ensure consistent output order).
3080 llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
3081 return A->getUniqueID() < B->getUniqueID();
3084 // Emit an arange table for each CU we used.
3085 for (DwarfCompileUnit *CU : CUs) {
3086 std::vector<ArangeSpan> &List = Spans[CU];
3088 // Describe the skeleton CU's offset and length, not the dwo file's.
3089 if (auto *Skel = CU->getSkeleton())
3092 // Emit size of content not including length itself.
3093 unsigned ContentSize =
3094 sizeof(int16_t) + // DWARF ARange version number
3095 Asm->getDwarfOffsetByteSize() + // Offset of CU in the .debug_info
3097 sizeof(int8_t) + // Pointer Size (in bytes)
3098 sizeof(int8_t); // Segment Size (in bytes)
3100 unsigned TupleSize = PtrSize * 2;
3102 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
3103 unsigned Padding = offsetToAlignment(
3104 Asm->getUnitLengthFieldByteSize() + ContentSize, Align(TupleSize));
3106 ContentSize += Padding;
3107 ContentSize += (List.size() + 1) * TupleSize;
3109 // For each compile unit, write the list of spans it covers.
3110 Asm->emitDwarfUnitLength(ContentSize, "Length of ARange Set");
3111 Asm->OutStreamer->AddComment("DWARF Arange version number");
3112 Asm->emitInt16(dwarf::DW_ARANGES_VERSION);
3113 Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
3114 emitSectionReference(*CU);
3115 Asm->OutStreamer->AddComment("Address Size (in bytes)");
3116 Asm->emitInt8(PtrSize);
3117 Asm->OutStreamer->AddComment("Segment Size (in bytes)");
3120 Asm->OutStreamer->emitFill(Padding, 0xff);
3122 for (const ArangeSpan &Span : List) {
3123 Asm->emitLabelReference(Span.Start, PtrSize);
3125 // Calculate the size as being from the span start to its end.
3127 // If the size is zero, then round it up to one byte. The DWARF
3128 // specification requires that entries in this table have nonzero
3130 auto SizeRef = SymSize.find(Span.Start);
3131 if ((SizeRef == SymSize.end() || SizeRef->second != 0) && Span.End) {
3132 Asm->emitLabelDifference(Span.End, Span.Start, PtrSize);
3134 // For symbols without an end marker (e.g. common), we
3135 // write a single arange entry containing just that one symbol.
3137 if (SizeRef == SymSize.end() || SizeRef->second == 0)
3140 Size = SizeRef->second;
3142 Asm->OutStreamer->emitIntValue(Size, PtrSize);
3146 Asm->OutStreamer->AddComment("ARange terminator");
3147 Asm->OutStreamer->emitIntValue(0, PtrSize);
3148 Asm->OutStreamer->emitIntValue(0, PtrSize);
3152 /// Emit a single range list. We handle both DWARF v5 and earlier.
3153 static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm,
3154 const RangeSpanList &List) {
3155 emitRangeList(DD, Asm, List.Label, List.Ranges, *List.CU,
3156 dwarf::DW_RLE_base_addressx, dwarf::DW_RLE_offset_pair,
3157 dwarf::DW_RLE_startx_length, dwarf::DW_RLE_end_of_list,
3158 llvm::dwarf::RangeListEncodingString,
3159 List.CU->getCUNode()->getRangesBaseAddress() ||
3160 DD.getDwarfVersion() >= 5,
3164 void DwarfDebug::emitDebugRangesImpl(const DwarfFile &Holder, MCSection *Section) {
3165 if (Holder.getRangeLists().empty())
3168 assert(useRangesSection());
3169 assert(!CUMap.empty());
3170 assert(llvm::any_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
3171 return !Pair.second->getCUNode()->isDebugDirectivesOnly();
3174 Asm->OutStreamer->switchSection(Section);
3176 MCSymbol *TableEnd = nullptr;
3177 if (getDwarfVersion() >= 5)
3178 TableEnd = emitRnglistsTableHeader(Asm, Holder);
3180 for (const RangeSpanList &List : Holder.getRangeLists())
3181 emitRangeList(*this, Asm, List);
3184 Asm->OutStreamer->emitLabel(TableEnd);
3187 /// Emit address ranges into the .debug_ranges section or into the DWARF v5
3188 /// .debug_rnglists section.
3189 void DwarfDebug::emitDebugRanges() {
3190 const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
3192 emitDebugRangesImpl(Holder,
3193 getDwarfVersion() >= 5
3194 ? Asm->getObjFileLowering().getDwarfRnglistsSection()
3195 : Asm->getObjFileLowering().getDwarfRangesSection());
3198 void DwarfDebug::emitDebugRangesDWO() {
3199 emitDebugRangesImpl(InfoHolder,
3200 Asm->getObjFileLowering().getDwarfRnglistsDWOSection());
3203 /// Emit the header of a DWARF 5 macro section, or the GNU extension for
3205 static void emitMacroHeader(AsmPrinter *Asm, const DwarfDebug &DD,
3206 const DwarfCompileUnit &CU, uint16_t DwarfVersion) {
3207 enum HeaderFlagMask {
3208 #define HANDLE_MACRO_FLAG(ID, NAME) MACRO_FLAG_##NAME = ID,
3209 #include "llvm/BinaryFormat/Dwarf.def"
3211 Asm->OutStreamer->AddComment("Macro information version");
3212 Asm->emitInt16(DwarfVersion >= 5 ? DwarfVersion : 4);
3213 // We emit the line offset flag unconditionally here, since line offset should
3214 // be mostly present.
3215 if (Asm->isDwarf64()) {
3216 Asm->OutStreamer->AddComment("Flags: 64 bit, debug_line_offset present");
3217 Asm->emitInt8(MACRO_FLAG_OFFSET_SIZE | MACRO_FLAG_DEBUG_LINE_OFFSET);
3219 Asm->OutStreamer->AddComment("Flags: 32 bit, debug_line_offset present");
3220 Asm->emitInt8(MACRO_FLAG_DEBUG_LINE_OFFSET);
3222 Asm->OutStreamer->AddComment("debug_line_offset");
3223 if (DD.useSplitDwarf())
3224 Asm->emitDwarfLengthOrOffset(0);
3226 Asm->emitDwarfSymbolReference(CU.getLineTableStartSym());
3229 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
3230 for (auto *MN : Nodes) {
3231 if (auto *M = dyn_cast<DIMacro>(MN))
3233 else if (auto *F = dyn_cast<DIMacroFile>(MN))
3234 emitMacroFile(*F, U);
3236 llvm_unreachable("Unexpected DI type!");
3240 void DwarfDebug::emitMacro(DIMacro &M) {
3241 StringRef Name = M.getName();
3242 StringRef Value = M.getValue();
3244 // There should be one space between the macro name and the macro value in
3245 // define entries. In undef entries, only the macro name is emitted.
3246 std::string Str = Value.empty() ? Name.str() : (Name + " " + Value).str();
3248 if (UseDebugMacroSection) {
3249 if (getDwarfVersion() >= 5) {
3250 unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define
3251 ? dwarf::DW_MACRO_define_strx
3252 : dwarf::DW_MACRO_undef_strx;
3253 Asm->OutStreamer->AddComment(dwarf::MacroString(Type));
3254 Asm->emitULEB128(Type);
3255 Asm->OutStreamer->AddComment("Line Number");
3256 Asm->emitULEB128(M.getLine());
3257 Asm->OutStreamer->AddComment("Macro String");
3259 InfoHolder.getStringPool().getIndexedEntry(*Asm, Str).getIndex());
3261 unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define
3262 ? dwarf::DW_MACRO_GNU_define_indirect
3263 : dwarf::DW_MACRO_GNU_undef_indirect;
3264 Asm->OutStreamer->AddComment(dwarf::GnuMacroString(Type));
3265 Asm->emitULEB128(Type);
3266 Asm->OutStreamer->AddComment("Line Number");
3267 Asm->emitULEB128(M.getLine());
3268 Asm->OutStreamer->AddComment("Macro String");
3269 Asm->emitDwarfSymbolReference(
3270 InfoHolder.getStringPool().getEntry(*Asm, Str).getSymbol());
3273 Asm->OutStreamer->AddComment(dwarf::MacinfoString(M.getMacinfoType()));
3274 Asm->emitULEB128(M.getMacinfoType());
3275 Asm->OutStreamer->AddComment("Line Number");
3276 Asm->emitULEB128(M.getLine());
3277 Asm->OutStreamer->AddComment("Macro String");
3278 Asm->OutStreamer->emitBytes(Str);
3279 Asm->emitInt8('\0');
3283 void DwarfDebug::emitMacroFileImpl(
3284 DIMacroFile &MF, DwarfCompileUnit &U, unsigned StartFile, unsigned EndFile,
3285 StringRef (*MacroFormToString)(unsigned Form)) {
3287 Asm->OutStreamer->AddComment(MacroFormToString(StartFile));
3288 Asm->emitULEB128(StartFile);
3289 Asm->OutStreamer->AddComment("Line Number");
3290 Asm->emitULEB128(MF.getLine());
3291 Asm->OutStreamer->AddComment("File Number");
3292 DIFile &F = *MF.getFile();
3293 if (useSplitDwarf())
3294 Asm->emitULEB128(getDwoLineTable(U)->getFile(
3295 F.getDirectory(), F.getFilename(), getMD5AsBytes(&F),
3296 Asm->OutContext.getDwarfVersion(), F.getSource()));
3298 Asm->emitULEB128(U.getOrCreateSourceID(&F));
3299 handleMacroNodes(MF.getElements(), U);
3300 Asm->OutStreamer->AddComment(MacroFormToString(EndFile));
3301 Asm->emitULEB128(EndFile);
3304 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
3305 // DWARFv5 macro and DWARFv4 macinfo share some common encodings,
3306 // so for readibility/uniformity, We are explicitly emitting those.
3307 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
3308 if (UseDebugMacroSection)
3310 F, U, dwarf::DW_MACRO_start_file, dwarf::DW_MACRO_end_file,
3311 (getDwarfVersion() >= 5) ? dwarf::MacroString : dwarf::GnuMacroString);
3313 emitMacroFileImpl(F, U, dwarf::DW_MACINFO_start_file,
3314 dwarf::DW_MACINFO_end_file, dwarf::MacinfoString);
3317 void DwarfDebug::emitDebugMacinfoImpl(MCSection *Section) {
3318 for (const auto &P : CUMap) {
3319 auto &TheCU = *P.second;
3320 auto *SkCU = TheCU.getSkeleton();
3321 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
3322 auto *CUNode = cast<DICompileUnit>(P.first);
3323 DIMacroNodeArray Macros = CUNode->getMacros();
3326 Asm->OutStreamer->switchSection(Section);
3327 Asm->OutStreamer->emitLabel(U.getMacroLabelBegin());
3328 if (UseDebugMacroSection)
3329 emitMacroHeader(Asm, *this, U, getDwarfVersion());
3330 handleMacroNodes(Macros, U);
3331 Asm->OutStreamer->AddComment("End Of Macro List Mark");
3336 /// Emit macros into a debug macinfo/macro section.
3337 void DwarfDebug::emitDebugMacinfo() {
3338 auto &ObjLower = Asm->getObjFileLowering();
3339 emitDebugMacinfoImpl(UseDebugMacroSection
3340 ? ObjLower.getDwarfMacroSection()
3341 : ObjLower.getDwarfMacinfoSection());
3344 void DwarfDebug::emitDebugMacinfoDWO() {
3345 auto &ObjLower = Asm->getObjFileLowering();
3346 emitDebugMacinfoImpl(UseDebugMacroSection
3347 ? ObjLower.getDwarfMacroDWOSection()
3348 : ObjLower.getDwarfMacinfoDWOSection());
3351 // DWARF5 Experimental Separate Dwarf emitters.
3353 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
3354 std::unique_ptr<DwarfCompileUnit> NewU) {
3356 if (!CompilationDir.empty())
3357 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
3358 addGnuPubAttributes(*NewU, Die);
3360 SkeletonHolder.addUnit(std::move(NewU));
3363 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
3365 auto OwnedUnit = std::make_unique<DwarfCompileUnit>(
3366 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder,
3367 UnitKind::Skeleton);
3368 DwarfCompileUnit &NewCU = *OwnedUnit;
3369 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
3371 NewCU.initStmtList();
3373 if (useSegmentedStringOffsetsTable())
3374 NewCU.addStringOffsetsStart();
3376 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
3381 // Emit the .debug_info.dwo section for separated dwarf. This contains the
3382 // compile units that would normally be in debug_info.
3383 void DwarfDebug::emitDebugInfoDWO() {
3384 assert(useSplitDwarf() && "No split dwarf debug info?");
3385 // Don't emit relocations into the dwo file.
3386 InfoHolder.emitUnits(/* UseOffsets */ true);
3389 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3390 // abbreviations for the .debug_info.dwo section.
3391 void DwarfDebug::emitDebugAbbrevDWO() {
3392 assert(useSplitDwarf() && "No split dwarf?");
3393 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
3396 void DwarfDebug::emitDebugLineDWO() {
3397 assert(useSplitDwarf() && "No split dwarf?");
3398 SplitTypeUnitFileTable.Emit(
3399 *Asm->OutStreamer, MCDwarfLineTableParams(),
3400 Asm->getObjFileLowering().getDwarfLineDWOSection());
3403 void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
3404 assert(useSplitDwarf() && "No split dwarf?");
3405 InfoHolder.getStringPool().emitStringOffsetsTableHeader(
3406 *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(),
3407 InfoHolder.getStringOffsetsStartSym());
3410 // Emit the .debug_str.dwo section for separated dwarf. This contains the
3411 // string section and is identical in format to traditional .debug_str
3413 void DwarfDebug::emitDebugStrDWO() {
3414 if (useSegmentedStringOffsetsTable())
3415 emitStringOffsetsTableHeaderDWO();
3416 assert(useSplitDwarf() && "No split dwarf?");
3417 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
3418 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
3419 OffSec, /* UseRelativeOffsets = */ false);
3422 // Emit address pool.
3423 void DwarfDebug::emitDebugAddr() {
3424 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
3427 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
3428 if (!useSplitDwarf())
3430 const DICompileUnit *DIUnit = CU.getCUNode();
3431 SplitTypeUnitFileTable.maybeSetRootFile(
3432 DIUnit->getDirectory(), DIUnit->getFilename(),
3433 getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource());
3434 return &SplitTypeUnitFileTable;
3437 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
3439 Hash.update(Identifier);
3440 // ... take the least significant 8 bytes and return those. Our MD5
3441 // implementation always returns its results in little endian, so we actually
3442 // need the "high" word.
3443 MD5::MD5Result Result;
3445 return Result.high();
3448 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
3449 StringRef Identifier, DIE &RefDie,
3450 const DICompositeType *CTy) {
3451 // Fast path if we're building some type units and one has already used the
3452 // address pool we know we're going to throw away all this work anyway, so
3453 // don't bother building dependent types.
3454 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
3457 auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
3459 CU.addDIETypeSignature(RefDie, Ins.first->second);
3463 bool TopLevelType = TypeUnitsUnderConstruction.empty();
3464 AddrPool.resetUsedFlag();
3466 auto OwnedUnit = std::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
3467 getDwoLineTable(CU));
3468 DwarfTypeUnit &NewTU = *OwnedUnit;
3469 DIE &UnitDie = NewTU.getUnitDie();
3470 TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
3472 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
3475 uint64_t Signature = makeTypeSignature(Identifier);
3476 NewTU.setTypeSignature(Signature);
3477 Ins.first->second = Signature;
3479 if (useSplitDwarf()) {
3480 MCSection *Section =
3481 getDwarfVersion() <= 4
3482 ? Asm->getObjFileLowering().getDwarfTypesDWOSection()
3483 : Asm->getObjFileLowering().getDwarfInfoDWOSection();
3484 NewTU.setSection(Section);
3486 MCSection *Section =
3487 getDwarfVersion() <= 4
3488 ? Asm->getObjFileLowering().getDwarfTypesSection(Signature)
3489 : Asm->getObjFileLowering().getDwarfInfoSection(Signature);
3490 NewTU.setSection(Section);
3491 // Non-split type units reuse the compile unit's line table.
3492 CU.applyStmtList(UnitDie);
3495 // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
3497 if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
3498 NewTU.addStringOffsetsStart();
3500 NewTU.setType(NewTU.createTypeDIE(CTy));
3503 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
3504 TypeUnitsUnderConstruction.clear();
3506 // Types referencing entries in the address table cannot be placed in type
3508 if (AddrPool.hasBeenUsed()) {
3510 // Remove all the types built while building this type.
3511 // This is pessimistic as some of these types might not be dependent on
3512 // the type that used an address.
3513 for (const auto &TU : TypeUnitsToAdd)
3514 TypeSignatures.erase(TU.second);
3516 // Construct this type in the CU directly.
3517 // This is inefficient because all the dependent types will be rebuilt
3518 // from scratch, including building them in type units, discovering that
3519 // they depend on addresses, throwing them out and rebuilding them.
3520 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
3524 // If the type wasn't dependent on fission addresses, finish adding the type
3525 // and all its dependent types.
3526 for (auto &TU : TypeUnitsToAdd) {
3527 InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
3528 InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
3531 CU.addDIETypeSignature(RefDie, Signature);
3534 // Add the Name along with its companion DIE to the appropriate accelerator
3535 // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
3536 // AccelTableKind::Apple, we use the table we got as an argument). If
3537 // accelerator tables are disabled, this function does nothing.
3538 template <typename DataT>
3539 void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU,
3540 AccelTable<DataT> &AppleAccel, StringRef Name,
3542 if (getAccelTableKind() == AccelTableKind::None || Name.empty())
3545 if (getAccelTableKind() != AccelTableKind::Apple &&
3546 CU.getNameTableKind() != DICompileUnit::DebugNameTableKind::Apple &&
3547 CU.getNameTableKind() != DICompileUnit::DebugNameTableKind::Default)
3550 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
3551 DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name);
3553 switch (getAccelTableKind()) {
3554 case AccelTableKind::Apple:
3555 AppleAccel.addName(Ref, Die);
3557 case AccelTableKind::Dwarf:
3558 AccelDebugNames.addName(Ref, Die);
3560 case AccelTableKind::Default:
3561 llvm_unreachable("Default should have already been resolved.");
3562 case AccelTableKind::None:
3563 llvm_unreachable("None handled above");
3567 void DwarfDebug::addAccelName(const DICompileUnit &CU, StringRef Name,
3569 addAccelNameImpl(CU, AccelNames, Name, Die);
3572 void DwarfDebug::addAccelObjC(const DICompileUnit &CU, StringRef Name,
3574 // ObjC names go only into the Apple accelerator tables.
3575 if (getAccelTableKind() == AccelTableKind::Apple)
3576 addAccelNameImpl(CU, AccelObjC, Name, Die);
3579 void DwarfDebug::addAccelNamespace(const DICompileUnit &CU, StringRef Name,
3581 addAccelNameImpl(CU, AccelNamespace, Name, Die);
3584 void DwarfDebug::addAccelType(const DICompileUnit &CU, StringRef Name,
3585 const DIE &Die, char Flags) {
3586 addAccelNameImpl(CU, AccelTypes, Name, Die);
3589 uint16_t DwarfDebug::getDwarfVersion() const {
3590 return Asm->OutStreamer->getContext().getDwarfVersion();
3593 dwarf::Form DwarfDebug::getDwarfSectionOffsetForm() const {
3594 if (Asm->getDwarfVersion() >= 4)
3595 return dwarf::Form::DW_FORM_sec_offset;
3596 assert((!Asm->isDwarf64() || (Asm->getDwarfVersion() == 3)) &&
3597 "DWARF64 is not defined prior DWARFv3");
3598 return Asm->isDwarf64() ? dwarf::Form::DW_FORM_data8
3599 : dwarf::Form::DW_FORM_data4;
3602 const MCSymbol *DwarfDebug::getSectionLabel(const MCSection *S) {
3603 return SectionLabels.lookup(S);
3606 void DwarfDebug::insertSectionLabel(const MCSymbol *S) {
3607 if (SectionLabels.insert(std::make_pair(&S->getSection(), S)).second)
3608 if (useSplitDwarf() || getDwarfVersion() >= 5)
3609 AddrPool.getIndex(S);
3612 std::optional<MD5::MD5Result>
3613 DwarfDebug::getMD5AsBytes(const DIFile *File) const {
3615 if (getDwarfVersion() < 5)
3616 return std::nullopt;
3617 std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = File->getChecksum();
3618 if (!Checksum || Checksum->Kind != DIFile::CSK_MD5)
3619 return std::nullopt;
3621 // Convert the string checksum to an MD5Result for the streamer.
3622 // The verifier validates the checksum so we assume it's okay.
3623 // An MD5 checksum is 16 bytes.
3624 std::string ChecksumString = fromHex(Checksum->Value);
3625 MD5::MD5Result CKMem;
3626 std::copy(ChecksumString.begin(), ChecksumString.end(), CKMem.data());
3630 bool DwarfDebug::alwaysUseRanges(const DwarfCompileUnit &CU) const {
3631 if (MinimizeAddr == MinimizeAddrInV5::Ranges)
3633 if (MinimizeAddr != MinimizeAddrInV5::Default)
3635 if (useSplitDwarf())