703274de492f16fdc25ac5bbc8ec8896ba498e00
[oota-llvm.git] / lib / DebugInfo / DWARFAcceleratorTable.cpp
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 "DWARFAcceleratorTable.h"
11
12 #include "llvm/Support/Dwarf.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
15
16 namespace llvm {
17
18 bool DWARFAcceleratorTable::extract() {
19   uint32_t Offset = 0;
20
21   // Check that we can at least read the header.
22   if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
23     return false;
24
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);
31
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))
36     return false;
37
38   HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
39   uint32_t NumAtoms = AccelSection.getU32(&Offset);
40
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));
45   }
46
47   return true;
48 }
49
50 void DWARFAcceleratorTable::dump(raw_ostream &OS) const {
51   // Dump the header.
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';
60
61   unsigned i = 0;
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))
66       OS << TypeString;
67     else
68       OS << format("DW_ATOM_Unknown_0x%x", Atom.first);
69     OS << " Form: ";
70     if (const char *FormString = dwarf::FormEncodingString(Atom.second))
71       OS << FormString;
72     else
73       OS << format("DW_FORM_Unknown_0x%x", Atom.second);
74     OS << '\n';
75     AtomForms.push_back(DWARFFormValue(Atom.second));
76   }
77
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;
82
83   for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) {
84     unsigned Index = AccelSection.getU32(&Offset);
85
86     OS << format("Bucket[%d]\n", Bucket);
87     if (Index == UINT32_MAX) {
88       OS << "  EMPTY\n";
89       continue;
90     }
91
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);
96
97       if (Hash % Hdr.NumBuckets != Bucket)
98         break;
99
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";
104         continue;
105       }
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;
111         if (!StringOffset)
112           break;
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);
118           unsigned i = 0;
119           for (auto &Atom : AtomForms) {
120             OS << format("{Atom[%d]: ", i++);
121             if (Atom.extractValue(AccelSection, &DataOffset, nullptr))
122               Atom.dump(OS, nullptr);
123             else
124               OS << "Error extracting the value";
125             OS << "} ";
126           }
127           OS << '\n';
128         }
129       }
130     }
131   }
132 }
133 }