1 //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains support for writing dwarf debug info into asm files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "DwarfDebug.h" 14 #include "ByteStreamer.h" 15 #include "DIEHash.h" 16 #include "DebugLocEntry.h" 17 #include "DebugLocStream.h" 18 #include "DwarfCompileUnit.h" 19 #include "DwarfExpression.h" 20 #include "DwarfFile.h" 21 #include "DwarfUnit.h" 22 #include "llvm/ADT/APInt.h" 23 #include "llvm/ADT/DenseMap.h" 24 #include "llvm/ADT/DenseSet.h" 25 #include "llvm/ADT/MapVector.h" 26 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/ADT/SmallVector.h" 28 #include "llvm/ADT/StringRef.h" 29 #include "llvm/ADT/Statistic.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/TargetInstrInfo.h" 43 #include "llvm/CodeGen/TargetLowering.h" 44 #include "llvm/CodeGen/TargetRegisterInfo.h" 45 #include "llvm/CodeGen/TargetSubtargetInfo.h" 46 #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 47 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" 48 #include "llvm/IR/Constants.h" 49 #include "llvm/IR/DebugInfoMetadata.h" 50 #include "llvm/IR/DebugLoc.h" 51 #include "llvm/IR/Function.h" 52 #include "llvm/IR/GlobalVariable.h" 53 #include "llvm/IR/Module.h" 54 #include "llvm/MC/MCAsmInfo.h" 55 #include "llvm/MC/MCContext.h" 56 #include "llvm/MC/MCDwarf.h" 57 #include "llvm/MC/MCSection.h" 58 #include "llvm/MC/MCStreamer.h" 59 #include "llvm/MC/MCSymbol.h" 60 #include "llvm/MC/MCTargetOptions.h" 61 #include "llvm/MC/MachineLocation.h" 62 #include "llvm/MC/SectionKind.h" 63 #include "llvm/Pass.h" 64 #include "llvm/Support/Casting.h" 65 #include "llvm/Support/CommandLine.h" 66 #include "llvm/Support/Debug.h" 67 #include "llvm/Support/ErrorHandling.h" 68 #include "llvm/Support/MD5.h" 69 #include "llvm/Support/MathExtras.h" 70 #include "llvm/Support/Timer.h" 71 #include "llvm/Support/raw_ostream.h" 72 #include "llvm/Target/TargetLoweringObjectFile.h" 73 #include "llvm/Target/TargetMachine.h" 74 #include "llvm/Target/TargetOptions.h" 75 #include <algorithm> 76 #include <cassert> 77 #include <cstddef> 78 #include <cstdint> 79 #include <iterator> 80 #include <string> 81 #include <utility> 82 #include <vector> 83 84 using namespace llvm; 85 86 #define DEBUG_TYPE "dwarfdebug" 87 88 STATISTIC(NumCSParams, "Number of dbg call site params created"); 89 90 static cl::opt<bool> 91 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, 92 cl::desc("Disable debug info printing")); 93 94 static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier( 95 "use-dwarf-ranges-base-address-specifier", cl::Hidden, 96 cl::desc("Use base address specifiers in debug_ranges"), cl::init(false)); 97 98 static cl::opt<bool> GenerateARangeSection("generate-arange-section", 99 cl::Hidden, 100 cl::desc("Generate dwarf aranges"), 101 cl::init(false)); 102 103 static cl::opt<bool> 104 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, 105 cl::desc("Generate DWARF4 type units."), 106 cl::init(false)); 107 108 static cl::opt<bool> SplitDwarfCrossCuReferences( 109 "split-dwarf-cross-cu-references", cl::Hidden, 110 cl::desc("Enable cross-cu references in DWO files"), cl::init(false)); 111 112 enum DefaultOnOff { Default, Enable, Disable }; 113 114 static cl::opt<DefaultOnOff> UnknownLocations( 115 "use-unknown-locations", cl::Hidden, 116 cl::desc("Make an absence of debug location information explicit."), 117 cl::values(clEnumVal(Default, "At top of block or after label"), 118 clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")), 119 cl::init(Default)); 120 121 static cl::opt<AccelTableKind> AccelTables( 122 "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."), 123 cl::values(clEnumValN(AccelTableKind::Default, "Default", 124 "Default for platform"), 125 clEnumValN(AccelTableKind::None, "Disable", "Disabled."), 126 clEnumValN(AccelTableKind::Apple, "Apple", "Apple"), 127 clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")), 128 cl::init(AccelTableKind::Default)); 129 130 static cl::opt<DefaultOnOff> 131 DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden, 132 cl::desc("Use inlined strings rather than string section."), 133 cl::values(clEnumVal(Default, "Default for platform"), 134 clEnumVal(Enable, "Enabled"), 135 clEnumVal(Disable, "Disabled")), 136 cl::init(Default)); 137 138 static cl::opt<bool> 139 NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden, 140 cl::desc("Disable emission .debug_ranges section."), 141 cl::init(false)); 142 143 static cl::opt<DefaultOnOff> DwarfSectionsAsReferences( 144 "dwarf-sections-as-references", cl::Hidden, 145 cl::desc("Use sections+offset as references rather than labels."), 146 cl::values(clEnumVal(Default, "Default for platform"), 147 clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), 148 cl::init(Default)); 149 150 enum LinkageNameOption { 151 DefaultLinkageNames, 152 AllLinkageNames, 153 AbstractLinkageNames 154 }; 155 156 static cl::opt<LinkageNameOption> 157 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, 158 cl::desc("Which DWARF linkage-name attributes to emit."), 159 cl::values(clEnumValN(DefaultLinkageNames, "Default", 160 "Default for platform"), 161 clEnumValN(AllLinkageNames, "All", "All"), 162 clEnumValN(AbstractLinkageNames, "Abstract", 163 "Abstract subprograms")), 164 cl::init(DefaultLinkageNames)); 165 166 static const char *const DWARFGroupName = "dwarf"; 167 static const char *const DWARFGroupDescription = "DWARF Emission"; 168 static const char *const DbgTimerName = "writer"; 169 static const char *const DbgTimerDescription = "DWARF Debug Writer"; 170 static constexpr unsigned ULEB128PadSize = 4; 171 172 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) { 173 getActiveStreamer().EmitInt8( 174 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op) 175 : dwarf::OperationEncodingString(Op)); 176 } 177 178 void DebugLocDwarfExpression::emitSigned(int64_t Value) { 179 getActiveStreamer().emitSLEB128(Value, Twine(Value)); 180 } 181 182 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) { 183 getActiveStreamer().emitULEB128(Value, Twine(Value)); 184 } 185 186 void DebugLocDwarfExpression::emitData1(uint8_t Value) { 187 getActiveStreamer().EmitInt8(Value, Twine(Value)); 188 } 189 190 void DebugLocDwarfExpression::emitBaseTypeRef(uint64_t Idx) { 191 assert(Idx < (1ULL << (ULEB128PadSize * 7)) && "Idx wont fit"); 192 getActiveStreamer().emitULEB128(Idx, Twine(Idx), ULEB128PadSize); 193 } 194 195 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, 196 unsigned MachineReg) { 197 // This information is not available while emitting .debug_loc entries. 198 return false; 199 } 200 201 void DebugLocDwarfExpression::enableTemporaryBuffer() { 202 assert(!IsBuffering && "Already buffering?"); 203 if (!TmpBuf) 204 TmpBuf = std::make_unique<TempBuffer>(OutBS.GenerateComments); 205 IsBuffering = true; 206 } 207 208 void DebugLocDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; } 209 210 unsigned DebugLocDwarfExpression::getTemporaryBufferSize() { 211 return TmpBuf ? TmpBuf->Bytes.size() : 0; 212 } 213 214 void DebugLocDwarfExpression::commitTemporaryBuffer() { 215 if (!TmpBuf) 216 return; 217 for (auto Byte : enumerate(TmpBuf->Bytes)) { 218 const char *Comment = (Byte.index() < TmpBuf->Comments.size()) 219 ? TmpBuf->Comments[Byte.index()].c_str() 220 : ""; 221 OutBS.EmitInt8(Byte.value(), Comment); 222 } 223 TmpBuf->Bytes.clear(); 224 TmpBuf->Comments.clear(); 225 } 226 227 const DIType *DbgVariable::getType() const { 228 return getVariable()->getType(); 229 } 230 231 /// Get .debug_loc entry for the instruction range starting at MI. 232 static DbgValueLoc getDebugLocValue(const MachineInstr *MI) { 233 const DIExpression *Expr = MI->getDebugExpression(); 234 assert(MI->getNumOperands() == 4); 235 if (MI->getOperand(0).isReg()) { 236 auto RegOp = MI->getOperand(0); 237 auto Op1 = MI->getOperand(1); 238 // If the second operand is an immediate, this is a 239 // register-indirect address. 240 assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset"); 241 MachineLocation MLoc(RegOp.getReg(), Op1.isImm()); 242 return DbgValueLoc(Expr, MLoc); 243 } 244 if (MI->getOperand(0).isTargetIndex()) { 245 auto Op = MI->getOperand(0); 246 return DbgValueLoc(Expr, 247 TargetIndexLocation(Op.getIndex(), Op.getOffset())); 248 } 249 if (MI->getOperand(0).isImm()) 250 return DbgValueLoc(Expr, MI->getOperand(0).getImm()); 251 if (MI->getOperand(0).isFPImm()) 252 return DbgValueLoc(Expr, MI->getOperand(0).getFPImm()); 253 if (MI->getOperand(0).isCImm()) 254 return DbgValueLoc(Expr, MI->getOperand(0).getCImm()); 255 256 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!"); 257 } 258 259 void DbgVariable::initializeDbgValue(const MachineInstr *DbgValue) { 260 assert(FrameIndexExprs.empty() && "Already initialized?"); 261 assert(!ValueLoc.get() && "Already initialized?"); 262 263 assert(getVariable() == DbgValue->getDebugVariable() && "Wrong variable"); 264 assert(getInlinedAt() == DbgValue->getDebugLoc()->getInlinedAt() && 265 "Wrong inlined-at"); 266 267 ValueLoc = std::make_unique<DbgValueLoc>(getDebugLocValue(DbgValue)); 268 if (auto *E = DbgValue->getDebugExpression()) 269 if (E->getNumElements()) 270 FrameIndexExprs.push_back({0, E}); 271 } 272 273 ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const { 274 if (FrameIndexExprs.size() == 1) 275 return FrameIndexExprs; 276 277 assert(llvm::all_of(FrameIndexExprs, 278 [](const FrameIndexExpr &A) { 279 return A.Expr->isFragment(); 280 }) && 281 "multiple FI expressions without DW_OP_LLVM_fragment"); 282 llvm::sort(FrameIndexExprs, 283 [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool { 284 return A.Expr->getFragmentInfo()->OffsetInBits < 285 B.Expr->getFragmentInfo()->OffsetInBits; 286 }); 287 288 return FrameIndexExprs; 289 } 290 291 void DbgVariable::addMMIEntry(const DbgVariable &V) { 292 assert(DebugLocListIndex == ~0U && !ValueLoc.get() && "not an MMI entry"); 293 assert(V.DebugLocListIndex == ~0U && !V.ValueLoc.get() && "not an MMI entry"); 294 assert(V.getVariable() == getVariable() && "conflicting variable"); 295 assert(V.getInlinedAt() == getInlinedAt() && "conflicting inlined-at location"); 296 297 assert(!FrameIndexExprs.empty() && "Expected an MMI entry"); 298 assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry"); 299 300 // FIXME: This logic should not be necessary anymore, as we now have proper 301 // deduplication. However, without it, we currently run into the assertion 302 // below, which means that we are likely dealing with broken input, i.e. two 303 // non-fragment entries for the same variable at different frame indices. 304 if (FrameIndexExprs.size()) { 305 auto *Expr = FrameIndexExprs.back().Expr; 306 if (!Expr || !Expr->isFragment()) 307 return; 308 } 309 310 for (const auto &FIE : V.FrameIndexExprs) 311 // Ignore duplicate entries. 312 if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) { 313 return FIE.FI == Other.FI && FIE.Expr == Other.Expr; 314 })) 315 FrameIndexExprs.push_back(FIE); 316 317 assert((FrameIndexExprs.size() == 1 || 318 llvm::all_of(FrameIndexExprs, 319 [](FrameIndexExpr &FIE) { 320 return FIE.Expr && FIE.Expr->isFragment(); 321 })) && 322 "conflicting locations for variable"); 323 } 324 325 static AccelTableKind computeAccelTableKind(unsigned DwarfVersion, 326 bool GenerateTypeUnits, 327 DebuggerKind Tuning, 328 const Triple &TT) { 329 // Honor an explicit request. 330 if (AccelTables != AccelTableKind::Default) 331 return AccelTables; 332 333 // Accelerator tables with type units are currently not supported. 334 if (GenerateTypeUnits) 335 return AccelTableKind::None; 336 337 // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5 338 // always implies debug_names. For lower standard versions we use apple 339 // accelerator tables on apple platforms and debug_names elsewhere. 340 if (DwarfVersion >= 5) 341 return AccelTableKind::Dwarf; 342 if (Tuning == DebuggerKind::LLDB) 343 return TT.isOSBinFormatMachO() ? AccelTableKind::Apple 344 : AccelTableKind::Dwarf; 345 return AccelTableKind::None; 346 } 347 348 DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) 349 : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()), 350 InfoHolder(A, "info_string", DIEValueAllocator), 351 SkeletonHolder(A, "skel_string", DIEValueAllocator), 352 IsDarwin(A->TM.getTargetTriple().isOSDarwin()) { 353 const Triple &TT = Asm->TM.getTargetTriple(); 354 355 // Make sure we know our "debugger tuning". The target option takes 356 // precedence; fall back to triple-based defaults. 357 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default) 358 DebuggerTuning = Asm->TM.Options.DebuggerTuning; 359 else if (IsDarwin) 360 DebuggerTuning = DebuggerKind::LLDB; 361 else if (TT.isPS4CPU()) 362 DebuggerTuning = DebuggerKind::SCE; 363 else 364 DebuggerTuning = DebuggerKind::GDB; 365 366 if (DwarfInlinedStrings == Default) 367 UseInlineStrings = TT.isNVPTX(); 368 else 369 UseInlineStrings = DwarfInlinedStrings == Enable; 370 371 UseLocSection = !TT.isNVPTX(); 372 373 HasAppleExtensionAttributes = tuneForLLDB(); 374 375 // Handle split DWARF. 376 HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty(); 377 378 // SCE defaults to linkage names only for abstract subprograms. 379 if (DwarfLinkageNames == DefaultLinkageNames) 380 UseAllLinkageNames = !tuneForSCE(); 381 else 382 UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames; 383 384 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion; 385 unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber 386 : MMI->getModule()->getDwarfVersion(); 387 // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2. 388 DwarfVersion = 389 TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION); 390 391 UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX(); 392 393 // Use sections as references. Force for NVPTX. 394 if (DwarfSectionsAsReferences == Default) 395 UseSectionsAsReferences = TT.isNVPTX(); 396 else 397 UseSectionsAsReferences = DwarfSectionsAsReferences == Enable; 398 399 // Don't generate type units for unsupported object file formats. 400 GenerateTypeUnits = 401 A->TM.getTargetTriple().isOSBinFormatELF() && GenerateDwarfTypeUnits; 402 403 TheAccelTableKind = computeAccelTableKind( 404 DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple()); 405 406 // Work around a GDB bug. GDB doesn't support the standard opcode; 407 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which 408 // is defined as of DWARF 3. 409 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented 410 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616 411 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3; 412 413 // GDB does not fully support the DWARF 4 representation for bitfields. 414 UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB(); 415 416 // The DWARF v5 string offsets table has - possibly shared - contributions 417 // from each compile and type unit each preceded by a header. The string 418 // offsets table used by the pre-DWARF v5 split-DWARF implementation uses 419 // a monolithic string offsets table without any header. 420 UseSegmentedStringOffsetsTable = DwarfVersion >= 5; 421 422 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion); 423 } 424 425 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h. 426 DwarfDebug::~DwarfDebug() = default; 427 428 static bool isObjCClass(StringRef Name) { 429 return Name.startswith("+") || Name.startswith("-"); 430 } 431 432 static bool hasObjCCategory(StringRef Name) { 433 if (!isObjCClass(Name)) 434 return false; 435 436 return Name.find(") ") != StringRef::npos; 437 } 438 439 static void getObjCClassCategory(StringRef In, StringRef &Class, 440 StringRef &Category) { 441 if (!hasObjCCategory(In)) { 442 Class = In.slice(In.find('[') + 1, In.find(' ')); 443 Category = ""; 444 return; 445 } 446 447 Class = In.slice(In.find('[') + 1, In.find('(')); 448 Category = In.slice(In.find('[') + 1, In.find(' ')); 449 } 450 451 static StringRef getObjCMethodName(StringRef In) { 452 return In.slice(In.find(' ') + 1, In.find(']')); 453 } 454 455 // Add the various names to the Dwarf accelerator table names. 456 void DwarfDebug::addSubprogramNames(const DICompileUnit &CU, 457 const DISubprogram *SP, DIE &Die) { 458 if (getAccelTableKind() != AccelTableKind::Apple && 459 CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None) 460 return; 461 462 if (!SP->isDefinition()) 463 return; 464 465 if (SP->getName() != "") 466 addAccelName(CU, SP->getName(), Die); 467 468 // If the linkage name is different than the name, go ahead and output that as 469 // well into the name table. Only do that if we are going to actually emit 470 // that name. 471 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() && 472 (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP))) 473 addAccelName(CU, SP->getLinkageName(), Die); 474 475 // If this is an Objective-C selector name add it to the ObjC accelerator 476 // too. 477 if (isObjCClass(SP->getName())) { 478 StringRef Class, Category; 479 getObjCClassCategory(SP->getName(), Class, Category); 480 addAccelObjC(CU, Class, Die); 481 if (Category != "") 482 addAccelObjC(CU, Category, Die); 483 // Also add the base method name to the name table. 484 addAccelName(CU, getObjCMethodName(SP->getName()), Die); 485 } 486 } 487 488 /// Check whether we should create a DIE for the given Scope, return true 489 /// if we don't create a DIE (the corresponding DIE is null). 490 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) { 491 if (Scope->isAbstractScope()) 492 return false; 493 494 // We don't create a DIE if there is no Range. 495 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges(); 496 if (Ranges.empty()) 497 return true; 498 499 if (Ranges.size() > 1) 500 return false; 501 502 // We don't create a DIE if we have a single Range and the end label 503 // is null. 504 return !getLabelAfterInsn(Ranges.front().second); 505 } 506 507 template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) { 508 F(CU); 509 if (auto *SkelCU = CU.getSkeleton()) 510 if (CU.getCUNode()->getSplitDebugInlining()) 511 F(*SkelCU); 512 } 513 514 bool DwarfDebug::shareAcrossDWOCUs() const { 515 return SplitDwarfCrossCuReferences; 516 } 517 518 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, 519 LexicalScope *Scope) { 520 assert(Scope && Scope->getScopeNode()); 521 assert(Scope->isAbstractScope()); 522 assert(!Scope->getInlinedAt()); 523 524 auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 525 526 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 527 // was inlined from another compile unit. 528 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining()) 529 // Avoid building the original CU if it won't be used 530 SrcCU.constructAbstractSubprogramScopeDIE(Scope); 531 else { 532 auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 533 if (auto *SkelCU = CU.getSkeleton()) { 534 (shareAcrossDWOCUs() ? CU : SrcCU) 535 .constructAbstractSubprogramScopeDIE(Scope); 536 if (CU.getCUNode()->getSplitDebugInlining()) 537 SkelCU->constructAbstractSubprogramScopeDIE(Scope); 538 } else 539 CU.constructAbstractSubprogramScopeDIE(Scope); 540 } 541 } 542 543 DIE &DwarfDebug::constructSubprogramDefinitionDIE(const DISubprogram *SP) { 544 DICompileUnit *Unit = SP->getUnit(); 545 assert(SP->isDefinition() && "Subprogram not a definition"); 546 assert(Unit && "Subprogram definition without parent unit"); 547 auto &CU = getOrCreateDwarfCompileUnit(Unit); 548 return *CU.getOrCreateSubprogramDIE(SP); 549 } 550 551 /// Represents a parameter whose call site value can be described by applying a 552 /// debug expression to a register in the forwarded register worklist. 553 struct FwdRegParamInfo { 554 /// The described parameter register. 555 unsigned ParamReg; 556 557 /// Debug expression that has been built up when walking through the 558 /// instruction chain that produces the parameter's value. 559 const DIExpression *Expr; 560 }; 561 562 /// Register worklist for finding call site values. 563 using FwdRegWorklist = MapVector<unsigned, SmallVector<FwdRegParamInfo, 2>>; 564 565 /// Emit call site parameter entries that are described by the given value and 566 /// debug expression. 567 template <typename ValT> 568 static void finishCallSiteParams(ValT Val, const DIExpression *Expr, 569 ArrayRef<FwdRegParamInfo> DescribedParams, 570 ParamSet &Params) { 571 for (auto Param : DescribedParams) { 572 bool ShouldCombineExpressions = Expr && Param.Expr->getNumElements() > 0; 573 574 // TODO: Entry value operations can currently not be combined with any 575 // other expressions, so we can't emit call site entries in those cases. 576 if (ShouldCombineExpressions && Expr->isEntryValue()) 577 continue; 578 579 // If a parameter's call site value is produced by a chain of 580 // instructions we may have already created an expression for the 581 // parameter when walking through the instructions. Append that to the 582 // base expression. 583 const DIExpression *CombinedExpr = 584 ShouldCombineExpressions 585 ? DIExpression::append(Expr, Param.Expr->getElements()) 586 : Expr; 587 assert((!CombinedExpr || CombinedExpr->isValid()) && 588 "Combined debug expression is invalid"); 589 590 DbgValueLoc DbgLocVal(CombinedExpr, Val); 591 DbgCallSiteParam CSParm(Param.ParamReg, DbgLocVal); 592 Params.push_back(CSParm); 593 ++NumCSParams; 594 } 595 } 596 597 /// Add \p Reg to the worklist, if it's not already present, and mark that the 598 /// given parameter registers' values can (potentially) be described using 599 /// that register and an debug expression. 600 static void addToFwdRegWorklist(FwdRegWorklist &Worklist, unsigned Reg, 601 const DIExpression *Expr, 602 ArrayRef<FwdRegParamInfo> ParamsToAdd) { 603 auto I = Worklist.insert({Reg, {}}); 604 auto &ParamsForFwdReg = I.first->second; 605 for (auto Param : ParamsToAdd) { 606 assert(none_of(ParamsForFwdReg, 607 [Param](const FwdRegParamInfo &D) { 608 return D.ParamReg == Param.ParamReg; 609 }) && 610 "Same parameter described twice by forwarding reg"); 611 612 // If a parameter's call site value is produced by a chain of 613 // instructions we may have already created an expression for the 614 // parameter when walking through the instructions. Append that to the 615 // new expression. 616 const DIExpression *CombinedExpr = 617 (Param.Expr->getNumElements() > 0) 618 ? DIExpression::append(Expr, Param.Expr->getElements()) 619 : Expr; 620 assert(CombinedExpr->isValid() && "Combined debug expression is invalid"); 621 622 ParamsForFwdReg.push_back({Param.ParamReg, CombinedExpr}); 623 } 624 } 625 626 /// Try to interpret values loaded into registers that forward parameters 627 /// for \p CallMI. Store parameters with interpreted value into \p Params. 628 static void collectCallSiteParameters(const MachineInstr *CallMI, 629 ParamSet &Params) { 630 auto *MF = CallMI->getMF(); 631 auto CalleesMap = MF->getCallSitesInfo(); 632 auto CallFwdRegsInfo = CalleesMap.find(CallMI); 633 634 // There is no information for the call instruction. 635 if (CallFwdRegsInfo == CalleesMap.end()) 636 return; 637 638 auto *MBB = CallMI->getParent(); 639 const auto &TRI = MF->getSubtarget().getRegisterInfo(); 640 const auto &TII = MF->getSubtarget().getInstrInfo(); 641 const auto &TLI = MF->getSubtarget().getTargetLowering(); 642 643 // Skip the call instruction. 644 auto I = std::next(CallMI->getReverseIterator()); 645 646 FwdRegWorklist ForwardedRegWorklist; 647 648 // If an instruction defines more than one item in the worklist, we may run 649 // into situations where a worklist register's value is (potentially) 650 // described by the previous value of another register that is also defined 651 // by that instruction. 652 // 653 // This can for example occur in cases like this: 654 // 655 // $r1 = mov 123 656 // $r0, $r1 = mvrr $r1, 456 657 // call @foo, $r0, $r1 658 // 659 // When describing $r1's value for the mvrr instruction, we need to make sure 660 // that we don't finalize an entry value for $r0, as that is dependent on the 661 // previous value of $r1 (123 rather than 456). 662 // 663 // In order to not have to distinguish between those cases when finalizing 664 // entry values, we simply postpone adding new parameter registers to the 665 // worklist, by first keeping them in this temporary container until the 666 // instruction has been handled. 667 FwdRegWorklist NewWorklistItems; 668 669 const DIExpression *EmptyExpr = 670 DIExpression::get(MF->getFunction().getContext(), {}); 671 672 // Add all the forwarding registers into the ForwardedRegWorklist. 673 for (auto ArgReg : CallFwdRegsInfo->second) { 674 bool InsertedReg = 675 ForwardedRegWorklist.insert({ArgReg.Reg, {{ArgReg.Reg, EmptyExpr}}}) 676 .second; 677 assert(InsertedReg && "Single register used to forward two arguments?"); 678 (void)InsertedReg; 679 } 680 681 // We erase, from the ForwardedRegWorklist, those forwarding registers for 682 // which we successfully describe a loaded value (by using 683 // the describeLoadedValue()). For those remaining arguments in the working 684 // list, for which we do not describe a loaded value by 685 // the describeLoadedValue(), we try to generate an entry value expression 686 // for their call site value description, if the call is within the entry MBB. 687 // TODO: Handle situations when call site parameter value can be described 688 // as the entry value within basic blocks other than the first one. 689 bool ShouldTryEmitEntryVals = MBB->getIterator() == MF->begin(); 690 691 // If the MI is an instruction defining one or more parameters' forwarding 692 // registers, add those defines. 693 auto getForwardingRegsDefinedByMI = [&](const MachineInstr &MI, 694 SmallSetVector<unsigned, 4> &Defs) { 695 if (MI.isDebugInstr()) 696 return; 697 698 for (const MachineOperand &MO : MI.operands()) { 699 if (MO.isReg() && MO.isDef() && 700 Register::isPhysicalRegister(MO.getReg())) { 701 for (auto FwdReg : ForwardedRegWorklist) 702 if (TRI->regsOverlap(FwdReg.first, MO.getReg())) 703 Defs.insert(FwdReg.first); 704 } 705 } 706 }; 707 708 // Search for a loading value in forwarding registers. 709 for (; I != MBB->rend(); ++I) { 710 // Skip bundle headers. 711 if (I->isBundle()) 712 continue; 713 714 // If the next instruction is a call we can not interpret parameter's 715 // forwarding registers or we finished the interpretation of all parameters. 716 if (I->isCall()) 717 return; 718 719 if (ForwardedRegWorklist.empty()) 720 return; 721 722 // Set of worklist registers that are defined by this instruction. 723 SmallSetVector<unsigned, 4> FwdRegDefs; 724 725 getForwardingRegsDefinedByMI(*I, FwdRegDefs); 726 if (FwdRegDefs.empty()) 727 continue; 728 729 for (auto ParamFwdReg : FwdRegDefs) { 730 if (auto ParamValue = TII->describeLoadedValue(*I, ParamFwdReg)) { 731 if (ParamValue->first.isImm()) { 732 int64_t Val = ParamValue->first.getImm(); 733 finishCallSiteParams(Val, ParamValue->second, 734 ForwardedRegWorklist[ParamFwdReg], Params); 735 } else if (ParamValue->first.isReg()) { 736 Register RegLoc = ParamValue->first.getReg(); 737 unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); 738 Register FP = TRI->getFrameRegister(*MF); 739 bool IsSPorFP = (RegLoc == SP) || (RegLoc == FP); 740 if (TRI->isCalleeSavedPhysReg(RegLoc, *MF) || IsSPorFP) { 741 MachineLocation MLoc(RegLoc, /*IsIndirect=*/IsSPorFP); 742 finishCallSiteParams(MLoc, ParamValue->second, 743 ForwardedRegWorklist[ParamFwdReg], Params); 744 } else { 745 // ParamFwdReg was described by the non-callee saved register 746 // RegLoc. Mark that the call site values for the parameters are 747 // dependent on that register instead of ParamFwdReg. Since RegLoc 748 // may be a register that will be handled in this iteration, we 749 // postpone adding the items to the worklist, and instead keep them 750 // in a temporary container. 751 addToFwdRegWorklist(NewWorklistItems, RegLoc, ParamValue->second, 752 ForwardedRegWorklist[ParamFwdReg]); 753 } 754 } 755 } 756 } 757 758 // Remove all registers that this instruction defines from the worklist. 759 for (auto ParamFwdReg : FwdRegDefs) 760 ForwardedRegWorklist.erase(ParamFwdReg); 761 762 // Now that we are done handling this instruction, add items from the 763 // temporary worklist to the real one. 764 for (auto New : NewWorklistItems) 765 addToFwdRegWorklist(ForwardedRegWorklist, New.first, EmptyExpr, 766 New.second); 767 NewWorklistItems.clear(); 768 } 769 770 // Emit the call site parameter's value as an entry value. 771 if (ShouldTryEmitEntryVals) { 772 // Create an expression where the register's entry value is used. 773 DIExpression *EntryExpr = DIExpression::get( 774 MF->getFunction().getContext(), {dwarf::DW_OP_LLVM_entry_value, 1}); 775 for (auto RegEntry : ForwardedRegWorklist) { 776 MachineLocation MLoc(RegEntry.first); 777 finishCallSiteParams(MLoc, EntryExpr, RegEntry.second, Params); 778 } 779 } 780 } 781 782 void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP, 783 DwarfCompileUnit &CU, DIE &ScopeDIE, 784 const MachineFunction &MF) { 785 // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if 786 // the subprogram is required to have one. 787 if (!SP.areAllCallsDescribed() || !SP.isDefinition()) 788 return; 789 790 // Use DW_AT_call_all_calls to express that call site entries are present 791 // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls 792 // because one of its requirements is not met: call site entries for 793 // optimized-out calls are elided. 794 CU.addFlag(ScopeDIE, CU.getDwarf5OrGNUAttr(dwarf::DW_AT_call_all_calls)); 795 796 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 797 assert(TII && "TargetInstrInfo not found: cannot label tail calls"); 798 799 // Emit call site entries for each call or tail call in the function. 800 for (const MachineBasicBlock &MBB : MF) { 801 for (const MachineInstr &MI : MBB.instrs()) { 802 // Bundles with call in them will pass the isCall() test below but do not 803 // have callee operand information so skip them here. Iterator will 804 // eventually reach the call MI. 805 if (MI.isBundle()) 806 continue; 807 808 // Skip instructions which aren't calls. Both calls and tail-calling jump 809 // instructions (e.g TAILJMPd64) are classified correctly here. 810 if (!MI.isCandidateForCallSiteEntry()) 811 continue; 812 813 // Skip instructions marked as frame setup, as they are not interesting to 814 // the user. 815 if (MI.getFlag(MachineInstr::FrameSetup)) 816 continue; 817 818 // TODO: Add support for targets with delay slots (see: beginInstruction). 819 if (MI.hasDelaySlot()) 820 return; 821 822 // If this is a direct call, find the callee's subprogram. 823 // In the case of an indirect call find the register that holds 824 // the callee. 825 const MachineOperand &CalleeOp = MI.getOperand(0); 826 if (!CalleeOp.isGlobal() && !CalleeOp.isReg()) 827 continue; 828 829 unsigned CallReg = 0; 830 DIE *CalleeDIE = nullptr; 831 const Function *CalleeDecl = nullptr; 832 if (CalleeOp.isReg()) { 833 CallReg = CalleeOp.getReg(); 834 if (!CallReg) 835 continue; 836 } else { 837 CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal()); 838 if (!CalleeDecl || !CalleeDecl->getSubprogram()) 839 continue; 840 const DISubprogram *CalleeSP = CalleeDecl->getSubprogram(); 841 842 if (CalleeSP->isDefinition()) { 843 // Ensure that a subprogram DIE for the callee is available in the 844 // appropriate CU. 845 CalleeDIE = &constructSubprogramDefinitionDIE(CalleeSP); 846 } else { 847 // Create the declaration DIE if it is missing. This is required to 848 // support compilation of old bitcode with an incomplete list of 849 // retained metadata. 850 CalleeDIE = CU.getOrCreateSubprogramDIE(CalleeSP); 851 } 852 assert(CalleeDIE && "Must have a DIE for the callee"); 853 } 854 855 // TODO: Omit call site entries for runtime calls (objc_msgSend, etc). 856 857 bool IsTail = TII->isTailCall(MI); 858 859 // If MI is in a bundle, the label was created after the bundle since 860 // EmitFunctionBody iterates over top-level MIs. Get that top-level MI 861 // to search for that label below. 862 const MachineInstr *TopLevelCallMI = 863 MI.isInsideBundle() ? &*getBundleStart(MI.getIterator()) : &MI; 864 865 // For tail calls, no return PC information is needed. 866 // For regular calls (and tail calls in GDB tuning), the return PC 867 // is needed to disambiguate paths in the call graph which could lead to 868 // some target function. 869 const MCSymbol *PCAddr = 870 (IsTail && !tuneForGDB()) 871 ? nullptr 872 : const_cast<MCSymbol *>(getLabelAfterInsn(TopLevelCallMI)); 873 874 assert((IsTail || PCAddr) && "Call without return PC information"); 875 876 LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> " 877 << (CalleeDecl ? CalleeDecl->getName() 878 : StringRef(MF.getSubtarget() 879 .getRegisterInfo() 880 ->getName(CallReg))) 881 << (IsTail ? " [IsTail]" : "") << "\n"); 882 883 DIE &CallSiteDIE = CU.constructCallSiteEntryDIE(ScopeDIE, CalleeDIE, 884 IsTail, PCAddr, CallReg); 885 886 // GDB and LLDB support call site parameter debug info. 887 if (Asm->TM.Options.EnableDebugEntryValues && 888 (tuneForGDB() || tuneForLLDB())) { 889 ParamSet Params; 890 // Try to interpret values of call site parameters. 891 collectCallSiteParameters(&MI, Params); 892 CU.constructCallSiteParmEntryDIEs(CallSiteDIE, Params); 893 } 894 } 895 } 896 } 897 898 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const { 899 if (!U.hasDwarfPubSections()) 900 return; 901 902 U.addFlag(D, dwarf::DW_AT_GNU_pubnames); 903 } 904 905 void DwarfDebug::finishUnitAttributes(const DICompileUnit *DIUnit, 906 DwarfCompileUnit &NewCU) { 907 DIE &Die = NewCU.getUnitDie(); 908 StringRef FN = DIUnit->getFilename(); 909 910 StringRef Producer = DIUnit->getProducer(); 911 StringRef Flags = DIUnit->getFlags(); 912 if (!Flags.empty() && !useAppleExtensionAttributes()) { 913 std::string ProducerWithFlags = Producer.str() + " " + Flags.str(); 914 NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags); 915 } else 916 NewCU.addString(Die, dwarf::DW_AT_producer, Producer); 917 918 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 919 DIUnit->getSourceLanguage()); 920 NewCU.addString(Die, dwarf::DW_AT_name, FN); 921 StringRef SysRoot = DIUnit->getSysRoot(); 922 if (!SysRoot.empty()) 923 NewCU.addString(Die, dwarf::DW_AT_LLVM_sysroot, SysRoot); 924 StringRef SDK = DIUnit->getSDK(); 925 if (!SDK.empty()) 926 NewCU.addString(Die, dwarf::DW_AT_APPLE_sdk, SDK); 927 928 // Add DW_str_offsets_base to the unit DIE, except for split units. 929 if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) 930 NewCU.addStringOffsetsStart(); 931 932 if (!useSplitDwarf()) { 933 NewCU.initStmtList(); 934 935 // If we're using split dwarf the compilation dir is going to be in the 936 // skeleton CU and so we don't need to duplicate it here. 937 if (!CompilationDir.empty()) 938 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 939 addGnuPubAttributes(NewCU, Die); 940 } 941 942 if (useAppleExtensionAttributes()) { 943 if (DIUnit->isOptimized()) 944 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized); 945 946 StringRef Flags = DIUnit->getFlags(); 947 if (!Flags.empty()) 948 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags); 949 950 if (unsigned RVer = DIUnit->getRuntimeVersion()) 951 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, 952 dwarf::DW_FORM_data1, RVer); 953 } 954 955 if (DIUnit->getDWOId()) { 956 // This CU is either a clang module DWO or a skeleton CU. 957 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, 958 DIUnit->getDWOId()); 959 if (!DIUnit->getSplitDebugFilename().empty()) { 960 // This is a prefabricated skeleton CU. 961 dwarf::Attribute attrDWOName = getDwarfVersion() >= 5 962 ? dwarf::DW_AT_dwo_name 963 : dwarf::DW_AT_GNU_dwo_name; 964 NewCU.addString(Die, attrDWOName, DIUnit->getSplitDebugFilename()); 965 } 966 } 967 } 968 // Create new DwarfCompileUnit for the given metadata node with tag 969 // DW_TAG_compile_unit. 970 DwarfCompileUnit & 971 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) { 972 if (auto *CU = CUMap.lookup(DIUnit)) 973 return *CU; 974 975 CompilationDir = DIUnit->getDirectory(); 976 977 auto OwnedUnit = std::make_unique<DwarfCompileUnit>( 978 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder); 979 DwarfCompileUnit &NewCU = *OwnedUnit; 980 InfoHolder.addUnit(std::move(OwnedUnit)); 981 982 for (auto *IE : DIUnit->getImportedEntities()) 983 NewCU.addImportedEntity(IE); 984 985 // LTO with assembly output shares a single line table amongst multiple CUs. 986 // To avoid the compilation directory being ambiguous, let the line table 987 // explicitly describe the directory of all files, never relying on the 988 // compilation directory. 989 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU) 990 Asm->OutStreamer->emitDwarfFile0Directive( 991 CompilationDir, DIUnit->getFilename(), 992 NewCU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource(), 993 NewCU.getUniqueID()); 994 995 if (useSplitDwarf()) { 996 NewCU.setSkeleton(constructSkeletonCU(NewCU)); 997 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection()); 998 } else { 999 finishUnitAttributes(DIUnit, NewCU); 1000 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 1001 } 1002 1003 CUMap.insert({DIUnit, &NewCU}); 1004 CUDieMap.insert({&NewCU.getUnitDie(), &NewCU}); 1005 return NewCU; 1006 } 1007 1008 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, 1009 const DIImportedEntity *N) { 1010 if (isa<DILocalScope>(N->getScope())) 1011 return; 1012 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope())) 1013 D->addChild(TheCU.constructImportedEntityDIE(N)); 1014 } 1015 1016 /// Sort and unique GVEs by comparing their fragment offset. 1017 static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & 1018 sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { 1019 llvm::sort( 1020 GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { 1021 // Sort order: first null exprs, then exprs without fragment 1022 // info, then sort by fragment offset in bits. 1023 // FIXME: Come up with a more comprehensive comparator so 1024 // the sorting isn't non-deterministic, and so the following 1025 // std::unique call works correctly. 1026 if (!A.Expr || !B.Expr) 1027 return !!B.Expr; 1028 auto FragmentA = A.Expr->getFragmentInfo(); 1029 auto FragmentB = B.Expr->getFragmentInfo(); 1030 if (!FragmentA || !FragmentB) 1031 return !!FragmentB; 1032 return FragmentA->OffsetInBits < FragmentB->OffsetInBits; 1033 }); 1034 GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), 1035 [](DwarfCompileUnit::GlobalExpr A, 1036 DwarfCompileUnit::GlobalExpr B) { 1037 return A.Expr == B.Expr; 1038 }), 1039 GVEs.end()); 1040 return GVEs; 1041 } 1042 1043 // Emit all Dwarf sections that should come prior to the content. Create 1044 // global DIEs and emit initial debug info sections. This is invoked by 1045 // the target AsmPrinter. 1046 void DwarfDebug::beginModule() { 1047 NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName, 1048 DWARFGroupDescription, TimePassesIsEnabled); 1049 if (DisableDebugInfoPrinting) { 1050 MMI->setDebugInfoAvailability(false); 1051 return; 1052 } 1053 1054 const Module *M = MMI->getModule(); 1055 1056 unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(), 1057 M->debug_compile_units_end()); 1058 // Tell MMI whether we have debug info. 1059 assert(MMI->hasDebugInfo() == (NumDebugCUs > 0) && 1060 "DebugInfoAvailabilty initialized unexpectedly"); 1061 SingleCU = NumDebugCUs == 1; 1062 DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>> 1063 GVMap; 1064 for (const GlobalVariable &Global : M->globals()) { 1065 SmallVector<DIGlobalVariableExpression *, 1> GVs; 1066 Global.getDebugInfo(GVs); 1067 for (auto *GVE : GVs) 1068 GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()}); 1069 } 1070 1071 // Create the symbol that designates the start of the unit's contribution 1072 // to the string offsets table. In a split DWARF scenario, only the skeleton 1073 // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol). 1074 if (useSegmentedStringOffsetsTable()) 1075 (useSplitDwarf() ? SkeletonHolder : InfoHolder) 1076 .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base")); 1077 1078 1079 // Create the symbols that designates the start of the DWARF v5 range list 1080 // and locations list tables. They are located past the table headers. 1081 if (getDwarfVersion() >= 5) { 1082 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1083 Holder.setRnglistsTableBaseSym( 1084 Asm->createTempSymbol("rnglists_table_base")); 1085 1086 if (useSplitDwarf()) 1087 InfoHolder.setRnglistsTableBaseSym( 1088 Asm->createTempSymbol("rnglists_dwo_table_base")); 1089 } 1090 1091 // Create the symbol that points to the first entry following the debug 1092 // address table (.debug_addr) header. 1093 AddrPool.setLabel(Asm->createTempSymbol("addr_table_base")); 1094 DebugLocs.setSym(Asm->createTempSymbol("loclists_table_base")); 1095 1096 for (DICompileUnit *CUNode : M->debug_compile_units()) { 1097 // FIXME: Move local imported entities into a list attached to the 1098 // subprogram, then this search won't be needed and a 1099 // getImportedEntities().empty() test should go below with the rest. 1100 bool HasNonLocalImportedEntities = llvm::any_of( 1101 CUNode->getImportedEntities(), [](const DIImportedEntity *IE) { 1102 return !isa<DILocalScope>(IE->getScope()); 1103 }); 1104 1105 if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() && 1106 CUNode->getRetainedTypes().empty() && 1107 CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty()) 1108 continue; 1109 1110 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode); 1111 1112 // Global Variables. 1113 for (auto *GVE : CUNode->getGlobalVariables()) { 1114 // Don't bother adding DIGlobalVariableExpressions listed in the CU if we 1115 // already know about the variable and it isn't adding a constant 1116 // expression. 1117 auto &GVMapEntry = GVMap[GVE->getVariable()]; 1118 auto *Expr = GVE->getExpression(); 1119 if (!GVMapEntry.size() || (Expr && Expr->isConstant())) 1120 GVMapEntry.push_back({nullptr, Expr}); 1121 } 1122 DenseSet<DIGlobalVariable *> Processed; 1123 for (auto *GVE : CUNode->getGlobalVariables()) { 1124 DIGlobalVariable *GV = GVE->getVariable(); 1125 if (Processed.insert(GV).second) 1126 CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV])); 1127 } 1128 1129 for (auto *Ty : CUNode->getEnumTypes()) { 1130 // The enum types array by design contains pointers to 1131 // MDNodes rather than DIRefs. Unique them here. 1132 CU.getOrCreateTypeDIE(cast<DIType>(Ty)); 1133 } 1134 for (auto *Ty : CUNode->getRetainedTypes()) { 1135 // The retained types array by design contains pointers to 1136 // MDNodes rather than DIRefs. Unique them here. 1137 if (DIType *RT = dyn_cast<DIType>(Ty)) 1138 // There is no point in force-emitting a forward declaration. 1139 CU.getOrCreateTypeDIE(RT); 1140 } 1141 // Emit imported_modules last so that the relevant context is already 1142 // available. 1143 for (auto *IE : CUNode->getImportedEntities()) 1144 constructAndAddImportedEntityDIE(CU, IE); 1145 } 1146 } 1147 1148 void DwarfDebug::finishEntityDefinitions() { 1149 for (const auto &Entity : ConcreteEntities) { 1150 DIE *Die = Entity->getDIE(); 1151 assert(Die); 1152 // FIXME: Consider the time-space tradeoff of just storing the unit pointer 1153 // in the ConcreteEntities list, rather than looking it up again here. 1154 // DIE::getUnit isn't simple - it walks parent pointers, etc. 1155 DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie()); 1156 assert(Unit); 1157 Unit->finishEntityDefinition(Entity.get()); 1158 } 1159 } 1160 1161 void DwarfDebug::finishSubprogramDefinitions() { 1162 for (const DISubprogram *SP : ProcessedSPNodes) { 1163 assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug); 1164 forBothCUs( 1165 getOrCreateDwarfCompileUnit(SP->getUnit()), 1166 [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); }); 1167 } 1168 } 1169 1170 void DwarfDebug::finalizeModuleInfo() { 1171 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1172 1173 finishSubprogramDefinitions(); 1174 1175 finishEntityDefinitions(); 1176 1177 // Include the DWO file name in the hash if there's more than one CU. 1178 // This handles ThinLTO's situation where imported CUs may very easily be 1179 // duplicate with the same CU partially imported into another ThinLTO unit. 1180 StringRef DWOName; 1181 if (CUMap.size() > 1) 1182 DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile; 1183 1184 // Handle anything that needs to be done on a per-unit basis after 1185 // all other generation. 1186 for (const auto &P : CUMap) { 1187 auto &TheCU = *P.second; 1188 if (TheCU.getCUNode()->isDebugDirectivesOnly()) 1189 continue; 1190 // Emit DW_AT_containing_type attribute to connect types with their 1191 // vtable holding type. 1192 TheCU.constructContainingTypeDIEs(); 1193 1194 // Add CU specific attributes if we need to add any. 1195 // If we're splitting the dwarf out now that we've got the entire 1196 // CU then add the dwo id to it. 1197 auto *SkCU = TheCU.getSkeleton(); 1198 1199 bool HasSplitUnit = SkCU && !TheCU.getUnitDie().children().empty(); 1200 1201 if (HasSplitUnit) { 1202 dwarf::Attribute attrDWOName = getDwarfVersion() >= 5 1203 ? dwarf::DW_AT_dwo_name 1204 : dwarf::DW_AT_GNU_dwo_name; 1205 finishUnitAttributes(TheCU.getCUNode(), TheCU); 1206 TheCU.addString(TheCU.getUnitDie(), attrDWOName, 1207 Asm->TM.Options.MCOptions.SplitDwarfFile); 1208 SkCU->addString(SkCU->getUnitDie(), attrDWOName, 1209 Asm->TM.Options.MCOptions.SplitDwarfFile); 1210 // Emit a unique identifier for this CU. 1211 uint64_t ID = 1212 DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie()); 1213 if (getDwarfVersion() >= 5) { 1214 TheCU.setDWOId(ID); 1215 SkCU->setDWOId(ID); 1216 } else { 1217 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 1218 dwarf::DW_FORM_data8, ID); 1219 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 1220 dwarf::DW_FORM_data8, ID); 1221 } 1222 1223 if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) { 1224 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); 1225 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, 1226 Sym, Sym); 1227 } 1228 } else if (SkCU) { 1229 finishUnitAttributes(SkCU->getCUNode(), *SkCU); 1230 } 1231 1232 // If we have code split among multiple sections or non-contiguous 1233 // ranges of code then emit a DW_AT_ranges attribute on the unit that will 1234 // remain in the .o file, otherwise add a DW_AT_low_pc. 1235 // FIXME: We should use ranges allow reordering of code ala 1236 // .subsections_via_symbols in mach-o. This would mean turning on 1237 // ranges for all subprogram DIEs for mach-o. 1238 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 1239 1240 if (unsigned NumRanges = TheCU.getRanges().size()) { 1241 if (NumRanges > 1 && useRangesSection()) 1242 // A DW_AT_low_pc attribute may also be specified in combination with 1243 // DW_AT_ranges to specify the default base address for use in 1244 // location lists (see Section 2.6.2) and range lists (see Section 1245 // 2.17.3). 1246 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0); 1247 else 1248 U.setBaseAddress(TheCU.getRanges().front().Begin); 1249 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); 1250 } 1251 1252 // We don't keep track of which addresses are used in which CU so this 1253 // is a bit pessimistic under LTO. 1254 if ((!AddrPool.isEmpty() || TheCU.hasRangeLists()) && 1255 (getDwarfVersion() >= 5 || HasSplitUnit)) 1256 U.addAddrTableBase(); 1257 1258 if (getDwarfVersion() >= 5) { 1259 if (U.hasRangeLists()) 1260 U.addRnglistsBase(); 1261 1262 if (!DebugLocs.getLists().empty()) { 1263 if (!useSplitDwarf()) 1264 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_loclists_base, 1265 DebugLocs.getSym(), 1266 TLOF.getDwarfLoclistsSection()->getBeginSymbol()); 1267 } 1268 } 1269 1270 auto *CUNode = cast<DICompileUnit>(P.first); 1271 // If compile Unit has macros, emit "DW_AT_macro_info" attribute. 1272 if (CUNode->getMacros()) { 1273 if (useSplitDwarf()) 1274 TheCU.addSectionDelta(TheCU.getUnitDie(), dwarf::DW_AT_macro_info, 1275 U.getMacroLabelBegin(), 1276 TLOF.getDwarfMacinfoDWOSection()->getBeginSymbol()); 1277 else 1278 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, 1279 U.getMacroLabelBegin(), 1280 TLOF.getDwarfMacinfoSection()->getBeginSymbol()); 1281 } 1282 } 1283 1284 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules. 1285 for (auto *CUNode : MMI->getModule()->debug_compile_units()) 1286 if (CUNode->getDWOId()) 1287 getOrCreateDwarfCompileUnit(CUNode); 1288 1289 // Compute DIE offsets and sizes. 1290 InfoHolder.computeSizeAndOffsets(); 1291 if (useSplitDwarf()) 1292 SkeletonHolder.computeSizeAndOffsets(); 1293 } 1294 1295 // Emit all Dwarf sections that should come after the content. 1296 void DwarfDebug::endModule() { 1297 assert(CurFn == nullptr); 1298 assert(CurMI == nullptr); 1299 1300 for (const auto &P : CUMap) { 1301 auto &CU = *P.second; 1302 CU.createBaseTypeDIEs(); 1303 } 1304 1305 // If we aren't actually generating debug info (check beginModule - 1306 // conditionalized on !DisableDebugInfoPrinting and the presence of the 1307 // llvm.dbg.cu metadata node) 1308 if (!MMI->hasDebugInfo()) 1309 return; 1310 1311 // Finalize the debug info for the module. 1312 finalizeModuleInfo(); 1313 1314 if (useSplitDwarf()) 1315 // Emit debug_loc.dwo/debug_loclists.dwo section. 1316 emitDebugLocDWO(); 1317 else 1318 // Emit debug_loc/debug_loclists section. 1319 emitDebugLoc(); 1320 1321 // Corresponding abbreviations into a abbrev section. 1322 emitAbbreviations(); 1323 1324 // Emit all the DIEs into a debug info section. 1325 emitDebugInfo(); 1326 1327 // Emit info into a debug aranges section. 1328 if (GenerateARangeSection) 1329 emitDebugARanges(); 1330 1331 // Emit info into a debug ranges section. 1332 emitDebugRanges(); 1333 1334 if (useSplitDwarf()) 1335 // Emit info into a debug macinfo.dwo section. 1336 emitDebugMacinfoDWO(); 1337 else 1338 // Emit info into a debug macinfo section. 1339 emitDebugMacinfo(); 1340 1341 emitDebugStr(); 1342 1343 if (useSplitDwarf()) { 1344 emitDebugStrDWO(); 1345 emitDebugInfoDWO(); 1346 emitDebugAbbrevDWO(); 1347 emitDebugLineDWO(); 1348 emitDebugRangesDWO(); 1349 } 1350 1351 emitDebugAddr(); 1352 1353 // Emit info into the dwarf accelerator table sections. 1354 switch (getAccelTableKind()) { 1355 case AccelTableKind::Apple: 1356 emitAccelNames(); 1357 emitAccelObjC(); 1358 emitAccelNamespaces(); 1359 emitAccelTypes(); 1360 break; 1361 case AccelTableKind::Dwarf: 1362 emitAccelDebugNames(); 1363 break; 1364 case AccelTableKind::None: 1365 break; 1366 case AccelTableKind::Default: 1367 llvm_unreachable("Default should have already been resolved."); 1368 } 1369 1370 // Emit the pubnames and pubtypes sections if requested. 1371 emitDebugPubSections(); 1372 1373 // clean up. 1374 // FIXME: AbstractVariables.clear(); 1375 } 1376 1377 void DwarfDebug::ensureAbstractEntityIsCreated(DwarfCompileUnit &CU, 1378 const DINode *Node, 1379 const MDNode *ScopeNode) { 1380 if (CU.getExistingAbstractEntity(Node)) 1381 return; 1382 1383 CU.createAbstractEntity(Node, LScopes.getOrCreateAbstractScope( 1384 cast<DILocalScope>(ScopeNode))); 1385 } 1386 1387 void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU, 1388 const DINode *Node, const MDNode *ScopeNode) { 1389 if (CU.getExistingAbstractEntity(Node)) 1390 return; 1391 1392 if (LexicalScope *Scope = 1393 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode))) 1394 CU.createAbstractEntity(Node, Scope); 1395 } 1396 1397 // Collect variable information from side table maintained by MF. 1398 void DwarfDebug::collectVariableInfoFromMFTable( 1399 DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) { 1400 SmallDenseMap<InlinedEntity, DbgVariable *> MFVars; 1401 LLVM_DEBUG(dbgs() << "DwarfDebug: collecting variables from MF side table\n"); 1402 for (const auto &VI : Asm->MF->getVariableDbgInfo()) { 1403 if (!VI.Var) 1404 continue; 1405 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && 1406 "Expected inlined-at fields to agree"); 1407 1408 InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt()); 1409 Processed.insert(Var); 1410 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); 1411 1412 // If variable scope is not found then skip this variable. 1413 if (!Scope) { 1414 LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI.Var->getName() 1415 << ", no variable scope found\n"); 1416 continue; 1417 } 1418 1419 ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode()); 1420 auto RegVar = std::make_unique<DbgVariable>( 1421 cast<DILocalVariable>(Var.first), Var.second); 1422 RegVar->initializeMMI(VI.Expr, VI.Slot); 1423 LLVM_DEBUG(dbgs() << "Created DbgVariable for " << VI.Var->getName() 1424 << "\n"); 1425 if (DbgVariable *DbgVar = MFVars.lookup(Var)) 1426 DbgVar->addMMIEntry(*RegVar); 1427 else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) { 1428 MFVars.insert({Var, RegVar.get()}); 1429 ConcreteEntities.push_back(std::move(RegVar)); 1430 } 1431 } 1432 } 1433 1434 /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its 1435 /// enclosing lexical scope. The check ensures there are no other instructions 1436 /// in the same lexical scope preceding the DBG_VALUE and that its range is 1437 /// either open or otherwise rolls off the end of the scope. 1438 static bool validThroughout(LexicalScopes &LScopes, 1439 const MachineInstr *DbgValue, 1440 const MachineInstr *RangeEnd) { 1441 assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location"); 1442 auto MBB = DbgValue->getParent(); 1443 auto DL = DbgValue->getDebugLoc(); 1444 auto *LScope = LScopes.findLexicalScope(DL); 1445 // Scope doesn't exist; this is a dead DBG_VALUE. 1446 if (!LScope) 1447 return false; 1448 auto &LSRange = LScope->getRanges(); 1449 if (LSRange.size() == 0) 1450 return false; 1451 1452 // Determine if the DBG_VALUE is valid at the beginning of its lexical block. 1453 const MachineInstr *LScopeBegin = LSRange.front().first; 1454 // Early exit if the lexical scope begins outside of the current block. 1455 if (LScopeBegin->getParent() != MBB) 1456 return false; 1457 MachineBasicBlock::const_reverse_iterator Pred(DbgValue); 1458 for (++Pred; Pred != MBB->rend(); ++Pred) { 1459 if (Pred->getFlag(MachineInstr::FrameSetup)) 1460 break; 1461 auto PredDL = Pred->getDebugLoc(); 1462 if (!PredDL || Pred->isMetaInstruction()) 1463 continue; 1464 // Check whether the instruction preceding the DBG_VALUE is in the same 1465 // (sub)scope as the DBG_VALUE. 1466 if (DL->getScope() == PredDL->getScope()) 1467 return false; 1468 auto *PredScope = LScopes.findLexicalScope(PredDL); 1469 if (!PredScope || LScope->dominates(PredScope)) 1470 return false; 1471 } 1472 1473 // If the range of the DBG_VALUE is open-ended, report success. 1474 if (!RangeEnd) 1475 return true; 1476 1477 // Fail if there are instructions belonging to our scope in another block. 1478 const MachineInstr *LScopeEnd = LSRange.back().second; 1479 if (LScopeEnd->getParent() != MBB) 1480 return false; 1481 1482 // Single, constant DBG_VALUEs in the prologue are promoted to be live 1483 // throughout the function. This is a hack, presumably for DWARF v2 and not 1484 // necessarily correct. It would be much better to use a dbg.declare instead 1485 // if we know the constant is live throughout the scope. 1486 if (DbgValue->getOperand(0).isImm() && MBB->pred_empty()) 1487 return true; 1488 1489 return false; 1490 } 1491 1492 /// Build the location list for all DBG_VALUEs in the function that 1493 /// describe the same variable. The resulting DebugLocEntries will have 1494 /// strict monotonically increasing begin addresses and will never 1495 /// overlap. If the resulting list has only one entry that is valid 1496 /// throughout variable's scope return true. 1497 // 1498 // See the definition of DbgValueHistoryMap::Entry for an explanation of the 1499 // different kinds of history map entries. One thing to be aware of is that if 1500 // a debug value is ended by another entry (rather than being valid until the 1501 // end of the function), that entry's instruction may or may not be included in 1502 // the range, depending on if the entry is a clobbering entry (it has an 1503 // instruction that clobbers one or more preceding locations), or if it is an 1504 // (overlapping) debug value entry. This distinction can be seen in the example 1505 // below. The first debug value is ended by the clobbering entry 2, and the 1506 // second and third debug values are ended by the overlapping debug value entry 1507 // 4. 1508 // 1509 // Input: 1510 // 1511 // History map entries [type, end index, mi] 1512 // 1513 // 0 | [DbgValue, 2, DBG_VALUE $reg0, [...] (fragment 0, 32)] 1514 // 1 | | [DbgValue, 4, DBG_VALUE $reg1, [...] (fragment 32, 32)] 1515 // 2 | | [Clobber, $reg0 = [...], -, -] 1516 // 3 | | [DbgValue, 4, DBG_VALUE 123, [...] (fragment 64, 32)] 1517 // 4 [DbgValue, ~0, DBG_VALUE @g, [...] (fragment 0, 96)] 1518 // 1519 // Output [start, end) [Value...]: 1520 // 1521 // [0-1) [(reg0, fragment 0, 32)] 1522 // [1-3) [(reg0, fragment 0, 32), (reg1, fragment 32, 32)] 1523 // [3-4) [(reg1, fragment 32, 32), (123, fragment 64, 32)] 1524 // [4-) [(@g, fragment 0, 96)] 1525 bool DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, 1526 const DbgValueHistoryMap::Entries &Entries) { 1527 using OpenRange = 1528 std::pair<DbgValueHistoryMap::EntryIndex, DbgValueLoc>; 1529 SmallVector<OpenRange, 4> OpenRanges; 1530 bool isSafeForSingleLocation = true; 1531 const MachineInstr *StartDebugMI = nullptr; 1532 const MachineInstr *EndMI = nullptr; 1533 1534 for (auto EB = Entries.begin(), EI = EB, EE = Entries.end(); EI != EE; ++EI) { 1535 const MachineInstr *Instr = EI->getInstr(); 1536 1537 // Remove all values that are no longer live. 1538 size_t Index = std::distance(EB, EI); 1539 auto Last = 1540 remove_if(OpenRanges, [&](OpenRange &R) { return R.first <= Index; }); 1541 OpenRanges.erase(Last, OpenRanges.end()); 1542 1543 // If we are dealing with a clobbering entry, this iteration will result in 1544 // a location list entry starting after the clobbering instruction. 1545 const MCSymbol *StartLabel = 1546 EI->isClobber() ? getLabelAfterInsn(Instr) : getLabelBeforeInsn(Instr); 1547 assert(StartLabel && 1548 "Forgot label before/after instruction starting a range!"); 1549 1550 const MCSymbol *EndLabel; 1551 if (std::next(EI) == Entries.end()) { 1552 EndLabel = Asm->getFunctionEnd(); 1553 if (EI->isClobber()) 1554 EndMI = EI->getInstr(); 1555 } 1556 else if (std::next(EI)->isClobber()) 1557 EndLabel = getLabelAfterInsn(std::next(EI)->getInstr()); 1558 else 1559 EndLabel = getLabelBeforeInsn(std::next(EI)->getInstr()); 1560 assert(EndLabel && "Forgot label after instruction ending a range!"); 1561 1562 if (EI->isDbgValue()) 1563 LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Instr << "\n"); 1564 1565 // If this history map entry has a debug value, add that to the list of 1566 // open ranges and check if its location is valid for a single value 1567 // location. 1568 if (EI->isDbgValue()) { 1569 // Do not add undef debug values, as they are redundant information in 1570 // the location list entries. An undef debug results in an empty location 1571 // description. If there are any non-undef fragments then padding pieces 1572 // with empty location descriptions will automatically be inserted, and if 1573 // all fragments are undef then the whole location list entry is 1574 // redundant. 1575 if (!Instr->isUndefDebugValue()) { 1576 auto Value = getDebugLocValue(Instr); 1577 OpenRanges.emplace_back(EI->getEndIndex(), Value); 1578 1579 // TODO: Add support for single value fragment locations. 1580 if (Instr->getDebugExpression()->isFragment()) 1581 isSafeForSingleLocation = false; 1582 1583 if (!StartDebugMI) 1584 StartDebugMI = Instr; 1585 } else { 1586 isSafeForSingleLocation = false; 1587 } 1588 } 1589 1590 // Location list entries with empty location descriptions are redundant 1591 // information in DWARF, so do not emit those. 1592 if (OpenRanges.empty()) 1593 continue; 1594 1595 // Omit entries with empty ranges as they do not have any effect in DWARF. 1596 if (StartLabel == EndLabel) { 1597 LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n"); 1598 continue; 1599 } 1600 1601 SmallVector<DbgValueLoc, 4> Values; 1602 for (auto &R : OpenRanges) 1603 Values.push_back(R.second); 1604 DebugLoc.emplace_back(StartLabel, EndLabel, Values); 1605 1606 // Attempt to coalesce the ranges of two otherwise identical 1607 // DebugLocEntries. 1608 auto CurEntry = DebugLoc.rbegin(); 1609 LLVM_DEBUG({ 1610 dbgs() << CurEntry->getValues().size() << " Values:\n"; 1611 for (auto &Value : CurEntry->getValues()) 1612 Value.dump(); 1613 dbgs() << "-----\n"; 1614 }); 1615 1616 auto PrevEntry = std::next(CurEntry); 1617 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry)) 1618 DebugLoc.pop_back(); 1619 } 1620 1621 return DebugLoc.size() == 1 && isSafeForSingleLocation && 1622 validThroughout(LScopes, StartDebugMI, EndMI); 1623 } 1624 1625 DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU, 1626 LexicalScope &Scope, 1627 const DINode *Node, 1628 const DILocation *Location, 1629 const MCSymbol *Sym) { 1630 ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode()); 1631 if (isa<const DILocalVariable>(Node)) { 1632 ConcreteEntities.push_back( 1633 std::make_unique<DbgVariable>(cast<const DILocalVariable>(Node), 1634 Location)); 1635 InfoHolder.addScopeVariable(&Scope, 1636 cast<DbgVariable>(ConcreteEntities.back().get())); 1637 } else if (isa<const DILabel>(Node)) { 1638 ConcreteEntities.push_back( 1639 std::make_unique<DbgLabel>(cast<const DILabel>(Node), 1640 Location, Sym)); 1641 InfoHolder.addScopeLabel(&Scope, 1642 cast<DbgLabel>(ConcreteEntities.back().get())); 1643 } 1644 return ConcreteEntities.back().get(); 1645 } 1646 1647 // Find variables for each lexical scope. 1648 void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU, 1649 const DISubprogram *SP, 1650 DenseSet<InlinedEntity> &Processed) { 1651 // Grab the variable info that was squirreled away in the MMI side-table. 1652 collectVariableInfoFromMFTable(TheCU, Processed); 1653 1654 for (const auto &I : DbgValues) { 1655 InlinedEntity IV = I.first; 1656 if (Processed.count(IV)) 1657 continue; 1658 1659 // Instruction ranges, specifying where IV is accessible. 1660 const auto &HistoryMapEntries = I.second; 1661 if (HistoryMapEntries.empty()) 1662 continue; 1663 1664 LexicalScope *Scope = nullptr; 1665 const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first); 1666 if (const DILocation *IA = IV.second) 1667 Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA); 1668 else 1669 Scope = LScopes.findLexicalScope(LocalVar->getScope()); 1670 // If variable scope is not found then skip this variable. 1671 if (!Scope) 1672 continue; 1673 1674 Processed.insert(IV); 1675 DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU, 1676 *Scope, LocalVar, IV.second)); 1677 1678 const MachineInstr *MInsn = HistoryMapEntries.front().getInstr(); 1679 assert(MInsn->isDebugValue() && "History must begin with debug value"); 1680 1681 // Check if there is a single DBG_VALUE, valid throughout the var's scope. 1682 // If the history map contains a single debug value, there may be an 1683 // additional entry which clobbers the debug value. 1684 size_t HistSize = HistoryMapEntries.size(); 1685 bool SingleValueWithClobber = 1686 HistSize == 2 && HistoryMapEntries[1].isClobber(); 1687 if (HistSize == 1 || SingleValueWithClobber) { 1688 const auto *End = 1689 SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr; 1690 if (validThroughout(LScopes, MInsn, End)) { 1691 RegVar->initializeDbgValue(MInsn); 1692 continue; 1693 } 1694 } 1695 1696 // Do not emit location lists if .debug_loc secton is disabled. 1697 if (!useLocSection()) 1698 continue; 1699 1700 // Handle multiple DBG_VALUE instructions describing one variable. 1701 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn); 1702 1703 // Build the location list for this variable. 1704 SmallVector<DebugLocEntry, 8> Entries; 1705 bool isValidSingleLocation = buildLocationList(Entries, HistoryMapEntries); 1706 1707 // Check whether buildLocationList managed to merge all locations to one 1708 // that is valid throughout the variable's scope. If so, produce single 1709 // value location. 1710 if (isValidSingleLocation) { 1711 RegVar->initializeDbgValue(Entries[0].getValues()[0]); 1712 continue; 1713 } 1714 1715 // If the variable has a DIBasicType, extract it. Basic types cannot have 1716 // unique identifiers, so don't bother resolving the type with the 1717 // identifier map. 1718 const DIBasicType *BT = dyn_cast<DIBasicType>( 1719 static_cast<const Metadata *>(LocalVar->getType())); 1720 1721 // Finalize the entry by lowering it into a DWARF bytestream. 1722 for (auto &Entry : Entries) 1723 Entry.finalize(*Asm, List, BT, TheCU); 1724 } 1725 1726 // For each InlinedEntity collected from DBG_LABEL instructions, convert to 1727 // DWARF-related DbgLabel. 1728 for (const auto &I : DbgLabels) { 1729 InlinedEntity IL = I.first; 1730 const MachineInstr *MI = I.second; 1731 if (MI == nullptr) 1732 continue; 1733 1734 LexicalScope *Scope = nullptr; 1735 const DILabel *Label = cast<DILabel>(IL.first); 1736 // The scope could have an extra lexical block file. 1737 const DILocalScope *LocalScope = 1738 Label->getScope()->getNonLexicalBlockFileScope(); 1739 // Get inlined DILocation if it is inlined label. 1740 if (const DILocation *IA = IL.second) 1741 Scope = LScopes.findInlinedScope(LocalScope, IA); 1742 else 1743 Scope = LScopes.findLexicalScope(LocalScope); 1744 // If label scope is not found then skip this label. 1745 if (!Scope) 1746 continue; 1747 1748 Processed.insert(IL); 1749 /// At this point, the temporary label is created. 1750 /// Save the temporary label to DbgLabel entity to get the 1751 /// actually address when generating Dwarf DIE. 1752 MCSymbol *Sym = getLabelBeforeInsn(MI); 1753 createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym); 1754 } 1755 1756 // Collect info for variables/labels that were optimized out. 1757 for (const DINode *DN : SP->getRetainedNodes()) { 1758 if (!Processed.insert(InlinedEntity(DN, nullptr)).second) 1759 continue; 1760 LexicalScope *Scope = nullptr; 1761 if (auto *DV = dyn_cast<DILocalVariable>(DN)) { 1762 Scope = LScopes.findLexicalScope(DV->getScope()); 1763 } else if (auto *DL = dyn_cast<DILabel>(DN)) { 1764 Scope = LScopes.findLexicalScope(DL->getScope()); 1765 } 1766 1767 if (Scope) 1768 createConcreteEntity(TheCU, *Scope, DN, nullptr); 1769 } 1770 } 1771 1772 // Process beginning of an instruction. 1773 void DwarfDebug::beginInstruction(const MachineInstr *MI) { 1774 DebugHandlerBase::beginInstruction(MI); 1775 assert(CurMI); 1776 1777 const auto *SP = MI->getMF()->getFunction().getSubprogram(); 1778 if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) 1779 return; 1780 1781 // Check if source location changes, but ignore DBG_VALUE and CFI locations. 1782 // If the instruction is part of the function frame setup code, do not emit 1783 // any line record, as there is no correspondence with any user code. 1784 if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup)) 1785 return; 1786 const DebugLoc &DL = MI->getDebugLoc(); 1787 // When we emit a line-0 record, we don't update PrevInstLoc; so look at 1788 // the last line number actually emitted, to see if it was line 0. 1789 unsigned LastAsmLine = 1790 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine(); 1791 1792 // Request a label after the call in order to emit AT_return_pc information 1793 // in call site entries. TODO: Add support for targets with delay slots. 1794 if (SP->areAllCallsDescribed() && MI->isCall() && !MI->hasDelaySlot()) 1795 requestLabelAfterInsn(MI); 1796 1797 if (DL == PrevInstLoc) { 1798 // If we have an ongoing unspecified location, nothing to do here. 1799 if (!DL) 1800 return; 1801 // We have an explicit location, same as the previous location. 1802 // But we might be coming back to it after a line 0 record. 1803 if (LastAsmLine == 0 && DL.getLine() != 0) { 1804 // Reinstate the source location but not marked as a statement. 1805 const MDNode *Scope = DL.getScope(); 1806 recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0); 1807 } 1808 return; 1809 } 1810 1811 if (!DL) { 1812 // We have an unspecified location, which might want to be line 0. 1813 // If we have already emitted a line-0 record, don't repeat it. 1814 if (LastAsmLine == 0) 1815 return; 1816 // If user said Don't Do That, don't do that. 1817 if (UnknownLocations == Disable) 1818 return; 1819 // See if we have a reason to emit a line-0 record now. 1820 // Reasons to emit a line-0 record include: 1821 // - User asked for it (UnknownLocations). 1822 // - Instruction has a label, so it's referenced from somewhere else, 1823 // possibly debug information; we want it to have a source location. 1824 // - Instruction is at the top of a block; we don't want to inherit the 1825 // location from the physically previous (maybe unrelated) block. 1826 if (UnknownLocations == Enable || PrevLabel || 1827 (PrevInstBB && PrevInstBB != MI->getParent())) { 1828 // Preserve the file and column numbers, if we can, to save space in 1829 // the encoded line table. 1830 // Do not update PrevInstLoc, it remembers the last non-0 line. 1831 const MDNode *Scope = nullptr; 1832 unsigned Column = 0; 1833 if (PrevInstLoc) { 1834 Scope = PrevInstLoc.getScope(); 1835 Column = PrevInstLoc.getCol(); 1836 } 1837 recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0); 1838 } 1839 return; 1840 } 1841 1842 // We have an explicit location, different from the previous location. 1843 // Don't repeat a line-0 record, but otherwise emit the new location. 1844 // (The new location might be an explicit line 0, which we do emit.) 1845 if (DL.getLine() == 0 && LastAsmLine == 0) 1846 return; 1847 unsigned Flags = 0; 1848 if (DL == PrologEndLoc) { 1849 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT; 1850 PrologEndLoc = DebugLoc(); 1851 } 1852 // If the line changed, we call that a new statement; unless we went to 1853 // line 0 and came back, in which case it is not a new statement. 1854 unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine; 1855 if (DL.getLine() && DL.getLine() != OldLine) 1856 Flags |= DWARF2_FLAG_IS_STMT; 1857 1858 const MDNode *Scope = DL.getScope(); 1859 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); 1860 1861 // If we're not at line 0, remember this location. 1862 if (DL.getLine()) 1863 PrevInstLoc = DL; 1864 } 1865 1866 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { 1867 // First known non-DBG_VALUE and non-frame setup location marks 1868 // the beginning of the function body. 1869 for (const auto &MBB : *MF) 1870 for (const auto &MI : MBB) 1871 if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && 1872 MI.getDebugLoc()) 1873 return MI.getDebugLoc(); 1874 return DebugLoc(); 1875 } 1876 1877 /// Register a source line with debug info. Returns the unique label that was 1878 /// emitted and which provides correspondence to the source line list. 1879 static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col, 1880 const MDNode *S, unsigned Flags, unsigned CUID, 1881 uint16_t DwarfVersion, 1882 ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs) { 1883 StringRef Fn; 1884 unsigned FileNo = 1; 1885 unsigned Discriminator = 0; 1886 if (auto *Scope = cast_or_null<DIScope>(S)) { 1887 Fn = Scope->getFilename(); 1888 if (Line != 0 && DwarfVersion >= 4) 1889 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) 1890 Discriminator = LBF->getDiscriminator(); 1891 1892 FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID]) 1893 .getOrCreateSourceID(Scope->getFile()); 1894 } 1895 Asm.OutStreamer->emitDwarfLocDirective(FileNo, Line, Col, Flags, 0, 1896 Discriminator, Fn); 1897 } 1898 1899 DebugLoc DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, 1900 unsigned CUID) { 1901 // Get beginning of function. 1902 if (DebugLoc PrologEndLoc = findPrologueEndLoc(&MF)) { 1903 // Ensure the compile unit is created if the function is called before 1904 // beginFunction(). 1905 (void)getOrCreateDwarfCompileUnit( 1906 MF.getFunction().getSubprogram()->getUnit()); 1907 // We'd like to list the prologue as "not statements" but GDB behaves 1908 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. 1909 const DISubprogram *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); 1910 ::recordSourceLine(*Asm, SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT, 1911 CUID, getDwarfVersion(), getUnits()); 1912 return PrologEndLoc; 1913 } 1914 return DebugLoc(); 1915 } 1916 1917 // Gather pre-function debug information. Assumes being called immediately 1918 // after the function entry point has been emitted. 1919 void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) { 1920 CurFn = MF; 1921 1922 auto *SP = MF->getFunction().getSubprogram(); 1923 assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode()); 1924 if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) 1925 return; 1926 1927 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 1928 1929 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function 1930 // belongs to so that we add to the correct per-cu line table in the 1931 // non-asm case. 1932 if (Asm->OutStreamer->hasRawTextSupport()) 1933 // Use a single line table if we are generating assembly. 1934 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1935 else 1936 Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID()); 1937 1938 // Record beginning of function. 1939 PrologEndLoc = emitInitialLocDirective( 1940 *MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID()); 1941 } 1942 1943 void DwarfDebug::skippedNonDebugFunction() { 1944 // If we don't have a subprogram for this function then there will be a hole 1945 // in the range information. Keep note of this by setting the previously used 1946 // section to nullptr. 1947 PrevCU = nullptr; 1948 CurFn = nullptr; 1949 } 1950 1951 // Gather and emit post-function debug information. 1952 void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { 1953 const DISubprogram *SP = MF->getFunction().getSubprogram(); 1954 1955 assert(CurFn == MF && 1956 "endFunction should be called with the same function as beginFunction"); 1957 1958 // Set DwarfDwarfCompileUnitID in MCContext to default value. 1959 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1960 1961 LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); 1962 assert(!FnScope || SP == FnScope->getScopeNode()); 1963 DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); 1964 if (TheCU.getCUNode()->isDebugDirectivesOnly()) { 1965 PrevLabel = nullptr; 1966 CurFn = nullptr; 1967 return; 1968 } 1969 1970 DenseSet<InlinedEntity> Processed; 1971 collectEntityInfo(TheCU, SP, Processed); 1972 1973 // Add the range of this function to the list of ranges for the CU. 1974 TheCU.addRange({Asm->getFunctionBegin(), Asm->getFunctionEnd()}); 1975 1976 // Under -gmlt, skip building the subprogram if there are no inlined 1977 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram 1978 // is still needed as we need its source location. 1979 if (!TheCU.getCUNode()->getDebugInfoForProfiling() && 1980 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && 1981 LScopes.getAbstractScopesList().empty() && !IsDarwin) { 1982 assert(InfoHolder.getScopeVariables().empty()); 1983 PrevLabel = nullptr; 1984 CurFn = nullptr; 1985 return; 1986 } 1987 1988 #ifndef NDEBUG 1989 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); 1990 #endif 1991 // Construct abstract scopes. 1992 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { 1993 auto *SP = cast<DISubprogram>(AScope->getScopeNode()); 1994 for (const DINode *DN : SP->getRetainedNodes()) { 1995 if (!Processed.insert(InlinedEntity(DN, nullptr)).second) 1996 continue; 1997 1998 const MDNode *Scope = nullptr; 1999 if (auto *DV = dyn_cast<DILocalVariable>(DN)) 2000 Scope = DV->getScope(); 2001 else if (auto *DL = dyn_cast<DILabel>(DN)) 2002 Scope = DL->getScope(); 2003 else 2004 llvm_unreachable("Unexpected DI type!"); 2005 2006 // Collect info for variables/labels that were optimized out. 2007 ensureAbstractEntityIsCreated(TheCU, DN, Scope); 2008 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes 2009 && "ensureAbstractEntityIsCreated inserted abstract scopes"); 2010 } 2011 constructAbstractSubprogramScopeDIE(TheCU, AScope); 2012 } 2013 2014 ProcessedSPNodes.insert(SP); 2015 DIE &ScopeDIE = TheCU.constructSubprogramScopeDIE(SP, FnScope); 2016 if (auto *SkelCU = TheCU.getSkeleton()) 2017 if (!LScopes.getAbstractScopesList().empty() && 2018 TheCU.getCUNode()->getSplitDebugInlining()) 2019 SkelCU->constructSubprogramScopeDIE(SP, FnScope); 2020 2021 // Construct call site entries. 2022 constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF); 2023 2024 // Clear debug info 2025 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the 2026 // DbgVariables except those that are also in AbstractVariables (since they 2027 // can be used cross-function) 2028 InfoHolder.getScopeVariables().clear(); 2029 InfoHolder.getScopeLabels().clear(); 2030 PrevLabel = nullptr; 2031 CurFn = nullptr; 2032 } 2033 2034 // Register a source line with debug info. Returns the unique label that was 2035 // emitted and which provides correspondence to the source line list. 2036 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, 2037 unsigned Flags) { 2038 ::recordSourceLine(*Asm, Line, Col, S, Flags, 2039 Asm->OutStreamer->getContext().getDwarfCompileUnitID(), 2040 getDwarfVersion(), getUnits()); 2041 } 2042 2043 //===----------------------------------------------------------------------===// 2044 // Emit Methods 2045 //===----------------------------------------------------------------------===// 2046 2047 // Emit the debug info section. 2048 void DwarfDebug::emitDebugInfo() { 2049 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2050 Holder.emitUnits(/* UseOffsets */ false); 2051 } 2052 2053 // Emit the abbreviation section. 2054 void DwarfDebug::emitAbbreviations() { 2055 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2056 2057 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); 2058 } 2059 2060 void DwarfDebug::emitStringOffsetsTableHeader() { 2061 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2062 Holder.getStringPool().emitStringOffsetsTableHeader( 2063 *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(), 2064 Holder.getStringOffsetsStartSym()); 2065 } 2066 2067 template <typename AccelTableT> 2068 void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section, 2069 StringRef TableName) { 2070 Asm->OutStreamer->SwitchSection(Section); 2071 2072 // Emit the full data. 2073 emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol()); 2074 } 2075 2076 void DwarfDebug::emitAccelDebugNames() { 2077 // Don't emit anything if we have no compilation units to index. 2078 if (getUnits().empty()) 2079 return; 2080 2081 emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits()); 2082 } 2083 2084 // Emit visible names into a hashed accelerator table section. 2085 void DwarfDebug::emitAccelNames() { 2086 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), 2087 "Names"); 2088 } 2089 2090 // Emit objective C classes and categories into a hashed accelerator table 2091 // section. 2092 void DwarfDebug::emitAccelObjC() { 2093 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), 2094 "ObjC"); 2095 } 2096 2097 // Emit namespace dies into a hashed accelerator table. 2098 void DwarfDebug::emitAccelNamespaces() { 2099 emitAccel(AccelNamespace, 2100 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), 2101 "namespac"); 2102 } 2103 2104 // Emit type dies into a hashed accelerator table. 2105 void DwarfDebug::emitAccelTypes() { 2106 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), 2107 "types"); 2108 } 2109 2110 // Public name handling. 2111 // The format for the various pubnames: 2112 // 2113 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU 2114 // for the DIE that is named. 2115 // 2116 // gnu pubnames - offset/index value/name tuples where the offset is the offset 2117 // into the CU and the index value is computed according to the type of value 2118 // for the DIE that is named. 2119 // 2120 // For type units the offset is the offset of the skeleton DIE. For split dwarf 2121 // it's the offset within the debug_info/debug_types dwo section, however, the 2122 // reference in the pubname header doesn't change. 2123 2124 /// computeIndexValue - Compute the gdb index value for the DIE and CU. 2125 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, 2126 const DIE *Die) { 2127 // Entities that ended up only in a Type Unit reference the CU instead (since 2128 // the pub entry has offsets within the CU there's no real offset that can be 2129 // provided anyway). As it happens all such entities (namespaces and types, 2130 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out 2131 // not to be true it would be necessary to persist this information from the 2132 // point at which the entry is added to the index data structure - since by 2133 // the time the index is built from that, the original type/namespace DIE in a 2134 // type unit has already been destroyed so it can't be queried for properties 2135 // like tag, etc. 2136 if (Die->getTag() == dwarf::DW_TAG_compile_unit) 2137 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, 2138 dwarf::GIEL_EXTERNAL); 2139 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; 2140 2141 // We could have a specification DIE that has our most of our knowledge, 2142 // look for that now. 2143 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { 2144 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); 2145 if (SpecDIE.findAttribute(dwarf::DW_AT_external)) 2146 Linkage = dwarf::GIEL_EXTERNAL; 2147 } else if (Die->findAttribute(dwarf::DW_AT_external)) 2148 Linkage = dwarf::GIEL_EXTERNAL; 2149 2150 switch (Die->getTag()) { 2151 case dwarf::DW_TAG_class_type: 2152 case dwarf::DW_TAG_structure_type: 2153 case dwarf::DW_TAG_union_type: 2154 case dwarf::DW_TAG_enumeration_type: 2155 return dwarf::PubIndexEntryDescriptor( 2156 dwarf::GIEK_TYPE, 2157 dwarf::isCPlusPlus((dwarf::SourceLanguage)CU->getLanguage()) 2158 ? dwarf::GIEL_EXTERNAL 2159 : dwarf::GIEL_STATIC); 2160 case dwarf::DW_TAG_typedef: 2161 case dwarf::DW_TAG_base_type: 2162 case dwarf::DW_TAG_subrange_type: 2163 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); 2164 case dwarf::DW_TAG_namespace: 2165 return dwarf::GIEK_TYPE; 2166 case dwarf::DW_TAG_subprogram: 2167 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); 2168 case dwarf::DW_TAG_variable: 2169 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); 2170 case dwarf::DW_TAG_enumerator: 2171 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, 2172 dwarf::GIEL_STATIC); 2173 default: 2174 return dwarf::GIEK_NONE; 2175 } 2176 } 2177 2178 /// emitDebugPubSections - Emit visible names and types into debug pubnames and 2179 /// pubtypes sections. 2180 void DwarfDebug::emitDebugPubSections() { 2181 for (const auto &NU : CUMap) { 2182 DwarfCompileUnit *TheU = NU.second; 2183 if (!TheU->hasDwarfPubSections()) 2184 continue; 2185 2186 bool GnuStyle = TheU->getCUNode()->getNameTableKind() == 2187 DICompileUnit::DebugNameTableKind::GNU; 2188 2189 Asm->OutStreamer->SwitchSection( 2190 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() 2191 : Asm->getObjFileLowering().getDwarfPubNamesSection()); 2192 emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames()); 2193 2194 Asm->OutStreamer->SwitchSection( 2195 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() 2196 : Asm->getObjFileLowering().getDwarfPubTypesSection()); 2197 emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes()); 2198 } 2199 } 2200 2201 void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) { 2202 if (useSectionsAsReferences()) 2203 Asm->emitDwarfOffset(CU.getSection()->getBeginSymbol(), 2204 CU.getDebugSectionOffset()); 2205 else 2206 Asm->emitDwarfSymbolReference(CU.getLabelBegin()); 2207 } 2208 2209 void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name, 2210 DwarfCompileUnit *TheU, 2211 const StringMap<const DIE *> &Globals) { 2212 if (auto *Skeleton = TheU->getSkeleton()) 2213 TheU = Skeleton; 2214 2215 // Emit the header. 2216 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info"); 2217 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin"); 2218 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end"); 2219 Asm->emitLabelDifference(EndLabel, BeginLabel, 4); 2220 2221 Asm->OutStreamer->emitLabel(BeginLabel); 2222 2223 Asm->OutStreamer->AddComment("DWARF Version"); 2224 Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); 2225 2226 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); 2227 emitSectionReference(*TheU); 2228 2229 Asm->OutStreamer->AddComment("Compilation Unit Length"); 2230 Asm->emitInt32(TheU->getLength()); 2231 2232 // Emit the pubnames for this compilation unit. 2233 for (const auto &GI : Globals) { 2234 const char *Name = GI.getKeyData(); 2235 const DIE *Entity = GI.second; 2236 2237 Asm->OutStreamer->AddComment("DIE offset"); 2238 Asm->emitInt32(Entity->getOffset()); 2239 2240 if (GnuStyle) { 2241 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); 2242 Asm->OutStreamer->AddComment( 2243 Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + 2244 ", " + dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); 2245 Asm->emitInt8(Desc.toBits()); 2246 } 2247 2248 Asm->OutStreamer->AddComment("External Name"); 2249 Asm->OutStreamer->emitBytes(StringRef(Name, GI.getKeyLength() + 1)); 2250 } 2251 2252 Asm->OutStreamer->AddComment("End Mark"); 2253 Asm->emitInt32(0); 2254 Asm->OutStreamer->emitLabel(EndLabel); 2255 } 2256 2257 /// Emit null-terminated strings into a debug str section. 2258 void DwarfDebug::emitDebugStr() { 2259 MCSection *StringOffsetsSection = nullptr; 2260 if (useSegmentedStringOffsetsTable()) { 2261 emitStringOffsetsTableHeader(); 2262 StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection(); 2263 } 2264 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2265 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(), 2266 StringOffsetsSection, /* UseRelativeOffsets = */ true); 2267 } 2268 2269 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, 2270 const DebugLocStream::Entry &Entry, 2271 const DwarfCompileUnit *CU) { 2272 auto &&Comments = DebugLocs.getComments(Entry); 2273 auto Comment = Comments.begin(); 2274 auto End = Comments.end(); 2275 2276 // The expressions are inserted into a byte stream rather early (see 2277 // DwarfExpression::addExpression) so for those ops (e.g. DW_OP_convert) that 2278 // need to reference a base_type DIE the offset of that DIE is not yet known. 2279 // To deal with this we instead insert a placeholder early and then extract 2280 // it here and replace it with the real reference. 2281 unsigned PtrSize = Asm->MAI->getCodePointerSize(); 2282 DWARFDataExtractor Data(StringRef(DebugLocs.getBytes(Entry).data(), 2283 DebugLocs.getBytes(Entry).size()), 2284 Asm->getDataLayout().isLittleEndian(), PtrSize); 2285 DWARFExpression Expr(Data, PtrSize); 2286 2287 using Encoding = DWARFExpression::Operation::Encoding; 2288 uint64_t Offset = 0; 2289 for (auto &Op : Expr) { 2290 assert(Op.getCode() != dwarf::DW_OP_const_type && 2291 "3 operand ops not yet supported"); 2292 Streamer.EmitInt8(Op.getCode(), Comment != End ? *(Comment++) : ""); 2293 Offset++; 2294 for (unsigned I = 0; I < 2; ++I) { 2295 if (Op.getDescription().Op[I] == Encoding::SizeNA) 2296 continue; 2297 if (Op.getDescription().Op[I] == Encoding::BaseTypeRef) { 2298 uint64_t Offset = 2299 CU->ExprRefedBaseTypes[Op.getRawOperand(I)].Die->getOffset(); 2300 assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit"); 2301 Streamer.emitULEB128(Offset, "", ULEB128PadSize); 2302 // Make sure comments stay aligned. 2303 for (unsigned J = 0; J < ULEB128PadSize; ++J) 2304 if (Comment != End) 2305 Comment++; 2306 } else { 2307 for (uint64_t J = Offset; J < Op.getOperandEndOffset(I); ++J) 2308 Streamer.EmitInt8(Data.getData()[J], Comment != End ? *(Comment++) : ""); 2309 } 2310 Offset = Op.getOperandEndOffset(I); 2311 } 2312 assert(Offset == Op.getEndOffset()); 2313 } 2314 } 2315 2316 void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, 2317 const DbgValueLoc &Value, 2318 DwarfExpression &DwarfExpr) { 2319 auto *DIExpr = Value.getExpression(); 2320 DIExpressionCursor ExprCursor(DIExpr); 2321 DwarfExpr.addFragmentOffset(DIExpr); 2322 // Regular entry. 2323 if (Value.isInt()) { 2324 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || 2325 BT->getEncoding() == dwarf::DW_ATE_signed_char)) 2326 DwarfExpr.addSignedConstant(Value.getInt()); 2327 else 2328 DwarfExpr.addUnsignedConstant(Value.getInt()); 2329 } else if (Value.isLocation()) { 2330 MachineLocation Location = Value.getLoc(); 2331 if (Location.isIndirect()) 2332 DwarfExpr.setMemoryLocationKind(); 2333 DIExpressionCursor Cursor(DIExpr); 2334 2335 if (DIExpr->isEntryValue()) { 2336 DwarfExpr.setEntryValueFlag(); 2337 DwarfExpr.beginEntryValueExpression(Cursor); 2338 } 2339 2340 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); 2341 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 2342 return; 2343 return DwarfExpr.addExpression(std::move(Cursor)); 2344 } else if (Value.isTargetIndexLocation()) { 2345 TargetIndexLocation Loc = Value.getTargetIndexLocation(); 2346 // TODO TargetIndexLocation is a target-independent. Currently only the WebAssembly-specific 2347 // encoding is supported. 2348 DwarfExpr.addWasmLocation(Loc.Index, Loc.Offset); 2349 } else if (Value.isConstantFP()) { 2350 APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt(); 2351 DwarfExpr.addUnsignedConstant(RawBytes); 2352 } 2353 DwarfExpr.addExpression(std::move(ExprCursor)); 2354 } 2355 2356 void DebugLocEntry::finalize(const AsmPrinter &AP, 2357 DebugLocStream::ListBuilder &List, 2358 const DIBasicType *BT, 2359 DwarfCompileUnit &TheCU) { 2360 assert(!Values.empty() && 2361 "location list entries without values are redundant"); 2362 assert(Begin != End && "unexpected location list entry with empty range"); 2363 DebugLocStream::EntryBuilder Entry(List, Begin, End); 2364 BufferByteStreamer Streamer = Entry.getStreamer(); 2365 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer, TheCU); 2366 const DbgValueLoc &Value = Values[0]; 2367 if (Value.isFragment()) { 2368 // Emit all fragments that belong to the same variable and range. 2369 assert(llvm::all_of(Values, [](DbgValueLoc P) { 2370 return P.isFragment(); 2371 }) && "all values are expected to be fragments"); 2372 assert(std::is_sorted(Values.begin(), Values.end()) && 2373 "fragments are expected to be sorted"); 2374 2375 for (auto Fragment : Values) 2376 DwarfDebug::emitDebugLocValue(AP, BT, Fragment, DwarfExpr); 2377 2378 } else { 2379 assert(Values.size() == 1 && "only fragments may have >1 value"); 2380 DwarfDebug::emitDebugLocValue(AP, BT, Value, DwarfExpr); 2381 } 2382 DwarfExpr.finalize(); 2383 if (DwarfExpr.TagOffset) 2384 List.setTagOffset(*DwarfExpr.TagOffset); 2385 } 2386 2387 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry, 2388 const DwarfCompileUnit *CU) { 2389 // Emit the size. 2390 Asm->OutStreamer->AddComment("Loc expr size"); 2391 if (getDwarfVersion() >= 5) 2392 Asm->emitULEB128(DebugLocs.getBytes(Entry).size()); 2393 else if (DebugLocs.getBytes(Entry).size() <= std::numeric_limits<uint16_t>::max()) 2394 Asm->emitInt16(DebugLocs.getBytes(Entry).size()); 2395 else { 2396 // The entry is too big to fit into 16 bit, drop it as there is nothing we 2397 // can do. 2398 Asm->emitInt16(0); 2399 return; 2400 } 2401 // Emit the entry. 2402 APByteStreamer Streamer(*Asm); 2403 emitDebugLocEntry(Streamer, Entry, CU); 2404 } 2405 2406 // Emit the header of a DWARF 5 range list table list table. Returns the symbol 2407 // that designates the end of the table for the caller to emit when the table is 2408 // complete. 2409 static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm, 2410 const DwarfFile &Holder) { 2411 MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer); 2412 2413 Asm->OutStreamer->AddComment("Offset entry count"); 2414 Asm->emitInt32(Holder.getRangeLists().size()); 2415 Asm->OutStreamer->emitLabel(Holder.getRnglistsTableBaseSym()); 2416 2417 for (const RangeSpanList &List : Holder.getRangeLists()) 2418 Asm->emitLabelDifference(List.Label, Holder.getRnglistsTableBaseSym(), 4); 2419 2420 return TableEnd; 2421 } 2422 2423 // Emit the header of a DWARF 5 locations list table. Returns the symbol that 2424 // designates the end of the table for the caller to emit when the table is 2425 // complete. 2426 static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm, 2427 const DwarfDebug &DD) { 2428 MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer); 2429 2430 const auto &DebugLocs = DD.getDebugLocs(); 2431 2432 Asm->OutStreamer->AddComment("Offset entry count"); 2433 Asm->emitInt32(DebugLocs.getLists().size()); 2434 Asm->OutStreamer->emitLabel(DebugLocs.getSym()); 2435 2436 for (const auto &List : DebugLocs.getLists()) 2437 Asm->emitLabelDifference(List.Label, DebugLocs.getSym(), 4); 2438 2439 return TableEnd; 2440 } 2441 2442 template <typename Ranges, typename PayloadEmitter> 2443 static void emitRangeList( 2444 DwarfDebug &DD, AsmPrinter *Asm, MCSymbol *Sym, const Ranges &R, 2445 const DwarfCompileUnit &CU, unsigned BaseAddressx, unsigned OffsetPair, 2446 unsigned StartxLength, unsigned EndOfList, 2447 StringRef (*StringifyEnum)(unsigned), 2448 bool ShouldUseBaseAddress, 2449 PayloadEmitter EmitPayload) { 2450 2451 auto Size = Asm->MAI->getCodePointerSize(); 2452 bool UseDwarf5 = DD.getDwarfVersion() >= 5; 2453 2454 // Emit our symbol so we can find the beginning of the range. 2455 Asm->OutStreamer->emitLabel(Sym); 2456 2457 // Gather all the ranges that apply to the same section so they can share 2458 // a base address entry. 2459 MapVector<const MCSection *, std::vector<decltype(&*R.begin())>> SectionRanges; 2460 2461 for (const auto &Range : R) 2462 SectionRanges[&Range.Begin->getSection()].push_back(&Range); 2463 2464 const MCSymbol *CUBase = CU.getBaseAddress(); 2465 bool BaseIsSet = false; 2466 for (const auto &P : SectionRanges) { 2467 auto *Base = CUBase; 2468 if (!Base && ShouldUseBaseAddress) { 2469 const MCSymbol *Begin = P.second.front()->Begin; 2470 const MCSymbol *NewBase = DD.getSectionLabel(&Begin->getSection()); 2471 if (!UseDwarf5) { 2472 Base = NewBase; 2473 BaseIsSet = true; 2474 Asm->OutStreamer->emitIntValue(-1, Size); 2475 Asm->OutStreamer->AddComment(" base address"); 2476 Asm->OutStreamer->emitSymbolValue(Base, Size); 2477 } else if (NewBase != Begin || P.second.size() > 1) { 2478 // Only use a base address if 2479 // * the existing pool address doesn't match (NewBase != Begin) 2480 // * or, there's more than one entry to share the base address 2481 Base = NewBase; 2482 BaseIsSet = true; 2483 Asm->OutStreamer->AddComment(StringifyEnum(BaseAddressx)); 2484 Asm->emitInt8(BaseAddressx); 2485 Asm->OutStreamer->AddComment(" base address index"); 2486 Asm->emitULEB128(DD.getAddressPool().getIndex(Base)); 2487 } 2488 } else if (BaseIsSet && !UseDwarf5) { 2489 BaseIsSet = false; 2490 assert(!Base); 2491 Asm->OutStreamer->emitIntValue(-1, Size); 2492 Asm->OutStreamer->emitIntValue(0, Size); 2493 } 2494 2495 for (const auto *RS : P.second) { 2496 const MCSymbol *Begin = RS->Begin; 2497 const MCSymbol *End = RS->End; 2498 assert(Begin && "Range without a begin symbol?"); 2499 assert(End && "Range without an end symbol?"); 2500 if (Base) { 2501 if (UseDwarf5) { 2502 // Emit offset_pair when we have a base. 2503 Asm->OutStreamer->AddComment(StringifyEnum(OffsetPair)); 2504 Asm->emitInt8(OffsetPair); 2505 Asm->OutStreamer->AddComment(" starting offset"); 2506 Asm->emitLabelDifferenceAsULEB128(Begin, Base); 2507 Asm->OutStreamer->AddComment(" ending offset"); 2508 Asm->emitLabelDifferenceAsULEB128(End, Base); 2509 } else { 2510 Asm->emitLabelDifference(Begin, Base, Size); 2511 Asm->emitLabelDifference(End, Base, Size); 2512 } 2513 } else if (UseDwarf5) { 2514 Asm->OutStreamer->AddComment(StringifyEnum(StartxLength)); 2515 Asm->emitInt8(StartxLength); 2516 Asm->OutStreamer->AddComment(" start index"); 2517 Asm->emitULEB128(DD.getAddressPool().getIndex(Begin)); 2518 Asm->OutStreamer->AddComment(" length"); 2519 Asm->emitLabelDifferenceAsULEB128(End, Begin); 2520 } else { 2521 Asm->OutStreamer->emitSymbolValue(Begin, Size); 2522 Asm->OutStreamer->emitSymbolValue(End, Size); 2523 } 2524 EmitPayload(*RS); 2525 } 2526 } 2527 2528 if (UseDwarf5) { 2529 Asm->OutStreamer->AddComment(StringifyEnum(EndOfList)); 2530 Asm->emitInt8(EndOfList); 2531 } else { 2532 // Terminate the list with two 0 values. 2533 Asm->OutStreamer->emitIntValue(0, Size); 2534 Asm->OutStreamer->emitIntValue(0, Size); 2535 } 2536 } 2537 2538 // Handles emission of both debug_loclist / debug_loclist.dwo 2539 static void emitLocList(DwarfDebug &DD, AsmPrinter *Asm, const DebugLocStream::List &List) { 2540 emitRangeList(DD, Asm, List.Label, DD.getDebugLocs().getEntries(List), 2541 *List.CU, dwarf::DW_LLE_base_addressx, 2542 dwarf::DW_LLE_offset_pair, dwarf::DW_LLE_startx_length, 2543 dwarf::DW_LLE_end_of_list, llvm::dwarf::LocListEncodingString, 2544 /* ShouldUseBaseAddress */ true, 2545 [&](const DebugLocStream::Entry &E) { 2546 DD.emitDebugLocEntryLocation(E, List.CU); 2547 }); 2548 } 2549 2550 void DwarfDebug::emitDebugLocImpl(MCSection *Sec) { 2551 if (DebugLocs.getLists().empty()) 2552 return; 2553 2554 Asm->OutStreamer->SwitchSection(Sec); 2555 2556 MCSymbol *TableEnd = nullptr; 2557 if (getDwarfVersion() >= 5) 2558 TableEnd = emitLoclistsTableHeader(Asm, *this); 2559 2560 for (const auto &List : DebugLocs.getLists()) 2561 emitLocList(*this, Asm, List); 2562 2563 if (TableEnd) 2564 Asm->OutStreamer->emitLabel(TableEnd); 2565 } 2566 2567 // Emit locations into the .debug_loc/.debug_loclists section. 2568 void DwarfDebug::emitDebugLoc() { 2569 emitDebugLocImpl( 2570 getDwarfVersion() >= 5 2571 ? Asm->getObjFileLowering().getDwarfLoclistsSection() 2572 : Asm->getObjFileLowering().getDwarfLocSection()); 2573 } 2574 2575 // Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section. 2576 void DwarfDebug::emitDebugLocDWO() { 2577 if (getDwarfVersion() >= 5) { 2578 emitDebugLocImpl( 2579 Asm->getObjFileLowering().getDwarfLoclistsDWOSection()); 2580 2581 return; 2582 } 2583 2584 for (const auto &List : DebugLocs.getLists()) { 2585 Asm->OutStreamer->SwitchSection( 2586 Asm->getObjFileLowering().getDwarfLocDWOSection()); 2587 Asm->OutStreamer->emitLabel(List.Label); 2588 2589 for (const auto &Entry : DebugLocs.getEntries(List)) { 2590 // GDB only supports startx_length in pre-standard split-DWARF. 2591 // (in v5 standard loclists, it currently* /only/ supports base_address + 2592 // offset_pair, so the implementations can't really share much since they 2593 // need to use different representations) 2594 // * as of October 2018, at least 2595 // 2596 // In v5 (see emitLocList), this uses SectionLabels to reuse existing 2597 // addresses in the address pool to minimize object size/relocations. 2598 Asm->emitInt8(dwarf::DW_LLE_startx_length); 2599 unsigned idx = AddrPool.getIndex(Entry.Begin); 2600 Asm->emitULEB128(idx); 2601 // Also the pre-standard encoding is slightly different, emitting this as 2602 // an address-length entry here, but its a ULEB128 in DWARFv5 loclists. 2603 Asm->emitLabelDifference(Entry.End, Entry.Begin, 4); 2604 emitDebugLocEntryLocation(Entry, List.CU); 2605 } 2606 Asm->emitInt8(dwarf::DW_LLE_end_of_list); 2607 } 2608 } 2609 2610 struct ArangeSpan { 2611 const MCSymbol *Start, *End; 2612 }; 2613 2614 // Emit a debug aranges section, containing a CU lookup for any 2615 // address we can tie back to a CU. 2616 void DwarfDebug::emitDebugARanges() { 2617 // Provides a unique id per text section. 2618 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; 2619 2620 // Filter labels by section. 2621 for (const SymbolCU &SCU : ArangeLabels) { 2622 if (SCU.Sym->isInSection()) { 2623 // Make a note of this symbol and it's section. 2624 MCSection *Section = &SCU.Sym->getSection(); 2625 if (!Section->getKind().isMetadata()) 2626 SectionMap[Section].push_back(SCU); 2627 } else { 2628 // Some symbols (e.g. common/bss on mach-o) can have no section but still 2629 // appear in the output. This sucks as we rely on sections to build 2630 // arange spans. We can do it without, but it's icky. 2631 SectionMap[nullptr].push_back(SCU); 2632 } 2633 } 2634 2635 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; 2636 2637 for (auto &I : SectionMap) { 2638 MCSection *Section = I.first; 2639 SmallVector<SymbolCU, 8> &List = I.second; 2640 if (List.size() < 1) 2641 continue; 2642 2643 // If we have no section (e.g. common), just write out 2644 // individual spans for each symbol. 2645 if (!Section) { 2646 for (const SymbolCU &Cur : List) { 2647 ArangeSpan Span; 2648 Span.Start = Cur.Sym; 2649 Span.End = nullptr; 2650 assert(Cur.CU); 2651 Spans[Cur.CU].push_back(Span); 2652 } 2653 continue; 2654 } 2655 2656 // Sort the symbols by offset within the section. 2657 llvm::stable_sort(List, [&](const SymbolCU &A, const SymbolCU &B) { 2658 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0; 2659 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0; 2660 2661 // Symbols with no order assigned should be placed at the end. 2662 // (e.g. section end labels) 2663 if (IA == 0) 2664 return false; 2665 if (IB == 0) 2666 return true; 2667 return IA < IB; 2668 }); 2669 2670 // Insert a final terminator. 2671 List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); 2672 2673 // Build spans between each label. 2674 const MCSymbol *StartSym = List[0].Sym; 2675 for (size_t n = 1, e = List.size(); n < e; n++) { 2676 const SymbolCU &Prev = List[n - 1]; 2677 const SymbolCU &Cur = List[n]; 2678 2679 // Try and build the longest span we can within the same CU. 2680 if (Cur.CU != Prev.CU) { 2681 ArangeSpan Span; 2682 Span.Start = StartSym; 2683 Span.End = Cur.Sym; 2684 assert(Prev.CU); 2685 Spans[Prev.CU].push_back(Span); 2686 StartSym = Cur.Sym; 2687 } 2688 } 2689 } 2690 2691 // Start the dwarf aranges section. 2692 Asm->OutStreamer->SwitchSection( 2693 Asm->getObjFileLowering().getDwarfARangesSection()); 2694 2695 unsigned PtrSize = Asm->MAI->getCodePointerSize(); 2696 2697 // Build a list of CUs used. 2698 std::vector<DwarfCompileUnit *> CUs; 2699 for (const auto &it : Spans) { 2700 DwarfCompileUnit *CU = it.first; 2701 CUs.push_back(CU); 2702 } 2703 2704 // Sort the CU list (again, to ensure consistent output order). 2705 llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { 2706 return A->getUniqueID() < B->getUniqueID(); 2707 }); 2708 2709 // Emit an arange table for each CU we used. 2710 for (DwarfCompileUnit *CU : CUs) { 2711 std::vector<ArangeSpan> &List = Spans[CU]; 2712 2713 // Describe the skeleton CU's offset and length, not the dwo file's. 2714 if (auto *Skel = CU->getSkeleton()) 2715 CU = Skel; 2716 2717 // Emit size of content not including length itself. 2718 unsigned ContentSize = 2719 sizeof(int16_t) + // DWARF ARange version number 2720 sizeof(int32_t) + // Offset of CU in the .debug_info section 2721 sizeof(int8_t) + // Pointer Size (in bytes) 2722 sizeof(int8_t); // Segment Size (in bytes) 2723 2724 unsigned TupleSize = PtrSize * 2; 2725 2726 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. 2727 unsigned Padding = 2728 offsetToAlignment(sizeof(int32_t) + ContentSize, Align(TupleSize)); 2729 2730 ContentSize += Padding; 2731 ContentSize += (List.size() + 1) * TupleSize; 2732 2733 // For each compile unit, write the list of spans it covers. 2734 Asm->OutStreamer->AddComment("Length of ARange Set"); 2735 Asm->emitInt32(ContentSize); 2736 Asm->OutStreamer->AddComment("DWARF Arange version number"); 2737 Asm->emitInt16(dwarf::DW_ARANGES_VERSION); 2738 Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); 2739 emitSectionReference(*CU); 2740 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 2741 Asm->emitInt8(PtrSize); 2742 Asm->OutStreamer->AddComment("Segment Size (in bytes)"); 2743 Asm->emitInt8(0); 2744 2745 Asm->OutStreamer->emitFill(Padding, 0xff); 2746 2747 for (const ArangeSpan &Span : List) { 2748 Asm->emitLabelReference(Span.Start, PtrSize); 2749 2750 // Calculate the size as being from the span start to it's end. 2751 if (Span.End) { 2752 Asm->emitLabelDifference(Span.End, Span.Start, PtrSize); 2753 } else { 2754 // For symbols without an end marker (e.g. common), we 2755 // write a single arange entry containing just that one symbol. 2756 uint64_t Size = SymSize[Span.Start]; 2757 if (Size == 0) 2758 Size = 1; 2759 2760 Asm->OutStreamer->emitIntValue(Size, PtrSize); 2761 } 2762 } 2763 2764 Asm->OutStreamer->AddComment("ARange terminator"); 2765 Asm->OutStreamer->emitIntValue(0, PtrSize); 2766 Asm->OutStreamer->emitIntValue(0, PtrSize); 2767 } 2768 } 2769 2770 /// Emit a single range list. We handle both DWARF v5 and earlier. 2771 static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm, 2772 const RangeSpanList &List) { 2773 emitRangeList(DD, Asm, List.Label, List.Ranges, *List.CU, 2774 dwarf::DW_RLE_base_addressx, dwarf::DW_RLE_offset_pair, 2775 dwarf::DW_RLE_startx_length, dwarf::DW_RLE_end_of_list, 2776 llvm::dwarf::RangeListEncodingString, 2777 List.CU->getCUNode()->getRangesBaseAddress() || 2778 DD.getDwarfVersion() >= 5, 2779 [](auto) {}); 2780 } 2781 2782 void DwarfDebug::emitDebugRangesImpl(const DwarfFile &Holder, MCSection *Section) { 2783 if (Holder.getRangeLists().empty()) 2784 return; 2785 2786 assert(useRangesSection()); 2787 assert(!CUMap.empty()); 2788 assert(llvm::any_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { 2789 return !Pair.second->getCUNode()->isDebugDirectivesOnly(); 2790 })); 2791 2792 Asm->OutStreamer->SwitchSection(Section); 2793 2794 MCSymbol *TableEnd = nullptr; 2795 if (getDwarfVersion() >= 5) 2796 TableEnd = emitRnglistsTableHeader(Asm, Holder); 2797 2798 for (const RangeSpanList &List : Holder.getRangeLists()) 2799 emitRangeList(*this, Asm, List); 2800 2801 if (TableEnd) 2802 Asm->OutStreamer->emitLabel(TableEnd); 2803 } 2804 2805 /// Emit address ranges into the .debug_ranges section or into the DWARF v5 2806 /// .debug_rnglists section. 2807 void DwarfDebug::emitDebugRanges() { 2808 const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2809 2810 emitDebugRangesImpl(Holder, 2811 getDwarfVersion() >= 5 2812 ? Asm->getObjFileLowering().getDwarfRnglistsSection() 2813 : Asm->getObjFileLowering().getDwarfRangesSection()); 2814 } 2815 2816 void DwarfDebug::emitDebugRangesDWO() { 2817 emitDebugRangesImpl(InfoHolder, 2818 Asm->getObjFileLowering().getDwarfRnglistsDWOSection()); 2819 } 2820 2821 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { 2822 for (auto *MN : Nodes) { 2823 if (auto *M = dyn_cast<DIMacro>(MN)) 2824 emitMacro(*M); 2825 else if (auto *F = dyn_cast<DIMacroFile>(MN)) 2826 emitMacroFile(*F, U); 2827 else 2828 llvm_unreachable("Unexpected DI type!"); 2829 } 2830 } 2831 2832 void DwarfDebug::emitMacro(DIMacro &M) { 2833 Asm->emitULEB128(M.getMacinfoType()); 2834 Asm->emitULEB128(M.getLine()); 2835 StringRef Name = M.getName(); 2836 StringRef Value = M.getValue(); 2837 Asm->OutStreamer->emitBytes(Name); 2838 if (!Value.empty()) { 2839 // There should be one space between macro name and macro value. 2840 Asm->emitInt8(' '); 2841 Asm->OutStreamer->emitBytes(Value); 2842 } 2843 Asm->emitInt8('\0'); 2844 } 2845 2846 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { 2847 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); 2848 Asm->emitULEB128(dwarf::DW_MACINFO_start_file); 2849 Asm->emitULEB128(F.getLine()); 2850 Asm->emitULEB128(U.getOrCreateSourceID(F.getFile())); 2851 handleMacroNodes(F.getElements(), U); 2852 Asm->emitULEB128(dwarf::DW_MACINFO_end_file); 2853 } 2854 2855 void DwarfDebug::emitDebugMacinfoImpl(MCSection *Section) { 2856 for (const auto &P : CUMap) { 2857 auto &TheCU = *P.second; 2858 auto *SkCU = TheCU.getSkeleton(); 2859 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 2860 auto *CUNode = cast<DICompileUnit>(P.first); 2861 DIMacroNodeArray Macros = CUNode->getMacros(); 2862 if (Macros.empty()) 2863 continue; 2864 Asm->OutStreamer->SwitchSection(Section); 2865 Asm->OutStreamer->emitLabel(U.getMacroLabelBegin()); 2866 handleMacroNodes(Macros, U); 2867 Asm->OutStreamer->AddComment("End Of Macro List Mark"); 2868 Asm->emitInt8(0); 2869 } 2870 } 2871 2872 /// Emit macros into a debug macinfo section. 2873 void DwarfDebug::emitDebugMacinfo() { 2874 emitDebugMacinfoImpl(Asm->getObjFileLowering().getDwarfMacinfoSection()); 2875 } 2876 2877 void DwarfDebug::emitDebugMacinfoDWO() { 2878 emitDebugMacinfoImpl(Asm->getObjFileLowering().getDwarfMacinfoDWOSection()); 2879 } 2880 2881 // DWARF5 Experimental Separate Dwarf emitters. 2882 2883 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, 2884 std::unique_ptr<DwarfCompileUnit> NewU) { 2885 2886 if (!CompilationDir.empty()) 2887 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 2888 addGnuPubAttributes(*NewU, Die); 2889 2890 SkeletonHolder.addUnit(std::move(NewU)); 2891 } 2892 2893 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { 2894 2895 auto OwnedUnit = std::make_unique<DwarfCompileUnit>( 2896 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder, 2897 UnitKind::Skeleton); 2898 DwarfCompileUnit &NewCU = *OwnedUnit; 2899 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 2900 2901 NewCU.initStmtList(); 2902 2903 if (useSegmentedStringOffsetsTable()) 2904 NewCU.addStringOffsetsStart(); 2905 2906 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); 2907 2908 return NewCU; 2909 } 2910 2911 // Emit the .debug_info.dwo section for separated dwarf. This contains the 2912 // compile units that would normally be in debug_info. 2913 void DwarfDebug::emitDebugInfoDWO() { 2914 assert(useSplitDwarf() && "No split dwarf debug info?"); 2915 // Don't emit relocations into the dwo file. 2916 InfoHolder.emitUnits(/* UseOffsets */ true); 2917 } 2918 2919 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the 2920 // abbreviations for the .debug_info.dwo section. 2921 void DwarfDebug::emitDebugAbbrevDWO() { 2922 assert(useSplitDwarf() && "No split dwarf?"); 2923 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); 2924 } 2925 2926 void DwarfDebug::emitDebugLineDWO() { 2927 assert(useSplitDwarf() && "No split dwarf?"); 2928 SplitTypeUnitFileTable.Emit( 2929 *Asm->OutStreamer, MCDwarfLineTableParams(), 2930 Asm->getObjFileLowering().getDwarfLineDWOSection()); 2931 } 2932 2933 void DwarfDebug::emitStringOffsetsTableHeaderDWO() { 2934 assert(useSplitDwarf() && "No split dwarf?"); 2935 InfoHolder.getStringPool().emitStringOffsetsTableHeader( 2936 *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(), 2937 InfoHolder.getStringOffsetsStartSym()); 2938 } 2939 2940 // Emit the .debug_str.dwo section for separated dwarf. This contains the 2941 // string section and is identical in format to traditional .debug_str 2942 // sections. 2943 void DwarfDebug::emitDebugStrDWO() { 2944 if (useSegmentedStringOffsetsTable()) 2945 emitStringOffsetsTableHeaderDWO(); 2946 assert(useSplitDwarf() && "No split dwarf?"); 2947 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); 2948 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), 2949 OffSec, /* UseRelativeOffsets = */ false); 2950 } 2951 2952 // Emit address pool. 2953 void DwarfDebug::emitDebugAddr() { 2954 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); 2955 } 2956 2957 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { 2958 if (!useSplitDwarf()) 2959 return nullptr; 2960 const DICompileUnit *DIUnit = CU.getCUNode(); 2961 SplitTypeUnitFileTable.maybeSetRootFile( 2962 DIUnit->getDirectory(), DIUnit->getFilename(), 2963 CU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource()); 2964 return &SplitTypeUnitFileTable; 2965 } 2966 2967 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { 2968 MD5 Hash; 2969 Hash.update(Identifier); 2970 // ... take the least significant 8 bytes and return those. Our MD5 2971 // implementation always returns its results in little endian, so we actually 2972 // need the "high" word. 2973 MD5::MD5Result Result; 2974 Hash.final(Result); 2975 return Result.high(); 2976 } 2977 2978 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, 2979 StringRef Identifier, DIE &RefDie, 2980 const DICompositeType *CTy) { 2981 // Fast path if we're building some type units and one has already used the 2982 // address pool we know we're going to throw away all this work anyway, so 2983 // don't bother building dependent types. 2984 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) 2985 return; 2986 2987 auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); 2988 if (!Ins.second) { 2989 CU.addDIETypeSignature(RefDie, Ins.first->second); 2990 return; 2991 } 2992 2993 bool TopLevelType = TypeUnitsUnderConstruction.empty(); 2994 AddrPool.resetUsedFlag(); 2995 2996 auto OwnedUnit = std::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, 2997 getDwoLineTable(CU)); 2998 DwarfTypeUnit &NewTU = *OwnedUnit; 2999 DIE &UnitDie = NewTU.getUnitDie(); 3000 TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); 3001 3002 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 3003 CU.getLanguage()); 3004 3005 uint64_t Signature = makeTypeSignature(Identifier); 3006 NewTU.setTypeSignature(Signature); 3007 Ins.first->second = Signature; 3008 3009 if (useSplitDwarf()) { 3010 MCSection *Section = 3011 getDwarfVersion() <= 4 3012 ? Asm->getObjFileLowering().getDwarfTypesDWOSection() 3013 : Asm->getObjFileLowering().getDwarfInfoDWOSection(); 3014 NewTU.setSection(Section); 3015 } else { 3016 MCSection *Section = 3017 getDwarfVersion() <= 4 3018 ? Asm->getObjFileLowering().getDwarfTypesSection(Signature) 3019 : Asm->getObjFileLowering().getDwarfInfoSection(Signature); 3020 NewTU.setSection(Section); 3021 // Non-split type units reuse the compile unit's line table. 3022 CU.applyStmtList(UnitDie); 3023 } 3024 3025 // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type 3026 // units. 3027 if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) 3028 NewTU.addStringOffsetsStart(); 3029 3030 NewTU.setType(NewTU.createTypeDIE(CTy)); 3031 3032 if (TopLevelType) { 3033 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); 3034 TypeUnitsUnderConstruction.clear(); 3035 3036 // Types referencing entries in the address table cannot be placed in type 3037 // units. 3038 if (AddrPool.hasBeenUsed()) { 3039 3040 // Remove all the types built while building this type. 3041 // This is pessimistic as some of these types might not be dependent on 3042 // the type that used an address. 3043 for (const auto &TU : TypeUnitsToAdd) 3044 TypeSignatures.erase(TU.second); 3045 3046 // Construct this type in the CU directly. 3047 // This is inefficient because all the dependent types will be rebuilt 3048 // from scratch, including building them in type units, discovering that 3049 // they depend on addresses, throwing them out and rebuilding them. 3050 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); 3051 return; 3052 } 3053 3054 // If the type wasn't dependent on fission addresses, finish adding the type 3055 // and all its dependent types. 3056 for (auto &TU : TypeUnitsToAdd) { 3057 InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); 3058 InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); 3059 } 3060 } 3061 CU.addDIETypeSignature(RefDie, Signature); 3062 } 3063 3064 DwarfDebug::NonTypeUnitContext::NonTypeUnitContext(DwarfDebug *DD) 3065 : DD(DD), 3066 TypeUnitsUnderConstruction(std::move(DD->TypeUnitsUnderConstruction)) { 3067 DD->TypeUnitsUnderConstruction.clear(); 3068 assert(TypeUnitsUnderConstruction.empty() || !DD->AddrPool.hasBeenUsed()); 3069 } 3070 3071 DwarfDebug::NonTypeUnitContext::~NonTypeUnitContext() { 3072 DD->TypeUnitsUnderConstruction = std::move(TypeUnitsUnderConstruction); 3073 DD->AddrPool.resetUsedFlag(); 3074 } 3075 3076 DwarfDebug::NonTypeUnitContext DwarfDebug::enterNonTypeUnitContext() { 3077 return NonTypeUnitContext(this); 3078 } 3079 3080 // Add the Name along with its companion DIE to the appropriate accelerator 3081 // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for 3082 // AccelTableKind::Apple, we use the table we got as an argument). If 3083 // accelerator tables are disabled, this function does nothing. 3084 template <typename DataT> 3085 void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU, 3086 AccelTable<DataT> &AppleAccel, StringRef Name, 3087 const DIE &Die) { 3088 if (getAccelTableKind() == AccelTableKind::None) 3089 return; 3090 3091 if (getAccelTableKind() != AccelTableKind::Apple && 3092 CU.getNameTableKind() != DICompileUnit::DebugNameTableKind::Default) 3093 return; 3094 3095 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 3096 DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name); 3097 3098 switch (getAccelTableKind()) { 3099 case AccelTableKind::Apple: 3100 AppleAccel.addName(Ref, Die); 3101 break; 3102 case AccelTableKind::Dwarf: 3103 AccelDebugNames.addName(Ref, Die); 3104 break; 3105 case AccelTableKind::Default: 3106 llvm_unreachable("Default should have already been resolved."); 3107 case AccelTableKind::None: 3108 llvm_unreachable("None handled above"); 3109 } 3110 } 3111 3112 void DwarfDebug::addAccelName(const DICompileUnit &CU, StringRef Name, 3113 const DIE &Die) { 3114 addAccelNameImpl(CU, AccelNames, Name, Die); 3115 } 3116 3117 void DwarfDebug::addAccelObjC(const DICompileUnit &CU, StringRef Name, 3118 const DIE &Die) { 3119 // ObjC names go only into the Apple accelerator tables. 3120 if (getAccelTableKind() == AccelTableKind::Apple) 3121 addAccelNameImpl(CU, AccelObjC, Name, Die); 3122 } 3123 3124 void DwarfDebug::addAccelNamespace(const DICompileUnit &CU, StringRef Name, 3125 const DIE &Die) { 3126 addAccelNameImpl(CU, AccelNamespace, Name, Die); 3127 } 3128 3129 void DwarfDebug::addAccelType(const DICompileUnit &CU, StringRef Name, 3130 const DIE &Die, char Flags) { 3131 addAccelNameImpl(CU, AccelTypes, Name, Die); 3132 } 3133 3134 uint16_t DwarfDebug::getDwarfVersion() const { 3135 return Asm->OutStreamer->getContext().getDwarfVersion(); 3136 } 3137 3138 const MCSymbol *DwarfDebug::getSectionLabel(const MCSection *S) { 3139 return SectionLabels.find(S)->second; 3140 } 3141 void DwarfDebug::insertSectionLabel(const MCSymbol *S) { 3142 SectionLabels.insert(std::make_pair(&S->getSection(), S)); 3143 } 3144