blob: f49ab40fad9a9493d76a6831a20922d677604c24 [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"
Pavel Labath9025f952018-03-21 11:46:37 +000019#include "llvm/Support/FormatVariadic.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000020#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000021#include <cstddef>
22#include <cstdint>
23
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000024using namespace llvm;
25using namespace dwarf;
26
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000027void DWARFAbbreviationDeclaration::clear() {
28 Code = 0;
Greg Clayton6c273762016-10-27 16:32:04 +000029 Tag = DW_TAG_null;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000030 CodeByteSize = 0;
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000031 HasChildren = false;
Alexey Samsonov1eabf982014-03-13 07:52:54 +000032 AttributeSpecs.clear();
Greg Clayton6f6e4db2016-11-15 01:23:06 +000033 FixedAttributeSize.reset();
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000034}
35
36DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
37 clear();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000038}
39
40bool
Fangrui Songf78650a2018-07-30 19:41:25 +000041DWARFAbbreviationDeclaration::extract(DataExtractor Data,
Greg Clayton6c273762016-10-27 16:32:04 +000042 uint32_t* OffsetPtr) {
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000043 clear();
Greg Clayton6f6e4db2016-11-15 01:23:06 +000044 const uint32_t Offset = *OffsetPtr;
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000045 Code = Data.getULEB128(OffsetPtr);
46 if (Code == 0) {
47 return false;
48 }
Greg Clayton6f6e4db2016-11-15 01:23:06 +000049 CodeByteSize = *OffsetPtr - Offset;
Greg Clayton6c273762016-10-27 16:32:04 +000050 Tag = static_cast<llvm::dwarf::Tag>(Data.getULEB128(OffsetPtr));
51 if (Tag == DW_TAG_null) {
52 clear();
53 return false;
54 }
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000055 uint8_t ChildrenByte = Data.getU8(OffsetPtr);
56 HasChildren = (ChildrenByte == DW_CHILDREN_yes);
Greg Clayton6f6e4db2016-11-15 01:23:06 +000057 // Assign a value to our optional FixedAttributeSize member variable. If
58 // this member variable still has a value after the while loop below, then
59 // all attribute data in this abbreviation declaration has a fixed byte size.
60 FixedAttributeSize = FixedSizeInfo();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000061
Greg Clayton6f6e4db2016-11-15 01:23:06 +000062 // Read all of the abbreviation attributes and forms.
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +000063 while (true) {
Greg Clayton6c273762016-10-27 16:32:04 +000064 auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr));
65 auto F = static_cast<Form>(Data.getULEB128(OffsetPtr));
66 if (A && F) {
Victor Leschukcbddae72017-01-10 21:18:26 +000067 bool IsImplicitConst = (F == DW_FORM_implicit_const);
Paul Robinson75c068c2017-06-26 18:43:01 +000068 if (IsImplicitConst) {
Benjamin Kramereb14c112017-09-21 15:27:45 +000069 int64_t 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 }
Benjamin Kramereb14c112017-09-21 15:27:45 +000073 Optional<uint8_t> ByteSize;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000074 // If this abbrevation still has a fixed byte size, then update the
75 // FixedAttributeSize as needed.
Paul Robinson75c068c2017-06-26 18:43:01 +000076 switch (F) {
77 case DW_FORM_addr:
78 if (FixedAttributeSize)
79 ++FixedAttributeSize->NumAddrs;
80 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000081
Paul Robinson75c068c2017-06-26 18:43:01 +000082 case DW_FORM_ref_addr:
83 if (FixedAttributeSize)
84 ++FixedAttributeSize->NumRefAddrs;
85 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000086
Paul Robinson75c068c2017-06-26 18:43:01 +000087 case DW_FORM_strp:
88 case DW_FORM_GNU_ref_alt:
89 case DW_FORM_GNU_strp_alt:
90 case DW_FORM_line_strp:
91 case DW_FORM_sec_offset:
92 case DW_FORM_strp_sup:
93 if (FixedAttributeSize)
94 ++FixedAttributeSize->NumDwarfOffsets;
95 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +000096
Paul Robinson75c068c2017-06-26 18:43:01 +000097 default:
98 // The form has a byte size that doesn't depend on Params.
99 // If it's a fixed size, keep track of it.
Pavel Labath322711f2018-03-14 09:39:54 +0000100 if ((ByteSize = dwarf::getFixedFormByteSize(F, dwarf::FormParams()))) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000101 if (FixedAttributeSize)
Benjamin Kramereb14c112017-09-21 15:27:45 +0000102 FixedAttributeSize->NumBytes += *ByteSize;
Paul Robinson75c068c2017-06-26 18:43:01 +0000103 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000104 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000105 // Indicate we no longer have a fixed byte size for this
106 // abbreviation by clearing the FixedAttributeSize optional value
107 // so it doesn't have a value.
108 FixedAttributeSize.reset();
109 break;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000110 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000111 // Record this attribute and its fixed size if it has one.
Benjamin Kramereb14c112017-09-21 15:27:45 +0000112 AttributeSpecs.push_back(AttributeSpec(A, F, ByteSize));
Greg Clayton6c273762016-10-27 16:32:04 +0000113 } else if (A == 0 && F == 0) {
114 // We successfully reached the end of this abbreviation declaration
115 // since both attribute and form are zero.
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +0000116 break;
Greg Clayton6c273762016-10-27 16:32:04 +0000117 } else {
118 // Attribute and form pairs must either both be non-zero, in which case
119 // they are added to the abbreviation declaration, or both be zero to
120 // terminate the abbrevation declaration. In this case only one was
121 // zero which is an error.
122 clear();
123 return false;
124 }
Alexey Samsonovd5cc93c2013-10-31 17:20:14 +0000125 }
126 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000127}
128
129void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
Benjamin Kramer21a50922011-09-15 04:15:59 +0000130 OS << '[' << getCode() << "] ";
Pavel Labath9025f952018-03-21 11:46:37 +0000131 OS << formatv("{0}", getTag());
Benjamin Kramer21a50922011-09-15 04:15:59 +0000132 OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000133 for (const AttributeSpec &Spec : AttributeSpecs) {
Pavel Labath9025f952018-03-21 11:46:37 +0000134 OS << formatv("\t{0}\t{1}", Spec.Attr, Spec.Form);
Victor Leschukcbddae72017-01-10 21:18:26 +0000135 if (Spec.isImplicitConst())
Benjamin Kramereb14c112017-09-21 15:27:45 +0000136 OS << '\t' << Spec.getImplicitConstValue();
Benjamin Kramer21a50922011-09-15 04:15:59 +0000137 OS << '\n';
Benjamin Kramer4137b6a2011-09-15 04:00:58 +0000138 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000139 OS << '\n';
140}
141
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000142Optional<uint32_t>
143DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute Attr) const {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000144 for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000145 if (AttributeSpecs[i].Attr == Attr)
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000146 return i;
147 }
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000148 return None;
149}
150
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000151Optional<DWARFFormValue> DWARFAbbreviationDeclaration::getAttributeValue(
152 const uint32_t DIEOffset, const dwarf::Attribute Attr,
153 const DWARFUnit &U) const {
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000154 Optional<uint32_t> MatchAttrIndex = findAttributeIndex(Attr);
155 if (!MatchAttrIndex)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000156 return None;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000157
158 auto DebugInfoData = U.getDebugInfoExtractor();
159
160 // Add the byte size of ULEB that for the abbrev Code so we can start
161 // skipping the attribute data.
162 uint32_t Offset = DIEOffset + CodeByteSize;
163 uint32_t AttrIndex = 0;
164 for (const auto &Spec : AttributeSpecs) {
165 if (*MatchAttrIndex == AttrIndex) {
166 // We have arrived at the attribute to extract, extract if from Offset.
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000167 DWARFFormValue FormValue(Spec.Form);
Victor Leschukcbddae72017-01-10 21:18:26 +0000168 if (Spec.isImplicitConst()) {
Benjamin Kramereb14c112017-09-21 15:27:45 +0000169 FormValue.setSValue(Spec.getImplicitConstValue());
Victor Leschukcbddae72017-01-10 21:18:26 +0000170 return FormValue;
171 }
Paul Robinsone5400f82017-11-07 19:57:12 +0000172 if (FormValue.extractValue(DebugInfoData, &Offset, U.getFormParams(), &U))
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000173 return FormValue;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000174 }
175 // March Offset along until we get to the attribute we want.
Victor Leschukcbddae72017-01-10 21:18:26 +0000176 if (auto FixedSize = Spec.getByteSize(U))
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000177 Offset += *FixedSize;
178 else
Paul Robinson75c068c2017-06-26 18:43:01 +0000179 DWARFFormValue::skipValue(Spec.Form, DebugInfoData, &Offset,
180 U.getFormParams());
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000181 ++AttrIndex;
182 }
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000183 return None;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000184}
185
186size_t DWARFAbbreviationDeclaration::FixedSizeInfo::getByteSize(
187 const DWARFUnit &U) const {
188 size_t ByteSize = NumBytes;
189 if (NumAddrs)
190 ByteSize += NumAddrs * U.getAddressByteSize();
191 if (NumRefAddrs)
192 ByteSize += NumRefAddrs * U.getRefAddrByteSize();
193 if (NumDwarfOffsets)
194 ByteSize += NumDwarfOffsets * U.getDwarfOffsetByteSize();
195 return ByteSize;
196}
197
Victor Leschukcbddae72017-01-10 21:18:26 +0000198Optional<int64_t> DWARFAbbreviationDeclaration::AttributeSpec::getByteSize(
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000199 const DWARFUnit &U) const {
Victor Leschukcbddae72017-01-10 21:18:26 +0000200 if (isImplicitConst())
201 return 0;
Benjamin Kramer0fad6dd2017-10-31 19:55:08 +0000202 if (ByteSize.HasByteSize)
203 return ByteSize.ByteSize;
Victor Leschukcbddae72017-01-10 21:18:26 +0000204 Optional<int64_t> S;
Pavel Labath322711f2018-03-14 09:39:54 +0000205 auto FixedByteSize = dwarf::getFixedFormByteSize(Form, U.getFormParams());
Victor Leschukcbddae72017-01-10 21:18:26 +0000206 if (FixedByteSize)
207 S = *FixedByteSize;
208 return S;
Greg Clayton6f6e4db2016-11-15 01:23:06 +0000209}
210
211Optional<size_t> DWARFAbbreviationDeclaration::getFixedAttributesByteSize(
212 const DWARFUnit &U) const {
213 if (FixedAttributeSize)
214 return FixedAttributeSize->getByteSize(U);
215 return None;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000216}