DwarfUnit.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //===-- llvm/CodeGen/DwarfUnit.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_DWARFUNIT_H
  13. #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
  14. #include "DwarfDebug.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/Optional.h"
  17. #include "llvm/CodeGen/AsmPrinter.h"
  18. #include "llvm/CodeGen/DIE.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include <string>
  21. namespace llvm {
  22. class ConstantFP;
  23. class ConstantInt;
  24. class DwarfCompileUnit;
  25. class MCDwarfDwoLineTable;
  26. class MCSymbol;
  27. //===----------------------------------------------------------------------===//
  28. /// This dwarf writer support class manages information associated with a
  29. /// source file.
  30. class DwarfUnit : public DIEUnit {
  31. protected:
  32. /// MDNode for the compile unit.
  33. const DICompileUnit *CUNode;
  34. // All DIEValues are allocated through this allocator.
  35. BumpPtrAllocator DIEValueAllocator;
  36. /// Target of Dwarf emission.
  37. AsmPrinter *Asm;
  38. /// Emitted at the end of the CU and used to compute the CU Length field.
  39. MCSymbol *EndLabel = nullptr;
  40. // Holders for some common dwarf information.
  41. DwarfDebug *DD;
  42. DwarfFile *DU;
  43. /// An anonymous type for index type. Owned by DIEUnit.
  44. DIE *IndexTyDie = nullptr;
  45. /// Tracks the mapping of unit level debug information variables to debug
  46. /// information entries.
  47. DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
  48. /// A list of all the DIEBlocks in use.
  49. std::vector<DIEBlock *> DIEBlocks;
  50. /// A list of all the DIELocs in use.
  51. std::vector<DIELoc *> DIELocs;
  52. /// This map is used to keep track of subprogram DIEs that need
  53. /// DW_AT_containing_type attribute. This attribute points to a DIE that
  54. /// corresponds to the MDNode mapped with the subprogram DIE.
  55. DenseMap<DIE *, const DINode *> ContainingTypeMap;
  56. DwarfUnit(dwarf::Tag, const DICompileUnit *Node, AsmPrinter *A, DwarfDebug *DW,
  57. DwarfFile *DWU);
  58. bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie, bool Minimal);
  59. bool isShareableAcrossCUs(const DINode *D) const;
  60. template <typename T>
  61. void addAttribute(DIEValueList &Die, dwarf::Attribute Attribute,
  62. dwarf::Form Form, T &&Value) {
  63. // For strict DWARF mode, only generate attributes available to current
  64. // DWARF version.
  65. // Attribute 0 is used when emitting form-encoded values in blocks, which
  66. // don't have attributes (only forms) so we cannot detect their DWARF
  67. // version compatibility here and assume they are compatible.
  68. if (Attribute != 0 && Asm->TM.Options.DebugStrictDwarf &&
  69. DD->getDwarfVersion() < dwarf::AttributeVersion(Attribute))
  70. return;
  71. Die.addValue(DIEValueAllocator,
  72. DIEValue(Attribute, Form, std::forward<T>(Value)));
  73. }
  74. public:
  75. // Accessors.
  76. AsmPrinter* getAsmPrinter() const { return Asm; }
  77. MCSymbol *getEndLabel() const { return EndLabel; }
  78. uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
  79. const DICompileUnit *getCUNode() const { return CUNode; }
  80. DwarfDebug &getDwarfDebug() const { return *DD; }
  81. /// Return true if this compile unit has something to write out.
  82. bool hasContent() const { return getUnitDie().hasChildren(); }
  83. /// Get string containing language specific context for a global name.
  84. ///
  85. /// Walks the metadata parent chain in a language specific manner (using the
  86. /// compile unit language) and returns it as a string. This is done at the
  87. /// metadata level because DIEs may not currently have been added to the
  88. /// parent context and walking the DIEs looking for names is more expensive
  89. /// than walking the metadata.
  90. std::string getParentContextString(const DIScope *Context) const;
  91. /// Add a new global name to the compile unit.
  92. virtual void addGlobalName(StringRef Name, const DIE &Die,
  93. const DIScope *Context) = 0;
  94. /// Add a new global type to the compile unit.
  95. virtual void addGlobalType(const DIType *Ty, const DIE &Die,
  96. const DIScope *Context) = 0;
  97. /// Returns the DIE map slot for the specified debug variable.
  98. ///
  99. /// We delegate the request to DwarfDebug when the MDNode can be part of the
  100. /// type system, since DIEs for the type system can be shared across CUs and
  101. /// the mappings are kept in DwarfDebug.
  102. DIE *getDIE(const DINode *D) const;
  103. /// Returns a fresh newly allocated DIELoc.
  104. DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; }
  105. /// Insert DIE into the map.
  106. ///
  107. /// We delegate the request to DwarfDebug when the MDNode can be part of the
  108. /// type system, since DIEs for the type system can be shared across CUs and
  109. /// the mappings are kept in DwarfDebug.
  110. void insertDIE(const DINode *Desc, DIE *D);
  111. void insertDIE(DIE *D);
  112. /// Add a flag that is true to the DIE.
  113. void addFlag(DIE &Die, dwarf::Attribute Attribute);
  114. /// Add an unsigned integer attribute data and value.
  115. void addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
  116. Optional<dwarf::Form> Form, uint64_t Integer);
  117. void addUInt(DIEValueList &Block, dwarf::Form Form, uint64_t Integer);
  118. /// Add an signed integer attribute data and value.
  119. void addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
  120. Optional<dwarf::Form> Form, int64_t Integer);
  121. void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
  122. /// Add a string attribute data and value.
  123. ///
  124. /// We always emit a reference to the string pool instead of immediate
  125. /// strings so that DIEs have more predictable sizes. In the case of split
  126. /// dwarf we emit an index into another table which gets us the static offset
  127. /// into the string table.
  128. void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
  129. /// Add a Dwarf label attribute data and value.
  130. void addLabel(DIEValueList &Die, dwarf::Attribute Attribute, dwarf::Form Form,
  131. const MCSymbol *Label);
  132. void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
  133. /// Add an offset into a section attribute data and value.
  134. void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
  135. /// Add a dwarf op address data and value using the form given and an
  136. /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
  137. void addOpAddress(DIELoc &Die, const MCSymbol *Sym);
  138. void addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label);
  139. /// Add a label delta attribute data and value.
  140. void addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute,
  141. const MCSymbol *Hi, const MCSymbol *Lo);
  142. /// Add a DIE attribute data and value.
  143. void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
  144. /// Add a DIE attribute data and value.
  145. void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry);
  146. /// Add a type's DW_AT_signature and set the declaration flag.
  147. void addDIETypeSignature(DIE &Die, uint64_t Signature);
  148. /// Add block data.
  149. void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc);
  150. /// Add block data.
  151. void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
  152. void addBlock(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
  153. DIEBlock *Block);
  154. /// Add location information to specified debug information entry.
  155. void addSourceLine(DIE &Die, unsigned Line, const DIFile *File);
  156. void addSourceLine(DIE &Die, const DILocalVariable *V);
  157. void addSourceLine(DIE &Die, const DIGlobalVariable *G);
  158. void addSourceLine(DIE &Die, const DISubprogram *SP);
  159. void addSourceLine(DIE &Die, const DILabel *L);
  160. void addSourceLine(DIE &Die, const DIType *Ty);
  161. void addSourceLine(DIE &Die, const DIObjCProperty *Ty);
  162. /// Add constant value entry in variable DIE.
  163. void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty);
  164. void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty);
  165. void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
  166. void addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty);
  167. void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
  168. /// Add constant value entry in variable DIE.
  169. void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
  170. /// Add a linkage name, if it isn't empty.
  171. void addLinkageName(DIE &Die, StringRef LinkageName);
  172. /// Add template parameters in buffer.
  173. void addTemplateParams(DIE &Buffer, DINodeArray TParams);
  174. /// Add thrown types.
  175. void addThrownTypes(DIE &Die, DINodeArray ThrownTypes);
  176. /// Add the accessibility attribute.
  177. void addAccess(DIE &Die, DINode::DIFlags Flags);
  178. /// Add a new type attribute to the specified entity.
  179. ///
  180. /// This takes and attribute parameter because DW_AT_friend attributes are
  181. /// also type references.
  182. void addType(DIE &Entity, const DIType *Ty,
  183. dwarf::Attribute Attribute = dwarf::DW_AT_type);
  184. DIE *getOrCreateNameSpace(const DINamespace *NS);
  185. DIE *getOrCreateModule(const DIModule *M);
  186. DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false);
  187. void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
  188. bool SkipSPAttributes = false);
  189. /// Creates type DIE with specific context.
  190. DIE *createTypeDIE(const DIScope *Context, DIE &ContextDIE, const DIType *Ty);
  191. /// Find existing DIE or create new DIE for the given type.
  192. DIE *getOrCreateTypeDIE(const MDNode *TyNode);
  193. /// Get context owner's DIE.
  194. DIE *getOrCreateContextDIE(const DIScope *Context);
  195. /// Construct DIEs for types that contain vtables.
  196. void constructContainingTypeDIEs();
  197. /// Construct function argument DIEs.
  198. void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
  199. /// Create a DIE with the given Tag, add the DIE to its parent, and
  200. /// call insertDIE if MD is not null.
  201. DIE &createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N = nullptr);
  202. bool useSegmentedStringOffsetsTable() const {
  203. return DD->useSegmentedStringOffsetsTable();
  204. }
  205. /// Compute the size of a header for this unit, not including the initial
  206. /// length field.
  207. virtual unsigned getHeaderSize() const {
  208. return sizeof(int16_t) + // DWARF version number
  209. Asm->getDwarfOffsetByteSize() + // Offset Into Abbrev. Section
  210. sizeof(int8_t) + // Pointer Size (in bytes)
  211. (DD->getDwarfVersion() >= 5 ? sizeof(int8_t)
  212. : 0); // DWARF v5 unit type
  213. }
  214. /// Emit the header for this unit, not including the initial length field.
  215. virtual void emitHeader(bool UseOffsets) = 0;
  216. /// Add the DW_AT_str_offsets_base attribute to the unit DIE.
  217. void addStringOffsetsStart();
  218. /// Add the DW_AT_rnglists_base attribute to the unit DIE.
  219. void addRnglistsBase();
  220. virtual DwarfCompileUnit &getCU() = 0;
  221. void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);
  222. /// addSectionDelta - Add a label delta attribute data and value.
  223. void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
  224. const MCSymbol *Lo);
  225. /// Add a Dwarf section label attribute data and value.
  226. void addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
  227. const MCSymbol *Label, const MCSymbol *Sec);
  228. /// Add DW_TAG_LLVM_annotation.
  229. void addAnnotation(DIE &Buffer, DINodeArray Annotations);
  230. /// Get context owner's DIE.
  231. DIE *createTypeDIE(const DICompositeType *Ty);
  232. protected:
  233. ~DwarfUnit();
  234. /// Create new static data member DIE.
  235. DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT);
  236. /// Look up the source ID for the given file. If none currently exists,
  237. /// create a new ID and insert it in the line table.
  238. virtual unsigned getOrCreateSourceID(const DIFile *File) = 0;
  239. /// Emit the common part of the header for this unit.
  240. void emitCommonHeader(bool UseOffsets, dwarf::UnitType UT);
  241. private:
  242. void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy);
  243. void constructTypeDIE(DIE &Buffer, const DIStringType *BTy);
  244. void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy);
  245. void constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy);
  246. void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy);
  247. void constructGenericSubrangeDIE(DIE &Buffer, const DIGenericSubrange *SR,
  248. DIE *IndexTy);
  249. void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy);
  250. void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy);
  251. DIE &constructMemberDIE(DIE &Buffer, const DIDerivedType *DT);
  252. void constructTemplateTypeParameterDIE(DIE &Buffer,
  253. const DITemplateTypeParameter *TP);
  254. void constructTemplateValueParameterDIE(DIE &Buffer,
  255. const DITemplateValueParameter *TVP);
  256. /// Return the default lower bound for an array.
  257. ///
  258. /// If the DWARF version doesn't handle the language, return -1.
  259. int64_t getDefaultLowerBound() const;
  260. /// Get an anonymous type for index type.
  261. DIE *getIndexTyDie();
  262. /// Set D as anonymous type for index which can be reused later.
  263. void setIndexTyDie(DIE *D) { IndexTyDie = D; }
  264. virtual void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) = 0;
  265. /// If this is a named finished type then include it in the list of types for
  266. /// the accelerator tables.
  267. void updateAcceleratorTables(const DIScope *Context, const DIType *Ty,
  268. const DIE &TyDIE);
  269. virtual bool isDwoUnit() const = 0;
  270. const MCSymbol *getCrossSectionRelativeBaseAddress() const override;
  271. };
  272. class DwarfTypeUnit final : public DwarfUnit {
  273. uint64_t TypeSignature;
  274. const DIE *Ty;
  275. DwarfCompileUnit &CU;
  276. MCDwarfDwoLineTable *SplitLineTable;
  277. bool UsedLineTable = false;
  278. unsigned getOrCreateSourceID(const DIFile *File) override;
  279. void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
  280. bool isDwoUnit() const override;
  281. public:
  282. DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW,
  283. DwarfFile *DWU, MCDwarfDwoLineTable *SplitLineTable = nullptr);
  284. void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
  285. void setType(const DIE *Ty) { this->Ty = Ty; }
  286. /// Emit the header for this unit, not including the initial length field.
  287. void emitHeader(bool UseOffsets) override;
  288. unsigned getHeaderSize() const override {
  289. return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
  290. Asm->getDwarfOffsetByteSize(); // Type DIE Offset
  291. }
  292. void addGlobalName(StringRef Name, const DIE &Die,
  293. const DIScope *Context) override;
  294. void addGlobalType(const DIType *Ty, const DIE &Die,
  295. const DIScope *Context) override;
  296. DwarfCompileUnit &getCU() override { return CU; }
  297. };
  298. } // end llvm namespace
  299. #endif