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