Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 1 | //===-- DWARFAbbreviationDeclaration.cpp ----------------------------------===// |
| 2 | // |
| 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 | |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 12 | #include "llvm/DebugInfo/DWARF/DWARFUnit.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Dwarf.h" |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Format.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | using namespace llvm; |
| 17 | using namespace dwarf; |
| 18 | |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 19 | void DWARFAbbreviationDeclaration::clear() { |
| 20 | Code = 0; |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 21 | Tag = DW_TAG_null; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 22 | CodeByteSize = 0; |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 23 | HasChildren = false; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 24 | AttributeSpecs.clear(); |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 25 | FixedAttributeSize.reset(); |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() { |
| 29 | clear(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | bool |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 33 | DWARFAbbreviationDeclaration::extract(DataExtractor Data, |
| 34 | uint32_t* OffsetPtr) { |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 35 | clear(); |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 36 | const uint32_t Offset = *OffsetPtr; |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 37 | Code = Data.getULEB128(OffsetPtr); |
| 38 | if (Code == 0) { |
| 39 | return false; |
| 40 | } |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 41 | CodeByteSize = *OffsetPtr - Offset; |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 42 | Tag = static_cast<llvm::dwarf::Tag>(Data.getULEB128(OffsetPtr)); |
| 43 | if (Tag == DW_TAG_null) { |
| 44 | clear(); |
| 45 | return false; |
| 46 | } |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 47 | uint8_t ChildrenByte = Data.getU8(OffsetPtr); |
| 48 | HasChildren = (ChildrenByte == DW_CHILDREN_yes); |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 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(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 53 | |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 54 | // Read all of the abbreviation attributes and forms. |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 55 | while (true) { |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 56 | auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr)); |
| 57 | auto F = static_cast<Form>(Data.getULEB128(OffsetPtr)); |
| 58 | if (A && F) { |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame^] | 59 | Optional<int64_t> V; |
| 60 | bool IsImplicitConst = (F == DW_FORM_implicit_const); |
| 61 | if (IsImplicitConst) |
| 62 | V = Data.getSLEB128(OffsetPtr); |
| 63 | else if (auto Size = DWARFFormValue::getFixedByteSize(F)) |
| 64 | V = *Size; |
| 65 | AttributeSpecs.push_back(AttributeSpec(A, F, V)); |
| 66 | if (IsImplicitConst) |
| 67 | continue; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 68 | // If this abbrevation still has a fixed byte size, then update the |
| 69 | // FixedAttributeSize as needed. |
| 70 | if (FixedAttributeSize) { |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame^] | 71 | if (V) |
| 72 | FixedAttributeSize->NumBytes += *V; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 73 | else { |
| 74 | switch (F) { |
| 75 | case DW_FORM_addr: |
| 76 | ++FixedAttributeSize->NumAddrs; |
| 77 | break; |
| 78 | |
| 79 | case DW_FORM_ref_addr: |
| 80 | ++FixedAttributeSize->NumRefAddrs; |
| 81 | break; |
| 82 | |
| 83 | case DW_FORM_strp: |
| 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: |
| 89 | case DW_FORM_ref_sup: |
| 90 | ++FixedAttributeSize->NumDwarfOffsets; |
| 91 | break; |
| 92 | |
| 93 | default: |
| 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(); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 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. |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 105 | break; |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 106 | } else { |
| 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. |
| 111 | clear(); |
| 112 | return false; |
| 113 | } |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 114 | } |
| 115 | return true; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const { |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 119 | auto tagString = TagString(getTag()); |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 120 | OS << '[' << getCode() << "] "; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 121 | if (!tagString.empty()) |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 122 | OS << tagString; |
| 123 | else |
| 124 | OS << format("DW_TAG_Unknown_%x", getTag()); |
| 125 | OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n'; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 126 | for (const AttributeSpec &Spec : AttributeSpecs) { |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 127 | OS << '\t'; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 128 | auto attrString = AttributeString(Spec.Attr); |
| 129 | if (!attrString.empty()) |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 130 | OS << attrString; |
| 131 | else |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 132 | OS << format("DW_AT_Unknown_%x", Spec.Attr); |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 133 | OS << '\t'; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 134 | auto formString = FormEncodingString(Spec.Form); |
| 135 | if (!formString.empty()) |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 136 | OS << formString; |
| 137 | else |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 138 | OS << format("DW_FORM_Unknown_%x", Spec.Form); |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame^] | 139 | if (Spec.isImplicitConst()) |
| 140 | OS << '\t' << *Spec.ByteSizeOrValue; |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 141 | OS << '\n'; |
Benjamin Kramer | 4137b6a | 2011-09-15 04:00:58 +0000 | [diff] [blame] | 142 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 143 | OS << '\n'; |
| 144 | } |
| 145 | |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 146 | Optional<uint32_t> |
| 147 | DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute Attr) const { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 148 | for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) { |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 149 | if (AttributeSpecs[i].Attr == Attr) |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 150 | return i; |
| 151 | } |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 152 | return None; |
| 153 | } |
| 154 | |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 155 | Optional<DWARFFormValue> DWARFAbbreviationDeclaration::getAttributeValue( |
| 156 | const uint32_t DIEOffset, const dwarf::Attribute Attr, |
| 157 | const DWARFUnit &U) const { |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 158 | Optional<uint32_t> MatchAttrIndex = findAttributeIndex(Attr); |
| 159 | if (!MatchAttrIndex) |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 160 | return None; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 161 | |
| 162 | auto DebugInfoData = U.getDebugInfoExtractor(); |
| 163 | |
| 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. |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 171 | DWARFFormValue FormValue(Spec.Form); |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame^] | 172 | if (Spec.isImplicitConst()) { |
| 173 | FormValue.setSValue(*Spec.ByteSizeOrValue); |
| 174 | return FormValue; |
| 175 | } |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 176 | if (FormValue.extractValue(DebugInfoData, &Offset, &U)) |
| 177 | return FormValue; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 178 | } |
| 179 | // March Offset along until we get to the attribute we want. |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame^] | 180 | if (auto FixedSize = Spec.getByteSize(U)) |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 181 | Offset += *FixedSize; |
| 182 | else |
| 183 | DWARFFormValue::skipValue(Spec.Form, DebugInfoData, &Offset, &U); |
| 184 | ++AttrIndex; |
| 185 | } |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 186 | return None; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | size_t DWARFAbbreviationDeclaration::FixedSizeInfo::getByteSize( |
| 190 | const DWARFUnit &U) const { |
| 191 | size_t ByteSize = NumBytes; |
| 192 | if (NumAddrs) |
| 193 | ByteSize += NumAddrs * U.getAddressByteSize(); |
| 194 | if (NumRefAddrs) |
| 195 | ByteSize += NumRefAddrs * U.getRefAddrByteSize(); |
| 196 | if (NumDwarfOffsets) |
| 197 | ByteSize += NumDwarfOffsets * U.getDwarfOffsetByteSize(); |
| 198 | return ByteSize; |
| 199 | } |
| 200 | |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame^] | 201 | Optional<int64_t> DWARFAbbreviationDeclaration::AttributeSpec::getByteSize( |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 202 | const DWARFUnit &U) const { |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame^] | 203 | if (isImplicitConst()) |
| 204 | return 0; |
| 205 | if (ByteSizeOrValue) |
| 206 | return ByteSizeOrValue; |
| 207 | Optional<int64_t> S; |
| 208 | auto FixedByteSize = DWARFFormValue::getFixedByteSize(Form, &U); |
| 209 | if (FixedByteSize) |
| 210 | S = *FixedByteSize; |
| 211 | return S; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | Optional<size_t> DWARFAbbreviationDeclaration::getFixedAttributesByteSize( |
| 215 | const DWARFUnit &U) const { |
| 216 | if (FixedAttributeSize) |
| 217 | return FixedAttributeSize->getByteSize(U); |
| 218 | return None; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 219 | } |