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