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/DebugInfo/DWARF/DWARFAcceleratorTable.h"
11
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/DJB.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/FormatVariadic.h"
20 #include "llvm/Support/ScopedPrinter.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cstddef>
23 #include <cstdint>
24 #include <utility>
25
26 using namespace llvm;
27
28 namespace {
29 struct Atom {
30 unsigned Value;
31 };
32
operator <<(raw_ostream & OS,const Atom & A)33 static raw_ostream &operator<<(raw_ostream &OS, const Atom &A) {
34 StringRef Str = dwarf::AtomTypeString(A.Value);
35 if (!Str.empty())
36 return OS << Str;
37 return OS << "DW_ATOM_unknown_" << format("%x", A.Value);
38 }
39 } // namespace
40
formatAtom(unsigned Atom)41 static Atom formatAtom(unsigned Atom) { return {Atom}; }
42
43 DWARFAcceleratorTable::~DWARFAcceleratorTable() = default;
44
extract()45 llvm::Error AppleAcceleratorTable::extract() {
46 uint32_t Offset = 0;
47
48 // Check that we can at least read the header.
49 if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength) + 4))
50 return createStringError(errc::illegal_byte_sequence,
51 "Section too small: cannot read header.");
52
53 Hdr.Magic = AccelSection.getU32(&Offset);
54 Hdr.Version = AccelSection.getU16(&Offset);
55 Hdr.HashFunction = AccelSection.getU16(&Offset);
56 Hdr.BucketCount = AccelSection.getU32(&Offset);
57 Hdr.HashCount = AccelSection.getU32(&Offset);
58 Hdr.HeaderDataLength = AccelSection.getU32(&Offset);
59
60 // Check that we can read all the hashes and offsets from the
61 // section (see SourceLevelDebugging.rst for the structure of the index).
62 // We need to substract one because we're checking for an *offset* which is
63 // equal to the size for an empty table and hence pointer after the section.
64 if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
65 Hdr.BucketCount * 4 + Hdr.HashCount * 8 - 1))
66 return createStringError(
67 errc::illegal_byte_sequence,
68 "Section too small: cannot read buckets and hashes.");
69
70 HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
71 uint32_t NumAtoms = AccelSection.getU32(&Offset);
72
73 for (unsigned i = 0; i < NumAtoms; ++i) {
74 uint16_t AtomType = AccelSection.getU16(&Offset);
75 auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset));
76 HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
77 }
78
79 IsValid = true;
80 return Error::success();
81 }
82
getNumBuckets()83 uint32_t AppleAcceleratorTable::getNumBuckets() { return Hdr.BucketCount; }
getNumHashes()84 uint32_t AppleAcceleratorTable::getNumHashes() { return Hdr.HashCount; }
getSizeHdr()85 uint32_t AppleAcceleratorTable::getSizeHdr() { return sizeof(Hdr); }
getHeaderDataLength()86 uint32_t AppleAcceleratorTable::getHeaderDataLength() {
87 return Hdr.HeaderDataLength;
88 }
89
90 ArrayRef<std::pair<AppleAcceleratorTable::HeaderData::AtomType,
91 AppleAcceleratorTable::HeaderData::Form>>
getAtomsDesc()92 AppleAcceleratorTable::getAtomsDesc() {
93 return HdrData.Atoms;
94 }
95
validateForms()96 bool AppleAcceleratorTable::validateForms() {
97 for (auto Atom : getAtomsDesc()) {
98 DWARFFormValue FormValue(Atom.second);
99 switch (Atom.first) {
100 case dwarf::DW_ATOM_die_offset:
101 case dwarf::DW_ATOM_die_tag:
102 case dwarf::DW_ATOM_type_flags:
103 if ((!FormValue.isFormClass(DWARFFormValue::FC_Constant) &&
104 !FormValue.isFormClass(DWARFFormValue::FC_Flag)) ||
105 FormValue.getForm() == dwarf::DW_FORM_sdata)
106 return false;
107 break;
108 default:
109 break;
110 }
111 }
112 return true;
113 }
114
115 std::pair<uint32_t, dwarf::Tag>
readAtoms(uint32_t & HashDataOffset)116 AppleAcceleratorTable::readAtoms(uint32_t &HashDataOffset) {
117 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
118 dwarf::Tag DieTag = dwarf::DW_TAG_null;
119 dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32};
120
121 for (auto Atom : getAtomsDesc()) {
122 DWARFFormValue FormValue(Atom.second);
123 FormValue.extractValue(AccelSection, &HashDataOffset, FormParams);
124 switch (Atom.first) {
125 case dwarf::DW_ATOM_die_offset:
126 DieOffset = *FormValue.getAsUnsignedConstant();
127 break;
128 case dwarf::DW_ATOM_die_tag:
129 DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant();
130 break;
131 default:
132 break;
133 }
134 }
135 return {DieOffset, DieTag};
136 }
137
dump(ScopedPrinter & W) const138 void AppleAcceleratorTable::Header::dump(ScopedPrinter &W) const {
139 DictScope HeaderScope(W, "Header");
140 W.printHex("Magic", Magic);
141 W.printHex("Version", Version);
142 W.printHex("Hash function", HashFunction);
143 W.printNumber("Bucket count", BucketCount);
144 W.printNumber("Hashes count", HashCount);
145 W.printNumber("HeaderData length", HeaderDataLength);
146 }
147
extractOffset(Optional<DWARFFormValue> Value) const148 Optional<uint64_t> AppleAcceleratorTable::HeaderData::extractOffset(
149 Optional<DWARFFormValue> Value) const {
150 if (!Value)
151 return None;
152
153 switch (Value->getForm()) {
154 case dwarf::DW_FORM_ref1:
155 case dwarf::DW_FORM_ref2:
156 case dwarf::DW_FORM_ref4:
157 case dwarf::DW_FORM_ref8:
158 case dwarf::DW_FORM_ref_udata:
159 return Value->getRawUValue() + DIEOffsetBase;
160 default:
161 return Value->getAsSectionOffset();
162 }
163 }
164
dumpName(ScopedPrinter & W,SmallVectorImpl<DWARFFormValue> & AtomForms,uint32_t * DataOffset) const165 bool AppleAcceleratorTable::dumpName(ScopedPrinter &W,
166 SmallVectorImpl<DWARFFormValue> &AtomForms,
167 uint32_t *DataOffset) const {
168 dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32};
169 uint32_t NameOffset = *DataOffset;
170 if (!AccelSection.isValidOffsetForDataOfSize(*DataOffset, 4)) {
171 W.printString("Incorrectly terminated list.");
172 return false;
173 }
174 unsigned StringOffset = AccelSection.getRelocatedValue(4, DataOffset);
175 if (!StringOffset)
176 return false; // End of list
177
178 DictScope NameScope(W, ("Name@0x" + Twine::utohexstr(NameOffset)).str());
179 W.startLine() << format("String: 0x%08x", StringOffset);
180 W.getOStream() << " \"" << StringSection.getCStr(&StringOffset) << "\"\n";
181
182 unsigned NumData = AccelSection.getU32(DataOffset);
183 for (unsigned Data = 0; Data < NumData; ++Data) {
184 ListScope DataScope(W, ("Data " + Twine(Data)).str());
185 unsigned i = 0;
186 for (auto &Atom : AtomForms) {
187 W.startLine() << format("Atom[%d]: ", i);
188 if (Atom.extractValue(AccelSection, DataOffset, FormParams)) {
189 Atom.dump(W.getOStream());
190 if (Optional<uint64_t> Val = Atom.getAsUnsignedConstant()) {
191 StringRef Str = dwarf::AtomValueString(HdrData.Atoms[i].first, *Val);
192 if (!Str.empty())
193 W.getOStream() << " (" << Str << ")";
194 }
195 } else
196 W.getOStream() << "Error extracting the value";
197 W.getOStream() << "\n";
198 i++;
199 }
200 }
201 return true; // more entries follow
202 }
203
dump(raw_ostream & OS) const204 LLVM_DUMP_METHOD void AppleAcceleratorTable::dump(raw_ostream &OS) const {
205 if (!IsValid)
206 return;
207
208 ScopedPrinter W(OS);
209
210 Hdr.dump(W);
211
212 W.printNumber("DIE offset base", HdrData.DIEOffsetBase);
213 W.printNumber("Number of atoms", uint64_t(HdrData.Atoms.size()));
214 SmallVector<DWARFFormValue, 3> AtomForms;
215 {
216 ListScope AtomsScope(W, "Atoms");
217 unsigned i = 0;
218 for (const auto &Atom : HdrData.Atoms) {
219 DictScope AtomScope(W, ("Atom " + Twine(i++)).str());
220 W.startLine() << "Type: " << formatAtom(Atom.first) << '\n';
221 W.startLine() << "Form: " << formatv("{0}", Atom.second) << '\n';
222 AtomForms.push_back(DWARFFormValue(Atom.second));
223 }
224 }
225
226 // Now go through the actual tables and dump them.
227 uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
228 unsigned HashesBase = Offset + Hdr.BucketCount * 4;
229 unsigned OffsetsBase = HashesBase + Hdr.HashCount * 4;
230
231 for (unsigned Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) {
232 unsigned Index = AccelSection.getU32(&Offset);
233
234 ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str());
235 if (Index == UINT32_MAX) {
236 W.printString("EMPTY");
237 continue;
238 }
239
240 for (unsigned HashIdx = Index; HashIdx < Hdr.HashCount; ++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.BucketCount != Bucket)
246 break;
247
248 unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
249 ListScope HashScope(W, ("Hash 0x" + Twine::utohexstr(Hash)).str());
250 if (!AccelSection.isValidOffset(DataOffset)) {
251 W.printString("Invalid section offset");
252 continue;
253 }
254 while (dumpName(W, AtomForms, &DataOffset))
255 /*empty*/;
256 }
257 }
258 }
259
Entry(const AppleAcceleratorTable::HeaderData & HdrData)260 AppleAcceleratorTable::Entry::Entry(
261 const AppleAcceleratorTable::HeaderData &HdrData)
262 : HdrData(&HdrData) {
263 Values.reserve(HdrData.Atoms.size());
264 for (const auto &Atom : HdrData.Atoms)
265 Values.push_back(DWARFFormValue(Atom.second));
266 }
267
extract(const AppleAcceleratorTable & AccelTable,uint32_t * Offset)268 void AppleAcceleratorTable::Entry::extract(
269 const AppleAcceleratorTable &AccelTable, uint32_t *Offset) {
270
271 dwarf::FormParams FormParams = {AccelTable.Hdr.Version, 0,
272 dwarf::DwarfFormat::DWARF32};
273 for (auto &Atom : Values)
274 Atom.extractValue(AccelTable.AccelSection, Offset, FormParams);
275 }
276
277 Optional<DWARFFormValue>
lookup(HeaderData::AtomType Atom) const278 AppleAcceleratorTable::Entry::lookup(HeaderData::AtomType Atom) const {
279 assert(HdrData && "Dereferencing end iterator?");
280 assert(HdrData->Atoms.size() == Values.size());
281 for (const auto &Tuple : zip_first(HdrData->Atoms, Values)) {
282 if (std::get<0>(Tuple).first == Atom)
283 return std::get<1>(Tuple);
284 }
285 return None;
286 }
287
getDIESectionOffset() const288 Optional<uint64_t> AppleAcceleratorTable::Entry::getDIESectionOffset() const {
289 return HdrData->extractOffset(lookup(dwarf::DW_ATOM_die_offset));
290 }
291
getCUOffset() const292 Optional<uint64_t> AppleAcceleratorTable::Entry::getCUOffset() const {
293 return HdrData->extractOffset(lookup(dwarf::DW_ATOM_cu_offset));
294 }
295
getTag() const296 Optional<dwarf::Tag> AppleAcceleratorTable::Entry::getTag() const {
297 Optional<DWARFFormValue> Tag = lookup(dwarf::DW_ATOM_die_tag);
298 if (!Tag)
299 return None;
300 if (Optional<uint64_t> Value = Tag->getAsUnsignedConstant())
301 return dwarf::Tag(*Value);
302 return None;
303 }
304
ValueIterator(const AppleAcceleratorTable & AccelTable,unsigned Offset)305 AppleAcceleratorTable::ValueIterator::ValueIterator(
306 const AppleAcceleratorTable &AccelTable, unsigned Offset)
307 : AccelTable(&AccelTable), Current(AccelTable.HdrData), DataOffset(Offset) {
308 if (!AccelTable.AccelSection.isValidOffsetForDataOfSize(DataOffset, 4))
309 return;
310
311 // Read the first entry.
312 NumData = AccelTable.AccelSection.getU32(&DataOffset);
313 Next();
314 }
315
Next()316 void AppleAcceleratorTable::ValueIterator::Next() {
317 assert(NumData > 0 && "attempted to increment iterator past the end");
318 auto &AccelSection = AccelTable->AccelSection;
319 if (Data >= NumData ||
320 !AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
321 NumData = 0;
322 DataOffset = 0;
323 return;
324 }
325 Current.extract(*AccelTable, &DataOffset);
326 ++Data;
327 }
328
329 iterator_range<AppleAcceleratorTable::ValueIterator>
equal_range(StringRef Key) const330 AppleAcceleratorTable::equal_range(StringRef Key) const {
331 if (!IsValid)
332 return make_range(ValueIterator(), ValueIterator());
333
334 // Find the bucket.
335 unsigned HashValue = djbHash(Key);
336 unsigned Bucket = HashValue % Hdr.BucketCount;
337 unsigned BucketBase = sizeof(Hdr) + Hdr.HeaderDataLength;
338 unsigned HashesBase = BucketBase + Hdr.BucketCount * 4;
339 unsigned OffsetsBase = HashesBase + Hdr.HashCount * 4;
340
341 unsigned BucketOffset = BucketBase + Bucket * 4;
342 unsigned Index = AccelSection.getU32(&BucketOffset);
343
344 // Search through all hashes in the bucket.
345 for (unsigned HashIdx = Index; HashIdx < Hdr.HashCount; ++HashIdx) {
346 unsigned HashOffset = HashesBase + HashIdx * 4;
347 unsigned OffsetsOffset = OffsetsBase + HashIdx * 4;
348 uint32_t Hash = AccelSection.getU32(&HashOffset);
349
350 if (Hash % Hdr.BucketCount != Bucket)
351 // We are already in the next bucket.
352 break;
353
354 unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
355 unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset);
356 if (!StringOffset)
357 break;
358
359 // Finally, compare the key.
360 if (Key == StringSection.getCStr(&StringOffset))
361 return make_range({*this, DataOffset}, ValueIterator());
362 }
363 return make_range(ValueIterator(), ValueIterator());
364 }
365
dump(ScopedPrinter & W) const366 void DWARFDebugNames::Header::dump(ScopedPrinter &W) const {
367 DictScope HeaderScope(W, "Header");
368 W.printHex("Length", UnitLength);
369 W.printNumber("Version", Version);
370 W.printHex("Padding", Padding);
371 W.printNumber("CU count", CompUnitCount);
372 W.printNumber("Local TU count", LocalTypeUnitCount);
373 W.printNumber("Foreign TU count", ForeignTypeUnitCount);
374 W.printNumber("Bucket count", BucketCount);
375 W.printNumber("Name count", NameCount);
376 W.printHex("Abbreviations table size", AbbrevTableSize);
377 W.startLine() << "Augmentation: '" << AugmentationString << "'\n";
378 }
379
extract(const DWARFDataExtractor & AS,uint32_t * Offset)380 llvm::Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS,
381 uint32_t *Offset) {
382 // Check that we can read the fixed-size part.
383 if (!AS.isValidOffset(*Offset + sizeof(HeaderPOD) - 1))
384 return createStringError(errc::illegal_byte_sequence,
385 "Section too small: cannot read header.");
386
387 UnitLength = AS.getU32(Offset);
388 Version = AS.getU16(Offset);
389 Padding = AS.getU16(Offset);
390 CompUnitCount = AS.getU32(Offset);
391 LocalTypeUnitCount = AS.getU32(Offset);
392 ForeignTypeUnitCount = AS.getU32(Offset);
393 BucketCount = AS.getU32(Offset);
394 NameCount = AS.getU32(Offset);
395 AbbrevTableSize = AS.getU32(Offset);
396 AugmentationStringSize = alignTo(AS.getU32(Offset), 4);
397
398 if (!AS.isValidOffsetForDataOfSize(*Offset, AugmentationStringSize))
399 return createStringError(
400 errc::illegal_byte_sequence,
401 "Section too small: cannot read header augmentation.");
402 AugmentationString.resize(AugmentationStringSize);
403 AS.getU8(Offset, reinterpret_cast<uint8_t *>(AugmentationString.data()),
404 AugmentationStringSize);
405 return Error::success();
406 }
407
dump(ScopedPrinter & W) const408 void DWARFDebugNames::Abbrev::dump(ScopedPrinter &W) const {
409 DictScope AbbrevScope(W, ("Abbreviation 0x" + Twine::utohexstr(Code)).str());
410 W.startLine() << formatv("Tag: {0}\n", Tag);
411
412 for (const auto &Attr : Attributes)
413 W.startLine() << formatv("{0}: {1}\n", Attr.Index, Attr.Form);
414 }
415
sentinelAttrEnc()416 static constexpr DWARFDebugNames::AttributeEncoding sentinelAttrEnc() {
417 return {dwarf::Index(0), dwarf::Form(0)};
418 }
419
isSentinel(const DWARFDebugNames::AttributeEncoding & AE)420 static bool isSentinel(const DWARFDebugNames::AttributeEncoding &AE) {
421 return AE == sentinelAttrEnc();
422 }
423
sentinelAbbrev()424 static DWARFDebugNames::Abbrev sentinelAbbrev() {
425 return DWARFDebugNames::Abbrev(0, dwarf::Tag(0), {});
426 }
427
isSentinel(const DWARFDebugNames::Abbrev & Abbr)428 static bool isSentinel(const DWARFDebugNames::Abbrev &Abbr) {
429 return Abbr.Code == 0;
430 }
431
getEmptyKey()432 DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getEmptyKey() {
433 return sentinelAbbrev();
434 }
435
getTombstoneKey()436 DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getTombstoneKey() {
437 return DWARFDebugNames::Abbrev(~0, dwarf::Tag(0), {});
438 }
439
440 Expected<DWARFDebugNames::AttributeEncoding>
extractAttributeEncoding(uint32_t * Offset)441 DWARFDebugNames::NameIndex::extractAttributeEncoding(uint32_t *Offset) {
442 if (*Offset >= EntriesBase) {
443 return createStringError(errc::illegal_byte_sequence,
444 "Incorrectly terminated abbreviation table.");
445 }
446
447 uint32_t Index = Section.AccelSection.getULEB128(Offset);
448 uint32_t Form = Section.AccelSection.getULEB128(Offset);
449 return AttributeEncoding(dwarf::Index(Index), dwarf::Form(Form));
450 }
451
452 Expected<std::vector<DWARFDebugNames::AttributeEncoding>>
extractAttributeEncodings(uint32_t * Offset)453 DWARFDebugNames::NameIndex::extractAttributeEncodings(uint32_t *Offset) {
454 std::vector<AttributeEncoding> Result;
455 for (;;) {
456 auto AttrEncOr = extractAttributeEncoding(Offset);
457 if (!AttrEncOr)
458 return AttrEncOr.takeError();
459 if (isSentinel(*AttrEncOr))
460 return std::move(Result);
461
462 Result.emplace_back(*AttrEncOr);
463 }
464 }
465
466 Expected<DWARFDebugNames::Abbrev>
extractAbbrev(uint32_t * Offset)467 DWARFDebugNames::NameIndex::extractAbbrev(uint32_t *Offset) {
468 if (*Offset >= EntriesBase) {
469 return createStringError(errc::illegal_byte_sequence,
470 "Incorrectly terminated abbreviation table.");
471 }
472
473 uint32_t Code = Section.AccelSection.getULEB128(Offset);
474 if (Code == 0)
475 return sentinelAbbrev();
476
477 uint32_t Tag = Section.AccelSection.getULEB128(Offset);
478 auto AttrEncOr = extractAttributeEncodings(Offset);
479 if (!AttrEncOr)
480 return AttrEncOr.takeError();
481 return Abbrev(Code, dwarf::Tag(Tag), std::move(*AttrEncOr));
482 }
483
extract()484 Error DWARFDebugNames::NameIndex::extract() {
485 const DWARFDataExtractor &AS = Section.AccelSection;
486 uint32_t Offset = Base;
487 if (Error E = Hdr.extract(AS, &Offset))
488 return E;
489
490 CUsBase = Offset;
491 Offset += Hdr.CompUnitCount * 4;
492 Offset += Hdr.LocalTypeUnitCount * 4;
493 Offset += Hdr.ForeignTypeUnitCount * 8;
494 BucketsBase = Offset;
495 Offset += Hdr.BucketCount * 4;
496 HashesBase = Offset;
497 if (Hdr.BucketCount > 0)
498 Offset += Hdr.NameCount * 4;
499 StringOffsetsBase = Offset;
500 Offset += Hdr.NameCount * 4;
501 EntryOffsetsBase = Offset;
502 Offset += Hdr.NameCount * 4;
503
504 if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize))
505 return createStringError(errc::illegal_byte_sequence,
506 "Section too small: cannot read abbreviations.");
507
508 EntriesBase = Offset + Hdr.AbbrevTableSize;
509
510 for (;;) {
511 auto AbbrevOr = extractAbbrev(&Offset);
512 if (!AbbrevOr)
513 return AbbrevOr.takeError();
514 if (isSentinel(*AbbrevOr))
515 return Error::success();
516
517 if (!Abbrevs.insert(std::move(*AbbrevOr)).second)
518 return createStringError(errc::invalid_argument,
519 "Duplicate abbreviation code.");
520 }
521 }
Entry(const NameIndex & NameIdx,const Abbrev & Abbr)522 DWARFDebugNames::Entry::Entry(const NameIndex &NameIdx, const Abbrev &Abbr)
523 : NameIdx(&NameIdx), Abbr(&Abbr) {
524 // This merely creates form values. It is up to the caller
525 // (NameIndex::getEntry) to populate them.
526 Values.reserve(Abbr.Attributes.size());
527 for (const auto &Attr : Abbr.Attributes)
528 Values.emplace_back(Attr.Form);
529 }
530
531 Optional<DWARFFormValue>
lookup(dwarf::Index Index) const532 DWARFDebugNames::Entry::lookup(dwarf::Index Index) const {
533 assert(Abbr->Attributes.size() == Values.size());
534 for (const auto &Tuple : zip_first(Abbr->Attributes, Values)) {
535 if (std::get<0>(Tuple).Index == Index)
536 return std::get<1>(Tuple);
537 }
538 return None;
539 }
540
getDIEUnitOffset() const541 Optional<uint64_t> DWARFDebugNames::Entry::getDIEUnitOffset() const {
542 if (Optional<DWARFFormValue> Off = lookup(dwarf::DW_IDX_die_offset))
543 return Off->getAsReferenceUVal();
544 return None;
545 }
546
getCUIndex() const547 Optional<uint64_t> DWARFDebugNames::Entry::getCUIndex() const {
548 if (Optional<DWARFFormValue> Off = lookup(dwarf::DW_IDX_compile_unit))
549 return Off->getAsUnsignedConstant();
550 // In a per-CU index, the entries without a DW_IDX_compile_unit attribute
551 // implicitly refer to the single CU.
552 if (NameIdx->getCUCount() == 1)
553 return 0;
554 return None;
555 }
556
getCUOffset() const557 Optional<uint64_t> DWARFDebugNames::Entry::getCUOffset() const {
558 Optional<uint64_t> Index = getCUIndex();
559 if (!Index || *Index >= NameIdx->getCUCount())
560 return None;
561 return NameIdx->getCUOffset(*Index);
562 }
563
dump(ScopedPrinter & W) const564 void DWARFDebugNames::Entry::dump(ScopedPrinter &W) const {
565 W.printHex("Abbrev", Abbr->Code);
566 W.startLine() << formatv("Tag: {0}\n", Abbr->Tag);
567 assert(Abbr->Attributes.size() == Values.size());
568 for (const auto &Tuple : zip_first(Abbr->Attributes, Values)) {
569 W.startLine() << formatv("{0}: ", std::get<0>(Tuple).Index);
570 std::get<1>(Tuple).dump(W.getOStream());
571 W.getOStream() << '\n';
572 }
573 }
574
575 char DWARFDebugNames::SentinelError::ID;
convertToErrorCode() const576 std::error_code DWARFDebugNames::SentinelError::convertToErrorCode() const {
577 return inconvertibleErrorCode();
578 }
579
getCUOffset(uint32_t CU) const580 uint32_t DWARFDebugNames::NameIndex::getCUOffset(uint32_t CU) const {
581 assert(CU < Hdr.CompUnitCount);
582 uint32_t Offset = CUsBase + 4 * CU;
583 return Section.AccelSection.getRelocatedValue(4, &Offset);
584 }
585
getLocalTUOffset(uint32_t TU) const586 uint32_t DWARFDebugNames::NameIndex::getLocalTUOffset(uint32_t TU) const {
587 assert(TU < Hdr.LocalTypeUnitCount);
588 uint32_t Offset = CUsBase + Hdr.CompUnitCount * 4;
589 return Section.AccelSection.getRelocatedValue(4, &Offset);
590 }
591
getForeignTUSignature(uint32_t TU) const592 uint64_t DWARFDebugNames::NameIndex::getForeignTUSignature(uint32_t TU) const {
593 assert(TU < Hdr.ForeignTypeUnitCount);
594 uint32_t Offset = CUsBase + (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) * 4;
595 return Section.AccelSection.getU64(&Offset);
596 }
597
598 Expected<DWARFDebugNames::Entry>
getEntry(uint32_t * Offset) const599 DWARFDebugNames::NameIndex::getEntry(uint32_t *Offset) const {
600 const DWARFDataExtractor &AS = Section.AccelSection;
601 if (!AS.isValidOffset(*Offset))
602 return createStringError(errc::illegal_byte_sequence,
603 "Incorrectly terminated entry list.");
604
605 uint32_t AbbrevCode = AS.getULEB128(Offset);
606 if (AbbrevCode == 0)
607 return make_error<SentinelError>();
608
609 const auto AbbrevIt = Abbrevs.find_as(AbbrevCode);
610 if (AbbrevIt == Abbrevs.end())
611 return createStringError(errc::invalid_argument, "Invalid abbreviation.");
612
613 Entry E(*this, *AbbrevIt);
614
615 dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32};
616 for (auto &Value : E.Values) {
617 if (!Value.extractValue(AS, Offset, FormParams))
618 return createStringError(errc::io_error,
619 "Error extracting index attribute values.");
620 }
621 return std::move(E);
622 }
623
624 DWARFDebugNames::NameTableEntry
getNameTableEntry(uint32_t Index) const625 DWARFDebugNames::NameIndex::getNameTableEntry(uint32_t Index) const {
626 assert(0 < Index && Index <= Hdr.NameCount);
627 uint32_t StringOffsetOffset = StringOffsetsBase + 4 * (Index - 1);
628 uint32_t EntryOffsetOffset = EntryOffsetsBase + 4 * (Index - 1);
629 const DWARFDataExtractor &AS = Section.AccelSection;
630
631 uint32_t StringOffset = AS.getRelocatedValue(4, &StringOffsetOffset);
632 uint32_t EntryOffset = AS.getU32(&EntryOffsetOffset);
633 EntryOffset += EntriesBase;
634 return {Section.StringSection, Index, StringOffset, EntryOffset};
635 }
636
637 uint32_t
getBucketArrayEntry(uint32_t Bucket) const638 DWARFDebugNames::NameIndex::getBucketArrayEntry(uint32_t Bucket) const {
639 assert(Bucket < Hdr.BucketCount);
640 uint32_t BucketOffset = BucketsBase + 4 * Bucket;
641 return Section.AccelSection.getU32(&BucketOffset);
642 }
643
getHashArrayEntry(uint32_t Index) const644 uint32_t DWARFDebugNames::NameIndex::getHashArrayEntry(uint32_t Index) const {
645 assert(0 < Index && Index <= Hdr.NameCount);
646 uint32_t HashOffset = HashesBase + 4 * (Index - 1);
647 return Section.AccelSection.getU32(&HashOffset);
648 }
649
650 // Returns true if we should continue scanning for entries, false if this is the
651 // last (sentinel) entry). In case of a parsing error we also return false, as
652 // it's not possible to recover this entry list (but the other lists may still
653 // parse OK).
dumpEntry(ScopedPrinter & W,uint32_t * Offset) const654 bool DWARFDebugNames::NameIndex::dumpEntry(ScopedPrinter &W,
655 uint32_t *Offset) const {
656 uint32_t EntryId = *Offset;
657 auto EntryOr = getEntry(Offset);
658 if (!EntryOr) {
659 handleAllErrors(EntryOr.takeError(), [](const SentinelError &) {},
660 [&W](const ErrorInfoBase &EI) { EI.log(W.startLine()); });
661 return false;
662 }
663
664 DictScope EntryScope(W, ("Entry @ 0x" + Twine::utohexstr(EntryId)).str());
665 EntryOr->dump(W);
666 return true;
667 }
668
dumpName(ScopedPrinter & W,const NameTableEntry & NTE,Optional<uint32_t> Hash) const669 void DWARFDebugNames::NameIndex::dumpName(ScopedPrinter &W,
670 const NameTableEntry &NTE,
671 Optional<uint32_t> Hash) const {
672 DictScope NameScope(W, ("Name " + Twine(NTE.getIndex())).str());
673 if (Hash)
674 W.printHex("Hash", *Hash);
675
676 W.startLine() << format("String: 0x%08x", NTE.getStringOffset());
677 W.getOStream() << " \"" << NTE.getString() << "\"\n";
678
679 uint32_t EntryOffset = NTE.getEntryOffset();
680 while (dumpEntry(W, &EntryOffset))
681 /*empty*/;
682 }
683
dumpCUs(ScopedPrinter & W) const684 void DWARFDebugNames::NameIndex::dumpCUs(ScopedPrinter &W) const {
685 ListScope CUScope(W, "Compilation Unit offsets");
686 for (uint32_t CU = 0; CU < Hdr.CompUnitCount; ++CU)
687 W.startLine() << format("CU[%u]: 0x%08x\n", CU, getCUOffset(CU));
688 }
689
dumpLocalTUs(ScopedPrinter & W) const690 void DWARFDebugNames::NameIndex::dumpLocalTUs(ScopedPrinter &W) const {
691 if (Hdr.LocalTypeUnitCount == 0)
692 return;
693
694 ListScope TUScope(W, "Local Type Unit offsets");
695 for (uint32_t TU = 0; TU < Hdr.LocalTypeUnitCount; ++TU)
696 W.startLine() << format("LocalTU[%u]: 0x%08x\n", TU, getLocalTUOffset(TU));
697 }
698
dumpForeignTUs(ScopedPrinter & W) const699 void DWARFDebugNames::NameIndex::dumpForeignTUs(ScopedPrinter &W) const {
700 if (Hdr.ForeignTypeUnitCount == 0)
701 return;
702
703 ListScope TUScope(W, "Foreign Type Unit signatures");
704 for (uint32_t TU = 0; TU < Hdr.ForeignTypeUnitCount; ++TU) {
705 W.startLine() << format("ForeignTU[%u]: 0x%016" PRIx64 "\n", TU,
706 getForeignTUSignature(TU));
707 }
708 }
709
dumpAbbreviations(ScopedPrinter & W) const710 void DWARFDebugNames::NameIndex::dumpAbbreviations(ScopedPrinter &W) const {
711 ListScope AbbrevsScope(W, "Abbreviations");
712 for (const auto &Abbr : Abbrevs)
713 Abbr.dump(W);
714 }
715
dumpBucket(ScopedPrinter & W,uint32_t Bucket) const716 void DWARFDebugNames::NameIndex::dumpBucket(ScopedPrinter &W,
717 uint32_t Bucket) const {
718 ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str());
719 uint32_t Index = getBucketArrayEntry(Bucket);
720 if (Index == 0) {
721 W.printString("EMPTY");
722 return;
723 }
724 if (Index > Hdr.NameCount) {
725 W.printString("Name index is invalid");
726 return;
727 }
728
729 for (; Index <= Hdr.NameCount; ++Index) {
730 uint32_t Hash = getHashArrayEntry(Index);
731 if (Hash % Hdr.BucketCount != Bucket)
732 break;
733
734 dumpName(W, getNameTableEntry(Index), Hash);
735 }
736 }
737
dump(ScopedPrinter & W) const738 LLVM_DUMP_METHOD void DWARFDebugNames::NameIndex::dump(ScopedPrinter &W) const {
739 DictScope UnitScope(W, ("Name Index @ 0x" + Twine::utohexstr(Base)).str());
740 Hdr.dump(W);
741 dumpCUs(W);
742 dumpLocalTUs(W);
743 dumpForeignTUs(W);
744 dumpAbbreviations(W);
745
746 if (Hdr.BucketCount > 0) {
747 for (uint32_t Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket)
748 dumpBucket(W, Bucket);
749 return;
750 }
751
752 W.startLine() << "Hash table not present\n";
753 for (NameTableEntry NTE : *this)
754 dumpName(W, NTE, None);
755 }
756
extract()757 llvm::Error DWARFDebugNames::extract() {
758 uint32_t Offset = 0;
759 while (AccelSection.isValidOffset(Offset)) {
760 NameIndex Next(*this, Offset);
761 if (llvm::Error E = Next.extract())
762 return E;
763 Offset = Next.getNextUnitOffset();
764 NameIndices.push_back(std::move(Next));
765 }
766 return Error::success();
767 }
768
769 iterator_range<DWARFDebugNames::ValueIterator>
equal_range(StringRef Key) const770 DWARFDebugNames::NameIndex::equal_range(StringRef Key) const {
771 return make_range(ValueIterator(*this, Key), ValueIterator());
772 }
773
dump(raw_ostream & OS) const774 LLVM_DUMP_METHOD void DWARFDebugNames::dump(raw_ostream &OS) const {
775 ScopedPrinter W(OS);
776 for (const NameIndex &NI : NameIndices)
777 NI.dump(W);
778 }
779
780 Optional<uint32_t>
findEntryOffsetInCurrentIndex()781 DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() {
782 const Header &Hdr = CurrentIndex->Hdr;
783 if (Hdr.BucketCount == 0) {
784 // No Hash Table, We need to search through all names in the Name Index.
785 for (NameTableEntry NTE : *CurrentIndex) {
786 if (NTE.getString() == Key)
787 return NTE.getEntryOffset();
788 }
789 return None;
790 }
791
792 // The Name Index has a Hash Table, so use that to speed up the search.
793 // Compute the Key Hash, if it has not been done already.
794 if (!Hash)
795 Hash = caseFoldingDjbHash(Key);
796 uint32_t Bucket = *Hash % Hdr.BucketCount;
797 uint32_t Index = CurrentIndex->getBucketArrayEntry(Bucket);
798 if (Index == 0)
799 return None; // Empty bucket
800
801 for (; Index <= Hdr.NameCount; ++Index) {
802 uint32_t Hash = CurrentIndex->getHashArrayEntry(Index);
803 if (Hash % Hdr.BucketCount != Bucket)
804 return None; // End of bucket
805
806 NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index);
807 if (NTE.getString() == Key)
808 return NTE.getEntryOffset();
809 }
810 return None;
811 }
812
getEntryAtCurrentOffset()813 bool DWARFDebugNames::ValueIterator::getEntryAtCurrentOffset() {
814 auto EntryOr = CurrentIndex->getEntry(&DataOffset);
815 if (!EntryOr) {
816 consumeError(EntryOr.takeError());
817 return false;
818 }
819 CurrentEntry = std::move(*EntryOr);
820 return true;
821 }
822
findInCurrentIndex()823 bool DWARFDebugNames::ValueIterator::findInCurrentIndex() {
824 Optional<uint32_t> Offset = findEntryOffsetInCurrentIndex();
825 if (!Offset)
826 return false;
827 DataOffset = *Offset;
828 return getEntryAtCurrentOffset();
829 }
830
searchFromStartOfCurrentIndex()831 void DWARFDebugNames::ValueIterator::searchFromStartOfCurrentIndex() {
832 for (const NameIndex *End = CurrentIndex->Section.NameIndices.end();
833 CurrentIndex != End; ++CurrentIndex) {
834 if (findInCurrentIndex())
835 return;
836 }
837 setEnd();
838 }
839
next()840 void DWARFDebugNames::ValueIterator::next() {
841 assert(CurrentIndex && "Incrementing an end() iterator?");
842
843 // First try the next entry in the current Index.
844 if (getEntryAtCurrentOffset())
845 return;
846
847 // If we're a local iterator or we have reached the last Index, we're done.
848 if (IsLocal || CurrentIndex == &CurrentIndex->Section.NameIndices.back()) {
849 setEnd();
850 return;
851 }
852
853 // Otherwise, try the next index.
854 ++CurrentIndex;
855 searchFromStartOfCurrentIndex();
856 }
857
ValueIterator(const DWARFDebugNames & AccelTable,StringRef Key)858 DWARFDebugNames::ValueIterator::ValueIterator(const DWARFDebugNames &AccelTable,
859 StringRef Key)
860 : CurrentIndex(AccelTable.NameIndices.begin()), IsLocal(false), Key(Key) {
861 searchFromStartOfCurrentIndex();
862 }
863
ValueIterator(const DWARFDebugNames::NameIndex & NI,StringRef Key)864 DWARFDebugNames::ValueIterator::ValueIterator(
865 const DWARFDebugNames::NameIndex &NI, StringRef Key)
866 : CurrentIndex(&NI), IsLocal(true), Key(Key) {
867 if (!findInCurrentIndex())
868 setEnd();
869 }
870
871 iterator_range<DWARFDebugNames::ValueIterator>
equal_range(StringRef Key) const872 DWARFDebugNames::equal_range(StringRef Key) const {
873 if (NameIndices.empty())
874 return make_range(ValueIterator(), ValueIterator());
875 return make_range(ValueIterator(*this, Key), ValueIterator());
876 }
877
878 const DWARFDebugNames::NameIndex *
getCUNameIndex(uint32_t CUOffset)879 DWARFDebugNames::getCUNameIndex(uint32_t CUOffset) {
880 if (CUToNameIndex.size() == 0 && NameIndices.size() > 0) {
881 for (const auto &NI : *this) {
882 for (uint32_t CU = 0; CU < NI.getCUCount(); ++CU)
883 CUToNameIndex.try_emplace(NI.getCUOffset(CU), &NI);
884 }
885 }
886 return CUToNameIndex.lookup(CUOffset);
887 }
888