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