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 <algorithm>
  27. #include <cassert>
  28. #include <cstdint>
  29. #include <memory>
  30. namespace llvm {
  31. class AsmPrinter;
  32. class DIE;
  33. class DIELoc;
  34. class DIEValueList;
  35. class DwarfFile;
  36. class GlobalVariable;
  37. class MCExpr;
  38. class MCSymbol;
  39. class MDNode;
  40. enum class UnitKind { Skeleton, Full };
  41. class DwarfCompileUnit final : public DwarfUnit {
  42. /// A numeric ID unique among all CUs in the module
  43. unsigned UniqueID;
  44. bool HasRangeLists = false;
  45. /// The start of the unit line section, this is also
  46. /// reused in appyStmtList.
  47. MCSymbol *LineTableStartSym;
  48. /// Skeleton unit associated with this unit.
  49. DwarfCompileUnit *Skeleton = nullptr;
  50. /// The start of the unit within its section.
  51. MCSymbol *LabelBegin = nullptr;
  52. /// The start of the unit macro info within macro section.
  53. MCSymbol *MacroLabelBegin;
  54. using ImportedEntityList = SmallVector<const MDNode *, 8>;
  55. using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
  56. ImportedEntityMap ImportedEntities;
  57. /// GlobalNames - A map of globally visible named entities for this unit.
  58. StringMap<const DIE *> GlobalNames;
  59. /// GlobalTypes - A map of globally visible types for this unit.
  60. StringMap<const DIE *> GlobalTypes;
  61. // List of ranges for a given compile unit.
  62. SmallVector<RangeSpan, 2> CURanges;
  63. // The base address of this unit, if any. Used for relative references in
  64. // ranges/locs.
  65. const MCSymbol *BaseAddress = nullptr;
  66. DenseMap<const MDNode *, DIE *> AbstractSPDies;
  67. DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
  68. /// DWO ID for correlating skeleton and split units.
  69. uint64_t DWOId = 0;
  70. const DIFile *LastFile = nullptr;
  71. unsigned LastFileID;
  72. /// Construct a DIE for the given DbgVariable without initializing the
  73. /// DbgVariable's DIE reference.
  74. DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
  75. bool isDwoUnit() const override;
  76. DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
  77. if (isDwoUnit() && !DD->shareAcrossDWOCUs())
  78. return AbstractSPDies;
  79. return DU->getAbstractSPDies();
  80. }
  81. DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
  82. if (isDwoUnit() && !DD->shareAcrossDWOCUs())
  83. return AbstractEntities;
  84. return DU->getAbstractEntities();
  85. }
  86. void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
  87. public:
  88. DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
  89. DwarfDebug *DW, DwarfFile *DWU,
  90. UnitKind Kind = UnitKind::Full);
  91. bool hasRangeLists() const { return HasRangeLists; }
  92. unsigned getUniqueID() const { return UniqueID; }
  93. DwarfCompileUnit *getSkeleton() const {
  94. return Skeleton;
  95. }
  96. bool includeMinimalInlineScopes() const;
  97. void initStmtList();
  98. /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
  99. void applyStmtList(DIE &D);
  100. /// Get line table start symbol for this unit.
  101. MCSymbol *getLineTableStartSym() const { return LineTableStartSym; }
  102. /// A pair of GlobalVariable and DIExpression.
  103. struct GlobalExpr {
  104. const GlobalVariable *Var;
  105. const DIExpression *Expr;
  106. };
  107. struct BaseTypeRef {
  108. BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding) :
  109. BitSize(BitSize), Encoding(Encoding) {}
  110. unsigned BitSize;
  111. dwarf::TypeKind Encoding;
  112. DIE *Die = nullptr;
  113. };
  114. std::vector<BaseTypeRef> ExprRefedBaseTypes;
  115. /// Get or create global variable DIE.
  116. DIE *
  117. getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
  118. ArrayRef<GlobalExpr> GlobalExprs);
  119. DIE *getOrCreateCommonBlock(const DICommonBlock *CB,
  120. ArrayRef<GlobalExpr> GlobalExprs);
  121. void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV,
  122. ArrayRef<GlobalExpr> GlobalExprs);
  123. /// addLabelAddress - Add a dwarf label attribute data and value using
  124. /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
  125. void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
  126. const MCSymbol *Label);
  127. /// addLocalLabelAddress - Add a dwarf label attribute data and value using
  128. /// DW_FORM_addr only.
  129. void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
  130. const MCSymbol *Label);
  131. DwarfCompileUnit &getCU() override { return *this; }
  132. unsigned getOrCreateSourceID(const DIFile *File) override;
  133. void addImportedEntity(const DIImportedEntity* IE) {
  134. DIScope *Scope = IE->getScope();
  135. assert(Scope && "Invalid Scope encoding!");
  136. if (!isa<DILocalScope>(Scope))
  137. // No need to add imported enities that are not local declaration.
  138. return;
  139. auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
  140. ImportedEntities[LocalScope].push_back(IE);
  141. }
  142. /// addRange - Add an address range to the list of ranges for this unit.
  143. void addRange(RangeSpan Range);
  144. void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
  145. /// Find DIE for the given subprogram and attach appropriate
  146. /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
  147. /// variables in this scope then create and insert DIEs for these
  148. /// variables.
  149. DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
  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 inlined body of a function. Construct
  158. /// DIE to represent this concrete inlined copy of the function.
  159. DIE *constructInlinedScopeDIE(LexicalScope *Scope);
  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