DwarfCompileUnit.h 13 KB

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