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