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