1 //===-- DWARFAbbreviationDeclaration.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_DEBUGINFO_DWARFABBREVIATIONDECLARATION_H
11 #define LLVM_LIB_DEBUGINFO_DWARFABBREVIATIONDECLARATION_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/Support/DataExtractor.h"
16 #include "llvm/Support/Dwarf.h"
23 class DWARFAbbreviationDeclaration {
25 struct AttributeSpec {
26 AttributeSpec(dwarf::Attribute A, dwarf::Form F, Optional<int64_t> V)
27 : Attr(A), Form(F), ByteSizeOrValue(V) {}
28 dwarf::Attribute Attr;
30 /// The following field is used for ByteSize for non-implicit_const
31 /// attributes and as value for implicit_const ones, indicated by
32 /// Form == DW_FORM_implicit_const.
33 /// The following cases are distinguished:
34 /// * Form != DW_FORM_implicit_const and ByteSizeOrValue has a value:
35 /// ByteSizeOrValue contains the fixed size in bytes
36 /// for the Form in this object.
37 /// * Form != DW_FORM_implicit_const and ByteSizeOrValue is None:
38 /// byte size of Form either varies according to the DWARFUnit
39 /// that it is contained in or the value size varies and must be
40 /// decoded from the debug information in order to determine its size.
41 /// * Form == DW_FORM_implicit_const:
42 /// ByteSizeOrValue contains value for the implicit_const attribute.
43 Optional<int64_t> ByteSizeOrValue;
44 bool isImplicitConst() const {
45 return Form == dwarf::DW_FORM_implicit_const;
47 /// Get the fixed byte size of this Form if possible. This function might
48 /// use the DWARFUnit to calculate the size of the Form, like for
49 /// DW_AT_address and DW_AT_ref_addr, so this isn't just an accessor for
50 /// the ByteSize member.
51 Optional<int64_t> getByteSize(const DWARFUnit &U) const;
53 typedef SmallVector<AttributeSpec, 8> AttributeSpecVector;
55 DWARFAbbreviationDeclaration();
57 uint32_t getCode() const { return Code; }
58 dwarf::Tag getTag() const { return Tag; }
59 bool hasChildren() const { return HasChildren; }
61 typedef iterator_range<AttributeSpecVector::const_iterator>
64 attr_iterator_range attributes() const {
65 return attr_iterator_range(AttributeSpecs.begin(), AttributeSpecs.end());
68 dwarf::Form getFormByIndex(uint32_t idx) const {
69 if (idx < AttributeSpecs.size())
70 return AttributeSpecs[idx].Form;
71 return dwarf::Form(0);
74 /// Get the index of the specified attribute.
76 /// Searches the this abbreviation declaration for the index of the specified
79 /// \param attr DWARF attribute to search for.
80 /// \returns Optional index of the attribute if found, None otherwise.
81 Optional<uint32_t> findAttributeIndex(dwarf::Attribute attr) const;
83 /// Extract a DWARF form value from a DIE specified by DIE offset.
85 /// Extract an attribute value for a DWARFUnit given the DIE offset and the
88 /// \param DIEOffset the DIE offset that points to the ULEB128 abbreviation
89 /// code in the .debug_info data.
90 /// \param Attr DWARF attribute to search for.
91 /// \param U the DWARFUnit the contains the DIE.
92 /// \returns Optional DWARF form value if the attribute was extracted.
93 Optional<DWARFFormValue> getAttributeValue(const uint32_t DIEOffset,
94 const dwarf::Attribute Attr,
95 const DWARFUnit &U) const;
97 bool extract(DataExtractor Data, uint32_t* OffsetPtr);
98 void dump(raw_ostream &OS) const;
100 // Return an optional byte size of all attribute data in this abbreviation
101 // if a constant byte size can be calculated given a DWARFUnit. This allows
102 // DWARF parsing to be faster as many DWARF DIEs have a fixed byte size.
103 Optional<size_t> getFixedAttributesByteSize(const DWARFUnit &U) const;
108 /// A helper structure that can quickly determine the size in bytes of an
109 /// abbreviation declaration.
110 struct FixedSizeInfo {
111 /// The fixed byte size for fixed size forms.
113 /// Number of DW_FORM_address forms in this abbrevation declaration.
115 /// Number of DW_FORM_ref_addr forms in this abbrevation declaration.
117 /// Number of 4 byte in DWARF32 and 8 byte in DWARF64 forms.
118 uint8_t NumDwarfOffsets;
121 : NumBytes(0), NumAddrs(0), NumRefAddrs(0), NumDwarfOffsets(0) {}
122 /// Calculate the fixed size in bytes given a DWARFUnit.
124 /// \param U the DWARFUnit to use when determing the byte size.
125 /// \returns the size in bytes for all attribute data in this abbreviation.
126 /// The returned size does not include bytes for the ULEB128 abbreviation
128 size_t getByteSize(const DWARFUnit &U) const;
133 uint8_t CodeByteSize;
135 AttributeSpecVector AttributeSpecs;
136 /// If this abbreviation has a fixed byte size then FixedAttributeSize member
137 /// variable below will have a value.
138 Optional<FixedSizeInfo> FixedAttributeSize;