Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 1 | //===- DWARFAcceleratorTable.cpp ------------------------------------------===// |
Frederic Riss | 7c41c64 | 2014-11-20 16:21:06 +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 | |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 11 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/Dwarf.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Compiler.h" |
Jonas Devlieghere | 92ac9d3 | 2018-01-28 11:05:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/DJB.h" |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Format.h" |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FormatVariadic.h" |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ScopedPrinter.h" |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 21 | #include <cstddef> |
| 22 | #include <cstdint> |
| 23 | #include <utility> |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 24 | |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 26 | |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 27 | namespace { |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 28 | struct Atom { |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 29 | unsigned Value; |
| 30 | }; |
| 31 | |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 32 | static raw_ostream &operator<<(raw_ostream &OS, const Atom &A) { |
| 33 | StringRef Str = dwarf::AtomTypeString(A.Value); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 34 | if (!Str.empty()) |
| 35 | return OS << Str; |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 36 | return OS << "DW_ATOM_unknown_" << format("%x", A.Value); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 37 | } |
| 38 | } // namespace |
| 39 | |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 40 | static Atom formatAtom(unsigned Atom) { return {Atom}; } |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 41 | |
| 42 | DWARFAcceleratorTable::~DWARFAcceleratorTable() = default; |
| 43 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 44 | llvm::Error AppleAcceleratorTable::extract() { |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 45 | uint32_t Offset = 0; |
| 46 | |
| 47 | // Check that we can at least read the header. |
| 48 | if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4)) |
Jonas Devlieghere | ba91589 | 2017-12-11 18:22:47 +0000 | [diff] [blame] | 49 | return make_error<StringError>("Section too small: cannot read header.", |
| 50 | inconvertibleErrorCode()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 51 | |
| 52 | Hdr.Magic = AccelSection.getU32(&Offset); |
| 53 | Hdr.Version = AccelSection.getU16(&Offset); |
| 54 | Hdr.HashFunction = AccelSection.getU16(&Offset); |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 55 | Hdr.BucketCount = AccelSection.getU32(&Offset); |
| 56 | Hdr.HashCount = AccelSection.getU32(&Offset); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 57 | Hdr.HeaderDataLength = AccelSection.getU32(&Offset); |
| 58 | |
| 59 | // Check that we can read all the hashes and offsets from the |
| 60 | // section (see SourceLevelDebugging.rst for the structure of the index). |
Jonas Devlieghere | ba91589 | 2017-12-11 18:22:47 +0000 | [diff] [blame] | 61 | // We need to substract one because we're checking for an *offset* which is |
| 62 | // equal to the size for an empty table and hence pointer after the section. |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 63 | if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength + |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 64 | Hdr.BucketCount * 4 + Hdr.HashCount * 8 - 1)) |
Jonas Devlieghere | ba91589 | 2017-12-11 18:22:47 +0000 | [diff] [blame] | 65 | return make_error<StringError>( |
| 66 | "Section too small: cannot read buckets and hashes.", |
| 67 | inconvertibleErrorCode()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 68 | |
| 69 | HdrData.DIEOffsetBase = AccelSection.getU32(&Offset); |
| 70 | uint32_t NumAtoms = AccelSection.getU32(&Offset); |
| 71 | |
| 72 | for (unsigned i = 0; i < NumAtoms; ++i) { |
| 73 | uint16_t AtomType = AccelSection.getU16(&Offset); |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 74 | auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset)); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 75 | HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm)); |
| 76 | } |
| 77 | |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 78 | IsValid = true; |
Jonas Devlieghere | ba91589 | 2017-12-11 18:22:47 +0000 | [diff] [blame] | 79 | return Error::success(); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 82 | uint32_t AppleAcceleratorTable::getNumBuckets() { return Hdr.BucketCount; } |
| 83 | uint32_t AppleAcceleratorTable::getNumHashes() { return Hdr.HashCount; } |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 84 | uint32_t AppleAcceleratorTable::getSizeHdr() { return sizeof(Hdr); } |
| 85 | uint32_t AppleAcceleratorTable::getHeaderDataLength() { |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 86 | return Hdr.HeaderDataLength; |
| 87 | } |
| 88 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 89 | ArrayRef<std::pair<AppleAcceleratorTable::HeaderData::AtomType, |
| 90 | AppleAcceleratorTable::HeaderData::Form>> |
| 91 | AppleAcceleratorTable::getAtomsDesc() { |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 92 | return HdrData.Atoms; |
| 93 | } |
| 94 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 95 | bool AppleAcceleratorTable::validateForms() { |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 96 | for (auto Atom : getAtomsDesc()) { |
| 97 | DWARFFormValue FormValue(Atom.second); |
| 98 | switch (Atom.first) { |
| 99 | case dwarf::DW_ATOM_die_offset: |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 100 | case dwarf::DW_ATOM_die_tag: |
| 101 | case dwarf::DW_ATOM_type_flags: |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 102 | if ((!FormValue.isFormClass(DWARFFormValue::FC_Constant) && |
| 103 | !FormValue.isFormClass(DWARFFormValue::FC_Flag)) || |
| 104 | FormValue.getForm() == dwarf::DW_FORM_sdata) |
| 105 | return false; |
Adrian Prantl | 0e6694d | 2017-12-19 22:05:25 +0000 | [diff] [blame] | 106 | break; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 107 | default: |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 114 | std::pair<uint32_t, dwarf::Tag> |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 115 | AppleAcceleratorTable::readAtoms(uint32_t &HashDataOffset) { |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 116 | uint32_t DieOffset = dwarf::DW_INVALID_OFFSET; |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 117 | dwarf::Tag DieTag = dwarf::DW_TAG_null; |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 118 | dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32}; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 119 | |
| 120 | for (auto Atom : getAtomsDesc()) { |
| 121 | DWARFFormValue FormValue(Atom.second); |
Paul Robinson | e5400f8 | 2017-11-07 19:57:12 +0000 | [diff] [blame] | 122 | FormValue.extractValue(AccelSection, &HashDataOffset, FormParams); |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 123 | switch (Atom.first) { |
| 124 | case dwarf::DW_ATOM_die_offset: |
| 125 | DieOffset = *FormValue.getAsUnsignedConstant(); |
| 126 | break; |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 127 | case dwarf::DW_ATOM_die_tag: |
| 128 | DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant(); |
| 129 | break; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | } |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 134 | return {DieOffset, DieTag}; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 137 | void AppleAcceleratorTable::Header::dump(ScopedPrinter &W) const { |
| 138 | DictScope HeaderScope(W, "Header"); |
| 139 | W.printHex("Magic", Magic); |
| 140 | W.printHex("Version", Version); |
| 141 | W.printHex("Hash function", HashFunction); |
| 142 | W.printNumber("Bucket count", BucketCount); |
| 143 | W.printNumber("Hashes count", HashCount); |
| 144 | W.printNumber("HeaderData length", HeaderDataLength); |
| 145 | } |
| 146 | |
Pavel Labath | 47c3472 | 2018-03-09 11:58:59 +0000 | [diff] [blame] | 147 | Optional<uint64_t> AppleAcceleratorTable::HeaderData::extractOffset( |
| 148 | Optional<DWARFFormValue> Value) const { |
| 149 | if (!Value) |
| 150 | return None; |
| 151 | |
| 152 | switch (Value->getForm()) { |
| 153 | case dwarf::DW_FORM_ref1: |
| 154 | case dwarf::DW_FORM_ref2: |
| 155 | case dwarf::DW_FORM_ref4: |
| 156 | case dwarf::DW_FORM_ref8: |
| 157 | case dwarf::DW_FORM_ref_udata: |
| 158 | return Value->getRawUValue() + DIEOffsetBase; |
| 159 | default: |
| 160 | return Value->getAsSectionOffset(); |
| 161 | } |
| 162 | } |
| 163 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 164 | bool AppleAcceleratorTable::dumpName(ScopedPrinter &W, |
| 165 | SmallVectorImpl<DWARFFormValue> &AtomForms, |
| 166 | uint32_t *DataOffset) const { |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 167 | dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32}; |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 168 | uint32_t NameOffset = *DataOffset; |
| 169 | if (!AccelSection.isValidOffsetForDataOfSize(*DataOffset, 4)) { |
| 170 | W.printString("Incorrectly terminated list."); |
| 171 | return false; |
| 172 | } |
| 173 | unsigned StringOffset = AccelSection.getRelocatedValue(4, DataOffset); |
| 174 | if (!StringOffset) |
| 175 | return false; // End of list |
| 176 | |
| 177 | DictScope NameScope(W, ("Name@0x" + Twine::utohexstr(NameOffset)).str()); |
| 178 | W.startLine() << format("String: 0x%08x", StringOffset); |
| 179 | W.getOStream() << " \"" << StringSection.getCStr(&StringOffset) << "\"\n"; |
| 180 | |
| 181 | unsigned NumData = AccelSection.getU32(DataOffset); |
| 182 | for (unsigned Data = 0; Data < NumData; ++Data) { |
| 183 | ListScope DataScope(W, ("Data " + Twine(Data)).str()); |
| 184 | unsigned i = 0; |
| 185 | for (auto &Atom : AtomForms) { |
| 186 | W.startLine() << format("Atom[%d]: ", i++); |
| 187 | if (Atom.extractValue(AccelSection, DataOffset, FormParams)) |
| 188 | Atom.dump(W.getOStream()); |
| 189 | else |
| 190 | W.getOStream() << "Error extracting the value"; |
| 191 | W.getOStream() << "\n"; |
| 192 | } |
| 193 | } |
| 194 | return true; // more entries follow |
| 195 | } |
| 196 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 197 | LLVM_DUMP_METHOD void AppleAcceleratorTable::dump(raw_ostream &OS) const { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 198 | if (!IsValid) |
| 199 | return; |
| 200 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 201 | ScopedPrinter W(OS); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 202 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 203 | Hdr.dump(W); |
| 204 | |
| 205 | W.printNumber("DIE offset base", HdrData.DIEOffsetBase); |
Pavel Labath | 3460957 | 2018-01-29 11:53:46 +0000 | [diff] [blame] | 206 | W.printNumber("Number of atoms", uint64_t(HdrData.Atoms.size())); |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 207 | SmallVector<DWARFFormValue, 3> AtomForms; |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 208 | { |
| 209 | ListScope AtomsScope(W, "Atoms"); |
| 210 | unsigned i = 0; |
| 211 | for (const auto &Atom : HdrData.Atoms) { |
| 212 | DictScope AtomScope(W, ("Atom " + Twine(i++)).str()); |
| 213 | W.startLine() << "Type: " << formatAtom(Atom.first) << '\n'; |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 214 | W.startLine() << "Form: " << formatv("{0}", Atom.second) << '\n'; |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 215 | AtomForms.push_back(DWARFFormValue(Atom.second)); |
| 216 | } |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Now go through the actual tables and dump them. |
| 220 | uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength; |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 221 | unsigned HashesBase = Offset + Hdr.BucketCount * 4; |
| 222 | unsigned OffsetsBase = HashesBase + Hdr.HashCount * 4; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 223 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 224 | for (unsigned Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) { |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 225 | unsigned Index = AccelSection.getU32(&Offset); |
| 226 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 227 | ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 228 | if (Index == UINT32_MAX) { |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 229 | W.printString("EMPTY"); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 230 | continue; |
| 231 | } |
| 232 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 233 | for (unsigned HashIdx = Index; HashIdx < Hdr.HashCount; ++HashIdx) { |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 234 | unsigned HashOffset = HashesBase + HashIdx*4; |
| 235 | unsigned OffsetsOffset = OffsetsBase + HashIdx*4; |
| 236 | uint32_t Hash = AccelSection.getU32(&HashOffset); |
| 237 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 238 | if (Hash % Hdr.BucketCount != Bucket) |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 239 | break; |
| 240 | |
| 241 | unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 242 | ListScope HashScope(W, ("Hash 0x" + Twine::utohexstr(Hash)).str()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 243 | if (!AccelSection.isValidOffset(DataOffset)) { |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 244 | W.printString("Invalid section offset"); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 245 | continue; |
| 246 | } |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 247 | while (dumpName(W, AtomForms, &DataOffset)) |
| 248 | /*empty*/; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 252 | |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 253 | AppleAcceleratorTable::Entry::Entry( |
| 254 | const AppleAcceleratorTable::HeaderData &HdrData) |
| 255 | : HdrData(&HdrData) { |
| 256 | Values.reserve(HdrData.Atoms.size()); |
| 257 | for (const auto &Atom : HdrData.Atoms) |
| 258 | Values.push_back(DWARFFormValue(Atom.second)); |
| 259 | } |
| 260 | |
| 261 | void AppleAcceleratorTable::Entry::extract( |
| 262 | const AppleAcceleratorTable &AccelTable, uint32_t *Offset) { |
| 263 | |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 264 | dwarf::FormParams FormParams = {AccelTable.Hdr.Version, 0, |
| 265 | dwarf::DwarfFormat::DWARF32}; |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 266 | for (auto &Atom : Values) |
| 267 | Atom.extractValue(AccelTable.AccelSection, Offset, FormParams); |
| 268 | } |
| 269 | |
| 270 | Optional<DWARFFormValue> |
| 271 | AppleAcceleratorTable::Entry::lookup(HeaderData::AtomType Atom) const { |
| 272 | assert(HdrData && "Dereferencing end iterator?"); |
| 273 | assert(HdrData->Atoms.size() == Values.size()); |
| 274 | for (const auto &Tuple : zip_first(HdrData->Atoms, Values)) { |
| 275 | if (std::get<0>(Tuple).first == Atom) |
| 276 | return std::get<1>(Tuple); |
| 277 | } |
| 278 | return None; |
| 279 | } |
| 280 | |
Pavel Labath | 47c3472 | 2018-03-09 11:58:59 +0000 | [diff] [blame] | 281 | Optional<uint64_t> AppleAcceleratorTable::Entry::getDIESectionOffset() const { |
| 282 | return HdrData->extractOffset(lookup(dwarf::DW_ATOM_die_offset)); |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | Optional<uint64_t> AppleAcceleratorTable::Entry::getCUOffset() const { |
Pavel Labath | 47c3472 | 2018-03-09 11:58:59 +0000 | [diff] [blame] | 286 | return HdrData->extractOffset(lookup(dwarf::DW_ATOM_cu_offset)); |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | Optional<dwarf::Tag> AppleAcceleratorTable::Entry::getTag() const { |
| 290 | Optional<DWARFFormValue> Tag = lookup(dwarf::DW_ATOM_die_tag); |
| 291 | if (!Tag) |
| 292 | return None; |
| 293 | if (Optional<uint64_t> Value = Tag->getAsUnsignedConstant()) |
| 294 | return dwarf::Tag(*Value); |
| 295 | return None; |
| 296 | } |
| 297 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 298 | AppleAcceleratorTable::ValueIterator::ValueIterator( |
| 299 | const AppleAcceleratorTable &AccelTable, unsigned Offset) |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 300 | : AccelTable(&AccelTable), Current(AccelTable.HdrData), DataOffset(Offset) { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 301 | if (!AccelTable.AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) |
| 302 | return; |
| 303 | |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 304 | // Read the first entry. |
| 305 | NumData = AccelTable.AccelSection.getU32(&DataOffset); |
| 306 | Next(); |
| 307 | } |
| 308 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 309 | void AppleAcceleratorTable::ValueIterator::Next() { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 310 | assert(NumData > 0 && "attempted to increment iterator past the end"); |
| 311 | auto &AccelSection = AccelTable->AccelSection; |
| 312 | if (Data >= NumData || |
| 313 | !AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) { |
| 314 | NumData = 0; |
| 315 | return; |
| 316 | } |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 317 | Current.extract(*AccelTable, &DataOffset); |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 318 | ++Data; |
| 319 | } |
| 320 | |
Pavel Labath | 9b36fd2 | 2018-01-22 13:17:23 +0000 | [diff] [blame] | 321 | iterator_range<AppleAcceleratorTable::ValueIterator> |
| 322 | AppleAcceleratorTable::equal_range(StringRef Key) const { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 323 | if (!IsValid) |
| 324 | return make_range(ValueIterator(), ValueIterator()); |
| 325 | |
| 326 | // Find the bucket. |
Jonas Devlieghere | 92ac9d3 | 2018-01-28 11:05:10 +0000 | [diff] [blame] | 327 | unsigned HashValue = djbHash(Key); |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 328 | unsigned Bucket = HashValue % Hdr.BucketCount; |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 329 | unsigned BucketBase = sizeof(Hdr) + Hdr.HeaderDataLength; |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 330 | unsigned HashesBase = BucketBase + Hdr.BucketCount * 4; |
| 331 | unsigned OffsetsBase = HashesBase + Hdr.HashCount * 4; |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 332 | |
| 333 | unsigned BucketOffset = BucketBase + Bucket * 4; |
| 334 | unsigned Index = AccelSection.getU32(&BucketOffset); |
| 335 | |
| 336 | // Search through all hashes in the bucket. |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 337 | for (unsigned HashIdx = Index; HashIdx < Hdr.HashCount; ++HashIdx) { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 338 | unsigned HashOffset = HashesBase + HashIdx * 4; |
| 339 | unsigned OffsetsOffset = OffsetsBase + HashIdx * 4; |
| 340 | uint32_t Hash = AccelSection.getU32(&HashOffset); |
| 341 | |
Pavel Labath | 394e805 | 2018-01-29 11:33:17 +0000 | [diff] [blame] | 342 | if (Hash % Hdr.BucketCount != Bucket) |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 343 | // We are already in the next bucket. |
| 344 | break; |
| 345 | |
| 346 | unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); |
| 347 | unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset); |
| 348 | if (!StringOffset) |
| 349 | break; |
| 350 | |
| 351 | // Finally, compare the key. |
| 352 | if (Key == StringSection.getCStr(&StringOffset)) |
| 353 | return make_range({*this, DataOffset}, ValueIterator()); |
| 354 | } |
| 355 | return make_range(ValueIterator(), ValueIterator()); |
| 356 | } |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 357 | |
| 358 | void DWARFDebugNames::Header::dump(ScopedPrinter &W) const { |
| 359 | DictScope HeaderScope(W, "Header"); |
| 360 | W.printHex("Length", UnitLength); |
| 361 | W.printNumber("Version", Version); |
| 362 | W.printHex("Padding", Padding); |
| 363 | W.printNumber("CU count", CompUnitCount); |
| 364 | W.printNumber("Local TU count", LocalTypeUnitCount); |
| 365 | W.printNumber("Foreign TU count", ForeignTypeUnitCount); |
| 366 | W.printNumber("Bucket count", BucketCount); |
| 367 | W.printNumber("Name count", NameCount); |
| 368 | W.printHex("Abbreviations table size", AbbrevTableSize); |
| 369 | W.startLine() << "Augmentation: '" << AugmentationString << "'\n"; |
| 370 | } |
| 371 | |
| 372 | llvm::Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS, |
| 373 | uint32_t *Offset) { |
| 374 | // Check that we can read the fixed-size part. |
Pavel Labath | e726410 | 2018-01-29 13:53:48 +0000 | [diff] [blame] | 375 | if (!AS.isValidOffset(*Offset + sizeof(HeaderPOD) - 1)) |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 376 | return make_error<StringError>("Section too small: cannot read header.", |
| 377 | inconvertibleErrorCode()); |
| 378 | |
| 379 | UnitLength = AS.getU32(Offset); |
| 380 | Version = AS.getU16(Offset); |
| 381 | Padding = AS.getU16(Offset); |
| 382 | CompUnitCount = AS.getU32(Offset); |
| 383 | LocalTypeUnitCount = AS.getU32(Offset); |
| 384 | ForeignTypeUnitCount = AS.getU32(Offset); |
| 385 | BucketCount = AS.getU32(Offset); |
| 386 | NameCount = AS.getU32(Offset); |
| 387 | AbbrevTableSize = AS.getU32(Offset); |
Pavel Labath | ea0f841 | 2018-03-29 15:12:45 +0000 | [diff] [blame] | 388 | AugmentationStringSize = alignTo(AS.getU32(Offset), 4); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 389 | |
| 390 | if (!AS.isValidOffsetForDataOfSize(*Offset, AugmentationStringSize)) |
| 391 | return make_error<StringError>( |
| 392 | "Section too small: cannot read header augmentation.", |
| 393 | inconvertibleErrorCode()); |
| 394 | AugmentationString.resize(AugmentationStringSize); |
| 395 | AS.getU8(Offset, reinterpret_cast<uint8_t *>(AugmentationString.data()), |
| 396 | AugmentationStringSize); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 397 | return Error::success(); |
| 398 | } |
| 399 | |
| 400 | void DWARFDebugNames::Abbrev::dump(ScopedPrinter &W) const { |
| 401 | DictScope AbbrevScope(W, ("Abbreviation 0x" + Twine::utohexstr(Code)).str()); |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 402 | W.startLine() << formatv("Tag: {0}\n", Tag); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 403 | |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 404 | for (const auto &Attr : Attributes) |
| 405 | W.startLine() << formatv("{0}: {1}\n", Attr.Index, Attr.Form); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static constexpr DWARFDebugNames::AttributeEncoding sentinelAttrEnc() { |
| 409 | return {dwarf::Index(0), dwarf::Form(0)}; |
| 410 | } |
| 411 | |
| 412 | static bool isSentinel(const DWARFDebugNames::AttributeEncoding &AE) { |
| 413 | return AE == sentinelAttrEnc(); |
| 414 | } |
| 415 | |
| 416 | static DWARFDebugNames::Abbrev sentinelAbbrev() { |
| 417 | return DWARFDebugNames::Abbrev(0, dwarf::Tag(0), {}); |
| 418 | } |
| 419 | |
| 420 | static bool isSentinel(const DWARFDebugNames::Abbrev &Abbr) { |
| 421 | return Abbr.Code == 0; |
| 422 | } |
| 423 | |
| 424 | DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getEmptyKey() { |
| 425 | return sentinelAbbrev(); |
| 426 | } |
| 427 | |
| 428 | DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getTombstoneKey() { |
| 429 | return DWARFDebugNames::Abbrev(~0, dwarf::Tag(0), {}); |
| 430 | } |
| 431 | |
| 432 | Expected<DWARFDebugNames::AttributeEncoding> |
| 433 | DWARFDebugNames::NameIndex::extractAttributeEncoding(uint32_t *Offset) { |
| 434 | if (*Offset >= EntriesBase) { |
| 435 | return make_error<StringError>("Incorrectly terminated abbreviation table.", |
| 436 | inconvertibleErrorCode()); |
| 437 | } |
| 438 | |
| 439 | uint32_t Index = Section.AccelSection.getULEB128(Offset); |
| 440 | uint32_t Form = Section.AccelSection.getULEB128(Offset); |
| 441 | return AttributeEncoding(dwarf::Index(Index), dwarf::Form(Form)); |
| 442 | } |
| 443 | |
| 444 | Expected<std::vector<DWARFDebugNames::AttributeEncoding>> |
| 445 | DWARFDebugNames::NameIndex::extractAttributeEncodings(uint32_t *Offset) { |
| 446 | std::vector<AttributeEncoding> Result; |
| 447 | for (;;) { |
| 448 | auto AttrEncOr = extractAttributeEncoding(Offset); |
| 449 | if (!AttrEncOr) |
| 450 | return AttrEncOr.takeError(); |
| 451 | if (isSentinel(*AttrEncOr)) |
| 452 | return std::move(Result); |
| 453 | |
| 454 | Result.emplace_back(*AttrEncOr); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | Expected<DWARFDebugNames::Abbrev> |
| 459 | DWARFDebugNames::NameIndex::extractAbbrev(uint32_t *Offset) { |
| 460 | if (*Offset >= EntriesBase) { |
| 461 | return make_error<StringError>("Incorrectly terminated abbreviation table.", |
| 462 | inconvertibleErrorCode()); |
| 463 | } |
| 464 | |
| 465 | uint32_t Code = Section.AccelSection.getULEB128(Offset); |
| 466 | if (Code == 0) |
| 467 | return sentinelAbbrev(); |
| 468 | |
| 469 | uint32_t Tag = Section.AccelSection.getULEB128(Offset); |
| 470 | auto AttrEncOr = extractAttributeEncodings(Offset); |
| 471 | if (!AttrEncOr) |
| 472 | return AttrEncOr.takeError(); |
| 473 | return Abbrev(Code, dwarf::Tag(Tag), std::move(*AttrEncOr)); |
| 474 | } |
| 475 | |
| 476 | Error DWARFDebugNames::NameIndex::extract() { |
| 477 | const DWARFDataExtractor &AS = Section.AccelSection; |
| 478 | uint32_t Offset = Base; |
| 479 | if (Error E = Hdr.extract(AS, &Offset)) |
| 480 | return E; |
| 481 | |
| 482 | CUsBase = Offset; |
| 483 | Offset += Hdr.CompUnitCount * 4; |
| 484 | Offset += Hdr.LocalTypeUnitCount * 4; |
| 485 | Offset += Hdr.ForeignTypeUnitCount * 8; |
| 486 | BucketsBase = Offset; |
| 487 | Offset += Hdr.BucketCount * 4; |
| 488 | HashesBase = Offset; |
| 489 | if (Hdr.BucketCount > 0) |
| 490 | Offset += Hdr.NameCount * 4; |
| 491 | StringOffsetsBase = Offset; |
| 492 | Offset += Hdr.NameCount * 4; |
| 493 | EntryOffsetsBase = Offset; |
| 494 | Offset += Hdr.NameCount * 4; |
| 495 | |
| 496 | if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize)) |
| 497 | return make_error<StringError>( |
| 498 | "Section too small: cannot read abbreviations.", |
| 499 | inconvertibleErrorCode()); |
| 500 | |
| 501 | EntriesBase = Offset + Hdr.AbbrevTableSize; |
| 502 | |
| 503 | for (;;) { |
| 504 | auto AbbrevOr = extractAbbrev(&Offset); |
| 505 | if (!AbbrevOr) |
| 506 | return AbbrevOr.takeError(); |
| 507 | if (isSentinel(*AbbrevOr)) |
| 508 | return Error::success(); |
| 509 | |
| 510 | if (!Abbrevs.insert(std::move(*AbbrevOr)).second) { |
| 511 | return make_error<StringError>("Duplicate abbreviation code.", |
| 512 | inconvertibleErrorCode()); |
| 513 | } |
| 514 | } |
| 515 | } |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 516 | DWARFDebugNames::Entry::Entry(const NameIndex &NameIdx, const Abbrev &Abbr) |
| 517 | : NameIdx(&NameIdx), Abbr(&Abbr) { |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 518 | // This merely creates form values. It is up to the caller |
| 519 | // (NameIndex::getEntry) to populate them. |
| 520 | Values.reserve(Abbr.Attributes.size()); |
| 521 | for (const auto &Attr : Abbr.Attributes) |
| 522 | Values.emplace_back(Attr.Form); |
| 523 | } |
| 524 | |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 525 | Optional<DWARFFormValue> |
| 526 | DWARFDebugNames::Entry::lookup(dwarf::Index Index) const { |
| 527 | assert(Abbr->Attributes.size() == Values.size()); |
| 528 | for (const auto &Tuple : zip_first(Abbr->Attributes, Values)) { |
| 529 | if (std::get<0>(Tuple).Index == Index) |
| 530 | return std::get<1>(Tuple); |
| 531 | } |
| 532 | return None; |
| 533 | } |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 534 | |
Pavel Labath | 47c3472 | 2018-03-09 11:58:59 +0000 | [diff] [blame] | 535 | Optional<uint64_t> DWARFDebugNames::Entry::getDIEUnitOffset() const { |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 536 | if (Optional<DWARFFormValue> Off = lookup(dwarf::DW_IDX_die_offset)) |
Pavel Labath | 2d1fc43 | 2018-03-29 13:47:57 +0000 | [diff] [blame] | 537 | return Off->getAsReferenceUVal(); |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 538 | return None; |
| 539 | } |
| 540 | |
| 541 | Optional<uint64_t> DWARFDebugNames::Entry::getCUIndex() const { |
| 542 | if (Optional<DWARFFormValue> Off = lookup(dwarf::DW_IDX_compile_unit)) |
| 543 | return Off->getAsUnsignedConstant(); |
Pavel Labath | 47c3472 | 2018-03-09 11:58:59 +0000 | [diff] [blame] | 544 | // In a per-CU index, the entries without a DW_IDX_compile_unit attribute |
| 545 | // implicitly refer to the single CU. |
| 546 | if (NameIdx->getCUCount() == 1) |
| 547 | return 0; |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 548 | return None; |
| 549 | } |
| 550 | |
| 551 | Optional<uint64_t> DWARFDebugNames::Entry::getCUOffset() const { |
| 552 | Optional<uint64_t> Index = getCUIndex(); |
| 553 | if (!Index || *Index >= NameIdx->getCUCount()) |
| 554 | return None; |
| 555 | return NameIdx->getCUOffset(*Index); |
| 556 | } |
| 557 | |
Pavel Labath | 47c3472 | 2018-03-09 11:58:59 +0000 | [diff] [blame] | 558 | Optional<uint64_t> DWARFDebugNames::Entry::getDIESectionOffset() const { |
| 559 | Optional<uint64_t> CUOff = getCUOffset(); |
| 560 | Optional<uint64_t> DIEOff = getDIEUnitOffset(); |
| 561 | if (CUOff && DIEOff) |
| 562 | return *CUOff + *DIEOff; |
| 563 | return None; |
| 564 | } |
| 565 | |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 566 | void DWARFDebugNames::Entry::dump(ScopedPrinter &W) const { |
| 567 | W.printHex("Abbrev", Abbr->Code); |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 568 | W.startLine() << formatv("Tag: {0}\n", Abbr->Tag); |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 569 | assert(Abbr->Attributes.size() == Values.size()); |
| 570 | for (const auto &Tuple : zip_first(Abbr->Attributes, Values)) { |
Pavel Labath | 9025f95 | 2018-03-21 11:46:37 +0000 | [diff] [blame] | 571 | W.startLine() << formatv("{0}: ", std::get<0>(Tuple).Index); |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 572 | std::get<1>(Tuple).dump(W.getOStream()); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 573 | W.getOStream() << '\n'; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | char DWARFDebugNames::SentinelError::ID; |
| 578 | std::error_code DWARFDebugNames::SentinelError::convertToErrorCode() const { |
| 579 | return inconvertibleErrorCode(); |
| 580 | } |
| 581 | |
| 582 | uint32_t DWARFDebugNames::NameIndex::getCUOffset(uint32_t CU) const { |
| 583 | assert(CU < Hdr.CompUnitCount); |
| 584 | uint32_t Offset = CUsBase + 4 * CU; |
| 585 | return Section.AccelSection.getRelocatedValue(4, &Offset); |
| 586 | } |
| 587 | |
| 588 | uint32_t DWARFDebugNames::NameIndex::getLocalTUOffset(uint32_t TU) const { |
| 589 | assert(TU < Hdr.LocalTypeUnitCount); |
| 590 | uint32_t Offset = CUsBase + Hdr.CompUnitCount * 4; |
| 591 | return Section.AccelSection.getRelocatedValue(4, &Offset); |
| 592 | } |
| 593 | |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 594 | uint64_t DWARFDebugNames::NameIndex::getForeignTUSignature(uint32_t TU) const { |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 595 | assert(TU < Hdr.ForeignTypeUnitCount); |
| 596 | uint32_t Offset = CUsBase + (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) * 4; |
| 597 | return Section.AccelSection.getU64(&Offset); |
| 598 | } |
| 599 | |
| 600 | Expected<DWARFDebugNames::Entry> |
| 601 | DWARFDebugNames::NameIndex::getEntry(uint32_t *Offset) const { |
| 602 | const DWARFDataExtractor &AS = Section.AccelSection; |
| 603 | if (!AS.isValidOffset(*Offset)) |
Pavel Labath | c9f07b0 | 2018-04-06 13:34:12 +0000 | [diff] [blame] | 604 | return make_error<StringError>("Incorrectly terminated entry list.", |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 605 | inconvertibleErrorCode()); |
| 606 | |
| 607 | uint32_t AbbrevCode = AS.getULEB128(Offset); |
| 608 | if (AbbrevCode == 0) |
| 609 | return make_error<SentinelError>(); |
| 610 | |
| 611 | const auto AbbrevIt = Abbrevs.find_as(AbbrevCode); |
| 612 | if (AbbrevIt == Abbrevs.end()) |
Pavel Labath | c9f07b0 | 2018-04-06 13:34:12 +0000 | [diff] [blame] | 613 | return make_error<StringError>("Invalid abbreviation.", |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 614 | inconvertibleErrorCode()); |
| 615 | |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 616 | Entry E(*this, *AbbrevIt); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 617 | |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 618 | dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32}; |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 619 | for (auto &Value : E.Values) { |
| 620 | if (!Value.extractValue(AS, Offset, FormParams)) |
Pavel Labath | c9f07b0 | 2018-04-06 13:34:12 +0000 | [diff] [blame] | 621 | return make_error<StringError>("Error extracting index attribute values.", |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 622 | inconvertibleErrorCode()); |
| 623 | } |
| 624 | return std::move(E); |
| 625 | } |
| 626 | |
| 627 | DWARFDebugNames::NameTableEntry |
| 628 | DWARFDebugNames::NameIndex::getNameTableEntry(uint32_t Index) const { |
| 629 | assert(0 < Index && Index <= Hdr.NameCount); |
| 630 | uint32_t StringOffsetOffset = StringOffsetsBase + 4 * (Index - 1); |
| 631 | uint32_t EntryOffsetOffset = EntryOffsetsBase + 4 * (Index - 1); |
| 632 | const DWARFDataExtractor &AS = Section.AccelSection; |
| 633 | |
| 634 | uint32_t StringOffset = AS.getRelocatedValue(4, &StringOffsetOffset); |
| 635 | uint32_t EntryOffset = AS.getU32(&EntryOffsetOffset); |
| 636 | EntryOffset += EntriesBase; |
| 637 | return {StringOffset, EntryOffset}; |
| 638 | } |
| 639 | |
| 640 | uint32_t |
| 641 | DWARFDebugNames::NameIndex::getBucketArrayEntry(uint32_t Bucket) const { |
| 642 | assert(Bucket < Hdr.BucketCount); |
| 643 | uint32_t BucketOffset = BucketsBase + 4 * Bucket; |
| 644 | return Section.AccelSection.getU32(&BucketOffset); |
| 645 | } |
| 646 | |
| 647 | uint32_t DWARFDebugNames::NameIndex::getHashArrayEntry(uint32_t Index) const { |
| 648 | assert(0 < Index && Index <= Hdr.NameCount); |
| 649 | uint32_t HashOffset = HashesBase + 4 * (Index - 1); |
| 650 | return Section.AccelSection.getU32(&HashOffset); |
| 651 | } |
| 652 | |
| 653 | // Returns true if we should continue scanning for entries, false if this is the |
| 654 | // last (sentinel) entry). In case of a parsing error we also return false, as |
| 655 | // it's not possible to recover this entry list (but the other lists may still |
| 656 | // parse OK). |
| 657 | bool DWARFDebugNames::NameIndex::dumpEntry(ScopedPrinter &W, |
| 658 | uint32_t *Offset) const { |
| 659 | uint32_t EntryId = *Offset; |
| 660 | auto EntryOr = getEntry(Offset); |
| 661 | if (!EntryOr) { |
| 662 | handleAllErrors(EntryOr.takeError(), [](const SentinelError &) {}, |
| 663 | [&W](const ErrorInfoBase &EI) { EI.log(W.startLine()); }); |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | DictScope EntryScope(W, ("Entry @ 0x" + Twine::utohexstr(EntryId)).str()); |
| 668 | EntryOr->dump(W); |
| 669 | return true; |
| 670 | } |
| 671 | |
| 672 | void DWARFDebugNames::NameIndex::dumpName(ScopedPrinter &W, uint32_t Index, |
| 673 | Optional<uint32_t> Hash) const { |
| 674 | const DataExtractor &SS = Section.StringSection; |
| 675 | NameTableEntry NTE = getNameTableEntry(Index); |
| 676 | |
| 677 | DictScope NameScope(W, ("Name " + Twine(Index)).str()); |
| 678 | if (Hash) |
| 679 | W.printHex("Hash", *Hash); |
| 680 | |
| 681 | W.startLine() << format("String: 0x%08x", NTE.StringOffset); |
| 682 | W.getOStream() << " \"" << SS.getCStr(&NTE.StringOffset) << "\"\n"; |
| 683 | |
| 684 | while (dumpEntry(W, &NTE.EntryOffset)) |
| 685 | /*empty*/; |
| 686 | } |
| 687 | |
| 688 | void DWARFDebugNames::NameIndex::dumpCUs(ScopedPrinter &W) const { |
| 689 | ListScope CUScope(W, "Compilation Unit offsets"); |
| 690 | for (uint32_t CU = 0; CU < Hdr.CompUnitCount; ++CU) |
| 691 | W.startLine() << format("CU[%u]: 0x%08x\n", CU, getCUOffset(CU)); |
| 692 | } |
| 693 | |
| 694 | void DWARFDebugNames::NameIndex::dumpLocalTUs(ScopedPrinter &W) const { |
| 695 | if (Hdr.LocalTypeUnitCount == 0) |
| 696 | return; |
| 697 | |
| 698 | ListScope TUScope(W, "Local Type Unit offsets"); |
| 699 | for (uint32_t TU = 0; TU < Hdr.LocalTypeUnitCount; ++TU) |
| 700 | W.startLine() << format("LocalTU[%u]: 0x%08x\n", TU, getLocalTUOffset(TU)); |
| 701 | } |
| 702 | |
| 703 | void DWARFDebugNames::NameIndex::dumpForeignTUs(ScopedPrinter &W) const { |
| 704 | if (Hdr.ForeignTypeUnitCount == 0) |
| 705 | return; |
| 706 | |
| 707 | ListScope TUScope(W, "Foreign Type Unit signatures"); |
| 708 | for (uint32_t TU = 0; TU < Hdr.ForeignTypeUnitCount; ++TU) { |
| 709 | W.startLine() << format("ForeignTU[%u]: 0x%016" PRIx64 "\n", TU, |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 710 | getForeignTUSignature(TU)); |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
| 714 | void DWARFDebugNames::NameIndex::dumpAbbreviations(ScopedPrinter &W) const { |
| 715 | ListScope AbbrevsScope(W, "Abbreviations"); |
| 716 | for (const auto &Abbr : Abbrevs) |
| 717 | Abbr.dump(W); |
| 718 | } |
| 719 | |
| 720 | void DWARFDebugNames::NameIndex::dumpBucket(ScopedPrinter &W, |
| 721 | uint32_t Bucket) const { |
| 722 | ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str()); |
| 723 | uint32_t Index = getBucketArrayEntry(Bucket); |
| 724 | if (Index == 0) { |
| 725 | W.printString("EMPTY"); |
| 726 | return; |
| 727 | } |
| 728 | if (Index > Hdr.NameCount) { |
| 729 | W.printString("Name index is invalid"); |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | for (; Index <= Hdr.NameCount; ++Index) { |
| 734 | uint32_t Hash = getHashArrayEntry(Index); |
| 735 | if (Hash % Hdr.BucketCount != Bucket) |
| 736 | break; |
| 737 | |
| 738 | dumpName(W, Index, Hash); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | LLVM_DUMP_METHOD void DWARFDebugNames::NameIndex::dump(ScopedPrinter &W) const { |
| 743 | DictScope UnitScope(W, ("Name Index @ 0x" + Twine::utohexstr(Base)).str()); |
| 744 | Hdr.dump(W); |
| 745 | dumpCUs(W); |
| 746 | dumpLocalTUs(W); |
| 747 | dumpForeignTUs(W); |
| 748 | dumpAbbreviations(W); |
| 749 | |
| 750 | if (Hdr.BucketCount > 0) { |
| 751 | for (uint32_t Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) |
| 752 | dumpBucket(W, Bucket); |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | W.startLine() << "Hash table not present\n"; |
| 757 | for (uint32_t Index = 1; Index <= Hdr.NameCount; ++Index) |
| 758 | dumpName(W, Index, None); |
| 759 | } |
| 760 | |
| 761 | llvm::Error DWARFDebugNames::extract() { |
| 762 | uint32_t Offset = 0; |
| 763 | while (AccelSection.isValidOffset(Offset)) { |
| 764 | NameIndex Next(*this, Offset); |
| 765 | if (llvm::Error E = Next.extract()) |
| 766 | return E; |
| 767 | Offset = Next.getNextUnitOffset(); |
| 768 | NameIndices.push_back(std::move(Next)); |
| 769 | } |
| 770 | return Error::success(); |
| 771 | } |
| 772 | |
Pavel Labath | 80827f1 | 2018-05-15 13:24:10 +0000 | [diff] [blame] | 773 | iterator_range<DWARFDebugNames::ValueIterator> |
| 774 | DWARFDebugNames::NameIndex::equal_range(StringRef Key) const { |
| 775 | return make_range(ValueIterator(*this, Key), ValueIterator()); |
| 776 | } |
| 777 | |
Pavel Labath | 3c9a918 | 2018-01-29 11:08:32 +0000 | [diff] [blame] | 778 | LLVM_DUMP_METHOD void DWARFDebugNames::dump(raw_ostream &OS) const { |
| 779 | ScopedPrinter W(OS); |
| 780 | for (const NameIndex &NI : NameIndices) |
| 781 | NI.dump(W); |
| 782 | } |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 783 | |
| 784 | Optional<uint32_t> |
| 785 | DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() { |
| 786 | const Header &Hdr = CurrentIndex->Hdr; |
| 787 | if (Hdr.BucketCount == 0) { |
| 788 | // No Hash Table, We need to search through all names in the Name Index. |
| 789 | for (uint32_t Index = 1; Index <= Hdr.NameCount; ++Index) { |
| 790 | NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index); |
| 791 | if (CurrentIndex->Section.StringSection.getCStr(&NTE.StringOffset) == Key) |
| 792 | return NTE.EntryOffset; |
| 793 | } |
| 794 | return None; |
| 795 | } |
| 796 | |
| 797 | // The Name Index has a Hash Table, so use that to speed up the search. |
| 798 | // Compute the Key Hash, if it has not been done already. |
| 799 | if (!Hash) |
| 800 | Hash = caseFoldingDjbHash(Key); |
| 801 | uint32_t Bucket = *Hash % Hdr.BucketCount; |
| 802 | uint32_t Index = CurrentIndex->getBucketArrayEntry(Bucket); |
| 803 | if (Index == 0) |
| 804 | return None; // Empty bucket |
| 805 | |
| 806 | for (; Index <= Hdr.NameCount; ++Index) { |
| 807 | uint32_t Hash = CurrentIndex->getHashArrayEntry(Index); |
| 808 | if (Hash % Hdr.BucketCount != Bucket) |
| 809 | return None; // End of bucket |
| 810 | |
| 811 | NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index); |
| 812 | if (CurrentIndex->Section.StringSection.getCStr(&NTE.StringOffset) == Key) |
| 813 | return NTE.EntryOffset; |
| 814 | } |
| 815 | return None; |
| 816 | } |
| 817 | |
| 818 | bool DWARFDebugNames::ValueIterator::getEntryAtCurrentOffset() { |
| 819 | auto EntryOr = CurrentIndex->getEntry(&DataOffset); |
| 820 | if (!EntryOr) { |
| 821 | consumeError(EntryOr.takeError()); |
| 822 | return false; |
| 823 | } |
| 824 | CurrentEntry = std::move(*EntryOr); |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | bool DWARFDebugNames::ValueIterator::findInCurrentIndex() { |
| 829 | Optional<uint32_t> Offset = findEntryOffsetInCurrentIndex(); |
| 830 | if (!Offset) |
| 831 | return false; |
| 832 | DataOffset = *Offset; |
| 833 | return getEntryAtCurrentOffset(); |
| 834 | } |
| 835 | |
| 836 | void DWARFDebugNames::ValueIterator::searchFromStartOfCurrentIndex() { |
| 837 | for (const NameIndex *End = CurrentIndex->Section.NameIndices.end(); |
| 838 | CurrentIndex != End; ++CurrentIndex) { |
| 839 | if (findInCurrentIndex()) |
| 840 | return; |
| 841 | } |
| 842 | setEnd(); |
| 843 | } |
| 844 | |
| 845 | void DWARFDebugNames::ValueIterator::next() { |
| 846 | assert(CurrentIndex && "Incrementing an end() iterator?"); |
| 847 | |
| 848 | // First try the next entry in the current Index. |
| 849 | if (getEntryAtCurrentOffset()) |
| 850 | return; |
| 851 | |
Pavel Labath | 80827f1 | 2018-05-15 13:24:10 +0000 | [diff] [blame] | 852 | // If we're a local iterator, we're done. |
| 853 | if (IsLocal) { |
| 854 | setEnd(); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | // Otherwise, try the next index. |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 859 | ++CurrentIndex; |
| 860 | searchFromStartOfCurrentIndex(); |
| 861 | } |
| 862 | |
| 863 | DWARFDebugNames::ValueIterator::ValueIterator(const DWARFDebugNames &AccelTable, |
| 864 | StringRef Key) |
Pavel Labath | 80827f1 | 2018-05-15 13:24:10 +0000 | [diff] [blame] | 865 | : CurrentIndex(AccelTable.NameIndices.begin()), IsLocal(false), Key(Key) { |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 866 | searchFromStartOfCurrentIndex(); |
| 867 | } |
| 868 | |
Pavel Labath | 80827f1 | 2018-05-15 13:24:10 +0000 | [diff] [blame] | 869 | DWARFDebugNames::ValueIterator::ValueIterator( |
| 870 | const DWARFDebugNames::NameIndex &NI, StringRef Key) |
| 871 | : CurrentIndex(&NI), IsLocal(true), Key(Key) { |
| 872 | if (!findInCurrentIndex()) |
| 873 | setEnd(); |
| 874 | } |
| 875 | |
Pavel Labath | d99072b | 2018-02-24 00:35:21 +0000 | [diff] [blame] | 876 | iterator_range<DWARFDebugNames::ValueIterator> |
| 877 | DWARFDebugNames::equal_range(StringRef Key) const { |
| 878 | if (NameIndices.empty()) |
| 879 | return make_range(ValueIterator(), ValueIterator()); |
| 880 | return make_range(ValueIterator(*this, Key), ValueIterator()); |
| 881 | } |
Pavel Labath | 80827f1 | 2018-05-15 13:24:10 +0000 | [diff] [blame] | 882 | |
| 883 | const DWARFDebugNames::NameIndex * |
| 884 | DWARFDebugNames::getCUNameIndex(uint32_t CUOffset) { |
| 885 | if (CUToNameIndex.size() == 0 && NameIndices.size() > 0) { |
| 886 | for (const auto &NI : *this) { |
| 887 | for (uint32_t CU = 0; CU < NI.getCUCount(); ++CU) |
| 888 | CUToNameIndex.try_emplace(NI.getCUOffset(CU), &NI); |
| 889 | } |
| 890 | } |
| 891 | return CUToNameIndex.lookup(CUOffset); |
| 892 | } |