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