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