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" |
George Rimar | f8a9642 | 2017-04-21 09:12:18 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Format.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 19 | #include <cstddef> |
| 20 | #include <cstdint> |
| 21 | #include <utility> |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 22 | |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 24 | |
| 25 | bool DWARFAcceleratorTable::extract() { |
| 26 | uint32_t Offset = 0; |
| 27 | |
| 28 | // Check that we can at least read the header. |
| 29 | if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4)) |
| 30 | return false; |
| 31 | |
| 32 | Hdr.Magic = AccelSection.getU32(&Offset); |
| 33 | Hdr.Version = AccelSection.getU16(&Offset); |
| 34 | Hdr.HashFunction = AccelSection.getU16(&Offset); |
| 35 | Hdr.NumBuckets = AccelSection.getU32(&Offset); |
| 36 | Hdr.NumHashes = AccelSection.getU32(&Offset); |
| 37 | Hdr.HeaderDataLength = AccelSection.getU32(&Offset); |
| 38 | |
| 39 | // Check that we can read all the hashes and offsets from the |
| 40 | // section (see SourceLevelDebugging.rst for the structure of the index). |
| 41 | if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength + |
| 42 | Hdr.NumBuckets*4 + Hdr.NumHashes*8)) |
| 43 | return false; |
| 44 | |
| 45 | HdrData.DIEOffsetBase = AccelSection.getU32(&Offset); |
| 46 | uint32_t NumAtoms = AccelSection.getU32(&Offset); |
| 47 | |
| 48 | for (unsigned i = 0; i < NumAtoms; ++i) { |
| 49 | uint16_t AtomType = AccelSection.getU16(&Offset); |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 50 | auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset)); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 51 | HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm)); |
| 52 | } |
| 53 | |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame^] | 54 | IsValid = true; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 58 | uint32_t DWARFAcceleratorTable::getNumBuckets() { return Hdr.NumBuckets; } |
| 59 | uint32_t DWARFAcceleratorTable::getNumHashes() { return Hdr.NumHashes; } |
| 60 | uint32_t DWARFAcceleratorTable::getSizeHdr() { return sizeof(Hdr); } |
| 61 | uint32_t DWARFAcceleratorTable::getHeaderDataLength() { |
| 62 | return Hdr.HeaderDataLength; |
| 63 | } |
| 64 | |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 65 | ArrayRef<std::pair<DWARFAcceleratorTable::HeaderData::AtomType, |
| 66 | DWARFAcceleratorTable::HeaderData::Form>> |
| 67 | DWARFAcceleratorTable::getAtomsDesc() { |
| 68 | return HdrData.Atoms; |
| 69 | } |
| 70 | |
| 71 | bool DWARFAcceleratorTable::validateForms() { |
| 72 | for (auto Atom : getAtomsDesc()) { |
| 73 | DWARFFormValue FormValue(Atom.second); |
| 74 | switch (Atom.first) { |
| 75 | case dwarf::DW_ATOM_die_offset: |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 76 | case dwarf::DW_ATOM_die_tag: |
| 77 | case dwarf::DW_ATOM_type_flags: |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 78 | if ((!FormValue.isFormClass(DWARFFormValue::FC_Constant) && |
| 79 | !FormValue.isFormClass(DWARFFormValue::FC_Flag)) || |
| 80 | FormValue.getForm() == dwarf::DW_FORM_sdata) |
| 81 | return false; |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 89 | std::pair<uint32_t, dwarf::Tag> |
| 90 | DWARFAcceleratorTable::readAtoms(uint32_t &HashDataOffset) { |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 91 | uint32_t DieOffset = dwarf::DW_INVALID_OFFSET; |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 92 | dwarf::Tag DieTag = dwarf::DW_TAG_null; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 93 | |
| 94 | for (auto Atom : getAtomsDesc()) { |
| 95 | DWARFFormValue FormValue(Atom.second); |
| 96 | FormValue.extractValue(AccelSection, &HashDataOffset, NULL); |
| 97 | switch (Atom.first) { |
| 98 | case dwarf::DW_ATOM_die_offset: |
| 99 | DieOffset = *FormValue.getAsUnsignedConstant(); |
| 100 | break; |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 101 | case dwarf::DW_ATOM_die_tag: |
| 102 | DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant(); |
| 103 | break; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | } |
Spyridoula Gravani | 70d35e1 | 2017-07-31 18:01:16 +0000 | [diff] [blame] | 108 | return {DieOffset, DieTag}; |
Spyridoula Gravani | 837c110 | 2017-06-29 20:13:05 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 111 | LLVM_DUMP_METHOD void DWARFAcceleratorTable::dump(raw_ostream &OS) const { |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame^] | 112 | if (!IsValid) |
| 113 | return; |
| 114 | |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 115 | // Dump the header. |
| 116 | OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n' |
| 117 | << "Version = " << format("0x%04x", Hdr.Version) << '\n' |
| 118 | << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n' |
| 119 | << "Bucket count = " << Hdr.NumBuckets << '\n' |
| 120 | << "Hashes count = " << Hdr.NumHashes << '\n' |
| 121 | << "HeaderData length = " << Hdr.HeaderDataLength << '\n' |
| 122 | << "DIE offset base = " << HdrData.DIEOffsetBase << '\n' |
| 123 | << "Number of atoms = " << HdrData.Atoms.size() << '\n'; |
| 124 | |
| 125 | unsigned i = 0; |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 126 | SmallVector<DWARFFormValue, 3> AtomForms; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 127 | for (const auto &Atom: HdrData.Atoms) { |
| 128 | OS << format("Atom[%d] Type: ", i++); |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 129 | auto TypeString = dwarf::AtomTypeString(Atom.first); |
| 130 | if (!TypeString.empty()) |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 131 | OS << TypeString; |
| 132 | else |
| 133 | OS << format("DW_ATOM_Unknown_0x%x", Atom.first); |
| 134 | OS << " Form: "; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 135 | auto FormString = dwarf::FormEncodingString(Atom.second); |
| 136 | if (!FormString.empty()) |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 137 | OS << FormString; |
| 138 | else |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 139 | OS << format("DW_FORM_Unknown_0x%x", Atom.second); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 140 | OS << '\n'; |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 141 | AtomForms.push_back(DWARFFormValue(Atom.second)); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Now go through the actual tables and dump them. |
| 145 | uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength; |
| 146 | unsigned HashesBase = Offset + Hdr.NumBuckets * 4; |
| 147 | unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4; |
| 148 | |
| 149 | for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) { |
| 150 | unsigned Index = AccelSection.getU32(&Offset); |
| 151 | |
| 152 | OS << format("Bucket[%d]\n", Bucket); |
| 153 | if (Index == UINT32_MAX) { |
| 154 | OS << " EMPTY\n"; |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) { |
| 159 | unsigned HashOffset = HashesBase + HashIdx*4; |
| 160 | unsigned OffsetsOffset = OffsetsBase + HashIdx*4; |
| 161 | uint32_t Hash = AccelSection.getU32(&HashOffset); |
| 162 | |
| 163 | if (Hash % Hdr.NumBuckets != Bucket) |
| 164 | break; |
| 165 | |
| 166 | unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); |
| 167 | OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset); |
| 168 | if (!AccelSection.isValidOffset(DataOffset)) { |
| 169 | OS << " Invalid section offset\n"; |
| 170 | continue; |
| 171 | } |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 172 | while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) { |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 173 | unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset); |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 174 | if (!StringOffset) |
| 175 | break; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 176 | OS << format(" Name: %08x \"%s\"\n", StringOffset, |
| 177 | StringSection.getCStr(&StringOffset)); |
| 178 | unsigned NumData = AccelSection.getU32(&DataOffset); |
| 179 | for (unsigned Data = 0; Data < NumData; ++Data) { |
| 180 | OS << format(" Data[%d] => ", Data); |
| 181 | unsigned i = 0; |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 182 | for (auto &Atom : AtomForms) { |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 183 | OS << format("{Atom[%d]: ", i++); |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 184 | if (Atom.extractValue(AccelSection, &DataOffset, nullptr)) |
Greg Clayton | cddab27 | 2016-10-31 16:46:02 +0000 | [diff] [blame] | 185 | Atom.dump(OS); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 186 | else |
| 187 | OS << "Error extracting the value"; |
| 188 | OS << "} "; |
| 189 | } |
| 190 | OS << '\n'; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
Adrian Prantl | 99fdb9d | 2017-09-28 18:10:52 +0000 | [diff] [blame^] | 196 | |
| 197 | DWARFAcceleratorTable::ValueIterator::ValueIterator( |
| 198 | const DWARFAcceleratorTable &AccelTable, unsigned Offset) |
| 199 | : AccelTable(&AccelTable), DataOffset(Offset) { |
| 200 | if (!AccelTable.AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) |
| 201 | return; |
| 202 | |
| 203 | for (const auto &Atom : AccelTable.HdrData.Atoms) |
| 204 | AtomForms.push_back(DWARFFormValue(Atom.second)); |
| 205 | |
| 206 | // Read the first entry. |
| 207 | NumData = AccelTable.AccelSection.getU32(&DataOffset); |
| 208 | Next(); |
| 209 | } |
| 210 | |
| 211 | void DWARFAcceleratorTable::ValueIterator::Next() { |
| 212 | assert(NumData > 0 && "attempted to increment iterator past the end"); |
| 213 | auto &AccelSection = AccelTable->AccelSection; |
| 214 | if (Data >= NumData || |
| 215 | !AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) { |
| 216 | NumData = 0; |
| 217 | return; |
| 218 | } |
| 219 | for (auto &Atom : AtomForms) |
| 220 | Atom.extractValue(AccelSection, &DataOffset, nullptr); |
| 221 | ++Data; |
| 222 | } |
| 223 | |
| 224 | iterator_range<DWARFAcceleratorTable::ValueIterator> |
| 225 | DWARFAcceleratorTable::equal_range(StringRef Key) const { |
| 226 | if (!IsValid) |
| 227 | return make_range(ValueIterator(), ValueIterator()); |
| 228 | |
| 229 | // Find the bucket. |
| 230 | unsigned HashValue = dwarf::djbHash(Key); |
| 231 | unsigned Bucket = HashValue % Hdr.NumBuckets; |
| 232 | unsigned BucketBase = sizeof(Hdr) + Hdr.HeaderDataLength; |
| 233 | unsigned HashesBase = BucketBase + Hdr.NumBuckets * 4; |
| 234 | unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4; |
| 235 | |
| 236 | unsigned BucketOffset = BucketBase + Bucket * 4; |
| 237 | unsigned Index = AccelSection.getU32(&BucketOffset); |
| 238 | |
| 239 | // Search through all hashes in the bucket. |
| 240 | for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) { |
| 241 | unsigned HashOffset = HashesBase + HashIdx * 4; |
| 242 | unsigned OffsetsOffset = OffsetsBase + HashIdx * 4; |
| 243 | uint32_t Hash = AccelSection.getU32(&HashOffset); |
| 244 | |
| 245 | if (Hash % Hdr.NumBuckets != Bucket) |
| 246 | // We are already in the next bucket. |
| 247 | break; |
| 248 | |
| 249 | unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); |
| 250 | unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset); |
| 251 | if (!StringOffset) |
| 252 | break; |
| 253 | |
| 254 | // Finally, compare the key. |
| 255 | if (Key == StringSection.getCStr(&StringOffset)) |
| 256 | return make_range({*this, DataOffset}, ValueIterator()); |
| 257 | } |
| 258 | return make_range(ValueIterator(), ValueIterator()); |
| 259 | } |