DwarfUnit.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains support for writing dwarf compile unit.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
  14. #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
  15. #include "DwarfDebug.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/ADT/StringMap.h"
  19. #include "llvm/CodeGen/AsmPrinter.h"
  20. #include "llvm/CodeGen/DIE.h"
  21. #include "llvm/IR/DIBuilder.h"
  22. #include "llvm/IR/DebugInfo.h"
  23. #include "llvm/MC/MCDwarf.h"
  24. #include "llvm/MC/MCExpr.h"
  25. #include "llvm/MC/MCSection.h"
  26. namespace llvm {
  27. class MachineLocation;
  28. class MachineOperand;
  29. class ConstantInt;
  30. class ConstantFP;
  31. class DbgVariable;
  32. class DwarfCompileUnit;
  33. // Data structure to hold a range for range lists.
  34. class RangeSpan {
  35. public:
  36. RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
  37. const MCSymbol *getStart() const { return Start; }
  38. const MCSymbol *getEnd() const { return End; }
  39. void setEnd(const MCSymbol *E) { End = E; }
  40. private:
  41. const MCSymbol *Start, *End;
  42. };
  43. class RangeSpanList {
  44. private:
  45. // Index for locating within the debug_range section this particular span.
  46. MCSymbol *RangeSym;
  47. // List of ranges.
  48. SmallVector<RangeSpan, 2> Ranges;
  49. public:
  50. RangeSpanList(MCSymbol *Sym, SmallVector<RangeSpan, 2> Ranges)
  51. : RangeSym(Sym), Ranges(std::move(Ranges)) {}
  52. MCSymbol *getSym() const { return RangeSym; }
  53. const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
  54. void addRange(RangeSpan Range) { Ranges.push_back(Range); }
  55. };
  56. // //
  57. ///////////////////////////////////////////////////////////////////////////////
  58. /// This dwarf writer support class manages information associated with a
  59. /// source file.
  60. class DwarfUnit {
  61. protected:
  62. /// A numeric ID unique among all CUs in the module
  63. unsigned UniqueID;
  64. /// MDNode for the compile unit.
  65. const DICompileUnit *CUNode;
  66. // All DIEValues are allocated through this allocator.
  67. BumpPtrAllocator DIEValueAllocator;
  68. /// Unit debug information entry.
  69. DIE &UnitDie;
  70. /// Offset of the UnitDie from beginning of debug info section.
  71. unsigned DebugInfoOffset;
  72. /// Target of Dwarf emission.
  73. AsmPrinter *Asm;
  74. // Holders for some common dwarf information.
  75. DwarfDebug *DD;
  76. DwarfFile *DU;
  77. /// An anonymous type for index type. Owned by UnitDie.
  78. DIE *IndexTyDie;
  79. /// Tracks the mapping of unit level debug information variables to debug
  80. /// information entries.
  81. DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
  82. /// A list of all the DIEBlocks in use.
  83. std::vector<DIEBlock *> DIEBlocks;
  84. /// A list of all the DIELocs in use.
  85. std::vector<DIELoc *> DIELocs;
  86. /// This map is used to keep track of subprogram DIEs that need
  87. /// DW_AT_containing_type attribute. This attribute points to a DIE that
  88. /// corresponds to the MDNode mapped with the subprogram DIE.
  89. DenseMap<DIE *, const DINode *> ContainingTypeMap;
  90. /// The section this unit will be emitted in.
  91. MCSection *Section;
  92. DwarfUnit(unsigned UID, dwarf::Tag, const DICompileUnit *CU, AsmPrinter *A,
  93. DwarfDebug *DW, DwarfFile *DWU);
  94. /// Add a string attribute data and value.
  95. ///
  96. /// This is guaranteed to be in the local string pool instead of indirected.
  97. void addLocalString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
  98. void addIndexedString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
  99. bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie);
  100. public:
  101. virtual ~DwarfUnit();
  102. void initSection(MCSection *Section);
  103. MCSection *getSection() const {
  104. assert(Section);
  105. return Section;
  106. }
  107. // Accessors.
  108. AsmPrinter* getAsmPrinter() const { return Asm; }
  109. unsigned getUniqueID() const { return UniqueID; }
  110. uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
  111. const DICompileUnit *getCUNode() const { return CUNode; }
  112. DIE &getUnitDie() { return UnitDie; }
  113. unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
  114. void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
  115. /// Return true if this compile unit has something to write out.
  116. bool hasContent() const { return UnitDie.hasChildren(); }
  117. /// Get string containing language specific context for a global name.
  118. ///
  119. /// Walks the metadata parent chain in a language specific manner (using the
  120. /// compile unit language) and returns it as a string. This is done at the
  121. /// metadata level because DIEs may not currently have been added to the
  122. /// parent context and walking the DIEs looking for names is more expensive
  123. /// than walking the metadata.
  124. std::string getParentContextString(const DIScope *Context) const;
  125. /// Add a new global name to the compile unit.
  126. virtual void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) {
  127. }
  128. /// Add a new global type to the compile unit.
  129. virtual void addGlobalType(const DIType *Ty, const DIE &Die,
  130. const DIScope *Context) {}
  131. /// Add a new name to the namespace accelerator table.
  132. void addAccelNamespace(StringRef Name, const DIE &Die);
  133. /// Returns the DIE map slot for the specified debug variable.
  134. ///
  135. /// We delegate the request to DwarfDebug when the MDNode can be part of the
  136. /// type system, since DIEs for the type system can be shared across CUs and
  137. /// the mappings are kept in DwarfDebug.
  138. DIE *getDIE(const DINode *D) const;
  139. /// Returns a fresh newly allocated DIELoc.
  140. DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; }
  141. /// Insert DIE into the map.
  142. ///
  143. /// We delegate the request to DwarfDebug when the MDNode can be part of the
  144. /// type system, since DIEs for the type system can be shared across CUs and
  145. /// the mappings are kept in DwarfDebug.
  146. void insertDIE(const DINode *Desc, DIE *D);
  147. /// Add a flag that is true to the DIE.
  148. void addFlag(DIE &Die, dwarf::Attribute Attribute);
  149. /// Add an unsigned integer attribute data and value.
  150. void addUInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
  151. uint64_t Integer);
  152. void addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer);
  153. /// Add an signed integer attribute data and value.
  154. void addSInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
  155. int64_t Integer);
  156. void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
  157. /// Add a string attribute data and value.
  158. ///
  159. /// We always emit a reference to the string pool instead of immediate
  160. /// strings so that DIEs have more predictable sizes. In the case of split
  161. /// dwarf we emit an index into another table which gets us the static offset
  162. /// into the string table.
  163. void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
  164. /// Add a Dwarf label attribute data and value.
  165. DIE::value_iterator addLabel(DIE &Die, dwarf::Attribute Attribute,
  166. dwarf::Form Form, const MCSymbol *Label);
  167. void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
  168. /// Add an offset into a section attribute data and value.
  169. void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
  170. /// Add a dwarf op address data and value using the form given and an
  171. /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
  172. void addOpAddress(DIELoc &Die, const MCSymbol *Label);
  173. /// Add a label delta attribute data and value.
  174. void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
  175. const MCSymbol *Lo);
  176. /// Add a DIE attribute data and value.
  177. void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
  178. /// Add a DIE attribute data and value.
  179. void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry);
  180. void addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type);
  181. /// Add block data.
  182. void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Block);
  183. /// Add block data.
  184. void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
  185. /// Add location information to specified debug information entry.
  186. void addSourceLine(DIE &Die, unsigned Line, StringRef File,
  187. StringRef Directory);
  188. void addSourceLine(DIE &Die, const DILocalVariable *V);
  189. void addSourceLine(DIE &Die, const DIGlobalVariable *G);
  190. void addSourceLine(DIE &Die, const DISubprogram *SP);
  191. void addSourceLine(DIE &Die, const DIType *Ty);
  192. void addSourceLine(DIE &Die, const DINamespace *NS);
  193. void addSourceLine(DIE &Die, const DIObjCProperty *Ty);
  194. /// Add constant value entry in variable DIE.
  195. void addConstantValue(DIE &Die, const MachineOperand &MO, const DIType *Ty);
  196. void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty);
  197. void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty);
  198. void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
  199. void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
  200. /// Add constant value entry in variable DIE.
  201. void addConstantFPValue(DIE &Die, const MachineOperand &MO);
  202. void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
  203. /// Add a linkage name, if it isn't empty.
  204. void addLinkageName(DIE &Die, StringRef LinkageName);
  205. /// Add template parameters in buffer.
  206. void addTemplateParams(DIE &Buffer, DINodeArray TParams);
  207. /// Add register operand.
  208. /// \returns false if the register does not exist, e.g., because it was never
  209. /// materialized.
  210. bool addRegisterOpPiece(DIELoc &TheDie, unsigned Reg,
  211. unsigned SizeInBits = 0, unsigned OffsetInBits = 0);
  212. /// Add register offset.
  213. /// \returns false if the register does not exist, e.g., because it was never
  214. /// materialized.
  215. bool addRegisterOffset(DIELoc &TheDie, unsigned Reg, int64_t Offset);
  216. // FIXME: Should be reformulated in terms of addComplexAddress.
  217. /// Start with the address based on the location provided, and generate the
  218. /// DWARF information necessary to find the actual Block variable (navigating
  219. /// the Block struct) based on the starting location. Add the DWARF
  220. /// information to the die. Obsolete, please use addComplexAddress instead.
  221. void addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
  222. dwarf::Attribute Attribute,
  223. const MachineLocation &Location);
  224. /// Add a new type attribute to the specified entity.
  225. ///
  226. /// This takes and attribute parameter because DW_AT_friend attributes are
  227. /// also type references.
  228. void addType(DIE &Entity, const DIType *Ty,
  229. dwarf::Attribute Attribute = dwarf::DW_AT_type);
  230. DIE *getOrCreateNameSpace(const DINamespace *NS);
  231. DIE *getOrCreateModule(const DIModule *M);
  232. DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false);
  233. void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
  234. bool Minimal = false);
  235. /// Find existing DIE or create new DIE for the given type.
  236. DIE *getOrCreateTypeDIE(const MDNode *N);
  237. /// Get context owner's DIE.
  238. DIE *createTypeDIE(const DICompositeType *Ty);
  239. /// Get context owner's DIE.
  240. DIE *getOrCreateContextDIE(const DIScope *Context);
  241. /// Construct DIEs for types that contain vtables.
  242. void constructContainingTypeDIEs();
  243. /// Construct function argument DIEs.
  244. void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
  245. /// Create a DIE with the given Tag, add the DIE to its parent, and
  246. /// call insertDIE if MD is not null.
  247. DIE &createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N = nullptr);
  248. /// Compute the size of a header for this unit, not including the initial
  249. /// length field.
  250. virtual unsigned getHeaderSize() const {
  251. return sizeof(int16_t) + // DWARF version number
  252. sizeof(int32_t) + // Offset Into Abbrev. Section
  253. sizeof(int8_t); // Pointer Size (in bytes)
  254. }
  255. /// Emit the header for this unit, not including the initial length field.
  256. virtual void emitHeader(bool UseOffsets);
  257. virtual DwarfCompileUnit &getCU() = 0;
  258. void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);
  259. protected:
  260. /// Create new static data member DIE.
  261. DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT);
  262. /// Look up the source ID with the given directory and source file names. If
  263. /// none currently exists, create a new ID and insert it in the line table.
  264. virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
  265. /// Look in the DwarfDebug map for the MDNode that corresponds to the
  266. /// reference.
  267. template <typename T> T *resolve(TypedDINodeRef<T> Ref) const {
  268. return DD->resolve(Ref);
  269. }
  270. private:
  271. void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy);
  272. void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy);
  273. void constructTypeDIE(DIE &Buffer, const DISubroutineType *DTy);
  274. void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy);
  275. void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy);
  276. void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy);
  277. void constructMemberDIE(DIE &Buffer, const DIDerivedType *DT);
  278. void constructTemplateTypeParameterDIE(DIE &Buffer,
  279. const DITemplateTypeParameter *TP);
  280. void constructTemplateValueParameterDIE(DIE &Buffer,
  281. const DITemplateValueParameter *TVP);
  282. /// Return the default lower bound for an array.
  283. ///
  284. /// If the DWARF version doesn't handle the language, return -1.
  285. int64_t getDefaultLowerBound() const;
  286. /// Get an anonymous type for index type.
  287. DIE *getIndexTyDie();
  288. /// Set D as anonymous type for index which can be reused later.
  289. void setIndexTyDie(DIE *D) { IndexTyDie = D; }
  290. /// If this is a named finished type then include it in the list of types for
  291. /// the accelerator tables.
  292. void updateAcceleratorTables(const DIScope *Context, const DIType *Ty,
  293. const DIE &TyDIE);
  294. virtual bool isDwoUnit() const = 0;
  295. };
  296. class DwarfTypeUnit : public DwarfUnit {
  297. uint64_t TypeSignature;
  298. const DIE *Ty;
  299. DwarfCompileUnit &CU;
  300. MCDwarfDwoLineTable *SplitLineTable;
  301. unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
  302. bool isDwoUnit() const override;
  303. public:
  304. DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A,
  305. DwarfDebug *DW, DwarfFile *DWU,
  306. MCDwarfDwoLineTable *SplitLineTable = nullptr);
  307. void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
  308. uint64_t getTypeSignature() const { return TypeSignature; }
  309. void setType(const DIE *Ty) { this->Ty = Ty; }
  310. /// Emit the header for this unit, not including the initial length field.
  311. void emitHeader(bool UseOffsets) override;
  312. unsigned getHeaderSize() const override {
  313. return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
  314. sizeof(uint32_t); // Type DIE Offset
  315. }
  316. DwarfCompileUnit &getCU() override { return CU; }
  317. };
  318. } // end llvm namespace
  319. #endif