blob: dbe6fe52407dd9b7df4f38f9b7ceb014b6812e01 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFAcceleratorTable.cpp ------------------------------------------===//
Frederic Riss7c41c642014-11-20 16:21:06 +00002//
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"
Zachary Turner264b5d92017-06-07 03:48:56 +000011
Chandler Carruth6bda14b2017-06-06 11:49:48 +000012#include "llvm/ADT/SmallVector.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/Dwarf.h"
George Rimarf8a96422017-04-21 09:12:18 +000014#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000015#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000016#include "llvm/Support/Compiler.h"
Frederic Risse837ec22014-11-14 16:15:53 +000017#include "llvm/Support/Format.h"
18#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000019#include <cstddef>
20#include <cstdint>
21#include <utility>
Frederic Risse837ec22014-11-14 16:15:53 +000022
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023using namespace llvm;
Frederic Risse837ec22014-11-14 16:15:53 +000024
25bool DWARFAcceleratorTable::extract() {
26 uint32_t Offset = 0;
27
28 // Check that we can at least read the header.
29 if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
30 return false;
31
32 Hdr.Magic = AccelSection.getU32(&Offset);
33 Hdr.Version = AccelSection.getU16(&Offset);
34 Hdr.HashFunction = AccelSection.getU16(&Offset);
35 Hdr.NumBuckets = AccelSection.getU32(&Offset);
36 Hdr.NumHashes = AccelSection.getU32(&Offset);
37 Hdr.HeaderDataLength = AccelSection.getU32(&Offset);
38
39 // Check that we can read all the hashes and offsets from the
40 // section (see SourceLevelDebugging.rst for the structure of the index).
41 if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
42 Hdr.NumBuckets*4 + Hdr.NumHashes*8))
43 return false;
44
45 HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
46 uint32_t NumAtoms = AccelSection.getU32(&Offset);
47
48 for (unsigned i = 0; i < NumAtoms; ++i) {
49 uint16_t AtomType = AccelSection.getU16(&Offset);
Greg Clayton6c273762016-10-27 16:32:04 +000050 auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset));
Frederic Risse837ec22014-11-14 16:15:53 +000051 HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
52 }
53
Adrian Prantl99fdb9d2017-09-28 18:10:52 +000054 IsValid = true;
Frederic Risse837ec22014-11-14 16:15:53 +000055 return true;
56}
57
Spyridoula Gravanie41823b2017-06-14 00:17:55 +000058uint32_t DWARFAcceleratorTable::getNumBuckets() { return Hdr.NumBuckets; }
59uint32_t DWARFAcceleratorTable::getNumHashes() { return Hdr.NumHashes; }
60uint32_t DWARFAcceleratorTable::getSizeHdr() { return sizeof(Hdr); }
61uint32_t DWARFAcceleratorTable::getHeaderDataLength() {
62 return Hdr.HeaderDataLength;
63}
64
Spyridoula Gravani837c1102017-06-29 20:13:05 +000065ArrayRef<std::pair<DWARFAcceleratorTable::HeaderData::AtomType,
66 DWARFAcceleratorTable::HeaderData::Form>>
67DWARFAcceleratorTable::getAtomsDesc() {
68 return HdrData.Atoms;
69}
70
71bool DWARFAcceleratorTable::validateForms() {
72 for (auto Atom : getAtomsDesc()) {
73 DWARFFormValue FormValue(Atom.second);
74 switch (Atom.first) {
75 case dwarf::DW_ATOM_die_offset:
Spyridoula Gravani70d35e12017-07-31 18:01:16 +000076 case dwarf::DW_ATOM_die_tag:
77 case dwarf::DW_ATOM_type_flags:
Spyridoula Gravani837c1102017-06-29 20:13:05 +000078 if ((!FormValue.isFormClass(DWARFFormValue::FC_Constant) &&
79 !FormValue.isFormClass(DWARFFormValue::FC_Flag)) ||
80 FormValue.getForm() == dwarf::DW_FORM_sdata)
81 return false;
82 default:
83 break;
84 }
85 }
86 return true;
87}
88
Spyridoula Gravani70d35e12017-07-31 18:01:16 +000089std::pair<uint32_t, dwarf::Tag>
90DWARFAcceleratorTable::readAtoms(uint32_t &HashDataOffset) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +000091 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +000092 dwarf::Tag DieTag = dwarf::DW_TAG_null;
Spyridoula Gravani837c1102017-06-29 20:13:05 +000093
94 for (auto Atom : getAtomsDesc()) {
95 DWARFFormValue FormValue(Atom.second);
96 FormValue.extractValue(AccelSection, &HashDataOffset, NULL);
97 switch (Atom.first) {
98 case dwarf::DW_ATOM_die_offset:
99 DieOffset = *FormValue.getAsUnsignedConstant();
100 break;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000101 case dwarf::DW_ATOM_die_tag:
102 DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant();
103 break;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000104 default:
105 break;
106 }
107 }
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000108 return {DieOffset, DieTag};
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000109}
110
Matthias Braun8c209aa2017-01-28 02:02:38 +0000111LLVM_DUMP_METHOD void DWARFAcceleratorTable::dump(raw_ostream &OS) const {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000112 if (!IsValid)
113 return;
114
Frederic Risse837ec22014-11-14 16:15:53 +0000115 // Dump the header.
116 OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n'
117 << "Version = " << format("0x%04x", Hdr.Version) << '\n'
118 << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n'
119 << "Bucket count = " << Hdr.NumBuckets << '\n'
120 << "Hashes count = " << Hdr.NumHashes << '\n'
121 << "HeaderData length = " << Hdr.HeaderDataLength << '\n'
122 << "DIE offset base = " << HdrData.DIEOffsetBase << '\n'
123 << "Number of atoms = " << HdrData.Atoms.size() << '\n';
124
125 unsigned i = 0;
Frederic Riss77a07432014-11-20 16:21:11 +0000126 SmallVector<DWARFFormValue, 3> AtomForms;
Frederic Risse837ec22014-11-14 16:15:53 +0000127 for (const auto &Atom: HdrData.Atoms) {
128 OS << format("Atom[%d] Type: ", i++);
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000129 auto TypeString = dwarf::AtomTypeString(Atom.first);
130 if (!TypeString.empty())
Frederic Risse837ec22014-11-14 16:15:53 +0000131 OS << TypeString;
132 else
133 OS << format("DW_ATOM_Unknown_0x%x", Atom.first);
134 OS << " Form: ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000135 auto FormString = dwarf::FormEncodingString(Atom.second);
136 if (!FormString.empty())
Frederic Risse837ec22014-11-14 16:15:53 +0000137 OS << FormString;
138 else
Frederic Riss77a07432014-11-20 16:21:11 +0000139 OS << format("DW_FORM_Unknown_0x%x", Atom.second);
Frederic Risse837ec22014-11-14 16:15:53 +0000140 OS << '\n';
Frederic Riss77a07432014-11-20 16:21:11 +0000141 AtomForms.push_back(DWARFFormValue(Atom.second));
Frederic Risse837ec22014-11-14 16:15:53 +0000142 }
143
144 // Now go through the actual tables and dump them.
145 uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
146 unsigned HashesBase = Offset + Hdr.NumBuckets * 4;
147 unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4;
148
149 for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) {
150 unsigned Index = AccelSection.getU32(&Offset);
151
152 OS << format("Bucket[%d]\n", Bucket);
153 if (Index == UINT32_MAX) {
154 OS << " EMPTY\n";
155 continue;
156 }
157
158 for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) {
159 unsigned HashOffset = HashesBase + HashIdx*4;
160 unsigned OffsetsOffset = OffsetsBase + HashIdx*4;
161 uint32_t Hash = AccelSection.getU32(&HashOffset);
162
163 if (Hash % Hdr.NumBuckets != Bucket)
164 break;
165
166 unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
167 OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset);
168 if (!AccelSection.isValidOffset(DataOffset)) {
169 OS << " Invalid section offset\n";
170 continue;
171 }
Frederic Riss7c500472014-11-14 19:30:08 +0000172 while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
Paul Robinson17536b92017-06-29 16:52:08 +0000173 unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset);
Frederic Riss7c500472014-11-14 19:30:08 +0000174 if (!StringOffset)
175 break;
Frederic Risse837ec22014-11-14 16:15:53 +0000176 OS << format(" Name: %08x \"%s\"\n", StringOffset,
177 StringSection.getCStr(&StringOffset));
178 unsigned NumData = AccelSection.getU32(&DataOffset);
179 for (unsigned Data = 0; Data < NumData; ++Data) {
180 OS << format(" Data[%d] => ", Data);
181 unsigned i = 0;
Frederic Riss77a07432014-11-20 16:21:11 +0000182 for (auto &Atom : AtomForms) {
Frederic Risse837ec22014-11-14 16:15:53 +0000183 OS << format("{Atom[%d]: ", i++);
Frederic Riss77a07432014-11-20 16:21:11 +0000184 if (Atom.extractValue(AccelSection, &DataOffset, nullptr))
Greg Claytoncddab272016-10-31 16:46:02 +0000185 Atom.dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000186 else
187 OS << "Error extracting the value";
188 OS << "} ";
189 }
190 OS << '\n';
191 }
192 }
193 }
194 }
195}
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000196
197DWARFAcceleratorTable::ValueIterator::ValueIterator(
198 const DWARFAcceleratorTable &AccelTable, unsigned Offset)
199 : AccelTable(&AccelTable), DataOffset(Offset) {
200 if (!AccelTable.AccelSection.isValidOffsetForDataOfSize(DataOffset, 4))
201 return;
202
203 for (const auto &Atom : AccelTable.HdrData.Atoms)
204 AtomForms.push_back(DWARFFormValue(Atom.second));
205
206 // Read the first entry.
207 NumData = AccelTable.AccelSection.getU32(&DataOffset);
208 Next();
209}
210
211void DWARFAcceleratorTable::ValueIterator::Next() {
212 assert(NumData > 0 && "attempted to increment iterator past the end");
213 auto &AccelSection = AccelTable->AccelSection;
214 if (Data >= NumData ||
215 !AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
216 NumData = 0;
217 return;
218 }
219 for (auto &Atom : AtomForms)
220 Atom.extractValue(AccelSection, &DataOffset, nullptr);
221 ++Data;
222}
223
224iterator_range<DWARFAcceleratorTable::ValueIterator>
225DWARFAcceleratorTable::equal_range(StringRef Key) const {
226 if (!IsValid)
227 return make_range(ValueIterator(), ValueIterator());
228
229 // Find the bucket.
230 unsigned HashValue = dwarf::djbHash(Key);
231 unsigned Bucket = HashValue % Hdr.NumBuckets;
232 unsigned BucketBase = sizeof(Hdr) + Hdr.HeaderDataLength;
233 unsigned HashesBase = BucketBase + Hdr.NumBuckets * 4;
234 unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4;
235
236 unsigned BucketOffset = BucketBase + Bucket * 4;
237 unsigned Index = AccelSection.getU32(&BucketOffset);
238
239 // Search through all hashes in the bucket.
240 for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) {
241 unsigned HashOffset = HashesBase + HashIdx * 4;
242 unsigned OffsetsOffset = OffsetsBase + HashIdx * 4;
243 uint32_t Hash = AccelSection.getU32(&HashOffset);
244
245 if (Hash % Hdr.NumBuckets != Bucket)
246 // We are already in the next bucket.
247 break;
248
249 unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
250 unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset);
251 if (!StringOffset)
252 break;
253
254 // Finally, compare the key.
255 if (Key == StringSection.getCStr(&StringOffset))
256 return make_range({*this, DataOffset}, ValueIterator());
257 }
258 return make_range(ValueIterator(), ValueIterator());
259}