blob: 9aa3a2bf1bfa37d4802d5ef935bae126a56caba1 [file] [log] [blame]
Frederic Riss7c41c642014-11-20 16:21:06 +00001//===--- 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
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Frederic Risse837ec22014-11-14 16:15:53 +000011#include "llvm/Support/Dwarf.h"
12#include "llvm/Support/Format.h"
13#include "llvm/Support/raw_ostream.h"
14
15namespace llvm {
16
17bool DWARFAcceleratorTable::extract() {
18 uint32_t Offset = 0;
19
20 // Check that we can at least read the header.
21 if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
22 return false;
23
24 Hdr.Magic = AccelSection.getU32(&Offset);
25 Hdr.Version = AccelSection.getU16(&Offset);
26 Hdr.HashFunction = AccelSection.getU16(&Offset);
27 Hdr.NumBuckets = AccelSection.getU32(&Offset);
28 Hdr.NumHashes = AccelSection.getU32(&Offset);
29 Hdr.HeaderDataLength = AccelSection.getU32(&Offset);
30
31 // Check that we can read all the hashes and offsets from the
32 // section (see SourceLevelDebugging.rst for the structure of the index).
33 if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
34 Hdr.NumBuckets*4 + Hdr.NumHashes*8))
35 return false;
36
37 HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
38 uint32_t NumAtoms = AccelSection.getU32(&Offset);
39
40 for (unsigned i = 0; i < NumAtoms; ++i) {
41 uint16_t AtomType = AccelSection.getU16(&Offset);
Frederic Riss77a07432014-11-20 16:21:11 +000042 uint16_t AtomForm = AccelSection.getU16(&Offset);
Frederic Risse837ec22014-11-14 16:15:53 +000043 HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
44 }
45
46 return true;
47}
48
Frederic Riss77a07432014-11-20 16:21:11 +000049void DWARFAcceleratorTable::dump(raw_ostream &OS) const {
Frederic Risse837ec22014-11-14 16:15:53 +000050 // Dump the header.
51 OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n'
52 << "Version = " << format("0x%04x", Hdr.Version) << '\n'
53 << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n'
54 << "Bucket count = " << Hdr.NumBuckets << '\n'
55 << "Hashes count = " << Hdr.NumHashes << '\n'
56 << "HeaderData length = " << Hdr.HeaderDataLength << '\n'
57 << "DIE offset base = " << HdrData.DIEOffsetBase << '\n'
58 << "Number of atoms = " << HdrData.Atoms.size() << '\n';
59
60 unsigned i = 0;
Frederic Riss77a07432014-11-20 16:21:11 +000061 SmallVector<DWARFFormValue, 3> AtomForms;
Frederic Risse837ec22014-11-14 16:15:53 +000062 for (const auto &Atom: HdrData.Atoms) {
63 OS << format("Atom[%d] Type: ", i++);
Mehdi Amini32b297a2016-10-05 00:37:18 +000064 auto TypeString = dwarf::AtomTypeString(Atom.first);
65 if (!TypeString.empty())
Frederic Risse837ec22014-11-14 16:15:53 +000066 OS << TypeString;
67 else
68 OS << format("DW_ATOM_Unknown_0x%x", Atom.first);
69 OS << " Form: ";
Mehdi Amini32b297a2016-10-05 00:37:18 +000070 auto FormString = dwarf::FormEncodingString(Atom.second);
71 if (!FormString.empty())
Frederic Risse837ec22014-11-14 16:15:53 +000072 OS << FormString;
73 else
Frederic Riss77a07432014-11-20 16:21:11 +000074 OS << format("DW_FORM_Unknown_0x%x", Atom.second);
Frederic Risse837ec22014-11-14 16:15:53 +000075 OS << '\n';
Frederic Riss77a07432014-11-20 16:21:11 +000076 AtomForms.push_back(DWARFFormValue(Atom.second));
Frederic Risse837ec22014-11-14 16:15:53 +000077 }
78
79 // Now go through the actual tables and dump them.
80 uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
81 unsigned HashesBase = Offset + Hdr.NumBuckets * 4;
82 unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4;
83
84 for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) {
85 unsigned Index = AccelSection.getU32(&Offset);
86
87 OS << format("Bucket[%d]\n", Bucket);
88 if (Index == UINT32_MAX) {
89 OS << " EMPTY\n";
90 continue;
91 }
92
93 for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) {
94 unsigned HashOffset = HashesBase + HashIdx*4;
95 unsigned OffsetsOffset = OffsetsBase + HashIdx*4;
96 uint32_t Hash = AccelSection.getU32(&HashOffset);
97
98 if (Hash % Hdr.NumBuckets != Bucket)
99 break;
100
101 unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
102 OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset);
103 if (!AccelSection.isValidOffset(DataOffset)) {
104 OS << " Invalid section offset\n";
105 continue;
106 }
Frederic Riss7c500472014-11-14 19:30:08 +0000107 while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
108 unsigned StringOffset = AccelSection.getU32(&DataOffset);
109 RelocAddrMap::const_iterator Reloc = Relocs.find(DataOffset-4);
110 if (Reloc != Relocs.end())
111 StringOffset += Reloc->second.second;
112 if (!StringOffset)
113 break;
Frederic Risse837ec22014-11-14 16:15:53 +0000114 OS << format(" Name: %08x \"%s\"\n", StringOffset,
115 StringSection.getCStr(&StringOffset));
116 unsigned NumData = AccelSection.getU32(&DataOffset);
117 for (unsigned Data = 0; Data < NumData; ++Data) {
118 OS << format(" Data[%d] => ", Data);
119 unsigned i = 0;
Frederic Riss77a07432014-11-20 16:21:11 +0000120 for (auto &Atom : AtomForms) {
Frederic Risse837ec22014-11-14 16:15:53 +0000121 OS << format("{Atom[%d]: ", i++);
Frederic Riss77a07432014-11-20 16:21:11 +0000122 if (Atom.extractValue(AccelSection, &DataOffset, nullptr))
123 Atom.dump(OS, nullptr);
Frederic Risse837ec22014-11-14 16:15:53 +0000124 else
125 OS << "Error extracting the value";
126 OS << "} ";
127 }
128 OS << '\n';
129 }
130 }
131 }
132 }
133}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000134}