DwarfCompileUnit.h 14 KB

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