LLVM  14.0.0git
DWARFAbbreviationDeclaration.h
Go to the documentation of this file.
1 //===- DWARFAbbreviationDeclaration.h ---------------------------*- 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 
9 #ifndef LLVM_DEBUGINFO_DWARF_DWARFABBREVIATIONDECLARATION_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFABBREVIATIONDECLARATION_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/SmallVector.h"
17 #include <cassert>
18 #include <cstddef>
19 #include <cstdint>
20 
21 namespace llvm {
22 
23 class DWARFFormValue;
24 class DWARFUnit;
25 class raw_ostream;
26 
28 public:
29  struct AttributeSpec {
31  : Attr(A), Form(F), Value(Value) {
33  }
35  : Attr(A), Form(F) {
37  this->ByteSize.HasByteSize = ByteSize.hasValue();
38  if (this->ByteSize.HasByteSize)
39  this->ByteSize.ByteSize = *ByteSize;
40  }
41 
44 
45  private:
46  /// The following field is used for ByteSize for non-implicit_const
47  /// attributes and as value for implicit_const ones, indicated by
48  /// Form == DW_FORM_implicit_const.
49  /// The following cases are distinguished:
50  /// * Form != DW_FORM_implicit_const and HasByteSize is true:
51  /// ByteSize contains the fixed size in bytes for the Form in this
52  /// object.
53  /// * Form != DW_FORM_implicit_const and HasByteSize is false:
54  /// byte size of Form either varies according to the DWARFUnit
55  /// that it is contained in or the value size varies and must be
56  /// decoded from the debug information in order to determine its size.
57  /// * Form == DW_FORM_implicit_const:
58  /// Value contains value for the implicit_const attribute.
59  struct ByteSizeStorage {
60  bool HasByteSize;
61  uint8_t ByteSize;
62  };
63  union {
64  ByteSizeStorage ByteSize;
65  int64_t Value;
66  };
67 
68  public:
69  bool isImplicitConst() const {
70  return Form == dwarf::DW_FORM_implicit_const;
71  }
72 
73  int64_t getImplicitConstValue() const {
75  return Value;
76  }
77 
78  /// Get the fixed byte size of this Form if possible. This function might
79  /// use the DWARFUnit to calculate the size of the Form, like for
80  /// DW_AT_address and DW_AT_ref_addr, so this isn't just an accessor for
81  /// the ByteSize member.
82  Optional<int64_t> getByteSize(const DWARFUnit &U) const;
83  };
85 
87 
88  uint32_t getCode() const { return Code; }
89  uint8_t getCodeByteSize() const { return CodeByteSize; }
90  dwarf::Tag getTag() const { return Tag; }
91  bool hasChildren() const { return HasChildren; }
92 
93  using attr_iterator_range =
95 
97  return attr_iterator_range(AttributeSpecs.begin(), AttributeSpecs.end());
98  }
99 
101  assert(idx < AttributeSpecs.size());
102  return AttributeSpecs[idx].Form;
103  }
104 
105  size_t getNumAttributes() const {
106  return AttributeSpecs.size();
107  }
108 
110  assert(idx < AttributeSpecs.size());
111  return AttributeSpecs[idx].Attr;
112  }
113 
115  assert(idx < AttributeSpecs.size());
116  return AttributeSpecs[idx].isImplicitConst();
117  }
118 
120  assert(idx < AttributeSpecs.size());
121  return AttributeSpecs[idx].getImplicitConstValue();
122  }
123 
124  /// Get the index of the specified attribute.
125  ///
126  /// Searches the this abbreviation declaration for the index of the specified
127  /// attribute.
128  ///
129  /// \param attr DWARF attribute to search for.
130  /// \returns Optional index of the attribute if found, None otherwise.
132 
133  /// Extract a DWARF form value from a DIE specified by DIE offset.
134  ///
135  /// Extract an attribute value for a DWARFUnit given the DIE offset and the
136  /// attribute.
137  ///
138  /// \param DIEOffset the DIE offset that points to the ULEB128 abbreviation
139  /// code in the .debug_info data.
140  /// \param Attr DWARF attribute to search for.
141  /// \param U the DWARFUnit the contains the DIE.
142  /// \returns Optional DWARF form value if the attribute was extracted.
144  const dwarf::Attribute Attr,
145  const DWARFUnit &U) const;
146 
147  /// Compute an offset from a DIE specified by DIE offset and attribute index.
148  ///
149  /// \param AttrIndex an index of DWARF attribute.
150  /// \param DIEOffset the DIE offset that points to the ULEB128 abbreviation
151  /// code in the .debug_info data.
152  /// \param U the DWARFUnit the contains the DIE.
153  /// \returns an offset of the attribute.
155  const DWARFUnit &U) const;
156 
157  /// Extract a DWARF form value from a DIE speccified by attribute index and
158  /// its offset.
159  ///
160  /// \param AttrIndex an index of DWARF attribute.
161  /// \param Offset offset of the attribute.
162  /// \param U the DWARFUnit the contains the DIE.
163  /// \returns Optional DWARF form value if the attribute was extracted.
166  const DWARFUnit &U) const;
167 
168  bool extract(DataExtractor Data, uint64_t* OffsetPtr);
169  void dump(raw_ostream &OS) const;
170 
171  // Return an optional byte size of all attribute data in this abbreviation
172  // if a constant byte size can be calculated given a DWARFUnit. This allows
173  // DWARF parsing to be faster as many DWARF DIEs have a fixed byte size.
175 
176 private:
177  void clear();
178 
179  /// A helper structure that can quickly determine the size in bytes of an
180  /// abbreviation declaration.
181  struct FixedSizeInfo {
182  /// The fixed byte size for fixed size forms.
183  uint16_t NumBytes = 0;
184  /// Number of DW_FORM_address forms in this abbrevation declaration.
185  uint8_t NumAddrs = 0;
186  /// Number of DW_FORM_ref_addr forms in this abbrevation declaration.
187  uint8_t NumRefAddrs = 0;
188  /// Number of 4 byte in DWARF32 and 8 byte in DWARF64 forms.
189  uint8_t NumDwarfOffsets = 0;
190 
191  FixedSizeInfo() = default;
192 
193  /// Calculate the fixed size in bytes given a DWARFUnit.
194  ///
195  /// \param U the DWARFUnit to use when determing the byte size.
196  /// \returns the size in bytes for all attribute data in this abbreviation.
197  /// The returned size does not include bytes for the ULEB128 abbreviation
198  /// code
199  size_t getByteSize(const DWARFUnit &U) const;
200  };
201 
202  uint32_t Code;
203  dwarf::Tag Tag;
204  uint8_t CodeByteSize;
205  bool HasChildren;
206  AttributeSpecVector AttributeSpecs;
207  /// If this abbreviation has a fixed byte size then FixedAttributeSize member
208  /// variable below will have a value.
209  Optional<FixedSizeInfo> FixedAttributeSize;
210 };
211 
212 } // end namespace llvm
213 
214 #endif // LLVM_DEBUGINFO_DWARF_DWARFABBREVIATIONDECLARATION_H
llvm::DWARFAbbreviationDeclaration::getTag
dwarf::Tag getTag() const
Definition: DWARFAbbreviationDeclaration.h:90
llvm::DWARFAbbreviationDeclaration::getFixedAttributesByteSize
Optional< size_t > getFixedAttributesByteSize(const DWARFUnit &U) const
Definition: DWARFAbbreviationDeclaration.cpp:226
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Optional.h
llvm::DWARFAbbreviationDeclaration::dump
void dump(raw_ostream &OS) const
Definition: DWARFAbbreviationDeclaration.cpp:128
llvm::DWARFAbbreviationDeclaration::getFormByIndex
dwarf::Form getFormByIndex(uint32_t idx) const
Definition: DWARFAbbreviationDeclaration.h:100
llvm::DWARFAbbreviationDeclaration::AttributeSpec
Definition: DWARFAbbreviationDeclaration.h:29
llvm::SmallVector< AttributeSpec, 8 >
llvm::DWARFAbbreviationDeclaration::getCodeByteSize
uint8_t getCodeByteSize() const
Definition: DWARFAbbreviationDeclaration.h:89
llvm::dwarf::Form
Form
Definition: Dwarf.h:131
llvm::DWARFAbbreviationDeclaration::getAttrByIndex
dwarf::Attribute getAttrByIndex(uint32_t idx) const
Definition: DWARFAbbreviationDeclaration.h:109
llvm::DWARFAbbreviationDeclaration::AttributeSpec::getByteSize
Optional< int64_t > getByteSize(const DWARFUnit &U) const
Get the fixed byte size of this Form if possible.
Definition: DWARFAbbreviationDeclaration.cpp:213
llvm::Optional< uint8_t >
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::DWARFAbbreviationDeclaration::hasChildren
bool hasChildren() const
Definition: DWARFAbbreviationDeclaration.h:91
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:104
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::dwarf::Attribute
Attribute
Attributes.
Definition: Dwarf.h:124
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::DWARFAbbreviationDeclaration::getAttributeValue
Optional< DWARFFormValue > getAttributeValue(const uint64_t DIEOffset, const dwarf::Attribute Attr, const DWARFUnit &U) const
Extract a DWARF form value from a DIE specified by DIE offset.
Definition: DWARFAbbreviationDeclaration.cpp:187
llvm::DWARFAbbreviationDeclaration::AttributeSpec::AttributeSpec
AttributeSpec(dwarf::Attribute A, dwarf::Form F, int64_t Value)
Definition: DWARFAbbreviationDeclaration.h:30
llvm::DWARFAbbreviationDeclaration::AttributeSpec::Value
int64_t Value
Definition: DWARFAbbreviationDeclaration.h:65
llvm::DWARFAbbreviationDeclaration::AttributeSpec::Attr
dwarf::Attribute Attr
Definition: DWARFAbbreviationDeclaration.h:42
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::DWARFAbbreviationDeclaration::extract
bool extract(DataExtractor Data, uint64_t *OffsetPtr)
Definition: DWARFAbbreviationDeclaration.cpp:40
llvm::DWARFAbbreviationDeclaration::AttributeSpecVector
SmallVector< AttributeSpec, 8 > AttributeSpecVector
Definition: DWARFAbbreviationDeclaration.h:84
uint64_t
llvm::DWARFAbbreviationDeclaration::AttributeSpec::Form
dwarf::Form Form
Definition: DWARFAbbreviationDeclaration.h:43
llvm::DWARFAbbreviationDeclaration::findAttributeIndex
Optional< uint32_t > findAttributeIndex(dwarf::Attribute attr) const
Get the index of the specified attribute.
Definition: DWARFAbbreviationDeclaration.cpp:142
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator_range.h
llvm::DWARFAbbreviationDeclaration::AttributeSpec::getImplicitConstValue
int64_t getImplicitConstValue() const
Definition: DWARFAbbreviationDeclaration.h:73
llvm::DWARFAbbreviationDeclaration::attr_iterator_range
iterator_range< AttributeSpecVector::const_iterator > attr_iterator_range
Definition: DWARFAbbreviationDeclaration.h:94
llvm::DWARFAbbreviationDeclaration::getNumAttributes
size_t getNumAttributes() const
Definition: DWARFAbbreviationDeclaration.h:105
llvm::DWARFAbbreviationDeclaration::getAttrIsImplicitConstByIndex
bool getAttrIsImplicitConstByIndex(uint32_t idx) const
Definition: DWARFAbbreviationDeclaration.h:114
Dwarf.h
llvm::DWARFAbbreviationDeclaration::AttributeSpec::ByteSize
ByteSizeStorage ByteSize
Definition: DWARFAbbreviationDeclaration.h:64
uint32_t
llvm::DWARFAbbreviationDeclaration::AttributeSpec::isImplicitConst
bool isImplicitConst() const
Definition: DWARFAbbreviationDeclaration.h:69
uint16_t
llvm::DWARFAbbreviationDeclaration
Definition: DWARFAbbreviationDeclaration.h:27
llvm::DWARFUnit
Definition: DWARFUnit.h:203
DataExtractor.h
llvm::DWARFAbbreviationDeclaration::AttributeSpec::AttributeSpec
AttributeSpec(dwarf::Attribute A, dwarf::Form F, Optional< uint8_t > ByteSize)
Definition: DWARFAbbreviationDeclaration.h:34
llvm::DWARFAbbreviationDeclaration::getCode
uint32_t getCode() const
Definition: DWARFAbbreviationDeclaration.h:88
llvm::DWARFAbbreviationDeclaration::getAttrImplicitConstValueByIndex
int64_t getAttrImplicitConstValueByIndex(uint32_t idx) const
Definition: DWARFAbbreviationDeclaration.h:119
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration
DWARFAbbreviationDeclaration()
Definition: DWARFAbbreviationDeclaration.cpp:35
SmallVector.h
llvm::DWARFAbbreviationDeclaration::getAttributeOffsetFromIndex
uint64_t getAttributeOffsetFromIndex(uint32_t AttrIndex, uint64_t DIEOffset, const DWARFUnit &U) const
Compute an offset from a DIE specified by DIE offset and attribute index.
Definition: DWARFAbbreviationDeclaration.cpp:150
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::DWARFAbbreviationDeclaration::getAttributeValueFromOffset
Optional< DWARFFormValue > getAttributeValueFromOffset(uint32_t AttrIndex, uint64_t Offset, const DWARFUnit &U) const
Extract a DWARF form value from a DIE speccified by attribute index and its offset.
Definition: DWARFAbbreviationDeclaration.cpp:168
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::DWARFAbbreviationDeclaration::attributes
attr_iterator_range attributes() const
Definition: DWARFAbbreviationDeclaration.h:96