1 //===-- DWARFAbbreviationDeclaration.cpp ----------------------------------===//
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 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
11 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
12 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
13 #include "llvm/Support/Dwarf.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
17 using namespace dwarf;
19 void DWARFAbbreviationDeclaration::clear() {
24 AttributeSpecs.clear();
25 FixedAttributeSize.reset();
28 DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
33 DWARFAbbreviationDeclaration::extract(DataExtractor Data,
34 uint32_t* OffsetPtr) {
36 const uint32_t Offset = *OffsetPtr;
37 Code = Data.getULEB128(OffsetPtr);
41 CodeByteSize = *OffsetPtr - Offset;
42 Tag = static_cast<llvm::dwarf::Tag>(Data.getULEB128(OffsetPtr));
43 if (Tag == DW_TAG_null) {
47 uint8_t ChildrenByte = Data.getU8(OffsetPtr);
48 HasChildren = (ChildrenByte == DW_CHILDREN_yes);
49 // Assign a value to our optional FixedAttributeSize member variable. If
50 // this member variable still has a value after the while loop below, then
51 // all attribute data in this abbreviation declaration has a fixed byte size.
52 FixedAttributeSize = FixedSizeInfo();
54 // Read all of the abbreviation attributes and forms.
56 auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr));
57 auto F = static_cast<Form>(Data.getULEB128(OffsetPtr));
60 bool IsImplicitConst = (F == DW_FORM_implicit_const);
62 V = Data.getSLEB128(OffsetPtr);
63 else if (auto Size = DWARFFormValue::getFixedByteSize(F))
65 AttributeSpecs.push_back(AttributeSpec(A, F, V));
68 // If this abbrevation still has a fixed byte size, then update the
69 // FixedAttributeSize as needed.
70 if (FixedAttributeSize) {
72 FixedAttributeSize->NumBytes += *V;
76 ++FixedAttributeSize->NumAddrs;
79 case DW_FORM_ref_addr:
80 ++FixedAttributeSize->NumRefAddrs;
84 case DW_FORM_GNU_ref_alt:
85 case DW_FORM_GNU_strp_alt:
86 case DW_FORM_line_strp:
87 case DW_FORM_sec_offset:
88 case DW_FORM_strp_sup:
90 ++FixedAttributeSize->NumDwarfOffsets;
94 // Indicate we no longer have a fixed byte size for this
95 // abbreviation by clearing the FixedAttributeSize optional value
96 // so it doesn't have a value.
97 FixedAttributeSize.reset();
102 } else if (A == 0 && F == 0) {
103 // We successfully reached the end of this abbreviation declaration
104 // since both attribute and form are zero.
107 // Attribute and form pairs must either both be non-zero, in which case
108 // they are added to the abbreviation declaration, or both be zero to
109 // terminate the abbrevation declaration. In this case only one was
110 // zero which is an error.
118 void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
119 auto tagString = TagString(getTag());
120 OS << '[' << getCode() << "] ";
121 if (!tagString.empty())
124 OS << format("DW_TAG_Unknown_%x", getTag());
125 OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
126 for (const AttributeSpec &Spec : AttributeSpecs) {
128 auto attrString = AttributeString(Spec.Attr);
129 if (!attrString.empty())
132 OS << format("DW_AT_Unknown_%x", Spec.Attr);
134 auto formString = FormEncodingString(Spec.Form);
135 if (!formString.empty())
138 OS << format("DW_FORM_Unknown_%x", Spec.Form);
139 if (Spec.isImplicitConst())
140 OS << '\t' << *Spec.ByteSizeOrValue;
147 DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute Attr) const {
148 for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
149 if (AttributeSpecs[i].Attr == Attr)
155 Optional<DWARFFormValue> DWARFAbbreviationDeclaration::getAttributeValue(
156 const uint32_t DIEOffset, const dwarf::Attribute Attr,
157 const DWARFUnit &U) const {
158 Optional<uint32_t> MatchAttrIndex = findAttributeIndex(Attr);
162 auto DebugInfoData = U.getDebugInfoExtractor();
164 // Add the byte size of ULEB that for the abbrev Code so we can start
165 // skipping the attribute data.
166 uint32_t Offset = DIEOffset + CodeByteSize;
167 uint32_t AttrIndex = 0;
168 for (const auto &Spec : AttributeSpecs) {
169 if (*MatchAttrIndex == AttrIndex) {
170 // We have arrived at the attribute to extract, extract if from Offset.
171 DWARFFormValue FormValue(Spec.Form);
172 if (Spec.isImplicitConst()) {
173 FormValue.setSValue(*Spec.ByteSizeOrValue);
176 if (FormValue.extractValue(DebugInfoData, &Offset, &U))
179 // March Offset along until we get to the attribute we want.
180 if (auto FixedSize = Spec.getByteSize(U))
181 Offset += *FixedSize;
183 DWARFFormValue::skipValue(Spec.Form, DebugInfoData, &Offset, &U);
189 size_t DWARFAbbreviationDeclaration::FixedSizeInfo::getByteSize(
190 const DWARFUnit &U) const {
191 size_t ByteSize = NumBytes;
193 ByteSize += NumAddrs * U.getAddressByteSize();
195 ByteSize += NumRefAddrs * U.getRefAddrByteSize();
197 ByteSize += NumDwarfOffsets * U.getDwarfOffsetByteSize();
201 Optional<int64_t> DWARFAbbreviationDeclaration::AttributeSpec::getByteSize(
202 const DWARFUnit &U) const {
203 if (isImplicitConst())
206 return ByteSizeOrValue;
208 auto FixedByteSize = DWARFFormValue::getFixedByteSize(Form, &U);
214 Optional<size_t> DWARFAbbreviationDeclaration::getFixedAttributesByteSize(
215 const DWARFUnit &U) const {
216 if (FixedAttributeSize)
217 return FixedAttributeSize->getByteSize(U);