DwarfUnit.h 14 KB

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