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