DWARFDie.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDie.h -----------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARF_DWARFDIE_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFDIE_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/iterator.h"
  17. #include "llvm/ADT/iterator_range.h"
  18. #include "llvm/BinaryFormat/Dwarf.h"
  19. #include "llvm/DebugInfo/DIContext.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFAttribute.h"
  22. #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
  23. #include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
  24. #include <cassert>
  25. #include <cstdint>
  26. #include <iterator>
  27. namespace llvm {
  28. class DWARFUnit;
  29. class raw_ostream;
  30. //===----------------------------------------------------------------------===//
  31. /// Utility class that carries the DWARF compile/type unit and the debug info
  32. /// entry in an object.
  33. ///
  34. /// When accessing information from a debug info entry we always need to DWARF
  35. /// compile/type unit in order to extract the info correctly as some information
  36. /// is relative to the compile/type unit. Prior to this class the DWARFUnit and
  37. /// the DWARFDebugInfoEntry was passed around separately and there was the
  38. /// possibility for error if the wrong DWARFUnit was used to extract a unit
  39. /// relative offset. This class helps to ensure that this doesn't happen and
  40. /// also simplifies the attribute extraction calls by not having to specify the
  41. /// DWARFUnit for each call.
  42. class DWARFDie {
  43. DWARFUnit *U = nullptr;
  44. const DWARFDebugInfoEntry *Die = nullptr;
  45. public:
  46. DWARFDie() = default;
  47. DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry *D) : U(Unit), Die(D) {}
  48. bool isValid() const { return U && Die; }
  49. explicit operator bool() const { return isValid(); }
  50. const DWARFDebugInfoEntry *getDebugInfoEntry() const { return Die; }
  51. DWARFUnit *getDwarfUnit() const { return U; }
  52. /// Get the abbreviation declaration for this DIE.
  53. ///
  54. /// \returns the abbreviation declaration or NULL for null tags.
  55. const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const {
  56. assert(isValid() && "must check validity prior to calling");
  57. return Die->getAbbreviationDeclarationPtr();
  58. }
  59. /// Get the absolute offset into the debug info or types section.
  60. ///
  61. /// \returns the DIE offset or -1U if invalid.
  62. uint64_t getOffset() const {
  63. assert(isValid() && "must check validity prior to calling");
  64. return Die->getOffset();
  65. }
  66. dwarf::Tag getTag() const {
  67. auto AbbrevDecl = getAbbreviationDeclarationPtr();
  68. if (AbbrevDecl)
  69. return AbbrevDecl->getTag();
  70. return dwarf::DW_TAG_null;
  71. }
  72. bool hasChildren() const {
  73. assert(isValid() && "must check validity prior to calling");
  74. return Die->hasChildren();
  75. }
  76. /// Returns true for a valid DIE that terminates a sibling chain.
  77. bool isNULL() const { return getAbbreviationDeclarationPtr() == nullptr; }
  78. /// Returns true if DIE represents a subprogram (not inlined).
  79. bool isSubprogramDIE() const;
  80. /// Returns true if DIE represents a subprogram or an inlined subroutine.
  81. bool isSubroutineDIE() const;
  82. /// Get the parent of this DIE object.
  83. ///
  84. /// \returns a valid DWARFDie instance if this object has a parent or an
  85. /// invalid DWARFDie instance if it doesn't.
  86. DWARFDie getParent() const;
  87. /// Get the sibling of this DIE object.
  88. ///
  89. /// \returns a valid DWARFDie instance if this object has a sibling or an
  90. /// invalid DWARFDie instance if it doesn't.
  91. DWARFDie getSibling() const;
  92. /// Get the previous sibling of this DIE object.
  93. ///
  94. /// \returns a valid DWARFDie instance if this object has a sibling or an
  95. /// invalid DWARFDie instance if it doesn't.
  96. DWARFDie getPreviousSibling() const;
  97. /// Get the first child of this DIE object.
  98. ///
  99. /// \returns a valid DWARFDie instance if this object has children or an
  100. /// invalid DWARFDie instance if it doesn't.
  101. DWARFDie getFirstChild() const;
  102. /// Get the last child of this DIE object.
  103. ///
  104. /// \returns a valid null DWARFDie instance if this object has children or an
  105. /// invalid DWARFDie instance if it doesn't.
  106. DWARFDie getLastChild() const;
  107. /// Dump the DIE and all of its attributes to the supplied stream.
  108. ///
  109. /// \param OS the stream to use for output.
  110. /// \param indent the number of characters to indent each line that is output.
  111. void dump(raw_ostream &OS, unsigned indent = 0,
  112. DIDumpOptions DumpOpts = DIDumpOptions()) const;
  113. /// Convenience zero-argument overload for debugging.
  114. LLVM_DUMP_METHOD void dump() const;
  115. /// Extract the specified attribute from this DIE.
  116. ///
  117. /// Extract an attribute value from this DIE only. This call doesn't look
  118. /// for the attribute value in any DW_AT_specification or
  119. /// DW_AT_abstract_origin referenced DIEs.
  120. ///
  121. /// \param Attr the attribute to extract.
  122. /// \returns an optional DWARFFormValue that will have the form value if the
  123. /// attribute was successfully extracted.
  124. std::optional<DWARFFormValue> find(dwarf::Attribute Attr) const;
  125. /// Extract the first value of any attribute in Attrs from this DIE.
  126. ///
  127. /// Extract the first attribute that matches from this DIE only. This call
  128. /// doesn't look for the attribute value in any DW_AT_specification or
  129. /// DW_AT_abstract_origin referenced DIEs. The attributes will be searched
  130. /// linearly in the order they are specified within Attrs.
  131. ///
  132. /// \param Attrs an array of DWARF attribute to look for.
  133. /// \returns an optional that has a valid DWARFFormValue for the first
  134. /// matching attribute in Attrs, or std::nullopt if none of the attributes in
  135. /// Attrs exist in this DIE.
  136. std::optional<DWARFFormValue> find(ArrayRef<dwarf::Attribute> Attrs) const;
  137. /// Extract the first value of any attribute in Attrs from this DIE and
  138. /// recurse into any DW_AT_specification or DW_AT_abstract_origin referenced
  139. /// DIEs.
  140. ///
  141. /// \param Attrs an array of DWARF attribute to look for.
  142. /// \returns an optional that has a valid DWARFFormValue for the first
  143. /// matching attribute in Attrs, or std::nullopt if none of the attributes in
  144. /// Attrs exist in this DIE or in any DW_AT_specification or
  145. /// DW_AT_abstract_origin DIEs.
  146. std::optional<DWARFFormValue>
  147. findRecursively(ArrayRef<dwarf::Attribute> Attrs) const;
  148. /// Extract the specified attribute from this DIE as the referenced DIE.
  149. ///
  150. /// Regardless of the reference type, return the correct DWARFDie instance if
  151. /// the attribute exists. The returned DWARFDie object might be from another
  152. /// DWARFUnit, but that is all encapsulated in the new DWARFDie object.
  153. ///
  154. /// Extract an attribute value from this DIE only. This call doesn't look
  155. /// for the attribute value in any DW_AT_specification or
  156. /// DW_AT_abstract_origin referenced DIEs.
  157. ///
  158. /// \param Attr the attribute to extract.
  159. /// \returns a valid DWARFDie instance if the attribute exists, or an invalid
  160. /// DWARFDie object if it doesn't.
  161. DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const;
  162. DWARFDie getAttributeValueAsReferencedDie(const DWARFFormValue &V) const;
  163. DWARFDie resolveTypeUnitReference() const;
  164. /// Extract the range base attribute from this DIE as absolute section offset.
  165. ///
  166. /// This is a utility function that checks for either the DW_AT_rnglists_base
  167. /// or DW_AT_GNU_ranges_base attribute.
  168. ///
  169. /// \returns anm optional absolute section offset value for the attribute.
  170. std::optional<uint64_t> getRangesBaseAttribute() const;
  171. std::optional<uint64_t> getLocBaseAttribute() const;
  172. /// Get the DW_AT_high_pc attribute value as an address.
  173. ///
  174. /// In DWARF version 4 and later the high PC can be encoded as an offset from
  175. /// the DW_AT_low_pc. This function takes care of extracting the value as an
  176. /// address or offset and adds it to the low PC if needed and returns the
  177. /// value as an optional in case the DIE doesn't have a DW_AT_high_pc
  178. /// attribute.
  179. ///
  180. /// \param LowPC the low PC that might be needed to calculate the high PC.
  181. /// \returns an optional address value for the attribute.
  182. std::optional<uint64_t> getHighPC(uint64_t LowPC) const;
  183. /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
  184. /// Returns true if both attributes are present.
  185. bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
  186. uint64_t &SectionIndex) const;
  187. /// Get the address ranges for this DIE.
  188. ///
  189. /// Get the hi/low PC range if both attributes are available or exrtracts the
  190. /// non-contiguous address ranges from the DW_AT_ranges attribute.
  191. ///
  192. /// Extracts the range information from this DIE only. This call doesn't look
  193. /// for the range in any DW_AT_specification or DW_AT_abstract_origin DIEs.
  194. ///
  195. /// \returns a address range vector that might be empty if no address range
  196. /// information is available.
  197. Expected<DWARFAddressRangesVector> getAddressRanges() const;
  198. bool addressRangeContainsAddress(const uint64_t Address) const;
  199. Expected<DWARFLocationExpressionsVector>
  200. getLocations(dwarf::Attribute Attr) const;
  201. /// If a DIE represents a subprogram (or inlined subroutine), returns its
  202. /// mangled name (or short name, if mangled is missing). This name may be
  203. /// fetched from specification or abstract origin for this subprogram.
  204. /// Returns null if no name is found.
  205. const char *getSubroutineName(DINameKind Kind) const;
  206. /// Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin
  207. /// references if necessary. For the LinkageName case it additionaly searches
  208. /// for ShortName if LinkageName is not found.
  209. /// Returns null if no name is found.
  210. const char *getName(DINameKind Kind) const;
  211. void getFullName(raw_string_ostream &,
  212. std::string *OriginalFullName = nullptr) const;
  213. /// Return the DIE short name resolving DW_AT_specification or
  214. /// DW_AT_abstract_origin references if necessary. Returns null if no name
  215. /// is found.
  216. const char *getShortName() const;
  217. /// Return the DIE linkage name resolving DW_AT_specification or
  218. /// DW_AT_abstract_origin references if necessary. Returns null if no name
  219. /// is found.
  220. const char *getLinkageName() const;
  221. /// Returns the declaration line (start line) for a DIE, assuming it specifies
  222. /// a subprogram. This may be fetched from specification or abstract origin
  223. /// for this subprogram by resolving DW_AT_sepcification or
  224. /// DW_AT_abstract_origin references if necessary.
  225. uint64_t getDeclLine() const;
  226. std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const;
  227. /// Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column
  228. /// from DIE (or zeroes if they are missing). This function looks for
  229. /// DW_AT_call attributes in this DIE only, it will not resolve the attribute
  230. /// values in any DW_AT_specification or DW_AT_abstract_origin DIEs.
  231. /// \param CallFile filled in with non-zero if successful, zero if there is no
  232. /// DW_AT_call_file attribute in this DIE.
  233. /// \param CallLine filled in with non-zero if successful, zero if there is no
  234. /// DW_AT_call_line attribute in this DIE.
  235. /// \param CallColumn filled in with non-zero if successful, zero if there is
  236. /// no DW_AT_call_column attribute in this DIE.
  237. /// \param CallDiscriminator filled in with non-zero if successful, zero if
  238. /// there is no DW_AT_GNU_discriminator attribute in this DIE.
  239. void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
  240. uint32_t &CallColumn, uint32_t &CallDiscriminator) const;
  241. class attribute_iterator;
  242. /// Get an iterator range to all attributes in the current DIE only.
  243. ///
  244. /// \returns an iterator range for the attributes of the current DIE.
  245. iterator_range<attribute_iterator> attributes() const;
  246. /// Gets the type size (in bytes) for this DIE.
  247. ///
  248. /// \param PointerSize the pointer size of the containing CU.
  249. /// \returns if this is a type DIE, or this DIE contains a DW_AT_type, returns
  250. /// the size of the type.
  251. std::optional<uint64_t> getTypeSize(uint64_t PointerSize);
  252. class iterator;
  253. iterator begin() const;
  254. iterator end() const;
  255. std::reverse_iterator<iterator> rbegin() const;
  256. std::reverse_iterator<iterator> rend() const;
  257. iterator_range<iterator> children() const;
  258. };
  259. class DWARFDie::attribute_iterator
  260. : public iterator_facade_base<attribute_iterator, std::forward_iterator_tag,
  261. const DWARFAttribute> {
  262. /// The DWARF DIE we are extracting attributes from.
  263. DWARFDie Die;
  264. /// The value vended to clients via the operator*() or operator->().
  265. DWARFAttribute AttrValue;
  266. /// The attribute index within the abbreviation declaration in Die.
  267. uint32_t Index;
  268. friend bool operator==(const attribute_iterator &LHS,
  269. const attribute_iterator &RHS);
  270. /// Update the attribute index and attempt to read the attribute value. If the
  271. /// attribute is able to be read, update AttrValue and the Index member
  272. /// variable. If the attribute value is not able to be read, an appropriate
  273. /// error will be set if the Err member variable is non-NULL and the iterator
  274. /// will be set to the end value so iteration stops.
  275. void updateForIndex(const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I);
  276. public:
  277. attribute_iterator() = delete;
  278. explicit attribute_iterator(DWARFDie D, bool End);
  279. attribute_iterator &operator++();
  280. attribute_iterator &operator--();
  281. explicit operator bool() const { return AttrValue.isValid(); }
  282. const DWARFAttribute &operator*() const { return AttrValue; }
  283. };
  284. inline bool operator==(const DWARFDie::attribute_iterator &LHS,
  285. const DWARFDie::attribute_iterator &RHS) {
  286. return LHS.Index == RHS.Index;
  287. }
  288. inline bool operator!=(const DWARFDie::attribute_iterator &LHS,
  289. const DWARFDie::attribute_iterator &RHS) {
  290. return !(LHS == RHS);
  291. }
  292. inline bool operator==(const DWARFDie &LHS, const DWARFDie &RHS) {
  293. return LHS.getDebugInfoEntry() == RHS.getDebugInfoEntry() &&
  294. LHS.getDwarfUnit() == RHS.getDwarfUnit();
  295. }
  296. inline bool operator!=(const DWARFDie &LHS, const DWARFDie &RHS) {
  297. return !(LHS == RHS);
  298. }
  299. inline bool operator<(const DWARFDie &LHS, const DWARFDie &RHS) {
  300. return LHS.getOffset() < RHS.getOffset();
  301. }
  302. class DWARFDie::iterator
  303. : public iterator_facade_base<iterator, std::bidirectional_iterator_tag,
  304. const DWARFDie> {
  305. DWARFDie Die;
  306. friend std::reverse_iterator<llvm::DWARFDie::iterator>;
  307. friend bool operator==(const DWARFDie::iterator &LHS,
  308. const DWARFDie::iterator &RHS);
  309. public:
  310. iterator() = default;
  311. explicit iterator(DWARFDie D) : Die(D) {}
  312. iterator &operator++() {
  313. Die = Die.getSibling();
  314. return *this;
  315. }
  316. iterator &operator--() {
  317. Die = Die.getPreviousSibling();
  318. return *this;
  319. }
  320. const DWARFDie &operator*() const { return Die; }
  321. };
  322. inline bool operator==(const DWARFDie::iterator &LHS,
  323. const DWARFDie::iterator &RHS) {
  324. return LHS.Die == RHS.Die;
  325. }
  326. // These inline functions must follow the DWARFDie::iterator definition above
  327. // as they use functions from that class.
  328. inline DWARFDie::iterator DWARFDie::begin() const {
  329. return iterator(getFirstChild());
  330. }
  331. inline DWARFDie::iterator DWARFDie::end() const {
  332. return iterator(getLastChild());
  333. }
  334. inline iterator_range<DWARFDie::iterator> DWARFDie::children() const {
  335. return make_range(begin(), end());
  336. }
  337. } // end namespace llvm
  338. namespace std {
  339. template <>
  340. class reverse_iterator<llvm::DWARFDie::iterator>
  341. : public llvm::iterator_facade_base<
  342. reverse_iterator<llvm::DWARFDie::iterator>,
  343. bidirectional_iterator_tag, const llvm::DWARFDie> {
  344. private:
  345. llvm::DWARFDie Die;
  346. bool AtEnd;
  347. public:
  348. reverse_iterator(llvm::DWARFDie::iterator It)
  349. : Die(It.Die), AtEnd(!It.Die.getPreviousSibling()) {
  350. if (!AtEnd)
  351. Die = Die.getPreviousSibling();
  352. }
  353. llvm::DWARFDie::iterator base() const {
  354. return llvm::DWARFDie::iterator(AtEnd ? Die : Die.getSibling());
  355. }
  356. reverse_iterator<llvm::DWARFDie::iterator> &operator++() {
  357. assert(!AtEnd && "Incrementing rend");
  358. llvm::DWARFDie D = Die.getPreviousSibling();
  359. if (D)
  360. Die = D;
  361. else
  362. AtEnd = true;
  363. return *this;
  364. }
  365. reverse_iterator<llvm::DWARFDie::iterator> &operator--() {
  366. if (AtEnd) {
  367. AtEnd = false;
  368. return *this;
  369. }
  370. Die = Die.getSibling();
  371. assert(!Die.isNULL() && "Decrementing rbegin");
  372. return *this;
  373. }
  374. const llvm::DWARFDie &operator*() const {
  375. assert(Die.isValid());
  376. return Die;
  377. }
  378. // FIXME: We should be able to specify the equals operator as a friend, but
  379. // that causes the compiler to think the operator overload is ambiguous
  380. // with the friend declaration and the actual definition as candidates.
  381. bool equals(const reverse_iterator<llvm::DWARFDie::iterator> &RHS) const {
  382. return Die == RHS.Die && AtEnd == RHS.AtEnd;
  383. }
  384. };
  385. } // namespace std
  386. namespace llvm {
  387. inline bool operator==(const std::reverse_iterator<DWARFDie::iterator> &LHS,
  388. const std::reverse_iterator<DWARFDie::iterator> &RHS) {
  389. return LHS.equals(RHS);
  390. }
  391. inline bool operator!=(const std::reverse_iterator<DWARFDie::iterator> &LHS,
  392. const std::reverse_iterator<DWARFDie::iterator> &RHS) {
  393. return !(LHS == RHS);
  394. }
  395. inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rbegin() const {
  396. return std::make_reverse_iterator(end());
  397. }
  398. inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rend() const {
  399. return std::make_reverse_iterator(begin());
  400. }
  401. void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS);
  402. void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS,
  403. std::string *OriginalFullName = nullptr);
  404. } // end namespace llvm
  405. #endif // LLVM_DEBUGINFO_DWARF_DWARFDIE_H
  406. #ifdef __GNUC__
  407. #pragma GCC diagnostic pop
  408. #endif