1 #include "DwarfCompileUnit.h" 2 #include "DwarfExpression.h" 3 #include "llvm/CodeGen/MachineFunction.h" 4 #include "llvm/IR/Constants.h" 5 #include "llvm/IR/DataLayout.h" 6 #include "llvm/IR/GlobalValue.h" 7 #include "llvm/IR/GlobalVariable.h" 8 #include "llvm/IR/Instruction.h" 9 #include "llvm/MC/MCAsmInfo.h" 10 #include "llvm/MC/MCStreamer.h" 11 #include "llvm/Target/TargetFrameLowering.h" 12 #include "llvm/Target/TargetLoweringObjectFile.h" 13 #include "llvm/Target/TargetMachine.h" 14 #include "llvm/Target/TargetRegisterInfo.h" 15 #include "llvm/Target/TargetSubtargetInfo.h" 16 17 namespace llvm { 18 19 DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, 20 AsmPrinter *A, DwarfDebug *DW, 21 DwarfFile *DWU) 22 : DwarfUnit(dwarf::DW_TAG_compile_unit, Node, A, DW, DWU), UniqueID(UID), 23 Skeleton(nullptr), BaseAddress(nullptr) { 24 insertDIE(Node, &getUnitDie()); 25 MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin"); 26 } 27 28 /// addLabelAddress - Add a dwarf label attribute data and value using 29 /// DW_FORM_addr or DW_FORM_GNU_addr_index. 30 /// 31 void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 32 const MCSymbol *Label) { 33 34 // Don't use the address pool in non-fission or in the skeleton unit itself. 35 // FIXME: Once GDB supports this, it's probably worthwhile using the address 36 // pool from the skeleton - maybe even in non-fission (possibly fewer 37 // relocations by sharing them in the pool, but we have other ideas about how 38 // to reduce the number of relocations as well/instead). 39 if (!DD->useSplitDwarf() || !Skeleton) 40 return addLocalLabelAddress(Die, Attribute, Label); 41 42 if (Label) 43 DD->addArangeLabel(SymbolCU(this, Label)); 44 45 unsigned idx = DD->getAddressPool().getIndex(Label); 46 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_GNU_addr_index, 47 DIEInteger(idx)); 48 } 49 50 void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, 51 dwarf::Attribute Attribute, 52 const MCSymbol *Label) { 53 if (Label) 54 DD->addArangeLabel(SymbolCU(this, Label)); 55 56 if (Label) 57 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 58 DIELabel(Label)); 59 else 60 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 61 DIEInteger(0)); 62 } 63 64 unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, 65 StringRef DirName) { 66 // If we print assembly, we can't separate .file entries according to 67 // compile units. Thus all files will belong to the default compile unit. 68 69 // FIXME: add a better feature test than hasRawTextSupport. Even better, 70 // extend .file to support this. 71 return Asm->OutStreamer->EmitDwarfFileDirective( 72 0, DirName, FileName, 73 Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID()); 74 } 75 76 /// getOrCreateGlobalVariableDIE - get or create global variable DIE. 77 DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( 78 const DIGlobalVariable *GV, const GlobalVariable *Global) { 79 // Check for pre-existence. 80 if (DIE *Die = getDIE(GV)) 81 return Die; 82 83 assert(GV); 84 85 auto *GVContext = GV->getScope(); 86 auto *GTy = DD->resolve(GV->getType()); 87 88 // Construct the context before querying for the existence of the DIE in 89 // case such construction creates the DIE. 90 DIE *ContextDIE = getOrCreateContextDIE(GVContext); 91 92 // Add to map. 93 DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); 94 DIScope *DeclContext; 95 if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { 96 DeclContext = resolve(SDMDecl->getScope()); 97 assert(SDMDecl->isStaticMember() && "Expected static member decl"); 98 assert(GV->isDefinition()); 99 // We need the declaration DIE that is in the static member's class. 100 DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); 101 addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); 102 } else { 103 DeclContext = GV->getScope(); 104 // Add name and type. 105 addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); 106 addType(*VariableDIE, GTy); 107 108 // Add scoping info. 109 if (!GV->isLocalToUnit()) 110 addFlag(*VariableDIE, dwarf::DW_AT_external); 111 112 // Add line number info. 113 addSourceLine(*VariableDIE, GV); 114 } 115 116 if (!GV->isDefinition()) 117 addFlag(*VariableDIE, dwarf::DW_AT_declaration); 118 else 119 addGlobalName(GV->getName(), *VariableDIE, DeclContext); 120 121 if (uint32_t AlignInBytes = GV->getAlignInBytes()) 122 addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 123 AlignInBytes); 124 125 // Add location. 126 bool addToAccelTable = false; 127 128 DIExpression *Expr = GV->getExpr(); 129 130 // For compatibility with DWARF 3 and earlier, 131 // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) becomes 132 // DW_AT_const_value(X). 133 if (Expr && Expr->getNumElements() == 3 && 134 Expr->getElement(0) == dwarf::DW_OP_constu && 135 Expr->getElement(2) == dwarf::DW_OP_stack_value) { 136 addConstantValue(*VariableDIE, /*Unsigned=*/true, Expr->getElement(1)); 137 // We cannot describe the location of dllimport'd variables: the computation 138 // of their address requires loads from the IAT. 139 } else if (!Global || !Global->hasDLLImportStorageClass()) { 140 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 141 if (Global) { 142 addToAccelTable = true; 143 const MCSymbol *Sym = Asm->getSymbol(Global); 144 if (Global->isThreadLocal()) { 145 if (Asm->TM.Options.EmulatedTLS) { 146 // TODO: add debug info for emulated thread local mode. 147 } else { 148 // FIXME: Make this work with -gsplit-dwarf. 149 unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 150 assert((PointerSize == 4 || PointerSize == 8) && 151 "Add support for other sizes if necessary"); 152 // Based on GCC's support for TLS: 153 if (!DD->useSplitDwarf()) { 154 // 1) Start with a constNu of the appropriate pointer size 155 addUInt(*Loc, dwarf::DW_FORM_data1, PointerSize == 4 156 ? dwarf::DW_OP_const4u 157 : dwarf::DW_OP_const8u); 158 // 2) containing the (relocated) offset of the TLS variable 159 // within the module's TLS block. 160 addExpr(*Loc, dwarf::DW_FORM_udata, 161 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); 162 } else { 163 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 164 addUInt(*Loc, dwarf::DW_FORM_udata, 165 DD->getAddressPool().getIndex(Sym, /* TLS */ true)); 166 } 167 // 3) followed by an OP to make the debugger do a TLS lookup. 168 addUInt(*Loc, dwarf::DW_FORM_data1, 169 DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address 170 : dwarf::DW_OP_form_tls_address); 171 } 172 } else { 173 DD->addArangeLabel(SymbolCU(this, Sym)); 174 addOpAddress(*Loc, Sym); 175 } 176 177 if (Expr) { 178 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 179 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end()); 180 } 181 } 182 183 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 184 185 if (DD->useAllLinkageNames()) 186 addLinkageName(*VariableDIE, GV->getLinkageName()); 187 } 188 189 if (addToAccelTable) { 190 DD->addAccelName(GV->getName(), *VariableDIE); 191 192 // If the linkage name is different than the name, go ahead and output 193 // that as well into the name table. 194 if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName()) 195 DD->addAccelName(GV->getLinkageName(), *VariableDIE); 196 } 197 198 return VariableDIE; 199 } 200 201 void DwarfCompileUnit::addRange(RangeSpan Range) { 202 bool SameAsPrevCU = this == DD->getPrevCU(); 203 DD->setPrevCU(this); 204 // If we have no current ranges just add the range and return, otherwise, 205 // check the current section and CU against the previous section and CU we 206 // emitted into and the subprogram was contained within. If these are the 207 // same then extend our current range, otherwise add this as a new range. 208 if (CURanges.empty() || !SameAsPrevCU || 209 (&CURanges.back().getEnd()->getSection() != 210 &Range.getEnd()->getSection())) { 211 CURanges.push_back(Range); 212 return; 213 } 214 215 CURanges.back().setEnd(Range.getEnd()); 216 } 217 218 DIE::value_iterator 219 DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 220 const MCSymbol *Label, const MCSymbol *Sec) { 221 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 222 return addLabel(Die, Attribute, 223 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 224 : dwarf::DW_FORM_data4, 225 Label); 226 return addSectionDelta(Die, Attribute, Label, Sec); 227 } 228 229 void DwarfCompileUnit::initStmtList() { 230 // Define start line table label for each Compile Unit. 231 MCSymbol *LineTableStartSym = 232 Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); 233 234 // DW_AT_stmt_list is a offset of line number information for this 235 // compile unit in debug_line section. For split dwarf this is 236 // left in the skeleton CU and so not included. 237 // The line table entries are not always emitted in assembly, so it 238 // is not okay to use line_table_start here. 239 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 240 StmtListValue = 241 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym, 242 TLOF.getDwarfLineSection()->getBeginSymbol()); 243 } 244 245 void DwarfCompileUnit::applyStmtList(DIE &D) { 246 D.addValue(DIEValueAllocator, *StmtListValue); 247 } 248 249 void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, 250 const MCSymbol *End) { 251 assert(Begin && "Begin label should not be null!"); 252 assert(End && "End label should not be null!"); 253 assert(Begin->isDefined() && "Invalid starting label"); 254 assert(End->isDefined() && "Invalid end label"); 255 256 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); 257 if (DD->getDwarfVersion() < 4) 258 addLabelAddress(D, dwarf::DW_AT_high_pc, End); 259 else 260 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); 261 } 262 263 // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc 264 // and DW_AT_high_pc attributes. If there are global variables in this 265 // scope then create and insert DIEs for these variables. 266 DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { 267 DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); 268 269 attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd()); 270 if (DD->useAppleExtensionAttributes() && 271 !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( 272 *DD->getCurrentFunction())) 273 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); 274 275 // Only include DW_AT_frame_base in full debug info 276 if (!includeMinimalInlineScopes()) { 277 const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo(); 278 MachineLocation Location(RI->getFrameRegister(*Asm->MF)); 279 if (RI->isPhysicalRegister(Location.getReg())) 280 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); 281 } 282 283 // Add name to the name table, we do this here because we're guaranteed 284 // to have concrete versions of our DW_TAG_subprogram nodes. 285 DD->addSubprogramNames(SP, *SPDie); 286 287 return *SPDie; 288 } 289 290 // Construct a DIE for this scope. 291 void DwarfCompileUnit::constructScopeDIE( 292 LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { 293 if (!Scope || !Scope->getScopeNode()) 294 return; 295 296 auto *DS = Scope->getScopeNode(); 297 298 assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && 299 "Only handle inlined subprograms here, use " 300 "constructSubprogramScopeDIE for non-inlined " 301 "subprograms"); 302 303 SmallVector<DIE *, 8> Children; 304 305 // We try to create the scope DIE first, then the children DIEs. This will 306 // avoid creating un-used children then removing them later when we find out 307 // the scope DIE is null. 308 DIE *ScopeDIE; 309 if (Scope->getParent() && isa<DISubprogram>(DS)) { 310 ScopeDIE = constructInlinedScopeDIE(Scope); 311 if (!ScopeDIE) 312 return; 313 // We create children when the scope DIE is not null. 314 createScopeChildrenDIE(Scope, Children); 315 } else { 316 // Early exit when we know the scope DIE is going to be null. 317 if (DD->isLexicalScopeDIENull(Scope)) 318 return; 319 320 unsigned ChildScopeCount; 321 322 // We create children here when we know the scope DIE is not going to be 323 // null and the children will be added to the scope DIE. 324 createScopeChildrenDIE(Scope, Children, &ChildScopeCount); 325 326 // Skip imported directives in gmlt-like data. 327 if (!includeMinimalInlineScopes()) { 328 // There is no need to emit empty lexical block DIE. 329 for (const auto *IE : ImportedEntities[DS]) 330 Children.push_back( 331 constructImportedEntityDIE(cast<DIImportedEntity>(IE))); 332 } 333 334 // If there are only other scopes as children, put them directly in the 335 // parent instead, as this scope would serve no purpose. 336 if (Children.size() == ChildScopeCount) { 337 FinalChildren.insert(FinalChildren.end(), 338 std::make_move_iterator(Children.begin()), 339 std::make_move_iterator(Children.end())); 340 return; 341 } 342 ScopeDIE = constructLexicalScopeDIE(Scope); 343 assert(ScopeDIE && "Scope DIE should not be null."); 344 } 345 346 // Add children 347 for (auto &I : Children) 348 ScopeDIE->addChild(std::move(I)); 349 350 FinalChildren.push_back(std::move(ScopeDIE)); 351 } 352 353 DIE::value_iterator 354 DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 355 const MCSymbol *Hi, const MCSymbol *Lo) { 356 return Die.addValue(DIEValueAllocator, Attribute, 357 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 358 : dwarf::DW_FORM_data4, 359 new (DIEValueAllocator) DIEDelta(Hi, Lo)); 360 } 361 362 void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, 363 SmallVector<RangeSpan, 2> Range) { 364 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 365 366 // Emit offset in .debug_range as a relocatable label. emitDIE will handle 367 // emitting it appropriately. 368 const MCSymbol *RangeSectionSym = 369 TLOF.getDwarfRangesSection()->getBeginSymbol(); 370 371 RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range)); 372 373 // Under fission, ranges are specified by constant offsets relative to the 374 // CU's DW_AT_GNU_ranges_base. 375 if (isDwoUnit()) 376 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), 377 RangeSectionSym); 378 else 379 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), 380 RangeSectionSym); 381 382 // Add the range list to the set of ranges to be emitted. 383 (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List)); 384 } 385 386 void DwarfCompileUnit::attachRangesOrLowHighPC( 387 DIE &Die, SmallVector<RangeSpan, 2> Ranges) { 388 if (Ranges.size() == 1) { 389 const auto &single = Ranges.front(); 390 attachLowHighPC(Die, single.getStart(), single.getEnd()); 391 } else 392 addScopeRangeList(Die, std::move(Ranges)); 393 } 394 395 void DwarfCompileUnit::attachRangesOrLowHighPC( 396 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { 397 SmallVector<RangeSpan, 2> List; 398 List.reserve(Ranges.size()); 399 for (const InsnRange &R : Ranges) 400 List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first), 401 DD->getLabelAfterInsn(R.second))); 402 attachRangesOrLowHighPC(Die, std::move(List)); 403 } 404 405 // This scope represents inlined body of a function. Construct DIE to 406 // represent this concrete inlined copy of the function. 407 DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { 408 assert(Scope->getScopeNode()); 409 auto *DS = Scope->getScopeNode(); 410 auto *InlinedSP = getDISubprogram(DS); 411 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 412 // was inlined from another compile unit. 413 DIE *OriginDIE = DU->getAbstractSPDies()[InlinedSP]; 414 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); 415 416 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); 417 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); 418 419 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 420 421 // Add the call site information to the DIE. 422 const DILocation *IA = Scope->getInlinedAt(); 423 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, 424 getOrCreateSourceID(IA->getFilename(), IA->getDirectory())); 425 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); 426 if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4) 427 addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, 428 IA->getDiscriminator()); 429 430 // Add name to the name table, we do this here because we're guaranteed 431 // to have concrete versions of our DW_TAG_inlined_subprogram nodes. 432 DD->addSubprogramNames(InlinedSP, *ScopeDIE); 433 434 return ScopeDIE; 435 } 436 437 // Construct new DW_TAG_lexical_block for this scope and attach 438 // DW_AT_low_pc/DW_AT_high_pc labels. 439 DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { 440 if (DD->isLexicalScopeDIENull(Scope)) 441 return nullptr; 442 443 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); 444 if (Scope->isAbstractScope()) 445 return ScopeDIE; 446 447 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 448 449 return ScopeDIE; 450 } 451 452 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 453 DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { 454 auto D = constructVariableDIEImpl(DV, Abstract); 455 DV.setDIE(*D); 456 return D; 457 } 458 459 DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, 460 bool Abstract) { 461 // Define variable debug information entry. 462 auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); 463 464 if (Abstract) { 465 applyVariableAttributes(DV, *VariableDie); 466 return VariableDie; 467 } 468 469 // Add variable address. 470 471 unsigned Offset = DV.getDebugLocListIndex(); 472 if (Offset != ~0U) { 473 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); 474 return VariableDie; 475 } 476 477 // Check if variable is described by a DBG_VALUE instruction. 478 if (const MachineInstr *DVInsn = DV.getMInsn()) { 479 assert(DVInsn->getNumOperands() == 4); 480 if (DVInsn->getOperand(0).isReg()) { 481 const MachineOperand RegOp = DVInsn->getOperand(0); 482 // If the second operand is an immediate, this is an indirect value. 483 if (DVInsn->getOperand(1).isImm()) { 484 MachineLocation Location(RegOp.getReg(), 485 DVInsn->getOperand(1).getImm()); 486 addVariableAddress(DV, *VariableDie, Location); 487 } else if (RegOp.getReg()) 488 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); 489 } else if (DVInsn->getOperand(0).isImm()) { 490 // This variable is described by a single constant. 491 // Check whether it has a DIExpression. 492 auto *Expr = DV.getSingleExpression(); 493 if (Expr && Expr->getNumElements()) { 494 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 495 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 496 // If there is an expression, emit raw unsigned bytes. 497 DwarfExpr.AddUnsignedConstant(DVInsn->getOperand(0).getImm()); 498 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end()); 499 addBlock(*VariableDie, dwarf::DW_AT_location, Loc); 500 } else 501 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); 502 } else if (DVInsn->getOperand(0).isFPImm()) 503 addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); 504 else if (DVInsn->getOperand(0).isCImm()) 505 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), 506 DV.getType()); 507 508 return VariableDie; 509 } 510 511 // .. else use frame index. 512 if (DV.getFrameIndex().empty()) 513 return VariableDie; 514 515 auto Expr = DV.getExpression().begin(); 516 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 517 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 518 for (auto FI : DV.getFrameIndex()) { 519 unsigned FrameReg = 0; 520 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); 521 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); 522 assert(Expr != DV.getExpression().end() && "Wrong number of expressions"); 523 DwarfExpr.AddMachineRegIndirect(*Asm->MF->getSubtarget().getRegisterInfo(), 524 FrameReg, Offset); 525 DwarfExpr.AddExpression((*Expr)->expr_op_begin(), (*Expr)->expr_op_end()); 526 ++Expr; 527 } 528 addBlock(*VariableDie, dwarf::DW_AT_location, Loc); 529 530 return VariableDie; 531 } 532 533 DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, 534 const LexicalScope &Scope, 535 DIE *&ObjectPointer) { 536 auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); 537 if (DV.isObjectPointer()) 538 ObjectPointer = Var; 539 return Var; 540 } 541 542 DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, 543 SmallVectorImpl<DIE *> &Children, 544 unsigned *ChildScopeCount) { 545 DIE *ObjectPointer = nullptr; 546 547 for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope)) 548 Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); 549 550 unsigned ChildCountWithoutScopes = Children.size(); 551 552 for (LexicalScope *LS : Scope->getChildren()) 553 constructScopeDIE(LS, Children); 554 555 if (ChildScopeCount) 556 *ChildScopeCount = Children.size() - ChildCountWithoutScopes; 557 558 return ObjectPointer; 559 } 560 561 void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) { 562 assert(Scope && Scope->getScopeNode()); 563 assert(!Scope->getInlinedAt()); 564 assert(!Scope->isAbstractScope()); 565 auto *Sub = cast<DISubprogram>(Scope->getScopeNode()); 566 567 DD->getProcessedSPNodes().insert(Sub); 568 569 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); 570 571 // If this is a variadic function, add an unspecified parameter. 572 DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); 573 574 // Collect lexical scope children first. 575 // ObjectPointer might be a local (non-argument) local variable if it's a 576 // block's synthetic this pointer. 577 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) 578 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); 579 580 // If we have a single element of null, it is a function that returns void. 581 // If we have more than one elements and the last one is null, it is a 582 // variadic function. 583 if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && 584 !includeMinimalInlineScopes()) 585 ScopeDIE.addChild( 586 DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); 587 } 588 589 DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, 590 DIE &ScopeDIE) { 591 // We create children when the scope DIE is not null. 592 SmallVector<DIE *, 8> Children; 593 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); 594 595 // Add children 596 for (auto &I : Children) 597 ScopeDIE.addChild(std::move(I)); 598 599 return ObjectPointer; 600 } 601 602 void DwarfCompileUnit::constructAbstractSubprogramScopeDIE( 603 LexicalScope *Scope) { 604 DIE *&AbsDef = DU->getAbstractSPDies()[Scope->getScopeNode()]; 605 if (AbsDef) 606 return; 607 608 auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 609 610 DIE *ContextDIE; 611 612 if (includeMinimalInlineScopes()) 613 ContextDIE = &getUnitDie(); 614 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with 615 // the important distinction that the debug node is not associated with the 616 // DIE (since the debug node will be associated with the concrete DIE, if 617 // any). It could be refactored to some common utility function. 618 else if (auto *SPDecl = SP->getDeclaration()) { 619 ContextDIE = &getUnitDie(); 620 getOrCreateSubprogramDIE(SPDecl); 621 } else 622 ContextDIE = getOrCreateContextDIE(resolve(SP->getScope())); 623 624 // Passing null as the associated node because the abstract definition 625 // shouldn't be found by lookup. 626 AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); 627 applySubprogramAttributesToDefinition(SP, *AbsDef); 628 629 if (!includeMinimalInlineScopes()) 630 addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); 631 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef)) 632 addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); 633 } 634 635 DIE *DwarfCompileUnit::constructImportedEntityDIE( 636 const DIImportedEntity *Module) { 637 DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); 638 insertDIE(Module, IMDie); 639 DIE *EntityDie; 640 auto *Entity = resolve(Module->getEntity()); 641 if (auto *NS = dyn_cast<DINamespace>(Entity)) 642 EntityDie = getOrCreateNameSpace(NS); 643 else if (auto *M = dyn_cast<DIModule>(Entity)) 644 EntityDie = getOrCreateModule(M); 645 else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 646 EntityDie = getOrCreateSubprogramDIE(SP); 647 else if (auto *T = dyn_cast<DIType>(Entity)) 648 EntityDie = getOrCreateTypeDIE(T); 649 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) 650 EntityDie = getOrCreateGlobalVariableDIE(GV, nullptr); 651 else 652 EntityDie = getDIE(Entity); 653 assert(EntityDie); 654 addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(), 655 Module->getScope()->getDirectory()); 656 addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); 657 StringRef Name = Module->getName(); 658 if (!Name.empty()) 659 addString(*IMDie, dwarf::DW_AT_name, Name); 660 661 return IMDie; 662 } 663 664 void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { 665 DIE *D = getDIE(SP); 666 if (DIE *AbsSPDIE = DU->getAbstractSPDies().lookup(SP)) { 667 if (D) 668 // If this subprogram has an abstract definition, reference that 669 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); 670 } else { 671 if (!D && !includeMinimalInlineScopes()) 672 // Lazily construct the subprogram if we didn't see either concrete or 673 // inlined versions during codegen. (except in -gmlt ^ where we want 674 // to omit these entirely) 675 D = getOrCreateSubprogramDIE(SP); 676 if (D) 677 // And attach the attributes 678 applySubprogramAttributesToDefinition(SP, *D); 679 } 680 } 681 682 void DwarfCompileUnit::emitHeader(bool UseOffsets) { 683 // Don't bother labeling the .dwo unit, as its offset isn't used. 684 if (!Skeleton) { 685 LabelBegin = Asm->createTempSymbol("cu_begin"); 686 Asm->OutStreamer->EmitLabel(LabelBegin); 687 } 688 689 DwarfUnit::emitHeader(UseOffsets); 690 } 691 692 /// addGlobalName - Add a new global name to the compile unit. 693 void DwarfCompileUnit::addGlobalName(StringRef Name, DIE &Die, 694 const DIScope *Context) { 695 if (includeMinimalInlineScopes()) 696 return; 697 std::string FullName = getParentContextString(Context) + Name.str(); 698 GlobalNames[FullName] = &Die; 699 } 700 701 /// Add a new global type to the unit. 702 void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, 703 const DIScope *Context) { 704 if (includeMinimalInlineScopes()) 705 return; 706 std::string FullName = getParentContextString(Context) + Ty->getName().str(); 707 GlobalTypes[FullName] = &Die; 708 } 709 710 /// addVariableAddress - Add DW_AT_location attribute for a 711 /// DbgVariable based on provided MachineLocation. 712 void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, 713 MachineLocation Location) { 714 if (DV.hasComplexAddress()) 715 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 716 else if (DV.isBlockByrefVariable()) 717 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 718 else 719 addAddress(Die, dwarf::DW_AT_location, Location); 720 } 721 722 /// Add an address attribute to a die based on the location provided. 723 void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, 724 const MachineLocation &Location) { 725 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 726 727 bool validReg; 728 if (Location.isReg()) 729 validReg = addRegisterOpPiece(*Loc, Location.getReg()); 730 else 731 validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 732 733 if (!validReg) 734 return; 735 736 // Now attach the location information to the DIE. 737 addBlock(Die, Attribute, Loc); 738 } 739 740 /// Start with the address based on the location provided, and generate the 741 /// DWARF information necessary to find the actual variable given the extra 742 /// address information encoded in the DbgVariable, starting from the starting 743 /// location. Add the DWARF information to the die. 744 void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, 745 dwarf::Attribute Attribute, 746 const MachineLocation &Location) { 747 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 748 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 749 const DIExpression *Expr = DV.getSingleExpression(); 750 bool ValidReg; 751 const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); 752 if (Location.getOffset()) { 753 ValidReg = DwarfExpr.AddMachineRegIndirect(TRI, Location.getReg(), 754 Location.getOffset()); 755 if (ValidReg) 756 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end()); 757 } else 758 ValidReg = DwarfExpr.AddMachineRegExpression(TRI, Expr, Location.getReg()); 759 760 // Now attach the location information to the DIE. 761 if (ValidReg) 762 addBlock(Die, Attribute, Loc); 763 } 764 765 /// Add a Dwarf loclistptr attribute data and value. 766 void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, 767 unsigned Index) { 768 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 769 : dwarf::DW_FORM_data4; 770 Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); 771 } 772 773 void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, 774 DIE &VariableDie) { 775 StringRef Name = Var.getName(); 776 if (!Name.empty()) 777 addString(VariableDie, dwarf::DW_AT_name, Name); 778 const auto *DIVar = Var.getVariable(); 779 if (DIVar) 780 if (uint32_t AlignInBytes = DIVar->getAlignInBytes()) 781 addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 782 AlignInBytes); 783 784 addSourceLine(VariableDie, DIVar); 785 addType(VariableDie, Var.getType()); 786 if (Var.isArtificial()) 787 addFlag(VariableDie, dwarf::DW_AT_artificial); 788 } 789 790 /// Add a Dwarf expression attribute data and value. 791 void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, 792 const MCExpr *Expr) { 793 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr)); 794 } 795 796 void DwarfCompileUnit::applySubprogramAttributesToDefinition( 797 const DISubprogram *SP, DIE &SPDie) { 798 auto *SPDecl = SP->getDeclaration(); 799 auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope()); 800 applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); 801 addGlobalName(SP->getName(), SPDie, Context); 802 } 803 804 bool DwarfCompileUnit::isDwoUnit() const { 805 return DD->useSplitDwarf() && Skeleton; 806 } 807 808 bool DwarfCompileUnit::includeMinimalInlineScopes() const { 809 return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly || 810 (DD->useSplitDwarf() && !Skeleton); 811 } 812 } // end llvm namespace 813