File indexing completed on 2026-05-10 08:43:40
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_DEBUGINFO_DWARF_DWARFABBREVIATIONDECLARATION_H
0010 #define LLVM_DEBUGINFO_DWARF_DWARFABBREVIATIONDECLARATION_H
0011
0012 #include "llvm/ADT/SmallVector.h"
0013 #include "llvm/ADT/iterator_range.h"
0014 #include "llvm/BinaryFormat/Dwarf.h"
0015 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
0016 #include <cassert>
0017 #include <cstddef>
0018 #include <cstdint>
0019
0020 namespace llvm {
0021
0022 class DataExtractor;
0023 class DWARFUnit;
0024 class raw_ostream;
0025
0026 class DWARFAbbreviationDeclaration {
0027 public:
0028 enum class ExtractState { Complete, MoreItems };
0029 struct AttributeSpec {
0030 AttributeSpec(dwarf::Attribute A, dwarf::Form F, int64_t Value)
0031 : Attr(A), Form(F), Value(Value) {
0032 assert(isImplicitConst());
0033 }
0034 AttributeSpec(dwarf::Attribute A, dwarf::Form F,
0035 std::optional<uint8_t> ByteSize)
0036 : Attr(A), Form(F) {
0037 assert(!isImplicitConst());
0038 this->ByteSize.HasByteSize = ByteSize.has_value();
0039 if (this->ByteSize.HasByteSize)
0040 this->ByteSize.ByteSize = *ByteSize;
0041 }
0042
0043 DWARFFormValue getFormValue() const {
0044 if (Form == dwarf::DW_FORM_implicit_const)
0045 return DWARFFormValue::createFromSValue(Form, getImplicitConstValue());
0046
0047 return DWARFFormValue(Form);
0048 }
0049
0050 dwarf::Attribute Attr;
0051 dwarf::Form Form;
0052
0053 private:
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 struct ByteSizeStorage {
0068 bool HasByteSize;
0069 uint8_t ByteSize;
0070 };
0071 union {
0072 ByteSizeStorage ByteSize;
0073 int64_t Value;
0074 };
0075
0076 public:
0077 bool isImplicitConst() const {
0078 return Form == dwarf::DW_FORM_implicit_const;
0079 }
0080
0081 int64_t getImplicitConstValue() const {
0082 assert(isImplicitConst());
0083 return Value;
0084 }
0085
0086
0087
0088
0089
0090 std::optional<int64_t> getByteSize(const DWARFUnit &U) const;
0091 };
0092 using AttributeSpecVector = SmallVector<AttributeSpec, 8>;
0093
0094 DWARFAbbreviationDeclaration();
0095
0096 uint32_t getCode() const { return Code; }
0097 uint8_t getCodeByteSize() const { return CodeByteSize; }
0098 dwarf::Tag getTag() const { return Tag; }
0099 bool hasChildren() const { return HasChildren; }
0100
0101 using attr_iterator_range =
0102 iterator_range<AttributeSpecVector::const_iterator>;
0103
0104 attr_iterator_range attributes() const {
0105 return attr_iterator_range(AttributeSpecs.begin(), AttributeSpecs.end());
0106 }
0107
0108 dwarf::Form getFormByIndex(uint32_t idx) const {
0109 assert(idx < AttributeSpecs.size());
0110 return AttributeSpecs[idx].Form;
0111 }
0112
0113 size_t getNumAttributes() const {
0114 return AttributeSpecs.size();
0115 }
0116
0117 dwarf::Attribute getAttrByIndex(uint32_t idx) const {
0118 assert(idx < AttributeSpecs.size());
0119 return AttributeSpecs[idx].Attr;
0120 }
0121
0122 bool getAttrIsImplicitConstByIndex(uint32_t idx) const {
0123 assert(idx < AttributeSpecs.size());
0124 return AttributeSpecs[idx].isImplicitConst();
0125 }
0126
0127 int64_t getAttrImplicitConstValueByIndex(uint32_t idx) const {
0128 assert(idx < AttributeSpecs.size());
0129 return AttributeSpecs[idx].getImplicitConstValue();
0130 }
0131
0132
0133
0134
0135
0136
0137
0138
0139 std::optional<uint32_t> findAttributeIndex(dwarf::Attribute attr) const;
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151 std::optional<DWARFFormValue> getAttributeValue(const uint64_t DIEOffset,
0152 const dwarf::Attribute Attr,
0153 const DWARFUnit &U) const;
0154
0155
0156
0157
0158
0159
0160
0161
0162 uint64_t getAttributeOffsetFromIndex(uint32_t AttrIndex, uint64_t DIEOffset,
0163 const DWARFUnit &U) const;
0164
0165
0166
0167
0168
0169
0170
0171
0172 std::optional<DWARFFormValue>
0173 getAttributeValueFromOffset(uint32_t AttrIndex, uint64_t Offset,
0174 const DWARFUnit &U) const;
0175
0176 llvm::Expected<ExtractState> extract(DataExtractor Data, uint64_t *OffsetPtr);
0177 void dump(raw_ostream &OS) const;
0178
0179
0180
0181
0182 std::optional<size_t> getFixedAttributesByteSize(const DWARFUnit &U) const;
0183
0184 private:
0185 void clear();
0186
0187
0188
0189 struct FixedSizeInfo {
0190
0191 uint16_t NumBytes = 0;
0192
0193 uint8_t NumAddrs = 0;
0194
0195 uint8_t NumRefAddrs = 0;
0196
0197 uint8_t NumDwarfOffsets = 0;
0198
0199 FixedSizeInfo() = default;
0200
0201
0202
0203
0204
0205
0206
0207 size_t getByteSize(const DWARFUnit &U) const;
0208 };
0209
0210 uint32_t Code;
0211 dwarf::Tag Tag;
0212 uint8_t CodeByteSize;
0213 bool HasChildren;
0214 AttributeSpecVector AttributeSpecs;
0215
0216
0217 std::optional<FixedSizeInfo> FixedAttributeSize;
0218 };
0219
0220 }
0221
0222 #endif