Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:41

0001 //===- DWARFDie.h -----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_DWARF_DWARFDIE_H
0010 #define LLVM_DEBUGINFO_DWARF_DWARFDIE_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/iterator.h"
0014 #include "llvm/ADT/iterator_range.h"
0015 #include "llvm/BinaryFormat/Dwarf.h"
0016 #include "llvm/DebugInfo/DIContext.h"
0017 #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
0018 #include "llvm/DebugInfo/DWARF/DWARFAttribute.h"
0019 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
0020 #include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
0021 #include <cassert>
0022 #include <cstdint>
0023 #include <iterator>
0024 
0025 namespace llvm {
0026 
0027 class DWARFUnit;
0028 class raw_ostream;
0029 
0030 //===----------------------------------------------------------------------===//
0031 /// Utility class that carries the DWARF compile/type unit and the debug info
0032 /// entry in an object.
0033 ///
0034 /// When accessing information from a debug info entry we always need to DWARF
0035 /// compile/type unit in order to extract the info correctly as some information
0036 /// is relative to the compile/type unit. Prior to this class the DWARFUnit and
0037 /// the DWARFDebugInfoEntry was passed around separately and there was the
0038 /// possibility for error if the wrong DWARFUnit was used to extract a unit
0039 /// relative offset. This class helps to ensure that this doesn't happen and
0040 /// also simplifies the attribute extraction calls by not having to specify the
0041 /// DWARFUnit for each call.
0042 class DWARFDie {
0043   DWARFUnit *U = nullptr;
0044   const DWARFDebugInfoEntry *Die = nullptr;
0045 
0046 public:
0047   using DWARFFormValue = llvm::DWARFFormValue;
0048   DWARFDie() = default;
0049   DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry *D) : U(Unit), Die(D) {}
0050 
0051   bool isValid() const { return U && Die; }
0052   explicit operator bool() const { return isValid(); }
0053   const DWARFDebugInfoEntry *getDebugInfoEntry() const { return Die; }
0054   DWARFUnit *getDwarfUnit() const { return U; }
0055 
0056   /// Get the abbreviation declaration for this DIE.
0057   ///
0058   /// \returns the abbreviation declaration or NULL for null tags.
0059   const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const {
0060     assert(isValid() && "must check validity prior to calling");
0061     return Die->getAbbreviationDeclarationPtr();
0062   }
0063 
0064   /// Get the absolute offset into the debug info or types section.
0065   ///
0066   /// \returns the DIE offset or -1U if invalid.
0067   uint64_t getOffset() const {
0068     assert(isValid() && "must check validity prior to calling");
0069     return Die->getOffset();
0070   }
0071 
0072   dwarf::Tag getTag() const {
0073     auto AbbrevDecl = getAbbreviationDeclarationPtr();
0074     if (AbbrevDecl)
0075       return AbbrevDecl->getTag();
0076     return dwarf::DW_TAG_null;
0077   }
0078 
0079   bool hasChildren() const {
0080     assert(isValid() && "must check validity prior to calling");
0081     return Die->hasChildren();
0082   }
0083 
0084   /// Returns true for a valid DIE that terminates a sibling chain.
0085   bool isNULL() const { return getAbbreviationDeclarationPtr() == nullptr; }
0086 
0087   /// Returns true if DIE represents a subprogram (not inlined).
0088   bool isSubprogramDIE() const;
0089 
0090   /// Returns true if DIE represents a subprogram or an inlined subroutine.
0091   bool isSubroutineDIE() const;
0092 
0093   /// Get the parent of this DIE object.
0094   ///
0095   /// \returns a valid DWARFDie instance if this object has a parent or an
0096   /// invalid DWARFDie instance if it doesn't.
0097   DWARFDie getParent() const;
0098 
0099   /// Get the sibling of this DIE object.
0100   ///
0101   /// \returns a valid DWARFDie instance if this object has a sibling or an
0102   /// invalid DWARFDie instance if it doesn't.
0103   DWARFDie getSibling() const;
0104 
0105   /// Get the previous sibling of this DIE object.
0106   ///
0107   /// \returns a valid DWARFDie instance if this object has a sibling or an
0108   /// invalid DWARFDie instance if it doesn't.
0109   DWARFDie getPreviousSibling() const;
0110 
0111   /// Get the first child of this DIE object.
0112   ///
0113   /// \returns a valid DWARFDie instance if this object has children or an
0114   /// invalid DWARFDie instance if it doesn't.
0115   DWARFDie getFirstChild() const;
0116 
0117   /// Get the last child of this DIE object.
0118   ///
0119   /// \returns a valid null DWARFDie instance if this object has children or an
0120   /// invalid DWARFDie instance if it doesn't.
0121   DWARFDie getLastChild() const;
0122 
0123   /// Dump the DIE and all of its attributes to the supplied stream.
0124   ///
0125   /// \param OS the stream to use for output.
0126   /// \param indent the number of characters to indent each line that is output.
0127   void dump(raw_ostream &OS, unsigned indent = 0,
0128             DIDumpOptions DumpOpts = DIDumpOptions()) const;
0129 
0130   /// Convenience zero-argument overload for debugging.
0131   LLVM_DUMP_METHOD void dump() const;
0132 
0133   /// Extract the specified attribute from this DIE.
0134   ///
0135   /// Extract an attribute value from this DIE only. This call doesn't look
0136   /// for the attribute value in any DW_AT_specification or
0137   /// DW_AT_abstract_origin referenced DIEs.
0138   ///
0139   /// \param Attr the attribute to extract.
0140   /// \returns an optional DWARFFormValue that will have the form value if the
0141   /// attribute was successfully extracted.
0142   std::optional<DWARFFormValue> find(dwarf::Attribute Attr) const;
0143 
0144   /// Extract the first value of any attribute in Attrs from this DIE.
0145   ///
0146   /// Extract the first attribute that matches from this DIE only. This call
0147   /// doesn't look for the attribute value in any DW_AT_specification or
0148   /// DW_AT_abstract_origin referenced DIEs. The attributes will be searched
0149   /// linearly in the order they are specified within Attrs.
0150   ///
0151   /// \param Attrs an array of DWARF attribute to look for.
0152   /// \returns an optional that has a valid DWARFFormValue for the first
0153   /// matching attribute in Attrs, or std::nullopt if none of the attributes in
0154   /// Attrs exist in this DIE.
0155   std::optional<DWARFFormValue> find(ArrayRef<dwarf::Attribute> Attrs) const;
0156 
0157   /// Extract the first value of any attribute in Attrs from this DIE and
0158   /// recurse into any DW_AT_specification or DW_AT_abstract_origin referenced
0159   /// DIEs.
0160   ///
0161   /// \param Attrs an array of DWARF attribute to look for.
0162   /// \returns an optional that has a valid DWARFFormValue for the first
0163   /// matching attribute in Attrs, or std::nullopt if none of the attributes in
0164   /// Attrs exist in this DIE or in any DW_AT_specification or
0165   /// DW_AT_abstract_origin DIEs.
0166   std::optional<DWARFFormValue>
0167   findRecursively(ArrayRef<dwarf::Attribute> Attrs) const;
0168 
0169   /// Extract the specified attribute from this DIE as the referenced DIE.
0170   ///
0171   /// Regardless of the reference type, return the correct DWARFDie instance if
0172   /// the attribute exists. The returned DWARFDie object might be from another
0173   /// DWARFUnit, but that is all encapsulated in the new DWARFDie object.
0174   ///
0175   /// Extract an attribute value from this DIE only. This call doesn't look
0176   /// for the attribute value in any DW_AT_specification or
0177   /// DW_AT_abstract_origin referenced DIEs.
0178   ///
0179   /// \param Attr the attribute to extract.
0180   /// \returns a valid DWARFDie instance if the attribute exists, or an invalid
0181   /// DWARFDie object if it doesn't.
0182   DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const;
0183   DWARFDie getAttributeValueAsReferencedDie(const DWARFFormValue &V) const;
0184 
0185   DWARFDie resolveTypeUnitReference() const;
0186 
0187   DWARFDie resolveReferencedType(dwarf::Attribute Attr) const;
0188   DWARFDie resolveReferencedType(const DWARFFormValue &V) const;
0189   /// Extract the range base attribute from this DIE as absolute section offset.
0190   ///
0191   /// This is a utility function that checks for either the DW_AT_rnglists_base
0192   /// or DW_AT_GNU_ranges_base attribute.
0193   ///
0194   /// \returns anm optional absolute section offset value for the attribute.
0195   std::optional<uint64_t> getRangesBaseAttribute() const;
0196   std::optional<uint64_t> getLocBaseAttribute() const;
0197 
0198   /// Get the DW_AT_high_pc attribute value as an address.
0199   ///
0200   /// In DWARF version 4 and later the high PC can be encoded as an offset from
0201   /// the DW_AT_low_pc. This function takes care of extracting the value as an
0202   /// address or offset and adds it to the low PC if needed and returns the
0203   /// value as an optional in case the DIE doesn't have a DW_AT_high_pc
0204   /// attribute.
0205   ///
0206   /// \param LowPC the low PC that might be needed to calculate the high PC.
0207   /// \returns an optional address value for the attribute.
0208   std::optional<uint64_t> getHighPC(uint64_t LowPC) const;
0209 
0210   /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
0211   /// Returns true if both attributes are present.
0212   bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
0213                        uint64_t &SectionIndex) const;
0214 
0215   /// Get the address ranges for this DIE.
0216   ///
0217   /// Get the hi/low PC range if both attributes are available or exrtracts the
0218   /// non-contiguous address ranges from the DW_AT_ranges attribute.
0219   ///
0220   /// Extracts the range information from this DIE only. This call doesn't look
0221   /// for the range in any DW_AT_specification or DW_AT_abstract_origin DIEs.
0222   ///
0223   /// \returns a address range vector that might be empty if no address range
0224   /// information is available.
0225   Expected<DWARFAddressRangesVector> getAddressRanges() const;
0226 
0227   bool addressRangeContainsAddress(const uint64_t Address) const;
0228 
0229   std::optional<uint64_t> getLanguage() const;
0230 
0231   Expected<DWARFLocationExpressionsVector>
0232   getLocations(dwarf::Attribute Attr) const;
0233 
0234   /// If a DIE represents a subprogram (or inlined subroutine), returns its
0235   /// mangled name (or short name, if mangled is missing). This name may be
0236   /// fetched from specification or abstract origin for this subprogram.
0237   /// Returns null if no name is found.
0238   const char *getSubroutineName(DINameKind Kind) const;
0239 
0240   /// Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin
0241   /// references if necessary. For the LinkageName case it additionaly searches
0242   /// for ShortName if LinkageName is not found.
0243   /// Returns null if no name is found.
0244   const char *getName(DINameKind Kind) const;
0245   void getFullName(raw_string_ostream &,
0246                    std::string *OriginalFullName = nullptr) const;
0247 
0248   /// Return the DIE short name resolving DW_AT_specification or
0249   /// DW_AT_abstract_origin references if necessary. Returns null if no name
0250   /// is found.
0251   const char *getShortName() const;
0252 
0253   /// Return the DIE linkage name resolving DW_AT_specification or
0254   /// DW_AT_abstract_origin references if necessary. Returns null if no name
0255   /// is found.
0256   const char *getLinkageName() const;
0257 
0258   /// Returns the declaration line (start line) for a DIE, assuming it specifies
0259   /// a subprogram. This may be fetched from specification or abstract origin
0260   /// for this subprogram by resolving DW_AT_sepcification or
0261   /// DW_AT_abstract_origin references if necessary.
0262   uint64_t getDeclLine() const;
0263   std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const;
0264 
0265   /// Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column
0266   /// from DIE (or zeroes if they are missing). This function looks for
0267   /// DW_AT_call attributes in this DIE only, it will not resolve the attribute
0268   /// values in any DW_AT_specification or DW_AT_abstract_origin DIEs.
0269   /// \param CallFile filled in with non-zero if successful, zero if there is no
0270   /// DW_AT_call_file attribute in this DIE.
0271   /// \param CallLine filled in with non-zero if successful, zero if there is no
0272   /// DW_AT_call_line attribute in this DIE.
0273   /// \param CallColumn filled in with non-zero if successful, zero if there is
0274   /// no DW_AT_call_column attribute in this DIE.
0275   /// \param CallDiscriminator filled in with non-zero if successful, zero if
0276   /// there is no DW_AT_GNU_discriminator attribute in this DIE.
0277   void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
0278                       uint32_t &CallColumn, uint32_t &CallDiscriminator) const;
0279 
0280   class attribute_iterator;
0281 
0282   /// Get an iterator range to all attributes in the current DIE only.
0283   ///
0284   /// \returns an iterator range for the attributes of the current DIE.
0285   iterator_range<attribute_iterator> attributes() const;
0286 
0287   /// Gets the type size (in bytes) for this DIE.
0288   ///
0289   /// \param PointerSize the pointer size of the containing CU.
0290   /// \returns if this is a type DIE, or this DIE contains a DW_AT_type, returns
0291   /// the size of the type.
0292   std::optional<uint64_t> getTypeSize(uint64_t PointerSize);
0293 
0294   class iterator;
0295 
0296   iterator begin() const;
0297   iterator end() const;
0298 
0299   std::reverse_iterator<iterator> rbegin() const;
0300   std::reverse_iterator<iterator> rend() const;
0301 
0302   iterator_range<iterator> children() const;
0303 };
0304 
0305 class DWARFDie::attribute_iterator
0306     : public iterator_facade_base<attribute_iterator, std::forward_iterator_tag,
0307                                   const DWARFAttribute> {
0308   /// The DWARF DIE we are extracting attributes from.
0309   DWARFDie Die;
0310   /// The value vended to clients via the operator*() or operator->().
0311   DWARFAttribute AttrValue;
0312   /// The attribute index within the abbreviation declaration in Die.
0313   uint32_t Index;
0314 
0315   friend bool operator==(const attribute_iterator &LHS,
0316                          const attribute_iterator &RHS);
0317 
0318   /// Update the attribute index and attempt to read the attribute value. If the
0319   /// attribute is able to be read, update AttrValue and the Index member
0320   /// variable. If the attribute value is not able to be read, an appropriate
0321   /// error will be set if the Err member variable is non-NULL and the iterator
0322   /// will be set to the end value so iteration stops.
0323   void updateForIndex(const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I);
0324 
0325 public:
0326   attribute_iterator() = delete;
0327   explicit attribute_iterator(DWARFDie D, bool End);
0328 
0329   attribute_iterator &operator++();
0330   attribute_iterator &operator--();
0331   explicit operator bool() const { return AttrValue.isValid(); }
0332   const DWARFAttribute &operator*() const { return AttrValue; }
0333 };
0334 
0335 inline bool operator==(const DWARFDie::attribute_iterator &LHS,
0336                        const DWARFDie::attribute_iterator &RHS) {
0337   return LHS.Index == RHS.Index;
0338 }
0339 
0340 inline bool operator!=(const DWARFDie::attribute_iterator &LHS,
0341                        const DWARFDie::attribute_iterator &RHS) {
0342   return !(LHS == RHS);
0343 }
0344 
0345 inline bool operator==(const DWARFDie &LHS, const DWARFDie &RHS) {
0346   return LHS.getDebugInfoEntry() == RHS.getDebugInfoEntry() &&
0347          LHS.getDwarfUnit() == RHS.getDwarfUnit();
0348 }
0349 
0350 inline bool operator!=(const DWARFDie &LHS, const DWARFDie &RHS) {
0351   return !(LHS == RHS);
0352 }
0353 
0354 inline bool operator<(const DWARFDie &LHS, const DWARFDie &RHS) {
0355   return LHS.getOffset() < RHS.getOffset();
0356 }
0357 
0358 class DWARFDie::iterator
0359     : public iterator_facade_base<iterator, std::bidirectional_iterator_tag,
0360                                   const DWARFDie> {
0361   DWARFDie Die;
0362 
0363   friend std::reverse_iterator<llvm::DWARFDie::iterator>;
0364   friend bool operator==(const DWARFDie::iterator &LHS,
0365                          const DWARFDie::iterator &RHS);
0366 
0367 public:
0368   iterator() = default;
0369 
0370   explicit iterator(DWARFDie D) : Die(D) {}
0371 
0372   iterator &operator++() {
0373     Die = Die.getSibling();
0374     return *this;
0375   }
0376 
0377   iterator &operator--() {
0378     Die = Die.getPreviousSibling();
0379     return *this;
0380   }
0381 
0382   const DWARFDie &operator*() const { return Die; }
0383 };
0384 
0385 inline bool operator==(const DWARFDie::iterator &LHS,
0386                        const DWARFDie::iterator &RHS) {
0387   return LHS.Die == RHS.Die;
0388 }
0389 
0390 // These inline functions must follow the DWARFDie::iterator definition above
0391 // as they use functions from that class.
0392 inline DWARFDie::iterator DWARFDie::begin() const {
0393   return iterator(getFirstChild());
0394 }
0395 
0396 inline DWARFDie::iterator DWARFDie::end() const {
0397   return iterator(getLastChild());
0398 }
0399 
0400 inline iterator_range<DWARFDie::iterator> DWARFDie::children() const {
0401   return make_range(begin(), end());
0402 }
0403 
0404 } // end namespace llvm
0405 
0406 namespace std {
0407 
0408 template <>
0409 class reverse_iterator<llvm::DWARFDie::iterator>
0410     : public llvm::iterator_facade_base<
0411           reverse_iterator<llvm::DWARFDie::iterator>,
0412           bidirectional_iterator_tag, const llvm::DWARFDie> {
0413 
0414 private:
0415   llvm::DWARFDie Die;
0416   bool AtEnd;
0417 
0418 public:
0419   reverse_iterator(llvm::DWARFDie::iterator It)
0420       : Die(It.Die), AtEnd(!It.Die.getPreviousSibling()) {
0421     if (!AtEnd)
0422       Die = Die.getPreviousSibling();
0423   }
0424 
0425   llvm::DWARFDie::iterator base() const {
0426     return llvm::DWARFDie::iterator(AtEnd ? Die : Die.getSibling());
0427   }
0428 
0429   reverse_iterator<llvm::DWARFDie::iterator> &operator++() {
0430     assert(!AtEnd && "Incrementing rend");
0431     llvm::DWARFDie D = Die.getPreviousSibling();
0432     if (D)
0433       Die = D;
0434     else
0435       AtEnd = true;
0436     return *this;
0437   }
0438 
0439   reverse_iterator<llvm::DWARFDie::iterator> &operator--() {
0440     if (AtEnd) {
0441       AtEnd = false;
0442       return *this;
0443     }
0444     Die = Die.getSibling();
0445     assert(!Die.isNULL() && "Decrementing rbegin");
0446     return *this;
0447   }
0448 
0449   const llvm::DWARFDie &operator*() const {
0450     assert(Die.isValid());
0451     return Die;
0452   }
0453 
0454   // FIXME: We should be able to specify the equals operator as a friend, but
0455   //        that causes the compiler to think the operator overload is ambiguous
0456   //        with the friend declaration and the actual definition as candidates.
0457   bool equals(const reverse_iterator<llvm::DWARFDie::iterator> &RHS) const {
0458     return Die == RHS.Die && AtEnd == RHS.AtEnd;
0459   }
0460 };
0461 
0462 } // namespace std
0463 
0464 namespace llvm {
0465 
0466 inline bool operator==(const std::reverse_iterator<DWARFDie::iterator> &LHS,
0467                        const std::reverse_iterator<DWARFDie::iterator> &RHS) {
0468   return LHS.equals(RHS);
0469 }
0470 
0471 inline bool operator!=(const std::reverse_iterator<DWARFDie::iterator> &LHS,
0472                        const std::reverse_iterator<DWARFDie::iterator> &RHS) {
0473   return !(LHS == RHS);
0474 }
0475 
0476 inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rbegin() const {
0477   return std::make_reverse_iterator(end());
0478 }
0479 
0480 inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rend() const {
0481   return std::make_reverse_iterator(begin());
0482 }
0483 
0484 void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS);
0485 void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS,
0486                              std::string *OriginalFullName = nullptr);
0487 
0488 } // end namespace llvm
0489 
0490 #endif // LLVM_DEBUGINFO_DWARF_DWARFDIE_H