Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 1 | //===- DWARFAbbreviationDeclaration.cpp -----------------------------------===// |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 11 | |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/None.h" |
| 13 | #include "llvm/ADT/Optional.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 14 | #include "llvm/BinaryFormat/Dwarf.h" |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFUnit.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DataExtractor.h" |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | using namespace dwarf; |
| 25 | |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 26 | void DWARFAbbreviationDeclaration::clear() { |
| 27 | Code = 0; |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 28 | Tag = DW_TAG_null; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 29 | CodeByteSize = 0; |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 30 | HasChildren = false; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 31 | AttributeSpecs.clear(); |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 32 | FixedAttributeSize.reset(); |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() { |
| 36 | clear(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | bool |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 40 | DWARFAbbreviationDeclaration::extract(DataExtractor Data, |
| 41 | uint32_t* OffsetPtr) { |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 42 | clear(); |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 43 | const uint32_t Offset = *OffsetPtr; |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 44 | Code = Data.getULEB128(OffsetPtr); |
| 45 | if (Code == 0) { |
| 46 | return false; |
| 47 | } |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 48 | CodeByteSize = *OffsetPtr - Offset; |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 49 | Tag = static_cast<llvm::dwarf::Tag>(Data.getULEB128(OffsetPtr)); |
| 50 | if (Tag == DW_TAG_null) { |
| 51 | clear(); |
| 52 | return false; |
| 53 | } |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 54 | uint8_t ChildrenByte = Data.getU8(OffsetPtr); |
| 55 | HasChildren = (ChildrenByte == DW_CHILDREN_yes); |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 56 | // 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 Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 60 | |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 61 | // Read all of the abbreviation attributes and forms. |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 62 | while (true) { |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 63 | auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr)); |
| 64 | auto F = static_cast<Form>(Data.getULEB128(OffsetPtr)); |
| 65 | if (A && F) { |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 66 | bool IsImplicitConst = (F == DW_FORM_implicit_const); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 67 | if (IsImplicitConst) { |
Benjamin Kramer | eb14c11 | 2017-09-21 15:27:45 +0000 | [diff] [blame] | 68 | int64_t V = Data.getSLEB128(OffsetPtr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 69 | AttributeSpecs.push_back(AttributeSpec(A, F, V)); |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 70 | continue; |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 71 | } |
Benjamin Kramer | eb14c11 | 2017-09-21 15:27:45 +0000 | [diff] [blame] | 72 | Optional<uint8_t> ByteSize; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 73 | // If this abbrevation still has a fixed byte size, then update the |
| 74 | // FixedAttributeSize as needed. |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 75 | switch (F) { |
| 76 | case DW_FORM_addr: |
| 77 | if (FixedAttributeSize) |
| 78 | ++FixedAttributeSize->NumAddrs; |
| 79 | break; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 80 | |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 81 | case DW_FORM_ref_addr: |
| 82 | if (FixedAttributeSize) |
| 83 | ++FixedAttributeSize->NumRefAddrs; |
| 84 | break; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 85 | |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 86 | 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 Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 95 | |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 96 | 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. |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 99 | if ((ByteSize = dwarf::getFixedFormByteSize(F, dwarf::FormParams()))) { |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 100 | if (FixedAttributeSize) |
Benjamin Kramer | eb14c11 | 2017-09-21 15:27:45 +0000 | [diff] [blame] | 101 | FixedAttributeSize->NumBytes += *ByteSize; |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 102 | break; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 103 | } |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 104 | // Indicate we no longer have a fixed byte size for this |
| 105 | // abbreviation by clearing the FixedAttributeSize optional value |
| 106 | // so it doesn't have a value. |
| 107 | FixedAttributeSize.reset(); |
| 108 | break; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 109 | } |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 110 | // Record this attribute and its fixed size if it has one. |
Benjamin Kramer | eb14c11 | 2017-09-21 15:27:45 +0000 | [diff] [blame] | 111 | AttributeSpecs.push_back(AttributeSpec(A, F, ByteSize)); |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 112 | } else if (A == 0 && F == 0) { |
| 113 | // We successfully reached the end of this abbreviation declaration |
| 114 | // since both attribute and form are zero. |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 115 | break; |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 116 | } else { |
| 117 | // Attribute and form pairs must either both be non-zero, in which case |
| 118 | // they are added to the abbreviation declaration, or both be zero to |
| 119 | // terminate the abbrevation declaration. In this case only one was |
| 120 | // zero which is an error. |
| 121 | clear(); |
| 122 | return false; |
| 123 | } |
Alexey Samsonov | d5cc93c | 2013-10-31 17:20:14 +0000 | [diff] [blame] | 124 | } |
| 125 | return true; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const { |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 129 | auto tagString = TagString(getTag()); |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 130 | OS << '[' << getCode() << "] "; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 131 | if (!tagString.empty()) |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 132 | OS << tagString; |
| 133 | else |
| 134 | OS << format("DW_TAG_Unknown_%x", getTag()); |
| 135 | OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n'; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 136 | for (const AttributeSpec &Spec : AttributeSpecs) { |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 137 | OS << '\t'; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 138 | auto attrString = AttributeString(Spec.Attr); |
| 139 | if (!attrString.empty()) |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 140 | OS << attrString; |
| 141 | else |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 142 | OS << format("DW_AT_Unknown_%x", Spec.Attr); |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 143 | OS << '\t'; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 144 | auto formString = FormEncodingString(Spec.Form); |
| 145 | if (!formString.empty()) |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 146 | OS << formString; |
| 147 | else |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 148 | OS << format("DW_FORM_Unknown_%x", Spec.Form); |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 149 | if (Spec.isImplicitConst()) |
Benjamin Kramer | eb14c11 | 2017-09-21 15:27:45 +0000 | [diff] [blame] | 150 | OS << '\t' << Spec.getImplicitConstValue(); |
Benjamin Kramer | 21a5092 | 2011-09-15 04:15:59 +0000 | [diff] [blame] | 151 | OS << '\n'; |
Benjamin Kramer | 4137b6a | 2011-09-15 04:00:58 +0000 | [diff] [blame] | 152 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 153 | OS << '\n'; |
| 154 | } |
| 155 | |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 156 | Optional<uint32_t> |
| 157 | DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute Attr) const { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 158 | for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) { |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 159 | if (AttributeSpecs[i].Attr == Attr) |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 160 | return i; |
| 161 | } |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 162 | return None; |
| 163 | } |
| 164 | |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 165 | Optional<DWARFFormValue> DWARFAbbreviationDeclaration::getAttributeValue( |
| 166 | const uint32_t DIEOffset, const dwarf::Attribute Attr, |
| 167 | const DWARFUnit &U) const { |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 168 | Optional<uint32_t> MatchAttrIndex = findAttributeIndex(Attr); |
| 169 | if (!MatchAttrIndex) |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 170 | return None; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 171 | |
| 172 | auto DebugInfoData = U.getDebugInfoExtractor(); |
| 173 | |
| 174 | // Add the byte size of ULEB that for the abbrev Code so we can start |
| 175 | // skipping the attribute data. |
| 176 | uint32_t Offset = DIEOffset + CodeByteSize; |
| 177 | uint32_t AttrIndex = 0; |
| 178 | for (const auto &Spec : AttributeSpecs) { |
| 179 | if (*MatchAttrIndex == AttrIndex) { |
| 180 | // We have arrived at the attribute to extract, extract if from Offset. |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 181 | DWARFFormValue FormValue(Spec.Form); |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 182 | if (Spec.isImplicitConst()) { |
Benjamin Kramer | eb14c11 | 2017-09-21 15:27:45 +0000 | [diff] [blame] | 183 | FormValue.setSValue(Spec.getImplicitConstValue()); |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 184 | return FormValue; |
| 185 | } |
Paul Robinson | e5400f8 | 2017-11-07 19:57:12 +0000 | [diff] [blame] | 186 | if (FormValue.extractValue(DebugInfoData, &Offset, U.getFormParams(), &U)) |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 187 | return FormValue; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 188 | } |
| 189 | // March Offset along until we get to the attribute we want. |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 190 | if (auto FixedSize = Spec.getByteSize(U)) |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 191 | Offset += *FixedSize; |
| 192 | else |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 193 | DWARFFormValue::skipValue(Spec.Form, DebugInfoData, &Offset, |
| 194 | U.getFormParams()); |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 195 | ++AttrIndex; |
| 196 | } |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 197 | return None; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | size_t DWARFAbbreviationDeclaration::FixedSizeInfo::getByteSize( |
| 201 | const DWARFUnit &U) const { |
| 202 | size_t ByteSize = NumBytes; |
| 203 | if (NumAddrs) |
| 204 | ByteSize += NumAddrs * U.getAddressByteSize(); |
| 205 | if (NumRefAddrs) |
| 206 | ByteSize += NumRefAddrs * U.getRefAddrByteSize(); |
| 207 | if (NumDwarfOffsets) |
| 208 | ByteSize += NumDwarfOffsets * U.getDwarfOffsetByteSize(); |
| 209 | return ByteSize; |
| 210 | } |
| 211 | |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 212 | Optional<int64_t> DWARFAbbreviationDeclaration::AttributeSpec::getByteSize( |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 213 | const DWARFUnit &U) const { |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 214 | if (isImplicitConst()) |
| 215 | return 0; |
Benjamin Kramer | 0fad6dd | 2017-10-31 19:55:08 +0000 | [diff] [blame] | 216 | if (ByteSize.HasByteSize) |
| 217 | return ByteSize.ByteSize; |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 218 | Optional<int64_t> S; |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 219 | auto FixedByteSize = dwarf::getFixedFormByteSize(Form, U.getFormParams()); |
Victor Leschuk | cbddae7 | 2017-01-10 21:18:26 +0000 | [diff] [blame] | 220 | if (FixedByteSize) |
| 221 | S = *FixedByteSize; |
| 222 | return S; |
Greg Clayton | 6f6e4db | 2016-11-15 01:23:06 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | Optional<size_t> DWARFAbbreviationDeclaration::getFixedAttributesByteSize( |
| 226 | const DWARFUnit &U) const { |
| 227 | if (FixedAttributeSize) |
| 228 | return FixedAttributeSize->getByteSize(U); |
| 229 | return None; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 230 | } |