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 // There is no point in force-emitting a forward declaration. 548 CU.getOrCreateTypeDIE(RT); 549 } 550 // Emit imported_modules last so that the relevant context is already 551 // available. 552 for (auto *IE : CUNode->getImportedEntities()) 553 constructAndAddImportedEntityDIE(CU, IE); 554 } 555 } 556 557 void DwarfDebug::finishVariableDefinitions() { 558 for (const auto &Var : ConcreteVariables) { 559 DIE *VariableDie = Var->getDIE(); 560 assert(VariableDie); 561 // FIXME: Consider the time-space tradeoff of just storing the unit pointer 562 // in the ConcreteVariables list, rather than looking it up again here. 563 // DIE::getUnit isn't simple - it walks parent pointers, etc. 564 DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie()); 565 assert(Unit); 566 DbgVariable *AbsVar = getExistingAbstractVariable( 567 InlinedVariable(Var->getVariable(), Var->getInlinedAt())); 568 if (AbsVar && AbsVar->getDIE()) { 569 Unit->addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin, 570 *AbsVar->getDIE()); 571 } else 572 Unit->applyVariableAttributes(*Var, *VariableDie); 573 } 574 } 575 576 void DwarfDebug::finishSubprogramDefinitions() { 577 for (const DISubprogram *SP : ProcessedSPNodes) 578 if (SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug) 579 forBothCUs(*CUMap.lookup(SP->getUnit()), [&](DwarfCompileUnit &CU) { 580 CU.finishSubprogramDefinition(SP); 581 }); 582 } 583 584 void DwarfDebug::finalizeModuleInfo() { 585 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 586 587 finishSubprogramDefinitions(); 588 589 finishVariableDefinitions(); 590 591 // Handle anything that needs to be done on a per-unit basis after 592 // all other generation. 593 for (const auto &P : CUMap) { 594 auto &TheCU = *P.second; 595 // Emit DW_AT_containing_type attribute to connect types with their 596 // vtable holding type. 597 TheCU.constructContainingTypeDIEs(); 598 599 // Add CU specific attributes if we need to add any. 600 // If we're splitting the dwarf out now that we've got the entire 601 // CU then add the dwo id to it. 602 auto *SkCU = TheCU.getSkeleton(); 603 if (useSplitDwarf()) { 604 // Emit a unique identifier for this CU. 605 uint64_t ID = DIEHash(Asm).computeCUSignature(TheCU.getUnitDie()); 606 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 607 dwarf::DW_FORM_data8, ID); 608 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 609 dwarf::DW_FORM_data8, ID); 610 611 // We don't keep track of which addresses are used in which CU so this 612 // is a bit pessimistic under LTO. 613 if (!AddrPool.isEmpty()) { 614 const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol(); 615 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base, 616 Sym, Sym); 617 } 618 if (!SkCU->getRangeLists().empty()) { 619 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); 620 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, 621 Sym, Sym); 622 } 623 } 624 625 // If we have code split among multiple sections or non-contiguous 626 // ranges of code then emit a DW_AT_ranges attribute on the unit that will 627 // remain in the .o file, otherwise add a DW_AT_low_pc. 628 // FIXME: We should use ranges allow reordering of code ala 629 // .subsections_via_symbols in mach-o. This would mean turning on 630 // ranges for all subprogram DIEs for mach-o. 631 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 632 if (unsigned NumRanges = TheCU.getRanges().size()) { 633 if (NumRanges > 1) 634 // A DW_AT_low_pc attribute may also be specified in combination with 635 // DW_AT_ranges to specify the default base address for use in 636 // location lists (see Section 2.6.2) and range lists (see Section 637 // 2.17.3). 638 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0); 639 else 640 U.setBaseAddress(TheCU.getRanges().front().getStart()); 641 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); 642 } 643 644 auto *CUNode = cast<DICompileUnit>(P.first); 645 // If compile Unit has macros, emit "DW_AT_macro_info" attribute. 646 if (CUNode->getMacros()) 647 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, 648 U.getMacroLabelBegin(), 649 TLOF.getDwarfMacinfoSection()->getBeginSymbol()); 650 } 651 652 // Compute DIE offsets and sizes. 653 InfoHolder.computeSizeAndOffsets(); 654 if (useSplitDwarf()) 655 SkeletonHolder.computeSizeAndOffsets(); 656 } 657 658 // Emit all Dwarf sections that should come after the content. 659 void DwarfDebug::endModule() { 660 assert(CurFn == nullptr); 661 assert(CurMI == nullptr); 662 663 // If we aren't actually generating debug info (check beginModule - 664 // conditionalized on !DisableDebugInfoPrinting and the presence of the 665 // llvm.dbg.cu metadata node) 666 if (!MMI->hasDebugInfo()) 667 return; 668 669 // Finalize the debug info for the module. 670 finalizeModuleInfo(); 671 672 emitDebugStr(); 673 674 if (useSplitDwarf()) 675 emitDebugLocDWO(); 676 else 677 // Emit info into a debug loc section. 678 emitDebugLoc(); 679 680 // Corresponding abbreviations into a abbrev section. 681 emitAbbreviations(); 682 683 // Emit all the DIEs into a debug info section. 684 emitDebugInfo(); 685 686 // Emit info into a debug aranges section. 687 if (GenerateARangeSection) 688 emitDebugARanges(); 689 690 // Emit info into a debug ranges section. 691 emitDebugRanges(); 692 693 // Emit info into a debug macinfo section. 694 emitDebugMacinfo(); 695 696 if (useSplitDwarf()) { 697 emitDebugStrDWO(); 698 emitDebugInfoDWO(); 699 emitDebugAbbrevDWO(); 700 emitDebugLineDWO(); 701 // Emit DWO addresses. 702 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); 703 } 704 705 // Emit info into the dwarf accelerator table sections. 706 if (useDwarfAccelTables()) { 707 emitAccelNames(); 708 emitAccelObjC(); 709 emitAccelNamespaces(); 710 emitAccelTypes(); 711 } 712 713 // Emit the pubnames and pubtypes sections if requested. 714 if (HasDwarfPubSections) { 715 emitDebugPubNames(GenerateGnuPubSections); 716 emitDebugPubTypes(GenerateGnuPubSections); 717 } 718 719 // clean up. 720 AbstractVariables.clear(); 721 } 722 723 // Find abstract variable, if any, associated with Var. 724 DbgVariable * 725 DwarfDebug::getExistingAbstractVariable(InlinedVariable IV, 726 const DILocalVariable *&Cleansed) { 727 // More then one inlined variable corresponds to one abstract variable. 728 Cleansed = IV.first; 729 auto I = AbstractVariables.find(Cleansed); 730 if (I != AbstractVariables.end()) 731 return I->second.get(); 732 return nullptr; 733 } 734 735 DbgVariable *DwarfDebug::getExistingAbstractVariable(InlinedVariable IV) { 736 const DILocalVariable *Cleansed; 737 return getExistingAbstractVariable(IV, Cleansed); 738 } 739 740 void DwarfDebug::createAbstractVariable(const DILocalVariable *Var, 741 LexicalScope *Scope) { 742 assert(Scope && Scope->isAbstractScope()); 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::beginFunctionImpl(const MachineFunction *MF) { 1141 CurFn = MF; 1142 1143 if (LScopes.empty()) 1144 return; 1145 1146 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function 1147 // belongs to so that we add to the correct per-cu line table in the 1148 // non-asm case. 1149 LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); 1150 // FnScope->getScopeNode() and DI->second should represent the same function, 1151 // though they may not be the same MDNode due to inline functions merged in 1152 // LTO where the debug info metadata still differs (either due to distinct 1153 // written differences - two versions of a linkonce_odr function 1154 // written/copied into two separate files, or some sub-optimal metadata that 1155 // isn't structurally identical (see: file path/name info from clang, which 1156 // includes the directory of the cpp file being built, even when the file name 1157 // is absolute (such as an <> lookup header))) 1158 auto *SP = cast<DISubprogram>(FnScope->getScopeNode()); 1159 DwarfCompileUnit *TheCU = CUMap.lookup(SP->getUnit()); 1160 if (!TheCU) { 1161 assert(SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug && 1162 "DICompileUnit missing from llvm.dbg.cu?"); 1163 return; 1164 } 1165 if (Asm->OutStreamer->hasRawTextSupport()) 1166 // Use a single line table if we are generating assembly. 1167 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1168 else 1169 Asm->OutStreamer->getContext().setDwarfCompileUnitID(TheCU->getUniqueID()); 1170 1171 // Record beginning of function. 1172 PrologEndLoc = findPrologueEndLoc(MF); 1173 if (DILocation *L = PrologEndLoc) { 1174 // We'd like to list the prologue as "not statements" but GDB behaves 1175 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. 1176 auto *SP = L->getInlinedAtScope()->getSubprogram(); 1177 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT); 1178 } 1179 } 1180 1181 void DwarfDebug::skippedNonDebugFunction() { 1182 // If we don't have a subprogram for this function then there will be a hole 1183 // in the range information. Keep note of this by setting the previously used 1184 // section to nullptr. 1185 PrevCU = nullptr; 1186 CurFn = nullptr; 1187 } 1188 1189 // Gather and emit post-function debug information. 1190 void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { 1191 const DISubprogram *SP = MF->getFunction()->getSubprogram(); 1192 1193 assert(CurFn == MF && 1194 "endFunction should be called with the same function as beginFunction"); 1195 1196 // Set DwarfDwarfCompileUnitID in MCContext to default value. 1197 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1198 1199 LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); 1200 assert(!FnScope || SP == FnScope->getScopeNode()); 1201 DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); 1202 1203 DenseSet<InlinedVariable> ProcessedVars; 1204 collectVariableInfo(TheCU, SP, ProcessedVars); 1205 1206 // Add the range of this function to the list of ranges for the CU. 1207 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd())); 1208 1209 // Under -gmlt, skip building the subprogram if there are no inlined 1210 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram 1211 // is still needed as we need its source location. 1212 if (!TheCU.getCUNode()->getDebugInfoForProfiling() && 1213 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && 1214 LScopes.getAbstractScopesList().empty() && !IsDarwin) { 1215 assert(InfoHolder.getScopeVariables().empty()); 1216 PrevLabel = nullptr; 1217 CurFn = nullptr; 1218 return; 1219 } 1220 1221 #ifndef NDEBUG 1222 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); 1223 #endif 1224 // Construct abstract scopes. 1225 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { 1226 auto *SP = cast<DISubprogram>(AScope->getScopeNode()); 1227 // Collect info for variables that were optimized out. 1228 for (const DILocalVariable *DV : SP->getVariables()) { 1229 if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second) 1230 continue; 1231 ensureAbstractVariableIsCreated(InlinedVariable(DV, nullptr), 1232 DV->getScope()); 1233 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes 1234 && "ensureAbstractVariableIsCreated inserted abstract scopes"); 1235 } 1236 constructAbstractSubprogramScopeDIE(AScope); 1237 } 1238 1239 ProcessedSPNodes.insert(SP); 1240 TheCU.constructSubprogramScopeDIE(SP, FnScope); 1241 if (auto *SkelCU = TheCU.getSkeleton()) 1242 if (!LScopes.getAbstractScopesList().empty() && 1243 TheCU.getCUNode()->getSplitDebugInlining()) 1244 SkelCU->constructSubprogramScopeDIE(SP, FnScope); 1245 1246 // Clear debug info 1247 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the 1248 // DbgVariables except those that are also in AbstractVariables (since they 1249 // can be used cross-function) 1250 InfoHolder.getScopeVariables().clear(); 1251 PrevLabel = nullptr; 1252 CurFn = nullptr; 1253 } 1254 1255 // Register a source line with debug info. Returns the unique label that was 1256 // emitted and which provides correspondence to the source line list. 1257 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, 1258 unsigned Flags) { 1259 StringRef Fn; 1260 StringRef Dir; 1261 unsigned Src = 1; 1262 unsigned Discriminator = 0; 1263 if (auto *Scope = cast_or_null<DIScope>(S)) { 1264 Fn = Scope->getFilename(); 1265 Dir = Scope->getDirectory(); 1266 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) 1267 if (getDwarfVersion() >= 4) 1268 Discriminator = LBF->getDiscriminator(); 1269 1270 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID(); 1271 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID]) 1272 .getOrCreateSourceID(Fn, Dir); 1273 } 1274 Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 1275 Discriminator, Fn); 1276 } 1277 1278 //===----------------------------------------------------------------------===// 1279 // Emit Methods 1280 //===----------------------------------------------------------------------===// 1281 1282 // Emit the debug info section. 1283 void DwarfDebug::emitDebugInfo() { 1284 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1285 Holder.emitUnits(/* UseOffsets */ false); 1286 } 1287 1288 // Emit the abbreviation section. 1289 void DwarfDebug::emitAbbreviations() { 1290 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1291 1292 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); 1293 } 1294 1295 void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section, 1296 StringRef TableName) { 1297 Accel.FinalizeTable(Asm, TableName); 1298 Asm->OutStreamer->SwitchSection(Section); 1299 1300 // Emit the full data. 1301 Accel.emit(Asm, Section->getBeginSymbol(), this); 1302 } 1303 1304 // Emit visible names into a hashed accelerator table section. 1305 void DwarfDebug::emitAccelNames() { 1306 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), 1307 "Names"); 1308 } 1309 1310 // Emit objective C classes and categories into a hashed accelerator table 1311 // section. 1312 void DwarfDebug::emitAccelObjC() { 1313 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), 1314 "ObjC"); 1315 } 1316 1317 // Emit namespace dies into a hashed accelerator table. 1318 void DwarfDebug::emitAccelNamespaces() { 1319 emitAccel(AccelNamespace, 1320 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), 1321 "namespac"); 1322 } 1323 1324 // Emit type dies into a hashed accelerator table. 1325 void DwarfDebug::emitAccelTypes() { 1326 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), 1327 "types"); 1328 } 1329 1330 // Public name handling. 1331 // The format for the various pubnames: 1332 // 1333 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU 1334 // for the DIE that is named. 1335 // 1336 // gnu pubnames - offset/index value/name tuples where the offset is the offset 1337 // into the CU and the index value is computed according to the type of value 1338 // for the DIE that is named. 1339 // 1340 // For type units the offset is the offset of the skeleton DIE. For split dwarf 1341 // it's the offset within the debug_info/debug_types dwo section, however, the 1342 // reference in the pubname header doesn't change. 1343 1344 /// computeIndexValue - Compute the gdb index value for the DIE and CU. 1345 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, 1346 const DIE *Die) { 1347 // Entities that ended up only in a Type Unit reference the CU instead (since 1348 // the pub entry has offsets within the CU there's no real offset that can be 1349 // provided anyway). As it happens all such entities (namespaces and types, 1350 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out 1351 // not to be true it would be necessary to persist this information from the 1352 // point at which the entry is added to the index data structure - since by 1353 // the time the index is built from that, the original type/namespace DIE in a 1354 // type unit has already been destroyed so it can't be queried for properties 1355 // like tag, etc. 1356 if (Die->getTag() == dwarf::DW_TAG_compile_unit) 1357 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, 1358 dwarf::GIEL_EXTERNAL); 1359 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; 1360 1361 // We could have a specification DIE that has our most of our knowledge, 1362 // look for that now. 1363 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { 1364 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); 1365 if (SpecDIE.findAttribute(dwarf::DW_AT_external)) 1366 Linkage = dwarf::GIEL_EXTERNAL; 1367 } else if (Die->findAttribute(dwarf::DW_AT_external)) 1368 Linkage = dwarf::GIEL_EXTERNAL; 1369 1370 switch (Die->getTag()) { 1371 case dwarf::DW_TAG_class_type: 1372 case dwarf::DW_TAG_structure_type: 1373 case dwarf::DW_TAG_union_type: 1374 case dwarf::DW_TAG_enumeration_type: 1375 return dwarf::PubIndexEntryDescriptor( 1376 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus 1377 ? dwarf::GIEL_STATIC 1378 : dwarf::GIEL_EXTERNAL); 1379 case dwarf::DW_TAG_typedef: 1380 case dwarf::DW_TAG_base_type: 1381 case dwarf::DW_TAG_subrange_type: 1382 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); 1383 case dwarf::DW_TAG_namespace: 1384 return dwarf::GIEK_TYPE; 1385 case dwarf::DW_TAG_subprogram: 1386 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); 1387 case dwarf::DW_TAG_variable: 1388 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); 1389 case dwarf::DW_TAG_enumerator: 1390 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, 1391 dwarf::GIEL_STATIC); 1392 default: 1393 return dwarf::GIEK_NONE; 1394 } 1395 } 1396 1397 /// emitDebugPubNames - Emit visible names into a debug pubnames section. 1398 /// 1399 void DwarfDebug::emitDebugPubNames(bool GnuStyle) { 1400 MCSection *PSec = GnuStyle 1401 ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() 1402 : Asm->getObjFileLowering().getDwarfPubNamesSection(); 1403 1404 emitDebugPubSection(GnuStyle, PSec, "Names", 1405 &DwarfCompileUnit::getGlobalNames); 1406 } 1407 1408 void DwarfDebug::emitDebugPubSection( 1409 bool GnuStyle, MCSection *PSec, StringRef Name, 1410 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) { 1411 for (const auto &NU : CUMap) { 1412 DwarfCompileUnit *TheU = NU.second; 1413 1414 const auto &Globals = (TheU->*Accessor)(); 1415 1416 if (Globals.empty()) 1417 continue; 1418 1419 if (auto *Skeleton = TheU->getSkeleton()) 1420 TheU = Skeleton; 1421 1422 // Start the dwarf pubnames section. 1423 Asm->OutStreamer->SwitchSection(PSec); 1424 1425 // Emit the header. 1426 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info"); 1427 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin"); 1428 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end"); 1429 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); 1430 1431 Asm->OutStreamer->EmitLabel(BeginLabel); 1432 1433 Asm->OutStreamer->AddComment("DWARF Version"); 1434 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); 1435 1436 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); 1437 Asm->emitDwarfSymbolReference(TheU->getLabelBegin()); 1438 1439 Asm->OutStreamer->AddComment("Compilation Unit Length"); 1440 Asm->EmitInt32(TheU->getLength()); 1441 1442 // Emit the pubnames for this compilation unit. 1443 for (const auto &GI : Globals) { 1444 const char *Name = GI.getKeyData(); 1445 const DIE *Entity = GI.second; 1446 1447 Asm->OutStreamer->AddComment("DIE offset"); 1448 Asm->EmitInt32(Entity->getOffset()); 1449 1450 if (GnuStyle) { 1451 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); 1452 Asm->OutStreamer->AddComment( 1453 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " + 1454 dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); 1455 Asm->EmitInt8(Desc.toBits()); 1456 } 1457 1458 Asm->OutStreamer->AddComment("External Name"); 1459 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1)); 1460 } 1461 1462 Asm->OutStreamer->AddComment("End Mark"); 1463 Asm->EmitInt32(0); 1464 Asm->OutStreamer->EmitLabel(EndLabel); 1465 } 1466 } 1467 1468 void DwarfDebug::emitDebugPubTypes(bool GnuStyle) { 1469 MCSection *PSec = GnuStyle 1470 ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() 1471 : Asm->getObjFileLowering().getDwarfPubTypesSection(); 1472 1473 emitDebugPubSection(GnuStyle, PSec, "Types", 1474 &DwarfCompileUnit::getGlobalTypes); 1475 } 1476 1477 /// Emit null-terminated strings into a debug str section. 1478 void DwarfDebug::emitDebugStr() { 1479 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1480 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection()); 1481 } 1482 1483 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, 1484 const DebugLocStream::Entry &Entry) { 1485 auto &&Comments = DebugLocs.getComments(Entry); 1486 auto Comment = Comments.begin(); 1487 auto End = Comments.end(); 1488 for (uint8_t Byte : DebugLocs.getBytes(Entry)) 1489 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : ""); 1490 } 1491 1492 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, 1493 ByteStreamer &Streamer, 1494 const DebugLocEntry::Value &Value, 1495 DwarfExpression &DwarfExpr) { 1496 DIExpressionCursor ExprCursor(Value.getExpression()); 1497 DwarfExpr.addFragmentOffset(Value.getExpression()); 1498 // Regular entry. 1499 if (Value.isInt()) { 1500 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || 1501 BT->getEncoding() == dwarf::DW_ATE_signed_char)) 1502 DwarfExpr.AddSignedConstant(Value.getInt()); 1503 else 1504 DwarfExpr.AddUnsignedConstant(Value.getInt()); 1505 } else if (Value.isLocation()) { 1506 MachineLocation Loc = Value.getLoc(); 1507 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); 1508 if (Loc.getOffset()) 1509 DwarfExpr.AddMachineRegIndirect(TRI, Loc.getReg(), Loc.getOffset()); 1510 else 1511 DwarfExpr.AddMachineRegExpression(TRI, ExprCursor, Loc.getReg()); 1512 } else if (Value.isConstantFP()) { 1513 APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt(); 1514 DwarfExpr.AddUnsignedConstant(RawBytes); 1515 } 1516 DwarfExpr.AddExpression(std::move(ExprCursor)); 1517 } 1518 1519 void DebugLocEntry::finalize(const AsmPrinter &AP, 1520 DebugLocStream::ListBuilder &List, 1521 const DIBasicType *BT) { 1522 DebugLocStream::EntryBuilder Entry(List, Begin, End); 1523 BufferByteStreamer Streamer = Entry.getStreamer(); 1524 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer); 1525 const DebugLocEntry::Value &Value = Values[0]; 1526 if (Value.isFragment()) { 1527 // Emit all fragments that belong to the same variable and range. 1528 assert(all_of(Values, [](DebugLocEntry::Value P) { 1529 return P.isFragment(); 1530 }) && "all values are expected to be fragments"); 1531 assert(std::is_sorted(Values.begin(), Values.end()) && 1532 "fragments are expected to be sorted"); 1533 1534 for (auto Fragment : Values) 1535 emitDebugLocValue(AP, BT, Streamer, Fragment, DwarfExpr); 1536 1537 } else { 1538 assert(Values.size() == 1 && "only fragments may have >1 value"); 1539 emitDebugLocValue(AP, BT, Streamer, Value, DwarfExpr); 1540 } 1541 DwarfExpr.finalize(); 1542 } 1543 1544 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) { 1545 // Emit the size. 1546 Asm->OutStreamer->AddComment("Loc expr size"); 1547 Asm->EmitInt16(DebugLocs.getBytes(Entry).size()); 1548 1549 // Emit the entry. 1550 APByteStreamer Streamer(*Asm); 1551 emitDebugLocEntry(Streamer, Entry); 1552 } 1553 1554 // Emit locations into the debug loc section. 1555 void DwarfDebug::emitDebugLoc() { 1556 // Start the dwarf loc section. 1557 Asm->OutStreamer->SwitchSection( 1558 Asm->getObjFileLowering().getDwarfLocSection()); 1559 unsigned char Size = Asm->getDataLayout().getPointerSize(); 1560 for (const auto &List : DebugLocs.getLists()) { 1561 Asm->OutStreamer->EmitLabel(List.Label); 1562 const DwarfCompileUnit *CU = List.CU; 1563 for (const auto &Entry : DebugLocs.getEntries(List)) { 1564 // Set up the range. This range is relative to the entry point of the 1565 // compile unit. This is a hard coded 0 for low_pc when we're emitting 1566 // ranges, or the DW_AT_low_pc on the compile unit otherwise. 1567 if (auto *Base = CU->getBaseAddress()) { 1568 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size); 1569 Asm->EmitLabelDifference(Entry.EndSym, Base, Size); 1570 } else { 1571 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size); 1572 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size); 1573 } 1574 1575 emitDebugLocEntryLocation(Entry); 1576 } 1577 Asm->OutStreamer->EmitIntValue(0, Size); 1578 Asm->OutStreamer->EmitIntValue(0, Size); 1579 } 1580 } 1581 1582 void DwarfDebug::emitDebugLocDWO() { 1583 Asm->OutStreamer->SwitchSection( 1584 Asm->getObjFileLowering().getDwarfLocDWOSection()); 1585 for (const auto &List : DebugLocs.getLists()) { 1586 Asm->OutStreamer->EmitLabel(List.Label); 1587 for (const auto &Entry : DebugLocs.getEntries(List)) { 1588 // Just always use start_length for now - at least that's one address 1589 // rather than two. We could get fancier and try to, say, reuse an 1590 // address we know we've emitted elsewhere (the start of the function? 1591 // The start of the CU or CU subrange that encloses this range?) 1592 Asm->EmitInt8(dwarf::DW_LLE_startx_length); 1593 unsigned idx = AddrPool.getIndex(Entry.BeginSym); 1594 Asm->EmitULEB128(idx); 1595 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4); 1596 1597 emitDebugLocEntryLocation(Entry); 1598 } 1599 Asm->EmitInt8(dwarf::DW_LLE_end_of_list); 1600 } 1601 } 1602 1603 struct ArangeSpan { 1604 const MCSymbol *Start, *End; 1605 }; 1606 1607 // Emit a debug aranges section, containing a CU lookup for any 1608 // address we can tie back to a CU. 1609 void DwarfDebug::emitDebugARanges() { 1610 // Provides a unique id per text section. 1611 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; 1612 1613 // Filter labels by section. 1614 for (const SymbolCU &SCU : ArangeLabels) { 1615 if (SCU.Sym->isInSection()) { 1616 // Make a note of this symbol and it's section. 1617 MCSection *Section = &SCU.Sym->getSection(); 1618 if (!Section->getKind().isMetadata()) 1619 SectionMap[Section].push_back(SCU); 1620 } else { 1621 // Some symbols (e.g. common/bss on mach-o) can have no section but still 1622 // appear in the output. This sucks as we rely on sections to build 1623 // arange spans. We can do it without, but it's icky. 1624 SectionMap[nullptr].push_back(SCU); 1625 } 1626 } 1627 1628 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; 1629 1630 for (auto &I : SectionMap) { 1631 MCSection *Section = I.first; 1632 SmallVector<SymbolCU, 8> &List = I.second; 1633 if (List.size() < 1) 1634 continue; 1635 1636 // If we have no section (e.g. common), just write out 1637 // individual spans for each symbol. 1638 if (!Section) { 1639 for (const SymbolCU &Cur : List) { 1640 ArangeSpan Span; 1641 Span.Start = Cur.Sym; 1642 Span.End = nullptr; 1643 assert(Cur.CU); 1644 Spans[Cur.CU].push_back(Span); 1645 } 1646 continue; 1647 } 1648 1649 // Sort the symbols by offset within the section. 1650 std::sort( 1651 List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) { 1652 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0; 1653 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0; 1654 1655 // Symbols with no order assigned should be placed at the end. 1656 // (e.g. section end labels) 1657 if (IA == 0) 1658 return false; 1659 if (IB == 0) 1660 return true; 1661 return IA < IB; 1662 }); 1663 1664 // Insert a final terminator. 1665 List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); 1666 1667 // Build spans between each label. 1668 const MCSymbol *StartSym = List[0].Sym; 1669 for (size_t n = 1, e = List.size(); n < e; n++) { 1670 const SymbolCU &Prev = List[n - 1]; 1671 const SymbolCU &Cur = List[n]; 1672 1673 // Try and build the longest span we can within the same CU. 1674 if (Cur.CU != Prev.CU) { 1675 ArangeSpan Span; 1676 Span.Start = StartSym; 1677 Span.End = Cur.Sym; 1678 assert(Prev.CU); 1679 Spans[Prev.CU].push_back(Span); 1680 StartSym = Cur.Sym; 1681 } 1682 } 1683 } 1684 1685 // Start the dwarf aranges section. 1686 Asm->OutStreamer->SwitchSection( 1687 Asm->getObjFileLowering().getDwarfARangesSection()); 1688 1689 unsigned PtrSize = Asm->getDataLayout().getPointerSize(); 1690 1691 // Build a list of CUs used. 1692 std::vector<DwarfCompileUnit *> CUs; 1693 for (const auto &it : Spans) { 1694 DwarfCompileUnit *CU = it.first; 1695 CUs.push_back(CU); 1696 } 1697 1698 // Sort the CU list (again, to ensure consistent output order). 1699 std::sort(CUs.begin(), CUs.end(), 1700 [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { 1701 return A->getUniqueID() < B->getUniqueID(); 1702 }); 1703 1704 // Emit an arange table for each CU we used. 1705 for (DwarfCompileUnit *CU : CUs) { 1706 std::vector<ArangeSpan> &List = Spans[CU]; 1707 1708 // Describe the skeleton CU's offset and length, not the dwo file's. 1709 if (auto *Skel = CU->getSkeleton()) 1710 CU = Skel; 1711 1712 // Emit size of content not including length itself. 1713 unsigned ContentSize = 1714 sizeof(int16_t) + // DWARF ARange version number 1715 sizeof(int32_t) + // Offset of CU in the .debug_info section 1716 sizeof(int8_t) + // Pointer Size (in bytes) 1717 sizeof(int8_t); // Segment Size (in bytes) 1718 1719 unsigned TupleSize = PtrSize * 2; 1720 1721 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. 1722 unsigned Padding = 1723 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize); 1724 1725 ContentSize += Padding; 1726 ContentSize += (List.size() + 1) * TupleSize; 1727 1728 // For each compile unit, write the list of spans it covers. 1729 Asm->OutStreamer->AddComment("Length of ARange Set"); 1730 Asm->EmitInt32(ContentSize); 1731 Asm->OutStreamer->AddComment("DWARF Arange version number"); 1732 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); 1733 Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); 1734 Asm->emitDwarfSymbolReference(CU->getLabelBegin()); 1735 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1736 Asm->EmitInt8(PtrSize); 1737 Asm->OutStreamer->AddComment("Segment Size (in bytes)"); 1738 Asm->EmitInt8(0); 1739 1740 Asm->OutStreamer->emitFill(Padding, 0xff); 1741 1742 for (const ArangeSpan &Span : List) { 1743 Asm->EmitLabelReference(Span.Start, PtrSize); 1744 1745 // Calculate the size as being from the span start to it's end. 1746 if (Span.End) { 1747 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize); 1748 } else { 1749 // For symbols without an end marker (e.g. common), we 1750 // write a single arange entry containing just that one symbol. 1751 uint64_t Size = SymSize[Span.Start]; 1752 if (Size == 0) 1753 Size = 1; 1754 1755 Asm->OutStreamer->EmitIntValue(Size, PtrSize); 1756 } 1757 } 1758 1759 Asm->OutStreamer->AddComment("ARange terminator"); 1760 Asm->OutStreamer->EmitIntValue(0, PtrSize); 1761 Asm->OutStreamer->EmitIntValue(0, PtrSize); 1762 } 1763 } 1764 1765 /// Emit address ranges into a debug ranges section. 1766 void DwarfDebug::emitDebugRanges() { 1767 // Start the dwarf ranges section. 1768 Asm->OutStreamer->SwitchSection( 1769 Asm->getObjFileLowering().getDwarfRangesSection()); 1770 1771 // Size for our labels. 1772 unsigned char Size = Asm->getDataLayout().getPointerSize(); 1773 1774 // Grab the specific ranges for the compile units in the module. 1775 for (const auto &I : CUMap) { 1776 DwarfCompileUnit *TheCU = I.second; 1777 1778 if (auto *Skel = TheCU->getSkeleton()) 1779 TheCU = Skel; 1780 1781 // Iterate over the misc ranges for the compile units in the module. 1782 for (const RangeSpanList &List : TheCU->getRangeLists()) { 1783 // Emit our symbol so we can find the beginning of the range. 1784 Asm->OutStreamer->EmitLabel(List.getSym()); 1785 1786 for (const RangeSpan &Range : List.getRanges()) { 1787 const MCSymbol *Begin = Range.getStart(); 1788 const MCSymbol *End = Range.getEnd(); 1789 assert(Begin && "Range without a begin symbol?"); 1790 assert(End && "Range without an end symbol?"); 1791 if (auto *Base = TheCU->getBaseAddress()) { 1792 Asm->EmitLabelDifference(Begin, Base, Size); 1793 Asm->EmitLabelDifference(End, Base, Size); 1794 } else { 1795 Asm->OutStreamer->EmitSymbolValue(Begin, Size); 1796 Asm->OutStreamer->EmitSymbolValue(End, Size); 1797 } 1798 } 1799 1800 // And terminate the list with two 0 values. 1801 Asm->OutStreamer->EmitIntValue(0, Size); 1802 Asm->OutStreamer->EmitIntValue(0, Size); 1803 } 1804 } 1805 } 1806 1807 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { 1808 for (auto *MN : Nodes) { 1809 if (auto *M = dyn_cast<DIMacro>(MN)) 1810 emitMacro(*M); 1811 else if (auto *F = dyn_cast<DIMacroFile>(MN)) 1812 emitMacroFile(*F, U); 1813 else 1814 llvm_unreachable("Unexpected DI type!"); 1815 } 1816 } 1817 1818 void DwarfDebug::emitMacro(DIMacro &M) { 1819 Asm->EmitULEB128(M.getMacinfoType()); 1820 Asm->EmitULEB128(M.getLine()); 1821 StringRef Name = M.getName(); 1822 StringRef Value = M.getValue(); 1823 Asm->OutStreamer->EmitBytes(Name); 1824 if (!Value.empty()) { 1825 // There should be one space between macro name and macro value. 1826 Asm->EmitInt8(' '); 1827 Asm->OutStreamer->EmitBytes(Value); 1828 } 1829 Asm->EmitInt8('\0'); 1830 } 1831 1832 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { 1833 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); 1834 Asm->EmitULEB128(dwarf::DW_MACINFO_start_file); 1835 Asm->EmitULEB128(F.getLine()); 1836 DIFile *File = F.getFile(); 1837 unsigned FID = 1838 U.getOrCreateSourceID(File->getFilename(), File->getDirectory()); 1839 Asm->EmitULEB128(FID); 1840 handleMacroNodes(F.getElements(), U); 1841 Asm->EmitULEB128(dwarf::DW_MACINFO_end_file); 1842 } 1843 1844 /// Emit macros into a debug macinfo section. 1845 void DwarfDebug::emitDebugMacinfo() { 1846 // Start the dwarf macinfo section. 1847 Asm->OutStreamer->SwitchSection( 1848 Asm->getObjFileLowering().getDwarfMacinfoSection()); 1849 1850 for (const auto &P : CUMap) { 1851 auto &TheCU = *P.second; 1852 auto *SkCU = TheCU.getSkeleton(); 1853 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 1854 auto *CUNode = cast<DICompileUnit>(P.first); 1855 Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin()); 1856 handleMacroNodes(CUNode->getMacros(), U); 1857 } 1858 Asm->OutStreamer->AddComment("End Of Macro List Mark"); 1859 Asm->EmitInt8(0); 1860 } 1861 1862 // DWARF5 Experimental Separate Dwarf emitters. 1863 1864 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, 1865 std::unique_ptr<DwarfCompileUnit> NewU) { 1866 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name, 1867 U.getCUNode()->getSplitDebugFilename()); 1868 1869 if (!CompilationDir.empty()) 1870 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 1871 1872 addGnuPubAttributes(*NewU, Die); 1873 1874 SkeletonHolder.addUnit(std::move(NewU)); 1875 } 1876 1877 // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list, 1878 // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id, 1879 // DW_AT_addr_base, DW_AT_ranges_base. 1880 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { 1881 1882 auto OwnedUnit = make_unique<DwarfCompileUnit>( 1883 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder); 1884 DwarfCompileUnit &NewCU = *OwnedUnit; 1885 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 1886 1887 NewCU.initStmtList(); 1888 1889 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); 1890 1891 return NewCU; 1892 } 1893 1894 // Emit the .debug_info.dwo section for separated dwarf. This contains the 1895 // compile units that would normally be in debug_info. 1896 void DwarfDebug::emitDebugInfoDWO() { 1897 assert(useSplitDwarf() && "No split dwarf debug info?"); 1898 // Don't emit relocations into the dwo file. 1899 InfoHolder.emitUnits(/* UseOffsets */ true); 1900 } 1901 1902 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the 1903 // abbreviations for the .debug_info.dwo section. 1904 void DwarfDebug::emitDebugAbbrevDWO() { 1905 assert(useSplitDwarf() && "No split dwarf?"); 1906 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); 1907 } 1908 1909 void DwarfDebug::emitDebugLineDWO() { 1910 assert(useSplitDwarf() && "No split dwarf?"); 1911 Asm->OutStreamer->SwitchSection( 1912 Asm->getObjFileLowering().getDwarfLineDWOSection()); 1913 SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams()); 1914 } 1915 1916 // Emit the .debug_str.dwo section for separated dwarf. This contains the 1917 // string section and is identical in format to traditional .debug_str 1918 // sections. 1919 void DwarfDebug::emitDebugStrDWO() { 1920 assert(useSplitDwarf() && "No split dwarf?"); 1921 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); 1922 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), 1923 OffSec); 1924 } 1925 1926 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { 1927 if (!useSplitDwarf()) 1928 return nullptr; 1929 if (SingleCU) 1930 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory()); 1931 return &SplitTypeUnitFileTable; 1932 } 1933 1934 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { 1935 MD5 Hash; 1936 Hash.update(Identifier); 1937 // ... take the least significant 8 bytes and return those. Our MD5 1938 // implementation always returns its results in little endian, swap bytes 1939 // appropriately. 1940 MD5::MD5Result Result; 1941 Hash.final(Result); 1942 return support::endian::read64le(Result + 8); 1943 } 1944 1945 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, 1946 StringRef Identifier, DIE &RefDie, 1947 const DICompositeType *CTy) { 1948 // Fast path if we're building some type units and one has already used the 1949 // address pool we know we're going to throw away all this work anyway, so 1950 // don't bother building dependent types. 1951 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) 1952 return; 1953 1954 auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); 1955 if (!Ins.second) { 1956 CU.addDIETypeSignature(RefDie, Ins.first->second); 1957 return; 1958 } 1959 1960 bool TopLevelType = TypeUnitsUnderConstruction.empty(); 1961 AddrPool.resetUsedFlag(); 1962 1963 auto OwnedUnit = make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, 1964 getDwoLineTable(CU)); 1965 DwarfTypeUnit &NewTU = *OwnedUnit; 1966 DIE &UnitDie = NewTU.getUnitDie(); 1967 TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); 1968 1969 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 1970 CU.getLanguage()); 1971 1972 uint64_t Signature = makeTypeSignature(Identifier); 1973 NewTU.setTypeSignature(Signature); 1974 Ins.first->second = Signature; 1975 1976 if (useSplitDwarf()) 1977 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection()); 1978 else { 1979 CU.applyStmtList(UnitDie); 1980 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature)); 1981 } 1982 1983 NewTU.setType(NewTU.createTypeDIE(CTy)); 1984 1985 if (TopLevelType) { 1986 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); 1987 TypeUnitsUnderConstruction.clear(); 1988 1989 // Types referencing entries in the address table cannot be placed in type 1990 // units. 1991 if (AddrPool.hasBeenUsed()) { 1992 1993 // Remove all the types built while building this type. 1994 // This is pessimistic as some of these types might not be dependent on 1995 // the type that used an address. 1996 for (const auto &TU : TypeUnitsToAdd) 1997 TypeSignatures.erase(TU.second); 1998 1999 // Construct this type in the CU directly. 2000 // This is inefficient because all the dependent types will be rebuilt 2001 // from scratch, including building them in type units, discovering that 2002 // they depend on addresses, throwing them out and rebuilding them. 2003 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); 2004 return; 2005 } 2006 2007 // If the type wasn't dependent on fission addresses, finish adding the type 2008 // and all its dependent types. 2009 for (auto &TU : TypeUnitsToAdd) { 2010 InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); 2011 InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); 2012 } 2013 } 2014 CU.addDIETypeSignature(RefDie, Signature); 2015 } 2016 2017 // Accelerator table mutators - add each name along with its companion 2018 // DIE to the proper table while ensuring that the name that we're going 2019 // to reference is in the string table. We do this since the names we 2020 // add may not only be identical to the names in the DIE. 2021 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) { 2022 if (!useDwarfAccelTables()) 2023 return; 2024 AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2025 } 2026 2027 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) { 2028 if (!useDwarfAccelTables()) 2029 return; 2030 AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2031 } 2032 2033 void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) { 2034 if (!useDwarfAccelTables()) 2035 return; 2036 AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2037 } 2038 2039 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) { 2040 if (!useDwarfAccelTables()) 2041 return; 2042 AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); 2043 } 2044 2045 uint16_t DwarfDebug::getDwarfVersion() const { 2046 return Asm->OutStreamer->getContext().getDwarfVersion(); 2047 } 2048