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 "DwarfCompileUnit.h"
19 #include "DwarfExpression.h"
20 #include "DwarfUnit.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/CodeGen/DIE.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DIBuilder.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/ValueHandle.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCDwarf.h"
37 #include "llvm/MC/MCSection.h"
38 #include "llvm/MC/MCStreamer.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/Dwarf.h"
43 #include "llvm/Support/Endian.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/FormattedStream.h"
46 #include "llvm/Support/LEB128.h"
47 #include "llvm/Support/MD5.h"
48 #include "llvm/Support/Path.h"
49 #include "llvm/Support/Timer.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include "llvm/Target/TargetFrameLowering.h"
52 #include "llvm/Target/TargetLoweringObjectFile.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include "llvm/Target/TargetOptions.h"
55 #include "llvm/Target/TargetRegisterInfo.h"
56 #include "llvm/Target/TargetSubtargetInfo.h"
59 #define DEBUG_TYPE "dwarfdebug"
62 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
63 cl::desc("Disable debug info printing"));
65 static cl::opt<bool> UnknownLocations(
66 "use-unknown-locations", cl::Hidden,
67 cl::desc("Make an absence of debug location information explicit."),
71 GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
72 cl::desc("Generate GNU-style pubnames and pubtypes"),
75 static cl::opt<bool> GenerateARangeSection("generate-arange-section",
77 cl::desc("Generate dwarf aranges"),
81 enum DefaultOnOff { Default, Enable, Disable };
84 static cl::opt<DefaultOnOff>
85 DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
86 cl::desc("Output prototype dwarf accelerator tables."),
87 cl::values(clEnumVal(Default, "Default for platform"),
88 clEnumVal(Enable, "Enabled"),
89 clEnumVal(Disable, "Disabled"), clEnumValEnd),
92 static cl::opt<DefaultOnOff>
93 SplitDwarf("split-dwarf", cl::Hidden,
94 cl::desc("Output DWARF5 split debug info."),
95 cl::values(clEnumVal(Default, "Default for platform"),
96 clEnumVal(Enable, "Enabled"),
97 clEnumVal(Disable, "Disabled"), clEnumValEnd),
100 static cl::opt<DefaultOnOff>
101 DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
102 cl::desc("Generate DWARF pubnames and pubtypes sections"),
103 cl::values(clEnumVal(Default, "Default for platform"),
104 clEnumVal(Enable, "Enabled"),
105 clEnumVal(Disable, "Disabled"), clEnumValEnd),
108 static cl::opt<DefaultOnOff>
109 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
110 cl::desc("Emit DWARF linkage-name attributes."),
111 cl::values(clEnumVal(Default, "Default for platform"),
112 clEnumVal(Enable, "Enabled"),
113 clEnumVal(Disable, "Disabled"), clEnumValEnd),
116 static const char *const DWARFGroupName = "DWARF Emission";
117 static const char *const DbgTimerName = "DWARF Debug Writer";
119 void DebugLocDwarfExpression::EmitOp(uint8_t Op, const char *Comment) {
121 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
122 : dwarf::OperationEncodingString(Op));
125 void DebugLocDwarfExpression::EmitSigned(int64_t Value) {
126 BS.EmitSLEB128(Value, Twine(Value));
129 void DebugLocDwarfExpression::EmitUnsigned(uint64_t Value) {
130 BS.EmitULEB128(Value, Twine(Value));
133 bool DebugLocDwarfExpression::isFrameRegister(unsigned MachineReg) {
134 // This information is not available while emitting .debug_loc entries.
138 //===----------------------------------------------------------------------===//
140 /// resolve - Look in the DwarfDebug map for the MDNode that
141 /// corresponds to the reference.
142 template <typename T> T *DbgVariable::resolve(TypedDINodeRef<T> Ref) const {
143 return DD->resolve(Ref);
146 bool DbgVariable::isBlockByrefVariable() const {
147 assert(Var && "Invalid complex DbgVariable!");
148 return Var->getType()
149 .resolve(DD->getTypeIdentifierMap())
150 ->isBlockByrefStruct();
153 const DIType *DbgVariable::getType() const {
154 DIType *Ty = Var->getType().resolve(DD->getTypeIdentifierMap());
155 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
156 // addresses instead.
157 if (Ty->isBlockByrefStruct()) {
158 /* Byref variables, in Blocks, are declared by the programmer as
159 "SomeType VarName;", but the compiler creates a
160 __Block_byref_x_VarName struct, and gives the variable VarName
161 either the struct, or a pointer to the struct, as its type. This
162 is necessary for various behind-the-scenes things the compiler
163 needs to do with by-reference variables in blocks.
165 However, as far as the original *programmer* is concerned, the
166 variable should still have type 'SomeType', as originally declared.
168 The following function dives into the __Block_byref_x_VarName
169 struct to find the original type of the variable. This will be
170 passed back to the code generating the type for the Debug
171 Information Entry for the variable 'VarName'. 'VarName' will then
172 have the original type 'SomeType' in its debug information.
174 The original type 'SomeType' will be the type of the field named
175 'VarName' inside the __Block_byref_x_VarName struct.
177 NOTE: In order for this to not completely fail on the debugger
178 side, the Debug Information Entry for the variable VarName needs to
179 have a DW_AT_location that tells the debugger how to unwind through
180 the pointers and __Block_byref_x_VarName struct to find the actual
181 value of the variable. The function addBlockByrefType does this. */
182 DIType *subType = Ty;
183 uint16_t tag = Ty->getTag();
185 if (tag == dwarf::DW_TAG_pointer_type)
186 subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
188 auto Elements = cast<DICompositeType>(subType)->getElements();
189 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
190 auto *DT = cast<DIDerivedType>(Elements[i]);
191 if (getName() == DT->getName())
192 return resolve(DT->getBaseType());
198 static LLVM_CONSTEXPR DwarfAccelTable::Atom TypeAtoms[] = {
199 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
200 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
201 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
203 DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
204 : Asm(A), MMI(Asm->MMI), DebugLocs(A->OutStreamer->isVerboseAsm()),
205 PrevLabel(nullptr), InfoHolder(A, "info_string", DIEValueAllocator),
206 SkeletonHolder(A, "skel_string", DIEValueAllocator),
207 IsDarwin(Triple(A->getTargetTriple()).isOSDarwin()),
208 AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
209 dwarf::DW_FORM_data4)),
210 AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
211 dwarf::DW_FORM_data4)),
212 AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
213 dwarf::DW_FORM_data4)),
214 AccelTypes(TypeAtoms), DebuggerTuning(DebuggerKind::Default) {
218 Triple TT(Asm->getTargetTriple());
220 // Make sure we know our "debugger tuning." The target option takes
221 // precedence; fall back to triple-based defaults.
222 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
223 DebuggerTuning = Asm->TM.Options.DebuggerTuning;
224 else if (IsDarwin || TT.isOSFreeBSD())
225 DebuggerTuning = DebuggerKind::LLDB;
226 else if (TT.isPS4CPU())
227 DebuggerTuning = DebuggerKind::SCE;
229 DebuggerTuning = DebuggerKind::GDB;
231 // Turn on accelerator tables for LLDB by default.
232 if (DwarfAccelTables == Default)
233 HasDwarfAccelTables = tuneForLLDB();
235 HasDwarfAccelTables = DwarfAccelTables == Enable;
237 // Handle split DWARF. Off by default for now.
238 if (SplitDwarf == Default)
239 HasSplitDwarf = false;
241 HasSplitDwarf = SplitDwarf == Enable;
243 // Pubnames/pubtypes on by default for GDB.
244 if (DwarfPubSections == Default)
245 HasDwarfPubSections = tuneForGDB();
247 HasDwarfPubSections = DwarfPubSections == Enable;
249 // SCE does not use linkage names.
250 if (DwarfLinkageNames == Default)
251 UseLinkageNames = !tuneForSCE();
253 UseLinkageNames = DwarfLinkageNames == Enable;
255 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
256 DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
257 : MMI->getModule()->getDwarfVersion();
258 // Use dwarf 4 by default if nothing is requested.
259 DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION;
261 // Work around a GDB bug. GDB doesn't support the standard opcode;
262 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
263 // is defined as of DWARF 3.
264 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
265 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
266 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
268 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
271 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
276 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
277 DwarfDebug::~DwarfDebug() { }
279 static bool isObjCClass(StringRef Name) {
280 return Name.startswith("+") || Name.startswith("-");
283 static bool hasObjCCategory(StringRef Name) {
284 if (!isObjCClass(Name))
287 return Name.find(") ") != StringRef::npos;
290 static void getObjCClassCategory(StringRef In, StringRef &Class,
291 StringRef &Category) {
292 if (!hasObjCCategory(In)) {
293 Class = In.slice(In.find('[') + 1, In.find(' '));
298 Class = In.slice(In.find('[') + 1, In.find('('));
299 Category = In.slice(In.find('[') + 1, In.find(' '));
303 static StringRef getObjCMethodName(StringRef In) {
304 return In.slice(In.find(' ') + 1, In.find(']'));
307 // Add the various names to the Dwarf accelerator table names.
308 // TODO: Determine whether or not we should add names for programs
309 // that do not have a DW_AT_name or DW_AT_linkage_name field - this
310 // is only slightly different than the lookup of non-standard ObjC names.
311 void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) {
312 if (!SP->isDefinition())
314 addAccelName(SP->getName(), Die);
316 // If the linkage name is different than the name, go ahead and output
317 // that as well into the name table.
318 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName())
319 addAccelName(SP->getLinkageName(), Die);
321 // If this is an Objective-C selector name add it to the ObjC accelerator
323 if (isObjCClass(SP->getName())) {
324 StringRef Class, Category;
325 getObjCClassCategory(SP->getName(), Class, Category);
326 addAccelObjC(Class, Die);
328 addAccelObjC(Category, Die);
329 // Also add the base method name to the name table.
330 addAccelName(getObjCMethodName(SP->getName()), Die);
334 /// Check whether we should create a DIE for the given Scope, return true
335 /// if we don't create a DIE (the corresponding DIE is null).
336 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
337 if (Scope->isAbstractScope())
340 // We don't create a DIE if there is no Range.
341 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
345 if (Ranges.size() > 1)
348 // We don't create a DIE if we have a single Range and the end label
350 return !getLabelAfterInsn(Ranges.front().second);
353 template <typename Func> void forBothCUs(DwarfCompileUnit &CU, Func F) {
355 if (auto *SkelCU = CU.getSkeleton())
359 void DwarfDebug::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) {
360 assert(Scope && Scope->getScopeNode());
361 assert(Scope->isAbstractScope());
362 assert(!Scope->getInlinedAt());
364 const MDNode *SP = Scope->getScopeNode();
366 ProcessedSPNodes.insert(SP);
368 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
369 // was inlined from another compile unit.
370 auto &CU = SPMap[SP];
371 forBothCUs(*CU, [&](DwarfCompileUnit &CU) {
372 CU.constructAbstractSubprogramScopeDIE(Scope);
376 void DwarfDebug::addGnuPubAttributes(DwarfUnit &U, DIE &D) const {
377 if (!GenerateGnuPubSections)
380 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
383 // Create new DwarfCompileUnit for the given metadata node with tag
384 // DW_TAG_compile_unit.
386 DwarfDebug::constructDwarfCompileUnit(const DICompileUnit *DIUnit) {
387 StringRef FN = DIUnit->getFilename();
388 CompilationDir = DIUnit->getDirectory();
390 auto OwnedUnit = make_unique<DwarfCompileUnit>(
391 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
392 DwarfCompileUnit &NewCU = *OwnedUnit;
393 DIE &Die = NewCU.getUnitDie();
394 InfoHolder.addUnit(std::move(OwnedUnit));
396 NewCU.setSkeleton(constructSkeletonCU(NewCU));
398 // LTO with assembly output shares a single line table amongst multiple CUs.
399 // To avoid the compilation directory being ambiguous, let the line table
400 // explicitly describe the directory of all files, never relying on the
401 // compilation directory.
402 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
403 Asm->OutStreamer->getContext().setMCLineTableCompilationDir(
404 NewCU.getUniqueID(), CompilationDir);
406 NewCU.addString(Die, dwarf::DW_AT_producer, DIUnit->getProducer());
407 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
408 DIUnit->getSourceLanguage());
409 NewCU.addString(Die, dwarf::DW_AT_name, FN);
411 if (!useSplitDwarf()) {
412 NewCU.initStmtList();
414 // If we're using split dwarf the compilation dir is going to be in the
415 // skeleton CU and so we don't need to duplicate it here.
416 if (!CompilationDir.empty())
417 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
419 addGnuPubAttributes(NewCU, Die);
422 if (DIUnit->isOptimized())
423 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
425 StringRef Flags = DIUnit->getFlags();
427 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
429 if (unsigned RVer = DIUnit->getRuntimeVersion())
430 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
431 dwarf::DW_FORM_data1, RVer);
434 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
436 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
438 if (DIUnit->getDWOId()) {
439 // This CU is either a clang module DWO or a skeleton CU.
440 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
442 if (!DIUnit->getSplitDebugFilename().empty())
443 // This is a prefabricated skeleton CU.
444 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
445 DIUnit->getSplitDebugFilename());
448 CUMap.insert(std::make_pair(DIUnit, &NewCU));
449 CUDieMap.insert(std::make_pair(&Die, &NewCU));
453 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
454 const DIImportedEntity *N) {
455 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
456 D->addChild(TheCU.constructImportedEntityDIE(N));
459 // Emit all Dwarf sections that should come prior to the content. Create
460 // global DIEs and emit initial debug info sections. This is invoked by
461 // the target AsmPrinter.
462 void DwarfDebug::beginModule() {
463 if (DisableDebugInfoPrinting)
466 const Module *M = MMI->getModule();
468 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
471 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
473 SingleCU = CU_Nodes->getNumOperands() == 1;
475 for (MDNode *N : CU_Nodes->operands()) {
476 auto *CUNode = cast<DICompileUnit>(N);
477 DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
478 for (auto *IE : CUNode->getImportedEntities())
479 CU.addImportedEntity(IE);
480 for (auto *GV : CUNode->getGlobalVariables())
481 CU.getOrCreateGlobalVariableDIE(GV);
482 for (auto *SP : CUNode->getSubprograms())
483 SPMap.insert(std::make_pair(SP, &CU));
484 for (auto *Ty : CUNode->getEnumTypes()) {
485 // The enum types array by design contains pointers to
486 // MDNodes rather than DIRefs. Unique them here.
487 CU.getOrCreateTypeDIE(cast<DIType>(resolve(Ty->getRef())));
489 for (auto *Ty : CUNode->getRetainedTypes()) {
490 // The retained types array by design contains pointers to
491 // MDNodes rather than DIRefs. Unique them here.
492 DIType *RT = cast<DIType>(resolve(Ty->getRef()));
493 if (!RT->isExternalTypeRef())
494 // There is no point in force-emitting a forward declaration.
495 CU.getOrCreateTypeDIE(RT);
497 // Emit imported_modules last so that the relevant context is already
499 for (auto *IE : CUNode->getImportedEntities())
500 constructAndAddImportedEntityDIE(CU, IE);
503 // Tell MMI that we have debug info.
504 MMI->setDebugInfoAvailability(true);
507 void DwarfDebug::finishVariableDefinitions() {
508 for (const auto &Var : ConcreteVariables) {
509 DIE *VariableDie = Var->getDIE();
511 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
512 // in the ConcreteVariables list, rather than looking it up again here.
513 // DIE::getUnit isn't simple - it walks parent pointers, etc.
514 DwarfCompileUnit *Unit = lookupUnit(VariableDie->getUnit());
516 DbgVariable *AbsVar = getExistingAbstractVariable(
517 InlinedVariable(Var->getVariable(), Var->getInlinedAt()));
518 if (AbsVar && AbsVar->getDIE()) {
519 Unit->addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin,
522 Unit->applyVariableAttributes(*Var, *VariableDie);
526 void DwarfDebug::finishSubprogramDefinitions() {
527 for (const auto &P : SPMap)
528 forBothCUs(*P.second, [&](DwarfCompileUnit &CU) {
529 CU.finishSubprogramDefinition(cast<DISubprogram>(P.first));
534 // Collect info for variables that were optimized out.
535 void DwarfDebug::collectDeadVariables() {
536 const Module *M = MMI->getModule();
538 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
539 for (MDNode *N : CU_Nodes->operands()) {
540 auto *TheCU = cast<DICompileUnit>(N);
541 // Construct subprogram DIE and add variables DIEs.
542 DwarfCompileUnit *SPCU =
543 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
544 assert(SPCU && "Unable to find Compile Unit!");
545 for (auto *SP : TheCU->getSubprograms()) {
546 if (ProcessedSPNodes.count(SP) != 0)
548 SPCU->collectDeadVariables(SP);
554 void DwarfDebug::finalizeModuleInfo() {
555 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
557 finishSubprogramDefinitions();
559 finishVariableDefinitions();
561 // Collect info for variables that were optimized out.
562 collectDeadVariables();
564 // Handle anything that needs to be done on a per-unit basis after
565 // all other generation.
566 for (const auto &P : CUMap) {
567 auto &TheCU = *P.second;
568 // Emit DW_AT_containing_type attribute to connect types with their
569 // vtable holding type.
570 TheCU.constructContainingTypeDIEs();
572 // Add CU specific attributes if we need to add any.
573 // If we're splitting the dwarf out now that we've got the entire
574 // CU then add the dwo id to it.
575 auto *SkCU = TheCU.getSkeleton();
576 if (useSplitDwarf()) {
577 // Emit a unique identifier for this CU.
578 uint64_t ID = DIEHash(Asm).computeCUSignature(TheCU.getUnitDie());
579 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
580 dwarf::DW_FORM_data8, ID);
581 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
582 dwarf::DW_FORM_data8, ID);
584 // We don't keep track of which addresses are used in which CU so this
585 // is a bit pessimistic under LTO.
586 if (!AddrPool.isEmpty()) {
587 const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol();
588 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
591 if (!SkCU->getRangeLists().empty()) {
592 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
593 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
598 // If we have code split among multiple sections or non-contiguous
599 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
600 // remain in the .o file, otherwise add a DW_AT_low_pc.
601 // FIXME: We should use ranges allow reordering of code ala
602 // .subsections_via_symbols in mach-o. This would mean turning on
603 // ranges for all subprogram DIEs for mach-o.
604 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
605 if (unsigned NumRanges = TheCU.getRanges().size()) {
607 // A DW_AT_low_pc attribute may also be specified in combination with
608 // DW_AT_ranges to specify the default base address for use in
609 // location lists (see Section 2.6.2) and range lists (see Section
611 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
613 U.setBaseAddress(TheCU.getRanges().front().getStart());
614 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
618 // Compute DIE offsets and sizes.
619 InfoHolder.computeSizeAndOffsets();
621 SkeletonHolder.computeSizeAndOffsets();
624 // Emit all Dwarf sections that should come after the content.
625 void DwarfDebug::endModule() {
626 assert(CurFn == nullptr);
627 assert(CurMI == nullptr);
629 // If we aren't actually generating debug info (check beginModule -
630 // conditionalized on !DisableDebugInfoPrinting and the presence of the
631 // llvm.dbg.cu metadata node)
632 if (!MMI->hasDebugInfo())
635 // Finalize the debug info for the module.
636 finalizeModuleInfo();
643 // Emit info into a debug loc section.
646 // Corresponding abbreviations into a abbrev section.
649 // Emit all the DIEs into a debug info section.
652 // Emit info into a debug aranges section.
653 if (GenerateARangeSection)
656 // Emit info into a debug ranges section.
659 if (useSplitDwarf()) {
662 emitDebugAbbrevDWO();
664 // Emit DWO addresses.
665 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
668 // Emit info into the dwarf accelerator table sections.
669 if (useDwarfAccelTables()) {
672 emitAccelNamespaces();
676 // Emit the pubnames and pubtypes sections if requested.
677 if (HasDwarfPubSections) {
678 emitDebugPubNames(GenerateGnuPubSections);
679 emitDebugPubTypes(GenerateGnuPubSections);
684 AbstractVariables.clear();
687 // Find abstract variable, if any, associated with Var.
689 DwarfDebug::getExistingAbstractVariable(InlinedVariable IV,
690 const DILocalVariable *&Cleansed) {
691 // More then one inlined variable corresponds to one abstract variable.
693 auto I = AbstractVariables.find(Cleansed);
694 if (I != AbstractVariables.end())
695 return I->second.get();
699 DbgVariable *DwarfDebug::getExistingAbstractVariable(InlinedVariable IV) {
700 const DILocalVariable *Cleansed;
701 return getExistingAbstractVariable(IV, Cleansed);
704 void DwarfDebug::createAbstractVariable(const DILocalVariable *Var,
705 LexicalScope *Scope) {
706 auto AbsDbgVariable = make_unique<DbgVariable>(Var, /* IA */ nullptr, this);
707 InfoHolder.addScopeVariable(Scope, AbsDbgVariable.get());
708 AbstractVariables[Var] = std::move(AbsDbgVariable);
711 void DwarfDebug::ensureAbstractVariableIsCreated(InlinedVariable IV,
712 const MDNode *ScopeNode) {
713 const DILocalVariable *Cleansed = nullptr;
714 if (getExistingAbstractVariable(IV, Cleansed))
717 createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(
718 cast<DILocalScope>(ScopeNode)));
721 void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(
722 InlinedVariable IV, const MDNode *ScopeNode) {
723 const DILocalVariable *Cleansed = nullptr;
724 if (getExistingAbstractVariable(IV, Cleansed))
727 if (LexicalScope *Scope =
728 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
729 createAbstractVariable(Cleansed, Scope);
732 // Collect variable information from side table maintained by MMI.
733 void DwarfDebug::collectVariableInfoFromMMITable(
734 DenseSet<InlinedVariable> &Processed) {
735 for (const auto &VI : MMI->getVariableDbgInfo()) {
738 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
739 "Expected inlined-at fields to agree");
741 InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt());
742 Processed.insert(Var);
743 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
745 // If variable scope is not found then skip this variable.
749 ensureAbstractVariableIsCreatedIfScoped(Var, Scope->getScopeNode());
750 auto RegVar = make_unique<DbgVariable>(Var.first, Var.second, this);
751 RegVar->initializeMMI(VI.Expr, VI.Slot);
752 if (InfoHolder.addScopeVariable(Scope, RegVar.get()))
753 ConcreteVariables.push_back(std::move(RegVar));
757 // Get .debug_loc entry for the instruction range starting at MI.
758 static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
759 const DIExpression *Expr = MI->getDebugExpression();
761 assert(MI->getNumOperands() == 4);
762 if (MI->getOperand(0).isReg()) {
763 MachineLocation MLoc;
764 // If the second operand is an immediate, this is a
765 // register-indirect address.
766 if (!MI->getOperand(1).isImm())
767 MLoc.set(MI->getOperand(0).getReg());
769 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
770 return DebugLocEntry::Value(Expr, MLoc);
772 if (MI->getOperand(0).isImm())
773 return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
774 if (MI->getOperand(0).isFPImm())
775 return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
776 if (MI->getOperand(0).isCImm())
777 return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
779 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
782 /// Determine whether two variable pieces overlap.
783 static bool piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
784 if (!P1->isBitPiece() || !P2->isBitPiece())
786 unsigned l1 = P1->getBitPieceOffset();
787 unsigned l2 = P2->getBitPieceOffset();
788 unsigned r1 = l1 + P1->getBitPieceSize();
789 unsigned r2 = l2 + P2->getBitPieceSize();
790 // True where [l1,r1[ and [r1,r2[ overlap.
791 return (l1 < r2) && (l2 < r1);
794 /// Build the location list for all DBG_VALUEs in the function that
795 /// describe the same variable. If the ranges of several independent
796 /// pieces of the same variable overlap partially, split them up and
797 /// combine the ranges. The resulting DebugLocEntries are will have
798 /// strict monotonically increasing begin addresses and will never
803 // Ranges History [var, loc, piece ofs size]
804 // 0 | [x, (reg0, piece 0, 32)]
805 // 1 | | [x, (reg1, piece 32, 32)] <- IsPieceOfPrevEntry
807 // 3 | [clobber reg0]
808 // 4 [x, (mem, piece 0, 64)] <- overlapping with both previous pieces of
813 // [0-1] [x, (reg0, piece 0, 32)]
814 // [1-3] [x, (reg0, piece 0, 32), (reg1, piece 32, 32)]
815 // [3-4] [x, (reg1, piece 32, 32)]
816 // [4- ] [x, (mem, piece 0, 64)]
818 DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
819 const DbgValueHistoryMap::InstrRanges &Ranges) {
820 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
822 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
823 const MachineInstr *Begin = I->first;
824 const MachineInstr *End = I->second;
825 assert(Begin->isDebugValue() && "Invalid History entry");
827 // Check if a variable is inaccessible in this range.
828 if (Begin->getNumOperands() > 1 &&
829 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
834 // If this piece overlaps with any open ranges, truncate them.
835 const DIExpression *DIExpr = Begin->getDebugExpression();
836 auto Last = std::remove_if(OpenRanges.begin(), OpenRanges.end(),
837 [&](DebugLocEntry::Value R) {
838 return piecesOverlap(DIExpr, R.getExpression());
840 OpenRanges.erase(Last, OpenRanges.end());
842 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
843 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
845 const MCSymbol *EndLabel;
847 EndLabel = getLabelAfterInsn(End);
848 else if (std::next(I) == Ranges.end())
849 EndLabel = Asm->getFunctionEnd();
851 EndLabel = getLabelBeforeInsn(std::next(I)->first);
852 assert(EndLabel && "Forgot label after instruction ending a range!");
854 DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
856 auto Value = getDebugLocValue(Begin);
857 DebugLocEntry Loc(StartLabel, EndLabel, Value);
858 bool couldMerge = false;
860 // If this is a piece, it may belong to the current DebugLocEntry.
861 if (DIExpr->isBitPiece()) {
862 // Add this value to the list of open ranges.
863 OpenRanges.push_back(Value);
865 // Attempt to add the piece to the last entry.
866 if (!DebugLoc.empty())
867 if (DebugLoc.back().MergeValues(Loc))
872 // Need to add a new DebugLocEntry. Add all values from still
873 // valid non-overlapping pieces.
874 if (OpenRanges.size())
875 Loc.addValues(OpenRanges);
877 DebugLoc.push_back(std::move(Loc));
880 // Attempt to coalesce the ranges of two otherwise identical
882 auto CurEntry = DebugLoc.rbegin();
884 dbgs() << CurEntry->getValues().size() << " Values:\n";
885 for (auto &Value : CurEntry->getValues())
886 Value.getExpression()->dump();
890 auto PrevEntry = std::next(CurEntry);
891 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
896 DbgVariable *DwarfDebug::createConcreteVariable(LexicalScope &Scope,
897 InlinedVariable IV) {
898 ensureAbstractVariableIsCreatedIfScoped(IV, Scope.getScopeNode());
899 ConcreteVariables.push_back(
900 make_unique<DbgVariable>(IV.first, IV.second, this));
901 InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get());
902 return ConcreteVariables.back().get();
905 // Find variables for each lexical scope.
906 void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
907 const DISubprogram *SP,
908 DenseSet<InlinedVariable> &Processed) {
909 // Grab the variable info that was squirreled away in the MMI side-table.
910 collectVariableInfoFromMMITable(Processed);
912 for (const auto &I : DbgValues) {
913 InlinedVariable IV = I.first;
914 if (Processed.count(IV))
917 // Instruction ranges, specifying where IV is accessible.
918 const auto &Ranges = I.second;
922 LexicalScope *Scope = nullptr;
923 if (const DILocation *IA = IV.second)
924 Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
926 Scope = LScopes.findLexicalScope(IV.first->getScope());
927 // If variable scope is not found then skip this variable.
931 Processed.insert(IV);
932 DbgVariable *RegVar = createConcreteVariable(*Scope, IV);
934 const MachineInstr *MInsn = Ranges.front().first;
935 assert(MInsn->isDebugValue() && "History must begin with debug value");
937 // Check if the first DBG_VALUE is valid for the rest of the function.
938 if (Ranges.size() == 1 && Ranges.front().second == nullptr) {
939 RegVar->initializeDbgValue(MInsn);
943 // Handle multiple DBG_VALUE instructions describing one variable.
944 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
946 // Build the location list for this variable.
947 SmallVector<DebugLocEntry, 8> Entries;
948 buildLocationList(Entries, Ranges);
950 // If the variable has an DIBasicType, extract it. Basic types cannot have
951 // unique identifiers, so don't bother resolving the type with the
953 const DIBasicType *BT = dyn_cast<DIBasicType>(
954 static_cast<const Metadata *>(IV.first->getType()));
956 // Finalize the entry by lowering it into a DWARF bytestream.
957 for (auto &Entry : Entries)
958 Entry.finalize(*Asm, List, BT);
961 // Collect info for variables that were optimized out.
962 for (const DILocalVariable *DV : SP->getVariables()) {
963 if (Processed.insert(InlinedVariable(DV, nullptr)).second)
964 if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
965 createConcreteVariable(*Scope, InlinedVariable(DV, nullptr));
969 // Return Label preceding the instruction.
970 MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
971 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
972 assert(Label && "Didn't insert label before instruction");
976 // Return Label immediately following the instruction.
977 MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
978 return LabelsAfterInsn.lookup(MI);
981 // Process beginning of an instruction.
982 void DwarfDebug::beginInstruction(const MachineInstr *MI) {
983 assert(CurMI == nullptr);
985 // Check if source location changes, but ignore DBG_VALUE locations.
986 if (!MI->isDebugValue()) {
987 DebugLoc DL = MI->getDebugLoc();
988 if (DL != PrevInstLoc) {
992 if (DL == PrologEndLoc) {
993 Flags |= DWARF2_FLAG_PROLOGUE_END;
994 PrologEndLoc = DebugLoc();
995 Flags |= DWARF2_FLAG_IS_STMT;
998 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine())
999 Flags |= DWARF2_FLAG_IS_STMT;
1001 const MDNode *Scope = DL.getScope();
1002 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
1003 } else if (UnknownLocations) {
1005 recordSourceLine(0, 0, nullptr, 0);
1010 // Insert labels where requested.
1011 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1012 LabelsBeforeInsn.find(MI);
1015 if (I == LabelsBeforeInsn.end())
1018 // Label already assigned.
1023 PrevLabel = MMI->getContext().createTempSymbol();
1024 Asm->OutStreamer->EmitLabel(PrevLabel);
1026 I->second = PrevLabel;
1029 // Process end of an instruction.
1030 void DwarfDebug::endInstruction() {
1031 assert(CurMI != nullptr);
1032 // Don't create a new label after DBG_VALUE instructions.
1033 // They don't generate code.
1034 if (!CurMI->isDebugValue())
1035 PrevLabel = nullptr;
1037 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1038 LabelsAfterInsn.find(CurMI);
1042 if (I == LabelsAfterInsn.end())
1045 // Label already assigned.
1049 // We need a label after this instruction.
1051 PrevLabel = MMI->getContext().createTempSymbol();
1052 Asm->OutStreamer->EmitLabel(PrevLabel);
1054 I->second = PrevLabel;
1057 // Each LexicalScope has first instruction and last instruction to mark
1058 // beginning and end of a scope respectively. Create an inverse map that list
1059 // scopes starts (and ends) with an instruction. One instruction may start (or
1060 // end) multiple scopes. Ignore scopes that are not reachable.
1061 void DwarfDebug::identifyScopeMarkers() {
1062 SmallVector<LexicalScope *, 4> WorkList;
1063 WorkList.push_back(LScopes.getCurrentFunctionScope());
1064 while (!WorkList.empty()) {
1065 LexicalScope *S = WorkList.pop_back_val();
1067 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
1068 if (!Children.empty())
1069 WorkList.append(Children.begin(), Children.end());
1071 if (S->isAbstractScope())
1074 for (const InsnRange &R : S->getRanges()) {
1075 assert(R.first && "InsnRange does not have first instruction!");
1076 assert(R.second && "InsnRange does not have second instruction!");
1077 requestLabelBeforeInsn(R.first);
1078 requestLabelAfterInsn(R.second);
1083 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1084 // First known non-DBG_VALUE and non-frame setup location marks
1085 // the beginning of the function body.
1086 for (const auto &MBB : *MF)
1087 for (const auto &MI : MBB)
1088 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
1090 return MI.getDebugLoc();
1094 // Gather pre-function debug information. Assumes being called immediately
1095 // after the function entry point has been emitted.
1096 void DwarfDebug::beginFunction(const MachineFunction *MF) {
1099 // If there's no debug info for the function we're not going to do anything.
1100 if (!MMI->hasDebugInfo())
1103 auto DI = MF->getFunction()->getSubprogram();
1107 // Grab the lexical scopes for the function, if we don't have any of those
1108 // then we're not going to be able to do anything.
1109 LScopes.initialize(*MF);
1110 if (LScopes.empty())
1113 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
1115 // Make sure that each lexical scope will have a begin/end label.
1116 identifyScopeMarkers();
1118 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1119 // belongs to so that we add to the correct per-cu line table in the
1121 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1122 // FnScope->getScopeNode() and DI->second should represent the same function,
1123 // though they may not be the same MDNode due to inline functions merged in
1124 // LTO where the debug info metadata still differs (either due to distinct
1125 // written differences - two versions of a linkonce_odr function
1126 // written/copied into two separate files, or some sub-optimal metadata that
1127 // isn't structurally identical (see: file path/name info from clang, which
1128 // includes the directory of the cpp file being built, even when the file name
1129 // is absolute (such as an <> lookup header)))
1130 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1131 assert(TheCU && "Unable to find compile unit!");
1132 if (Asm->OutStreamer->hasRawTextSupport())
1133 // Use a single line table if we are generating assembly.
1134 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1136 Asm->OutStreamer->getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
1138 // Calculate history for local variables.
1139 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
1142 // Request labels for the full history.
1143 for (const auto &I : DbgValues) {
1144 const auto &Ranges = I.second;
1148 // The first mention of a function argument gets the CurrentFnBegin
1149 // label, so arguments are visible when breaking at function entry.
1150 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
1151 if (DIVar->isParameter() &&
1152 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
1153 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
1154 if (Ranges.front().first->getDebugExpression()->isBitPiece()) {
1155 // Mark all non-overlapping initial pieces.
1156 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
1157 const DIExpression *Piece = I->first->getDebugExpression();
1158 if (std::all_of(Ranges.begin(), I,
1159 [&](DbgValueHistoryMap::InstrRange Pred) {
1160 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
1162 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
1169 for (const auto &Range : Ranges) {
1170 requestLabelBeforeInsn(Range.first);
1172 requestLabelAfterInsn(Range.second);
1176 PrevInstLoc = DebugLoc();
1177 PrevLabel = Asm->getFunctionBegin();
1179 // Record beginning of function.
1180 PrologEndLoc = findPrologueEndLoc(MF);
1181 if (DILocation *L = PrologEndLoc) {
1182 // We'd like to list the prologue as "not statements" but GDB behaves
1183 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1184 auto *SP = L->getInlinedAtScope()->getSubprogram();
1185 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
1189 // Gather and emit post-function debug information.
1190 void DwarfDebug::endFunction(const MachineFunction *MF) {
1191 assert(CurFn == MF &&
1192 "endFunction should be called with the same function as beginFunction");
1194 if (!MMI->hasDebugInfo() || LScopes.empty() ||
1195 !MF->getFunction()->getSubprogram()) {
1196 // If we don't have a lexical scope for this function then there will
1197 // be a hole in the range information. Keep note of this by setting the
1198 // previously used section to nullptr.
1204 // Set DwarfDwarfCompileUnitID in MCContext to default value.
1205 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1207 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1208 auto *SP = cast<DISubprogram>(FnScope->getScopeNode());
1209 DwarfCompileUnit &TheCU = *SPMap.lookup(SP);
1211 DenseSet<InlinedVariable> ProcessedVars;
1212 collectVariableInfo(TheCU, SP, ProcessedVars);
1214 // Add the range of this function to the list of ranges for the CU.
1215 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
1217 // Under -gmlt, skip building the subprogram if there are no inlined
1218 // subroutines inside it.
1219 if (TheCU.getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly &&
1220 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1221 assert(InfoHolder.getScopeVariables().empty());
1222 assert(DbgValues.empty());
1223 // FIXME: This wouldn't be true in LTO with a -g (with inlining) CU followed
1224 // by a -gmlt CU. Add a test and remove this assertion.
1225 assert(AbstractVariables.empty());
1226 LabelsBeforeInsn.clear();
1227 LabelsAfterInsn.clear();
1228 PrevLabel = nullptr;
1234 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1236 // Construct abstract scopes.
1237 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
1238 auto *SP = cast<DISubprogram>(AScope->getScopeNode());
1239 // Collect info for variables that were optimized out.
1240 for (const DILocalVariable *DV : SP->getVariables()) {
1241 if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
1243 ensureAbstractVariableIsCreated(InlinedVariable(DV, nullptr),
1245 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1246 && "ensureAbstractVariableIsCreated inserted abstract scopes");
1248 constructAbstractSubprogramScopeDIE(AScope);
1251 TheCU.constructSubprogramScopeDIE(FnScope);
1252 if (auto *SkelCU = TheCU.getSkeleton())
1253 if (!LScopes.getAbstractScopesList().empty())
1254 SkelCU->constructSubprogramScopeDIE(FnScope);
1257 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1258 // DbgVariables except those that are also in AbstractVariables (since they
1259 // can be used cross-function)
1260 InfoHolder.getScopeVariables().clear();
1262 LabelsBeforeInsn.clear();
1263 LabelsAfterInsn.clear();
1264 PrevLabel = nullptr;
1268 // Register a source line with debug info. Returns the unique label that was
1269 // emitted and which provides correspondence to the source line list.
1270 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1275 unsigned Discriminator = 0;
1276 if (auto *Scope = cast_or_null<DIScope>(S)) {
1277 Fn = Scope->getFilename();
1278 Dir = Scope->getDirectory();
1279 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1280 Discriminator = LBF->getDiscriminator();
1282 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
1283 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1284 .getOrCreateSourceID(Fn, Dir);
1286 Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1290 //===----------------------------------------------------------------------===//
1292 //===----------------------------------------------------------------------===//
1294 // Emit the debug info section.
1295 void DwarfDebug::emitDebugInfo() {
1296 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1297 Holder.emitUnits(/* UseOffsets */ false);
1300 // Emit the abbreviation section.
1301 void DwarfDebug::emitAbbreviations() {
1302 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1304 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1307 void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section,
1308 StringRef TableName) {
1309 Accel.FinalizeTable(Asm, TableName);
1310 Asm->OutStreamer->SwitchSection(Section);
1312 // Emit the full data.
1313 Accel.emit(Asm, Section->getBeginSymbol(), this);
1316 // Emit visible names into a hashed accelerator table section.
1317 void DwarfDebug::emitAccelNames() {
1318 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1322 // Emit objective C classes and categories into a hashed accelerator table
1324 void DwarfDebug::emitAccelObjC() {
1325 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1329 // Emit namespace dies into a hashed accelerator table.
1330 void DwarfDebug::emitAccelNamespaces() {
1331 emitAccel(AccelNamespace,
1332 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
1336 // Emit type dies into a hashed accelerator table.
1337 void DwarfDebug::emitAccelTypes() {
1338 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1342 // Public name handling.
1343 // The format for the various pubnames:
1345 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1346 // for the DIE that is named.
1348 // gnu pubnames - offset/index value/name tuples where the offset is the offset
1349 // into the CU and the index value is computed according to the type of value
1350 // for the DIE that is named.
1352 // For type units the offset is the offset of the skeleton DIE. For split dwarf
1353 // it's the offset within the debug_info/debug_types dwo section, however, the
1354 // reference in the pubname header doesn't change.
1356 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
1357 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
1359 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1361 // We could have a specification DIE that has our most of our knowledge,
1362 // look for that now.
1363 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1364 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
1365 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
1366 Linkage = dwarf::GIEL_EXTERNAL;
1367 } else if (Die->findAttribute(dwarf::DW_AT_external))
1368 Linkage = dwarf::GIEL_EXTERNAL;
1370 switch (Die->getTag()) {
1371 case dwarf::DW_TAG_class_type:
1372 case dwarf::DW_TAG_structure_type:
1373 case dwarf::DW_TAG_union_type:
1374 case dwarf::DW_TAG_enumeration_type:
1375 return dwarf::PubIndexEntryDescriptor(
1376 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1377 ? dwarf::GIEL_STATIC
1378 : dwarf::GIEL_EXTERNAL);
1379 case dwarf::DW_TAG_typedef:
1380 case dwarf::DW_TAG_base_type:
1381 case dwarf::DW_TAG_subrange_type:
1382 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
1383 case dwarf::DW_TAG_namespace:
1384 return dwarf::GIEK_TYPE;
1385 case dwarf::DW_TAG_subprogram:
1386 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
1387 case dwarf::DW_TAG_variable:
1388 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
1389 case dwarf::DW_TAG_enumerator:
1390 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1391 dwarf::GIEL_STATIC);
1393 return dwarf::GIEK_NONE;
1397 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
1399 void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
1400 MCSection *PSec = GnuStyle
1401 ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1402 : Asm->getObjFileLowering().getDwarfPubNamesSection();
1404 emitDebugPubSection(GnuStyle, PSec, "Names",
1405 &DwarfCompileUnit::getGlobalNames);
1408 void DwarfDebug::emitDebugPubSection(
1409 bool GnuStyle, MCSection *PSec, StringRef Name,
1410 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) {
1411 for (const auto &NU : CUMap) {
1412 DwarfCompileUnit *TheU = NU.second;
1414 const auto &Globals = (TheU->*Accessor)();
1416 if (Globals.empty())
1419 if (auto *Skeleton = TheU->getSkeleton())
1422 // Start the dwarf pubnames section.
1423 Asm->OutStreamer->SwitchSection(PSec);
1426 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1427 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1428 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1429 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1431 Asm->OutStreamer->EmitLabel(BeginLabel);
1433 Asm->OutStreamer->AddComment("DWARF Version");
1434 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
1436 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
1437 Asm->emitDwarfSymbolReference(TheU->getLabelBegin());
1439 Asm->OutStreamer->AddComment("Compilation Unit Length");
1440 Asm->EmitInt32(TheU->getLength());
1442 // Emit the pubnames for this compilation unit.
1443 for (const auto &GI : Globals) {
1444 const char *Name = GI.getKeyData();
1445 const DIE *Entity = GI.second;
1447 Asm->OutStreamer->AddComment("DIE offset");
1448 Asm->EmitInt32(Entity->getOffset());
1451 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
1452 Asm->OutStreamer->AddComment(
1453 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1454 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
1455 Asm->EmitInt8(Desc.toBits());
1458 Asm->OutStreamer->AddComment("External Name");
1459 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1462 Asm->OutStreamer->AddComment("End Mark");
1464 Asm->OutStreamer->EmitLabel(EndLabel);
1468 void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
1469 MCSection *PSec = GnuStyle
1470 ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1471 : Asm->getObjFileLowering().getDwarfPubTypesSection();
1473 emitDebugPubSection(GnuStyle, PSec, "Types",
1474 &DwarfCompileUnit::getGlobalTypes);
1477 // Emit visible names into a debug str section.
1478 void DwarfDebug::emitDebugStr() {
1479 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1480 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1483 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
1484 const DebugLocStream::Entry &Entry) {
1485 auto &&Comments = DebugLocs.getComments(Entry);
1486 auto Comment = Comments.begin();
1487 auto End = Comments.end();
1488 for (uint8_t Byte : DebugLocs.getBytes(Entry))
1489 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
1492 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
1493 ByteStreamer &Streamer,
1494 const DebugLocEntry::Value &Value,
1495 unsigned PieceOffsetInBits) {
1496 DebugLocDwarfExpression DwarfExpr(*AP.MF->getSubtarget().getRegisterInfo(),
1497 AP.getDwarfDebug()->getDwarfVersion(),
1500 if (Value.isInt()) {
1501 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1502 BT->getEncoding() == dwarf::DW_ATE_signed_char))
1503 DwarfExpr.AddSignedConstant(Value.getInt());
1505 DwarfExpr.AddUnsignedConstant(Value.getInt());
1506 } else if (Value.isLocation()) {
1507 MachineLocation Loc = Value.getLoc();
1508 const DIExpression *Expr = Value.getExpression();
1509 if (!Expr || !Expr->getNumElements())
1511 AP.EmitDwarfRegOp(Streamer, Loc);
1513 // Complex address entry.
1514 if (Loc.getOffset()) {
1515 DwarfExpr.AddMachineRegIndirect(Loc.getReg(), Loc.getOffset());
1516 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end(),
1519 DwarfExpr.AddMachineRegExpression(Expr, Loc.getReg(),
1523 // else ... ignore constant fp. There is not any good way to
1524 // to represent them here in dwarf.
1528 void DebugLocEntry::finalize(const AsmPrinter &AP,
1529 DebugLocStream::ListBuilder &List,
1530 const DIBasicType *BT) {
1531 DebugLocStream::EntryBuilder Entry(List, Begin, End);
1532 BufferByteStreamer Streamer = Entry.getStreamer();
1533 const DebugLocEntry::Value &Value = Values[0];
1534 if (Value.isBitPiece()) {
1535 // Emit all pieces that belong to the same variable and range.
1536 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value P) {
1537 return P.isBitPiece();
1538 }) && "all values are expected to be pieces");
1539 assert(std::is_sorted(Values.begin(), Values.end()) &&
1540 "pieces are expected to be sorted");
1542 unsigned Offset = 0;
1543 for (auto Piece : Values) {
1544 const DIExpression *Expr = Piece.getExpression();
1545 unsigned PieceOffset = Expr->getBitPieceOffset();
1546 unsigned PieceSize = Expr->getBitPieceSize();
1547 assert(Offset <= PieceOffset && "overlapping or duplicate pieces");
1548 if (Offset < PieceOffset) {
1549 // The DWARF spec seriously mandates pieces with no locations for gaps.
1550 DebugLocDwarfExpression Expr(*AP.MF->getSubtarget().getRegisterInfo(),
1551 AP.getDwarfDebug()->getDwarfVersion(),
1553 Expr.AddOpPiece(PieceOffset-Offset, 0);
1554 Offset += PieceOffset-Offset;
1556 Offset += PieceSize;
1558 emitDebugLocValue(AP, BT, Streamer, Piece, PieceOffset);
1561 assert(Values.size() == 1 && "only pieces may have >1 value");
1562 emitDebugLocValue(AP, BT, Streamer, Value, 0);
1566 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
1568 Asm->OutStreamer->AddComment("Loc expr size");
1569 Asm->EmitInt16(DebugLocs.getBytes(Entry).size());
1572 APByteStreamer Streamer(*Asm);
1573 emitDebugLocEntry(Streamer, Entry);
1576 // Emit locations into the debug loc section.
1577 void DwarfDebug::emitDebugLoc() {
1578 // Start the dwarf loc section.
1579 Asm->OutStreamer->SwitchSection(
1580 Asm->getObjFileLowering().getDwarfLocSection());
1581 unsigned char Size = Asm->getDataLayout().getPointerSize();
1582 for (const auto &List : DebugLocs.getLists()) {
1583 Asm->OutStreamer->EmitLabel(List.Label);
1584 const DwarfCompileUnit *CU = List.CU;
1585 for (const auto &Entry : DebugLocs.getEntries(List)) {
1586 // Set up the range. This range is relative to the entry point of the
1587 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1588 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
1589 if (auto *Base = CU->getBaseAddress()) {
1590 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1591 Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
1593 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1594 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
1597 emitDebugLocEntryLocation(Entry);
1599 Asm->OutStreamer->EmitIntValue(0, Size);
1600 Asm->OutStreamer->EmitIntValue(0, Size);
1604 void DwarfDebug::emitDebugLocDWO() {
1605 Asm->OutStreamer->SwitchSection(
1606 Asm->getObjFileLowering().getDwarfLocDWOSection());
1607 for (const auto &List : DebugLocs.getLists()) {
1608 Asm->OutStreamer->EmitLabel(List.Label);
1609 for (const auto &Entry : DebugLocs.getEntries(List)) {
1610 // Just always use start_length for now - at least that's one address
1611 // rather than two. We could get fancier and try to, say, reuse an
1612 // address we know we've emitted elsewhere (the start of the function?
1613 // The start of the CU or CU subrange that encloses this range?)
1614 Asm->EmitInt8(dwarf::DW_LLE_start_length_entry);
1615 unsigned idx = AddrPool.getIndex(Entry.BeginSym);
1616 Asm->EmitULEB128(idx);
1617 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
1619 emitDebugLocEntryLocation(Entry);
1621 Asm->EmitInt8(dwarf::DW_LLE_end_of_list_entry);
1626 const MCSymbol *Start, *End;
1629 // Emit a debug aranges section, containing a CU lookup for any
1630 // address we can tie back to a CU.
1631 void DwarfDebug::emitDebugARanges() {
1632 // Provides a unique id per text section.
1633 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
1635 // Filter labels by section.
1636 for (const SymbolCU &SCU : ArangeLabels) {
1637 if (SCU.Sym->isInSection()) {
1638 // Make a note of this symbol and it's section.
1639 MCSection *Section = &SCU.Sym->getSection();
1640 if (!Section->getKind().isMetadata())
1641 SectionMap[Section].push_back(SCU);
1643 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1644 // appear in the output. This sucks as we rely on sections to build
1645 // arange spans. We can do it without, but it's icky.
1646 SectionMap[nullptr].push_back(SCU);
1650 // Add terminating symbols for each section.
1651 for (const auto &I : SectionMap) {
1652 MCSection *Section = I.first;
1653 MCSymbol *Sym = nullptr;
1656 Sym = Asm->OutStreamer->endSection(Section);
1658 // Insert a final terminator.
1659 SectionMap[Section].push_back(SymbolCU(nullptr, Sym));
1662 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
1664 for (auto &I : SectionMap) {
1665 const MCSection *Section = I.first;
1666 SmallVector<SymbolCU, 8> &List = I.second;
1667 if (List.size() < 2)
1670 // If we have no section (e.g. common), just write out
1671 // individual spans for each symbol.
1673 for (const SymbolCU &Cur : List) {
1675 Span.Start = Cur.Sym;
1678 Spans[Cur.CU].push_back(Span);
1683 // Sort the symbols by offset within the section.
1684 std::sort(List.begin(), List.end(),
1685 [&](const SymbolCU &A, const SymbolCU &B) {
1686 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1687 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
1689 // Symbols with no order assigned should be placed at the end.
1690 // (e.g. section end labels)
1698 // Build spans between each label.
1699 const MCSymbol *StartSym = List[0].Sym;
1700 for (size_t n = 1, e = List.size(); n < e; n++) {
1701 const SymbolCU &Prev = List[n - 1];
1702 const SymbolCU &Cur = List[n];
1704 // Try and build the longest span we can within the same CU.
1705 if (Cur.CU != Prev.CU) {
1707 Span.Start = StartSym;
1709 Spans[Prev.CU].push_back(Span);
1715 // Start the dwarf aranges section.
1716 Asm->OutStreamer->SwitchSection(
1717 Asm->getObjFileLowering().getDwarfARangesSection());
1719 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
1721 // Build a list of CUs used.
1722 std::vector<DwarfCompileUnit *> CUs;
1723 for (const auto &it : Spans) {
1724 DwarfCompileUnit *CU = it.first;
1728 // Sort the CU list (again, to ensure consistent output order).
1729 std::sort(CUs.begin(), CUs.end(), [](const DwarfUnit *A, const DwarfUnit *B) {
1730 return A->getUniqueID() < B->getUniqueID();
1733 // Emit an arange table for each CU we used.
1734 for (DwarfCompileUnit *CU : CUs) {
1735 std::vector<ArangeSpan> &List = Spans[CU];
1737 // Describe the skeleton CU's offset and length, not the dwo file's.
1738 if (auto *Skel = CU->getSkeleton())
1741 // Emit size of content not including length itself.
1742 unsigned ContentSize =
1743 sizeof(int16_t) + // DWARF ARange version number
1744 sizeof(int32_t) + // Offset of CU in the .debug_info section
1745 sizeof(int8_t) + // Pointer Size (in bytes)
1746 sizeof(int8_t); // Segment Size (in bytes)
1748 unsigned TupleSize = PtrSize * 2;
1750 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
1752 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
1754 ContentSize += Padding;
1755 ContentSize += (List.size() + 1) * TupleSize;
1757 // For each compile unit, write the list of spans it covers.
1758 Asm->OutStreamer->AddComment("Length of ARange Set");
1759 Asm->EmitInt32(ContentSize);
1760 Asm->OutStreamer->AddComment("DWARF Arange version number");
1761 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
1762 Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
1763 Asm->emitDwarfSymbolReference(CU->getLabelBegin());
1764 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1765 Asm->EmitInt8(PtrSize);
1766 Asm->OutStreamer->AddComment("Segment Size (in bytes)");
1769 Asm->OutStreamer->EmitFill(Padding, 0xff);
1771 for (const ArangeSpan &Span : List) {
1772 Asm->EmitLabelReference(Span.Start, PtrSize);
1774 // Calculate the size as being from the span start to it's end.
1776 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
1778 // For symbols without an end marker (e.g. common), we
1779 // write a single arange entry containing just that one symbol.
1780 uint64_t Size = SymSize[Span.Start];
1784 Asm->OutStreamer->EmitIntValue(Size, PtrSize);
1788 Asm->OutStreamer->AddComment("ARange terminator");
1789 Asm->OutStreamer->EmitIntValue(0, PtrSize);
1790 Asm->OutStreamer->EmitIntValue(0, PtrSize);
1794 // Emit visible names into a debug ranges section.
1795 void DwarfDebug::emitDebugRanges() {
1796 // Start the dwarf ranges section.
1797 Asm->OutStreamer->SwitchSection(
1798 Asm->getObjFileLowering().getDwarfRangesSection());
1800 // Size for our labels.
1801 unsigned char Size = Asm->getDataLayout().getPointerSize();
1803 // Grab the specific ranges for the compile units in the module.
1804 for (const auto &I : CUMap) {
1805 DwarfCompileUnit *TheCU = I.second;
1807 if (auto *Skel = TheCU->getSkeleton())
1810 // Iterate over the misc ranges for the compile units in the module.
1811 for (const RangeSpanList &List : TheCU->getRangeLists()) {
1812 // Emit our symbol so we can find the beginning of the range.
1813 Asm->OutStreamer->EmitLabel(List.getSym());
1815 for (const RangeSpan &Range : List.getRanges()) {
1816 const MCSymbol *Begin = Range.getStart();
1817 const MCSymbol *End = Range.getEnd();
1818 assert(Begin && "Range without a begin symbol?");
1819 assert(End && "Range without an end symbol?");
1820 if (auto *Base = TheCU->getBaseAddress()) {
1821 Asm->EmitLabelDifference(Begin, Base, Size);
1822 Asm->EmitLabelDifference(End, Base, Size);
1824 Asm->OutStreamer->EmitSymbolValue(Begin, Size);
1825 Asm->OutStreamer->EmitSymbolValue(End, Size);
1829 // And terminate the list with two 0 values.
1830 Asm->OutStreamer->EmitIntValue(0, Size);
1831 Asm->OutStreamer->EmitIntValue(0, Size);
1836 // DWARF5 Experimental Separate Dwarf emitters.
1838 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
1839 std::unique_ptr<DwarfUnit> NewU) {
1840 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
1841 U.getCUNode()->getSplitDebugFilename());
1843 if (!CompilationDir.empty())
1844 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
1846 addGnuPubAttributes(*NewU, Die);
1848 SkeletonHolder.addUnit(std::move(NewU));
1851 // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
1852 // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
1853 // DW_AT_addr_base, DW_AT_ranges_base.
1854 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
1856 auto OwnedUnit = make_unique<DwarfCompileUnit>(
1857 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
1858 DwarfCompileUnit &NewCU = *OwnedUnit;
1859 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
1861 NewCU.initStmtList();
1863 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
1868 // Emit the .debug_info.dwo section for separated dwarf. This contains the
1869 // compile units that would normally be in debug_info.
1870 void DwarfDebug::emitDebugInfoDWO() {
1871 assert(useSplitDwarf() && "No split dwarf debug info?");
1872 // Don't emit relocations into the dwo file.
1873 InfoHolder.emitUnits(/* UseOffsets */ true);
1876 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
1877 // abbreviations for the .debug_info.dwo section.
1878 void DwarfDebug::emitDebugAbbrevDWO() {
1879 assert(useSplitDwarf() && "No split dwarf?");
1880 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
1883 void DwarfDebug::emitDebugLineDWO() {
1884 assert(useSplitDwarf() && "No split dwarf?");
1885 Asm->OutStreamer->SwitchSection(
1886 Asm->getObjFileLowering().getDwarfLineDWOSection());
1887 SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams());
1890 // Emit the .debug_str.dwo section for separated dwarf. This contains the
1891 // string section and is identical in format to traditional .debug_str
1893 void DwarfDebug::emitDebugStrDWO() {
1894 assert(useSplitDwarf() && "No split dwarf?");
1895 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
1896 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
1900 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
1901 if (!useSplitDwarf())
1904 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory());
1905 return &SplitTypeUnitFileTable;
1908 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
1910 Hash.update(Identifier);
1911 // ... take the least significant 8 bytes and return those. Our MD5
1912 // implementation always returns its results in little endian, swap bytes
1914 MD5::MD5Result Result;
1916 return support::endian::read64le(Result + 8);
1919 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
1920 StringRef Identifier, DIE &RefDie,
1921 const DICompositeType *CTy) {
1922 // Fast path if we're building some type units and one has already used the
1923 // address pool we know we're going to throw away all this work anyway, so
1924 // don't bother building dependent types.
1925 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
1928 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
1930 CU.addDIETypeSignature(RefDie, *TU);
1934 bool TopLevelType = TypeUnitsUnderConstruction.empty();
1935 AddrPool.resetUsedFlag();
1937 auto OwnedUnit = make_unique<DwarfTypeUnit>(
1938 InfoHolder.getUnits().size() + TypeUnitsUnderConstruction.size(), CU, Asm,
1939 this, &InfoHolder, getDwoLineTable(CU));
1940 DwarfTypeUnit &NewTU = *OwnedUnit;
1941 DIE &UnitDie = NewTU.getUnitDie();
1943 TypeUnitsUnderConstruction.push_back(
1944 std::make_pair(std::move(OwnedUnit), CTy));
1946 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
1949 uint64_t Signature = makeTypeSignature(Identifier);
1950 NewTU.setTypeSignature(Signature);
1952 if (useSplitDwarf())
1953 NewTU.initSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
1955 CU.applyStmtList(UnitDie);
1957 Asm->getObjFileLowering().getDwarfTypesSection(Signature));
1960 NewTU.setType(NewTU.createTypeDIE(CTy));
1963 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
1964 TypeUnitsUnderConstruction.clear();
1966 // Types referencing entries in the address table cannot be placed in type
1968 if (AddrPool.hasBeenUsed()) {
1970 // Remove all the types built while building this type.
1971 // This is pessimistic as some of these types might not be dependent on
1972 // the type that used an address.
1973 for (const auto &TU : TypeUnitsToAdd)
1974 DwarfTypeUnits.erase(TU.second);
1976 // Construct this type in the CU directly.
1977 // This is inefficient because all the dependent types will be rebuilt
1978 // from scratch, including building them in type units, discovering that
1979 // they depend on addresses, throwing them out and rebuilding them.
1980 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
1984 // If the type wasn't dependent on fission addresses, finish adding the type
1985 // and all its dependent types.
1986 for (auto &TU : TypeUnitsToAdd)
1987 InfoHolder.addUnit(std::move(TU.first));
1989 CU.addDIETypeSignature(RefDie, NewTU);
1992 // Accelerator table mutators - add each name along with its companion
1993 // DIE to the proper table while ensuring that the name that we're going
1994 // to reference is in the string table. We do this since the names we
1995 // add may not only be identical to the names in the DIE.
1996 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
1997 if (!useDwarfAccelTables())
1999 AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2002 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2003 if (!useDwarfAccelTables())
2005 AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2008 void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
2009 if (!useDwarfAccelTables())
2011 AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2014 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2015 if (!useDwarfAccelTables())
2017 AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);