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 auto SingleLocExprOpt
= DIExpression::convertToNonVariadicExpression(Expr
);
238 const bool IsVariadic
= !SingleLocExprOpt
;
239 // If we have a variadic debug value instruction that is equivalent to a
240 // non-variadic instruction, then convert it to non-variadic form here.
241 if (!IsVariadic
&& !MI
->isNonListDebugValue()) {
242 assert(MI
->getNumDebugOperands() == 1 &&
243 "Mismatched DIExpression and debug operands for debug instruction.");
244 Expr
= *SingleLocExprOpt
;
246 assert(MI
->getNumOperands() >= 3);
247 SmallVector
<DbgValueLocEntry
, 4> DbgValueLocEntries
;
248 for (const MachineOperand
&Op
: MI
->debug_operands()) {
250 MachineLocation
MLoc(Op
.getReg(),
251 MI
->isNonListDebugValue() && MI
->isDebugOffsetImm());
252 DbgValueLocEntries
.push_back(DbgValueLocEntry(MLoc
));
253 } else if (Op
.isTargetIndex()) {
254 DbgValueLocEntries
.push_back(
255 DbgValueLocEntry(TargetIndexLocation(Op
.getIndex(), Op
.getOffset())));
256 } else if (Op
.isImm())
257 DbgValueLocEntries
.push_back(DbgValueLocEntry(Op
.getImm()));
258 else if (Op
.isFPImm())
259 DbgValueLocEntries
.push_back(DbgValueLocEntry(Op
.getFPImm()));
260 else if (Op
.isCImm())
261 DbgValueLocEntries
.push_back(DbgValueLocEntry(Op
.getCImm()));
263 llvm_unreachable("Unexpected debug operand in DBG_VALUE* instruction!");
265 return DbgValueLoc(Expr
, DbgValueLocEntries
, IsVariadic
);
268 static uint64_t getFragmentOffsetInBits(const DIExpression
&Expr
) {
269 std::optional
<DIExpression::FragmentInfo
> Fragment
= Expr
.getFragmentInfo();
270 return Fragment
? Fragment
->OffsetInBits
: 0;
273 bool llvm::operator<(const FrameIndexExpr
&LHS
, const FrameIndexExpr
&RHS
) {
274 return getFragmentOffsetInBits(*LHS
.Expr
) <
275 getFragmentOffsetInBits(*RHS
.Expr
);
278 bool llvm::operator<(const EntryValueInfo
&LHS
, const EntryValueInfo
&RHS
) {
279 return getFragmentOffsetInBits(LHS
.Expr
) < getFragmentOffsetInBits(RHS
.Expr
);
282 Loc::Single::Single(DbgValueLoc ValueLoc
)
283 : ValueLoc(std::make_unique
<DbgValueLoc
>(ValueLoc
)),
284 Expr(ValueLoc
.getExpression()) {
285 if (!Expr
->getNumElements())
289 Loc::Single::Single(const MachineInstr
*DbgValue
)
290 : Single(getDebugLocValue(DbgValue
)) {}
292 const std::set
<FrameIndexExpr
> &Loc::MMI::getFrameIndexExprs() const {
293 return FrameIndexExprs
;
296 void Loc::MMI::addFrameIndexExpr(const DIExpression
*Expr
, int FI
) {
297 FrameIndexExprs
.insert({FI
, Expr
});
298 assert((FrameIndexExprs
.size() == 1 ||
299 llvm::all_of(FrameIndexExprs
,
300 [](const FrameIndexExpr
&FIE
) {
301 return FIE
.Expr
&& FIE
.Expr
->isFragment();
303 "conflicting locations for variable");
306 static AccelTableKind
computeAccelTableKind(unsigned DwarfVersion
,
307 bool GenerateTypeUnits
,
310 // Honor an explicit request.
311 if (AccelTables
!= AccelTableKind::Default
)
314 // Generating DWARF5 acceleration table.
315 // Currently Split dwarf and non ELF format is not supported.
316 if (GenerateTypeUnits
&& (DwarfVersion
< 5 || !TT
.isOSBinFormatELF()))
317 return AccelTableKind::None
;
319 // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5
320 // always implies debug_names. For lower standard versions we use apple
321 // accelerator tables on apple platforms and debug_names elsewhere.
322 if (DwarfVersion
>= 5)
323 return AccelTableKind::Dwarf
;
324 if (Tuning
== DebuggerKind::LLDB
)
325 return TT
.isOSBinFormatMachO() ? AccelTableKind::Apple
326 : AccelTableKind::Dwarf
;
327 return AccelTableKind::None
;
330 DwarfDebug::DwarfDebug(AsmPrinter
*A
)
331 : DebugHandlerBase(A
), DebugLocs(A
->OutStreamer
->isVerboseAsm()),
332 InfoHolder(A
, "info_string", DIEValueAllocator
),
333 SkeletonHolder(A
, "skel_string", DIEValueAllocator
),
334 IsDarwin(A
->TM
.getTargetTriple().isOSDarwin()) {
335 const Triple
&TT
= Asm
->TM
.getTargetTriple();
337 // Make sure we know our "debugger tuning". The target option takes
338 // precedence; fall back to triple-based defaults.
339 if (Asm
->TM
.Options
.DebuggerTuning
!= DebuggerKind::Default
)
340 DebuggerTuning
= Asm
->TM
.Options
.DebuggerTuning
;
342 DebuggerTuning
= DebuggerKind::LLDB
;
344 DebuggerTuning
= DebuggerKind::SCE
;
345 else if (TT
.isOSAIX())
346 DebuggerTuning
= DebuggerKind::DBX
;
348 DebuggerTuning
= DebuggerKind::GDB
;
350 if (DwarfInlinedStrings
== Default
)
351 UseInlineStrings
= TT
.isNVPTX() || tuneForDBX();
353 UseInlineStrings
= DwarfInlinedStrings
== Enable
;
355 UseLocSection
= !TT
.isNVPTX();
357 HasAppleExtensionAttributes
= tuneForLLDB();
359 // Handle split DWARF.
360 HasSplitDwarf
= !Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
.empty();
362 // SCE defaults to linkage names only for abstract subprograms.
363 if (DwarfLinkageNames
== DefaultLinkageNames
)
364 UseAllLinkageNames
= !tuneForSCE();
366 UseAllLinkageNames
= DwarfLinkageNames
== AllLinkageNames
;
368 unsigned DwarfVersionNumber
= Asm
->TM
.Options
.MCOptions
.DwarfVersion
;
369 unsigned DwarfVersion
= DwarfVersionNumber
? DwarfVersionNumber
370 : MMI
->getModule()->getDwarfVersion();
371 // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2.
373 TT
.isNVPTX() ? 2 : (DwarfVersion
? DwarfVersion
: dwarf::DWARF_VERSION
);
375 bool Dwarf64
= DwarfVersion
>= 3 && // DWARF64 was introduced in DWARFv3.
376 TT
.isArch64Bit(); // DWARF64 requires 64-bit relocations.
379 // 1: For ELF when requested.
380 // 2: For XCOFF64: the AIX assembler will fill in debug section lengths
381 // according to the DWARF64 format for 64-bit assembly, so we must use
382 // DWARF64 in the compiler too for 64-bit mode.
384 ((Asm
->TM
.Options
.MCOptions
.Dwarf64
|| MMI
->getModule()->isDwarf64()) &&
385 TT
.isOSBinFormatELF()) ||
386 TT
.isOSBinFormatXCOFF();
388 if (!Dwarf64
&& TT
.isArch64Bit() && TT
.isOSBinFormatXCOFF())
389 report_fatal_error("XCOFF requires DWARF64 for 64-bit mode!");
391 UseRangesSection
= !NoDwarfRangesSection
&& !TT
.isNVPTX();
393 // Use sections as references. Force for NVPTX.
394 if (DwarfSectionsAsReferences
== Default
)
395 UseSectionsAsReferences
= TT
.isNVPTX();
397 UseSectionsAsReferences
= DwarfSectionsAsReferences
== Enable
;
399 // Don't generate type units for unsupported object file formats.
400 GenerateTypeUnits
= (A
->TM
.getTargetTriple().isOSBinFormatELF() ||
401 A
->TM
.getTargetTriple().isOSBinFormatWasm()) &&
402 GenerateDwarfTypeUnits
;
404 TheAccelTableKind
= computeAccelTableKind(
405 DwarfVersion
, GenerateTypeUnits
, DebuggerTuning
, A
->TM
.getTargetTriple());
407 // Work around a GDB bug. GDB doesn't support the standard opcode;
408 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
409 // is defined as of DWARF 3.
410 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
411 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
412 UseGNUTLSOpcode
= tuneForGDB() || DwarfVersion
< 3;
414 UseDWARF2Bitfields
= DwarfVersion
< 4;
416 // The DWARF v5 string offsets table has - possibly shared - contributions
417 // from each compile and type unit each preceded by a header. The string
418 // offsets table used by the pre-DWARF v5 split-DWARF implementation uses
419 // a monolithic string offsets table without any header.
420 UseSegmentedStringOffsetsTable
= DwarfVersion
>= 5;
422 // Emit call-site-param debug info for GDB and LLDB, if the target supports
423 // the debug entry values feature. It can also be enabled explicitly.
424 EmitDebugEntryValues
= Asm
->TM
.Options
.ShouldEmitDebugEntryValues();
426 // It is unclear if the GCC .debug_macro extension is well-specified
427 // for split DWARF. For now, do not allow LLVM to emit it.
428 UseDebugMacroSection
=
429 DwarfVersion
>= 5 || (UseGNUDebugMacro
&& !useSplitDwarf());
430 if (DwarfOpConvert
== Default
)
431 EnableOpConvert
= !((tuneForGDB() && useSplitDwarf()) || (tuneForLLDB() && !TT
.isOSBinFormatMachO()));
433 EnableOpConvert
= (DwarfOpConvert
== Enable
);
435 // Split DWARF would benefit object size significantly by trading reductions
436 // in address pool usage for slightly increased range list encodings.
437 if (DwarfVersion
>= 5)
438 MinimizeAddr
= MinimizeAddrInV5Option
;
440 Asm
->OutStreamer
->getContext().setDwarfVersion(DwarfVersion
);
441 Asm
->OutStreamer
->getContext().setDwarfFormat(Dwarf64
? dwarf::DWARF64
445 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
446 DwarfDebug::~DwarfDebug() = default;
448 static bool isObjCClass(StringRef Name
) {
449 return Name
.starts_with("+") || Name
.starts_with("-");
452 static bool hasObjCCategory(StringRef Name
) {
453 if (!isObjCClass(Name
))
456 return Name
.contains(") ");
459 static void getObjCClassCategory(StringRef In
, StringRef
&Class
,
460 StringRef
&Category
) {
461 if (!hasObjCCategory(In
)) {
462 Class
= In
.slice(In
.find('[') + 1, In
.find(' '));
467 Class
= In
.slice(In
.find('[') + 1, In
.find('('));
468 Category
= In
.slice(In
.find('[') + 1, In
.find(' '));
471 static StringRef
getObjCMethodName(StringRef In
) {
472 return In
.slice(In
.find(' ') + 1, In
.find(']'));
475 // Add the various names to the Dwarf accelerator table names.
476 void DwarfDebug::addSubprogramNames(
477 const DwarfUnit
&Unit
,
478 const DICompileUnit::DebugNameTableKind NameTableKind
,
479 const DISubprogram
*SP
, DIE
&Die
) {
480 if (getAccelTableKind() != AccelTableKind::Apple
&&
481 NameTableKind
!= DICompileUnit::DebugNameTableKind::Apple
&&
482 NameTableKind
== DICompileUnit::DebugNameTableKind::None
)
485 if (!SP
->isDefinition())
488 if (SP
->getName() != "")
489 addAccelName(Unit
, NameTableKind
, SP
->getName(), Die
);
491 // If the linkage name is different than the name, go ahead and output that as
492 // well into the name table. Only do that if we are going to actually emit
494 if (SP
->getLinkageName() != "" && SP
->getName() != SP
->getLinkageName() &&
495 (useAllLinkageNames() || InfoHolder
.getAbstractScopeDIEs().lookup(SP
)))
496 addAccelName(Unit
, NameTableKind
, SP
->getLinkageName(), Die
);
498 // If this is an Objective-C selector name add it to the ObjC accelerator
500 if (isObjCClass(SP
->getName())) {
501 StringRef Class
, Category
;
502 getObjCClassCategory(SP
->getName(), Class
, Category
);
503 addAccelObjC(Unit
, NameTableKind
, Class
, Die
);
505 addAccelObjC(Unit
, NameTableKind
, Category
, Die
);
506 // Also add the base method name to the name table.
507 addAccelName(Unit
, NameTableKind
, getObjCMethodName(SP
->getName()), Die
);
511 /// Check whether we should create a DIE for the given Scope, return true
512 /// if we don't create a DIE (the corresponding DIE is null).
513 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope
*Scope
) {
514 if (Scope
->isAbstractScope())
517 // We don't create a DIE if there is no Range.
518 const SmallVectorImpl
<InsnRange
> &Ranges
= Scope
->getRanges();
522 if (Ranges
.size() > 1)
525 // We don't create a DIE if we have a single Range and the end label
527 return !getLabelAfterInsn(Ranges
.front().second
);
530 template <typename Func
> static void forBothCUs(DwarfCompileUnit
&CU
, Func F
) {
532 if (auto *SkelCU
= CU
.getSkeleton())
533 if (CU
.getCUNode()->getSplitDebugInlining())
537 bool DwarfDebug::shareAcrossDWOCUs() const {
538 return SplitDwarfCrossCuReferences
;
541 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit
&SrcCU
,
542 LexicalScope
*Scope
) {
543 assert(Scope
&& Scope
->getScopeNode());
544 assert(Scope
->isAbstractScope());
545 assert(!Scope
->getInlinedAt());
547 auto *SP
= cast
<DISubprogram
>(Scope
->getScopeNode());
549 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
550 // was inlined from another compile unit.
551 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP
->getUnit()->getSplitDebugInlining())
552 // Avoid building the original CU if it won't be used
553 SrcCU
.constructAbstractSubprogramScopeDIE(Scope
);
555 auto &CU
= getOrCreateDwarfCompileUnit(SP
->getUnit());
556 if (auto *SkelCU
= CU
.getSkeleton()) {
557 (shareAcrossDWOCUs() ? CU
: SrcCU
)
558 .constructAbstractSubprogramScopeDIE(Scope
);
559 if (CU
.getCUNode()->getSplitDebugInlining())
560 SkelCU
->constructAbstractSubprogramScopeDIE(Scope
);
562 CU
.constructAbstractSubprogramScopeDIE(Scope
);
566 /// Represents a parameter whose call site value can be described by applying a
567 /// debug expression to a register in the forwarded register worklist.
568 struct FwdRegParamInfo
{
569 /// The described parameter register.
572 /// Debug expression that has been built up when walking through the
573 /// instruction chain that produces the parameter's value.
574 const DIExpression
*Expr
;
577 /// Register worklist for finding call site values.
578 using FwdRegWorklist
= MapVector
<unsigned, SmallVector
<FwdRegParamInfo
, 2>>;
579 /// Container for the set of registers known to be clobbered on the path to a
581 using ClobberedRegSet
= SmallSet
<Register
, 16>;
583 /// Append the expression \p Addition to \p Original and return the result.
584 static const DIExpression
*combineDIExpressions(const DIExpression
*Original
,
585 const DIExpression
*Addition
) {
586 std::vector
<uint64_t> Elts
= Addition
->getElements().vec();
587 // Avoid multiple DW_OP_stack_values.
588 if (Original
->isImplicit() && Addition
->isImplicit())
589 llvm::erase(Elts
, dwarf::DW_OP_stack_value
);
590 const DIExpression
*CombinedExpr
=
591 (Elts
.size() > 0) ? DIExpression::append(Original
, Elts
) : Original
;
595 /// Emit call site parameter entries that are described by the given value and
596 /// debug expression.
597 template <typename ValT
>
598 static void finishCallSiteParams(ValT Val
, const DIExpression
*Expr
,
599 ArrayRef
<FwdRegParamInfo
> DescribedParams
,
601 for (auto Param
: DescribedParams
) {
602 bool ShouldCombineExpressions
= Expr
&& Param
.Expr
->getNumElements() > 0;
604 // TODO: Entry value operations can currently not be combined with any
605 // other expressions, so we can't emit call site entries in those cases.
606 if (ShouldCombineExpressions
&& Expr
->isEntryValue())
609 // If a parameter's call site value is produced by a chain of
610 // instructions we may have already created an expression for the
611 // parameter when walking through the instructions. Append that to the
613 const DIExpression
*CombinedExpr
=
614 ShouldCombineExpressions
? combineDIExpressions(Expr
, Param
.Expr
)
616 assert((!CombinedExpr
|| CombinedExpr
->isValid()) &&
617 "Combined debug expression is invalid");
619 DbgValueLoc
DbgLocVal(CombinedExpr
, DbgValueLocEntry(Val
));
620 DbgCallSiteParam
CSParm(Param
.ParamReg
, DbgLocVal
);
621 Params
.push_back(CSParm
);
626 /// Add \p Reg to the worklist, if it's not already present, and mark that the
627 /// given parameter registers' values can (potentially) be described using
628 /// that register and an debug expression.
629 static void addToFwdRegWorklist(FwdRegWorklist
&Worklist
, unsigned Reg
,
630 const DIExpression
*Expr
,
631 ArrayRef
<FwdRegParamInfo
> ParamsToAdd
) {
632 auto I
= Worklist
.insert({Reg
, {}});
633 auto &ParamsForFwdReg
= I
.first
->second
;
634 for (auto Param
: ParamsToAdd
) {
635 assert(none_of(ParamsForFwdReg
,
636 [Param
](const FwdRegParamInfo
&D
) {
637 return D
.ParamReg
== Param
.ParamReg
;
639 "Same parameter described twice by forwarding reg");
641 // If a parameter's call site value is produced by a chain of
642 // instructions we may have already created an expression for the
643 // parameter when walking through the instructions. Append that to the
645 const DIExpression
*CombinedExpr
= combineDIExpressions(Expr
, Param
.Expr
);
646 ParamsForFwdReg
.push_back({Param
.ParamReg
, CombinedExpr
});
650 /// Interpret values loaded into registers by \p CurMI.
651 static void interpretValues(const MachineInstr
*CurMI
,
652 FwdRegWorklist
&ForwardedRegWorklist
,
654 ClobberedRegSet
&ClobberedRegUnits
) {
656 const MachineFunction
*MF
= CurMI
->getMF();
657 const DIExpression
*EmptyExpr
=
658 DIExpression::get(MF
->getFunction().getContext(), {});
659 const auto &TRI
= *MF
->getSubtarget().getRegisterInfo();
660 const auto &TII
= *MF
->getSubtarget().getInstrInfo();
661 const auto &TLI
= *MF
->getSubtarget().getTargetLowering();
663 // If an instruction defines more than one item in the worklist, we may run
664 // into situations where a worklist register's value is (potentially)
665 // described by the previous value of another register that is also defined
666 // by that instruction.
668 // This can for example occur in cases like this:
671 // $r0, $r1 = mvrr $r1, 456
672 // call @foo, $r0, $r1
674 // When describing $r1's value for the mvrr instruction, we need to make sure
675 // that we don't finalize an entry value for $r0, as that is dependent on the
676 // previous value of $r1 (123 rather than 456).
678 // In order to not have to distinguish between those cases when finalizing
679 // entry values, we simply postpone adding new parameter registers to the
680 // worklist, by first keeping them in this temporary container until the
681 // instruction has been handled.
682 FwdRegWorklist TmpWorklistItems
;
684 // If the MI is an instruction defining one or more parameters' forwarding
685 // registers, add those defines.
686 ClobberedRegSet NewClobberedRegUnits
;
687 auto getForwardingRegsDefinedByMI
= [&](const MachineInstr
&MI
,
688 SmallSetVector
<unsigned, 4> &Defs
) {
689 if (MI
.isDebugInstr())
692 for (const MachineOperand
&MO
: MI
.all_defs()) {
693 if (MO
.getReg().isPhysical()) {
694 for (auto &FwdReg
: ForwardedRegWorklist
)
695 if (TRI
.regsOverlap(FwdReg
.first
, MO
.getReg()))
696 Defs
.insert(FwdReg
.first
);
697 for (MCRegUnit Unit
: TRI
.regunits(MO
.getReg()))
698 NewClobberedRegUnits
.insert(Unit
);
703 // Set of worklist registers that are defined by this instruction.
704 SmallSetVector
<unsigned, 4> FwdRegDefs
;
706 getForwardingRegsDefinedByMI(*CurMI
, FwdRegDefs
);
707 if (FwdRegDefs
.empty()) {
708 // Any definitions by this instruction will clobber earlier reg movements.
709 ClobberedRegUnits
.insert(NewClobberedRegUnits
.begin(),
710 NewClobberedRegUnits
.end());
714 // It's possible that we find a copy from a non-volatile register to the param
715 // register, which is clobbered in the meantime. Test for clobbered reg unit
716 // overlaps before completing.
717 auto IsRegClobberedInMeantime
= [&](Register Reg
) -> bool {
718 for (auto &RegUnit
: ClobberedRegUnits
)
719 if (TRI
.hasRegUnit(Reg
, RegUnit
))
724 for (auto ParamFwdReg
: FwdRegDefs
) {
725 if (auto ParamValue
= TII
.describeLoadedValue(*CurMI
, ParamFwdReg
)) {
726 if (ParamValue
->first
.isImm()) {
727 int64_t Val
= ParamValue
->first
.getImm();
728 finishCallSiteParams(Val
, ParamValue
->second
,
729 ForwardedRegWorklist
[ParamFwdReg
], Params
);
730 } else if (ParamValue
->first
.isReg()) {
731 Register RegLoc
= ParamValue
->first
.getReg();
732 Register SP
= TLI
.getStackPointerRegisterToSaveRestore();
733 Register FP
= TRI
.getFrameRegister(*MF
);
734 bool IsSPorFP
= (RegLoc
== SP
) || (RegLoc
== FP
);
735 if (!IsRegClobberedInMeantime(RegLoc
) &&
736 (TRI
.isCalleeSavedPhysReg(RegLoc
, *MF
) || IsSPorFP
)) {
737 MachineLocation
MLoc(RegLoc
, /*Indirect=*/IsSPorFP
);
738 finishCallSiteParams(MLoc
, ParamValue
->second
,
739 ForwardedRegWorklist
[ParamFwdReg
], Params
);
741 // ParamFwdReg was described by the non-callee saved register
742 // RegLoc. Mark that the call site values for the parameters are
743 // dependent on that register instead of ParamFwdReg. Since RegLoc
744 // may be a register that will be handled in this iteration, we
745 // postpone adding the items to the worklist, and instead keep them
746 // in a temporary container.
747 addToFwdRegWorklist(TmpWorklistItems
, RegLoc
, ParamValue
->second
,
748 ForwardedRegWorklist
[ParamFwdReg
]);
754 // Remove all registers that this instruction defines from the worklist.
755 for (auto ParamFwdReg
: FwdRegDefs
)
756 ForwardedRegWorklist
.erase(ParamFwdReg
);
758 // Any definitions by this instruction will clobber earlier reg movements.
759 ClobberedRegUnits
.insert(NewClobberedRegUnits
.begin(),
760 NewClobberedRegUnits
.end());
762 // Now that we are done handling this instruction, add items from the
763 // temporary worklist to the real one.
764 for (auto &New
: TmpWorklistItems
)
765 addToFwdRegWorklist(ForwardedRegWorklist
, New
.first
, EmptyExpr
, New
.second
);
766 TmpWorklistItems
.clear();
769 static bool interpretNextInstr(const MachineInstr
*CurMI
,
770 FwdRegWorklist
&ForwardedRegWorklist
,
772 ClobberedRegSet
&ClobberedRegUnits
) {
773 // Skip bundle headers.
774 if (CurMI
->isBundle())
777 // If the next instruction is a call we can not interpret parameter's
778 // forwarding registers or we finished the interpretation of all
783 if (ForwardedRegWorklist
.empty())
786 // Avoid NOP description.
787 if (CurMI
->getNumOperands() == 0)
790 interpretValues(CurMI
, ForwardedRegWorklist
, Params
, ClobberedRegUnits
);
795 /// Try to interpret values loaded into registers that forward parameters
796 /// for \p CallMI. Store parameters with interpreted value into \p Params.
797 static void collectCallSiteParameters(const MachineInstr
*CallMI
,
799 const MachineFunction
*MF
= CallMI
->getMF();
800 const auto &CalleesMap
= MF
->getCallSitesInfo();
801 auto CSInfo
= CalleesMap
.find(CallMI
);
803 // There is no information for the call instruction.
804 if (CSInfo
== CalleesMap
.end())
807 const MachineBasicBlock
*MBB
= CallMI
->getParent();
809 // Skip the call instruction.
810 auto I
= std::next(CallMI
->getReverseIterator());
812 FwdRegWorklist ForwardedRegWorklist
;
814 const DIExpression
*EmptyExpr
=
815 DIExpression::get(MF
->getFunction().getContext(), {});
817 // Add all the forwarding registers into the ForwardedRegWorklist.
818 for (const auto &ArgReg
: CSInfo
->second
.ArgRegPairs
) {
820 ForwardedRegWorklist
.insert({ArgReg
.Reg
, {{ArgReg
.Reg
, EmptyExpr
}}})
822 assert(InsertedReg
&& "Single register used to forward two arguments?");
826 // Do not emit CSInfo for undef forwarding registers.
827 for (const auto &MO
: CallMI
->uses())
828 if (MO
.isReg() && MO
.isUndef())
829 ForwardedRegWorklist
.erase(MO
.getReg());
831 // We erase, from the ForwardedRegWorklist, those forwarding registers for
832 // which we successfully describe a loaded value (by using
833 // the describeLoadedValue()). For those remaining arguments in the working
834 // list, for which we do not describe a loaded value by
835 // the describeLoadedValue(), we try to generate an entry value expression
836 // for their call site value description, if the call is within the entry MBB.
837 // TODO: Handle situations when call site parameter value can be described
838 // as the entry value within basic blocks other than the first one.
839 bool ShouldTryEmitEntryVals
= MBB
->getIterator() == MF
->begin();
841 // Search for a loading value in forwarding registers inside call delay slot.
842 ClobberedRegSet ClobberedRegUnits
;
843 if (CallMI
->hasDelaySlot()) {
844 auto Suc
= std::next(CallMI
->getIterator());
845 // Only one-instruction delay slot is supported.
846 auto BundleEnd
= llvm::getBundleEnd(CallMI
->getIterator());
848 assert(std::next(Suc
) == BundleEnd
&&
849 "More than one instruction in call delay slot");
850 // Try to interpret value loaded by instruction.
851 if (!interpretNextInstr(&*Suc
, ForwardedRegWorklist
, Params
, ClobberedRegUnits
))
855 // Search for a loading value in forwarding registers.
856 for (; I
!= MBB
->rend(); ++I
) {
857 // Try to interpret values loaded by instruction.
858 if (!interpretNextInstr(&*I
, ForwardedRegWorklist
, Params
, ClobberedRegUnits
))
862 // Emit the call site parameter's value as an entry value.
863 if (ShouldTryEmitEntryVals
) {
864 // Create an expression where the register's entry value is used.
865 DIExpression
*EntryExpr
= DIExpression::get(
866 MF
->getFunction().getContext(), {dwarf::DW_OP_LLVM_entry_value
, 1});
867 for (auto &RegEntry
: ForwardedRegWorklist
) {
868 MachineLocation
MLoc(RegEntry
.first
);
869 finishCallSiteParams(MLoc
, EntryExpr
, RegEntry
.second
, Params
);
874 void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram
&SP
,
875 DwarfCompileUnit
&CU
, DIE
&ScopeDIE
,
876 const MachineFunction
&MF
) {
877 // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if
878 // the subprogram is required to have one.
879 if (!SP
.areAllCallsDescribed() || !SP
.isDefinition())
882 // Use DW_AT_call_all_calls to express that call site entries are present
883 // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls
884 // because one of its requirements is not met: call site entries for
885 // optimized-out calls are elided.
886 CU
.addFlag(ScopeDIE
, CU
.getDwarf5OrGNUAttr(dwarf::DW_AT_call_all_calls
));
888 const TargetInstrInfo
*TII
= MF
.getSubtarget().getInstrInfo();
889 assert(TII
&& "TargetInstrInfo not found: cannot label tail calls");
891 // Delay slot support check.
892 auto delaySlotSupported
= [&](const MachineInstr
&MI
) {
893 if (!MI
.isBundledWithSucc())
895 auto Suc
= std::next(MI
.getIterator());
896 auto CallInstrBundle
= getBundleStart(MI
.getIterator());
897 (void)CallInstrBundle
;
898 auto DelaySlotBundle
= getBundleStart(Suc
);
899 (void)DelaySlotBundle
;
900 // Ensure that label after call is following delay slot instruction.
901 // Ex. CALL_INSTRUCTION {
902 // DELAY_SLOT_INSTRUCTION }
904 assert(getLabelAfterInsn(&*CallInstrBundle
) ==
905 getLabelAfterInsn(&*DelaySlotBundle
) &&
906 "Call and its successor instruction don't have same label after.");
910 // Emit call site entries for each call or tail call in the function.
911 for (const MachineBasicBlock
&MBB
: MF
) {
912 for (const MachineInstr
&MI
: MBB
.instrs()) {
913 // Bundles with call in them will pass the isCall() test below but do not
914 // have callee operand information so skip them here. Iterator will
915 // eventually reach the call MI.
919 // Skip instructions which aren't calls. Both calls and tail-calling jump
920 // instructions (e.g TAILJMPd64) are classified correctly here.
921 if (!MI
.isCandidateForCallSiteEntry())
924 // Skip instructions marked as frame setup, as they are not interesting to
926 if (MI
.getFlag(MachineInstr::FrameSetup
))
929 // Check if delay slot support is enabled.
930 if (MI
.hasDelaySlot() && !delaySlotSupported(*&MI
))
933 // If this is a direct call, find the callee's subprogram.
934 // In the case of an indirect call find the register that holds
936 const MachineOperand
&CalleeOp
= TII
->getCalleeOperand(MI
);
937 if (!CalleeOp
.isGlobal() &&
938 (!CalleeOp
.isReg() || !CalleeOp
.getReg().isPhysical()))
941 unsigned CallReg
= 0;
942 const DISubprogram
*CalleeSP
= nullptr;
943 const Function
*CalleeDecl
= nullptr;
944 if (CalleeOp
.isReg()) {
945 CallReg
= CalleeOp
.getReg();
949 CalleeDecl
= dyn_cast
<Function
>(CalleeOp
.getGlobal());
950 if (!CalleeDecl
|| !CalleeDecl
->getSubprogram())
952 CalleeSP
= CalleeDecl
->getSubprogram();
955 // TODO: Omit call site entries for runtime calls (objc_msgSend, etc).
957 bool IsTail
= TII
->isTailCall(MI
);
959 // If MI is in a bundle, the label was created after the bundle since
960 // EmitFunctionBody iterates over top-level MIs. Get that top-level MI
961 // to search for that label below.
962 const MachineInstr
*TopLevelCallMI
=
963 MI
.isInsideBundle() ? &*getBundleStart(MI
.getIterator()) : &MI
;
965 // For non-tail calls, the return PC is needed to disambiguate paths in
966 // the call graph which could lead to some target function. For tail
967 // calls, no return PC information is needed, unless tuning for GDB in
968 // DWARF4 mode in which case we fake a return PC for compatibility.
969 const MCSymbol
*PCAddr
=
970 (!IsTail
|| CU
.useGNUAnalogForDwarf5Feature())
971 ? const_cast<MCSymbol
*>(getLabelAfterInsn(TopLevelCallMI
))
974 // For tail calls, it's necessary to record the address of the branch
975 // instruction so that the debugger can show where the tail call occurred.
976 const MCSymbol
*CallAddr
=
977 IsTail
? getLabelBeforeInsn(TopLevelCallMI
) : nullptr;
979 assert((IsTail
|| PCAddr
) && "Non-tail call without return PC");
981 LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF
.getName() << " -> "
982 << (CalleeDecl
? CalleeDecl
->getName()
983 : StringRef(MF
.getSubtarget()
986 << (IsTail
? " [IsTail]" : "") << "\n");
988 DIE
&CallSiteDIE
= CU
.constructCallSiteEntryDIE(
989 ScopeDIE
, CalleeSP
, IsTail
, PCAddr
, CallAddr
, CallReg
);
991 // Optionally emit call-site-param debug info.
992 if (emitDebugEntryValues()) {
994 // Try to interpret values of call site parameters.
995 collectCallSiteParameters(&MI
, Params
);
996 CU
.constructCallSiteParmEntryDIEs(CallSiteDIE
, Params
);
1002 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit
&U
, DIE
&D
) const {
1003 if (!U
.hasDwarfPubSections())
1006 U
.addFlag(D
, dwarf::DW_AT_GNU_pubnames
);
1009 void DwarfDebug::finishUnitAttributes(const DICompileUnit
*DIUnit
,
1010 DwarfCompileUnit
&NewCU
) {
1011 DIE
&Die
= NewCU
.getUnitDie();
1012 StringRef FN
= DIUnit
->getFilename();
1014 StringRef Producer
= DIUnit
->getProducer();
1015 StringRef Flags
= DIUnit
->getFlags();
1016 if (!Flags
.empty() && !useAppleExtensionAttributes()) {
1017 std::string ProducerWithFlags
= Producer
.str() + " " + Flags
.str();
1018 NewCU
.addString(Die
, dwarf::DW_AT_producer
, ProducerWithFlags
);
1020 NewCU
.addString(Die
, dwarf::DW_AT_producer
, Producer
);
1022 NewCU
.addUInt(Die
, dwarf::DW_AT_language
, dwarf::DW_FORM_data2
,
1023 DIUnit
->getSourceLanguage());
1024 NewCU
.addString(Die
, dwarf::DW_AT_name
, FN
);
1025 StringRef SysRoot
= DIUnit
->getSysRoot();
1026 if (!SysRoot
.empty())
1027 NewCU
.addString(Die
, dwarf::DW_AT_LLVM_sysroot
, SysRoot
);
1028 StringRef SDK
= DIUnit
->getSDK();
1030 NewCU
.addString(Die
, dwarf::DW_AT_APPLE_sdk
, SDK
);
1032 if (!useSplitDwarf()) {
1033 // Add DW_str_offsets_base to the unit DIE, except for split units.
1034 if (useSegmentedStringOffsetsTable())
1035 NewCU
.addStringOffsetsStart();
1037 NewCU
.initStmtList();
1039 // If we're using split dwarf the compilation dir is going to be in the
1040 // skeleton CU and so we don't need to duplicate it here.
1041 if (!CompilationDir
.empty())
1042 NewCU
.addString(Die
, dwarf::DW_AT_comp_dir
, CompilationDir
);
1043 addGnuPubAttributes(NewCU
, Die
);
1046 if (useAppleExtensionAttributes()) {
1047 if (DIUnit
->isOptimized())
1048 NewCU
.addFlag(Die
, dwarf::DW_AT_APPLE_optimized
);
1050 StringRef Flags
= DIUnit
->getFlags();
1052 NewCU
.addString(Die
, dwarf::DW_AT_APPLE_flags
, Flags
);
1054 if (unsigned RVer
= DIUnit
->getRuntimeVersion())
1055 NewCU
.addUInt(Die
, dwarf::DW_AT_APPLE_major_runtime_vers
,
1056 dwarf::DW_FORM_data1
, RVer
);
1059 if (DIUnit
->getDWOId()) {
1060 // This CU is either a clang module DWO or a skeleton CU.
1061 NewCU
.addUInt(Die
, dwarf::DW_AT_GNU_dwo_id
, dwarf::DW_FORM_data8
,
1062 DIUnit
->getDWOId());
1063 if (!DIUnit
->getSplitDebugFilename().empty()) {
1064 // This is a prefabricated skeleton CU.
1065 dwarf::Attribute attrDWOName
= getDwarfVersion() >= 5
1066 ? dwarf::DW_AT_dwo_name
1067 : dwarf::DW_AT_GNU_dwo_name
;
1068 NewCU
.addString(Die
, attrDWOName
, DIUnit
->getSplitDebugFilename());
1072 // Create new DwarfCompileUnit for the given metadata node with tag
1073 // DW_TAG_compile_unit.
1075 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit
*DIUnit
) {
1076 if (auto *CU
= CUMap
.lookup(DIUnit
))
1079 if (useSplitDwarf() &&
1080 !shareAcrossDWOCUs() &&
1081 (!DIUnit
->getSplitDebugInlining() ||
1082 DIUnit
->getEmissionKind() == DICompileUnit::FullDebug
) &&
1084 return *CUMap
.begin()->second
;
1086 CompilationDir
= DIUnit
->getDirectory();
1088 auto OwnedUnit
= std::make_unique
<DwarfCompileUnit
>(
1089 InfoHolder
.getUnits().size(), DIUnit
, Asm
, this, &InfoHolder
);
1090 DwarfCompileUnit
&NewCU
= *OwnedUnit
;
1091 InfoHolder
.addUnit(std::move(OwnedUnit
));
1093 // LTO with assembly output shares a single line table amongst multiple CUs.
1094 // To avoid the compilation directory being ambiguous, let the line table
1095 // explicitly describe the directory of all files, never relying on the
1096 // compilation directory.
1097 if (!Asm
->OutStreamer
->hasRawTextSupport() || SingleCU
)
1098 Asm
->OutStreamer
->emitDwarfFile0Directive(
1099 CompilationDir
, DIUnit
->getFilename(), getMD5AsBytes(DIUnit
->getFile()),
1100 DIUnit
->getSource(), NewCU
.getUniqueID());
1102 if (useSplitDwarf()) {
1103 NewCU
.setSkeleton(constructSkeletonCU(NewCU
));
1104 NewCU
.setSection(Asm
->getObjFileLowering().getDwarfInfoDWOSection());
1106 finishUnitAttributes(DIUnit
, NewCU
);
1107 NewCU
.setSection(Asm
->getObjFileLowering().getDwarfInfoSection());
1110 CUMap
.insert({DIUnit
, &NewCU
});
1111 CUDieMap
.insert({&NewCU
.getUnitDie(), &NewCU
});
1115 /// Sort and unique GVEs by comparing their fragment offset.
1116 static SmallVectorImpl
<DwarfCompileUnit::GlobalExpr
> &
1117 sortGlobalExprs(SmallVectorImpl
<DwarfCompileUnit::GlobalExpr
> &GVEs
) {
1119 GVEs
, [](DwarfCompileUnit::GlobalExpr A
, DwarfCompileUnit::GlobalExpr B
) {
1120 // Sort order: first null exprs, then exprs without fragment
1121 // info, then sort by fragment offset in bits.
1122 // FIXME: Come up with a more comprehensive comparator so
1123 // the sorting isn't non-deterministic, and so the following
1124 // std::unique call works correctly.
1125 if (!A
.Expr
|| !B
.Expr
)
1127 auto FragmentA
= A
.Expr
->getFragmentInfo();
1128 auto FragmentB
= B
.Expr
->getFragmentInfo();
1129 if (!FragmentA
|| !FragmentB
)
1131 return FragmentA
->OffsetInBits
< FragmentB
->OffsetInBits
;
1133 GVEs
.erase(llvm::unique(GVEs
,
1134 [](DwarfCompileUnit::GlobalExpr A
,
1135 DwarfCompileUnit::GlobalExpr B
) {
1136 return A
.Expr
== B
.Expr
;
1142 // Emit all Dwarf sections that should come prior to the content. Create
1143 // global DIEs and emit initial debug info sections. This is invoked by
1144 // the target AsmPrinter.
1145 void DwarfDebug::beginModule(Module
*M
) {
1146 DebugHandlerBase::beginModule(M
);
1148 if (!Asm
|| !MMI
->hasDebugInfo())
1151 unsigned NumDebugCUs
= std::distance(M
->debug_compile_units_begin(),
1152 M
->debug_compile_units_end());
1153 assert(NumDebugCUs
> 0 && "Asm unexpectedly initialized");
1154 assert(MMI
->hasDebugInfo() &&
1155 "DebugInfoAvailabilty unexpectedly not initialized");
1156 SingleCU
= NumDebugCUs
== 1;
1157 DenseMap
<DIGlobalVariable
*, SmallVector
<DwarfCompileUnit::GlobalExpr
, 1>>
1159 for (const GlobalVariable
&Global
: M
->globals()) {
1160 SmallVector
<DIGlobalVariableExpression
*, 1> GVs
;
1161 Global
.getDebugInfo(GVs
);
1162 for (auto *GVE
: GVs
)
1163 GVMap
[GVE
->getVariable()].push_back({&Global
, GVE
->getExpression()});
1166 // Create the symbol that designates the start of the unit's contribution
1167 // to the string offsets table. In a split DWARF scenario, only the skeleton
1168 // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol).
1169 if (useSegmentedStringOffsetsTable())
1170 (useSplitDwarf() ? SkeletonHolder
: InfoHolder
)
1171 .setStringOffsetsStartSym(Asm
->createTempSymbol("str_offsets_base"));
1174 // Create the symbols that designates the start of the DWARF v5 range list
1175 // and locations list tables. They are located past the table headers.
1176 if (getDwarfVersion() >= 5) {
1177 DwarfFile
&Holder
= useSplitDwarf() ? SkeletonHolder
: InfoHolder
;
1178 Holder
.setRnglistsTableBaseSym(
1179 Asm
->createTempSymbol("rnglists_table_base"));
1181 if (useSplitDwarf())
1182 InfoHolder
.setRnglistsTableBaseSym(
1183 Asm
->createTempSymbol("rnglists_dwo_table_base"));
1186 // Create the symbol that points to the first entry following the debug
1187 // address table (.debug_addr) header.
1188 AddrPool
.setLabel(Asm
->createTempSymbol("addr_table_base"));
1189 DebugLocs
.setSym(Asm
->createTempSymbol("loclists_table_base"));
1191 for (DICompileUnit
*CUNode
: M
->debug_compile_units()) {
1192 if (CUNode
->getImportedEntities().empty() &&
1193 CUNode
->getEnumTypes().empty() && CUNode
->getRetainedTypes().empty() &&
1194 CUNode
->getGlobalVariables().empty() && CUNode
->getMacros().empty())
1197 DwarfCompileUnit
&CU
= getOrCreateDwarfCompileUnit(CUNode
);
1199 // Global Variables.
1200 for (auto *GVE
: CUNode
->getGlobalVariables()) {
1201 // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
1202 // already know about the variable and it isn't adding a constant
1204 auto &GVMapEntry
= GVMap
[GVE
->getVariable()];
1205 auto *Expr
= GVE
->getExpression();
1206 if (!GVMapEntry
.size() || (Expr
&& Expr
->isConstant()))
1207 GVMapEntry
.push_back({nullptr, Expr
});
1210 DenseSet
<DIGlobalVariable
*> Processed
;
1211 for (auto *GVE
: CUNode
->getGlobalVariables()) {
1212 DIGlobalVariable
*GV
= GVE
->getVariable();
1213 if (Processed
.insert(GV
).second
)
1214 CU
.getOrCreateGlobalVariableDIE(GV
, sortGlobalExprs(GVMap
[GV
]));
1217 for (auto *Ty
: CUNode
->getEnumTypes())
1218 CU
.getOrCreateTypeDIE(cast
<DIType
>(Ty
));
1220 for (auto *Ty
: CUNode
->getRetainedTypes()) {
1221 // The retained types array by design contains pointers to
1222 // MDNodes rather than DIRefs. Unique them here.
1223 if (DIType
*RT
= dyn_cast
<DIType
>(Ty
))
1224 // There is no point in force-emitting a forward declaration.
1225 CU
.getOrCreateTypeDIE(RT
);
1230 void DwarfDebug::finishEntityDefinitions() {
1231 for (const auto &Entity
: ConcreteEntities
) {
1232 DIE
*Die
= Entity
->getDIE();
1234 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
1235 // in the ConcreteEntities list, rather than looking it up again here.
1236 // DIE::getUnit isn't simple - it walks parent pointers, etc.
1237 DwarfCompileUnit
*Unit
= CUDieMap
.lookup(Die
->getUnitDie());
1239 Unit
->finishEntityDefinition(Entity
.get());
1243 void DwarfDebug::finishSubprogramDefinitions() {
1244 for (const DISubprogram
*SP
: ProcessedSPNodes
) {
1245 assert(SP
->getUnit()->getEmissionKind() != DICompileUnit::NoDebug
);
1247 getOrCreateDwarfCompileUnit(SP
->getUnit()),
1248 [&](DwarfCompileUnit
&CU
) { CU
.finishSubprogramDefinition(SP
); });
1252 void DwarfDebug::finalizeModuleInfo() {
1253 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1255 finishSubprogramDefinitions();
1257 finishEntityDefinitions();
1259 // Include the DWO file name in the hash if there's more than one CU.
1260 // This handles ThinLTO's situation where imported CUs may very easily be
1261 // duplicate with the same CU partially imported into another ThinLTO unit.
1263 if (CUMap
.size() > 1)
1264 DWOName
= Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
;
1266 bool HasEmittedSplitCU
= false;
1268 // Handle anything that needs to be done on a per-unit basis after
1269 // all other generation.
1270 for (const auto &P
: CUMap
) {
1271 auto &TheCU
= *P
.second
;
1272 if (TheCU
.getCUNode()->isDebugDirectivesOnly())
1274 // Emit DW_AT_containing_type attribute to connect types with their
1275 // vtable holding type.
1276 TheCU
.constructContainingTypeDIEs();
1278 // Add CU specific attributes if we need to add any.
1279 // If we're splitting the dwarf out now that we've got the entire
1280 // CU then add the dwo id to it.
1281 auto *SkCU
= TheCU
.getSkeleton();
1283 bool HasSplitUnit
= SkCU
&& !TheCU
.getUnitDie().children().empty();
1286 (void)HasEmittedSplitCU
;
1287 assert((shareAcrossDWOCUs() || !HasEmittedSplitCU
) &&
1288 "Multiple CUs emitted into a single dwo file");
1289 HasEmittedSplitCU
= true;
1290 dwarf::Attribute attrDWOName
= getDwarfVersion() >= 5
1291 ? dwarf::DW_AT_dwo_name
1292 : dwarf::DW_AT_GNU_dwo_name
;
1293 finishUnitAttributes(TheCU
.getCUNode(), TheCU
);
1294 TheCU
.addString(TheCU
.getUnitDie(), attrDWOName
,
1295 Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
);
1296 SkCU
->addString(SkCU
->getUnitDie(), attrDWOName
,
1297 Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
);
1298 // Emit a unique identifier for this CU.
1300 DIEHash(Asm
, &TheCU
).computeCUSignature(DWOName
, TheCU
.getUnitDie());
1301 if (getDwarfVersion() >= 5) {
1305 TheCU
.addUInt(TheCU
.getUnitDie(), dwarf::DW_AT_GNU_dwo_id
,
1306 dwarf::DW_FORM_data8
, ID
);
1307 SkCU
->addUInt(SkCU
->getUnitDie(), dwarf::DW_AT_GNU_dwo_id
,
1308 dwarf::DW_FORM_data8
, ID
);
1311 if (getDwarfVersion() < 5 && !SkeletonHolder
.getRangeLists().empty()) {
1312 const MCSymbol
*Sym
= TLOF
.getDwarfRangesSection()->getBeginSymbol();
1313 SkCU
->addSectionLabel(SkCU
->getUnitDie(), dwarf::DW_AT_GNU_ranges_base
,
1317 finishUnitAttributes(SkCU
->getCUNode(), *SkCU
);
1320 // If we have code split among multiple sections or non-contiguous
1321 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
1322 // remain in the .o file, otherwise add a DW_AT_low_pc.
1323 // FIXME: We should use ranges allow reordering of code ala
1324 // .subsections_via_symbols in mach-o. This would mean turning on
1325 // ranges for all subprogram DIEs for mach-o.
1326 DwarfCompileUnit
&U
= SkCU
? *SkCU
: TheCU
;
1328 if (unsigned NumRanges
= TheCU
.getRanges().size()) {
1329 if (NumRanges
> 1 && useRangesSection())
1330 // A DW_AT_low_pc attribute may also be specified in combination with
1331 // DW_AT_ranges to specify the default base address for use in
1332 // location lists (see Section 2.6.2) and range lists (see Section
1334 U
.addUInt(U
.getUnitDie(), dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
, 0);
1336 U
.setBaseAddress(TheCU
.getRanges().front().Begin
);
1337 U
.attachRangesOrLowHighPC(U
.getUnitDie(), TheCU
.takeRanges());
1340 // We don't keep track of which addresses are used in which CU so this
1341 // is a bit pessimistic under LTO.
1342 if ((HasSplitUnit
|| getDwarfVersion() >= 5) && !AddrPool
.isEmpty())
1343 U
.addAddrTableBase();
1345 if (getDwarfVersion() >= 5) {
1346 if (U
.hasRangeLists())
1347 U
.addRnglistsBase();
1349 if (!DebugLocs
.getLists().empty() && !useSplitDwarf()) {
1350 U
.addSectionLabel(U
.getUnitDie(), dwarf::DW_AT_loclists_base
,
1352 TLOF
.getDwarfLoclistsSection()->getBeginSymbol());
1356 auto *CUNode
= cast
<DICompileUnit
>(P
.first
);
1357 // If compile Unit has macros, emit "DW_AT_macro_info/DW_AT_macros"
1359 if (CUNode
->getMacros()) {
1360 if (UseDebugMacroSection
) {
1361 if (useSplitDwarf())
1362 TheCU
.addSectionDelta(
1363 TheCU
.getUnitDie(), dwarf::DW_AT_macros
, U
.getMacroLabelBegin(),
1364 TLOF
.getDwarfMacroDWOSection()->getBeginSymbol());
1366 dwarf::Attribute MacrosAttr
= getDwarfVersion() >= 5
1367 ? dwarf::DW_AT_macros
1368 : dwarf::DW_AT_GNU_macros
;
1369 U
.addSectionLabel(U
.getUnitDie(), MacrosAttr
, U
.getMacroLabelBegin(),
1370 TLOF
.getDwarfMacroSection()->getBeginSymbol());
1373 if (useSplitDwarf())
1374 TheCU
.addSectionDelta(
1375 TheCU
.getUnitDie(), dwarf::DW_AT_macro_info
,
1376 U
.getMacroLabelBegin(),
1377 TLOF
.getDwarfMacinfoDWOSection()->getBeginSymbol());
1379 U
.addSectionLabel(U
.getUnitDie(), dwarf::DW_AT_macro_info
,
1380 U
.getMacroLabelBegin(),
1381 TLOF
.getDwarfMacinfoSection()->getBeginSymbol());
1386 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
1387 for (auto *CUNode
: MMI
->getModule()->debug_compile_units())
1388 if (CUNode
->getDWOId())
1389 getOrCreateDwarfCompileUnit(CUNode
);
1391 // Compute DIE offsets and sizes.
1392 InfoHolder
.computeSizeAndOffsets();
1393 if (useSplitDwarf())
1394 SkeletonHolder
.computeSizeAndOffsets();
1396 // Now that offsets are computed, can replace DIEs in debug_names Entry with
1397 // an actual offset.
1398 AccelDebugNames
.convertDieToOffset();
1401 // Emit all Dwarf sections that should come after the content.
1402 void DwarfDebug::endModule() {
1403 // Terminate the pending line table.
1405 terminateLineTable(PrevCU
);
1407 assert(CurFn
== nullptr);
1408 assert(CurMI
== nullptr);
1410 for (const auto &P
: CUMap
) {
1411 const auto *CUNode
= cast
<DICompileUnit
>(P
.first
);
1412 DwarfCompileUnit
*CU
= &*P
.second
;
1414 // Emit imported entities.
1415 for (auto *IE
: CUNode
->getImportedEntities()) {
1416 assert(!isa_and_nonnull
<DILocalScope
>(IE
->getScope()) &&
1417 "Unexpected function-local entity in 'imports' CU field.");
1418 CU
->getOrCreateImportedEntityDIE(IE
);
1420 for (const auto *D
: CU
->getDeferredLocalDecls()) {
1421 if (auto *IE
= dyn_cast
<DIImportedEntity
>(D
))
1422 CU
->getOrCreateImportedEntityDIE(IE
);
1424 llvm_unreachable("Unexpected local retained node!");
1428 CU
->createBaseTypeDIEs();
1431 // If we aren't actually generating debug info (check beginModule -
1432 // conditionalized on the presence of the llvm.dbg.cu metadata node)
1433 if (!Asm
|| !MMI
->hasDebugInfo())
1436 // Finalize the debug info for the module.
1437 finalizeModuleInfo();
1439 if (useSplitDwarf())
1440 // Emit debug_loc.dwo/debug_loclists.dwo section.
1443 // Emit debug_loc/debug_loclists section.
1446 // Corresponding abbreviations into a abbrev section.
1447 emitAbbreviations();
1449 // Emit all the DIEs into a debug info section.
1452 // Emit info into a debug aranges section.
1453 if (GenerateARangeSection
)
1456 // Emit info into a debug ranges section.
1459 if (useSplitDwarf())
1460 // Emit info into a debug macinfo.dwo section.
1461 emitDebugMacinfoDWO();
1463 // Emit info into a debug macinfo/macro section.
1468 if (useSplitDwarf()) {
1471 emitDebugAbbrevDWO();
1473 emitDebugRangesDWO();
1478 // Emit info into the dwarf accelerator table sections.
1479 switch (getAccelTableKind()) {
1480 case AccelTableKind::Apple
:
1483 emitAccelNamespaces();
1486 case AccelTableKind::Dwarf
:
1487 emitAccelDebugNames();
1489 case AccelTableKind::None
:
1491 case AccelTableKind::Default
:
1492 llvm_unreachable("Default should have already been resolved.");
1495 // Emit the pubnames and pubtypes sections if requested.
1496 emitDebugPubSections();
1499 // FIXME: AbstractVariables.clear();
1502 void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit
&CU
,
1503 const DINode
*Node
, const MDNode
*ScopeNode
) {
1504 if (CU
.getExistingAbstractEntity(Node
))
1507 if (LexicalScope
*Scope
=
1508 LScopes
.findAbstractScope(cast_or_null
<DILocalScope
>(ScopeNode
)))
1509 CU
.createAbstractEntity(Node
, Scope
);
1512 static const DILocalScope
*getRetainedNodeScope(const MDNode
*N
) {
1514 if (const auto *LV
= dyn_cast
<DILocalVariable
>(N
))
1516 else if (const auto *L
= dyn_cast
<DILabel
>(N
))
1518 else if (const auto *IE
= dyn_cast
<DIImportedEntity
>(N
))
1521 llvm_unreachable("Unexpected retained node!");
1523 // Ensure the scope is not a DILexicalBlockFile.
1524 return cast
<DILocalScope
>(S
)->getNonLexicalBlockFileScope();
1527 // Collect variable information from side table maintained by MF.
1528 void DwarfDebug::collectVariableInfoFromMFTable(
1529 DwarfCompileUnit
&TheCU
, DenseSet
<InlinedEntity
> &Processed
) {
1530 SmallDenseMap
<InlinedEntity
, DbgVariable
*> MFVars
;
1531 LLVM_DEBUG(dbgs() << "DwarfDebug: collecting variables from MF side table\n");
1532 for (const auto &VI
: Asm
->MF
->getVariableDbgInfo()) {
1535 assert(VI
.Var
->isValidLocationForIntrinsic(VI
.Loc
) &&
1536 "Expected inlined-at fields to agree");
1538 InlinedEntity
Var(VI
.Var
, VI
.Loc
->getInlinedAt());
1539 Processed
.insert(Var
);
1540 LexicalScope
*Scope
= LScopes
.findLexicalScope(VI
.Loc
);
1542 // If variable scope is not found then skip this variable.
1544 LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI
.Var
->getName()
1545 << ", no variable scope found\n");
1549 ensureAbstractEntityIsCreatedIfScoped(TheCU
, Var
.first
, Scope
->getScopeNode());
1551 // If we have already seen information for this variable, add to what we
1553 if (DbgVariable
*PreviousLoc
= MFVars
.lookup(Var
)) {
1554 auto *PreviousMMI
= std::get_if
<Loc::MMI
>(PreviousLoc
);
1555 auto *PreviousEntryValue
= std::get_if
<Loc::EntryValue
>(PreviousLoc
);
1556 // Previous and new locations are both stack slots (MMI).
1557 if (PreviousMMI
&& VI
.inStackSlot())
1558 PreviousMMI
->addFrameIndexExpr(VI
.Expr
, VI
.getStackSlot());
1559 // Previous and new locations are both entry values.
1560 else if (PreviousEntryValue
&& VI
.inEntryValueRegister())
1561 PreviousEntryValue
->addExpr(VI
.getEntryValueRegister(), *VI
.Expr
);
1563 // Locations differ, this should (rarely) happen in optimized async
1565 // Prefer whichever location has an EntryValue.
1566 if (PreviousLoc
->holds
<Loc::MMI
>())
1567 PreviousLoc
->emplace
<Loc::EntryValue
>(VI
.getEntryValueRegister(),
1569 LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI
.Var
->getName()
1570 << ", conflicting fragment location types\n");
1575 auto RegVar
= std::make_unique
<DbgVariable
>(
1576 cast
<DILocalVariable
>(Var
.first
), Var
.second
);
1577 if (VI
.inStackSlot())
1578 RegVar
->emplace
<Loc::MMI
>(VI
.Expr
, VI
.getStackSlot());
1580 RegVar
->emplace
<Loc::EntryValue
>(VI
.getEntryValueRegister(), *VI
.Expr
);
1581 LLVM_DEBUG(dbgs() << "Created DbgVariable for " << VI
.Var
->getName()
1583 InfoHolder
.addScopeVariable(Scope
, RegVar
.get());
1584 MFVars
.insert({Var
, RegVar
.get()});
1585 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
.getSectionID()].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
->emplace
<Loc::Single
>(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
);
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
->emplace
<Loc::Single
>(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
->getSectionID() == MI
->getParent()->getSectionID());
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 case dwarf::DW_TAG_template_alias
:
2487 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE
, dwarf::GIEL_STATIC
);
2488 case dwarf::DW_TAG_namespace
:
2489 return dwarf::GIEK_TYPE
;
2490 case dwarf::DW_TAG_subprogram
:
2491 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION
, Linkage
);
2492 case dwarf::DW_TAG_variable
:
2493 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE
, Linkage
);
2494 case dwarf::DW_TAG_enumerator
:
2495 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE
,
2496 dwarf::GIEL_STATIC
);
2498 return dwarf::GIEK_NONE
;
2502 /// emitDebugPubSections - Emit visible names and types into debug pubnames and
2503 /// pubtypes sections.
2504 void DwarfDebug::emitDebugPubSections() {
2505 for (const auto &NU
: CUMap
) {
2506 DwarfCompileUnit
*TheU
= NU
.second
;
2507 if (!TheU
->hasDwarfPubSections())
2510 bool GnuStyle
= TheU
->getCUNode()->getNameTableKind() ==
2511 DICompileUnit::DebugNameTableKind::GNU
;
2513 Asm
->OutStreamer
->switchSection(
2514 GnuStyle
? Asm
->getObjFileLowering().getDwarfGnuPubNamesSection()
2515 : Asm
->getObjFileLowering().getDwarfPubNamesSection());
2516 emitDebugPubSection(GnuStyle
, "Names", TheU
, TheU
->getGlobalNames());
2518 Asm
->OutStreamer
->switchSection(
2519 GnuStyle
? Asm
->getObjFileLowering().getDwarfGnuPubTypesSection()
2520 : Asm
->getObjFileLowering().getDwarfPubTypesSection());
2521 emitDebugPubSection(GnuStyle
, "Types", TheU
, TheU
->getGlobalTypes());
2525 void DwarfDebug::emitSectionReference(const DwarfCompileUnit
&CU
) {
2526 if (useSectionsAsReferences())
2527 Asm
->emitDwarfOffset(CU
.getSection()->getBeginSymbol(),
2528 CU
.getDebugSectionOffset());
2530 Asm
->emitDwarfSymbolReference(CU
.getLabelBegin());
2533 void DwarfDebug::emitDebugPubSection(bool GnuStyle
, StringRef Name
,
2534 DwarfCompileUnit
*TheU
,
2535 const StringMap
<const DIE
*> &Globals
) {
2536 if (auto *Skeleton
= TheU
->getSkeleton())
2540 MCSymbol
*EndLabel
= Asm
->emitDwarfUnitLength(
2541 "pub" + Name
, "Length of Public " + Name
+ " Info");
2543 Asm
->OutStreamer
->AddComment("DWARF Version");
2544 Asm
->emitInt16(dwarf::DW_PUBNAMES_VERSION
);
2546 Asm
->OutStreamer
->AddComment("Offset of Compilation Unit Info");
2547 emitSectionReference(*TheU
);
2549 Asm
->OutStreamer
->AddComment("Compilation Unit Length");
2550 Asm
->emitDwarfLengthOrOffset(TheU
->getLength());
2552 // Emit the pubnames for this compilation unit.
2553 SmallVector
<std::pair
<StringRef
, const DIE
*>, 0> Vec
;
2554 for (const auto &GI
: Globals
)
2555 Vec
.emplace_back(GI
.first(), GI
.second
);
2556 llvm::sort(Vec
, [](auto &A
, auto &B
) {
2557 return A
.second
->getOffset() < B
.second
->getOffset();
2559 for (const auto &[Name
, Entity
] : Vec
) {
2560 Asm
->OutStreamer
->AddComment("DIE offset");
2561 Asm
->emitDwarfLengthOrOffset(Entity
->getOffset());
2564 dwarf::PubIndexEntryDescriptor Desc
= computeIndexValue(TheU
, Entity
);
2565 Asm
->OutStreamer
->AddComment(
2566 Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Desc
.Kind
) +
2567 ", " + dwarf::GDBIndexEntryLinkageString(Desc
.Linkage
));
2568 Asm
->emitInt8(Desc
.toBits());
2571 Asm
->OutStreamer
->AddComment("External Name");
2572 Asm
->OutStreamer
->emitBytes(StringRef(Name
.data(), Name
.size() + 1));
2575 Asm
->OutStreamer
->AddComment("End Mark");
2576 Asm
->emitDwarfLengthOrOffset(0);
2577 Asm
->OutStreamer
->emitLabel(EndLabel
);
2580 /// Emit null-terminated strings into a debug str section.
2581 void DwarfDebug::emitDebugStr() {
2582 MCSection
*StringOffsetsSection
= nullptr;
2583 if (useSegmentedStringOffsetsTable()) {
2584 emitStringOffsetsTableHeader();
2585 StringOffsetsSection
= Asm
->getObjFileLowering().getDwarfStrOffSection();
2587 DwarfFile
&Holder
= useSplitDwarf() ? SkeletonHolder
: InfoHolder
;
2588 Holder
.emitStrings(Asm
->getObjFileLowering().getDwarfStrSection(),
2589 StringOffsetsSection
, /* UseRelativeOffsets = */ true);
2592 void DwarfDebug::emitDebugLocEntry(ByteStreamer
&Streamer
,
2593 const DebugLocStream::Entry
&Entry
,
2594 const DwarfCompileUnit
*CU
) {
2595 auto &&Comments
= DebugLocs
.getComments(Entry
);
2596 auto Comment
= Comments
.begin();
2597 auto End
= Comments
.end();
2599 // The expressions are inserted into a byte stream rather early (see
2600 // DwarfExpression::addExpression) so for those ops (e.g. DW_OP_convert) that
2601 // need to reference a base_type DIE the offset of that DIE is not yet known.
2602 // To deal with this we instead insert a placeholder early and then extract
2603 // it here and replace it with the real reference.
2604 unsigned PtrSize
= Asm
->MAI
->getCodePointerSize();
2605 DWARFDataExtractor
Data(StringRef(DebugLocs
.getBytes(Entry
).data(),
2606 DebugLocs
.getBytes(Entry
).size()),
2607 Asm
->getDataLayout().isLittleEndian(), PtrSize
);
2608 DWARFExpression
Expr(Data
, PtrSize
, Asm
->OutContext
.getDwarfFormat());
2610 using Encoding
= DWARFExpression::Operation::Encoding
;
2611 uint64_t Offset
= 0;
2612 for (const auto &Op
: Expr
) {
2613 assert(Op
.getCode() != dwarf::DW_OP_const_type
&&
2614 "3 operand ops not yet supported");
2615 assert(!Op
.getSubCode() && "SubOps not yet supported");
2616 Streamer
.emitInt8(Op
.getCode(), Comment
!= End
? *(Comment
++) : "");
2618 for (unsigned I
= 0; I
< Op
.getDescription().Op
.size(); ++I
) {
2619 if (Op
.getDescription().Op
[I
] == Encoding::BaseTypeRef
) {
2621 Streamer
.emitDIERef(*CU
->ExprRefedBaseTypes
[Op
.getRawOperand(I
)].Die
);
2622 // Make sure comments stay aligned.
2623 for (unsigned J
= 0; J
< Length
; ++J
)
2627 for (uint64_t J
= Offset
; J
< Op
.getOperandEndOffset(I
); ++J
)
2628 Streamer
.emitInt8(Data
.getData()[J
], Comment
!= End
? *(Comment
++) : "");
2630 Offset
= Op
.getOperandEndOffset(I
);
2632 assert(Offset
== Op
.getEndOffset());
2636 void DwarfDebug::emitDebugLocValue(const AsmPrinter
&AP
, const DIBasicType
*BT
,
2637 const DbgValueLoc
&Value
,
2638 DwarfExpression
&DwarfExpr
) {
2639 auto *DIExpr
= Value
.getExpression();
2640 DIExpressionCursor
ExprCursor(DIExpr
);
2641 DwarfExpr
.addFragmentOffset(DIExpr
);
2643 // If the DIExpr is an Entry Value, we want to follow the same code path
2644 // regardless of whether the DBG_VALUE is variadic or not.
2645 if (DIExpr
&& DIExpr
->isEntryValue()) {
2646 // Entry values can only be a single register with no additional DIExpr,
2647 // so just add it directly.
2648 assert(Value
.getLocEntries().size() == 1);
2649 assert(Value
.getLocEntries()[0].isLocation());
2650 MachineLocation Location
= Value
.getLocEntries()[0].getLoc();
2651 DwarfExpr
.setLocation(Location
, DIExpr
);
2653 DwarfExpr
.beginEntryValueExpression(ExprCursor
);
2655 const TargetRegisterInfo
&TRI
= *AP
.MF
->getSubtarget().getRegisterInfo();
2656 if (!DwarfExpr
.addMachineRegExpression(TRI
, ExprCursor
, Location
.getReg()))
2658 return DwarfExpr
.addExpression(std::move(ExprCursor
));
2662 auto EmitValueLocEntry
= [&DwarfExpr
, &BT
,
2663 &AP
](const DbgValueLocEntry
&Entry
,
2664 DIExpressionCursor
&Cursor
) -> bool {
2665 if (Entry
.isInt()) {
2666 if (BT
&& (BT
->getEncoding() == dwarf::DW_ATE_signed
||
2667 BT
->getEncoding() == dwarf::DW_ATE_signed_char
))
2668 DwarfExpr
.addSignedConstant(Entry
.getInt());
2670 DwarfExpr
.addUnsignedConstant(Entry
.getInt());
2671 } else if (Entry
.isLocation()) {
2672 MachineLocation Location
= Entry
.getLoc();
2673 if (Location
.isIndirect())
2674 DwarfExpr
.setMemoryLocationKind();
2676 const TargetRegisterInfo
&TRI
= *AP
.MF
->getSubtarget().getRegisterInfo();
2677 if (!DwarfExpr
.addMachineRegExpression(TRI
, Cursor
, Location
.getReg()))
2679 } else if (Entry
.isTargetIndexLocation()) {
2680 TargetIndexLocation Loc
= Entry
.getTargetIndexLocation();
2681 // TODO TargetIndexLocation is a target-independent. Currently only the
2682 // WebAssembly-specific encoding is supported.
2683 assert(AP
.TM
.getTargetTriple().isWasm());
2684 DwarfExpr
.addWasmLocation(Loc
.Index
, static_cast<uint64_t>(Loc
.Offset
));
2685 } else if (Entry
.isConstantFP()) {
2686 if (AP
.getDwarfVersion() >= 4 && !AP
.getDwarfDebug()->tuneForSCE() &&
2688 DwarfExpr
.addConstantFP(Entry
.getConstantFP()->getValueAPF(), AP
);
2689 } else if (Entry
.getConstantFP()
2692 .getBitWidth() <= 64 /*bits*/) {
2693 DwarfExpr
.addUnsignedConstant(
2694 Entry
.getConstantFP()->getValueAPF().bitcastToAPInt());
2697 dbgs() << "Skipped DwarfExpression creation for ConstantFP of size"
2698 << Entry
.getConstantFP()
2709 if (!Value
.isVariadic()) {
2710 if (!EmitValueLocEntry(Value
.getLocEntries()[0], ExprCursor
))
2712 DwarfExpr
.addExpression(std::move(ExprCursor
));
2716 // If any of the location entries are registers with the value 0, then the
2717 // location is undefined.
2718 if (any_of(Value
.getLocEntries(), [](const DbgValueLocEntry
&Entry
) {
2719 return Entry
.isLocation() && !Entry
.getLoc().getReg();
2723 DwarfExpr
.addExpression(
2724 std::move(ExprCursor
),
2725 [EmitValueLocEntry
, &Value
](unsigned Idx
,
2726 DIExpressionCursor
&Cursor
) -> bool {
2727 return EmitValueLocEntry(Value
.getLocEntries()[Idx
], Cursor
);
2731 void DebugLocEntry::finalize(const AsmPrinter
&AP
,
2732 DebugLocStream::ListBuilder
&List
,
2733 const DIBasicType
*BT
,
2734 DwarfCompileUnit
&TheCU
) {
2735 assert(!Values
.empty() &&
2736 "location list entries without values are redundant");
2737 assert(Begin
!= End
&& "unexpected location list entry with empty range");
2738 DebugLocStream::EntryBuilder
Entry(List
, Begin
, End
);
2739 BufferByteStreamer Streamer
= Entry
.getStreamer();
2740 DebugLocDwarfExpression
DwarfExpr(AP
.getDwarfVersion(), Streamer
, TheCU
);
2741 const DbgValueLoc
&Value
= Values
[0];
2742 if (Value
.isFragment()) {
2743 // Emit all fragments that belong to the same variable and range.
2744 assert(llvm::all_of(Values
, [](DbgValueLoc P
) {
2745 return P
.isFragment();
2746 }) && "all values are expected to be fragments");
2747 assert(llvm::is_sorted(Values
) && "fragments are expected to be sorted");
2749 for (const auto &Fragment
: Values
)
2750 DwarfDebug::emitDebugLocValue(AP
, BT
, Fragment
, DwarfExpr
);
2753 assert(Values
.size() == 1 && "only fragments may have >1 value");
2754 DwarfDebug::emitDebugLocValue(AP
, BT
, Value
, DwarfExpr
);
2756 DwarfExpr
.finalize();
2757 if (DwarfExpr
.TagOffset
)
2758 List
.setTagOffset(*DwarfExpr
.TagOffset
);
2761 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry
&Entry
,
2762 const DwarfCompileUnit
*CU
) {
2764 Asm
->OutStreamer
->AddComment("Loc expr size");
2765 if (getDwarfVersion() >= 5)
2766 Asm
->emitULEB128(DebugLocs
.getBytes(Entry
).size());
2767 else if (DebugLocs
.getBytes(Entry
).size() <= std::numeric_limits
<uint16_t>::max())
2768 Asm
->emitInt16(DebugLocs
.getBytes(Entry
).size());
2770 // The entry is too big to fit into 16 bit, drop it as there is nothing we
2776 APByteStreamer
Streamer(*Asm
);
2777 emitDebugLocEntry(Streamer
, Entry
, CU
);
2780 // Emit the header of a DWARF 5 range list table list table. Returns the symbol
2781 // that designates the end of the table for the caller to emit when the table is
2783 static MCSymbol
*emitRnglistsTableHeader(AsmPrinter
*Asm
,
2784 const DwarfFile
&Holder
) {
2785 MCSymbol
*TableEnd
= mcdwarf::emitListsTableHeaderStart(*Asm
->OutStreamer
);
2787 Asm
->OutStreamer
->AddComment("Offset entry count");
2788 Asm
->emitInt32(Holder
.getRangeLists().size());
2789 Asm
->OutStreamer
->emitLabel(Holder
.getRnglistsTableBaseSym());
2791 for (const RangeSpanList
&List
: Holder
.getRangeLists())
2792 Asm
->emitLabelDifference(List
.Label
, Holder
.getRnglistsTableBaseSym(),
2793 Asm
->getDwarfOffsetByteSize());
2798 // Emit the header of a DWARF 5 locations list table. Returns the symbol that
2799 // designates the end of the table for the caller to emit when the table is
2801 static MCSymbol
*emitLoclistsTableHeader(AsmPrinter
*Asm
,
2802 const DwarfDebug
&DD
) {
2803 MCSymbol
*TableEnd
= mcdwarf::emitListsTableHeaderStart(*Asm
->OutStreamer
);
2805 const auto &DebugLocs
= DD
.getDebugLocs();
2807 Asm
->OutStreamer
->AddComment("Offset entry count");
2808 Asm
->emitInt32(DebugLocs
.getLists().size());
2809 Asm
->OutStreamer
->emitLabel(DebugLocs
.getSym());
2811 for (const auto &List
: DebugLocs
.getLists())
2812 Asm
->emitLabelDifference(List
.Label
, DebugLocs
.getSym(),
2813 Asm
->getDwarfOffsetByteSize());
2818 template <typename Ranges
, typename PayloadEmitter
>
2819 static void emitRangeList(
2820 DwarfDebug
&DD
, AsmPrinter
*Asm
, MCSymbol
*Sym
, const Ranges
&R
,
2821 const DwarfCompileUnit
&CU
, unsigned BaseAddressx
, unsigned OffsetPair
,
2822 unsigned StartxLength
, unsigned EndOfList
,
2823 StringRef (*StringifyEnum
)(unsigned),
2824 bool ShouldUseBaseAddress
,
2825 PayloadEmitter EmitPayload
) {
2827 auto Size
= Asm
->MAI
->getCodePointerSize();
2828 bool UseDwarf5
= DD
.getDwarfVersion() >= 5;
2830 // Emit our symbol so we can find the beginning of the range.
2831 Asm
->OutStreamer
->emitLabel(Sym
);
2833 // Gather all the ranges that apply to the same section so they can share
2834 // a base address entry.
2835 MapVector
<const MCSection
*, std::vector
<decltype(&*R
.begin())>> SectionRanges
;
2837 for (const auto &Range
: R
)
2838 SectionRanges
[&Range
.Begin
->getSection()].push_back(&Range
);
2840 const MCSymbol
*CUBase
= CU
.getBaseAddress();
2841 bool BaseIsSet
= false;
2842 for (const auto &P
: SectionRanges
) {
2843 auto *Base
= CUBase
;
2844 if (!Base
&& ShouldUseBaseAddress
) {
2845 const MCSymbol
*Begin
= P
.second
.front()->Begin
;
2846 const MCSymbol
*NewBase
= DD
.getSectionLabel(&Begin
->getSection());
2850 Asm
->OutStreamer
->emitIntValue(-1, Size
);
2851 Asm
->OutStreamer
->AddComment(" base address");
2852 Asm
->OutStreamer
->emitSymbolValue(Base
, Size
);
2853 } else if (NewBase
!= Begin
|| P
.second
.size() > 1) {
2854 // Only use a base address if
2855 // * the existing pool address doesn't match (NewBase != Begin)
2856 // * or, there's more than one entry to share the base address
2859 Asm
->OutStreamer
->AddComment(StringifyEnum(BaseAddressx
));
2860 Asm
->emitInt8(BaseAddressx
);
2861 Asm
->OutStreamer
->AddComment(" base address index");
2862 Asm
->emitULEB128(DD
.getAddressPool().getIndex(Base
));
2864 } else if (BaseIsSet
&& !UseDwarf5
) {
2867 Asm
->OutStreamer
->emitIntValue(-1, Size
);
2868 Asm
->OutStreamer
->emitIntValue(0, Size
);
2871 for (const auto *RS
: P
.second
) {
2872 const MCSymbol
*Begin
= RS
->Begin
;
2873 const MCSymbol
*End
= RS
->End
;
2874 assert(Begin
&& "Range without a begin symbol?");
2875 assert(End
&& "Range without an end symbol?");
2878 // Emit offset_pair when we have a base.
2879 Asm
->OutStreamer
->AddComment(StringifyEnum(OffsetPair
));
2880 Asm
->emitInt8(OffsetPair
);
2881 Asm
->OutStreamer
->AddComment(" starting offset");
2882 Asm
->emitLabelDifferenceAsULEB128(Begin
, Base
);
2883 Asm
->OutStreamer
->AddComment(" ending offset");
2884 Asm
->emitLabelDifferenceAsULEB128(End
, Base
);
2886 Asm
->emitLabelDifference(Begin
, Base
, Size
);
2887 Asm
->emitLabelDifference(End
, Base
, Size
);
2889 } else if (UseDwarf5
) {
2890 Asm
->OutStreamer
->AddComment(StringifyEnum(StartxLength
));
2891 Asm
->emitInt8(StartxLength
);
2892 Asm
->OutStreamer
->AddComment(" start index");
2893 Asm
->emitULEB128(DD
.getAddressPool().getIndex(Begin
));
2894 Asm
->OutStreamer
->AddComment(" length");
2895 Asm
->emitLabelDifferenceAsULEB128(End
, Begin
);
2897 Asm
->OutStreamer
->emitSymbolValue(Begin
, Size
);
2898 Asm
->OutStreamer
->emitSymbolValue(End
, Size
);
2905 Asm
->OutStreamer
->AddComment(StringifyEnum(EndOfList
));
2906 Asm
->emitInt8(EndOfList
);
2908 // Terminate the list with two 0 values.
2909 Asm
->OutStreamer
->emitIntValue(0, Size
);
2910 Asm
->OutStreamer
->emitIntValue(0, Size
);
2914 // Handles emission of both debug_loclist / debug_loclist.dwo
2915 static void emitLocList(DwarfDebug
&DD
, AsmPrinter
*Asm
, const DebugLocStream::List
&List
) {
2916 emitRangeList(DD
, Asm
, List
.Label
, DD
.getDebugLocs().getEntries(List
),
2917 *List
.CU
, dwarf::DW_LLE_base_addressx
,
2918 dwarf::DW_LLE_offset_pair
, dwarf::DW_LLE_startx_length
,
2919 dwarf::DW_LLE_end_of_list
, llvm::dwarf::LocListEncodingString
,
2920 /* ShouldUseBaseAddress */ true,
2921 [&](const DebugLocStream::Entry
&E
) {
2922 DD
.emitDebugLocEntryLocation(E
, List
.CU
);
2926 void DwarfDebug::emitDebugLocImpl(MCSection
*Sec
) {
2927 if (DebugLocs
.getLists().empty())
2930 Asm
->OutStreamer
->switchSection(Sec
);
2932 MCSymbol
*TableEnd
= nullptr;
2933 if (getDwarfVersion() >= 5)
2934 TableEnd
= emitLoclistsTableHeader(Asm
, *this);
2936 for (const auto &List
: DebugLocs
.getLists())
2937 emitLocList(*this, Asm
, List
);
2940 Asm
->OutStreamer
->emitLabel(TableEnd
);
2943 // Emit locations into the .debug_loc/.debug_loclists section.
2944 void DwarfDebug::emitDebugLoc() {
2946 getDwarfVersion() >= 5
2947 ? Asm
->getObjFileLowering().getDwarfLoclistsSection()
2948 : Asm
->getObjFileLowering().getDwarfLocSection());
2951 // Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section.
2952 void DwarfDebug::emitDebugLocDWO() {
2953 if (getDwarfVersion() >= 5) {
2955 Asm
->getObjFileLowering().getDwarfLoclistsDWOSection());
2960 for (const auto &List
: DebugLocs
.getLists()) {
2961 Asm
->OutStreamer
->switchSection(
2962 Asm
->getObjFileLowering().getDwarfLocDWOSection());
2963 Asm
->OutStreamer
->emitLabel(List
.Label
);
2965 for (const auto &Entry
: DebugLocs
.getEntries(List
)) {
2966 // GDB only supports startx_length in pre-standard split-DWARF.
2967 // (in v5 standard loclists, it currently* /only/ supports base_address +
2968 // offset_pair, so the implementations can't really share much since they
2969 // need to use different representations)
2970 // * as of October 2018, at least
2972 // In v5 (see emitLocList), this uses SectionLabels to reuse existing
2973 // addresses in the address pool to minimize object size/relocations.
2974 Asm
->emitInt8(dwarf::DW_LLE_startx_length
);
2975 unsigned idx
= AddrPool
.getIndex(Entry
.Begin
);
2976 Asm
->emitULEB128(idx
);
2977 // Also the pre-standard encoding is slightly different, emitting this as
2978 // an address-length entry here, but its a ULEB128 in DWARFv5 loclists.
2979 Asm
->emitLabelDifference(Entry
.End
, Entry
.Begin
, 4);
2980 emitDebugLocEntryLocation(Entry
, List
.CU
);
2982 Asm
->emitInt8(dwarf::DW_LLE_end_of_list
);
2987 const MCSymbol
*Start
, *End
;
2990 // Emit a debug aranges section, containing a CU lookup for any
2991 // address we can tie back to a CU.
2992 void DwarfDebug::emitDebugARanges() {
2993 if (ArangeLabels
.empty())
2996 // Provides a unique id per text section.
2997 MapVector
<MCSection
*, SmallVector
<SymbolCU
, 8>> SectionMap
;
2999 // Filter labels by section.
3000 for (const SymbolCU
&SCU
: ArangeLabels
) {
3001 if (SCU
.Sym
->isInSection()) {
3002 // Make a note of this symbol and it's section.
3003 MCSection
*Section
= &SCU
.Sym
->getSection();
3004 SectionMap
[Section
].push_back(SCU
);
3006 // Some symbols (e.g. common/bss on mach-o) can have no section but still
3007 // appear in the output. This sucks as we rely on sections to build
3008 // arange spans. We can do it without, but it's icky.
3009 SectionMap
[nullptr].push_back(SCU
);
3013 DenseMap
<DwarfCompileUnit
*, std::vector
<ArangeSpan
>> Spans
;
3015 for (auto &I
: SectionMap
) {
3016 MCSection
*Section
= I
.first
;
3017 SmallVector
<SymbolCU
, 8> &List
= I
.second
;
3018 assert(!List
.empty());
3020 // If we have no section (e.g. common), just write out
3021 // individual spans for each symbol.
3023 for (const SymbolCU
&Cur
: List
) {
3025 Span
.Start
= Cur
.Sym
;
3028 Spans
[Cur
.CU
].push_back(Span
);
3033 // Insert a final terminator.
3034 List
.push_back(SymbolCU(nullptr, Asm
->OutStreamer
->endSection(Section
)));
3036 // Build spans between each label.
3037 const MCSymbol
*StartSym
= List
[0].Sym
;
3038 for (size_t n
= 1, e
= List
.size(); n
< e
; n
++) {
3039 const SymbolCU
&Prev
= List
[n
- 1];
3040 const SymbolCU
&Cur
= List
[n
];
3042 // Try and build the longest span we can within the same CU.
3043 if (Cur
.CU
!= Prev
.CU
) {
3045 Span
.Start
= StartSym
;
3048 Spans
[Prev
.CU
].push_back(Span
);
3054 // Start the dwarf aranges section.
3055 Asm
->OutStreamer
->switchSection(
3056 Asm
->getObjFileLowering().getDwarfARangesSection());
3058 unsigned PtrSize
= Asm
->MAI
->getCodePointerSize();
3060 // Build a list of CUs used.
3061 std::vector
<DwarfCompileUnit
*> CUs
;
3062 for (const auto &it
: Spans
) {
3063 DwarfCompileUnit
*CU
= it
.first
;
3067 // Sort the CU list (again, to ensure consistent output order).
3068 llvm::sort(CUs
, [](const DwarfCompileUnit
*A
, const DwarfCompileUnit
*B
) {
3069 return A
->getUniqueID() < B
->getUniqueID();
3072 // Emit an arange table for each CU we used.
3073 for (DwarfCompileUnit
*CU
: CUs
) {
3074 std::vector
<ArangeSpan
> &List
= Spans
[CU
];
3076 // Describe the skeleton CU's offset and length, not the dwo file's.
3077 if (auto *Skel
= CU
->getSkeleton())
3080 // Emit size of content not including length itself.
3081 unsigned ContentSize
=
3082 sizeof(int16_t) + // DWARF ARange version number
3083 Asm
->getDwarfOffsetByteSize() + // Offset of CU in the .debug_info
3085 sizeof(int8_t) + // Pointer Size (in bytes)
3086 sizeof(int8_t); // Segment Size (in bytes)
3088 unsigned TupleSize
= PtrSize
* 2;
3090 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
3091 unsigned Padding
= offsetToAlignment(
3092 Asm
->getUnitLengthFieldByteSize() + ContentSize
, Align(TupleSize
));
3094 ContentSize
+= Padding
;
3095 ContentSize
+= (List
.size() + 1) * TupleSize
;
3097 // For each compile unit, write the list of spans it covers.
3098 Asm
->emitDwarfUnitLength(ContentSize
, "Length of ARange Set");
3099 Asm
->OutStreamer
->AddComment("DWARF Arange version number");
3100 Asm
->emitInt16(dwarf::DW_ARANGES_VERSION
);
3101 Asm
->OutStreamer
->AddComment("Offset Into Debug Info Section");
3102 emitSectionReference(*CU
);
3103 Asm
->OutStreamer
->AddComment("Address Size (in bytes)");
3104 Asm
->emitInt8(PtrSize
);
3105 Asm
->OutStreamer
->AddComment("Segment Size (in bytes)");
3108 Asm
->OutStreamer
->emitFill(Padding
, 0xff);
3110 for (const ArangeSpan
&Span
: List
) {
3111 Asm
->emitLabelReference(Span
.Start
, PtrSize
);
3113 // Calculate the size as being from the span start to its end.
3115 // If the size is zero, then round it up to one byte. The DWARF
3116 // specification requires that entries in this table have nonzero
3118 auto SizeRef
= SymSize
.find(Span
.Start
);
3119 if ((SizeRef
== SymSize
.end() || SizeRef
->second
!= 0) && Span
.End
) {
3120 Asm
->emitLabelDifference(Span
.End
, Span
.Start
, PtrSize
);
3122 // For symbols without an end marker (e.g. common), we
3123 // write a single arange entry containing just that one symbol.
3125 if (SizeRef
== SymSize
.end() || SizeRef
->second
== 0)
3128 Size
= SizeRef
->second
;
3130 Asm
->OutStreamer
->emitIntValue(Size
, PtrSize
);
3134 Asm
->OutStreamer
->AddComment("ARange terminator");
3135 Asm
->OutStreamer
->emitIntValue(0, PtrSize
);
3136 Asm
->OutStreamer
->emitIntValue(0, PtrSize
);
3140 /// Emit a single range list. We handle both DWARF v5 and earlier.
3141 static void emitRangeList(DwarfDebug
&DD
, AsmPrinter
*Asm
,
3142 const RangeSpanList
&List
) {
3143 emitRangeList(DD
, Asm
, List
.Label
, List
.Ranges
, *List
.CU
,
3144 dwarf::DW_RLE_base_addressx
, dwarf::DW_RLE_offset_pair
,
3145 dwarf::DW_RLE_startx_length
, dwarf::DW_RLE_end_of_list
,
3146 llvm::dwarf::RangeListEncodingString
,
3147 List
.CU
->getCUNode()->getRangesBaseAddress() ||
3148 DD
.getDwarfVersion() >= 5,
3152 void DwarfDebug::emitDebugRangesImpl(const DwarfFile
&Holder
, MCSection
*Section
) {
3153 if (Holder
.getRangeLists().empty())
3156 assert(useRangesSection());
3157 assert(!CUMap
.empty());
3158 assert(llvm::any_of(CUMap
, [](const decltype(CUMap
)::value_type
&Pair
) {
3159 return !Pair
.second
->getCUNode()->isDebugDirectivesOnly();
3162 Asm
->OutStreamer
->switchSection(Section
);
3164 MCSymbol
*TableEnd
= nullptr;
3165 if (getDwarfVersion() >= 5)
3166 TableEnd
= emitRnglistsTableHeader(Asm
, Holder
);
3168 for (const RangeSpanList
&List
: Holder
.getRangeLists())
3169 emitRangeList(*this, Asm
, List
);
3172 Asm
->OutStreamer
->emitLabel(TableEnd
);
3175 /// Emit address ranges into the .debug_ranges section or into the DWARF v5
3176 /// .debug_rnglists section.
3177 void DwarfDebug::emitDebugRanges() {
3178 const auto &Holder
= useSplitDwarf() ? SkeletonHolder
: InfoHolder
;
3180 emitDebugRangesImpl(Holder
,
3181 getDwarfVersion() >= 5
3182 ? Asm
->getObjFileLowering().getDwarfRnglistsSection()
3183 : Asm
->getObjFileLowering().getDwarfRangesSection());
3186 void DwarfDebug::emitDebugRangesDWO() {
3187 emitDebugRangesImpl(InfoHolder
,
3188 Asm
->getObjFileLowering().getDwarfRnglistsDWOSection());
3191 /// Emit the header of a DWARF 5 macro section, or the GNU extension for
3193 static void emitMacroHeader(AsmPrinter
*Asm
, const DwarfDebug
&DD
,
3194 const DwarfCompileUnit
&CU
, uint16_t DwarfVersion
) {
3195 enum HeaderFlagMask
{
3196 #define HANDLE_MACRO_FLAG(ID, NAME) MACRO_FLAG_##NAME = ID,
3197 #include "llvm/BinaryFormat/Dwarf.def"
3199 Asm
->OutStreamer
->AddComment("Macro information version");
3200 Asm
->emitInt16(DwarfVersion
>= 5 ? DwarfVersion
: 4);
3201 // We emit the line offset flag unconditionally here, since line offset should
3202 // be mostly present.
3203 if (Asm
->isDwarf64()) {
3204 Asm
->OutStreamer
->AddComment("Flags: 64 bit, debug_line_offset present");
3205 Asm
->emitInt8(MACRO_FLAG_OFFSET_SIZE
| MACRO_FLAG_DEBUG_LINE_OFFSET
);
3207 Asm
->OutStreamer
->AddComment("Flags: 32 bit, debug_line_offset present");
3208 Asm
->emitInt8(MACRO_FLAG_DEBUG_LINE_OFFSET
);
3210 Asm
->OutStreamer
->AddComment("debug_line_offset");
3211 if (DD
.useSplitDwarf())
3212 Asm
->emitDwarfLengthOrOffset(0);
3214 Asm
->emitDwarfSymbolReference(CU
.getLineTableStartSym());
3217 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes
, DwarfCompileUnit
&U
) {
3218 for (auto *MN
: Nodes
) {
3219 if (auto *M
= dyn_cast
<DIMacro
>(MN
))
3221 else if (auto *F
= dyn_cast
<DIMacroFile
>(MN
))
3222 emitMacroFile(*F
, U
);
3224 llvm_unreachable("Unexpected DI type!");
3228 void DwarfDebug::emitMacro(DIMacro
&M
) {
3229 StringRef Name
= M
.getName();
3230 StringRef Value
= M
.getValue();
3232 // There should be one space between the macro name and the macro value in
3233 // define entries. In undef entries, only the macro name is emitted.
3234 std::string Str
= Value
.empty() ? Name
.str() : (Name
+ " " + Value
).str();
3236 if (UseDebugMacroSection
) {
3237 if (getDwarfVersion() >= 5) {
3238 unsigned Type
= M
.getMacinfoType() == dwarf::DW_MACINFO_define
3239 ? dwarf::DW_MACRO_define_strx
3240 : dwarf::DW_MACRO_undef_strx
;
3241 Asm
->OutStreamer
->AddComment(dwarf::MacroString(Type
));
3242 Asm
->emitULEB128(Type
);
3243 Asm
->OutStreamer
->AddComment("Line Number");
3244 Asm
->emitULEB128(M
.getLine());
3245 Asm
->OutStreamer
->AddComment("Macro String");
3247 InfoHolder
.getStringPool().getIndexedEntry(*Asm
, Str
).getIndex());
3249 unsigned Type
= M
.getMacinfoType() == dwarf::DW_MACINFO_define
3250 ? dwarf::DW_MACRO_GNU_define_indirect
3251 : dwarf::DW_MACRO_GNU_undef_indirect
;
3252 Asm
->OutStreamer
->AddComment(dwarf::GnuMacroString(Type
));
3253 Asm
->emitULEB128(Type
);
3254 Asm
->OutStreamer
->AddComment("Line Number");
3255 Asm
->emitULEB128(M
.getLine());
3256 Asm
->OutStreamer
->AddComment("Macro String");
3257 Asm
->emitDwarfSymbolReference(
3258 InfoHolder
.getStringPool().getEntry(*Asm
, Str
).getSymbol());
3261 Asm
->OutStreamer
->AddComment(dwarf::MacinfoString(M
.getMacinfoType()));
3262 Asm
->emitULEB128(M
.getMacinfoType());
3263 Asm
->OutStreamer
->AddComment("Line Number");
3264 Asm
->emitULEB128(M
.getLine());
3265 Asm
->OutStreamer
->AddComment("Macro String");
3266 Asm
->OutStreamer
->emitBytes(Str
);
3267 Asm
->emitInt8('\0');
3271 void DwarfDebug::emitMacroFileImpl(
3272 DIMacroFile
&MF
, DwarfCompileUnit
&U
, unsigned StartFile
, unsigned EndFile
,
3273 StringRef (*MacroFormToString
)(unsigned Form
)) {
3275 Asm
->OutStreamer
->AddComment(MacroFormToString(StartFile
));
3276 Asm
->emitULEB128(StartFile
);
3277 Asm
->OutStreamer
->AddComment("Line Number");
3278 Asm
->emitULEB128(MF
.getLine());
3279 Asm
->OutStreamer
->AddComment("File Number");
3280 DIFile
&F
= *MF
.getFile();
3281 if (useSplitDwarf())
3282 Asm
->emitULEB128(getDwoLineTable(U
)->getFile(
3283 F
.getDirectory(), F
.getFilename(), getMD5AsBytes(&F
),
3284 Asm
->OutContext
.getDwarfVersion(), F
.getSource()));
3286 Asm
->emitULEB128(U
.getOrCreateSourceID(&F
));
3287 handleMacroNodes(MF
.getElements(), U
);
3288 Asm
->OutStreamer
->AddComment(MacroFormToString(EndFile
));
3289 Asm
->emitULEB128(EndFile
);
3292 void DwarfDebug::emitMacroFile(DIMacroFile
&F
, DwarfCompileUnit
&U
) {
3293 // DWARFv5 macro and DWARFv4 macinfo share some common encodings,
3294 // so for readibility/uniformity, We are explicitly emitting those.
3295 assert(F
.getMacinfoType() == dwarf::DW_MACINFO_start_file
);
3296 if (UseDebugMacroSection
)
3298 F
, U
, dwarf::DW_MACRO_start_file
, dwarf::DW_MACRO_end_file
,
3299 (getDwarfVersion() >= 5) ? dwarf::MacroString
: dwarf::GnuMacroString
);
3301 emitMacroFileImpl(F
, U
, dwarf::DW_MACINFO_start_file
,
3302 dwarf::DW_MACINFO_end_file
, dwarf::MacinfoString
);
3305 void DwarfDebug::emitDebugMacinfoImpl(MCSection
*Section
) {
3306 for (const auto &P
: CUMap
) {
3307 auto &TheCU
= *P
.second
;
3308 auto *SkCU
= TheCU
.getSkeleton();
3309 DwarfCompileUnit
&U
= SkCU
? *SkCU
: TheCU
;
3310 auto *CUNode
= cast
<DICompileUnit
>(P
.first
);
3311 DIMacroNodeArray Macros
= CUNode
->getMacros();
3314 Asm
->OutStreamer
->switchSection(Section
);
3315 Asm
->OutStreamer
->emitLabel(U
.getMacroLabelBegin());
3316 if (UseDebugMacroSection
)
3317 emitMacroHeader(Asm
, *this, U
, getDwarfVersion());
3318 handleMacroNodes(Macros
, U
);
3319 Asm
->OutStreamer
->AddComment("End Of Macro List Mark");
3324 /// Emit macros into a debug macinfo/macro section.
3325 void DwarfDebug::emitDebugMacinfo() {
3326 auto &ObjLower
= Asm
->getObjFileLowering();
3327 emitDebugMacinfoImpl(UseDebugMacroSection
3328 ? ObjLower
.getDwarfMacroSection()
3329 : ObjLower
.getDwarfMacinfoSection());
3332 void DwarfDebug::emitDebugMacinfoDWO() {
3333 auto &ObjLower
= Asm
->getObjFileLowering();
3334 emitDebugMacinfoImpl(UseDebugMacroSection
3335 ? ObjLower
.getDwarfMacroDWOSection()
3336 : ObjLower
.getDwarfMacinfoDWOSection());
3339 // DWARF5 Experimental Separate Dwarf emitters.
3341 void DwarfDebug::initSkeletonUnit(const DwarfUnit
&U
, DIE
&Die
,
3342 std::unique_ptr
<DwarfCompileUnit
> NewU
) {
3344 if (!CompilationDir
.empty())
3345 NewU
->addString(Die
, dwarf::DW_AT_comp_dir
, CompilationDir
);
3346 addGnuPubAttributes(*NewU
, Die
);
3348 SkeletonHolder
.addUnit(std::move(NewU
));
3351 DwarfCompileUnit
&DwarfDebug::constructSkeletonCU(const DwarfCompileUnit
&CU
) {
3353 auto OwnedUnit
= std::make_unique
<DwarfCompileUnit
>(
3354 CU
.getUniqueID(), CU
.getCUNode(), Asm
, this, &SkeletonHolder
,
3355 UnitKind::Skeleton
);
3356 DwarfCompileUnit
&NewCU
= *OwnedUnit
;
3357 NewCU
.setSection(Asm
->getObjFileLowering().getDwarfInfoSection());
3359 NewCU
.initStmtList();
3361 if (useSegmentedStringOffsetsTable())
3362 NewCU
.addStringOffsetsStart();
3364 initSkeletonUnit(CU
, NewCU
.getUnitDie(), std::move(OwnedUnit
));
3369 // Emit the .debug_info.dwo section for separated dwarf. This contains the
3370 // compile units that would normally be in debug_info.
3371 void DwarfDebug::emitDebugInfoDWO() {
3372 assert(useSplitDwarf() && "No split dwarf debug info?");
3373 // Don't emit relocations into the dwo file.
3374 InfoHolder
.emitUnits(/* UseOffsets */ true);
3377 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3378 // abbreviations for the .debug_info.dwo section.
3379 void DwarfDebug::emitDebugAbbrevDWO() {
3380 assert(useSplitDwarf() && "No split dwarf?");
3381 InfoHolder
.emitAbbrevs(Asm
->getObjFileLowering().getDwarfAbbrevDWOSection());
3384 void DwarfDebug::emitDebugLineDWO() {
3385 assert(useSplitDwarf() && "No split dwarf?");
3386 SplitTypeUnitFileTable
.Emit(
3387 *Asm
->OutStreamer
, MCDwarfLineTableParams(),
3388 Asm
->getObjFileLowering().getDwarfLineDWOSection());
3391 void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
3392 assert(useSplitDwarf() && "No split dwarf?");
3393 InfoHolder
.getStringPool().emitStringOffsetsTableHeader(
3394 *Asm
, Asm
->getObjFileLowering().getDwarfStrOffDWOSection(),
3395 InfoHolder
.getStringOffsetsStartSym());
3398 // Emit the .debug_str.dwo section for separated dwarf. This contains the
3399 // string section and is identical in format to traditional .debug_str
3401 void DwarfDebug::emitDebugStrDWO() {
3402 if (useSegmentedStringOffsetsTable())
3403 emitStringOffsetsTableHeaderDWO();
3404 assert(useSplitDwarf() && "No split dwarf?");
3405 MCSection
*OffSec
= Asm
->getObjFileLowering().getDwarfStrOffDWOSection();
3406 InfoHolder
.emitStrings(Asm
->getObjFileLowering().getDwarfStrDWOSection(),
3407 OffSec
, /* UseRelativeOffsets = */ false);
3410 // Emit address pool.
3411 void DwarfDebug::emitDebugAddr() {
3412 AddrPool
.emit(*Asm
, Asm
->getObjFileLowering().getDwarfAddrSection());
3415 MCDwarfDwoLineTable
*DwarfDebug::getDwoLineTable(const DwarfCompileUnit
&CU
) {
3416 if (!useSplitDwarf())
3418 const DICompileUnit
*DIUnit
= CU
.getCUNode();
3419 SplitTypeUnitFileTable
.maybeSetRootFile(
3420 DIUnit
->getDirectory(), DIUnit
->getFilename(),
3421 getMD5AsBytes(DIUnit
->getFile()), DIUnit
->getSource());
3422 return &SplitTypeUnitFileTable
;
3425 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier
) {
3427 Hash
.update(Identifier
);
3428 // ... take the least significant 8 bytes and return those. Our MD5
3429 // implementation always returns its results in little endian, so we actually
3430 // need the "high" word.
3431 MD5::MD5Result Result
;
3433 return Result
.high();
3436 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit
&CU
,
3437 StringRef Identifier
, DIE
&RefDie
,
3438 const DICompositeType
*CTy
) {
3439 // Fast path if we're building some type units and one has already used the
3440 // address pool we know we're going to throw away all this work anyway, so
3441 // don't bother building dependent types.
3442 if (!TypeUnitsUnderConstruction
.empty() && AddrPool
.hasBeenUsed())
3445 auto Ins
= TypeSignatures
.insert(std::make_pair(CTy
, 0));
3447 CU
.addDIETypeSignature(RefDie
, Ins
.first
->second
);
3451 setCurrentDWARF5AccelTable(DWARF5AccelTableKind::TU
);
3452 bool TopLevelType
= TypeUnitsUnderConstruction
.empty();
3453 AddrPool
.resetUsedFlag();
3455 auto OwnedUnit
= std::make_unique
<DwarfTypeUnit
>(
3456 CU
, Asm
, this, &InfoHolder
, NumTypeUnitsCreated
++, getDwoLineTable(CU
));
3457 DwarfTypeUnit
&NewTU
= *OwnedUnit
;
3458 DIE
&UnitDie
= NewTU
.getUnitDie();
3459 TypeUnitsUnderConstruction
.emplace_back(std::move(OwnedUnit
), CTy
);
3461 NewTU
.addUInt(UnitDie
, dwarf::DW_AT_language
, dwarf::DW_FORM_data2
,
3464 uint64_t Signature
= makeTypeSignature(Identifier
);
3465 NewTU
.setTypeSignature(Signature
);
3466 Ins
.first
->second
= Signature
;
3468 if (useSplitDwarf()) {
3469 // Although multiple type units can have the same signature, they are not
3470 // guranteed to be bit identical. When LLDB uses .debug_names it needs to
3471 // know from which CU a type unit came from. These two attrbutes help it to
3473 if (getDwarfVersion() >= 5) {
3474 if (!CompilationDir
.empty())
3475 NewTU
.addString(UnitDie
, dwarf::DW_AT_comp_dir
, CompilationDir
);
3476 NewTU
.addString(UnitDie
, dwarf::DW_AT_dwo_name
,
3477 Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
);
3479 MCSection
*Section
=
3480 getDwarfVersion() <= 4
3481 ? Asm
->getObjFileLowering().getDwarfTypesDWOSection()
3482 : Asm
->getObjFileLowering().getDwarfInfoDWOSection();
3483 NewTU
.setSection(Section
);
3485 MCSection
*Section
=
3486 getDwarfVersion() <= 4
3487 ? Asm
->getObjFileLowering().getDwarfTypesSection(Signature
)
3488 : Asm
->getObjFileLowering().getDwarfInfoSection(Signature
);
3489 NewTU
.setSection(Section
);
3490 // Non-split type units reuse the compile unit's line table.
3491 CU
.applyStmtList(UnitDie
);
3494 // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
3496 if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
3497 NewTU
.addStringOffsetsStart();
3499 NewTU
.setType(NewTU
.createTypeDIE(CTy
));
3502 auto TypeUnitsToAdd
= std::move(TypeUnitsUnderConstruction
);
3503 TypeUnitsUnderConstruction
.clear();
3505 // Types referencing entries in the address table cannot be placed in type
3507 if (AddrPool
.hasBeenUsed()) {
3508 AccelTypeUnitsDebugNames
.clear();
3509 // Remove all the types built while building this type.
3510 // This is pessimistic as some of these types might not be dependent on
3511 // the type that used an address.
3512 for (const auto &TU
: TypeUnitsToAdd
)
3513 TypeSignatures
.erase(TU
.second
);
3515 // Construct this type in the CU directly.
3516 // This is inefficient because all the dependent types will be rebuilt
3517 // from scratch, including building them in type units, discovering that
3518 // they depend on addresses, throwing them out and rebuilding them.
3519 setCurrentDWARF5AccelTable(DWARF5AccelTableKind::CU
);
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());
3529 if (getDwarfVersion() >= 5 &&
3530 getAccelTableKind() == AccelTableKind::Dwarf
) {
3531 if (useSplitDwarf())
3532 AccelDebugNames
.addTypeUnitSignature(*TU
.first
);
3534 AccelDebugNames
.addTypeUnitSymbol(*TU
.first
);
3537 AccelTypeUnitsDebugNames
.convertDieToOffset();
3538 AccelDebugNames
.addTypeEntries(AccelTypeUnitsDebugNames
);
3539 AccelTypeUnitsDebugNames
.clear();
3540 setCurrentDWARF5AccelTable(DWARF5AccelTableKind::CU
);
3542 CU
.addDIETypeSignature(RefDie
, Signature
);
3545 // Add the Name along with its companion DIE to the appropriate accelerator
3546 // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
3547 // AccelTableKind::Apple, we use the table we got as an argument). If
3548 // accelerator tables are disabled, this function does nothing.
3549 template <typename DataT
>
3550 void DwarfDebug::addAccelNameImpl(
3551 const DwarfUnit
&Unit
,
3552 const DICompileUnit::DebugNameTableKind NameTableKind
,
3553 AccelTable
<DataT
> &AppleAccel
, StringRef Name
, const DIE
&Die
) {
3554 if (getAccelTableKind() == AccelTableKind::None
||
3555 Unit
.getUnitDie().getTag() == dwarf::DW_TAG_skeleton_unit
|| Name
.empty())
3558 if (getAccelTableKind() != AccelTableKind::Apple
&&
3559 NameTableKind
!= DICompileUnit::DebugNameTableKind::Apple
&&
3560 NameTableKind
!= DICompileUnit::DebugNameTableKind::Default
)
3563 DwarfFile
&Holder
= useSplitDwarf() ? SkeletonHolder
: InfoHolder
;
3564 DwarfStringPoolEntryRef Ref
= Holder
.getStringPool().getEntry(*Asm
, Name
);
3566 switch (getAccelTableKind()) {
3567 case AccelTableKind::Apple
:
3568 AppleAccel
.addName(Ref
, Die
);
3570 case AccelTableKind::Dwarf
: {
3571 DWARF5AccelTable
&Current
= getCurrentDWARF5AccelTable();
3572 assert(((&Current
== &AccelTypeUnitsDebugNames
) ||
3573 ((&Current
== &AccelDebugNames
) &&
3574 (Unit
.getUnitDie().getTag() != dwarf::DW_TAG_type_unit
))) &&
3575 "Kind is CU but TU is being processed.");
3576 assert(((&Current
== &AccelDebugNames
) ||
3577 ((&Current
== &AccelTypeUnitsDebugNames
) &&
3578 (Unit
.getUnitDie().getTag() == dwarf::DW_TAG_type_unit
))) &&
3579 "Kind is TU but CU is being processed.");
3580 // The type unit can be discarded, so need to add references to final
3581 // acceleration table once we know it's complete and we emit it.
3582 Current
.addName(Ref
, Die
, Unit
.getUniqueID(),
3583 Unit
.getUnitDie().getTag() == dwarf::DW_TAG_type_unit
);
3586 case AccelTableKind::Default
:
3587 llvm_unreachable("Default should have already been resolved.");
3588 case AccelTableKind::None
:
3589 llvm_unreachable("None handled above");
3593 void DwarfDebug::addAccelName(
3594 const DwarfUnit
&Unit
,
3595 const DICompileUnit::DebugNameTableKind NameTableKind
, StringRef Name
,
3597 addAccelNameImpl(Unit
, NameTableKind
, AccelNames
, Name
, Die
);
3600 void DwarfDebug::addAccelObjC(
3601 const DwarfUnit
&Unit
,
3602 const DICompileUnit::DebugNameTableKind NameTableKind
, StringRef Name
,
3604 // ObjC names go only into the Apple accelerator tables.
3605 if (getAccelTableKind() == AccelTableKind::Apple
)
3606 addAccelNameImpl(Unit
, NameTableKind
, AccelObjC
, Name
, Die
);
3609 void DwarfDebug::addAccelNamespace(
3610 const DwarfUnit
&Unit
,
3611 const DICompileUnit::DebugNameTableKind NameTableKind
, StringRef Name
,
3613 addAccelNameImpl(Unit
, NameTableKind
, AccelNamespace
, Name
, Die
);
3616 void DwarfDebug::addAccelType(
3617 const DwarfUnit
&Unit
,
3618 const DICompileUnit::DebugNameTableKind NameTableKind
, StringRef Name
,
3619 const DIE
&Die
, char Flags
) {
3620 addAccelNameImpl(Unit
, NameTableKind
, AccelTypes
, Name
, Die
);
3623 uint16_t DwarfDebug::getDwarfVersion() const {
3624 return Asm
->OutStreamer
->getContext().getDwarfVersion();
3627 dwarf::Form
DwarfDebug::getDwarfSectionOffsetForm() const {
3628 if (Asm
->getDwarfVersion() >= 4)
3629 return dwarf::Form::DW_FORM_sec_offset
;
3630 assert((!Asm
->isDwarf64() || (Asm
->getDwarfVersion() == 3)) &&
3631 "DWARF64 is not defined prior DWARFv3");
3632 return Asm
->isDwarf64() ? dwarf::Form::DW_FORM_data8
3633 : dwarf::Form::DW_FORM_data4
;
3636 const MCSymbol
*DwarfDebug::getSectionLabel(const MCSection
*S
) {
3637 return SectionLabels
.lookup(S
);
3640 void DwarfDebug::insertSectionLabel(const MCSymbol
*S
) {
3641 if (SectionLabels
.insert(std::make_pair(&S
->getSection(), S
)).second
)
3642 if (useSplitDwarf() || getDwarfVersion() >= 5)
3643 AddrPool
.getIndex(S
);
3646 std::optional
<MD5::MD5Result
>
3647 DwarfDebug::getMD5AsBytes(const DIFile
*File
) const {
3649 if (getDwarfVersion() < 5)
3650 return std::nullopt
;
3651 std::optional
<DIFile::ChecksumInfo
<StringRef
>> Checksum
= File
->getChecksum();
3652 if (!Checksum
|| Checksum
->Kind
!= DIFile::CSK_MD5
)
3653 return std::nullopt
;
3655 // Convert the string checksum to an MD5Result for the streamer.
3656 // The verifier validates the checksum so we assume it's okay.
3657 // An MD5 checksum is 16 bytes.
3658 std::string ChecksumString
= fromHex(Checksum
->Value
);
3659 MD5::MD5Result CKMem
;
3660 std::copy(ChecksumString
.begin(), ChecksumString
.end(), CKMem
.data());
3664 bool DwarfDebug::alwaysUseRanges(const DwarfCompileUnit
&CU
) const {
3665 if (MinimizeAddr
== MinimizeAddrInV5::Ranges
)
3667 if (MinimizeAddr
!= MinimizeAddrInV5::Default
)
3669 if (useSplitDwarf())