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