1 //===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===// 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 compile unit. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 15 16 #include "DwarfDebug.h" 17 #include "DwarfUnit.h" 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/BinaryFormat/Dwarf.h" 24 #include "llvm/CodeGen/DbgEntityHistoryCalculator.h" 25 #include "llvm/CodeGen/DIE.h" 26 #include "llvm/CodeGen/LexicalScopes.h" 27 #include "llvm/IR/DebugInfoMetadata.h" 28 #include "llvm/Support/Casting.h" 29 #include <algorithm> 30 #include <cassert> 31 #include <cstdint> 32 #include <memory> 33 34 namespace llvm { 35 36 class AsmPrinter; 37 class DwarfFile; 38 class GlobalVariable; 39 class MCExpr; 40 class MCSymbol; 41 class MDNode; 42 43 enum class UnitKind { Skeleton, Full }; 44 45 class DwarfCompileUnit final : public DwarfUnit { 46 /// A numeric ID unique among all CUs in the module 47 unsigned UniqueID; 48 bool HasRangeLists = false; 49 50 /// The start of the unit line section, this is also 51 /// reused in appyStmtList. 52 MCSymbol *LineTableStartSym; 53 54 /// Skeleton unit associated with this unit. 55 DwarfCompileUnit *Skeleton = nullptr; 56 57 /// The start of the unit within its section. 58 MCSymbol *LabelBegin; 59 60 /// The start of the unit macro info within macro section. 61 MCSymbol *MacroLabelBegin; 62 63 using ImportedEntityList = SmallVector<const MDNode *, 8>; 64 using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>; 65 66 ImportedEntityMap ImportedEntities; 67 68 /// GlobalNames - A map of globally visible named entities for this unit. 69 StringMap<const DIE *> GlobalNames; 70 71 /// GlobalTypes - A map of globally visible types for this unit. 72 StringMap<const DIE *> GlobalTypes; 73 74 // List of ranges for a given compile unit. 75 SmallVector<RangeSpan, 2> CURanges; 76 77 // The base address of this unit, if any. Used for relative references in 78 // ranges/locs. 79 const MCSymbol *BaseAddress = nullptr; 80 81 DenseMap<const MDNode *, DIE *> AbstractSPDies; 82 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities; 83 84 /// DWO ID for correlating skeleton and split units. 85 uint64_t DWOId = 0; 86 87 /// Construct a DIE for the given DbgVariable without initializing the 88 /// DbgVariable's DIE reference. 89 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract); 90 91 bool isDwoUnit() const override; 92 93 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() { 94 if (isDwoUnit() && !DD->shareAcrossDWOCUs()) 95 return AbstractSPDies; 96 return DU->getAbstractSPDies(); 97 } 98 99 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() { 100 if (isDwoUnit() && !DD->shareAcrossDWOCUs()) 101 return AbstractEntities; 102 return DU->getAbstractEntities(); 103 } 104 105 void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override; 106 107 public: 108 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A, 109 DwarfDebug *DW, DwarfFile *DWU, 110 UnitKind Kind = UnitKind::Full); 111 112 bool hasRangeLists() const { return HasRangeLists; } 113 unsigned getUniqueID() const { return UniqueID; } 114 115 DwarfCompileUnit *getSkeleton() const { 116 return Skeleton; 117 } 118 119 bool includeMinimalInlineScopes() const; 120 121 void initStmtList(); 122 123 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE. 124 void applyStmtList(DIE &D); 125 126 /// Get line table start symbol for this unit. 127 MCSymbol *getLineTableStartSym() const { return LineTableStartSym; } 128 129 /// A pair of GlobalVariable and DIExpression. 130 struct GlobalExpr { 131 const GlobalVariable *Var; 132 const DIExpression *Expr; 133 }; 134 135 struct BaseTypeRef { 136 BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding) : 137 BitSize(BitSize), Encoding(Encoding) {} 138 unsigned BitSize; 139 dwarf::TypeKind Encoding; 140 DIE *Die = nullptr; 141 }; 142 143 std::vector<BaseTypeRef> ExprRefedBaseTypes; 144 145 /// Get or create global variable DIE. 146 DIE * 147 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, 148 ArrayRef<GlobalExpr> GlobalExprs); 149 150 DIE *getOrCreateCommonBlock(const DICommonBlock *CB, 151 ArrayRef<GlobalExpr> GlobalExprs); 152 153 void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV, 154 ArrayRef<GlobalExpr> GlobalExprs); 155 156 /// addLabelAddress - Add a dwarf label attribute data and value using 157 /// either DW_FORM_addr or DW_FORM_GNU_addr_index. 158 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 159 const MCSymbol *Label); 160 161 /// addLocalLabelAddress - Add a dwarf label attribute data and value using 162 /// DW_FORM_addr only. 163 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, 164 const MCSymbol *Label); 165 166 DwarfCompileUnit &getCU() override { return *this; } 167 168 unsigned getOrCreateSourceID(const DIFile *File) override; 169 170 void addImportedEntity(const DIImportedEntity* IE) { 171 DIScope *Scope = IE->getScope(); 172 assert(Scope && "Invalid Scope encoding!"); 173 if (!isa<DILocalScope>(Scope)) 174 // No need to add imported enities that are not local declaration. 175 return; 176 177 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope(); 178 ImportedEntities[LocalScope].push_back(IE); 179 } 180 181 /// addRange - Add an address range to the list of ranges for this unit. 182 void addRange(RangeSpan Range); 183 184 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End); 185 186 /// Find DIE for the given subprogram and attach appropriate 187 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global 188 /// variables in this scope then create and insert DIEs for these 189 /// variables. 190 DIE &updateSubprogramScopeDIE(const DISubprogram *SP); 191 192 void constructScopeDIE(LexicalScope *Scope, 193 SmallVectorImpl<DIE *> &FinalChildren); 194 195 /// A helper function to construct a RangeSpanList for a given 196 /// lexical scope. 197 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range); 198 199 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges); 200 201 void attachRangesOrLowHighPC(DIE &D, 202 const SmallVectorImpl<InsnRange> &Ranges); 203 204 /// This scope represents inlined body of a function. Construct 205 /// DIE to represent this concrete inlined copy of the function. 206 DIE *constructInlinedScopeDIE(LexicalScope *Scope); 207 208 /// Construct new DW_TAG_lexical_block for this scope and 209 /// attach DW_AT_low_pc/DW_AT_high_pc labels. 210 DIE *constructLexicalScopeDIE(LexicalScope *Scope); 211 212 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 213 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false); 214 215 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope, 216 DIE *&ObjectPointer); 217 218 /// Construct a DIE for the given DbgLabel. 219 DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope); 220 221 /// A helper function to create children of a Scope DIE. 222 DIE *createScopeChildrenDIE(LexicalScope *Scope, 223 SmallVectorImpl<DIE *> &Children, 224 bool *HasNonScopeChildren = nullptr); 225 226 void createBaseTypeDIEs(); 227 228 /// Construct a DIE for this subprogram scope. 229 DIE &constructSubprogramScopeDIE(const DISubprogram *Sub, 230 LexicalScope *Scope); 231 232 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE); 233 234 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope); 235 236 /// Whether to use the GNU analog for a DWARF5 tag, attribute, or location 237 /// atom. Only applicable when emitting otherwise DWARF4-compliant debug info. 238 bool useGNUAnalogForDwarf5Feature() const; 239 240 /// This takes a DWARF 5 tag and returns it or a GNU analog. 241 dwarf::Tag getDwarf5OrGNUTag(dwarf::Tag Tag) const; 242 243 /// This takes a DWARF 5 attribute and returns it or a GNU analog. 244 dwarf::Attribute getDwarf5OrGNUAttr(dwarf::Attribute Attr) const; 245 246 /// This takes a DWARF 5 location atom and either returns it or a GNU analog. 247 dwarf::LocationAtom getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const; 248 249 /// Construct a call site entry DIE describing a call within \p Scope to a 250 /// callee described by \p CalleeDIE. 251 /// \p CalleeDIE is a declaration or definition subprogram DIE for the callee. 252 /// For indirect calls \p CalleeDIE is set to nullptr. 253 /// \p IsTail specifies whether the call is a tail call. 254 /// \p PCAddr points to the PC value after the call instruction. 255 /// \p CallAddr points to the PC value at the call instruction (or is null). 256 /// \p CallReg is a register location for an indirect call. For direct calls 257 /// the \p CallReg is set to 0. 258 DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, DIE *CalleeDIE, bool IsTail, 259 const MCSymbol *PCAddr, 260 const MCSymbol *CallAddr, unsigned CallReg); 261 /// Construct call site parameter DIEs for the \p CallSiteDIE. The \p Params 262 /// were collected by the \ref collectCallSiteParameters. 263 /// Note: The order of parameters does not matter, since debuggers recognize 264 /// call site parameters by the DW_AT_location attribute. 265 void constructCallSiteParmEntryDIEs(DIE &CallSiteDIE, 266 SmallVector<DbgCallSiteParam, 4> &Params); 267 268 /// Construct import_module DIE. 269 DIE *constructImportedEntityDIE(const DIImportedEntity *Module); 270 271 void finishSubprogramDefinition(const DISubprogram *SP); 272 void finishEntityDefinition(const DbgEntity *Entity); 273 274 /// Find abstract variable associated with Var. 275 using InlinedEntity = DbgValueHistoryMap::InlinedEntity; 276 DbgEntity *getExistingAbstractEntity(const DINode *Node); 277 void createAbstractEntity(const DINode *Node, LexicalScope *Scope); 278 279 /// Set the skeleton unit associated with this unit. 280 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; } 281 282 unsigned getHeaderSize() const override { 283 // DWARF v5 added the DWO ID to the header for split/skeleton units. 284 unsigned DWOIdSize = 285 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t) 286 : 0; 287 return DwarfUnit::getHeaderSize() + DWOIdSize; 288 } 289 unsigned getLength() { 290 return sizeof(uint32_t) + // Length field 291 getHeaderSize() + getUnitDie().getSize(); 292 } 293 294 void emitHeader(bool UseOffsets) override; 295 296 /// Add the DW_AT_addr_base attribute to the unit DIE. 297 void addAddrTableBase(); 298 299 MCSymbol *getLabelBegin() const { 300 assert(getSection()); 301 return LabelBegin; 302 } 303 304 MCSymbol *getMacroLabelBegin() const { 305 return MacroLabelBegin; 306 } 307 308 /// Add a new global name to the compile unit. 309 void addGlobalName(StringRef Name, const DIE &Die, 310 const DIScope *Context) override; 311 312 /// Add a new global name present in a type unit to this compile unit. 313 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context); 314 315 /// Add a new global type to the compile unit. 316 void addGlobalType(const DIType *Ty, const DIE &Die, 317 const DIScope *Context) override; 318 319 /// Add a new global type present in a type unit to this compile unit. 320 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context); 321 322 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; } 323 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; } 324 325 /// Add DW_AT_location attribute for a DbgVariable based on provided 326 /// MachineLocation. 327 void addVariableAddress(const DbgVariable &DV, DIE &Die, 328 MachineLocation Location); 329 /// Add an address attribute to a die based on the location provided. 330 void addAddress(DIE &Die, dwarf::Attribute Attribute, 331 const MachineLocation &Location); 332 333 /// Start with the address based on the location provided, and generate the 334 /// DWARF information necessary to find the actual variable (navigating the 335 /// extra location information encoded in the type) based on the starting 336 /// location. Add the DWARF information to the die. 337 void addComplexAddress(const DbgVariable &DV, DIE &Die, 338 dwarf::Attribute Attribute, 339 const MachineLocation &Location); 340 341 /// Add a Dwarf loclistptr attribute data and value. 342 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index); 343 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie); 344 345 /// Add a Dwarf expression attribute data and value. 346 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr); 347 348 void applySubprogramAttributesToDefinition(const DISubprogram *SP, 349 DIE &SPDie); 350 351 void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie); 352 353 /// getRanges - Get the list of ranges for this unit. 354 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; } 355 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); } 356 357 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; } 358 const MCSymbol *getBaseAddress() const { return BaseAddress; } 359 360 uint64_t getDWOId() const { return DWOId; } 361 void setDWOId(uint64_t DwoId) { DWOId = DwoId; } 362 363 bool hasDwarfPubSections() const; 364 365 void addBaseTypeRef(DIEValueList &Die, int64_t Idx); 366 }; 367 368 } // end namespace llvm 369 370 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 371