DwarfFile.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- 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. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
  9. #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
  10. #include "DwarfStringPool.h"
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/CodeGen/DIE.h"
  15. #include "llvm/IR/Metadata.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include <map>
  18. #include <memory>
  19. #include <utility>
  20. namespace llvm {
  21. class AsmPrinter;
  22. class DbgEntity;
  23. class DbgVariable;
  24. class DbgLabel;
  25. class DwarfCompileUnit;
  26. class DwarfUnit;
  27. class LexicalScope;
  28. class MCSection;
  29. // Data structure to hold a range for range lists.
  30. class RangeSpan {
  31. public:
  32. RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
  33. const MCSymbol *getStart() const { return Start; }
  34. const MCSymbol *getEnd() const { return End; }
  35. void setEnd(const MCSymbol *E) { End = E; }
  36. private:
  37. const MCSymbol *Start, *End;
  38. };
  39. class RangeSpanList {
  40. private:
  41. // Index for locating within the debug_range section this particular span.
  42. MCSymbol *RangeSym;
  43. const DwarfCompileUnit *CU;
  44. // List of ranges.
  45. SmallVector<RangeSpan, 2> Ranges;
  46. public:
  47. RangeSpanList(MCSymbol *Sym, const DwarfCompileUnit &CU,
  48. SmallVector<RangeSpan, 2> Ranges)
  49. : RangeSym(Sym), CU(&CU), Ranges(std::move(Ranges)) {}
  50. MCSymbol *getSym() const { return RangeSym; }
  51. const DwarfCompileUnit &getCU() const { return *CU; }
  52. const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
  53. };
  54. class DwarfFile {
  55. // Target of Dwarf emission, used for sizing of abbreviations.
  56. AsmPrinter *Asm;
  57. BumpPtrAllocator AbbrevAllocator;
  58. // Used to uniquely define abbreviations.
  59. DIEAbbrevSet Abbrevs;
  60. // A pointer to all units in the section.
  61. SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
  62. DwarfStringPool StrPool;
  63. // List of range lists for a given compile unit, separate from the ranges for
  64. // the CU itself.
  65. SmallVector<RangeSpanList, 1> CURangeLists;
  66. /// DWARF v5: The symbol that designates the start of the contribution to
  67. /// the string offsets table. The contribution is shared by all units.
  68. MCSymbol *StringOffsetsStartSym = nullptr;
  69. /// DWARF v5: The symbol that designates the base of the range list table.
  70. /// The table is shared by all units.
  71. MCSymbol *RnglistsTableBaseSym = nullptr;
  72. /// DWARF v5: The symbol that designates the base of the locations list table.
  73. /// The table is shared by all units.
  74. MCSymbol *LoclistsTableBaseSym = nullptr;
  75. /// The variables of a lexical scope.
  76. struct ScopeVars {
  77. /// We need to sort Args by ArgNo and check for duplicates. This could also
  78. /// be implemented as a list or vector + std::lower_bound().
  79. std::map<unsigned, DbgVariable *> Args;
  80. SmallVector<DbgVariable *, 8> Locals;
  81. };
  82. /// Collection of DbgVariables of each lexical scope.
  83. DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
  84. /// Collection of DbgLabels of each lexical scope.
  85. using LabelList = SmallVector<DbgLabel *, 4>;
  86. DenseMap<LexicalScope *, LabelList> ScopeLabels;
  87. // Collection of abstract subprogram DIEs.
  88. DenseMap<const MDNode *, DIE *> AbstractSPDies;
  89. DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
  90. /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
  91. /// be shared across CUs, that is why we keep the map here instead
  92. /// of in DwarfCompileUnit.
  93. DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
  94. public:
  95. DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
  96. const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
  97. return CUs;
  98. }
  99. std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU,
  100. SmallVector<RangeSpan, 2> R);
  101. /// getRangeLists - Get the vector of range lists.
  102. const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
  103. return CURangeLists;
  104. }
  105. /// Compute the size and offset of a DIE given an incoming Offset.
  106. unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
  107. /// Compute the size and offset of all the DIEs.
  108. void computeSizeAndOffsets();
  109. /// Compute the size and offset of all the DIEs in the given unit.
  110. /// \returns The size of the root DIE.
  111. unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
  112. /// Add a unit to the list of CUs.
  113. void addUnit(std::unique_ptr<DwarfCompileUnit> U);
  114. /// Emit all of the units to the section listed with the given
  115. /// abbreviation section.
  116. void emitUnits(bool UseOffsets);
  117. /// Emit the given unit to its section.
  118. void emitUnit(DwarfUnit *TheU, bool UseOffsets);
  119. /// Emit a set of abbreviations to the specific section.
  120. void emitAbbrevs(MCSection *);
  121. /// Emit all of the strings to the section given. If OffsetSection is
  122. /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
  123. /// is false, emit absolute offsets to the strings. Otherwise, emit
  124. /// relocatable references to the strings if they are supported by the target.
  125. void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
  126. bool UseRelativeOffsets = false);
  127. /// Returns the string pool.
  128. DwarfStringPool &getStringPool() { return StrPool; }
  129. MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
  130. void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
  131. MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
  132. void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
  133. MCSymbol *getLoclistsTableBaseSym() const { return LoclistsTableBaseSym; }
  134. void setLoclistsTableBaseSym(MCSymbol *Sym) { LoclistsTableBaseSym = Sym; }
  135. /// \returns false if the variable was merged with a previous one.
  136. bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
  137. void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
  138. DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
  139. return ScopeVariables;
  140. }
  141. DenseMap<LexicalScope *, LabelList> &getScopeLabels() {
  142. return ScopeLabels;
  143. }
  144. DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
  145. return AbstractSPDies;
  146. }
  147. DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
  148. return AbstractEntities;
  149. }
  150. void insertDIE(const MDNode *TypeMD, DIE *Die) {
  151. DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
  152. }
  153. DIE *getDIE(const MDNode *TypeMD) {
  154. return DITypeNodeToDieMap.lookup(TypeMD);
  155. }
  156. };
  157. } // end namespace llvm
  158. #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H