1 //===- DWARFAcceleratorTable.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 10 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" 11 12 #include "llvm/ADT/SmallVector.h" 13 #include "llvm/BinaryFormat/Dwarf.h" 14 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 15 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" 16 #include "llvm/Support/Compiler.h" 17 #include "llvm/Support/Format.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include <cstddef> 20 #include <cstdint> 21 #include <utility> 22 23 using namespace llvm; 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); 50 auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset)); 51 HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm)); 52 } 53 54 IsValid = true; 55 return true; 56 } 57 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 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: 76 case dwarf::DW_ATOM_die_tag: 77 case dwarf::DW_ATOM_type_flags: 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 89 std::pair<uint32_t, dwarf::Tag> 90 DWARFAcceleratorTable::readAtoms(uint32_t &HashDataOffset) { 91 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET; 92 dwarf::Tag DieTag = dwarf::DW_TAG_null; 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; 101 case dwarf::DW_ATOM_die_tag: 102 DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant(); 103 break; 104 default: 105 break; 106 } 107 } 108 return {DieOffset, DieTag}; 109 } 110 111 LLVM_DUMP_METHOD void DWARFAcceleratorTable::dump(raw_ostream &OS) const { 112 if (!IsValid) 113 return; 114 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; 126 SmallVector<DWARFFormValue, 3> AtomForms; 127 for (const auto &Atom: HdrData.Atoms) { 128 OS << format("Atom[%d] Type: ", i++); 129 auto TypeString = dwarf::AtomTypeString(Atom.first); 130 if (!TypeString.empty()) 131 OS << TypeString; 132 else 133 OS << format("DW_ATOM_Unknown_0x%x", Atom.first); 134 OS << " Form: "; 135 auto FormString = dwarf::FormEncodingString(Atom.second); 136 if (!FormString.empty()) 137 OS << FormString; 138 else 139 OS << format("DW_FORM_Unknown_0x%x", Atom.second); 140 OS << '\n'; 141 AtomForms.push_back(DWARFFormValue(Atom.second)); 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 } 172 while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) { 173 unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset); 174 if (!StringOffset) 175 break; 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; 182 for (auto &Atom : AtomForms) { 183 OS << format("{Atom[%d]: ", i++); 184 if (Atom.extractValue(AccelSection, &DataOffset, nullptr)) 185 Atom.dump(OS); 186 else 187 OS << "Error extracting the value"; 188 OS << "} "; 189 } 190 OS << '\n'; 191 } 192 } 193 } 194 } 195 } 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 } 260