blob: bb475a669efb21bf297e9179137b9705ccecbfb0 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFAbbreviationDeclaration.cpp -----------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000011
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000012#include "llvm/ADT/None.h"
13#include "llvm/ADT/Optional.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Dwarf.h"
Greg Clayton6f6e4db2016-11-15 01:23:06 +000015#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
16#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000017#include "llvm/Support/DataExtractor.h"
Benjamin Kramer21a50922011-09-15 04:15:59 +000018#include "llvm/Support/Format.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000019#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000020#include <cstddef>
21#include <cstdint>
22
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000023using namespace llvm;
24using namespace dwarf;
25
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000026void DWARFAbbreviationDeclaration::clear() {
27 Code = 0;
Greg Clayton6c273762016-10-27 16:32:04 +000028 Tag = DW_TAG_null;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000029 CodeByteSize = 0;
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000030 HasChildren = false;
Alexey Samsonov1eabf982014-03-13 07:52:54 +000031 AttributeSpecs.clear();
Greg Clayton6f6e4db2016-11-15 01:23:06 +000032 FixedAttributeSize.reset();
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000033}
34
35DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
36 clear();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000037}
38
39bool
Greg Clayton6c273762016-10-27 16:32:04 +000040DWARFAbbreviationDeclaration::extract(DataExtractor Data,
41 uint32_t* OffsetPtr) {
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000042 clear();
Greg Clayton6f6e4db2016-11-15 01:23:06 +000043 const uint32_t Offset = *OffsetPtr;
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000044 Code = Data.getULEB128(OffsetPtr);
45 if (Code == 0) {
46 return false;
47 }
Greg Clayton6f6e4db2016-11-15 01:23:06 +000048 CodeByteSize = *OffsetPtr - Offset;
Greg Clayton6c273762016-10-27 16:32:04 +000049 Tag = static_cast<llvm::dwarf::Tag>(Data.getULEB128(OffsetPtr));
50 if (Tag == DW_TAG_null) {
51 clear();
52 return false;
53 }
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000054 uint8_t ChildrenByte = Data.getU8(OffsetPtr);
55 HasChildren = (ChildrenByte == DW_CHILDREN_yes);
Greg Clayton6f6e4db2016-11-15 01:23:06 +000056 // Assign a value to our optional FixedAttributeSize member variable. If
57 // this member variable still has a value after the while loop below, then
58 // all attribute data in this abbreviation declaration has a fixed byte size.
59 FixedAttributeSize = FixedSizeInfo();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000060
Greg Clayton6f6e4db2016-11-15 01:23:06 +000061 // Read all of the abbreviation attributes and forms.
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000062 while (true) {
Greg Clayton6c273762016-10-27 16:32:04 +000063 auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr));
64 auto F = static_cast<Form>(Data.getULEB128(OffsetPtr));
65 if (A && F) {
Victor Leschukcbddae72017-01-10 21:18:26 +000066 Optional<int64_t> V;
67 bool IsImplicitConst = (F == DW_FORM_implicit_const);
Paul Robinson75c068c2017-06-26 18:43:01 +000068 if (IsImplicitConst) {
Victor Leschukcbddae72017-01-10 21:18:26 +000069 V = Data.getSLEB128(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +000070 AttributeSpecs.push_back(AttributeSpec(A, F, V));
Victor Leschukcbddae72017-01-10 21:18:26 +000071 continue;
Paul Robinson75c068c2017-06-26 18:43:01 +000072 }
Greg Clayton6f6e4db2016-11-15 01:23:06 +000073 // If this abbrevation still has a fixed byte size, then update the
74 // FixedAttributeSize as needed.
Paul Robinson75c068c2017-06-26 18:43:01 +000075 switch (F) {
76 case DW_FORM_addr:
77 if (FixedAttributeSize)
78 ++FixedAttributeSize->NumAddrs;
79 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000080
Paul Robinson75c068c2017-06-26 18:43:01 +000081 case DW_FORM_ref_addr:
82 if (FixedAttributeSize)
83 ++FixedAttributeSize->NumRefAddrs;
84 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000085
Paul Robinson75c068c2017-06-26 18:43:01 +000086 case DW_FORM_strp:
87 case DW_FORM_GNU_ref_alt:
88 case DW_FORM_GNU_strp_alt:
89 case DW_FORM_line_strp:
90 case DW_FORM_sec_offset:
91 case DW_FORM_strp_sup:
92 if (FixedAttributeSize)
93 ++FixedAttributeSize->NumDwarfOffsets;
94 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000095
Paul Robinson75c068c2017-06-26 18:43:01 +000096 default:
97 // The form has a byte size that doesn't depend on Params.
98 // If it's a fixed size, keep track of it.
99 if (auto Size =
100 DWARFFormValue::getFixedByteSize(F, DWARFFormParams())) {
101 V = *Size;
102 if (FixedAttributeSize)
103 FixedAttributeSize->NumBytes += *V;
104 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000105 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000106 // Indicate we no longer have a fixed byte size for this
107 // abbreviation by clearing the FixedAttributeSize optional value
108 // so it doesn't have a value.
109 FixedAttributeSize.reset();
110 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000111 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000112 // Record this attribute and its fixed size if it has one.
113 AttributeSpecs.push_back(AttributeSpec(A, F, V));
Greg Clayton6c273762016-10-27 16:32:04 +0000114 } else if (A == 0 && F == 0) {
115 // We successfully reached the end of this abbreviation declaration
116 // since both attribute and form are zero.
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +0000117 break;
Greg Clayton6c273762016-10-27 16:32:04 +0000118 } else {
119 // Attribute and form pairs must either both be non-zero, in which case
120 // they are added to the abbreviation declaration, or both be zero to
121 // terminate the abbrevation declaration. In this case only one was
122 // zero which is an error.
123 clear();
124 return false;
125 }
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +0000126 }
127 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000128}
129
130void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000131 auto tagString = TagString(getTag());
Benjamin Kramer21a50922011-09-15 04:15:59 +0000132 OS << '[' << getCode() << "] ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000133 if (!tagString.empty())
Benjamin Kramer21a50922011-09-15 04:15:59 +0000134 OS << tagString;
135 else
136 OS << format("DW_TAG_Unknown_%x", getTag());
137 OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000138 for (const AttributeSpec &Spec : AttributeSpecs) {
Benjamin Kramer21a50922011-09-15 04:15:59 +0000139 OS << '\t';
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000140 auto attrString = AttributeString(Spec.Attr);
141 if (!attrString.empty())
Benjamin Kramer21a50922011-09-15 04:15:59 +0000142 OS << attrString;
143 else
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000144 OS << format("DW_AT_Unknown_%x", Spec.Attr);
Benjamin Kramer21a50922011-09-15 04:15:59 +0000145 OS << '\t';
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000146 auto formString = FormEncodingString(Spec.Form);
147 if (!formString.empty())
Benjamin Kramer21a50922011-09-15 04:15:59 +0000148 OS << formString;
149 else
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000150 OS << format("DW_FORM_Unknown_%x", Spec.Form);
Victor Leschukcbddae72017-01-10 21:18:26 +0000151 if (Spec.isImplicitConst())
152 OS << '\t' << *Spec.ByteSizeOrValue;
Benjamin Kramer21a50922011-09-15 04:15:59 +0000153 OS << '\n';
Benjamin Kramer4137b6a2011-09-15 04:00:58 +0000154 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000155 OS << '\n';
156}
157
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000158Optional<uint32_t>
159DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute Attr) const {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000160 for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000161 if (AttributeSpecs[i].Attr == Attr)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000162 return i;
163 }
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000164 return None;
165}
166
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000167Optional<DWARFFormValue> DWARFAbbreviationDeclaration::getAttributeValue(
168 const uint32_t DIEOffset, const dwarf::Attribute Attr,
169 const DWARFUnit &U) const {
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000170 Optional<uint32_t> MatchAttrIndex = findAttributeIndex(Attr);
171 if (!MatchAttrIndex)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000172 return None;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000173
174 auto DebugInfoData = U.getDebugInfoExtractor();
175
176 // Add the byte size of ULEB that for the abbrev Code so we can start
177 // skipping the attribute data.
178 uint32_t Offset = DIEOffset + CodeByteSize;
179 uint32_t AttrIndex = 0;
180 for (const auto &Spec : AttributeSpecs) {
181 if (*MatchAttrIndex == AttrIndex) {
182 // We have arrived at the attribute to extract, extract if from Offset.
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000183 DWARFFormValue FormValue(Spec.Form);
Victor Leschukcbddae72017-01-10 21:18:26 +0000184 if (Spec.isImplicitConst()) {
185 FormValue.setSValue(*Spec.ByteSizeOrValue);
186 return FormValue;
187 }
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000188 if (FormValue.extractValue(DebugInfoData, &Offset, &U))
189 return FormValue;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000190 }
191 // March Offset along until we get to the attribute we want.
Victor Leschukcbddae72017-01-10 21:18:26 +0000192 if (auto FixedSize = Spec.getByteSize(U))
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000193 Offset += *FixedSize;
194 else
Paul Robinson75c068c2017-06-26 18:43:01 +0000195 DWARFFormValue::skipValue(Spec.Form, DebugInfoData, &Offset,
196 U.getFormParams());
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000197 ++AttrIndex;
198 }
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000199 return None;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000200}
201
202size_t DWARFAbbreviationDeclaration::FixedSizeInfo::getByteSize(
203 const DWARFUnit &U) const {
204 size_t ByteSize = NumBytes;
205 if (NumAddrs)
206 ByteSize += NumAddrs * U.getAddressByteSize();
207 if (NumRefAddrs)
208 ByteSize += NumRefAddrs * U.getRefAddrByteSize();
209 if (NumDwarfOffsets)
210 ByteSize += NumDwarfOffsets * U.getDwarfOffsetByteSize();
211 return ByteSize;
212}
213
Victor Leschukcbddae72017-01-10 21:18:26 +0000214Optional<int64_t> DWARFAbbreviationDeclaration::AttributeSpec::getByteSize(
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000215 const DWARFUnit &U) const {
Victor Leschukcbddae72017-01-10 21:18:26 +0000216 if (isImplicitConst())
217 return 0;
218 if (ByteSizeOrValue)
219 return ByteSizeOrValue;
220 Optional<int64_t> S;
Paul Robinson75c068c2017-06-26 18:43:01 +0000221 auto FixedByteSize =
222 DWARFFormValue::getFixedByteSize(Form, U.getFormParams());
Victor Leschukcbddae72017-01-10 21:18:26 +0000223 if (FixedByteSize)
224 S = *FixedByteSize;
225 return S;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000226}
227
228Optional<size_t> DWARFAbbreviationDeclaration::getFixedAttributesByteSize(
229 const DWARFUnit &U) const {
230 if (FixedAttributeSize)
231 return FixedAttributeSize->getByteSize(U);
232 return None;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000233}