1 //===--- DWARFAcceleratorTable.cpp ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/DWARFAcceleratorTable.h"
12 #include "llvm/Support/Dwarf.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
18 bool DWARFAcceleratorTable::extract() {
21 // Check that we can at least read the header.
22 if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
25 Hdr.Magic = AccelSection.getU32(&Offset);
26 Hdr.Version = AccelSection.getU16(&Offset);
27 Hdr.HashFunction = AccelSection.getU16(&Offset);
28 Hdr.NumBuckets = AccelSection.getU32(&Offset);
29 Hdr.NumHashes = AccelSection.getU32(&Offset);
30 Hdr.HeaderDataLength = AccelSection.getU32(&Offset);
32 // Check that we can read all the hashes and offsets from the
33 // section (see SourceLevelDebugging.rst for the structure of the index).
34 if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
35 Hdr.NumBuckets*4 + Hdr.NumHashes*8))
38 HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
39 uint32_t NumAtoms = AccelSection.getU32(&Offset);
41 for (unsigned i = 0; i < NumAtoms; ++i) {
42 uint16_t AtomType = AccelSection.getU16(&Offset);
43 uint16_t AtomForm = AccelSection.getU16(&Offset);
44 HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
50 void DWARFAcceleratorTable::dump(raw_ostream &OS) const {
52 OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n'
53 << "Version = " << format("0x%04x", Hdr.Version) << '\n'
54 << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n'
55 << "Bucket count = " << Hdr.NumBuckets << '\n'
56 << "Hashes count = " << Hdr.NumHashes << '\n'
57 << "HeaderData length = " << Hdr.HeaderDataLength << '\n'
58 << "DIE offset base = " << HdrData.DIEOffsetBase << '\n'
59 << "Number of atoms = " << HdrData.Atoms.size() << '\n';
62 SmallVector<DWARFFormValue, 3> AtomForms;
63 for (const auto &Atom: HdrData.Atoms) {
64 OS << format("Atom[%d] Type: ", i++);
65 if (const char *TypeString = dwarf::AtomTypeString(Atom.first))
68 OS << format("DW_ATOM_Unknown_0x%x", Atom.first);
70 if (const char *FormString = dwarf::FormEncodingString(Atom.second))
73 OS << format("DW_FORM_Unknown_0x%x", Atom.second);
75 AtomForms.push_back(DWARFFormValue(Atom.second));
78 // Now go through the actual tables and dump them.
79 uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
80 unsigned HashesBase = Offset + Hdr.NumBuckets * 4;
81 unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4;
83 for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) {
84 unsigned Index = AccelSection.getU32(&Offset);
86 OS << format("Bucket[%d]\n", Bucket);
87 if (Index == UINT32_MAX) {
92 for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) {
93 unsigned HashOffset = HashesBase + HashIdx*4;
94 unsigned OffsetsOffset = OffsetsBase + HashIdx*4;
95 uint32_t Hash = AccelSection.getU32(&HashOffset);
97 if (Hash % Hdr.NumBuckets != Bucket)
100 unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
101 OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset);
102 if (!AccelSection.isValidOffset(DataOffset)) {
103 OS << " Invalid section offset\n";
106 while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
107 unsigned StringOffset = AccelSection.getU32(&DataOffset);
108 RelocAddrMap::const_iterator Reloc = Relocs.find(DataOffset-4);
109 if (Reloc != Relocs.end())
110 StringOffset += Reloc->second.second;
113 OS << format(" Name: %08x \"%s\"\n", StringOffset,
114 StringSection.getCStr(&StringOffset));
115 unsigned NumData = AccelSection.getU32(&DataOffset);
116 for (unsigned Data = 0; Data < NumData; ++Data) {
117 OS << format(" Data[%d] => ", Data);
119 for (auto &Atom : AtomForms) {
120 OS << format("{Atom[%d]: ", i++);
121 if (Atom.extractValue(AccelSection, &DataOffset, nullptr))
122 Atom.dump(OS, nullptr);
124 OS << "Error extracting the value";