1 //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf debug info into asm files.
12 //===----------------------------------------------------------------------===//
14 #include "DwarfDebug.h"
15 #include "ByteStreamer.h"
17 #include "DebugLocEntry.h"
18 #include "DebugLocStream.h"
19 #include "DwarfAccelTable.h"
20 #include "DwarfCompileUnit.h"
21 #include "DwarfExpression.h"
22 #include "DwarfFile.h"
23 #include "DwarfUnit.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/DenseSet.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/Triple.h"
32 #include "llvm/ADT/Twine.h"
33 #include "llvm/BinaryFormat/Dwarf.h"
34 #include "llvm/CodeGen/AsmPrinter.h"
35 #include "llvm/CodeGen/DIE.h"
36 #include "llvm/CodeGen/LexicalScopes.h"
37 #include "llvm/CodeGen/MachineBasicBlock.h"
38 #include "llvm/CodeGen/MachineFunction.h"
39 #include "llvm/CodeGen/MachineInstr.h"
40 #include "llvm/CodeGen/MachineModuleInfo.h"
41 #include "llvm/CodeGen/MachineOperand.h"
42 #include "llvm/IR/Constants.h"
43 #include "llvm/IR/DebugInfoMetadata.h"
44 #include "llvm/IR/DebugLoc.h"
45 #include "llvm/IR/Function.h"
46 #include "llvm/IR/GlobalVariable.h"
47 #include "llvm/IR/Module.h"
48 #include "llvm/MC/MCAsmInfo.h"
49 #include "llvm/MC/MCContext.h"
50 #include "llvm/MC/MCDwarf.h"
51 #include "llvm/MC/MCSection.h"
52 #include "llvm/MC/MCStreamer.h"
53 #include "llvm/MC/MCSymbol.h"
54 #include "llvm/MC/MCTargetOptions.h"
55 #include "llvm/MC/MachineLocation.h"
56 #include "llvm/MC/SectionKind.h"
57 #include "llvm/Pass.h"
58 #include "llvm/Support/Casting.h"
59 #include "llvm/Support/CommandLine.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/ErrorHandling.h"
62 #include "llvm/Support/MD5.h"
63 #include "llvm/Support/MathExtras.h"
64 #include "llvm/Support/Timer.h"
65 #include "llvm/Support/raw_ostream.h"
66 #include "llvm/Target/TargetLoweringObjectFile.h"
67 #include "llvm/Target/TargetMachine.h"
68 #include "llvm/Target/TargetOptions.h"
69 #include "llvm/Target/TargetRegisterInfo.h"
70 #include "llvm/Target/TargetSubtargetInfo.h"
82 #define DEBUG_TYPE "dwarfdebug"
85 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden
,
86 cl::desc("Disable debug info printing"));
88 static cl::opt
<bool> UseDwarfRangesBaseAddressSpecifier(
89 "use-dwarf-ranges-base-address-specifier", cl::Hidden
,
90 cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
92 static cl::opt
<bool> GenerateARangeSection("generate-arange-section",
94 cl::desc("Generate dwarf aranges"),
97 static cl::opt
<bool> SplitDwarfCrossCuReferences(
98 "split-dwarf-cross-cu-references", cl::Hidden
,
99 cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
101 enum DefaultOnOff
{ Default
, Enable
, Disable
};
103 static cl::opt
<DefaultOnOff
> UnknownLocations(
104 "use-unknown-locations", cl::Hidden
,
105 cl::desc("Make an absence of debug location information explicit."),
106 cl::values(clEnumVal(Default
, "At top of block or after label"),
107 clEnumVal(Enable
, "In all cases"), clEnumVal(Disable
, "Never")),
110 static cl::opt
<DefaultOnOff
>
111 DwarfAccelTables("dwarf-accel-tables", cl::Hidden
,
112 cl::desc("Output prototype dwarf accelerator tables."),
113 cl::values(clEnumVal(Default
, "Default for platform"),
114 clEnumVal(Enable
, "Enabled"),
115 clEnumVal(Disable
, "Disabled")),
118 enum LinkageNameOption
{
124 static cl::opt
<LinkageNameOption
>
125 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden
,
126 cl::desc("Which DWARF linkage-name attributes to emit."),
127 cl::values(clEnumValN(DefaultLinkageNames
, "Default",
128 "Default for platform"),
129 clEnumValN(AllLinkageNames
, "All", "All"),
130 clEnumValN(AbstractLinkageNames
, "Abstract",
131 "Abstract subprograms")),
132 cl::init(DefaultLinkageNames
));
134 static const char *const DWARFGroupName
= "dwarf";
135 static const char *const DWARFGroupDescription
= "DWARF Emission";
136 static const char *const DbgTimerName
= "writer";
137 static const char *const DbgTimerDescription
= "DWARF Debug Writer";
139 void DebugLocDwarfExpression::emitOp(uint8_t Op
, const char *Comment
) {
141 Op
, Comment
? Twine(Comment
) + " " + dwarf::OperationEncodingString(Op
)
142 : dwarf::OperationEncodingString(Op
));
145 void DebugLocDwarfExpression::emitSigned(int64_t Value
) {
146 BS
.EmitSLEB128(Value
, Twine(Value
));
149 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value
) {
150 BS
.EmitULEB128(Value
, Twine(Value
));
153 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo
&TRI
,
154 unsigned MachineReg
) {
155 // This information is not available while emitting .debug_loc entries.
159 bool DbgVariable::isBlockByrefVariable() const {
160 assert(Var
&& "Invalid complex DbgVariable!");
161 return Var
->getType().resolve()->isBlockByrefStruct();
164 const DIType
*DbgVariable::getType() const {
165 DIType
*Ty
= Var
->getType().resolve();
166 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
167 // addresses instead.
168 if (Ty
->isBlockByrefStruct()) {
169 /* Byref variables, in Blocks, are declared by the programmer as
170 "SomeType VarName;", but the compiler creates a
171 __Block_byref_x_VarName struct, and gives the variable VarName
172 either the struct, or a pointer to the struct, as its type. This
173 is necessary for various behind-the-scenes things the compiler
174 needs to do with by-reference variables in blocks.
176 However, as far as the original *programmer* is concerned, the
177 variable should still have type 'SomeType', as originally declared.
179 The following function dives into the __Block_byref_x_VarName
180 struct to find the original type of the variable. This will be
181 passed back to the code generating the type for the Debug
182 Information Entry for the variable 'VarName'. 'VarName' will then
183 have the original type 'SomeType' in its debug information.
185 The original type 'SomeType' will be the type of the field named
186 'VarName' inside the __Block_byref_x_VarName struct.
188 NOTE: In order for this to not completely fail on the debugger
189 side, the Debug Information Entry for the variable VarName needs to
190 have a DW_AT_location that tells the debugger how to unwind through
191 the pointers and __Block_byref_x_VarName struct to find the actual
192 value of the variable. The function addBlockByrefType does this. */
193 DIType
*subType
= Ty
;
194 uint16_t tag
= Ty
->getTag();
196 if (tag
== dwarf::DW_TAG_pointer_type
)
197 subType
= resolve(cast
<DIDerivedType
>(Ty
)->getBaseType());
199 auto Elements
= cast
<DICompositeType
>(subType
)->getElements();
200 for (unsigned i
= 0, N
= Elements
.size(); i
< N
; ++i
) {
201 auto *DT
= cast
<DIDerivedType
>(Elements
[i
]);
202 if (getName() == DT
->getName())
203 return resolve(DT
->getBaseType());
209 ArrayRef
<DbgVariable::FrameIndexExpr
> DbgVariable::getFrameIndexExprs() const {
210 if (FrameIndexExprs
.size() == 1)
211 return FrameIndexExprs
;
213 assert(llvm::all_of(FrameIndexExprs
,
214 [](const FrameIndexExpr
&A
) {
215 return A
.Expr
->isFragment();
217 "multiple FI expressions without DW_OP_LLVM_fragment");
218 std::sort(FrameIndexExprs
.begin(), FrameIndexExprs
.end(),
219 [](const FrameIndexExpr
&A
, const FrameIndexExpr
&B
) -> bool {
220 return A
.Expr
->getFragmentInfo()->OffsetInBits
<
221 B
.Expr
->getFragmentInfo()->OffsetInBits
;
224 return FrameIndexExprs
;
227 void DbgVariable::addMMIEntry(const DbgVariable
&V
) {
228 assert(DebugLocListIndex
== ~0U && !MInsn
&& "not an MMI entry");
229 assert(V
.DebugLocListIndex
== ~0U && !V
.MInsn
&& "not an MMI entry");
230 assert(V
.Var
== Var
&& "conflicting variable");
231 assert(V
.IA
== IA
&& "conflicting inlined-at location");
233 assert(!FrameIndexExprs
.empty() && "Expected an MMI entry");
234 assert(!V
.FrameIndexExprs
.empty() && "Expected an MMI entry");
236 for (const auto &FIE
: V
.FrameIndexExprs
)
237 // Ignore duplicate entries.
238 if (llvm::none_of(FrameIndexExprs
, [&](const FrameIndexExpr
&Other
) {
239 return FIE
.FI
== Other
.FI
&& FIE
.Expr
== Other
.Expr
;
241 FrameIndexExprs
.push_back(FIE
);
243 assert((FrameIndexExprs
.size() == 1 ||
244 llvm::all_of(FrameIndexExprs
,
245 [](FrameIndexExpr
&FIE
) {
246 return FIE
.Expr
&& FIE
.Expr
->isFragment();
248 "conflicting locations for variable");
251 static const DwarfAccelTable::Atom TypeAtoms
[] = {
252 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset
, dwarf::DW_FORM_data4
),
253 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag
, dwarf::DW_FORM_data2
),
254 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags
, dwarf::DW_FORM_data1
)};
256 DwarfDebug::DwarfDebug(AsmPrinter
*A
, Module
*M
)
257 : DebugHandlerBase(A
), DebugLocs(A
->OutStreamer
->isVerboseAsm()),
258 InfoHolder(A
, "info_string", DIEValueAllocator
),
259 SkeletonHolder(A
, "skel_string", DIEValueAllocator
),
260 IsDarwin(A
->TM
.getTargetTriple().isOSDarwin()),
261 AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset
,
262 dwarf::DW_FORM_data4
)),
263 AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset
,
264 dwarf::DW_FORM_data4
)),
265 AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset
,
266 dwarf::DW_FORM_data4
)),
267 AccelTypes(TypeAtoms
) {
268 const Triple
&TT
= Asm
->TM
.getTargetTriple();
270 // Make sure we know our "debugger tuning." The target option takes
271 // precedence; fall back to triple-based defaults.
272 if (Asm
->TM
.Options
.DebuggerTuning
!= DebuggerKind::Default
)
273 DebuggerTuning
= Asm
->TM
.Options
.DebuggerTuning
;
275 DebuggerTuning
= DebuggerKind::LLDB
;
276 else if (TT
.isPS4CPU())
277 DebuggerTuning
= DebuggerKind::SCE
;
279 DebuggerTuning
= DebuggerKind::GDB
;
281 // Turn on accelerator tables for LLDB by default.
282 if (DwarfAccelTables
== Default
)
283 HasDwarfAccelTables
= tuneForLLDB();
285 HasDwarfAccelTables
= DwarfAccelTables
== Enable
;
287 HasAppleExtensionAttributes
= tuneForLLDB();
289 // Handle split DWARF.
290 HasSplitDwarf
= !Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
.empty();
292 // SCE defaults to linkage names only for abstract subprograms.
293 if (DwarfLinkageNames
== DefaultLinkageNames
)
294 UseAllLinkageNames
= !tuneForSCE();
296 UseAllLinkageNames
= DwarfLinkageNames
== AllLinkageNames
;
298 unsigned DwarfVersionNumber
= Asm
->TM
.Options
.MCOptions
.DwarfVersion
;
299 unsigned DwarfVersion
= DwarfVersionNumber
? DwarfVersionNumber
300 : MMI
->getModule()->getDwarfVersion();
301 // Use dwarf 4 by default if nothing is requested.
302 DwarfVersion
= DwarfVersion
? DwarfVersion
: dwarf::DWARF_VERSION
;
304 // Work around a GDB bug. GDB doesn't support the standard opcode;
305 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
306 // is defined as of DWARF 3.
307 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
308 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
309 UseGNUTLSOpcode
= tuneForGDB() || DwarfVersion
< 3;
311 // GDB does not fully support the DWARF 4 representation for bitfields.
312 UseDWARF2Bitfields
= (DwarfVersion
< 4) || tuneForGDB();
314 Asm
->OutStreamer
->getContext().setDwarfVersion(DwarfVersion
);
317 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
318 DwarfDebug::~DwarfDebug() = default;
320 static bool isObjCClass(StringRef Name
) {
321 return Name
.startswith("+") || Name
.startswith("-");
324 static bool hasObjCCategory(StringRef Name
) {
325 if (!isObjCClass(Name
))
328 return Name
.find(") ") != StringRef::npos
;
331 static void getObjCClassCategory(StringRef In
, StringRef
&Class
,
332 StringRef
&Category
) {
333 if (!hasObjCCategory(In
)) {
334 Class
= In
.slice(In
.find('[') + 1, In
.find(' '));
339 Class
= In
.slice(In
.find('[') + 1, In
.find('('));
340 Category
= In
.slice(In
.find('[') + 1, In
.find(' '));
343 static StringRef
getObjCMethodName(StringRef In
) {
344 return In
.slice(In
.find(' ') + 1, In
.find(']'));
347 // Add the various names to the Dwarf accelerator table names.
348 // TODO: Determine whether or not we should add names for programs
349 // that do not have a DW_AT_name or DW_AT_linkage_name field - this
350 // is only slightly different than the lookup of non-standard ObjC names.
351 void DwarfDebug::addSubprogramNames(const DISubprogram
*SP
, DIE
&Die
) {
352 if (!SP
->isDefinition())
354 addAccelName(SP
->getName(), Die
);
356 // If the linkage name is different than the name, go ahead and output
357 // that as well into the name table.
358 if (SP
->getLinkageName() != "" && SP
->getName() != SP
->getLinkageName())
359 addAccelName(SP
->getLinkageName(), Die
);
361 // If this is an Objective-C selector name add it to the ObjC accelerator
363 if (isObjCClass(SP
->getName())) {
364 StringRef Class
, Category
;
365 getObjCClassCategory(SP
->getName(), Class
, Category
);
366 addAccelObjC(Class
, Die
);
368 addAccelObjC(Category
, Die
);
369 // Also add the base method name to the name table.
370 addAccelName(getObjCMethodName(SP
->getName()), Die
);
374 /// Check whether we should create a DIE for the given Scope, return true
375 /// if we don't create a DIE (the corresponding DIE is null).
376 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope
*Scope
) {
377 if (Scope
->isAbstractScope())
380 // We don't create a DIE if there is no Range.
381 const SmallVectorImpl
<InsnRange
> &Ranges
= Scope
->getRanges();
385 if (Ranges
.size() > 1)
388 // We don't create a DIE if we have a single Range and the end label
390 return !getLabelAfterInsn(Ranges
.front().second
);
393 template <typename Func
> static void forBothCUs(DwarfCompileUnit
&CU
, Func F
) {
395 if (auto *SkelCU
= CU
.getSkeleton())
396 if (CU
.getCUNode()->getSplitDebugInlining())
400 bool DwarfDebug::shareAcrossDWOCUs() const {
401 return SplitDwarfCrossCuReferences
;
404 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit
&SrcCU
,
405 LexicalScope
*Scope
) {
406 assert(Scope
&& Scope
->getScopeNode());
407 assert(Scope
->isAbstractScope());
408 assert(!Scope
->getInlinedAt());
410 auto *SP
= cast
<DISubprogram
>(Scope
->getScopeNode());
412 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
413 // was inlined from another compile unit.
414 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP
->getUnit()->getSplitDebugInlining())
415 // Avoid building the original CU if it won't be used
416 SrcCU
.constructAbstractSubprogramScopeDIE(Scope
);
418 auto &CU
= getOrCreateDwarfCompileUnit(SP
->getUnit());
419 if (auto *SkelCU
= CU
.getSkeleton()) {
420 (shareAcrossDWOCUs() ? CU
: SrcCU
)
421 .constructAbstractSubprogramScopeDIE(Scope
);
422 if (CU
.getCUNode()->getSplitDebugInlining())
423 SkelCU
->constructAbstractSubprogramScopeDIE(Scope
);
425 CU
.constructAbstractSubprogramScopeDIE(Scope
);
429 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit
&U
, DIE
&D
) const {
430 if (!U
.hasDwarfPubSections())
433 U
.addFlag(D
, dwarf::DW_AT_GNU_pubnames
);
436 // Create new DwarfCompileUnit for the given metadata node with tag
437 // DW_TAG_compile_unit.
439 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit
*DIUnit
) {
440 if (auto *CU
= CUMap
.lookup(DIUnit
))
442 StringRef FN
= DIUnit
->getFilename();
443 CompilationDir
= DIUnit
->getDirectory();
445 auto OwnedUnit
= llvm::make_unique
<DwarfCompileUnit
>(
446 InfoHolder
.getUnits().size(), DIUnit
, Asm
, this, &InfoHolder
);
447 DwarfCompileUnit
&NewCU
= *OwnedUnit
;
448 DIE
&Die
= NewCU
.getUnitDie();
449 InfoHolder
.addUnit(std::move(OwnedUnit
));
450 if (useSplitDwarf()) {
451 NewCU
.setSkeleton(constructSkeletonCU(NewCU
));
452 NewCU
.addString(Die
, dwarf::DW_AT_GNU_dwo_name
,
453 Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
);
456 for (auto *IE
: DIUnit
->getImportedEntities())
457 NewCU
.addImportedEntity(IE
);
459 // LTO with assembly output shares a single line table amongst multiple CUs.
460 // To avoid the compilation directory being ambiguous, let the line table
461 // explicitly describe the directory of all files, never relying on the
462 // compilation directory.
463 if (!Asm
->OutStreamer
->hasRawTextSupport() || SingleCU
)
464 Asm
->OutStreamer
->getContext().setMCLineTableCompilationDir(
465 NewCU
.getUniqueID(), CompilationDir
);
467 StringRef Producer
= DIUnit
->getProducer();
468 StringRef Flags
= DIUnit
->getFlags();
469 if (!Flags
.empty()) {
470 std::string ProducerWithFlags
= Producer
.str() + " " + Flags
.str();
471 NewCU
.addString(Die
, dwarf::DW_AT_producer
, ProducerWithFlags
);
473 NewCU
.addString(Die
, dwarf::DW_AT_producer
, Producer
);
475 NewCU
.addUInt(Die
, dwarf::DW_AT_language
, dwarf::DW_FORM_data2
,
476 DIUnit
->getSourceLanguage());
477 NewCU
.addString(Die
, dwarf::DW_AT_name
, FN
);
479 if (!useSplitDwarf()) {
480 NewCU
.initStmtList();
482 // If we're using split dwarf the compilation dir is going to be in the
483 // skeleton CU and so we don't need to duplicate it here.
484 if (!CompilationDir
.empty())
485 NewCU
.addString(Die
, dwarf::DW_AT_comp_dir
, CompilationDir
);
487 addGnuPubAttributes(NewCU
, Die
);
490 if (useAppleExtensionAttributes()) {
491 if (DIUnit
->isOptimized())
492 NewCU
.addFlag(Die
, dwarf::DW_AT_APPLE_optimized
);
494 StringRef Flags
= DIUnit
->getFlags();
496 NewCU
.addString(Die
, dwarf::DW_AT_APPLE_flags
, Flags
);
498 if (unsigned RVer
= DIUnit
->getRuntimeVersion())
499 NewCU
.addUInt(Die
, dwarf::DW_AT_APPLE_major_runtime_vers
,
500 dwarf::DW_FORM_data1
, RVer
);
504 NewCU
.setSection(Asm
->getObjFileLowering().getDwarfInfoDWOSection());
506 NewCU
.setSection(Asm
->getObjFileLowering().getDwarfInfoSection());
508 if (DIUnit
->getDWOId()) {
509 // This CU is either a clang module DWO or a skeleton CU.
510 NewCU
.addUInt(Die
, dwarf::DW_AT_GNU_dwo_id
, dwarf::DW_FORM_data8
,
512 if (!DIUnit
->getSplitDebugFilename().empty())
513 // This is a prefabricated skeleton CU.
514 NewCU
.addString(Die
, dwarf::DW_AT_GNU_dwo_name
,
515 DIUnit
->getSplitDebugFilename());
518 CUMap
.insert({DIUnit
, &NewCU
});
519 CUDieMap
.insert({&Die
, &NewCU
});
523 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit
&TheCU
,
524 const DIImportedEntity
*N
) {
525 if (isa
<DILocalScope
>(N
->getScope()))
527 if (DIE
*D
= TheCU
.getOrCreateContextDIE(N
->getScope()))
528 D
->addChild(TheCU
.constructImportedEntityDIE(N
));
531 /// Sort and unique GVEs by comparing their fragment offset.
532 static SmallVectorImpl
<DwarfCompileUnit::GlobalExpr
> &
533 sortGlobalExprs(SmallVectorImpl
<DwarfCompileUnit::GlobalExpr
> &GVEs
) {
534 std::sort(GVEs
.begin(), GVEs
.end(),
535 [](DwarfCompileUnit::GlobalExpr A
, DwarfCompileUnit::GlobalExpr B
) {
536 if (A
.Expr
!= B
.Expr
&& A
.Expr
&& B
.Expr
) {
537 auto FragmentA
= A
.Expr
->getFragmentInfo();
538 auto FragmentB
= B
.Expr
->getFragmentInfo();
539 if (FragmentA
&& FragmentB
)
540 return FragmentA
->OffsetInBits
< FragmentB
->OffsetInBits
;
544 GVEs
.erase(std::unique(GVEs
.begin(), GVEs
.end(),
545 [](DwarfCompileUnit::GlobalExpr A
,
546 DwarfCompileUnit::GlobalExpr B
) {
547 return A
.Expr
== B
.Expr
;
553 // Emit all Dwarf sections that should come prior to the content. Create
554 // global DIEs and emit initial debug info sections. This is invoked by
555 // the target AsmPrinter.
556 void DwarfDebug::beginModule() {
557 NamedRegionTimer
T(DbgTimerName
, DbgTimerDescription
, DWARFGroupName
,
558 DWARFGroupDescription
, TimePassesIsEnabled
);
559 if (DisableDebugInfoPrinting
)
562 const Module
*M
= MMI
->getModule();
564 unsigned NumDebugCUs
= std::distance(M
->debug_compile_units_begin(),
565 M
->debug_compile_units_end());
566 // Tell MMI whether we have debug info.
567 MMI
->setDebugInfoAvailability(NumDebugCUs
> 0);
568 SingleCU
= NumDebugCUs
== 1;
569 DenseMap
<DIGlobalVariable
*, SmallVector
<DwarfCompileUnit::GlobalExpr
, 1>>
571 for (const GlobalVariable
&Global
: M
->globals()) {
572 SmallVector
<DIGlobalVariableExpression
*, 1> GVs
;
573 Global
.getDebugInfo(GVs
);
574 for (auto *GVE
: GVs
)
575 GVMap
[GVE
->getVariable()].push_back({&Global
, GVE
->getExpression()});
578 for (DICompileUnit
*CUNode
: M
->debug_compile_units()) {
579 // FIXME: Move local imported entities into a list attached to the
580 // subprogram, then this search won't be needed and a
581 // getImportedEntities().empty() test should go below with the rest.
582 bool HasNonLocalImportedEntities
= llvm::any_of(
583 CUNode
->getImportedEntities(), [](const DIImportedEntity
*IE
) {
584 return !isa
<DILocalScope
>(IE
->getScope());
587 if (!HasNonLocalImportedEntities
&& CUNode
->getEnumTypes().empty() &&
588 CUNode
->getRetainedTypes().empty() &&
589 CUNode
->getGlobalVariables().empty() && CUNode
->getMacros().empty())
592 DwarfCompileUnit
&CU
= getOrCreateDwarfCompileUnit(CUNode
);
595 for (auto *GVE
: CUNode
->getGlobalVariables()) {
596 // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
597 // already know about the variable and it isn't adding a constant
599 auto &GVMapEntry
= GVMap
[GVE
->getVariable()];
600 auto *Expr
= GVE
->getExpression();
601 if (!GVMapEntry
.size() || (Expr
&& Expr
->isConstant()))
602 GVMapEntry
.push_back({nullptr, Expr
});
604 DenseSet
<DIGlobalVariable
*> Processed
;
605 for (auto *GVE
: CUNode
->getGlobalVariables()) {
606 DIGlobalVariable
*GV
= GVE
->getVariable();
607 if (Processed
.insert(GV
).second
)
608 CU
.getOrCreateGlobalVariableDIE(GV
, sortGlobalExprs(GVMap
[GV
]));
611 for (auto *Ty
: CUNode
->getEnumTypes()) {
612 // The enum types array by design contains pointers to
613 // MDNodes rather than DIRefs. Unique them here.
614 CU
.getOrCreateTypeDIE(cast
<DIType
>(Ty
));
616 for (auto *Ty
: CUNode
->getRetainedTypes()) {
617 // The retained types array by design contains pointers to
618 // MDNodes rather than DIRefs. Unique them here.
619 if (DIType
*RT
= dyn_cast
<DIType
>(Ty
))
620 // There is no point in force-emitting a forward declaration.
621 CU
.getOrCreateTypeDIE(RT
);
623 // Emit imported_modules last so that the relevant context is already
625 for (auto *IE
: CUNode
->getImportedEntities())
626 constructAndAddImportedEntityDIE(CU
, IE
);
630 void DwarfDebug::finishVariableDefinitions() {
631 for (const auto &Var
: ConcreteVariables
) {
632 DIE
*VariableDie
= Var
->getDIE();
634 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
635 // in the ConcreteVariables list, rather than looking it up again here.
636 // DIE::getUnit isn't simple - it walks parent pointers, etc.
637 DwarfCompileUnit
*Unit
= CUDieMap
.lookup(VariableDie
->getUnitDie());
639 Unit
->finishVariableDefinition(*Var
);
643 void DwarfDebug::finishSubprogramDefinitions() {
644 for (const DISubprogram
*SP
: ProcessedSPNodes
) {
645 assert(SP
->getUnit()->getEmissionKind() != DICompileUnit::NoDebug
);
647 getOrCreateDwarfCompileUnit(SP
->getUnit()),
648 [&](DwarfCompileUnit
&CU
) { CU
.finishSubprogramDefinition(SP
); });
652 void DwarfDebug::finalizeModuleInfo() {
653 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
655 finishSubprogramDefinitions();
657 finishVariableDefinitions();
659 // Include the DWO file name in the hash if there's more than one CU.
660 // This handles ThinLTO's situation where imported CUs may very easily be
661 // duplicate with the same CU partially imported into another ThinLTO unit.
663 if (CUMap
.size() > 1)
664 DWOName
= Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
;
666 // Handle anything that needs to be done on a per-unit basis after
667 // all other generation.
668 for (const auto &P
: CUMap
) {
669 auto &TheCU
= *P
.second
;
670 // Emit DW_AT_containing_type attribute to connect types with their
671 // vtable holding type.
672 TheCU
.constructContainingTypeDIEs();
674 // Add CU specific attributes if we need to add any.
675 // If we're splitting the dwarf out now that we've got the entire
676 // CU then add the dwo id to it.
677 auto *SkCU
= TheCU
.getSkeleton();
678 if (useSplitDwarf()) {
679 // Emit a unique identifier for this CU.
681 DIEHash(Asm
).computeCUSignature(DWOName
, TheCU
.getUnitDie());
682 TheCU
.addUInt(TheCU
.getUnitDie(), dwarf::DW_AT_GNU_dwo_id
,
683 dwarf::DW_FORM_data8
, ID
);
684 SkCU
->addUInt(SkCU
->getUnitDie(), dwarf::DW_AT_GNU_dwo_id
,
685 dwarf::DW_FORM_data8
, ID
);
687 // We don't keep track of which addresses are used in which CU so this
688 // is a bit pessimistic under LTO.
689 if (!AddrPool
.isEmpty()) {
690 const MCSymbol
*Sym
= TLOF
.getDwarfAddrSection()->getBeginSymbol();
691 SkCU
->addSectionLabel(SkCU
->getUnitDie(), dwarf::DW_AT_GNU_addr_base
,
694 if (!SkCU
->getRangeLists().empty()) {
695 const MCSymbol
*Sym
= TLOF
.getDwarfRangesSection()->getBeginSymbol();
696 SkCU
->addSectionLabel(SkCU
->getUnitDie(), dwarf::DW_AT_GNU_ranges_base
,
701 // If we have code split among multiple sections or non-contiguous
702 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
703 // remain in the .o file, otherwise add a DW_AT_low_pc.
704 // FIXME: We should use ranges allow reordering of code ala
705 // .subsections_via_symbols in mach-o. This would mean turning on
706 // ranges for all subprogram DIEs for mach-o.
707 DwarfCompileUnit
&U
= SkCU
? *SkCU
: TheCU
;
708 if (unsigned NumRanges
= TheCU
.getRanges().size()) {
710 // A DW_AT_low_pc attribute may also be specified in combination with
711 // DW_AT_ranges to specify the default base address for use in
712 // location lists (see Section 2.6.2) and range lists (see Section
714 U
.addUInt(U
.getUnitDie(), dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
, 0);
716 U
.setBaseAddress(TheCU
.getRanges().front().getStart());
717 U
.attachRangesOrLowHighPC(U
.getUnitDie(), TheCU
.takeRanges());
720 auto *CUNode
= cast
<DICompileUnit
>(P
.first
);
721 // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
722 if (CUNode
->getMacros())
723 U
.addSectionLabel(U
.getUnitDie(), dwarf::DW_AT_macro_info
,
724 U
.getMacroLabelBegin(),
725 TLOF
.getDwarfMacinfoSection()->getBeginSymbol());
728 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
729 for (auto *CUNode
: MMI
->getModule()->debug_compile_units())
730 if (CUNode
->getDWOId())
731 getOrCreateDwarfCompileUnit(CUNode
);
733 // Compute DIE offsets and sizes.
734 InfoHolder
.computeSizeAndOffsets();
736 SkeletonHolder
.computeSizeAndOffsets();
739 // Emit all Dwarf sections that should come after the content.
740 void DwarfDebug::endModule() {
741 assert(CurFn
== nullptr);
742 assert(CurMI
== nullptr);
744 // If we aren't actually generating debug info (check beginModule -
745 // conditionalized on !DisableDebugInfoPrinting and the presence of the
746 // llvm.dbg.cu metadata node)
747 if (!MMI
->hasDebugInfo())
750 // Finalize the debug info for the module.
751 finalizeModuleInfo();
758 // Emit info into a debug loc section.
761 // Corresponding abbreviations into a abbrev section.
764 // Emit all the DIEs into a debug info section.
767 // Emit info into a debug aranges section.
768 if (GenerateARangeSection
)
771 // Emit info into a debug ranges section.
774 // Emit info into a debug macinfo section.
777 if (useSplitDwarf()) {
780 emitDebugAbbrevDWO();
782 // Emit DWO addresses.
783 AddrPool
.emit(*Asm
, Asm
->getObjFileLowering().getDwarfAddrSection());
786 // Emit info into the dwarf accelerator table sections.
787 if (useDwarfAccelTables()) {
790 emitAccelNamespaces();
794 // Emit the pubnames and pubtypes sections if requested.
795 emitDebugPubSections();
798 // FIXME: AbstractVariables.clear();
801 void DwarfDebug::ensureAbstractVariableIsCreated(DwarfCompileUnit
&CU
, InlinedVariable IV
,
802 const MDNode
*ScopeNode
) {
803 const DILocalVariable
*Cleansed
= nullptr;
804 if (CU
.getExistingAbstractVariable(IV
, Cleansed
))
807 CU
.createAbstractVariable(Cleansed
, LScopes
.getOrCreateAbstractScope(
808 cast
<DILocalScope
>(ScopeNode
)));
811 void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit
&CU
,
812 InlinedVariable IV
, const MDNode
*ScopeNode
) {
813 const DILocalVariable
*Cleansed
= nullptr;
814 if (CU
.getExistingAbstractVariable(IV
, Cleansed
))
817 if (LexicalScope
*Scope
=
818 LScopes
.findAbstractScope(cast_or_null
<DILocalScope
>(ScopeNode
)))
819 CU
.createAbstractVariable(Cleansed
, Scope
);
822 // Collect variable information from side table maintained by MF.
823 void DwarfDebug::collectVariableInfoFromMFTable(
824 DwarfCompileUnit
&TheCU
, DenseSet
<InlinedVariable
> &Processed
) {
825 SmallDenseMap
<InlinedVariable
, DbgVariable
*> MFVars
;
826 for (const auto &VI
: Asm
->MF
->getVariableDbgInfo()) {
829 assert(VI
.Var
->isValidLocationForIntrinsic(VI
.Loc
) &&
830 "Expected inlined-at fields to agree");
832 InlinedVariable
Var(VI
.Var
, VI
.Loc
->getInlinedAt());
833 Processed
.insert(Var
);
834 LexicalScope
*Scope
= LScopes
.findLexicalScope(VI
.Loc
);
836 // If variable scope is not found then skip this variable.
840 ensureAbstractVariableIsCreatedIfScoped(TheCU
, Var
, Scope
->getScopeNode());
841 auto RegVar
= llvm::make_unique
<DbgVariable
>(Var
.first
, Var
.second
);
842 RegVar
->initializeMMI(VI
.Expr
, VI
.Slot
);
843 if (DbgVariable
*DbgVar
= MFVars
.lookup(Var
))
844 DbgVar
->addMMIEntry(*RegVar
);
845 else if (InfoHolder
.addScopeVariable(Scope
, RegVar
.get())) {
846 MFVars
.insert({Var
, RegVar
.get()});
847 ConcreteVariables
.push_back(std::move(RegVar
));
852 // Get .debug_loc entry for the instruction range starting at MI.
853 static DebugLocEntry::Value
getDebugLocValue(const MachineInstr
*MI
) {
854 const DIExpression
*Expr
= MI
->getDebugExpression();
855 assert(MI
->getNumOperands() == 4);
856 if (MI
->getOperand(0).isReg()) {
857 auto RegOp
= MI
->getOperand(0);
858 auto Op1
= MI
->getOperand(1);
859 // If the second operand is an immediate, this is a
860 // register-indirect address.
861 assert((!Op1
.isImm() || (Op1
.getImm() == 0)) && "unexpected offset");
862 MachineLocation
MLoc(RegOp
.getReg(), Op1
.isImm());
863 return DebugLocEntry::Value(Expr
, MLoc
);
865 if (MI
->getOperand(0).isImm())
866 return DebugLocEntry::Value(Expr
, MI
->getOperand(0).getImm());
867 if (MI
->getOperand(0).isFPImm())
868 return DebugLocEntry::Value(Expr
, MI
->getOperand(0).getFPImm());
869 if (MI
->getOperand(0).isCImm())
870 return DebugLocEntry::Value(Expr
, MI
->getOperand(0).getCImm());
872 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
875 /// \brief If this and Next are describing different fragments of the same
876 /// variable, merge them by appending Next's values to the current
878 /// Return true if the merge was successful.
879 bool DebugLocEntry::MergeValues(const DebugLocEntry
&Next
) {
880 if (Begin
== Next
.Begin
) {
881 auto *FirstExpr
= cast
<DIExpression
>(Values
[0].Expression
);
882 auto *FirstNextExpr
= cast
<DIExpression
>(Next
.Values
[0].Expression
);
883 if (!FirstExpr
->isFragment() || !FirstNextExpr
->isFragment())
886 // We can only merge entries if none of the fragments overlap any others.
887 // In doing so, we can take advantage of the fact that both lists are
889 for (unsigned i
= 0, j
= 0; i
< Values
.size(); ++i
) {
890 for (; j
< Next
.Values
.size(); ++j
) {
891 int res
= DebugHandlerBase::fragmentCmp(
892 cast
<DIExpression
>(Values
[i
].Expression
),
893 cast
<DIExpression
>(Next
.Values
[j
].Expression
));
894 if (res
== 0) // The two expressions overlap, we can't merge.
896 // Values[i] is entirely before Next.Values[j],
897 // so go back to the next entry of Values.
900 // Next.Values[j] is entirely before Values[i], so go on to the
901 // next entry of Next.Values.
905 addValues(Next
.Values
);
912 /// Build the location list for all DBG_VALUEs in the function that
913 /// describe the same variable. If the ranges of several independent
914 /// fragments of the same variable overlap partially, split them up and
915 /// combine the ranges. The resulting DebugLocEntries are will have
916 /// strict monotonically increasing begin addresses and will never
921 // Ranges History [var, loc, fragment ofs size]
922 // 0 | [x, (reg0, fragment 0, 32)]
923 // 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry
925 // 3 | [clobber reg0]
926 // 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of
931 // [0-1] [x, (reg0, fragment 0, 32)]
932 // [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)]
933 // [3-4] [x, (reg1, fragment 32, 32)]
934 // [4- ] [x, (mem, fragment 0, 64)]
936 DwarfDebug::buildLocationList(SmallVectorImpl
<DebugLocEntry
> &DebugLoc
,
937 const DbgValueHistoryMap::InstrRanges
&Ranges
) {
938 SmallVector
<DebugLocEntry::Value
, 4> OpenRanges
;
940 for (auto I
= Ranges
.begin(), E
= Ranges
.end(); I
!= E
; ++I
) {
941 const MachineInstr
*Begin
= I
->first
;
942 const MachineInstr
*End
= I
->second
;
943 assert(Begin
->isDebugValue() && "Invalid History entry");
945 // Check if a variable is inaccessible in this range.
946 if (Begin
->getNumOperands() > 1 &&
947 Begin
->getOperand(0).isReg() && !Begin
->getOperand(0).getReg()) {
952 // If this fragment overlaps with any open ranges, truncate them.
953 const DIExpression
*DIExpr
= Begin
->getDebugExpression();
954 auto Last
= remove_if(OpenRanges
, [&](DebugLocEntry::Value R
) {
955 return fragmentsOverlap(DIExpr
, R
.getExpression());
957 OpenRanges
.erase(Last
, OpenRanges
.end());
959 const MCSymbol
*StartLabel
= getLabelBeforeInsn(Begin
);
960 assert(StartLabel
&& "Forgot label before DBG_VALUE starting a range!");
962 const MCSymbol
*EndLabel
;
964 EndLabel
= getLabelAfterInsn(End
);
965 else if (std::next(I
) == Ranges
.end())
966 EndLabel
= Asm
->getFunctionEnd();
968 EndLabel
= getLabelBeforeInsn(std::next(I
)->first
);
969 assert(EndLabel
&& "Forgot label after instruction ending a range!");
971 DEBUG(dbgs() << "DotDebugLoc: " << *Begin
<< "\n");
973 auto Value
= getDebugLocValue(Begin
);
974 DebugLocEntry
Loc(StartLabel
, EndLabel
, Value
);
975 bool couldMerge
= false;
977 // If this is a fragment, it may belong to the current DebugLocEntry.
978 if (DIExpr
->isFragment()) {
979 // Add this value to the list of open ranges.
980 OpenRanges
.push_back(Value
);
982 // Attempt to add the fragment to the last entry.
983 if (!DebugLoc
.empty())
984 if (DebugLoc
.back().MergeValues(Loc
))
989 // Need to add a new DebugLocEntry. Add all values from still
990 // valid non-overlapping fragments.
991 if (OpenRanges
.size())
992 Loc
.addValues(OpenRanges
);
994 DebugLoc
.push_back(std::move(Loc
));
997 // Attempt to coalesce the ranges of two otherwise identical
999 auto CurEntry
= DebugLoc
.rbegin();
1001 dbgs() << CurEntry
->getValues().size() << " Values:\n";
1002 for (auto &Value
: CurEntry
->getValues())
1004 dbgs() << "-----\n";
1007 auto PrevEntry
= std::next(CurEntry
);
1008 if (PrevEntry
!= DebugLoc
.rend() && PrevEntry
->MergeRanges(*CurEntry
))
1009 DebugLoc
.pop_back();
1013 DbgVariable
*DwarfDebug::createConcreteVariable(DwarfCompileUnit
&TheCU
,
1014 LexicalScope
&Scope
,
1015 InlinedVariable IV
) {
1016 ensureAbstractVariableIsCreatedIfScoped(TheCU
, IV
, Scope
.getScopeNode());
1017 ConcreteVariables
.push_back(
1018 llvm::make_unique
<DbgVariable
>(IV
.first
, IV
.second
));
1019 InfoHolder
.addScopeVariable(&Scope
, ConcreteVariables
.back().get());
1020 return ConcreteVariables
.back().get();
1023 /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1024 /// enclosing lexical scope. The check ensures there are no other instructions
1025 /// in the same lexical scope preceding the DBG_VALUE and that its range is
1026 /// either open or otherwise rolls off the end of the scope.
1027 static bool validThroughout(LexicalScopes
&LScopes
,
1028 const MachineInstr
*DbgValue
,
1029 const MachineInstr
*RangeEnd
) {
1030 assert(DbgValue
->getDebugLoc() && "DBG_VALUE without a debug location");
1031 auto MBB
= DbgValue
->getParent();
1032 auto DL
= DbgValue
->getDebugLoc();
1033 auto *LScope
= LScopes
.findLexicalScope(DL
);
1034 // Scope doesn't exist; this is a dead DBG_VALUE.
1037 auto &LSRange
= LScope
->getRanges();
1038 if (LSRange
.size() == 0)
1041 // Determine if the DBG_VALUE is valid at the beginning of its lexical block.
1042 const MachineInstr
*LScopeBegin
= LSRange
.front().first
;
1043 // Early exit if the lexical scope begins outside of the current block.
1044 if (LScopeBegin
->getParent() != MBB
)
1046 MachineBasicBlock::const_reverse_iterator
Pred(DbgValue
);
1047 for (++Pred
; Pred
!= MBB
->rend(); ++Pred
) {
1048 if (Pred
->getFlag(MachineInstr::FrameSetup
))
1050 auto PredDL
= Pred
->getDebugLoc();
1051 if (!PredDL
|| Pred
->isMetaInstruction())
1053 // Check whether the instruction preceding the DBG_VALUE is in the same
1054 // (sub)scope as the DBG_VALUE.
1055 if (DL
->getScope() == PredDL
->getScope())
1057 auto *PredScope
= LScopes
.findLexicalScope(PredDL
);
1058 if (!PredScope
|| LScope
->dominates(PredScope
))
1062 // If the range of the DBG_VALUE is open-ended, report success.
1066 // Fail if there are instructions belonging to our scope in another block.
1067 const MachineInstr
*LScopeEnd
= LSRange
.back().second
;
1068 if (LScopeEnd
->getParent() != MBB
)
1071 // Single, constant DBG_VALUEs in the prologue are promoted to be live
1072 // throughout the function. This is a hack, presumably for DWARF v2 and not
1073 // necessarily correct. It would be much better to use a dbg.declare instead
1074 // if we know the constant is live throughout the scope.
1075 if (DbgValue
->getOperand(0).isImm() && MBB
->pred_empty())
1081 // Find variables for each lexical scope.
1082 void DwarfDebug::collectVariableInfo(DwarfCompileUnit
&TheCU
,
1083 const DISubprogram
*SP
,
1084 DenseSet
<InlinedVariable
> &Processed
) {
1085 // Grab the variable info that was squirreled away in the MMI side-table.
1086 collectVariableInfoFromMFTable(TheCU
, Processed
);
1088 for (const auto &I
: DbgValues
) {
1089 InlinedVariable IV
= I
.first
;
1090 if (Processed
.count(IV
))
1093 // Instruction ranges, specifying where IV is accessible.
1094 const auto &Ranges
= I
.second
;
1098 LexicalScope
*Scope
= nullptr;
1099 if (const DILocation
*IA
= IV
.second
)
1100 Scope
= LScopes
.findInlinedScope(IV
.first
->getScope(), IA
);
1102 Scope
= LScopes
.findLexicalScope(IV
.first
->getScope());
1103 // If variable scope is not found then skip this variable.
1107 Processed
.insert(IV
);
1108 DbgVariable
*RegVar
= createConcreteVariable(TheCU
, *Scope
, IV
);
1110 const MachineInstr
*MInsn
= Ranges
.front().first
;
1111 assert(MInsn
->isDebugValue() && "History must begin with debug value");
1113 // Check if there is a single DBG_VALUE, valid throughout the var's scope.
1114 if (Ranges
.size() == 1 &&
1115 validThroughout(LScopes
, MInsn
, Ranges
.front().second
)) {
1116 RegVar
->initializeDbgValue(MInsn
);
1120 // Handle multiple DBG_VALUE instructions describing one variable.
1121 DebugLocStream::ListBuilder
List(DebugLocs
, TheCU
, *Asm
, *RegVar
, *MInsn
);
1123 // Build the location list for this variable.
1124 SmallVector
<DebugLocEntry
, 8> Entries
;
1125 buildLocationList(Entries
, Ranges
);
1127 // If the variable has a DIBasicType, extract it. Basic types cannot have
1128 // unique identifiers, so don't bother resolving the type with the
1130 const DIBasicType
*BT
= dyn_cast
<DIBasicType
>(
1131 static_cast<const Metadata
*>(IV
.first
->getType()));
1133 // Finalize the entry by lowering it into a DWARF bytestream.
1134 for (auto &Entry
: Entries
)
1135 Entry
.finalize(*Asm
, List
, BT
);
1138 // Collect info for variables that were optimized out.
1139 for (const DILocalVariable
*DV
: SP
->getVariables()) {
1140 if (Processed
.insert(InlinedVariable(DV
, nullptr)).second
)
1141 if (LexicalScope
*Scope
= LScopes
.findLexicalScope(DV
->getScope()))
1142 createConcreteVariable(TheCU
, *Scope
, InlinedVariable(DV
, nullptr));
1146 // Process beginning of an instruction.
1147 void DwarfDebug::beginInstruction(const MachineInstr
*MI
) {
1148 DebugHandlerBase::beginInstruction(MI
);
1151 const auto *SP
= MI
->getParent()->getParent()->getFunction()->getSubprogram();
1152 if (!SP
|| SP
->getUnit()->getEmissionKind() == DICompileUnit::NoDebug
)
1155 // Check if source location changes, but ignore DBG_VALUE and CFI locations.
1156 if (MI
->isMetaInstruction())
1158 const DebugLoc
&DL
= MI
->getDebugLoc();
1159 // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1160 // the last line number actually emitted, to see if it was line 0.
1161 unsigned LastAsmLine
=
1162 Asm
->OutStreamer
->getContext().getCurrentDwarfLoc().getLine();
1164 if (DL
== PrevInstLoc
) {
1165 // If we have an ongoing unspecified location, nothing to do here.
1168 // We have an explicit location, same as the previous location.
1169 // But we might be coming back to it after a line 0 record.
1170 if (LastAsmLine
== 0 && DL
.getLine() != 0) {
1171 // Reinstate the source location but not marked as a statement.
1172 const MDNode
*Scope
= DL
.getScope();
1173 recordSourceLine(DL
.getLine(), DL
.getCol(), Scope
, /*Flags=*/0);
1179 // We have an unspecified location, which might want to be line 0.
1180 // If we have already emitted a line-0 record, don't repeat it.
1181 if (LastAsmLine
== 0)
1183 // If user said Don't Do That, don't do that.
1184 if (UnknownLocations
== Disable
)
1186 // See if we have a reason to emit a line-0 record now.
1187 // Reasons to emit a line-0 record include:
1188 // - User asked for it (UnknownLocations).
1189 // - Instruction has a label, so it's referenced from somewhere else,
1190 // possibly debug information; we want it to have a source location.
1191 // - Instruction is at the top of a block; we don't want to inherit the
1192 // location from the physically previous (maybe unrelated) block.
1193 if (UnknownLocations
== Enable
|| PrevLabel
||
1194 (PrevInstBB
&& PrevInstBB
!= MI
->getParent())) {
1195 // Preserve the file and column numbers, if we can, to save space in
1196 // the encoded line table.
1197 // Do not update PrevInstLoc, it remembers the last non-0 line.
1198 const MDNode
*Scope
= nullptr;
1199 unsigned Column
= 0;
1201 Scope
= PrevInstLoc
.getScope();
1202 Column
= PrevInstLoc
.getCol();
1204 recordSourceLine(/*Line=*/0, Column
, Scope
, /*Flags=*/0);
1209 // We have an explicit location, different from the previous location.
1210 // Don't repeat a line-0 record, but otherwise emit the new location.
1211 // (The new location might be an explicit line 0, which we do emit.)
1212 if (PrevInstLoc
&& DL
.getLine() == 0 && LastAsmLine
== 0)
1215 if (DL
== PrologEndLoc
) {
1216 Flags
|= DWARF2_FLAG_PROLOGUE_END
| DWARF2_FLAG_IS_STMT
;
1217 PrologEndLoc
= DebugLoc();
1219 // If the line changed, we call that a new statement; unless we went to
1220 // line 0 and came back, in which case it is not a new statement.
1221 unsigned OldLine
= PrevInstLoc
? PrevInstLoc
.getLine() : LastAsmLine
;
1222 if (DL
.getLine() && DL
.getLine() != OldLine
)
1223 Flags
|= DWARF2_FLAG_IS_STMT
;
1225 const MDNode
*Scope
= DL
.getScope();
1226 recordSourceLine(DL
.getLine(), DL
.getCol(), Scope
, Flags
);
1228 // If we're not at line 0, remember this location.
1233 static DebugLoc
findPrologueEndLoc(const MachineFunction
*MF
) {
1234 // First known non-DBG_VALUE and non-frame setup location marks
1235 // the beginning of the function body.
1236 for (const auto &MBB
: *MF
)
1237 for (const auto &MI
: MBB
)
1238 if (!MI
.isMetaInstruction() && !MI
.getFlag(MachineInstr::FrameSetup
) &&
1240 return MI
.getDebugLoc();
1244 // Gather pre-function debug information. Assumes being called immediately
1245 // after the function entry point has been emitted.
1246 void DwarfDebug::beginFunctionImpl(const MachineFunction
*MF
) {
1249 auto *SP
= MF
->getFunction()->getSubprogram();
1250 assert(LScopes
.empty() || SP
== LScopes
.getCurrentFunctionScope()->getScopeNode());
1251 if (SP
->getUnit()->getEmissionKind() == DICompileUnit::NoDebug
)
1254 DwarfCompileUnit
&CU
= getOrCreateDwarfCompileUnit(SP
->getUnit());
1256 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1257 // belongs to so that we add to the correct per-cu line table in the
1259 if (Asm
->OutStreamer
->hasRawTextSupport())
1260 // Use a single line table if we are generating assembly.
1261 Asm
->OutStreamer
->getContext().setDwarfCompileUnitID(0);
1263 Asm
->OutStreamer
->getContext().setDwarfCompileUnitID(CU
.getUniqueID());
1265 // Record beginning of function.
1266 PrologEndLoc
= findPrologueEndLoc(MF
);
1268 // We'd like to list the prologue as "not statements" but GDB behaves
1269 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1270 auto *SP
= PrologEndLoc
->getInlinedAtScope()->getSubprogram();
1271 recordSourceLine(SP
->getScopeLine(), 0, SP
, DWARF2_FLAG_IS_STMT
);
1275 void DwarfDebug::skippedNonDebugFunction() {
1276 // If we don't have a subprogram for this function then there will be a hole
1277 // in the range information. Keep note of this by setting the previously used
1278 // section to nullptr.
1283 // Gather and emit post-function debug information.
1284 void DwarfDebug::endFunctionImpl(const MachineFunction
*MF
) {
1285 const DISubprogram
*SP
= MF
->getFunction()->getSubprogram();
1287 assert(CurFn
== MF
&&
1288 "endFunction should be called with the same function as beginFunction");
1290 // Set DwarfDwarfCompileUnitID in MCContext to default value.
1291 Asm
->OutStreamer
->getContext().setDwarfCompileUnitID(0);
1293 LexicalScope
*FnScope
= LScopes
.getCurrentFunctionScope();
1294 assert(!FnScope
|| SP
== FnScope
->getScopeNode());
1295 DwarfCompileUnit
&TheCU
= *CUMap
.lookup(SP
->getUnit());
1297 DenseSet
<InlinedVariable
> ProcessedVars
;
1298 collectVariableInfo(TheCU
, SP
, ProcessedVars
);
1300 // Add the range of this function to the list of ranges for the CU.
1301 TheCU
.addRange(RangeSpan(Asm
->getFunctionBegin(), Asm
->getFunctionEnd()));
1303 // Under -gmlt, skip building the subprogram if there are no inlined
1304 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
1305 // is still needed as we need its source location.
1306 if (!TheCU
.getCUNode()->getDebugInfoForProfiling() &&
1307 TheCU
.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly
&&
1308 LScopes
.getAbstractScopesList().empty() && !IsDarwin
) {
1309 assert(InfoHolder
.getScopeVariables().empty());
1310 PrevLabel
= nullptr;
1316 size_t NumAbstractScopes
= LScopes
.getAbstractScopesList().size();
1318 // Construct abstract scopes.
1319 for (LexicalScope
*AScope
: LScopes
.getAbstractScopesList()) {
1320 auto *SP
= cast
<DISubprogram
>(AScope
->getScopeNode());
1321 // Collect info for variables that were optimized out.
1322 for (const DILocalVariable
*DV
: SP
->getVariables()) {
1323 if (!ProcessedVars
.insert(InlinedVariable(DV
, nullptr)).second
)
1325 ensureAbstractVariableIsCreated(TheCU
, InlinedVariable(DV
, nullptr),
1327 assert(LScopes
.getAbstractScopesList().size() == NumAbstractScopes
1328 && "ensureAbstractVariableIsCreated inserted abstract scopes");
1330 constructAbstractSubprogramScopeDIE(TheCU
, AScope
);
1333 ProcessedSPNodes
.insert(SP
);
1334 TheCU
.constructSubprogramScopeDIE(SP
, FnScope
);
1335 if (auto *SkelCU
= TheCU
.getSkeleton())
1336 if (!LScopes
.getAbstractScopesList().empty() &&
1337 TheCU
.getCUNode()->getSplitDebugInlining())
1338 SkelCU
->constructSubprogramScopeDIE(SP
, FnScope
);
1341 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1342 // DbgVariables except those that are also in AbstractVariables (since they
1343 // can be used cross-function)
1344 InfoHolder
.getScopeVariables().clear();
1345 PrevLabel
= nullptr;
1349 // Register a source line with debug info. Returns the unique label that was
1350 // emitted and which provides correspondence to the source line list.
1351 void DwarfDebug::recordSourceLine(unsigned Line
, unsigned Col
, const MDNode
*S
,
1356 unsigned Discriminator
= 0;
1357 if (auto *Scope
= cast_or_null
<DIScope
>(S
)) {
1358 Fn
= Scope
->getFilename();
1359 Dir
= Scope
->getDirectory();
1360 if (Line
!= 0 && getDwarfVersion() >= 4)
1361 if (auto *LBF
= dyn_cast
<DILexicalBlockFile
>(Scope
))
1362 Discriminator
= LBF
->getDiscriminator();
1364 unsigned CUID
= Asm
->OutStreamer
->getContext().getDwarfCompileUnitID();
1365 Src
= static_cast<DwarfCompileUnit
&>(*InfoHolder
.getUnits()[CUID
])
1366 .getOrCreateSourceID(Fn
, Dir
);
1368 Asm
->OutStreamer
->EmitDwarfLocDirective(Src
, Line
, Col
, Flags
, 0,
1372 //===----------------------------------------------------------------------===//
1374 //===----------------------------------------------------------------------===//
1376 // Emit the debug info section.
1377 void DwarfDebug::emitDebugInfo() {
1378 DwarfFile
&Holder
= useSplitDwarf() ? SkeletonHolder
: InfoHolder
;
1379 Holder
.emitUnits(/* UseOffsets */ false);
1382 // Emit the abbreviation section.
1383 void DwarfDebug::emitAbbreviations() {
1384 DwarfFile
&Holder
= useSplitDwarf() ? SkeletonHolder
: InfoHolder
;
1386 Holder
.emitAbbrevs(Asm
->getObjFileLowering().getDwarfAbbrevSection());
1389 void DwarfDebug::emitAccel(DwarfAccelTable
&Accel
, MCSection
*Section
,
1390 StringRef TableName
) {
1391 Accel
.FinalizeTable(Asm
, TableName
);
1392 Asm
->OutStreamer
->SwitchSection(Section
);
1394 // Emit the full data.
1395 Accel
.emit(Asm
, Section
->getBeginSymbol(), this);
1398 // Emit visible names into a hashed accelerator table section.
1399 void DwarfDebug::emitAccelNames() {
1400 emitAccel(AccelNames
, Asm
->getObjFileLowering().getDwarfAccelNamesSection(),
1404 // Emit objective C classes and categories into a hashed accelerator table
1406 void DwarfDebug::emitAccelObjC() {
1407 emitAccel(AccelObjC
, Asm
->getObjFileLowering().getDwarfAccelObjCSection(),
1411 // Emit namespace dies into a hashed accelerator table.
1412 void DwarfDebug::emitAccelNamespaces() {
1413 emitAccel(AccelNamespace
,
1414 Asm
->getObjFileLowering().getDwarfAccelNamespaceSection(),
1418 // Emit type dies into a hashed accelerator table.
1419 void DwarfDebug::emitAccelTypes() {
1420 emitAccel(AccelTypes
, Asm
->getObjFileLowering().getDwarfAccelTypesSection(),
1424 // Public name handling.
1425 // The format for the various pubnames:
1427 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1428 // for the DIE that is named.
1430 // gnu pubnames - offset/index value/name tuples where the offset is the offset
1431 // into the CU and the index value is computed according to the type of value
1432 // for the DIE that is named.
1434 // For type units the offset is the offset of the skeleton DIE. For split dwarf
1435 // it's the offset within the debug_info/debug_types dwo section, however, the
1436 // reference in the pubname header doesn't change.
1438 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
1439 static dwarf::PubIndexEntryDescriptor
computeIndexValue(DwarfUnit
*CU
,
1441 // Entities that ended up only in a Type Unit reference the CU instead (since
1442 // the pub entry has offsets within the CU there's no real offset that can be
1443 // provided anyway). As it happens all such entities (namespaces and types,
1444 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
1445 // not to be true it would be necessary to persist this information from the
1446 // point at which the entry is added to the index data structure - since by
1447 // the time the index is built from that, the original type/namespace DIE in a
1448 // type unit has already been destroyed so it can't be queried for properties
1450 if (Die
->getTag() == dwarf::DW_TAG_compile_unit
)
1451 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE
,
1452 dwarf::GIEL_EXTERNAL
);
1453 dwarf::GDBIndexEntryLinkage Linkage
= dwarf::GIEL_STATIC
;
1455 // We could have a specification DIE that has our most of our knowledge,
1456 // look for that now.
1457 if (DIEValue SpecVal
= Die
->findAttribute(dwarf::DW_AT_specification
)) {
1458 DIE
&SpecDIE
= SpecVal
.getDIEEntry().getEntry();
1459 if (SpecDIE
.findAttribute(dwarf::DW_AT_external
))
1460 Linkage
= dwarf::GIEL_EXTERNAL
;
1461 } else if (Die
->findAttribute(dwarf::DW_AT_external
))
1462 Linkage
= dwarf::GIEL_EXTERNAL
;
1464 switch (Die
->getTag()) {
1465 case dwarf::DW_TAG_class_type
:
1466 case dwarf::DW_TAG_structure_type
:
1467 case dwarf::DW_TAG_union_type
:
1468 case dwarf::DW_TAG_enumeration_type
:
1469 return dwarf::PubIndexEntryDescriptor(
1470 dwarf::GIEK_TYPE
, CU
->getLanguage() != dwarf::DW_LANG_C_plus_plus
1471 ? dwarf::GIEL_STATIC
1472 : dwarf::GIEL_EXTERNAL
);
1473 case dwarf::DW_TAG_typedef
:
1474 case dwarf::DW_TAG_base_type
:
1475 case dwarf::DW_TAG_subrange_type
:
1476 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE
, dwarf::GIEL_STATIC
);
1477 case dwarf::DW_TAG_namespace
:
1478 return dwarf::GIEK_TYPE
;
1479 case dwarf::DW_TAG_subprogram
:
1480 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION
, Linkage
);
1481 case dwarf::DW_TAG_variable
:
1482 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE
, Linkage
);
1483 case dwarf::DW_TAG_enumerator
:
1484 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE
,
1485 dwarf::GIEL_STATIC
);
1487 return dwarf::GIEK_NONE
;
1491 /// emitDebugPubSections - Emit visible names and types into debug pubnames and
1492 /// pubtypes sections.
1493 void DwarfDebug::emitDebugPubSections() {
1494 for (const auto &NU
: CUMap
) {
1495 DwarfCompileUnit
*TheU
= NU
.second
;
1496 if (!TheU
->hasDwarfPubSections())
1499 bool GnuStyle
= TheU
->getCUNode()->getGnuPubnames();
1501 Asm
->OutStreamer
->SwitchSection(
1502 GnuStyle
? Asm
->getObjFileLowering().getDwarfGnuPubNamesSection()
1503 : Asm
->getObjFileLowering().getDwarfPubNamesSection());
1504 emitDebugPubSection(GnuStyle
, "Names", TheU
, TheU
->getGlobalNames());
1506 Asm
->OutStreamer
->SwitchSection(
1507 GnuStyle
? Asm
->getObjFileLowering().getDwarfGnuPubTypesSection()
1508 : Asm
->getObjFileLowering().getDwarfPubTypesSection());
1509 emitDebugPubSection(GnuStyle
, "Types", TheU
, TheU
->getGlobalTypes());
1513 void DwarfDebug::emitDebugPubSection(bool GnuStyle
, StringRef Name
,
1514 DwarfCompileUnit
*TheU
,
1515 const StringMap
<const DIE
*> &Globals
) {
1516 if (auto *Skeleton
= TheU
->getSkeleton())
1520 Asm
->OutStreamer
->AddComment("Length of Public " + Name
+ " Info");
1521 MCSymbol
*BeginLabel
= Asm
->createTempSymbol("pub" + Name
+ "_begin");
1522 MCSymbol
*EndLabel
= Asm
->createTempSymbol("pub" + Name
+ "_end");
1523 Asm
->EmitLabelDifference(EndLabel
, BeginLabel
, 4);
1525 Asm
->OutStreamer
->EmitLabel(BeginLabel
);
1527 Asm
->OutStreamer
->AddComment("DWARF Version");
1528 Asm
->EmitInt16(dwarf::DW_PUBNAMES_VERSION
);
1530 Asm
->OutStreamer
->AddComment("Offset of Compilation Unit Info");
1531 Asm
->emitDwarfSymbolReference(TheU
->getLabelBegin());
1533 Asm
->OutStreamer
->AddComment("Compilation Unit Length");
1534 Asm
->EmitInt32(TheU
->getLength());
1536 // Emit the pubnames for this compilation unit.
1537 for (const auto &GI
: Globals
) {
1538 const char *Name
= GI
.getKeyData();
1539 const DIE
*Entity
= GI
.second
;
1541 Asm
->OutStreamer
->AddComment("DIE offset");
1542 Asm
->EmitInt32(Entity
->getOffset());
1545 dwarf::PubIndexEntryDescriptor Desc
= computeIndexValue(TheU
, Entity
);
1546 Asm
->OutStreamer
->AddComment(
1547 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc
.Kind
) + ", " +
1548 dwarf::GDBIndexEntryLinkageString(Desc
.Linkage
));
1549 Asm
->EmitInt8(Desc
.toBits());
1552 Asm
->OutStreamer
->AddComment("External Name");
1553 Asm
->OutStreamer
->EmitBytes(StringRef(Name
, GI
.getKeyLength() + 1));
1556 Asm
->OutStreamer
->AddComment("End Mark");
1558 Asm
->OutStreamer
->EmitLabel(EndLabel
);
1561 /// Emit null-terminated strings into a debug str section.
1562 void DwarfDebug::emitDebugStr() {
1563 DwarfFile
&Holder
= useSplitDwarf() ? SkeletonHolder
: InfoHolder
;
1564 Holder
.emitStrings(Asm
->getObjFileLowering().getDwarfStrSection());
1567 void DwarfDebug::emitDebugLocEntry(ByteStreamer
&Streamer
,
1568 const DebugLocStream::Entry
&Entry
) {
1569 auto &&Comments
= DebugLocs
.getComments(Entry
);
1570 auto Comment
= Comments
.begin();
1571 auto End
= Comments
.end();
1572 for (uint8_t Byte
: DebugLocs
.getBytes(Entry
))
1573 Streamer
.EmitInt8(Byte
, Comment
!= End
? *(Comment
++) : "");
1576 static void emitDebugLocValue(const AsmPrinter
&AP
, const DIBasicType
*BT
,
1577 ByteStreamer
&Streamer
,
1578 const DebugLocEntry::Value
&Value
,
1579 DwarfExpression
&DwarfExpr
) {
1580 auto *DIExpr
= Value
.getExpression();
1581 DIExpressionCursor
ExprCursor(DIExpr
);
1582 DwarfExpr
.addFragmentOffset(DIExpr
);
1584 if (Value
.isInt()) {
1585 if (BT
&& (BT
->getEncoding() == dwarf::DW_ATE_signed
||
1586 BT
->getEncoding() == dwarf::DW_ATE_signed_char
))
1587 DwarfExpr
.addSignedConstant(Value
.getInt());
1589 DwarfExpr
.addUnsignedConstant(Value
.getInt());
1590 } else if (Value
.isLocation()) {
1591 MachineLocation Location
= Value
.getLoc();
1592 if (Location
.isIndirect())
1593 DwarfExpr
.setMemoryLocationKind();
1594 DIExpressionCursor
Cursor(DIExpr
);
1595 const TargetRegisterInfo
&TRI
= *AP
.MF
->getSubtarget().getRegisterInfo();
1596 if (!DwarfExpr
.addMachineRegExpression(TRI
, Cursor
, Location
.getReg()))
1598 return DwarfExpr
.addExpression(std::move(Cursor
));
1599 } else if (Value
.isConstantFP()) {
1600 APInt RawBytes
= Value
.getConstantFP()->getValueAPF().bitcastToAPInt();
1601 DwarfExpr
.addUnsignedConstant(RawBytes
);
1603 DwarfExpr
.addExpression(std::move(ExprCursor
));
1606 void DebugLocEntry::finalize(const AsmPrinter
&AP
,
1607 DebugLocStream::ListBuilder
&List
,
1608 const DIBasicType
*BT
) {
1609 DebugLocStream::EntryBuilder
Entry(List
, Begin
, End
);
1610 BufferByteStreamer Streamer
= Entry
.getStreamer();
1611 DebugLocDwarfExpression
DwarfExpr(AP
.getDwarfVersion(), Streamer
);
1612 const DebugLocEntry::Value
&Value
= Values
[0];
1613 if (Value
.isFragment()) {
1614 // Emit all fragments that belong to the same variable and range.
1615 assert(llvm::all_of(Values
, [](DebugLocEntry::Value P
) {
1616 return P
.isFragment();
1617 }) && "all values are expected to be fragments");
1618 assert(std::is_sorted(Values
.begin(), Values
.end()) &&
1619 "fragments are expected to be sorted");
1621 for (auto Fragment
: Values
)
1622 emitDebugLocValue(AP
, BT
, Streamer
, Fragment
, DwarfExpr
);
1625 assert(Values
.size() == 1 && "only fragments may have >1 value");
1626 emitDebugLocValue(AP
, BT
, Streamer
, Value
, DwarfExpr
);
1628 DwarfExpr
.finalize();
1631 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry
&Entry
) {
1633 Asm
->OutStreamer
->AddComment("Loc expr size");
1634 Asm
->EmitInt16(DebugLocs
.getBytes(Entry
).size());
1637 APByteStreamer
Streamer(*Asm
);
1638 emitDebugLocEntry(Streamer
, Entry
);
1641 // Emit locations into the debug loc section.
1642 void DwarfDebug::emitDebugLoc() {
1643 if (DebugLocs
.getLists().empty())
1646 // Start the dwarf loc section.
1647 Asm
->OutStreamer
->SwitchSection(
1648 Asm
->getObjFileLowering().getDwarfLocSection());
1649 unsigned char Size
= Asm
->MAI
->getCodePointerSize();
1650 for (const auto &List
: DebugLocs
.getLists()) {
1651 Asm
->OutStreamer
->EmitLabel(List
.Label
);
1652 const DwarfCompileUnit
*CU
= List
.CU
;
1653 for (const auto &Entry
: DebugLocs
.getEntries(List
)) {
1654 // Set up the range. This range is relative to the entry point of the
1655 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1656 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
1657 if (auto *Base
= CU
->getBaseAddress()) {
1658 Asm
->EmitLabelDifference(Entry
.BeginSym
, Base
, Size
);
1659 Asm
->EmitLabelDifference(Entry
.EndSym
, Base
, Size
);
1661 Asm
->OutStreamer
->EmitSymbolValue(Entry
.BeginSym
, Size
);
1662 Asm
->OutStreamer
->EmitSymbolValue(Entry
.EndSym
, Size
);
1665 emitDebugLocEntryLocation(Entry
);
1667 Asm
->OutStreamer
->EmitIntValue(0, Size
);
1668 Asm
->OutStreamer
->EmitIntValue(0, Size
);
1672 void DwarfDebug::emitDebugLocDWO() {
1673 Asm
->OutStreamer
->SwitchSection(
1674 Asm
->getObjFileLowering().getDwarfLocDWOSection());
1675 for (const auto &List
: DebugLocs
.getLists()) {
1676 Asm
->OutStreamer
->EmitLabel(List
.Label
);
1677 for (const auto &Entry
: DebugLocs
.getEntries(List
)) {
1678 // Just always use start_length for now - at least that's one address
1679 // rather than two. We could get fancier and try to, say, reuse an
1680 // address we know we've emitted elsewhere (the start of the function?
1681 // The start of the CU or CU subrange that encloses this range?)
1682 Asm
->EmitInt8(dwarf::DW_LLE_startx_length
);
1683 unsigned idx
= AddrPool
.getIndex(Entry
.BeginSym
);
1684 Asm
->EmitULEB128(idx
);
1685 Asm
->EmitLabelDifference(Entry
.EndSym
, Entry
.BeginSym
, 4);
1687 emitDebugLocEntryLocation(Entry
);
1689 Asm
->EmitInt8(dwarf::DW_LLE_end_of_list
);
1694 const MCSymbol
*Start
, *End
;
1697 // Emit a debug aranges section, containing a CU lookup for any
1698 // address we can tie back to a CU.
1699 void DwarfDebug::emitDebugARanges() {
1700 // Provides a unique id per text section.
1701 MapVector
<MCSection
*, SmallVector
<SymbolCU
, 8>> SectionMap
;
1703 // Filter labels by section.
1704 for (const SymbolCU
&SCU
: ArangeLabels
) {
1705 if (SCU
.Sym
->isInSection()) {
1706 // Make a note of this symbol and it's section.
1707 MCSection
*Section
= &SCU
.Sym
->getSection();
1708 if (!Section
->getKind().isMetadata())
1709 SectionMap
[Section
].push_back(SCU
);
1711 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1712 // appear in the output. This sucks as we rely on sections to build
1713 // arange spans. We can do it without, but it's icky.
1714 SectionMap
[nullptr].push_back(SCU
);
1718 DenseMap
<DwarfCompileUnit
*, std::vector
<ArangeSpan
>> Spans
;
1720 for (auto &I
: SectionMap
) {
1721 MCSection
*Section
= I
.first
;
1722 SmallVector
<SymbolCU
, 8> &List
= I
.second
;
1723 if (List
.size() < 1)
1726 // If we have no section (e.g. common), just write out
1727 // individual spans for each symbol.
1729 for (const SymbolCU
&Cur
: List
) {
1731 Span
.Start
= Cur
.Sym
;
1734 Spans
[Cur
.CU
].push_back(Span
);
1739 // Sort the symbols by offset within the section.
1741 List
.begin(), List
.end(), [&](const SymbolCU
&A
, const SymbolCU
&B
) {
1742 unsigned IA
= A
.Sym
? Asm
->OutStreamer
->GetSymbolOrder(A
.Sym
) : 0;
1743 unsigned IB
= B
.Sym
? Asm
->OutStreamer
->GetSymbolOrder(B
.Sym
) : 0;
1745 // Symbols with no order assigned should be placed at the end.
1746 // (e.g. section end labels)
1754 // Insert a final terminator.
1755 List
.push_back(SymbolCU(nullptr, Asm
->OutStreamer
->endSection(Section
)));
1757 // Build spans between each label.
1758 const MCSymbol
*StartSym
= List
[0].Sym
;
1759 for (size_t n
= 1, e
= List
.size(); n
< e
; n
++) {
1760 const SymbolCU
&Prev
= List
[n
- 1];
1761 const SymbolCU
&Cur
= List
[n
];
1763 // Try and build the longest span we can within the same CU.
1764 if (Cur
.CU
!= Prev
.CU
) {
1766 Span
.Start
= StartSym
;
1769 Spans
[Prev
.CU
].push_back(Span
);
1775 // Start the dwarf aranges section.
1776 Asm
->OutStreamer
->SwitchSection(
1777 Asm
->getObjFileLowering().getDwarfARangesSection());
1779 unsigned PtrSize
= Asm
->MAI
->getCodePointerSize();
1781 // Build a list of CUs used.
1782 std::vector
<DwarfCompileUnit
*> CUs
;
1783 for (const auto &it
: Spans
) {
1784 DwarfCompileUnit
*CU
= it
.first
;
1788 // Sort the CU list (again, to ensure consistent output order).
1789 std::sort(CUs
.begin(), CUs
.end(),
1790 [](const DwarfCompileUnit
*A
, const DwarfCompileUnit
*B
) {
1791 return A
->getUniqueID() < B
->getUniqueID();
1794 // Emit an arange table for each CU we used.
1795 for (DwarfCompileUnit
*CU
: CUs
) {
1796 std::vector
<ArangeSpan
> &List
= Spans
[CU
];
1798 // Describe the skeleton CU's offset and length, not the dwo file's.
1799 if (auto *Skel
= CU
->getSkeleton())
1802 // Emit size of content not including length itself.
1803 unsigned ContentSize
=
1804 sizeof(int16_t) + // DWARF ARange version number
1805 sizeof(int32_t) + // Offset of CU in the .debug_info section
1806 sizeof(int8_t) + // Pointer Size (in bytes)
1807 sizeof(int8_t); // Segment Size (in bytes)
1809 unsigned TupleSize
= PtrSize
* 2;
1811 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
1813 OffsetToAlignment(sizeof(int32_t) + ContentSize
, TupleSize
);
1815 ContentSize
+= Padding
;
1816 ContentSize
+= (List
.size() + 1) * TupleSize
;
1818 // For each compile unit, write the list of spans it covers.
1819 Asm
->OutStreamer
->AddComment("Length of ARange Set");
1820 Asm
->EmitInt32(ContentSize
);
1821 Asm
->OutStreamer
->AddComment("DWARF Arange version number");
1822 Asm
->EmitInt16(dwarf::DW_ARANGES_VERSION
);
1823 Asm
->OutStreamer
->AddComment("Offset Into Debug Info Section");
1824 Asm
->emitDwarfSymbolReference(CU
->getLabelBegin());
1825 Asm
->OutStreamer
->AddComment("Address Size (in bytes)");
1826 Asm
->EmitInt8(PtrSize
);
1827 Asm
->OutStreamer
->AddComment("Segment Size (in bytes)");
1830 Asm
->OutStreamer
->emitFill(Padding
, 0xff);
1832 for (const ArangeSpan
&Span
: List
) {
1833 Asm
->EmitLabelReference(Span
.Start
, PtrSize
);
1835 // Calculate the size as being from the span start to it's end.
1837 Asm
->EmitLabelDifference(Span
.End
, Span
.Start
, PtrSize
);
1839 // For symbols without an end marker (e.g. common), we
1840 // write a single arange entry containing just that one symbol.
1841 uint64_t Size
= SymSize
[Span
.Start
];
1845 Asm
->OutStreamer
->EmitIntValue(Size
, PtrSize
);
1849 Asm
->OutStreamer
->AddComment("ARange terminator");
1850 Asm
->OutStreamer
->EmitIntValue(0, PtrSize
);
1851 Asm
->OutStreamer
->EmitIntValue(0, PtrSize
);
1855 /// Emit address ranges into a debug ranges section.
1856 void DwarfDebug::emitDebugRanges() {
1860 // Start the dwarf ranges section.
1861 Asm
->OutStreamer
->SwitchSection(
1862 Asm
->getObjFileLowering().getDwarfRangesSection());
1864 // Size for our labels.
1865 unsigned char Size
= Asm
->MAI
->getCodePointerSize();
1867 // Grab the specific ranges for the compile units in the module.
1868 for (const auto &I
: CUMap
) {
1869 DwarfCompileUnit
*TheCU
= I
.second
;
1871 if (auto *Skel
= TheCU
->getSkeleton())
1874 // Iterate over the misc ranges for the compile units in the module.
1875 for (const RangeSpanList
&List
: TheCU
->getRangeLists()) {
1876 // Emit our symbol so we can find the beginning of the range.
1877 Asm
->OutStreamer
->EmitLabel(List
.getSym());
1879 // Gather all the ranges that apply to the same section so they can share
1880 // a base address entry.
1881 MapVector
<const MCSection
*, std::vector
<const RangeSpan
*>> MV
;
1882 for (const RangeSpan
&Range
: List
.getRanges()) {
1883 MV
[&Range
.getStart()->getSection()].push_back(&Range
);
1886 auto *CUBase
= TheCU
->getBaseAddress();
1887 bool BaseIsSet
= false;
1888 for (const auto &P
: MV
) {
1889 // Don't bother with a base address entry if there's only one range in
1890 // this section in this range list - for example ranges for a CU will
1891 // usually consist of single regions from each of many sections
1892 // (-ffunction-sections, or just C++ inline functions) except under LTO
1893 // or optnone where there may be holes in a single CU's section
1895 auto *Base
= CUBase
;
1896 if (!Base
&& P
.second
.size() > 1 &&
1897 UseDwarfRangesBaseAddressSpecifier
) {
1899 // FIXME/use care: This may not be a useful base address if it's not
1900 // the lowest address/range in this object.
1901 Base
= P
.second
.front()->getStart();
1902 Asm
->OutStreamer
->EmitIntValue(-1, Size
);
1903 Asm
->OutStreamer
->EmitSymbolValue(Base
, Size
);
1904 } else if (BaseIsSet
) {
1906 Asm
->OutStreamer
->EmitIntValue(-1, Size
);
1907 Asm
->OutStreamer
->EmitIntValue(0, Size
);
1910 for (const auto *RS
: P
.second
) {
1911 const MCSymbol
*Begin
= RS
->getStart();
1912 const MCSymbol
*End
= RS
->getEnd();
1913 assert(Begin
&& "Range without a begin symbol?");
1914 assert(End
&& "Range without an end symbol?");
1916 Asm
->EmitLabelDifference(Begin
, Base
, Size
);
1917 Asm
->EmitLabelDifference(End
, Base
, Size
);
1919 Asm
->OutStreamer
->EmitSymbolValue(Begin
, Size
);
1920 Asm
->OutStreamer
->EmitSymbolValue(End
, Size
);
1925 // And terminate the list with two 0 values.
1926 Asm
->OutStreamer
->EmitIntValue(0, Size
);
1927 Asm
->OutStreamer
->EmitIntValue(0, Size
);
1932 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes
, DwarfCompileUnit
&U
) {
1933 for (auto *MN
: Nodes
) {
1934 if (auto *M
= dyn_cast
<DIMacro
>(MN
))
1936 else if (auto *F
= dyn_cast
<DIMacroFile
>(MN
))
1937 emitMacroFile(*F
, U
);
1939 llvm_unreachable("Unexpected DI type!");
1943 void DwarfDebug::emitMacro(DIMacro
&M
) {
1944 Asm
->EmitULEB128(M
.getMacinfoType());
1945 Asm
->EmitULEB128(M
.getLine());
1946 StringRef Name
= M
.getName();
1947 StringRef Value
= M
.getValue();
1948 Asm
->OutStreamer
->EmitBytes(Name
);
1949 if (!Value
.empty()) {
1950 // There should be one space between macro name and macro value.
1952 Asm
->OutStreamer
->EmitBytes(Value
);
1954 Asm
->EmitInt8('\0');
1957 void DwarfDebug::emitMacroFile(DIMacroFile
&F
, DwarfCompileUnit
&U
) {
1958 assert(F
.getMacinfoType() == dwarf::DW_MACINFO_start_file
);
1959 Asm
->EmitULEB128(dwarf::DW_MACINFO_start_file
);
1960 Asm
->EmitULEB128(F
.getLine());
1961 DIFile
*File
= F
.getFile();
1963 U
.getOrCreateSourceID(File
->getFilename(), File
->getDirectory());
1964 Asm
->EmitULEB128(FID
);
1965 handleMacroNodes(F
.getElements(), U
);
1966 Asm
->EmitULEB128(dwarf::DW_MACINFO_end_file
);
1969 /// Emit macros into a debug macinfo section.
1970 void DwarfDebug::emitDebugMacinfo() {
1974 // Start the dwarf macinfo section.
1975 Asm
->OutStreamer
->SwitchSection(
1976 Asm
->getObjFileLowering().getDwarfMacinfoSection());
1978 for (const auto &P
: CUMap
) {
1979 auto &TheCU
= *P
.second
;
1980 auto *SkCU
= TheCU
.getSkeleton();
1981 DwarfCompileUnit
&U
= SkCU
? *SkCU
: TheCU
;
1982 auto *CUNode
= cast
<DICompileUnit
>(P
.first
);
1983 Asm
->OutStreamer
->EmitLabel(U
.getMacroLabelBegin());
1984 handleMacroNodes(CUNode
->getMacros(), U
);
1986 Asm
->OutStreamer
->AddComment("End Of Macro List Mark");
1990 // DWARF5 Experimental Separate Dwarf emitters.
1992 void DwarfDebug::initSkeletonUnit(const DwarfUnit
&U
, DIE
&Die
,
1993 std::unique_ptr
<DwarfCompileUnit
> NewU
) {
1994 NewU
->addString(Die
, dwarf::DW_AT_GNU_dwo_name
,
1995 Asm
->TM
.Options
.MCOptions
.SplitDwarfFile
);
1997 if (!CompilationDir
.empty())
1998 NewU
->addString(Die
, dwarf::DW_AT_comp_dir
, CompilationDir
);
2000 addGnuPubAttributes(*NewU
, Die
);
2002 SkeletonHolder
.addUnit(std::move(NewU
));
2005 // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2006 // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2007 // DW_AT_addr_base, DW_AT_ranges_base.
2008 DwarfCompileUnit
&DwarfDebug::constructSkeletonCU(const DwarfCompileUnit
&CU
) {
2010 auto OwnedUnit
= llvm::make_unique
<DwarfCompileUnit
>(
2011 CU
.getUniqueID(), CU
.getCUNode(), Asm
, this, &SkeletonHolder
);
2012 DwarfCompileUnit
&NewCU
= *OwnedUnit
;
2013 NewCU
.setSection(Asm
->getObjFileLowering().getDwarfInfoSection());
2015 NewCU
.initStmtList();
2017 initSkeletonUnit(CU
, NewCU
.getUnitDie(), std::move(OwnedUnit
));
2022 // Emit the .debug_info.dwo section for separated dwarf. This contains the
2023 // compile units that would normally be in debug_info.
2024 void DwarfDebug::emitDebugInfoDWO() {
2025 assert(useSplitDwarf() && "No split dwarf debug info?");
2026 // Don't emit relocations into the dwo file.
2027 InfoHolder
.emitUnits(/* UseOffsets */ true);
2030 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2031 // abbreviations for the .debug_info.dwo section.
2032 void DwarfDebug::emitDebugAbbrevDWO() {
2033 assert(useSplitDwarf() && "No split dwarf?");
2034 InfoHolder
.emitAbbrevs(Asm
->getObjFileLowering().getDwarfAbbrevDWOSection());
2037 void DwarfDebug::emitDebugLineDWO() {
2038 assert(useSplitDwarf() && "No split dwarf?");
2039 Asm
->OutStreamer
->SwitchSection(
2040 Asm
->getObjFileLowering().getDwarfLineDWOSection());
2041 SplitTypeUnitFileTable
.Emit(*Asm
->OutStreamer
, MCDwarfLineTableParams());
2044 // Emit the .debug_str.dwo section for separated dwarf. This contains the
2045 // string section and is identical in format to traditional .debug_str
2047 void DwarfDebug::emitDebugStrDWO() {
2048 assert(useSplitDwarf() && "No split dwarf?");
2049 MCSection
*OffSec
= Asm
->getObjFileLowering().getDwarfStrOffDWOSection();
2050 InfoHolder
.emitStrings(Asm
->getObjFileLowering().getDwarfStrDWOSection(),
2054 MCDwarfDwoLineTable
*DwarfDebug::getDwoLineTable(const DwarfCompileUnit
&CU
) {
2055 if (!useSplitDwarf())
2058 SplitTypeUnitFileTable
.setCompilationDir(CU
.getCUNode()->getDirectory());
2059 return &SplitTypeUnitFileTable
;
2062 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier
) {
2064 Hash
.update(Identifier
);
2065 // ... take the least significant 8 bytes and return those. Our MD5
2066 // implementation always returns its results in little endian, so we actually
2067 // need the "high" word.
2068 MD5::MD5Result Result
;
2070 return Result
.high();
2073 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit
&CU
,
2074 StringRef Identifier
, DIE
&RefDie
,
2075 const DICompositeType
*CTy
) {
2076 // Fast path if we're building some type units and one has already used the
2077 // address pool we know we're going to throw away all this work anyway, so
2078 // don't bother building dependent types.
2079 if (!TypeUnitsUnderConstruction
.empty() && AddrPool
.hasBeenUsed())
2082 auto Ins
= TypeSignatures
.insert(std::make_pair(CTy
, 0));
2084 CU
.addDIETypeSignature(RefDie
, Ins
.first
->second
);
2088 bool TopLevelType
= TypeUnitsUnderConstruction
.empty();
2089 AddrPool
.resetUsedFlag();
2091 auto OwnedUnit
= llvm::make_unique
<DwarfTypeUnit
>(CU
, Asm
, this, &InfoHolder
,
2092 getDwoLineTable(CU
));
2093 DwarfTypeUnit
&NewTU
= *OwnedUnit
;
2094 DIE
&UnitDie
= NewTU
.getUnitDie();
2095 TypeUnitsUnderConstruction
.emplace_back(std::move(OwnedUnit
), CTy
);
2097 NewTU
.addUInt(UnitDie
, dwarf::DW_AT_language
, dwarf::DW_FORM_data2
,
2100 uint64_t Signature
= makeTypeSignature(Identifier
);
2101 NewTU
.setTypeSignature(Signature
);
2102 Ins
.first
->second
= Signature
;
2104 if (useSplitDwarf())
2105 NewTU
.setSection(Asm
->getObjFileLowering().getDwarfTypesDWOSection());
2107 CU
.applyStmtList(UnitDie
);
2108 NewTU
.setSection(Asm
->getObjFileLowering().getDwarfTypesSection(Signature
));
2111 NewTU
.setType(NewTU
.createTypeDIE(CTy
));
2114 auto TypeUnitsToAdd
= std::move(TypeUnitsUnderConstruction
);
2115 TypeUnitsUnderConstruction
.clear();
2117 // Types referencing entries in the address table cannot be placed in type
2119 if (AddrPool
.hasBeenUsed()) {
2121 // Remove all the types built while building this type.
2122 // This is pessimistic as some of these types might not be dependent on
2123 // the type that used an address.
2124 for (const auto &TU
: TypeUnitsToAdd
)
2125 TypeSignatures
.erase(TU
.second
);
2127 // Construct this type in the CU directly.
2128 // This is inefficient because all the dependent types will be rebuilt
2129 // from scratch, including building them in type units, discovering that
2130 // they depend on addresses, throwing them out and rebuilding them.
2131 CU
.constructTypeDIE(RefDie
, cast
<DICompositeType
>(CTy
));
2135 // If the type wasn't dependent on fission addresses, finish adding the type
2136 // and all its dependent types.
2137 for (auto &TU
: TypeUnitsToAdd
) {
2138 InfoHolder
.computeSizeAndOffsetsForUnit(TU
.first
.get());
2139 InfoHolder
.emitUnit(TU
.first
.get(), useSplitDwarf());
2142 CU
.addDIETypeSignature(RefDie
, Signature
);
2145 // Accelerator table mutators - add each name along with its companion
2146 // DIE to the proper table while ensuring that the name that we're going
2147 // to reference is in the string table. We do this since the names we
2148 // add may not only be identical to the names in the DIE.
2149 void DwarfDebug::addAccelName(StringRef Name
, const DIE
&Die
) {
2150 if (!useDwarfAccelTables())
2152 AccelNames
.AddName(InfoHolder
.getStringPool().getEntry(*Asm
, Name
), &Die
);
2155 void DwarfDebug::addAccelObjC(StringRef Name
, const DIE
&Die
) {
2156 if (!useDwarfAccelTables())
2158 AccelObjC
.AddName(InfoHolder
.getStringPool().getEntry(*Asm
, Name
), &Die
);
2161 void DwarfDebug::addAccelNamespace(StringRef Name
, const DIE
&Die
) {
2162 if (!useDwarfAccelTables())
2164 AccelNamespace
.AddName(InfoHolder
.getStringPool().getEntry(*Asm
, Name
), &Die
);
2167 void DwarfDebug::addAccelType(StringRef Name
, const DIE
&Die
, char Flags
) {
2168 if (!useDwarfAccelTables())
2170 AccelTypes
.AddName(InfoHolder
.getStringPool().getEntry(*Asm
, Name
), &Die
);
2173 uint16_t DwarfDebug::getDwarfVersion() const {
2174 return Asm
->OutStreamer
->getContext().getDwarfVersion();