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/DWARFFormValue.h" 16 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" 17 #include "llvm/Support/Compiler.h" 18 #include "llvm/Support/Format.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <cstddef> 21 #include <cstdint> 22 #include <utility> 23 24 using namespace llvm; 25 26 bool DWARFAcceleratorTable::extract() { 27 uint32_t Offset = 0; 28 29 // Check that we can at least read the header. 30 if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4)) 31 return false; 32 33 Hdr.Magic = AccelSection.getU32(&Offset); 34 Hdr.Version = AccelSection.getU16(&Offset); 35 Hdr.HashFunction = AccelSection.getU16(&Offset); 36 Hdr.NumBuckets = AccelSection.getU32(&Offset); 37 Hdr.NumHashes = AccelSection.getU32(&Offset); 38 Hdr.HeaderDataLength = AccelSection.getU32(&Offset); 39 40 // Check that we can read all the hashes and offsets from the 41 // section (see SourceLevelDebugging.rst for the structure of the index). 42 if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength + 43 Hdr.NumBuckets*4 + Hdr.NumHashes*8)) 44 return false; 45 46 HdrData.DIEOffsetBase = AccelSection.getU32(&Offset); 47 uint32_t NumAtoms = AccelSection.getU32(&Offset); 48 49 for (unsigned i = 0; i < NumAtoms; ++i) { 50 uint16_t AtomType = AccelSection.getU16(&Offset); 51 auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset)); 52 HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm)); 53 } 54 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 LLVM_DUMP_METHOD void DWARFAcceleratorTable::dump(raw_ostream &OS) const { 66 // Dump the header. 67 OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n' 68 << "Version = " << format("0x%04x", Hdr.Version) << '\n' 69 << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n' 70 << "Bucket count = " << Hdr.NumBuckets << '\n' 71 << "Hashes count = " << Hdr.NumHashes << '\n' 72 << "HeaderData length = " << Hdr.HeaderDataLength << '\n' 73 << "DIE offset base = " << HdrData.DIEOffsetBase << '\n' 74 << "Number of atoms = " << HdrData.Atoms.size() << '\n'; 75 76 unsigned i = 0; 77 SmallVector<DWARFFormValue, 3> AtomForms; 78 for (const auto &Atom: HdrData.Atoms) { 79 OS << format("Atom[%d] Type: ", i++); 80 auto TypeString = dwarf::AtomTypeString(Atom.first); 81 if (!TypeString.empty()) 82 OS << TypeString; 83 else 84 OS << format("DW_ATOM_Unknown_0x%x", Atom.first); 85 OS << " Form: "; 86 auto FormString = dwarf::FormEncodingString(Atom.second); 87 if (!FormString.empty()) 88 OS << FormString; 89 else 90 OS << format("DW_FORM_Unknown_0x%x", Atom.second); 91 OS << '\n'; 92 AtomForms.push_back(DWARFFormValue(Atom.second)); 93 } 94 95 // Now go through the actual tables and dump them. 96 uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength; 97 unsigned HashesBase = Offset + Hdr.NumBuckets * 4; 98 unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4; 99 100 for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) { 101 unsigned Index = AccelSection.getU32(&Offset); 102 103 OS << format("Bucket[%d]\n", Bucket); 104 if (Index == UINT32_MAX) { 105 OS << " EMPTY\n"; 106 continue; 107 } 108 109 for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) { 110 unsigned HashOffset = HashesBase + HashIdx*4; 111 unsigned OffsetsOffset = OffsetsBase + HashIdx*4; 112 uint32_t Hash = AccelSection.getU32(&HashOffset); 113 114 if (Hash % Hdr.NumBuckets != Bucket) 115 break; 116 117 unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); 118 OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset); 119 if (!AccelSection.isValidOffset(DataOffset)) { 120 OS << " Invalid section offset\n"; 121 continue; 122 } 123 while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) { 124 unsigned StringOffset = 125 getRelocatedValue(AccelSection, 4, &DataOffset, &Relocs); 126 if (!StringOffset) 127 break; 128 OS << format(" Name: %08x \"%s\"\n", StringOffset, 129 StringSection.getCStr(&StringOffset)); 130 unsigned NumData = AccelSection.getU32(&DataOffset); 131 for (unsigned Data = 0; Data < NumData; ++Data) { 132 OS << format(" Data[%d] => ", Data); 133 unsigned i = 0; 134 for (auto &Atom : AtomForms) { 135 OS << format("{Atom[%d]: ", i++); 136 if (Atom.extractValue(AccelSection, &DataOffset, nullptr)) 137 Atom.dump(OS); 138 else 139 OS << "Error extracting the value"; 140 OS << "} "; 141 } 142 OS << '\n'; 143 } 144 } 145 } 146 } 147 } 148