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