1 //===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains support for writing dwarf compile unit. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 16 17 #include "DbgValueHistoryCalculator.h" 18 #include "DwarfDebug.h" 19 #include "DwarfUnit.h" 20 #include "llvm/ADT/ArrayRef.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringMap.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/BinaryFormat/Dwarf.h" 26 #include "llvm/CodeGen/DIE.h" 27 #include "llvm/CodeGen/LexicalScopes.h" 28 #include "llvm/IR/DebugInfoMetadata.h" 29 #include "llvm/Support/Casting.h" 30 #include <algorithm> 31 #include <cassert> 32 #include <cstdint> 33 #include <memory> 34 35 namespace llvm { 36 37 class AsmPrinter; 38 class DwarfFile; 39 class GlobalVariable; 40 class MCExpr; 41 class MCSymbol; 42 class MDNode; 43 44 class DwarfCompileUnit final : public DwarfUnit { 45 /// A numeric ID unique among all CUs in the module 46 unsigned UniqueID; 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 range lists for a given compile unit, separate from the ranges for 73 // the CU itself. 74 SmallVector<RangeSpanList, 1> CURangeLists; 75 76 // List of ranges for a given compile unit. 77 SmallVector<RangeSpan, 2> CURanges; 78 79 // The base address of this unit, if any. Used for relative references in 80 // ranges/locs. 81 const MCSymbol *BaseAddress = nullptr; 82 83 DenseMap<const MDNode *, DIE *> AbstractSPDies; 84 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables; 85 86 /// DWO ID for correlating skeleton and split units. 87 uint64_t DWOId = 0; 88 89 /// Construct a DIE for the given DbgVariable without initializing the 90 /// DbgVariable's DIE reference. 91 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract); 92 93 bool isDwoUnit() const override; 94 getAbstractSPDies()95 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() { 96 if (isDwoUnit() && !DD->shareAcrossDWOCUs()) 97 return AbstractSPDies; 98 return DU->getAbstractSPDies(); 99 } 100 getAbstractVariables()101 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() { 102 if (isDwoUnit() && !DD->shareAcrossDWOCUs()) 103 return AbstractVariables; 104 return DU->getAbstractVariables(); 105 } 106 107 public: 108 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A, 109 DwarfDebug *DW, DwarfFile *DWU); 110 getUniqueID()111 unsigned getUniqueID() const { return UniqueID; } 112 getSkeleton()113 DwarfCompileUnit *getSkeleton() const { 114 return Skeleton; 115 } 116 117 bool includeMinimalInlineScopes() const; 118 119 void initStmtList(); 120 121 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE. 122 void applyStmtList(DIE &D); 123 124 /// A pair of GlobalVariable and DIExpression. 125 struct GlobalExpr { 126 const GlobalVariable *Var; 127 const DIExpression *Expr; 128 }; 129 130 /// Get or create global variable DIE. 131 DIE * 132 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, 133 ArrayRef<GlobalExpr> GlobalExprs); 134 135 /// addLabelAddress - Add a dwarf label attribute data and value using 136 /// either DW_FORM_addr or DW_FORM_GNU_addr_index. 137 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 138 const MCSymbol *Label); 139 140 /// addLocalLabelAddress - Add a dwarf label attribute data and value using 141 /// DW_FORM_addr only. 142 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, 143 const MCSymbol *Label); 144 getCU()145 DwarfCompileUnit &getCU() override { return *this; } 146 147 unsigned getOrCreateSourceID(const DIFile *File) override; 148 addImportedEntity(const DIImportedEntity * IE)149 void addImportedEntity(const DIImportedEntity* IE) { 150 DIScope *Scope = IE->getScope(); 151 assert(Scope && "Invalid Scope encoding!"); 152 if (!isa<DILocalScope>(Scope)) 153 // No need to add imported enities that are not local declaration. 154 return; 155 156 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope(); 157 ImportedEntities[LocalScope].push_back(IE); 158 } 159 160 /// addRange - Add an address range to the list of ranges for this unit. 161 void addRange(RangeSpan Range); 162 163 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End); 164 165 /// Find DIE for the given subprogram and attach appropriate 166 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global 167 /// variables in this scope then create and insert DIEs for these 168 /// variables. 169 DIE &updateSubprogramScopeDIE(const DISubprogram *SP); 170 171 void constructScopeDIE(LexicalScope *Scope, 172 SmallVectorImpl<DIE *> &FinalChildren); 173 174 /// A helper function to construct a RangeSpanList for a given 175 /// lexical scope. 176 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range); 177 178 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges); 179 180 void attachRangesOrLowHighPC(DIE &D, 181 const SmallVectorImpl<InsnRange> &Ranges); 182 183 /// This scope represents inlined body of a function. Construct 184 /// DIE to represent this concrete inlined copy of the function. 185 DIE *constructInlinedScopeDIE(LexicalScope *Scope); 186 187 /// Construct new DW_TAG_lexical_block for this scope and 188 /// attach DW_AT_low_pc/DW_AT_high_pc labels. 189 DIE *constructLexicalScopeDIE(LexicalScope *Scope); 190 191 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 192 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false); 193 194 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope, 195 DIE *&ObjectPointer); 196 197 /// A helper function to create children of a Scope DIE. 198 DIE *createScopeChildrenDIE(LexicalScope *Scope, 199 SmallVectorImpl<DIE *> &Children, 200 bool *HasNonScopeChildren = nullptr); 201 202 /// Construct a DIE for this subprogram scope. 203 void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope); 204 205 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE); 206 207 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope); 208 209 /// Construct import_module DIE. 210 DIE *constructImportedEntityDIE(const DIImportedEntity *Module); 211 212 void finishSubprogramDefinition(const DISubprogram *SP); 213 void finishVariableDefinition(const DbgVariable &Var); 214 215 /// Find abstract variable associated with Var. 216 using InlinedVariable = DbgValueHistoryMap::InlinedVariable; 217 DbgVariable *getExistingAbstractVariable(InlinedVariable IV, 218 const DILocalVariable *&Cleansed); 219 DbgVariable *getExistingAbstractVariable(InlinedVariable IV); 220 void createAbstractVariable(const DILocalVariable *Var, LexicalScope *Scope); 221 222 /// Set the skeleton unit associated with this unit. setSkeleton(DwarfCompileUnit & Skel)223 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; } 224 getHeaderSize()225 unsigned getHeaderSize() const override { 226 // DWARF v5 added the DWO ID to the header for split/skeleton units. 227 unsigned DWOIdSize = 228 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t) 229 : 0; 230 return DwarfUnit::getHeaderSize() + DWOIdSize; 231 } getLength()232 unsigned getLength() { 233 return sizeof(uint32_t) + // Length field 234 getHeaderSize() + getUnitDie().getSize(); 235 } 236 237 void emitHeader(bool UseOffsets) override; 238 getLabelBegin()239 MCSymbol *getLabelBegin() const { 240 assert(getSection()); 241 return LabelBegin; 242 } 243 getMacroLabelBegin()244 MCSymbol *getMacroLabelBegin() const { 245 return MacroLabelBegin; 246 } 247 248 /// Add a new global name to the compile unit. 249 void addGlobalName(StringRef Name, const DIE &Die, 250 const DIScope *Context) override; 251 252 /// Add a new global name present in a type unit to this compile unit. 253 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context); 254 255 /// Add a new global type to the compile unit. 256 void addGlobalType(const DIType *Ty, const DIE &Die, 257 const DIScope *Context) override; 258 259 /// Add a new global type present in a type unit to this compile unit. 260 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context); 261 getGlobalNames()262 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; } getGlobalTypes()263 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; } 264 265 /// Add DW_AT_location attribute for a DbgVariable based on provided 266 /// MachineLocation. 267 void addVariableAddress(const DbgVariable &DV, DIE &Die, 268 MachineLocation Location); 269 /// Add an address attribute to a die based on the location provided. 270 void addAddress(DIE &Die, dwarf::Attribute Attribute, 271 const MachineLocation &Location); 272 273 /// Start with the address based on the location provided, and generate the 274 /// DWARF information necessary to find the actual variable (navigating the 275 /// extra location information encoded in the type) based on the starting 276 /// location. Add the DWARF information to the die. 277 void addComplexAddress(const DbgVariable &DV, DIE &Die, 278 dwarf::Attribute Attribute, 279 const MachineLocation &Location); 280 281 /// Add a Dwarf loclistptr attribute data and value. 282 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index); 283 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie); 284 285 /// Add a Dwarf expression attribute data and value. 286 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr); 287 288 void applySubprogramAttributesToDefinition(const DISubprogram *SP, 289 DIE &SPDie); 290 291 /// getRangeLists - Get the vector of range lists. getRangeLists()292 const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 293 return (Skeleton ? Skeleton : this)->CURangeLists; 294 } 295 296 /// getRanges - Get the list of ranges for this unit. getRanges()297 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; } takeRanges()298 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); } 299 setBaseAddress(const MCSymbol * Base)300 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; } getBaseAddress()301 const MCSymbol *getBaseAddress() const { return BaseAddress; } 302 getDWOId()303 uint64_t getDWOId() const { return DWOId; } setDWOId(uint64_t DwoId)304 void setDWOId(uint64_t DwoId) { DWOId = DwoId; } 305 306 bool hasDwarfPubSections() const; 307 }; 308 309 } // end namespace llvm 310 311 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 312