DWARFContext.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //===-- DWARFContext.h ------------------------------------------*- 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. #ifndef LLVM_LIB_DEBUGINFO_DWARFCONTEXT_H
  10. #define LLVM_LIB_DEBUGINFO_DWARFCONTEXT_H
  11. #include "llvm/ADT/MapVector.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/DebugInfo/DIContext.h"
  14. #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
  15. #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
  17. #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
  18. #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFSection.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
  22. #include <vector>
  23. namespace llvm {
  24. // In place of applying the relocations to the data we've read from disk we use
  25. // a separate mapping table to the side and checking that at locations in the
  26. // dwarf where we expect relocated values. This adds a bit of complexity to the
  27. // dwarf parsing/extraction at the benefit of not allocating memory for the
  28. // entire size of the debug info sections.
  29. typedef DenseMap<uint64_t, std::pair<uint8_t, int64_t> > RelocAddrMap;
  30. /// DWARFContext
  31. /// This data structure is the top level entity that deals with dwarf debug
  32. /// information parsing. The actual data is supplied through pure virtual
  33. /// methods that a concrete implementation provides.
  34. class DWARFContext : public DIContext {
  35. DWARFUnitSection<DWARFCompileUnit> CUs;
  36. std::vector<DWARFUnitSection<DWARFTypeUnit>> TUs;
  37. std::unique_ptr<DWARFDebugAbbrev> Abbrev;
  38. std::unique_ptr<DWARFDebugLoc> Loc;
  39. std::unique_ptr<DWARFDebugAranges> Aranges;
  40. std::unique_ptr<DWARFDebugLine> Line;
  41. std::unique_ptr<DWARFDebugFrame> DebugFrame;
  42. DWARFUnitSection<DWARFCompileUnit> DWOCUs;
  43. std::vector<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
  44. std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
  45. std::unique_ptr<DWARFDebugLocDWO> LocDWO;
  46. DWARFContext(DWARFContext &) = delete;
  47. DWARFContext &operator=(DWARFContext &) = delete;
  48. /// Read compile units from the debug_info section (if necessary)
  49. /// and store them in CUs.
  50. void parseCompileUnits();
  51. /// Read type units from the debug_types sections (if necessary)
  52. /// and store them in TUs.
  53. void parseTypeUnits();
  54. /// Read compile units from the debug_info.dwo section (if necessary)
  55. /// and store them in DWOCUs.
  56. void parseDWOCompileUnits();
  57. /// Read type units from the debug_types.dwo section (if necessary)
  58. /// and store them in DWOTUs.
  59. void parseDWOTypeUnits();
  60. public:
  61. DWARFContext() : DIContext(CK_DWARF) {}
  62. static bool classof(const DIContext *DICtx) {
  63. return DICtx->getKind() == CK_DWARF;
  64. }
  65. void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
  66. typedef DWARFUnitSection<DWARFCompileUnit>::iterator_range cu_iterator_range;
  67. typedef DWARFUnitSection<DWARFTypeUnit>::iterator_range tu_iterator_range;
  68. typedef iterator_range<std::vector<DWARFUnitSection<DWARFTypeUnit>>::iterator> tu_section_iterator_range;
  69. /// Get compile units in this context.
  70. cu_iterator_range compile_units() {
  71. parseCompileUnits();
  72. return cu_iterator_range(CUs.begin(), CUs.end());
  73. }
  74. /// Get type units in this context.
  75. tu_section_iterator_range type_unit_sections() {
  76. parseTypeUnits();
  77. return tu_section_iterator_range(TUs.begin(), TUs.end());
  78. }
  79. /// Get compile units in the DWO context.
  80. cu_iterator_range dwo_compile_units() {
  81. parseDWOCompileUnits();
  82. return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
  83. }
  84. /// Get type units in the DWO context.
  85. tu_section_iterator_range dwo_type_unit_sections() {
  86. parseDWOTypeUnits();
  87. return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
  88. }
  89. /// Get the number of compile units in this context.
  90. unsigned getNumCompileUnits() {
  91. parseCompileUnits();
  92. return CUs.size();
  93. }
  94. /// Get the number of compile units in this context.
  95. unsigned getNumTypeUnits() {
  96. parseTypeUnits();
  97. return TUs.size();
  98. }
  99. /// Get the number of compile units in the DWO context.
  100. unsigned getNumDWOCompileUnits() {
  101. parseDWOCompileUnits();
  102. return DWOCUs.size();
  103. }
  104. /// Get the number of compile units in the DWO context.
  105. unsigned getNumDWOTypeUnits() {
  106. parseDWOTypeUnits();
  107. return DWOTUs.size();
  108. }
  109. /// Get the compile unit at the specified index for this compile unit.
  110. DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
  111. parseCompileUnits();
  112. return CUs[index].get();
  113. }
  114. /// Get the compile unit at the specified index for the DWO compile units.
  115. DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
  116. parseDWOCompileUnits();
  117. return DWOCUs[index].get();
  118. }
  119. /// Get a pointer to the parsed DebugAbbrev object.
  120. const DWARFDebugAbbrev *getDebugAbbrev();
  121. /// Get a pointer to the parsed DebugLoc object.
  122. const DWARFDebugLoc *getDebugLoc();
  123. /// Get a pointer to the parsed dwo abbreviations object.
  124. const DWARFDebugAbbrev *getDebugAbbrevDWO();
  125. /// Get a pointer to the parsed DebugLoc object.
  126. const DWARFDebugLocDWO *getDebugLocDWO();
  127. /// Get a pointer to the parsed DebugAranges object.
  128. const DWARFDebugAranges *getDebugAranges();
  129. /// Get a pointer to the parsed frame information object.
  130. const DWARFDebugFrame *getDebugFrame();
  131. /// Get a pointer to a parsed line table corresponding to a compile unit.
  132. const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
  133. DILineInfo getLineInfoForAddress(uint64_t Address,
  134. DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
  135. DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
  136. DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
  137. DIInliningInfo getInliningInfoForAddress(uint64_t Address,
  138. DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
  139. virtual bool isLittleEndian() const = 0;
  140. virtual uint8_t getAddressSize() const = 0;
  141. virtual const DWARFSection &getInfoSection() = 0;
  142. typedef MapVector<object::SectionRef, DWARFSection,
  143. std::map<object::SectionRef, unsigned>> TypeSectionMap;
  144. virtual const TypeSectionMap &getTypesSections() = 0;
  145. virtual StringRef getAbbrevSection() = 0;
  146. virtual const DWARFSection &getLocSection() = 0;
  147. virtual StringRef getARangeSection() = 0;
  148. virtual StringRef getDebugFrameSection() = 0;
  149. virtual const DWARFSection &getLineSection() = 0;
  150. virtual StringRef getStringSection() = 0;
  151. virtual StringRef getRangeSection() = 0;
  152. virtual StringRef getPubNamesSection() = 0;
  153. virtual StringRef getPubTypesSection() = 0;
  154. virtual StringRef getGnuPubNamesSection() = 0;
  155. virtual StringRef getGnuPubTypesSection() = 0;
  156. // Sections for DWARF5 split dwarf proposal.
  157. virtual const DWARFSection &getInfoDWOSection() = 0;
  158. virtual const TypeSectionMap &getTypesDWOSections() = 0;
  159. virtual StringRef getAbbrevDWOSection() = 0;
  160. virtual const DWARFSection &getLineDWOSection() = 0;
  161. virtual const DWARFSection &getLocDWOSection() = 0;
  162. virtual StringRef getStringDWOSection() = 0;
  163. virtual StringRef getStringOffsetDWOSection() = 0;
  164. virtual StringRef getRangeDWOSection() = 0;
  165. virtual StringRef getAddrSection() = 0;
  166. virtual const DWARFSection& getAppleNamesSection() = 0;
  167. virtual const DWARFSection& getAppleTypesSection() = 0;
  168. virtual const DWARFSection& getAppleNamespacesSection() = 0;
  169. virtual const DWARFSection& getAppleObjCSection() = 0;
  170. static bool isSupportedVersion(unsigned version) {
  171. return version == 2 || version == 3 || version == 4;
  172. }
  173. private:
  174. /// Return the compile unit that includes an offset (relative to .debug_info).
  175. DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
  176. /// Return the compile unit which contains instruction with provided
  177. /// address.
  178. DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
  179. };
  180. /// DWARFContextInMemory is the simplest possible implementation of a
  181. /// DWARFContext. It assumes all content is available in memory and stores
  182. /// pointers to it.
  183. class DWARFContextInMemory : public DWARFContext {
  184. virtual void anchor();
  185. bool IsLittleEndian;
  186. uint8_t AddressSize;
  187. DWARFSection InfoSection;
  188. TypeSectionMap TypesSections;
  189. StringRef AbbrevSection;
  190. DWARFSection LocSection;
  191. StringRef ARangeSection;
  192. StringRef DebugFrameSection;
  193. DWARFSection LineSection;
  194. StringRef StringSection;
  195. StringRef RangeSection;
  196. StringRef PubNamesSection;
  197. StringRef PubTypesSection;
  198. StringRef GnuPubNamesSection;
  199. StringRef GnuPubTypesSection;
  200. // Sections for DWARF5 split dwarf proposal.
  201. DWARFSection InfoDWOSection;
  202. TypeSectionMap TypesDWOSections;
  203. StringRef AbbrevDWOSection;
  204. DWARFSection LineDWOSection;
  205. DWARFSection LocDWOSection;
  206. StringRef StringDWOSection;
  207. StringRef StringOffsetDWOSection;
  208. StringRef RangeDWOSection;
  209. StringRef AddrSection;
  210. DWARFSection AppleNamesSection;
  211. DWARFSection AppleTypesSection;
  212. DWARFSection AppleNamespacesSection;
  213. DWARFSection AppleObjCSection;
  214. SmallVector<SmallString<32>, 4> UncompressedSections;
  215. public:
  216. DWARFContextInMemory(const object::ObjectFile &Obj,
  217. const LoadedObjectInfo *L = nullptr);
  218. bool isLittleEndian() const override { return IsLittleEndian; }
  219. uint8_t getAddressSize() const override { return AddressSize; }
  220. const DWARFSection &getInfoSection() override { return InfoSection; }
  221. const TypeSectionMap &getTypesSections() override { return TypesSections; }
  222. StringRef getAbbrevSection() override { return AbbrevSection; }
  223. const DWARFSection &getLocSection() override { return LocSection; }
  224. StringRef getARangeSection() override { return ARangeSection; }
  225. StringRef getDebugFrameSection() override { return DebugFrameSection; }
  226. const DWARFSection &getLineSection() override { return LineSection; }
  227. StringRef getStringSection() override { return StringSection; }
  228. StringRef getRangeSection() override { return RangeSection; }
  229. StringRef getPubNamesSection() override { return PubNamesSection; }
  230. StringRef getPubTypesSection() override { return PubTypesSection; }
  231. StringRef getGnuPubNamesSection() override { return GnuPubNamesSection; }
  232. StringRef getGnuPubTypesSection() override { return GnuPubTypesSection; }
  233. const DWARFSection& getAppleNamesSection() override { return AppleNamesSection; }
  234. const DWARFSection& getAppleTypesSection() override { return AppleTypesSection; }
  235. const DWARFSection& getAppleNamespacesSection() override { return AppleNamespacesSection; }
  236. const DWARFSection& getAppleObjCSection() override { return AppleObjCSection; }
  237. // Sections for DWARF5 split dwarf proposal.
  238. const DWARFSection &getInfoDWOSection() override { return InfoDWOSection; }
  239. const TypeSectionMap &getTypesDWOSections() override {
  240. return TypesDWOSections;
  241. }
  242. StringRef getAbbrevDWOSection() override { return AbbrevDWOSection; }
  243. const DWARFSection &getLineDWOSection() override { return LineDWOSection; }
  244. const DWARFSection &getLocDWOSection() override { return LocDWOSection; }
  245. StringRef getStringDWOSection() override { return StringDWOSection; }
  246. StringRef getStringOffsetDWOSection() override {
  247. return StringOffsetDWOSection;
  248. }
  249. StringRef getRangeDWOSection() override { return RangeDWOSection; }
  250. StringRef getAddrSection() override {
  251. return AddrSection;
  252. }
  253. };
  254. }
  255. #endif