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 "DwarfCompileUnit.h" 19 #include "DwarfExpression.h" 20 #include "DwarfUnit.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/ADT/StringExtras.h" 24 #include "llvm/ADT/Triple.h" 25 #include "llvm/BinaryFormat/Dwarf.h" 26 #include "llvm/CodeGen/DIE.h" 27 #include "llvm/CodeGen/MachineFunction.h" 28 #include "llvm/CodeGen/MachineModuleInfo.h" 29 #include "llvm/IR/Constants.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/DebugInfo.h" 32 #include "llvm/IR/Instructions.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/IR/ValueHandle.h" 35 #include "llvm/MC/MCAsmInfo.h" 36 #include "llvm/MC/MCDwarf.h" 37 #include "llvm/MC/MCSection.h" 38 #include "llvm/MC/MCStreamer.h" 39 #include "llvm/MC/MCSymbol.h" 40 #include "llvm/Support/CommandLine.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/ErrorHandling.h" 43 #include "llvm/Support/FormattedStream.h" 44 #include "llvm/Support/LEB128.h" 45 #include "llvm/Support/MD5.h" 46 #include "llvm/Support/Path.h" 47 #include "llvm/Support/Timer.h" 48 #include "llvm/Support/raw_ostream.h" 49 #include "llvm/Target/TargetFrameLowering.h" 50 #include "llvm/Target/TargetLoweringObjectFile.h" 51 #include "llvm/Target/TargetMachine.h" 52 #include "llvm/Target/TargetOptions.h" 53 #include "llvm/Target/TargetRegisterInfo.h" 54 #include "llvm/Target/TargetSubtargetInfo.h" 55 56 using namespace llvm; 57 58 #define DEBUG_TYPE "dwarfdebug" 59 60 static cl::opt<bool> 61 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, 62 cl::desc("Disable debug info printing")); 63 64 static cl::opt<bool> 65 GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden, 66 cl::desc("Generate GNU-style pubnames and pubtypes"), 67 cl::init(false)); 68 69 static cl::opt<bool> GenerateARangeSection("generate-arange-section", 70 cl::Hidden, 71 cl::desc("Generate dwarf aranges"), 72 cl::init(false)); 73 74 static cl::opt<bool> SplitDwarfCrossCuReferences( 75 "split-dwarf-cross-cu-references", cl::Hidden, 76 cl::desc("Enable cross-cu references in DWO files"), cl::init(false)); 77 78 namespace { 79 enum DefaultOnOff { Default, Enable, Disable }; 80 } 81 82 static cl::opt<DefaultOnOff> UnknownLocations( 83 "use-unknown-locations", cl::Hidden, 84 cl::desc("Make an absence of debug location information explicit."), 85 cl::values(clEnumVal(Default, "At top of block or after label"), 86 clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")), 87 cl::init(Default)); 88 89 static cl::opt<DefaultOnOff> 90 DwarfAccelTables("dwarf-accel-tables", cl::Hidden, 91 cl::desc("Output prototype dwarf accelerator tables."), 92 cl::values(clEnumVal(Default, "Default for platform"), 93 clEnumVal(Enable, "Enabled"), 94 clEnumVal(Disable, "Disabled")), 95 cl::init(Default)); 96 97 static cl::opt<DefaultOnOff> 98 DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden, 99 cl::desc("Generate DWARF pubnames and pubtypes sections"), 100 cl::values(clEnumVal(Default, "Default for platform"), 101 clEnumVal(Enable, "Enabled"), 102 clEnumVal(Disable, "Disabled")), 103 cl::init(Default)); 104 105 enum LinkageNameOption { 106 DefaultLinkageNames, 107 AllLinkageNames, 108 AbstractLinkageNames 109 }; 110 static cl::opt<LinkageNameOption> 111 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, 112 cl::desc("Which DWARF linkage-name attributes to emit."), 113 cl::values(clEnumValN(DefaultLinkageNames, "Default", 114 "Default for platform"), 115 clEnumValN(AllLinkageNames, "All", "All"), 116 clEnumValN(AbstractLinkageNames, "Abstract", 117 "Abstract subprograms")), 118 cl::init(DefaultLinkageNames)); 119 120 static const char *const DWARFGroupName = "dwarf"; 121 static const char *const DWARFGroupDescription = "DWARF Emission"; 122 static const char *const DbgTimerName = "writer"; 123 static const char *const DbgTimerDescription = "DWARF Debug Writer"; 124 125 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) { 126 BS.EmitInt8( 127 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op) 128 : dwarf::OperationEncodingString(Op)); 129 } 130 131 void DebugLocDwarfExpression::emitSigned(int64_t Value) { 132 BS.EmitSLEB128(Value, Twine(Value)); 133 } 134 135 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) { 136 BS.EmitULEB128(Value, Twine(Value)); 137 } 138 139 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, 140 unsigned MachineReg) { 141 // This information is not available while emitting .debug_loc entries. 142 return false; 143 } 144 145 //===----------------------------------------------------------------------===// 146 147 bool DbgVariable::isBlockByrefVariable() const { 148 assert(Var && "Invalid complex DbgVariable!"); 149 return Var->getType().resolve()->isBlockByrefStruct(); 150 } 151 152 const DIType *DbgVariable::getType() const { 153 DIType *Ty = Var->getType().resolve(); 154 // FIXME: isBlockByrefVariable should be reformulated in terms of complex 155 // addresses instead. 156 if (Ty->isBlockByrefStruct()) { 157 /* Byref variables, in Blocks, are declared by the programmer as 158 "SomeType VarName;", but the compiler creates a 159 __Block_byref_x_VarName struct, and gives the variable VarName 160 either the struct, or a pointer to the struct, as its type. This 161 is necessary for various behind-the-scenes things the compiler 162 needs to do with by-reference variables in blocks. 163 164 However, as far as the original *programmer* is concerned, the 165 variable should still have type 'SomeType', as originally declared. 166 167 The following function dives into the __Block_byref_x_VarName 168 struct to find the original type of the variable. This will be 169 passed back to the code generating the type for the Debug 170 Information Entry for the variable 'VarName'. 'VarName' will then 171 have the original type 'SomeType' in its debug information. 172 173 The original type 'SomeType' will be the type of the field named 174 'VarName' inside the __Block_byref_x_VarName struct. 175 176 NOTE: In order for this to not completely fail on the debugger 177 side, the Debug Information Entry for the variable VarName needs to 178 have a DW_AT_location that tells the debugger how to unwind through 179 the pointers and __Block_byref_x_VarName struct to find the actual 180 value of the variable. The function addBlockByrefType does this. */ 181 DIType *subType = Ty; 182 uint16_t tag = Ty->getTag(); 183 184 if (tag == dwarf::DW_TAG_pointer_type) 185 subType = resolve(cast<DIDerivedType>(Ty)->getBaseType()); 186 187 auto Elements = cast<DICompositeType>(subType)->getElements(); 188 for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 189 auto *DT = cast<DIDerivedType>(Elements[i]); 190 if (getName() == DT->getName()) 191 return resolve(DT->getBaseType()); 192 } 193 } 194 return Ty; 195 } 196 197 ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const { 198 if (FrameIndexExprs.size() == 1) 199 return FrameIndexExprs; 200 201 assert(all_of(FrameIndexExprs, 202 [](const FrameIndexExpr &A) { return A.Expr->isFragment(); }) && 203 "multiple FI expressions without DW_OP_LLVM_fragment"); 204 std::sort(FrameIndexExprs.begin(), FrameIndexExprs.end(), 205 [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool { 206 return A.Expr->getFragmentInfo()->OffsetInBits < 207 B.Expr->getFragmentInfo()->OffsetInBits; 208 }); 209 return FrameIndexExprs; 210 } 211 212 static const DwarfAccelTable::Atom TypeAtoms[] = { 213 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4), 214 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2), 215 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)}; 216 217 DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) 218 : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()), 219 InfoHolder(A, "info_string", DIEValueAllocator), 220 SkeletonHolder(A, "skel_string", DIEValueAllocator), 221 IsDarwin(A->TM.getTargetTriple().isOSDarwin()), 222 AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, 223 dwarf::DW_FORM_data4)), 224 AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, 225 dwarf::DW_FORM_data4)), 226 AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, 227 dwarf::DW_FORM_data4)), 228 AccelTypes(TypeAtoms), DebuggerTuning(DebuggerKind::Default) { 229 230 CurFn = nullptr; 231 const Triple &TT = Asm->TM.getTargetTriple(); 232 233 // Make sure we know our "debugger tuning." The target option takes 234 // precedence; fall back to triple-based defaults. 235 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default) 236 DebuggerTuning = Asm->TM.Options.DebuggerTuning; 237 else if (IsDarwin) 238 DebuggerTuning = DebuggerKind::LLDB; 239 else if (TT.isPS4CPU()) 240 DebuggerTuning = DebuggerKind::SCE; 241 else 242 DebuggerTuning = DebuggerKind::GDB; 243 244 // Turn on accelerator tables for LLDB by default. 245 if (DwarfAccelTables == Default) 246 HasDwarfAccelTables = tuneForLLDB(); 247 else 248 HasDwarfAccelTables = DwarfAccelTables == Enable; 249 250 HasAppleExtensionAttributes = tuneForLLDB(); 251 252 // Handle split DWARF. 253 HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty(); 254 255 // SCE defaults to linkage names only for abstract subprograms. 256 if (DwarfLinkageNames == DefaultLinkageNames) 257 UseAllLinkageNames = !tuneForSCE(); 258 else 259 UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames; 260 261 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion; 262 unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber 263 : MMI->getModule()->getDwarfVersion(); 264 // Use dwarf 4 by default if nothing is requested. 265 DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION; 266 267 // Work around a GDB bug. GDB doesn't support the standard opcode; 268 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which 269 // is defined as of DWARF 3. 270 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented 271 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616 272 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3; 273 274 // GDB does not fully support the DWARF 4 representation for bitfields. 275 UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB(); 276 277 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion); 278 } 279 280 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h. 281 DwarfDebug::~DwarfDebug() { } 282 283 static bool isObjCClass(StringRef Name) { 284 return Name.startswith("+") || Name.startswith("-"); 285 } 286 287 static bool hasObjCCategory(StringRef Name) { 288 if (!isObjCClass(Name)) 289 return false; 290 291 return Name.find(") ") != StringRef::npos; 292 } 293 294 static void getObjCClassCategory(StringRef In, StringRef &Class, 295 StringRef &Category) { 296 if (!hasObjCCategory(In)) { 297 Class = In.slice(In.find('[') + 1, In.find(' ')); 298 Category = ""; 299 return; 300 } 301 302 Class = In.slice(In.find('[') + 1, In.find('(')); 303 Category = In.slice(In.find('[') + 1, In.find(' ')); 304 } 305 306 static StringRef getObjCMethodName(StringRef In) { 307 return In.slice(In.find(' ') + 1, In.find(']')); 308 } 309 310 // Add the various names to the Dwarf accelerator table names. 311 // TODO: Determine whether or not we should add names for programs 312 // that do not have a DW_AT_name or DW_AT_linkage_name field - this 313 // is only slightly different than the lookup of non-standard ObjC names. 314 void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) { 315 if (!SP->isDefinition()) 316 return; 317 addAccelName(SP->getName(), Die); 318 319 // If the linkage name is different than the name, go ahead and output 320 // that as well into the name table. 321 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName()) 322 addAccelName(SP->getLinkageName(), Die); 323 324 // If this is an Objective-C selector name add it to the ObjC accelerator 325 // too. 326 if (isObjCClass(SP->getName())) { 327 StringRef Class, Category; 328 getObjCClassCategory(SP->getName(), Class, Category); 329 addAccelObjC(Class, Die); 330 if (Category != "") 331 addAccelObjC(Category, Die); 332 // Also add the base method name to the name table. 333 addAccelName(getObjCMethodName(SP->getName()), Die); 334 } 335 } 336 337 /// Check whether we should create a DIE for the given Scope, return true 338 /// if we don't create a DIE (the corresponding DIE is null). 339 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) { 340 if (Scope->isAbstractScope()) 341 return false; 342 343 // We don't create a DIE if there is no Range. 344 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges(); 345 if (Ranges.empty()) 346 return true; 347 348 if (Ranges.size() > 1) 349 return false; 350 351 // We don't create a DIE if we have a single Range and the end label 352 // is null. 353 return !getLabelAfterInsn(Ranges.front().second); 354 } 355 356 template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) { 357 F(CU); 358 if (auto *SkelCU = CU.getSkeleton()) 359 if (CU.getCUNode()->getSplitDebugInlining()) 360 F(*SkelCU); 361 } 362 363 bool DwarfDebug::shareAcrossDWOCUs() const { 364 return SplitDwarfCrossCuReferences; 365 } 366 367 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, 368 LexicalScope *Scope) { 369 assert(Scope && Scope->getScopeNode()); 370 assert(Scope->isAbstractScope()); 371 assert(!Scope->getInlinedAt()); 372 373 auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 374 375 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 376 // was inlined from another compile unit. 377 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining()) 378 // Avoid building the original CU if it won't be used 379 SrcCU.constructAbstractSubprogramScopeDIE(Scope); 380 else { 381 auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 382 if (auto *SkelCU = CU.getSkeleton()) { 383 (shareAcrossDWOCUs() ? CU : SrcCU) 384 .constructAbstractSubprogramScopeDIE(Scope); 385 if (CU.getCUNode()->getSplitDebugInlining()) 386 SkelCU->constructAbstractSubprogramScopeDIE(Scope); 387 } else 388 CU.constructAbstractSubprogramScopeDIE(Scope); 389 } 390 } 391 392 bool DwarfDebug::hasDwarfPubSections(bool includeMinimalInlineScopes) const { 393 // Opting in to GNU Pubnames/types overrides the default to ensure these are 394 // generated for things like Gold's gdb_index generation. 395 if (GenerateGnuPubSections) 396 return true; 397 398 if (DwarfPubSections == Default) 399 return tuneForGDB() && !includeMinimalInlineScopes; 400 401 return DwarfPubSections == Enable; 402 } 403 404 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const { 405 if (!hasDwarfPubSections(U.includeMinimalInlineScopes())) 406 return; 407 408 U.addFlag(D, dwarf::DW_AT_GNU_pubnames); 409 } 410 411 // Create new DwarfCompileUnit for the given metadata node with tag 412 // DW_TAG_compile_unit. 413 DwarfCompileUnit & 414 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) { 415 if (auto *CU = CUMap.lookup(DIUnit)) 416 return *CU; 417 StringRef FN = DIUnit->getFilename(); 418 CompilationDir = DIUnit->getDirectory(); 419 420 auto OwnedUnit = make_unique<DwarfCompileUnit>( 421 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder); 422 DwarfCompileUnit &NewCU = *OwnedUnit; 423 DIE &Die = NewCU.getUnitDie(); 424 InfoHolder.addUnit(std::move(OwnedUnit)); 425 if (useSplitDwarf()) { 426 NewCU.setSkeleton(constructSkeletonCU(NewCU)); 427 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, 428 Asm->TM.Options.MCOptions.SplitDwarfFile); 429 } 430 431 // LTO with assembly output shares a single line table amongst multiple CUs. 432 // To avoid the compilation directory being ambiguous, let the line table 433 // explicitly describe the directory of all files, never relying on the 434 // compilation directory. 435 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU) 436 Asm->OutStreamer->getContext().setMCLineTableCompilationDir( 437 NewCU.getUniqueID(), CompilationDir); 438 439 StringRef Producer = DIUnit->getProducer(); 440 StringRef Flags = DIUnit->getFlags(); 441 if (!Flags.empty()) { 442 std::string ProducerWithFlags = Producer.str() + " " + Flags.str(); 443 NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags); 444 } else 445 NewCU.addString(Die, dwarf::DW_AT_producer, Producer); 446 447 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 448 DIUnit->getSourceLanguage()); 449 NewCU.addString(Die, dwarf::DW_AT_name, FN); 450 451 if (!useSplitDwarf()) { 452 NewCU.initStmtList(); 453 454 // If we're using split dwarf the compilation dir is going to be in the 455 // skeleton CU and so we don't need to duplicate it here. 456 if (!CompilationDir.empty()) 457 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 458 459 addGnuPubAttributes(NewCU, Die); 460 } 461 462 if (useAppleExtensionAttributes()) { 463 if (DIUnit->isOptimized()) 464 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized); 465 466 StringRef Flags = DIUnit->getFlags(); 467 if (!Flags.empty()) 468 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags); 469 470 if (unsigned RVer = DIUnit->getRuntimeVersion()) 471 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, 472 dwarf::DW_FORM_data1, RVer); 473 } 474 475 if (useSplitDwarf()) 476 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection()); 477 else 478 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 479 480 if (DIUnit->getDWOId()) { 481 // This CU is either a clang module DWO or a skeleton CU. 482 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, 483 DIUnit->getDWOId()); 484 if (!DIUnit->getSplitDebugFilename().empty()) 485 // This is a prefabricated skeleton CU. 486 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, 487 DIUnit->getSplitDebugFilename()); 488 } 489 490 CUMap.insert({DIUnit, &NewCU}); 491 CUDieMap.insert({&Die, &NewCU}); 492 return NewCU; 493 } 494 495 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, 496 const DIImportedEntity *N) { 497 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope())) 498 D->addChild(TheCU.constructImportedEntityDIE(N)); 499 } 500 501 /// Sort and unique GVEs by comparing their fragment offset. 502 static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & 503 sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { 504 std::sort(GVEs.begin(), GVEs.end(), 505 [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { 506 if (A.Expr != B.Expr && A.Expr && B.Expr) { 507 auto FragmentA = A.Expr->getFragmentInfo(); 508 auto FragmentB = B.Expr->getFragmentInfo(); 509 if (FragmentA && FragmentB) 510 return FragmentA->OffsetInBits < FragmentB->OffsetInBits; 511 } 512 return false; 513 }); 514 GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), 515 [](DwarfCompileUnit::GlobalExpr A, 516 DwarfCompileUnit::GlobalExpr B) { 517 return A.Expr == B.Expr; 518 }), 519 GVEs.end()); 520 return GVEs; 521 } 522 523 // Emit all Dwarf sections that should come prior to the content. Create 524 // global DIEs and emit initial debug info sections. This is invoked by 525 // the target AsmPrinter. 526 void DwarfDebug::beginModule() { 527 NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName, 528 DWARFGroupDescription, TimePassesIsEnabled); 529 if (DisableDebugInfoPrinting) 530 return; 531 532 const Module *M = MMI->getModule(); 533 534 unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(), 535 M->debug_compile_units_end()); 536 // Tell MMI whether we have debug info. 537 MMI->setDebugInfoAvailability(NumDebugCUs > 0); 538 SingleCU = NumDebugCUs == 1; 539 DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>> 540 GVMap; 541 for (const GlobalVariable &Global : M->globals()) { 542 SmallVector<DIGlobalVariableExpression *, 1> GVs; 543 Global.getDebugInfo(GVs); 544 for (auto *GVE : GVs) 545 GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()}); 546 } 547 548 for (DICompileUnit *CUNode : M->debug_compile_units()) { 549 if (CUNode->getEnumTypes().empty() && CUNode->getRetainedTypes().empty() && 550 CUNode->getGlobalVariables().empty() && 551 CUNode->getImportedEntities().empty() && CUNode->getMacros().empty()) 552 continue; 553 554 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode); 555 for (auto *IE : CUNode->getImportedEntities()) 556 CU.addImportedEntity(IE); 557 558 // Global Variables. 559 for (auto *GVE : CUNode->getGlobalVariables()) 560 GVMap[GVE->getVariable()].push_back({nullptr, GVE->getExpression()}); 561 DenseSet<DIGlobalVariable *> Processed; 562 for (auto *GVE : CUNode->getGlobalVariables()) { 563 DIGlobalVariable *GV = GVE->getVariable(); 564 if (Processed.insert(GV).second) 565 CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV])); 566 } 567 568 for (auto *Ty : CUNode->getEnumTypes()) { 569 // The enum types array by design contains pointers to 570 // MDNodes rather than DIRefs. Unique them here. 571 CU.getOrCreateTypeDIE(cast<DIType>(Ty)); 572 } 573 for (auto *Ty : CUNode->getRetainedTypes()) { 574 // The retained types array by design contains pointers to 575 // MDNodes rather than DIRefs. Unique them here. 576 if (DIType *RT = dyn_cast<DIType>(Ty)) 577 // There is no point in force-emitting a forward declaration. 578 CU.getOrCreateTypeDIE(RT); 579 } 580 // Emit imported_modules last so that the relevant context is already 581 // available. 582 for (auto *IE : CUNode->getImportedEntities()) 583 constructAndAddImportedEntityDIE(CU, IE); 584 } 585 } 586 587 void DwarfDebug::finishVariableDefinitions() { 588 for (const auto &Var : ConcreteVariables) { 589 DIE *VariableDie = Var->getDIE(); 590 assert(VariableDie); 591 // FIXME: Consider the time-space tradeoff of just storing the unit pointer 592 // in the ConcreteVariables list, rather than looking it up again here. 593 // DIE::getUnit isn't simple - it walks parent pointers, etc. 594 DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie()); 595 assert(Unit); 596 Unit->finishVariableDefinition(*Var); 597 } 598 } 599 600 void DwarfDebug::finishSubprogramDefinitions() { 601 for (const DISubprogram *SP : ProcessedSPNodes) { 602 assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug); 603 forBothCUs( 604 getOrCreateDwarfCompileUnit(SP->getUnit()), 605 [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); }); 606 } 607 } 608 609 void DwarfDebug::finalizeModuleInfo() { 610 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 611 612 finishSubprogramDefinitions(); 613 614 finishVariableDefinitions(); 615 616 // Include the DWO file name in the hash if there's more than one CU. 617 // This handles ThinLTO's situation where imported CUs may very easily be 618 // duplicate with the same CU partially imported into another ThinLTO unit. 619 StringRef DWOName; 620 if (CUMap.size() > 1) 621 DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile; 622 623 // Handle anything that needs to be done on a per-unit basis after 624 // all other generation. 625 for (const auto &P : CUMap) { 626 auto &TheCU = *P.second; 627 // Emit DW_AT_containing_type attribute to connect types with their 628 // vtable holding type. 629 TheCU.constructContainingTypeDIEs(); 630 631 // Add CU specific attributes if we need to add any. 632 // If we're splitting the dwarf out now that we've got the entire 633 // CU then add the dwo id to it. 634 auto *SkCU = TheCU.getSkeleton(); 635 if (useSplitDwarf()) { 636 // Emit a unique identifier for this CU. 637 uint64_t ID = 638 DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie()); 639 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 640 dwarf::DW_FORM_data8, ID); 641 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 642 dwarf::DW_FORM_data8, ID); 643 644 // We don't keep track of which addresses are used in which CU so this 645 // is a bit pessimistic under LTO. 646 if (!AddrPool.isEmpty()) { 647 const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol(); 648 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base, 649 Sym, Sym); 650 } 651 if (!SkCU->getRangeLists().empty()) { 652 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); 653 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, 654 Sym, Sym); 655 } 656 } 657 658 // If we have code split among multiple sections or non-contiguous 659 // ranges of code then emit a DW_AT_ranges attribute on the unit that will 660 // remain in the .o file, otherwise add a DW_AT_low_pc. 661 // FIXME: We should use ranges allow reordering of code ala 662 // .subsections_via_symbols in mach-o. This would mean turning on 663 // ranges for all subprogram DIEs for mach-o. 664 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 665 if (unsigned NumRanges = TheCU.getRanges().size()) { 666 if (NumRanges > 1) 667 // A DW_AT_low_pc attribute may also be specified in combination with 668 // DW_AT_ranges to specify the default base address for use in 669 // location lists (see Section 2.6.2) and range lists (see Section 670 // 2.17.3). 671 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0); 672 else 673 U.setBaseAddress(TheCU.getRanges().front().getStart()); 674 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); 675 } 676 677 auto *CUNode = cast<DICompileUnit>(P.first); 678 // If compile Unit has macros, emit "DW_AT_macro_info" attribute. 679 if (CUNode->getMacros()) 680 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, 681 U.getMacroLabelBegin(), 682 TLOF.getDwarfMacinfoSection()->getBeginSymbol()); 683 } 684 685 // Compute DIE offsets and sizes. 686 InfoHolder.computeSizeAndOffsets(); 687 if (useSplitDwarf()) 688 SkeletonHolder.computeSizeAndOffsets(); 689 } 690 691 // Emit all Dwarf sections that should come after the content. 692 void DwarfDebug::endModule() { 693 assert(CurFn == nullptr); 694 assert(CurMI == nullptr); 695 696 // If we aren't actually generating debug info (check beginModule - 697 // conditionalized on !DisableDebugInfoPrinting and the presence of the 698 // llvm.dbg.cu metadata node) 699 if (!MMI->hasDebugInfo()) 700 return; 701 702 // Finalize the debug info for the module. 703 finalizeModuleInfo(); 704 705 emitDebugStr(); 706 707 if (useSplitDwarf()) 708 emitDebugLocDWO(); 709 else 710 // Emit info into a debug loc section. 711 emitDebugLoc(); 712 713 // Corresponding abbreviations into a abbrev section. 714 emitAbbreviations(); 715 716 // Emit all the DIEs into a debug info section. 717 emitDebugInfo(); 718 719 // Emit info into a debug aranges section. 720 if (GenerateARangeSection) 721 emitDebugARanges(); 722 723 // Emit info into a debug ranges section. 724 emitDebugRanges(); 725 726 // Emit info into a debug macinfo section. 727 emitDebugMacinfo(); 728 729 if (useSplitDwarf()) { 730 emitDebugStrDWO(); 731 emitDebugInfoDWO(); 732 emitDebugAbbrevDWO(); 733 emitDebugLineDWO(); 734 // Emit DWO addresses. 735 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); 736 } 737 738 // Emit info into the dwarf accelerator table sections. 739 if (useDwarfAccelTables()) { 740 emitAccelNames(); 741 emitAccelObjC(); 742 emitAccelNamespaces(); 743 emitAccelTypes(); 744 } 745 746 // Emit the pubnames and pubtypes sections if requested. 747 // The condition is optimistically correct - any CU not using GMLT (& 748 // implicit/default pubnames state) might still have pubnames. 749 if (hasDwarfPubSections(/* gmlt */ false)) { 750 emitDebugPubNames(GenerateGnuPubSections); 751 emitDebugPubTypes(GenerateGnuPubSections); 752 } 753 754 // clean up. 755 // FIXME: AbstractVariables.clear(); 756 } 757 758 void DwarfDebug::ensureAbstractVariableIsCreated(DwarfCompileUnit &CU, InlinedVariable IV, 759 const MDNode *ScopeNode) { 760 const DILocalVariable *Cleansed = nullptr; 761 if (CU.getExistingAbstractVariable(IV, Cleansed)) 762 return; 763 764 CU.createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope( 765 cast<DILocalScope>(ScopeNode))); 766 } 767 768 void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit &CU, 769 InlinedVariable IV, const MDNode *ScopeNode) { 770 const DILocalVariable *Cleansed = nullptr; 771 if (CU.getExistingAbstractVariable(IV, Cleansed)) 772 return; 773 774 if (LexicalScope *Scope = 775 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode))) 776 CU.createAbstractVariable(Cleansed, Scope); 777 } 778 // Collect variable information from side table maintained by MF. 779 void DwarfDebug::collectVariableInfoFromMFTable( 780 DwarfCompileUnit &TheCU, DenseSet<InlinedVariable> &Processed) { 781 for (const auto &VI : Asm->MF->getVariableDbgInfo()) { 782 if (!VI.Var) 783 continue; 784 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && 785 "Expected inlined-at fields to agree"); 786 787 InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt()); 788 Processed.insert(Var); 789 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); 790 791 // If variable scope is not found then skip this variable. 792 if (!Scope) 793 continue; 794 795 ensureAbstractVariableIsCreatedIfScoped(TheCU, Var, Scope->getScopeNode()); 796 auto RegVar = make_unique<DbgVariable>(Var.first, Var.second); 797 RegVar->initializeMMI(VI.Expr, VI.Slot); 798 if (InfoHolder.addScopeVariable(Scope, RegVar.get())) 799 ConcreteVariables.push_back(std::move(RegVar)); 800 } 801 } 802 803 // Get .debug_loc entry for the instruction range starting at MI. 804 static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) { 805 const DIExpression *Expr = MI->getDebugExpression(); 806 807 assert(MI->getNumOperands() == 4); 808 if (MI->getOperand(0).isReg()) { 809 MachineLocation MLoc; 810 // If the second operand is an immediate, this is a 811 // register-indirect address. 812 if (!MI->getOperand(1).isImm()) 813 MLoc.set(MI->getOperand(0).getReg()); 814 else 815 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm()); 816 return DebugLocEntry::Value(Expr, MLoc); 817 } 818 if (MI->getOperand(0).isImm()) 819 return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm()); 820 if (MI->getOperand(0).isFPImm()) 821 return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm()); 822 if (MI->getOperand(0).isCImm()) 823 return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm()); 824 825 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!"); 826 } 827 828 /// \brief If this and Next are describing different fragments of the same 829 /// variable, merge them by appending Next's values to the current 830 /// list of values. 831 /// Return true if the merge was successful. 832 bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) { 833 if (Begin == Next.Begin) { 834 auto *FirstExpr = cast<DIExpression>(Values[0].Expression); 835 auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression); 836 if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment()) 837 return false; 838 839 // We can only merge entries if none of the fragments overlap any others. 840 // In doing so, we can take advantage of the fact that both lists are 841 // sorted. 842 for (unsigned i = 0, j = 0; i < Values.size(); ++i) { 843 for (; j < Next.Values.size(); ++j) { 844 int res = DebugHandlerBase::fragmentCmp( 845 cast<DIExpression>(Values[i].Expression), 846 cast<DIExpression>(Next.Values[j].Expression)); 847 if (res == 0) // The two expressions overlap, we can't merge. 848 return false; 849 // Values[i] is entirely before Next.Values[j], 850 // so go back to the next entry of Values. 851 else if (res == -1) 852 break; 853 // Next.Values[j] is entirely before Values[i], so go on to the 854 // next entry of Next.Values. 855 } 856 } 857 858 addValues(Next.Values); 859 End = Next.End; 860 return true; 861 } 862 return false; 863 } 864 865 /// Build the location list for all DBG_VALUEs in the function that 866 /// describe the same variable. If the ranges of several independent 867 /// fragments of the same variable overlap partially, split them up and 868 /// combine the ranges. The resulting DebugLocEntries are will have 869 /// strict monotonically increasing begin addresses and will never 870 /// overlap. 871 // 872 // Input: 873 // 874 // Ranges History [var, loc, fragment ofs size] 875 // 0 | [x, (reg0, fragment 0, 32)] 876 // 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry 877 // 2 | | ... 878 // 3 | [clobber reg0] 879 // 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of 880 // x. 881 // 882 // Output: 883 // 884 // [0-1] [x, (reg0, fragment 0, 32)] 885 // [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)] 886 // [3-4] [x, (reg1, fragment 32, 32)] 887 // [4- ] [x, (mem, fragment 0, 64)] 888 void 889 DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, 890 const DbgValueHistoryMap::InstrRanges &Ranges) { 891 SmallVector<DebugLocEntry::Value, 4> OpenRanges; 892 893 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { 894 const MachineInstr *Begin = I->first; 895 const MachineInstr *End = I->second; 896 assert(Begin->isDebugValue() && "Invalid History entry"); 897 898 // Check if a variable is inaccessible in this range. 899 if (Begin->getNumOperands() > 1 && 900 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) { 901 OpenRanges.clear(); 902 continue; 903 } 904 905 // If this fragment overlaps with any open ranges, truncate them. 906 const DIExpression *DIExpr = Begin->getDebugExpression(); 907 auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) { 908 return fragmentsOverlap(DIExpr, R.getExpression()); 909 }); 910 OpenRanges.erase(Last, OpenRanges.end()); 911 912 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin); 913 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!"); 914 915 const MCSymbol *EndLabel; 916 if (End != nullptr) 917 EndLabel = getLabelAfterInsn(End); 918 else if (std::next(I) == Ranges.end()) 919 EndLabel = Asm->getFunctionEnd(); 920 else 921 EndLabel = getLabelBeforeInsn(std::next(I)->first); 922 assert(EndLabel && "Forgot label after instruction ending a range!"); 923 924 DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n"); 925 926 auto Value = getDebugLocValue(Begin); 927 DebugLocEntry Loc(StartLabel, EndLabel, Value); 928 bool couldMerge = false; 929 930 // If this is a fragment, it may belong to the current DebugLocEntry. 931 if (DIExpr->isFragment()) { 932 // Add this value to the list of open ranges. 933 OpenRanges.push_back(Value); 934 935 // Attempt to add the fragment to the last entry. 936 if (!DebugLoc.empty()) 937 if (DebugLoc.back().MergeValues(Loc)) 938 couldMerge = true; 939 } 940 941 if (!couldMerge) { 942 // Need to add a new DebugLocEntry. Add all values from still 943 // valid non-overlapping fragments. 944 if (OpenRanges.size()) 945 Loc.addValues(OpenRanges); 946 947 DebugLoc.push_back(std::move(Loc)); 948 } 949 950 // Attempt to coalesce the ranges of two otherwise identical 951 // DebugLocEntries. 952 auto CurEntry = DebugLoc.rbegin(); 953 DEBUG({ 954 dbgs() << CurEntry->getValues().size() << " Values:\n"; 955 for (auto &Value : CurEntry->getValues()) 956 Value.dump(); 957 dbgs() << "-----\n"; 958 }); 959 960 auto PrevEntry = std::next(CurEntry); 961 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry)) 962 DebugLoc.pop_back(); 963 } 964 } 965 966 DbgVariable *DwarfDebug::createConcreteVariable(DwarfCompileUnit &TheCU, 967 LexicalScope &Scope, 968 InlinedVariable IV) { 969 ensureAbstractVariableIsCreatedIfScoped(TheCU, IV, Scope.getScopeNode()); 970 ConcreteVariables.push_back(make_unique<DbgVariable>(IV.first, IV.second)); 971 InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get()); 972 return ConcreteVariables.back().get(); 973 } 974 975 // Determine whether this DBG_VALUE is valid at the beginning of the function. 976 static bool validAtEntry(const MachineInstr *MInsn) { 977 auto MBB = MInsn->getParent(); 978 // Is it in the entry basic block? 979 if (!MBB->pred_empty()) 980 return false; 981 for (MachineBasicBlock::const_reverse_iterator I(MInsn); I != MBB->rend(); ++I) 982 if (!(I->isDebugValue() || I->getFlag(MachineInstr::FrameSetup))) 983 return false; 984 return true; 985 } 986 987 // Find variables for each lexical scope. 988 void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU, 989 const DISubprogram *SP, 990 DenseSet<InlinedVariable> &Processed) { 991 // Grab the variable info that was squirreled away in the MMI side-table. 992 collectVariableInfoFromMFTable(TheCU, Processed); 993 994 for (const auto &I : DbgValues) { 995 InlinedVariable IV = I.first; 996 if (Processed.count(IV)) 997 continue; 998 999 // Instruction ranges, specifying where IV is accessible. 1000 const auto &Ranges = I.second; 1001 if (Ranges.empty()) 1002 continue; 1003 1004 LexicalScope *Scope = nullptr; 1005 if (const DILocation *IA = IV.second) 1006 Scope = LScopes.findInlinedScope(IV.first->getScope(), IA); 1007 else 1008 Scope = LScopes.findLexicalScope(IV.first->getScope()); 1009 // If variable scope is not found then skip this variable. 1010 if (!Scope) 1011 continue; 1012 1013 Processed.insert(IV); 1014 DbgVariable *RegVar = createConcreteVariable(TheCU, *Scope, IV); 1015 1016 const MachineInstr *MInsn = Ranges.front().first; 1017 assert(MInsn->isDebugValue() && "History must begin with debug value"); 1018 1019 // Check if there is a single DBG_VALUE, valid throughout the function. 1020 // A single constant is also considered valid for the entire function. 1021 if (Ranges.size() == 1 && 1022 (MInsn->getOperand(0).isImm() || 1023 (validAtEntry(MInsn) && Ranges.front().second == nullptr))) { 1024 RegVar->initializeDbgValue(MInsn); 1025 continue; 1026 } 1027 1028 // Handle multiple DBG_VALUE instructions describing one variable. 1029 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn); 1030 1031 // Build the location list for this variable. 1032 SmallVector<DebugLocEntry, 8> Entries; 1033 buildLocationList(Entries, Ranges); 1034 1035 // If the variable has a DIBasicType, extract it. Basic types cannot have 1036 // unique identifiers, so don't bother resolving the type with the 1037 // identifier map. 1038 const DIBasicType *BT = dyn_cast<DIBasicType>( 1039 static_cast<const Metadata *>(IV.first->getType())); 1040 1041 // Finalize the entry by lowering it into a DWARF bytestream. 1042 for (auto &Entry : Entries) 1043 Entry.finalize(*Asm, List, BT); 1044 } 1045 1046 // Collect info for variables that were optimized out. 1047 for (const DILocalVariable *DV : SP->getVariables()) { 1048 if (Processed.insert(InlinedVariable(DV, nullptr)).second) 1049 if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope())) 1050 createConcreteVariable(TheCU, *Scope, InlinedVariable(DV, nullptr)); 1051 } 1052 } 1053 1054 // Process beginning of an instruction. 1055 void DwarfDebug::beginInstruction(const MachineInstr *MI) { 1056 DebugHandlerBase::beginInstruction(MI); 1057 assert(CurMI); 1058 1059 const auto *SP = MI->getParent()->getParent()->getFunction()->getSubprogram(); 1060 if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) 1061 return; 1062 1063 // Check if source location changes, but ignore DBG_VALUE and CFI locations. 1064 if (MI->isMetaInstruction()) 1065 return; 1066 const DebugLoc &DL = MI->getDebugLoc(); 1067 // When we emit a line-0 record, we don't update PrevInstLoc; so look at 1068 // the last line number actually emitted, to see if it was line 0. 1069 unsigned LastAsmLine = 1070 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine(); 1071 1072 if (DL == PrevInstLoc) { 1073 // If we have an ongoing unspecified location, nothing to do here. 1074 if (!DL) 1075 return; 1076 // We have an explicit location, same as the previous location. 1077 // But we might be coming back to it after a line 0 record. 1078 if (LastAsmLine == 0 && DL.getLine() != 0) { 1079 // Reinstate the source location but not marked as a statement. 1080 const MDNode *Scope = DL.getScope(); 1081 recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0); 1082 } 1083 return; 1084 } 1085 1086 if (!DL) { 1087 // We have an unspecified location, which might want to be line 0. 1088 // If we have already emitted a line-0 record, don't repeat it. 1089 if (LastAsmLine == 0) 1090 return; 1091 // If user said Don't Do That, don't do that. 1092 if (UnknownLocations == Disable) 1093 return; 1094 // See if we have a reason to emit a line-0 record now. 1095 // Reasons to emit a line-0 record include: 1096 // - User asked for it (UnknownLocations). 1097 // - Instruction has a label, so it's referenced from somewhere else, 1098 // possibly debug information; we want it to have a source location. 1099 // - Instruction is at the top of a block; we don't want to inherit the 1100 // location from the physically previous (maybe unrelated) block. 1101 if (UnknownLocations == Enable || PrevLabel || 1102 (PrevInstBB && PrevInstBB != MI->getParent())) { 1103 // Preserve the file and column numbers, if we can, to save space in 1104 // the encoded line table. 1105 // Do not update PrevInstLoc, it remembers the last non-0 line. 1106 const MDNode *Scope = nullptr; 1107 unsigned Column = 0; 1108 if (PrevInstLoc) { 1109 Scope = PrevInstLoc.getScope(); 1110 Column = PrevInstLoc.getCol(); 1111 } 1112 recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0); 1113 } 1114 return; 1115 } 1116 1117 // We have an explicit location, different from the previous location. 1118 // Don't repeat a line-0 record, but otherwise emit the new location. 1119 // (The new location might be an explicit line 0, which we do emit.) 1120 if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0) 1121 return; 1122 unsigned Flags = 0; 1123 if (DL == PrologEndLoc) { 1124 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT; 1125 PrologEndLoc = DebugLoc(); 1126 } 1127 // If the line changed, we call that a new statement; unless we went to 1128 // line 0 and came back, in which case it is not a new statement. 1129 unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine; 1130 if (DL.getLine() && DL.getLine() != OldLine) 1131 Flags |= DWARF2_FLAG_IS_STMT; 1132 1133 const MDNode *Scope = DL.getScope(); 1134 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); 1135 1136 // If we're not at line 0, remember this location. 1137 if (DL.getLine()) 1138 PrevInstLoc = DL; 1139 } 1140 1141 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { 1142 // First known non-DBG_VALUE and non-frame setup location marks 1143 // the beginning of the function body. 1144 for (const auto &MBB : *MF) 1145 for (const auto &MI : MBB) 1146 if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && 1147 MI.getDebugLoc()) 1148 return MI.getDebugLoc(); 1149 return DebugLoc(); 1150 } 1151 1152 // Gather pre-function debug information. Assumes being called immediately 1153 // after the function entry point has been emitted. 1154 void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) { 1155 CurFn = MF; 1156 1157 auto *SP = MF->getFunction()->getSubprogram(); 1158 assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode()); 1159 if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) 1160 return; 1161 1162 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 1163 1164 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function 1165 // belongs to so that we add to the correct per-cu line table in the 1166 // non-asm case. 1167 if (Asm->OutStreamer->hasRawTextSupport()) 1168 // Use a single line table if we are generating assembly. 1169 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1170 else 1171 Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID()); 1172 1173 // Record beginning of function. 1174 PrologEndLoc = findPrologueEndLoc(MF); 1175 if (PrologEndLoc) { 1176 // We'd like to list the prologue as "not statements" but GDB behaves 1177 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. 1178 auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); 1179 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT); 1180 } 1181 } 1182 1183 void DwarfDebug::skippedNonDebugFunction() { 1184 // If we don't have a subprogram for this function then there will be a hole 1185 // in the range information. Keep note of this by setting the previously used 1186 // section to nullptr. 1187 PrevCU = nullptr; 1188 CurFn = nullptr; 1189 } 1190 1191 // Gather and emit post-function debug information. 1192 void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { 1193 const DISubprogram *SP = MF->getFunction()->getSubprogram(); 1194 1195 assert(CurFn == MF && 1196 "endFunction should be called with the same function as beginFunction"); 1197 1198 // Set DwarfDwarfCompileUnitID in MCContext to default value. 1199 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1200 1201 LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); 1202 assert(!FnScope || SP == FnScope->getScopeNode()); 1203 DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); 1204 1205 DenseSet<InlinedVariable> ProcessedVars; 1206 collectVariableInfo(TheCU, SP, ProcessedVars); 1207 1208 // Add the range of this function to the list of ranges for the CU. 1209 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd())); 1210 1211 // Under -gmlt, skip building the subprogram if there are no inlined 1212 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram 1213 // is still needed as we need its source location. 1214 if (!TheCU.getCUNode()->getDebugInfoForProfiling() && 1215 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && 1216 LScopes.getAbstractScopesList().empty() && !IsDarwin) { 1217 assert(InfoHolder.getScopeVariables().empty()); 1218 PrevLabel = nullptr; 1219 CurFn = nullptr; 1220 return; 1221 } 1222 1223 #ifndef NDEBUG 1224 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); 1225 #endif 1226 // Construct abstract scopes. 1227 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { 1228 auto *SP = cast<DISubprogram>(AScope->getScopeNode()); 1229 // Collect info for variables that were optimized out. 1230 for (const DILocalVariable *DV : SP->getVariables()) { 1231 if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second) 1232 continue; 1233 ensureAbstractVariableIsCreated(TheCU, InlinedVariable(DV, nullptr), 1234 DV->getScope()); 1235 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes 1236 && "ensureAbstractVariableIsCreated inserted abstract scopes"); 1237 } 1238 constructAbstractSubprogramScopeDIE(TheCU, AScope); 1239 } 1240 1241 ProcessedSPNodes.insert(SP); 1242 TheCU.constructSubprogramScopeDIE(SP, FnScope); 1243 if (auto *SkelCU = TheCU.getSkeleton()) 1244 if (!LScopes.getAbstractScopesList().empty() && 1245 TheCU.getCUNode()->getSplitDebugInlining()) 1246 SkelCU->constructSubprogramScopeDIE(SP, FnScope); 1247 1248 // Clear debug info 1249 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the 1250 // DbgVariables except those that are also in AbstractVariables (since they 1251 // can be used cross-function) 1252 InfoHolder.getScopeVariables().clear(); 1253 PrevLabel = nullptr; 1254 CurFn = nullptr; 1255 } 1256 1257 // Register a source line with debug info. Returns the unique label that was 1258 // emitted and which provides correspondence to the source line list. 1259 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, 1260 unsigned Flags) { 1261 StringRef Fn; 1262 StringRef Dir; 1263 unsigned Src = 1; 1264 unsigned Discriminator = 0; 1265 if (auto *Scope = cast_or_null<DIScope>(S)) { 1266 Fn = Scope->getFilename(); 1267 Dir = Scope->getDirectory(); 1268 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) 1269 if (getDwarfVersion() >= 4) 1270 Discriminator = LBF->getDiscriminator(); 1271 1272 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID(); 1273 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID]) 1274 .getOrCreateSourceID(Fn, Dir); 1275 } 1276 Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 1277 Discriminator, Fn); 1278 } 1279 1280 //===----------------------------------------------------------------------===// 1281 // Emit Methods 1282 //===----------------------------------------------------------------------===// 1283 1284 // Emit the debug info section. 1285 void DwarfDebug::emitDebugInfo() { 1286 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1287 Holder.emitUnits(/* UseOffsets */ false); 1288 } 1289 1290 // Emit the abbreviation section. 1291 void DwarfDebug::emitAbbreviations() { 1292 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1293 1294 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); 1295 } 1296 1297 void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section, 1298 StringRef TableName) { 1299 Accel.FinalizeTable(Asm, TableName); 1300 Asm->OutStreamer->SwitchSection(Section); 1301 1302 // Emit the full data. 1303 Accel.emit(Asm, Section->getBeginSymbol(), this); 1304 } 1305 1306 // Emit visible names into a hashed accelerator table section. 1307 void DwarfDebug::emitAccelNames() { 1308 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), 1309 "Names"); 1310 } 1311 1312 // Emit objective C classes and categories into a hashed accelerator table 1313 // section. 1314 void DwarfDebug::emitAccelObjC() { 1315 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), 1316 "ObjC"); 1317 } 1318 1319 // Emit namespace dies into a hashed accelerator table. 1320 void DwarfDebug::emitAccelNamespaces() { 1321 emitAccel(AccelNamespace, 1322 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), 1323 "namespac"); 1324 } 1325 1326 // Emit type dies into a hashed accelerator table. 1327 void DwarfDebug::emitAccelTypes() { 1328 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), 1329 "types"); 1330 } 1331 1332 // Public name handling. 1333 // The format for the various pubnames: 1334 // 1335 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU 1336 // for the DIE that is named. 1337 // 1338 // gnu pubnames - offset/index value/name tuples where the offset is the offset 1339 // into the CU and the index value is computed according to the type of value 1340 // for the DIE that is named. 1341 // 1342 // For type units the offset is the offset of the skeleton DIE. For split dwarf 1343 // it's the offset within the debug_info/debug_types dwo section, however, the 1344 // reference in the pubname header doesn't change. 1345 1346 /// computeIndexValue - Compute the gdb index value for the DIE and CU. 1347 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, 1348 const DIE *Die) { 1349 // Entities that ended up only in a Type Unit reference the CU instead (since 1350 // the pub entry has offsets within the CU there's no real offset that can be 1351 // provided anyway). As it happens all such entities (namespaces and types, 1352 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out 1353 // not to be true it would be necessary to persist this information from the 1354 // point at which the entry is added to the index data structure - since by 1355 // the time the index is built from that, the original type/namespace DIE in a 1356 // type unit has already been destroyed so it can't be queried for properties 1357 // like tag, etc. 1358 if (Die->getTag() == dwarf::DW_TAG_compile_unit) 1359 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, 1360 dwarf::GIEL_EXTERNAL); 1361 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; 1362 1363 // We could have a specification DIE that has our most of our knowledge, 1364 // look for that now. 1365 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { 1366 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); 1367 if (SpecDIE.findAttribute(dwarf::DW_AT_external)) 1368 Linkage = dwarf::GIEL_EXTERNAL; 1369 } else if (Die->findAttribute(dwarf::DW_AT_external)) 1370 Linkage = dwarf::GIEL_EXTERNAL; 1371 1372 switch (Die->getTag()) { 1373 case dwarf::DW_TAG_class_type: 1374 case dwarf::DW_TAG_structure_type: 1375 case dwarf::DW_TAG_union_type: 1376 case dwarf::DW_TAG_enumeration_type: 1377 return dwarf::PubIndexEntryDescriptor( 1378 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus 1379 ? dwarf::GIEL_STATIC 1380 : dwarf::GIEL_EXTERNAL); 1381 case dwarf::DW_TAG_typedef: 1382 case dwarf::DW_TAG_base_type: 1383 case dwarf::DW_TAG_subrange_type: 1384 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); 1385 case dwarf::DW_TAG_namespace: 1386 return dwarf::GIEK_TYPE; 1387 case dwarf::DW_TAG_subprogram: 1388 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); 1389 case dwarf::DW_TAG_variable: 1390 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); 1391 case dwarf::DW_TAG_enumerator: 1392 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, 1393 dwarf::GIEL_STATIC); 1394 default: 1395 return dwarf::GIEK_NONE; 1396 } 1397 } 1398 1399 /// emitDebugPubNames - Emit visible names into a debug pubnames section. 1400 /// 1401 void DwarfDebug::emitDebugPubNames(bool GnuStyle) { 1402 MCSection *PSec = GnuStyle 1403 ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() 1404 : Asm->getObjFileLowering().getDwarfPubNamesSection(); 1405 1406 emitDebugPubSection(GnuStyle, PSec, "Names", 1407 &DwarfCompileUnit::getGlobalNames); 1408 } 1409 1410 void DwarfDebug::emitDebugPubSection( 1411 bool GnuStyle, MCSection *PSec, StringRef Name, 1412 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) { 1413 for (const auto &NU : CUMap) { 1414 DwarfCompileUnit *TheU = NU.second; 1415 1416 const auto &Globals = (TheU->*Accessor)(); 1417 1418 if (!hasDwarfPubSections(TheU->includeMinimalInlineScopes())) 1419 continue; 1420 1421 if (auto *Skeleton = TheU->getSkeleton()) 1422 TheU = Skeleton; 1423 1424 // Start the dwarf pubnames section. 1425 Asm->OutStreamer->SwitchSection(PSec); 1426 1427 // Emit the header. 1428 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info"); 1429 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin"); 1430 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end"); 1431 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); 1432 1433 Asm->OutStreamer->EmitLabel(BeginLabel); 1434 1435 Asm->OutStreamer->AddComment("DWARF Version"); 1436 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); 1437 1438 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); 1439 Asm->emitDwarfSymbolReference(TheU->getLabelBegin()); 1440 1441 Asm->OutStreamer->AddComment("Compilation Unit Length"); 1442 Asm->EmitInt32(TheU->getLength()); 1443 1444 // Emit the pubnames for this compilation unit. 1445 for (const auto &GI : Globals) { 1446 const char *Name = GI.getKeyData(); 1447 const DIE *Entity = GI.second; 1448 1449 Asm->OutStreamer->AddComment("DIE offset"); 1450 Asm->EmitInt32(Entity->getOffset()); 1451 1452 if (GnuStyle) { 1453 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); 1454 Asm->OutStreamer->AddComment( 1455 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " + 1456 dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); 1457 Asm->EmitInt8(Desc.toBits()); 1458 } 1459 1460 Asm->OutStreamer->AddComment("External Name"); 1461 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1)); 1462 } 1463 1464 Asm->OutStreamer->AddComment("End Mark"); 1465 Asm->EmitInt32(0); 1466 Asm->OutStreamer->EmitLabel(EndLabel); 1467 } 1468 } 1469 1470 void DwarfDebug::emitDebugPubTypes(bool GnuStyle) { 1471 MCSection *PSec = GnuStyle 1472 ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() 1473 : Asm->getObjFileLowering().getDwarfPubTypesSection(); 1474 1475 emitDebugPubSection(GnuStyle, PSec, "Types", 1476 &DwarfCompileUnit::getGlobalTypes); 1477 } 1478 1479 /// Emit null-terminated strings into a debug str section. 1480 void DwarfDebug::emitDebugStr() { 1481 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1482 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection()); 1483 } 1484 1485 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, 1486 const DebugLocStream::Entry &Entry) { 1487 auto &&Comments = DebugLocs.getComments(Entry); 1488 auto Comment = Comments.begin(); 1489 auto End = Comments.end(); 1490 for (uint8_t Byte : DebugLocs.getBytes(Entry)) 1491 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : ""); 1492 } 1493 1494 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, 1495 ByteStreamer &Streamer, 1496 const DebugLocEntry::Value &Value, 1497 DwarfExpression &DwarfExpr) { 1498 auto *DIExpr = Value.getExpression(); 1499 DIExpressionCursor ExprCursor(DIExpr); 1500 DwarfExpr.addFragmentOffset(DIExpr); 1501 // Regular entry. 1502 if (Value.isInt()) { 1503 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || 1504 BT->getEncoding() == dwarf::DW_ATE_signed_char)) 1505 DwarfExpr.addSignedConstant(Value.getInt()); 1506 else 1507 DwarfExpr.addUnsignedConstant(Value.getInt()); 1508 } else if (Value.isLocation()) { 1509 MachineLocation Location = Value.getLoc(); 1510 if (Location.isIndirect()) 1511 DwarfExpr.setMemoryLocationKind(); 1512 SmallVector<uint64_t, 8> Ops; 1513 if (Location.isIndirect() && Location.getOffset()) { 1514 Ops.push_back(dwarf::DW_OP_plus); 1515 Ops.push_back(Location.getOffset()); 1516 } 1517 Ops.append(DIExpr->elements_begin(), DIExpr->elements_end()); 1518 DIExpressionCursor Cursor(Ops); 1519 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); 1520 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 1521 return; 1522 return DwarfExpr.addExpression(std::move(Cursor)); 1523 } else if (Value.isConstantFP()) { 1524 APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt(); 1525 DwarfExpr.addUnsignedConstant(RawBytes); 1526 } 1527 DwarfExpr.addExpression(std::move(ExprCursor)); 1528 } 1529 1530 void DebugLocEntry::finalize(const AsmPrinter &AP, 1531 DebugLocStream::ListBuilder &List, 1532 const DIBasicType *BT) { 1533 DebugLocStream::EntryBuilder Entry(List, Begin, End); 1534 BufferByteStreamer Streamer = Entry.getStreamer(); 1535 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer); 1536 const DebugLocEntry::Value &Value = Values[0]; 1537 if (Value.isFragment()) { 1538 // Emit all fragments that belong to the same variable and range. 1539 assert(all_of(Values, [](DebugLocEntry::Value P) { 1540 return P.isFragment(); 1541 }) && "all values are expected to be fragments"); 1542 assert(std::is_sorted(Values.begin(), Values.end()) && 1543 "fragments are expected to be sorted"); 1544 1545 for (auto Fragment : Values) 1546 emitDebugLocValue(AP, BT, Streamer, Fragment, DwarfExpr); 1547 1548 } else { 1549 assert(Values.size() == 1 && "only fragments may have >1 value"); 1550 emitDebugLocValue(AP, BT, Streamer, Value, DwarfExpr); 1551 } 1552 DwarfExpr.finalize(); 1553 } 1554 1555 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) { 1556 // Emit the size. 1557 Asm->OutStreamer->AddComment("Loc expr size"); 1558 Asm->EmitInt16(DebugLocs.getBytes(Entry).size()); 1559 1560 // Emit the entry. 1561 APByteStreamer Streamer(*Asm); 1562 emitDebugLocEntry(Streamer, Entry); 1563 } 1564 1565 // Emit locations into the debug loc section. 1566 void DwarfDebug::emitDebugLoc() { 1567 if (DebugLocs.getLists().empty()) 1568 return; 1569 1570 // Start the dwarf loc section. 1571 Asm->OutStreamer->SwitchSection( 1572 Asm->getObjFileLowering().getDwarfLocSection()); 1573 unsigned char Size = Asm->MAI->getCodePointerSize(); 1574 for (const auto &List : DebugLocs.getLists()) { 1575 Asm->OutStreamer->EmitLabel(List.Label); 1576 const DwarfCompileUnit *CU = List.CU; 1577 for (const auto &Entry : DebugLocs.getEntries(List)) { 1578 // Set up the range. This range is relative to the entry point of the 1579 // compile unit. This is a hard coded 0 for low_pc when we're emitting 1580 // ranges, or the DW_AT_low_pc on the compile unit otherwise. 1581 if (auto *Base = CU->getBaseAddress()) { 1582 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size); 1583 Asm->EmitLabelDifference(Entry.EndSym, Base, Size); 1584 } else { 1585 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size); 1586 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size); 1587 } 1588 1589 emitDebugLocEntryLocation(Entry); 1590 } 1591 Asm->OutStreamer->EmitIntValue(0, Size); 1592 Asm->OutStreamer->EmitIntValue(0, Size); 1593 } 1594 } 1595 1596 void DwarfDebug::emitDebugLocDWO() { 1597 Asm->OutStreamer->SwitchSection( 1598 Asm->getObjFileLowering().getDwarfLocDWOSection()); 1599 for (const auto &List : DebugLocs.getLists()) { 1600 Asm->OutStreamer->EmitLabel(List.Label); 1601 for (const auto &Entry : DebugLocs.getEntries(List)) { 1602 // Just always use start_length for now - at least that's one address 1603 // rather than two. We could get fancier and try to, say, reuse an 1604 // address we know we've emitted elsewhere (the start of the function? 1605 // The start of the CU or CU subrange that encloses this range?) 1606 Asm->EmitInt8(dwarf::DW_LLE_startx_length); 1607 unsigned idx = AddrPool.getIndex(Entry.BeginSym); 1608 Asm->EmitULEB128(idx); 1609 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4); 1610 1611 emitDebugLocEntryLocation(Entry); 1612 } 1613 Asm->EmitInt8(dwarf::DW_LLE_end_of_list); 1614 } 1615 } 1616 1617 struct ArangeSpan { 1618 const MCSymbol *Start, *End; 1619 }; 1620 1621 // Emit a debug aranges section, containing a CU lookup for any 1622 // address we can tie back to a CU. 1623 void DwarfDebug::emitDebugARanges() { 1624 // Provides a unique id per text section. 1625 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; 1626 1627 // Filter labels by section. 1628 for (const SymbolCU &SCU : ArangeLabels) { 1629 if (SCU.Sym->isInSection()) { 1630 // Make a note of this symbol and it's section. 1631 MCSection *Section = &SCU.Sym->getSection(); 1632 if (!Section->getKind().isMetadata()) 1633 SectionMap[Section].push_back(SCU); 1634 } else { 1635 // Some symbols (e.g. common/bss on mach-o) can have no section but still 1636 // appear in the output. This sucks as we rely on sections to build 1637 // arange spans. We can do it without, but it's icky. 1638 SectionMap[nullptr].push_back(SCU); 1639 } 1640 } 1641 1642 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; 1643 1644 for (auto &I : SectionMap) { 1645 MCSection *Section = I.first; 1646 SmallVector<SymbolCU, 8> &List = I.second; 1647 if (List.size() < 1) 1648 continue; 1649 1650 // If we have no section (e.g. common), just write out 1651 // individual spans for each symbol. 1652 if (!Section) { 1653 for (const SymbolCU &Cur : List) { 1654 ArangeSpan Span; 1655 Span.Start = Cur.Sym; 1656 Span.End = nullptr; 1657 assert(Cur.CU); 1658 Spans[Cur.CU].push_back(Span); 1659 } 1660 continue; 1661 } 1662 1663 // Sort the symbols by offset within the section. 1664 std::sort( 1665 List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) { 1666 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0; 1667 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0; 1668 1669 // Symbols with no order assigned should be placed at the end. 1670 // (e.g. section end labels) 1671 if (IA == 0) 1672 return false; 1673 if (IB == 0) 1674 return true; 1675 return IA < IB; 1676 }); 1677 1678 // Insert a final terminator. 1679 List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); 1680 1681 // Build spans between each label. 1682 const MCSymbol *StartSym = List[0].Sym; 1683 for (size_t n = 1, e = List.size(); n < e; n++) { 1684 const SymbolCU &Prev = List[n - 1]; 1685 const SymbolCU &Cur = List[n]; 1686 1687 // Try and build the longest span we can within the same CU. 1688 if (Cur.CU != Prev.CU) { 1689 ArangeSpan Span; 1690 Span.Start = StartSym; 1691 Span.End = Cur.Sym; 1692 assert(Prev.CU); 1693 Spans[Prev.CU].push_back(Span); 1694 StartSym = Cur.Sym; 1695 } 1696 } 1697 } 1698 1699 // Start the dwarf aranges section. 1700 Asm->OutStreamer->SwitchSection( 1701 Asm->getObjFileLowering().getDwarfARangesSection()); 1702 1703 unsigned PtrSize = Asm->MAI->getCodePointerSize(); 1704 1705 // Build a list of CUs used. 1706 std::vector<DwarfCompileUnit *> CUs; 1707 for (const auto &it : Spans) { 1708 DwarfCompileUnit *CU = it.first; 1709 CUs.push_back(CU); 1710 } 1711 1712 // Sort the CU list (again, to ensure consistent output order). 1713 std::sort(CUs.begin(), CUs.end(), 1714 [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { 1715 return A->getUniqueID() < B->getUniqueID(); 1716 }); 1717 1718 // Emit an arange table for each CU we used. 1719 for (DwarfCompileUnit *CU : CUs) { 1720 std::vector<ArangeSpan> &List = Spans[CU]; 1721 1722 // Describe the skeleton CU's offset and length, not the dwo file's. 1723 if (auto *Skel = CU->getSkeleton()) 1724 CU = Skel; 1725 1726 // Emit size of content not including length itself. 1727 unsigned ContentSize = 1728 sizeof(int16_t) + // DWARF ARange version number 1729 sizeof(int32_t) + // Offset of CU in the .debug_info section 1730 sizeof(int8_t) + // Pointer Size (in bytes) 1731 sizeof(int8_t); // Segment Size (in bytes) 1732 1733 unsigned TupleSize = PtrSize * 2; 1734 1735 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. 1736 unsigned Padding = 1737 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize); 1738 1739 ContentSize += Padding; 1740 ContentSize += (List.size() + 1) * TupleSize; 1741 1742 // For each compile unit, write the list of spans it covers. 1743 Asm->OutStreamer->AddComment("Length of ARange Set"); 1744 Asm->EmitInt32(ContentSize); 1745 Asm->OutStreamer->AddComment("DWARF Arange version number"); 1746 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); 1747 Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); 1748 Asm->emitDwarfSymbolReference(CU->getLabelBegin()); 1749 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1750 Asm->EmitInt8(PtrSize); 1751 Asm->OutStreamer->AddComment("Segment Size (in bytes)"); 1752 Asm->EmitInt8(0); 1753 1754 Asm->OutStreamer->emitFill(Padding, 0xff); 1755 1756 for (const ArangeSpan &Span : List) { 1757 Asm->EmitLabelReference(Span.Start, PtrSize); 1758 1759 // Calculate the size as being from the span start to it's end. 1760 if (Span.End) { 1761 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize); 1762 } else { 1763 // For symbols without an end marker (e.g. common), we 1764 // write a single arange entry containing just that one symbol. 1765 uint64_t Size = SymSize[Span.Start]; 1766 if (Size == 0) 1767 Size = 1; 1768 1769 Asm->OutStreamer->EmitIntValue(Size, PtrSize); 1770 } 1771 } 1772 1773 Asm->OutStreamer->AddComment("ARange terminator"); 1774 Asm->OutStreamer->EmitIntValue(0, PtrSize); 1775 Asm->OutStreamer->EmitIntValue(0, PtrSize); 1776 } 1777 } 1778 1779 /// Emit address ranges into a debug ranges section. 1780 void DwarfDebug::emitDebugRanges() { 1781 if (CUMap.empty()) 1782 return; 1783 1784 // Start the dwarf ranges section. 1785 Asm->OutStreamer->SwitchSection( 1786 Asm->getObjFileLowering().getDwarfRangesSection()); 1787 1788 // Size for our labels. 1789 unsigned char Size = Asm->MAI->getCodePointerSize(); 1790 1791 // Grab the specific ranges for the compile units in the module. 1792 for (const auto &I : CUMap) { 1793 DwarfCompileUnit *TheCU = I.second; 1794 1795 if (auto *Skel = TheCU->getSkeleton()) 1796 TheCU = Skel; 1797 1798 // Iterate over the misc ranges for the compile units in the module. 1799 for (const RangeSpanList &List : TheCU->getRangeLists()) { 1800 // Emit our symbol so we can find the beginning of the range. 1801 Asm->OutStreamer->EmitLabel(List.getSym()); 1802 1803 for (const RangeSpan &Range : List.getRanges()) { 1804 const MCSymbol *Begin = Range.getStart(); 1805 const MCSymbol *End = Range.getEnd(); 1806 assert(Begin && "Range without a begin symbol?"); 1807 assert(End && "Range without an end symbol?"); 1808 if (auto *Base = TheCU->getBaseAddress()) { 1809 Asm->EmitLabelDifference(Begin, Base, Size); 1810 Asm->EmitLabelDifference(End, Base, Size); 1811 } else { 1812 Asm->OutStreamer->EmitSymbolValue(Begin, Size); 1813 Asm->OutStreamer->EmitSymbolValue(End, Size); 1814 } 1815 } 1816 1817 // And terminate the list with two 0 values. 1818 Asm->OutStreamer->EmitIntValue(0, Size); 1819 Asm->OutStreamer->EmitIntValue(0, Size); 1820 } 1821 } 1822 } 1823 1824 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { 1825 for (auto *MN : Nodes) { 1826 if (auto *M = dyn_cast<DIMacro>(MN)) 1827 emitMacro(*M); 1828 else if (auto *F = dyn_cast<DIMacroFile>(MN)) 1829 emitMacroFile(*F, U); 1830 else 1831 llvm_unreachable("Unexpected DI type!"); 1832 } 1833 } 1834 1835 void DwarfDebug::emitMacro(DIMacro &M) { 1836 Asm->EmitULEB128(M.getMacinfoType()); 1837 Asm->EmitULEB128(M.getLine()); 1838 StringRef Name = M.getName(); 1839 StringRef Value = M.getValue(); 1840 Asm->OutStreamer->EmitBytes(Name); 1841 if (!Value.empty()) { 1842 // There should be one space between macro name and macro value. 1843 Asm->EmitInt8(' '); 1844 Asm->OutStreamer->EmitBytes(Value); 1845 } 1846 Asm->EmitInt8('\0'); 1847 } 1848 1849 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { 1850 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); 1851 Asm->EmitULEB128(dwarf::DW_MACINFO_start_file); 1852 Asm->EmitULEB128(F.getLine()); 1853 DIFile *File = F.getFile(); 1854 unsigned FID = 1855 U.getOrCreateSourceID(File->getFilename(), File->getDirectory()); 1856 Asm->EmitULEB128(FID); 1857 handleMacroNodes(F.getElements(), U); 1858 Asm->EmitULEB128(dwarf::DW_MACINFO_end_file); 1859 } 1860 1861 /// Emit macros into a debug macinfo section. 1862 void DwarfDebug::emitDebugMacinfo() { 1863 if (CUMap.empty()) 1864 return; 1865 1866 // Start the dwarf macinfo section. 1867 Asm->OutStreamer->SwitchSection( 1868 Asm->getObjFileLowering().getDwarfMacinfoSection()); 1869 1870 for (const auto &P : CUMap) { 1871 auto &TheCU = *P.second; 1872 auto *SkCU = TheCU.getSkeleton(); 1873 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 1874 auto *CUNode = cast<DICompileUnit>(P.first); 1875 Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin()); 1876 handleMacroNodes(CUNode->getMacros(), U); 1877 } 1878 Asm->OutStreamer->AddComment("End Of Macro List Mark"); 1879 Asm->EmitInt8(0); 1880 } 1881 1882 // DWARF5 Experimental Separate Dwarf emitters. 1883 1884 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, 1885 std::unique_ptr<DwarfCompileUnit> NewU) { 1886 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name, 1887 Asm->TM.Options.MCOptions.SplitDwarfFile); 1888 1889 if (!CompilationDir.empty()) 1890 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 1891 1892 addGnuPubAttributes(*NewU, Die); 1893 1894 SkeletonHolder.addUnit(std::move(NewU)); 1895 } 1896 1897 // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list, 1898 // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id, 1899 // DW_AT_addr_base, DW_AT_ranges_base. 1900 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { 1901 1902 auto OwnedUnit = make_unique<DwarfCompileUnit>( 1903 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder); 1904 DwarfCompileUnit &NewCU = *OwnedUnit; 1905 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 1906 1907 NewCU.initStmtList(); 1908 1909 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); 1910 1911 return NewCU; 1912 } 1913 1914 // Emit the .debug_info.dwo section for separated dwarf. This contains the 1915 // compile units that would normally be in debug_info. 1916 void DwarfDebug::emitDebugInfoDWO() { 1917 assert(useSplitDwarf() && "No split dwarf debug info?"); 1918 // Don't emit relocations into the dwo file. 1919 InfoHolder.emitUnits(/* UseOffsets */ true); 1920 } 1921 1922 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the 1923 // abbreviations for the .debug_info.dwo section. 1924 void DwarfDebug::emitDebugAbbrevDWO() { 1925 assert(useSplitDwarf() && "No split dwarf?"); 1926 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); 1927 } 1928 1929 void DwarfDebug::emitDebugLineDWO() { 1930 assert(useSplitDwarf() && "No split dwarf?"); 1931 Asm->OutStreamer->SwitchSection( 1932 Asm->getObjFileLowering().getDwarfLineDWOSection()); 1933 SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams()); 1934 } 1935 1936 // Emit the .debug_str.dwo section for separated dwarf. This contains the 1937 // string section and is identical in format to traditional .debug_str 1938 // sections. 1939 void DwarfDebug::emitDebugStrDWO() { 1940 assert(useSplitDwarf() && "No split dwarf?"); 1941 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); 1942 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), 1943 OffSec); 1944 } 1945 1946 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { 1947 if (!useSplitDwarf()) 1948 return nullptr; 1949 if (SingleCU) 1950 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory()); 1951 return &SplitTypeUnitFileTable; 1952 } 1953 1954 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { 1955 MD5 Hash; 1956 Hash.update(Identifier); 1957 // ... take the least significant 8 bytes and return those. Our MD5 1958 // implementation always returns its results in little endian, so we actually 1959 // need the "high" word. 1960 MD5::MD5Result Result; 1961 Hash.final(Result); 1962 return Result.high(); 1963 } 1964 1965 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, 1966 StringRef Identifier, DIE &RefDie, 1967 const DICompositeType *CTy) { 1968 // Fast path if we're building some type units and one has already used the 1969 // address pool we know we're going to throw away all this work anyway, so 1970 // don't bother building dependent types. 1971 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) 1972 return; 1973 1974 auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); 1975 if (!Ins.second) { 1976 CU.addDIETypeSignature(RefDie, Ins.first->second); 1977 return; 1978 } 1979 1980 bool TopLevelType = TypeUnitsUnderConstruction.empty(); 1981 AddrPool.resetUsedFlag(); 1982 1983 auto OwnedUnit = make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, 1984 getDwoLineTable(CU)); 1985 DwarfTypeUnit &NewTU = *OwnedUnit; 1986 DIE &UnitDie = NewTU.getUnitDie(); 1987 TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); 1988 1989 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 1990 CU.getLanguage()); 1991 1992 uint64_t Signature = makeTypeSignature(Identifier); 1993 NewTU.setTypeSignature(Signature); 1994 Ins.first->second = Signature; 1995 1996 if (useSplitDwarf()) 1997 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection()); 1998 else { 1999 CU.applyStmtList(UnitDie); 2000 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature)); 2001 } 2002 2003 NewTU.setType(NewTU.createTypeDIE(CTy)); 2004 2005 if (TopLevelType) { 2006 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); 2007 TypeUnitsUnderConstruction.clear(); 2008 2009 // Types referencing entries in the address table cannot be placed in type 2010 // units. 2011 if (AddrPool.hasBeenUsed()) { 2012 2013 // Remove all the types built while building this type. 2014 // This is pessimistic as some of these types might not be dependent on 2015 // the type that used an address. 2016 for (const auto &TU : TypeUnitsToAdd) 2017 TypeSignatures.erase(TU.second); 2018 2019 // Construct this type in the CU directly. 2020 // This is inefficient because all the dependent types will be rebuilt 2021 // from scratch, including building them in type units, discovering that 2022 // they depend on addresses, throwing them out and rebuilding them. 2023 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); 2024 return; 2025 } 2026 2027 // If the type wasn't dependent on fission addresses, finish adding the type 2028 // and all its dependent types. 2029 for (auto &TU : TypeUnitsToAdd) { 2030 InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); 2031 InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); 2032 } 2033 } 2034 CU.addDIETypeSignature(RefDie, Signature); 2035 } 2036 2037 // Accelerator table mutators - add each name along with its companion 2038 // DIE to the proper table while ensuring that the name that we're going 2039 // to reference is in the string table. We do this since the names we 2040 // add may not only be identical to the names in the DIE. 2041 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) { 2042 if (!useDwarfAccelTables()) 2043 return; 2044 AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2045 } 2046 2047 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) { 2048 if (!useDwarfAccelTables()) 2049 return; 2050 AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2051 } 2052 2053 void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) { 2054 if (!useDwarfAccelTables()) 2055 return; 2056 AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2057 } 2058 2059 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) { 2060 if (!useDwarfAccelTables()) 2061 return; 2062 AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2063 } 2064 2065 uint16_t DwarfDebug::getDwarfVersion() const { 2066 return Asm->OutStreamer->getContext().getDwarfVersion(); 2067 } 2068