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