1e94042caSEugene Zelenko //===- DWARFAcceleratorTable.cpp ------------------------------------------===// 282af9438SZachary Turner // 382af9438SZachary Turner // The LLVM Compiler Infrastructure 482af9438SZachary Turner // 582af9438SZachary Turner // This file is distributed under the University of Illinois Open Source 682af9438SZachary Turner // License. See LICENSE.TXT for details. 782af9438SZachary Turner // 882af9438SZachary Turner //===----------------------------------------------------------------------===// 982af9438SZachary Turner 1082af9438SZachary Turner #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" 11264b5d9eSZachary Turner 126bda14b3SChandler Carruth #include "llvm/ADT/SmallVector.h" 13264b5d9eSZachary Turner #include "llvm/BinaryFormat/Dwarf.h" 14e94042caSEugene Zelenko #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" 15e94042caSEugene Zelenko #include "llvm/Support/Compiler.h" 1692ac9d3eSJonas Devlieghere #include "llvm/Support/DJB.h" 17*cba595daSVictor Leschuk #include "llvm/Support/Errc.h" 1882af9438SZachary Turner #include "llvm/Support/Format.h" 199025f955SPavel Labath #include "llvm/Support/FormatVariadic.h" 203c9a918cSPavel Labath #include "llvm/Support/ScopedPrinter.h" 2182af9438SZachary Turner #include "llvm/Support/raw_ostream.h" 22e94042caSEugene Zelenko #include <cstddef> 23e94042caSEugene Zelenko #include <cstdint> 24e94042caSEugene Zelenko #include <utility> 2582af9438SZachary Turner 26e94042caSEugene Zelenko using namespace llvm; 2782af9438SZachary Turner 283c9a918cSPavel Labath namespace { 299025f955SPavel Labath struct Atom { 303c9a918cSPavel Labath unsigned Value; 313c9a918cSPavel Labath }; 323c9a918cSPavel Labath 339025f955SPavel Labath static raw_ostream &operator<<(raw_ostream &OS, const Atom &A) { 349025f955SPavel Labath StringRef Str = dwarf::AtomTypeString(A.Value); 353c9a918cSPavel Labath if (!Str.empty()) 363c9a918cSPavel Labath return OS << Str; 379025f955SPavel Labath return OS << "DW_ATOM_unknown_" << format("%x", A.Value); 383c9a918cSPavel Labath } 393c9a918cSPavel Labath } // namespace 403c9a918cSPavel Labath 419025f955SPavel Labath static Atom formatAtom(unsigned Atom) { return {Atom}; } 423c9a918cSPavel Labath 433c9a918cSPavel Labath DWARFAcceleratorTable::~DWARFAcceleratorTable() = default; 443c9a918cSPavel Labath 459b36fd25SPavel Labath llvm::Error AppleAcceleratorTable::extract() { 4682af9438SZachary Turner uint32_t Offset = 0; 4782af9438SZachary Turner 4882af9438SZachary Turner // Check that we can at least read the header. 4982af9438SZachary Turner if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength) + 4)) 50*cba595daSVictor Leschuk return createStringError(errc::illegal_byte_sequence, 51*cba595daSVictor Leschuk "Section too small: cannot read header."); 5282af9438SZachary Turner 5382af9438SZachary Turner Hdr.Magic = AccelSection.getU32(&Offset); 5482af9438SZachary Turner Hdr.Version = AccelSection.getU16(&Offset); 5582af9438SZachary Turner Hdr.HashFunction = AccelSection.getU16(&Offset); 56394e8056SPavel Labath Hdr.BucketCount = AccelSection.getU32(&Offset); 57394e8056SPavel Labath Hdr.HashCount = AccelSection.getU32(&Offset); 5882af9438SZachary Turner Hdr.HeaderDataLength = AccelSection.getU32(&Offset); 5982af9438SZachary Turner 6082af9438SZachary Turner // Check that we can read all the hashes and offsets from the 6182af9438SZachary Turner // section (see SourceLevelDebugging.rst for the structure of the index). 62ba915897SJonas Devlieghere // We need to substract one because we're checking for an *offset* which is 63ba915897SJonas Devlieghere // equal to the size for an empty table and hence pointer after the section. 6482af9438SZachary Turner if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength + 65394e8056SPavel Labath Hdr.BucketCount * 4 + Hdr.HashCount * 8 - 1)) 66*cba595daSVictor Leschuk return createStringError( 67*cba595daSVictor Leschuk errc::illegal_byte_sequence, 68*cba595daSVictor Leschuk "Section too small: cannot read buckets and hashes."); 6982af9438SZachary Turner 7082af9438SZachary Turner HdrData.DIEOffsetBase = AccelSection.getU32(&Offset); 7182af9438SZachary Turner uint32_t NumAtoms = AccelSection.getU32(&Offset); 7282af9438SZachary Turner 7382af9438SZachary Turner for (unsigned i = 0; i < NumAtoms; ++i) { 7482af9438SZachary Turner uint16_t AtomType = AccelSection.getU16(&Offset); 756c273763SGreg Clayton auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset)); 7682af9438SZachary Turner HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm)); 7782af9438SZachary Turner } 7882af9438SZachary Turner 7999fdb9d9SAdrian Prantl IsValid = true; 80ba915897SJonas Devlieghere return Error::success(); 8182af9438SZachary Turner } 8282af9438SZachary Turner 83394e8056SPavel Labath uint32_t AppleAcceleratorTable::getNumBuckets() { return Hdr.BucketCount; } 84394e8056SPavel Labath uint32_t AppleAcceleratorTable::getNumHashes() { return Hdr.HashCount; } 859b36fd25SPavel Labath uint32_t AppleAcceleratorTable::getSizeHdr() { return sizeof(Hdr); } 869b36fd25SPavel Labath uint32_t AppleAcceleratorTable::getHeaderDataLength() { 87e41823bbSSpyridoula Gravani return Hdr.HeaderDataLength; 88e41823bbSSpyridoula Gravani } 89e41823bbSSpyridoula Gravani 909b36fd25SPavel Labath ArrayRef<std::pair<AppleAcceleratorTable::HeaderData::AtomType, 919b36fd25SPavel Labath AppleAcceleratorTable::HeaderData::Form>> 929b36fd25SPavel Labath AppleAcceleratorTable::getAtomsDesc() { 93837c110cSSpyridoula Gravani return HdrData.Atoms; 94837c110cSSpyridoula Gravani } 95837c110cSSpyridoula Gravani 969b36fd25SPavel Labath bool AppleAcceleratorTable::validateForms() { 97837c110cSSpyridoula Gravani for (auto Atom : getAtomsDesc()) { 98837c110cSSpyridoula Gravani DWARFFormValue FormValue(Atom.second); 99837c110cSSpyridoula Gravani switch (Atom.first) { 100837c110cSSpyridoula Gravani case dwarf::DW_ATOM_die_offset: 10170d35e10SSpyridoula Gravani case dwarf::DW_ATOM_die_tag: 10270d35e10SSpyridoula Gravani case dwarf::DW_ATOM_type_flags: 103837c110cSSpyridoula Gravani if ((!FormValue.isFormClass(DWARFFormValue::FC_Constant) && 104837c110cSSpyridoula Gravani !FormValue.isFormClass(DWARFFormValue::FC_Flag)) || 105837c110cSSpyridoula Gravani FormValue.getForm() == dwarf::DW_FORM_sdata) 106837c110cSSpyridoula Gravani return false; 1070e6694d1SAdrian Prantl break; 108837c110cSSpyridoula Gravani default: 109837c110cSSpyridoula Gravani break; 110837c110cSSpyridoula Gravani } 111837c110cSSpyridoula Gravani } 112837c110cSSpyridoula Gravani return true; 113837c110cSSpyridoula Gravani } 114837c110cSSpyridoula Gravani 11570d35e10SSpyridoula Gravani std::pair<uint32_t, dwarf::Tag> 1169b36fd25SPavel Labath AppleAcceleratorTable::readAtoms(uint32_t &HashDataOffset) { 117837c110cSSpyridoula Gravani uint32_t DieOffset = dwarf::DW_INVALID_OFFSET; 11870d35e10SSpyridoula Gravani dwarf::Tag DieTag = dwarf::DW_TAG_null; 119322711f5SPavel Labath dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32}; 120837c110cSSpyridoula Gravani 121837c110cSSpyridoula Gravani for (auto Atom : getAtomsDesc()) { 122837c110cSSpyridoula Gravani DWARFFormValue FormValue(Atom.second); 123e5400f8aSPaul Robinson FormValue.extractValue(AccelSection, &HashDataOffset, FormParams); 124837c110cSSpyridoula Gravani switch (Atom.first) { 125837c110cSSpyridoula Gravani case dwarf::DW_ATOM_die_offset: 126837c110cSSpyridoula Gravani DieOffset = *FormValue.getAsUnsignedConstant(); 127837c110cSSpyridoula Gravani break; 12870d35e10SSpyridoula Gravani case dwarf::DW_ATOM_die_tag: 12970d35e10SSpyridoula Gravani DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant(); 13070d35e10SSpyridoula Gravani break; 131837c110cSSpyridoula Gravani default: 132837c110cSSpyridoula Gravani break; 133837c110cSSpyridoula Gravani } 134837c110cSSpyridoula Gravani } 13570d35e10SSpyridoula Gravani return {DieOffset, DieTag}; 136837c110cSSpyridoula Gravani } 137837c110cSSpyridoula Gravani 138394e8056SPavel Labath void AppleAcceleratorTable::Header::dump(ScopedPrinter &W) const { 139394e8056SPavel Labath DictScope HeaderScope(W, "Header"); 140394e8056SPavel Labath W.printHex("Magic", Magic); 141394e8056SPavel Labath W.printHex("Version", Version); 142394e8056SPavel Labath W.printHex("Hash function", HashFunction); 143394e8056SPavel Labath W.printNumber("Bucket count", BucketCount); 144394e8056SPavel Labath W.printNumber("Hashes count", HashCount); 145394e8056SPavel Labath W.printNumber("HeaderData length", HeaderDataLength); 146394e8056SPavel Labath } 147394e8056SPavel Labath 14847c3472cSPavel Labath Optional<uint64_t> AppleAcceleratorTable::HeaderData::extractOffset( 14947c3472cSPavel Labath Optional<DWARFFormValue> Value) const { 15047c3472cSPavel Labath if (!Value) 15147c3472cSPavel Labath return None; 15247c3472cSPavel Labath 15347c3472cSPavel Labath switch (Value->getForm()) { 15447c3472cSPavel Labath case dwarf::DW_FORM_ref1: 15547c3472cSPavel Labath case dwarf::DW_FORM_ref2: 15647c3472cSPavel Labath case dwarf::DW_FORM_ref4: 15747c3472cSPavel Labath case dwarf::DW_FORM_ref8: 15847c3472cSPavel Labath case dwarf::DW_FORM_ref_udata: 15947c3472cSPavel Labath return Value->getRawUValue() + DIEOffsetBase; 16047c3472cSPavel Labath default: 16147c3472cSPavel Labath return Value->getAsSectionOffset(); 16247c3472cSPavel Labath } 16347c3472cSPavel Labath } 16447c3472cSPavel Labath 165394e8056SPavel Labath bool AppleAcceleratorTable::dumpName(ScopedPrinter &W, 166394e8056SPavel Labath SmallVectorImpl<DWARFFormValue> &AtomForms, 167394e8056SPavel Labath uint32_t *DataOffset) const { 168322711f5SPavel Labath dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32}; 169394e8056SPavel Labath uint32_t NameOffset = *DataOffset; 170394e8056SPavel Labath if (!AccelSection.isValidOffsetForDataOfSize(*DataOffset, 4)) { 171394e8056SPavel Labath W.printString("Incorrectly terminated list."); 172394e8056SPavel Labath return false; 173394e8056SPavel Labath } 174394e8056SPavel Labath unsigned StringOffset = AccelSection.getRelocatedValue(4, DataOffset); 175394e8056SPavel Labath if (!StringOffset) 176394e8056SPavel Labath return false; // End of list 177394e8056SPavel Labath 178394e8056SPavel Labath DictScope NameScope(W, ("Name@0x" + Twine::utohexstr(NameOffset)).str()); 179394e8056SPavel Labath W.startLine() << format("String: 0x%08x", StringOffset); 180394e8056SPavel Labath W.getOStream() << " \"" << StringSection.getCStr(&StringOffset) << "\"\n"; 181394e8056SPavel Labath 182394e8056SPavel Labath unsigned NumData = AccelSection.getU32(DataOffset); 183394e8056SPavel Labath for (unsigned Data = 0; Data < NumData; ++Data) { 184394e8056SPavel Labath ListScope DataScope(W, ("Data " + Twine(Data)).str()); 185394e8056SPavel Labath unsigned i = 0; 186394e8056SPavel Labath for (auto &Atom : AtomForms) { 187327e7a16SJonas Devlieghere W.startLine() << format("Atom[%d]: ", i); 188327e7a16SJonas Devlieghere if (Atom.extractValue(AccelSection, DataOffset, FormParams)) { 189394e8056SPavel Labath Atom.dump(W.getOStream()); 190327e7a16SJonas Devlieghere if (Optional<uint64_t> Val = Atom.getAsUnsignedConstant()) { 191327e7a16SJonas Devlieghere StringRef Str = dwarf::AtomValueString(HdrData.Atoms[i].first, *Val); 192327e7a16SJonas Devlieghere if (!Str.empty()) 193327e7a16SJonas Devlieghere W.getOStream() << " (" << Str << ")"; 194327e7a16SJonas Devlieghere } 195327e7a16SJonas Devlieghere } else 196394e8056SPavel Labath W.getOStream() << "Error extracting the value"; 197394e8056SPavel Labath W.getOStream() << "\n"; 198327e7a16SJonas Devlieghere i++; 199394e8056SPavel Labath } 200394e8056SPavel Labath } 201394e8056SPavel Labath return true; // more entries follow 202394e8056SPavel Labath } 203394e8056SPavel Labath 2049b36fd25SPavel Labath LLVM_DUMP_METHOD void AppleAcceleratorTable::dump(raw_ostream &OS) const { 20599fdb9d9SAdrian Prantl if (!IsValid) 20699fdb9d9SAdrian Prantl return; 20799fdb9d9SAdrian Prantl 208394e8056SPavel Labath ScopedPrinter W(OS); 20982af9438SZachary Turner 210394e8056SPavel Labath Hdr.dump(W); 211394e8056SPavel Labath 212394e8056SPavel Labath W.printNumber("DIE offset base", HdrData.DIEOffsetBase); 2133460957eSPavel Labath W.printNumber("Number of atoms", uint64_t(HdrData.Atoms.size())); 21482af9438SZachary Turner SmallVector<DWARFFormValue, 3> AtomForms; 215394e8056SPavel Labath { 216394e8056SPavel Labath ListScope AtomsScope(W, "Atoms"); 217394e8056SPavel Labath unsigned i = 0; 21882af9438SZachary Turner for (const auto &Atom : HdrData.Atoms) { 219394e8056SPavel Labath DictScope AtomScope(W, ("Atom " + Twine(i++)).str()); 220394e8056SPavel Labath W.startLine() << "Type: " << formatAtom(Atom.first) << '\n'; 2219025f955SPavel Labath W.startLine() << "Form: " << formatv("{0}", Atom.second) << '\n'; 22282af9438SZachary Turner AtomForms.push_back(DWARFFormValue(Atom.second)); 22382af9438SZachary Turner } 224394e8056SPavel Labath } 22582af9438SZachary Turner 22682af9438SZachary Turner // Now go through the actual tables and dump them. 22782af9438SZachary Turner uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength; 228394e8056SPavel Labath unsigned HashesBase = Offset + Hdr.BucketCount * 4; 229394e8056SPavel Labath unsigned OffsetsBase = HashesBase + Hdr.HashCount * 4; 23082af9438SZachary Turner 231394e8056SPavel Labath for (unsigned Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) { 23282af9438SZachary Turner unsigned Index = AccelSection.getU32(&Offset); 23382af9438SZachary Turner 234394e8056SPavel Labath ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str()); 23582af9438SZachary Turner if (Index == UINT32_MAX) { 236394e8056SPavel Labath W.printString("EMPTY"); 23782af9438SZachary Turner continue; 23882af9438SZachary Turner } 23982af9438SZachary Turner 240394e8056SPavel Labath for (unsigned HashIdx = Index; HashIdx < Hdr.HashCount; ++HashIdx) { 24182af9438SZachary Turner unsigned HashOffset = HashesBase + HashIdx*4; 24282af9438SZachary Turner unsigned OffsetsOffset = OffsetsBase + HashIdx*4; 24382af9438SZachary Turner uint32_t Hash = AccelSection.getU32(&HashOffset); 24482af9438SZachary Turner 245394e8056SPavel Labath if (Hash % Hdr.BucketCount != Bucket) 24682af9438SZachary Turner break; 24782af9438SZachary Turner 24882af9438SZachary Turner unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); 249394e8056SPavel Labath ListScope HashScope(W, ("Hash 0x" + Twine::utohexstr(Hash)).str()); 25082af9438SZachary Turner if (!AccelSection.isValidOffset(DataOffset)) { 251394e8056SPavel Labath W.printString("Invalid section offset"); 25282af9438SZachary Turner continue; 25382af9438SZachary Turner } 254394e8056SPavel Labath while (dumpName(W, AtomForms, &DataOffset)) 255394e8056SPavel Labath /*empty*/; 25682af9438SZachary Turner } 25782af9438SZachary Turner } 25882af9438SZachary Turner } 25999fdb9d9SAdrian Prantl 260d99072bcSPavel Labath AppleAcceleratorTable::Entry::Entry( 261d99072bcSPavel Labath const AppleAcceleratorTable::HeaderData &HdrData) 262d99072bcSPavel Labath : HdrData(&HdrData) { 263d99072bcSPavel Labath Values.reserve(HdrData.Atoms.size()); 264d99072bcSPavel Labath for (const auto &Atom : HdrData.Atoms) 265d99072bcSPavel Labath Values.push_back(DWARFFormValue(Atom.second)); 266d99072bcSPavel Labath } 267d99072bcSPavel Labath 268d99072bcSPavel Labath void AppleAcceleratorTable::Entry::extract( 269d99072bcSPavel Labath const AppleAcceleratorTable &AccelTable, uint32_t *Offset) { 270d99072bcSPavel Labath 271322711f5SPavel Labath dwarf::FormParams FormParams = {AccelTable.Hdr.Version, 0, 272d99072bcSPavel Labath dwarf::DwarfFormat::DWARF32}; 273d99072bcSPavel Labath for (auto &Atom : Values) 274d99072bcSPavel Labath Atom.extractValue(AccelTable.AccelSection, Offset, FormParams); 275d99072bcSPavel Labath } 276d99072bcSPavel Labath 277d99072bcSPavel Labath Optional<DWARFFormValue> 278d99072bcSPavel Labath AppleAcceleratorTable::Entry::lookup(HeaderData::AtomType Atom) const { 279d99072bcSPavel Labath assert(HdrData && "Dereferencing end iterator?"); 280d99072bcSPavel Labath assert(HdrData->Atoms.size() == Values.size()); 281d99072bcSPavel Labath for (const auto &Tuple : zip_first(HdrData->Atoms, Values)) { 282d99072bcSPavel Labath if (std::get<0>(Tuple).first == Atom) 283d99072bcSPavel Labath return std::get<1>(Tuple); 284d99072bcSPavel Labath } 285d99072bcSPavel Labath return None; 286d99072bcSPavel Labath } 287d99072bcSPavel Labath 28847c3472cSPavel Labath Optional<uint64_t> AppleAcceleratorTable::Entry::getDIESectionOffset() const { 28947c3472cSPavel Labath return HdrData->extractOffset(lookup(dwarf::DW_ATOM_die_offset)); 290d99072bcSPavel Labath } 291d99072bcSPavel Labath 292d99072bcSPavel Labath Optional<uint64_t> AppleAcceleratorTable::Entry::getCUOffset() const { 29347c3472cSPavel Labath return HdrData->extractOffset(lookup(dwarf::DW_ATOM_cu_offset)); 294d99072bcSPavel Labath } 295d99072bcSPavel Labath 296d99072bcSPavel Labath Optional<dwarf::Tag> AppleAcceleratorTable::Entry::getTag() const { 297d99072bcSPavel Labath Optional<DWARFFormValue> Tag = lookup(dwarf::DW_ATOM_die_tag); 298d99072bcSPavel Labath if (!Tag) 299d99072bcSPavel Labath return None; 300d99072bcSPavel Labath if (Optional<uint64_t> Value = Tag->getAsUnsignedConstant()) 301d99072bcSPavel Labath return dwarf::Tag(*Value); 302d99072bcSPavel Labath return None; 303d99072bcSPavel Labath } 304d99072bcSPavel Labath 3059b36fd25SPavel Labath AppleAcceleratorTable::ValueIterator::ValueIterator( 3069b36fd25SPavel Labath const AppleAcceleratorTable &AccelTable, unsigned Offset) 307d99072bcSPavel Labath : AccelTable(&AccelTable), Current(AccelTable.HdrData), DataOffset(Offset) { 30899fdb9d9SAdrian Prantl if (!AccelTable.AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) 30999fdb9d9SAdrian Prantl return; 31099fdb9d9SAdrian Prantl 31199fdb9d9SAdrian Prantl // Read the first entry. 31299fdb9d9SAdrian Prantl NumData = AccelTable.AccelSection.getU32(&DataOffset); 31399fdb9d9SAdrian Prantl Next(); 31499fdb9d9SAdrian Prantl } 31599fdb9d9SAdrian Prantl 3169b36fd25SPavel Labath void AppleAcceleratorTable::ValueIterator::Next() { 31799fdb9d9SAdrian Prantl assert(NumData > 0 && "attempted to increment iterator past the end"); 31899fdb9d9SAdrian Prantl auto &AccelSection = AccelTable->AccelSection; 31999fdb9d9SAdrian Prantl if (Data >= NumData || 32099fdb9d9SAdrian Prantl !AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) { 32199fdb9d9SAdrian Prantl NumData = 0; 32259870af6SPavel Labath DataOffset = 0; 32399fdb9d9SAdrian Prantl return; 32499fdb9d9SAdrian Prantl } 325d99072bcSPavel Labath Current.extract(*AccelTable, &DataOffset); 32699fdb9d9SAdrian Prantl ++Data; 32799fdb9d9SAdrian Prantl } 32899fdb9d9SAdrian Prantl 3299b36fd25SPavel Labath iterator_range<AppleAcceleratorTable::ValueIterator> 3309b36fd25SPavel Labath AppleAcceleratorTable::equal_range(StringRef Key) const { 33199fdb9d9SAdrian Prantl if (!IsValid) 33299fdb9d9SAdrian Prantl return make_range(ValueIterator(), ValueIterator()); 33399fdb9d9SAdrian Prantl 33499fdb9d9SAdrian Prantl // Find the bucket. 33592ac9d3eSJonas Devlieghere unsigned HashValue = djbHash(Key); 336394e8056SPavel Labath unsigned Bucket = HashValue % Hdr.BucketCount; 33799fdb9d9SAdrian Prantl unsigned BucketBase = sizeof(Hdr) + Hdr.HeaderDataLength; 338394e8056SPavel Labath unsigned HashesBase = BucketBase + Hdr.BucketCount * 4; 339394e8056SPavel Labath unsigned OffsetsBase = HashesBase + Hdr.HashCount * 4; 34099fdb9d9SAdrian Prantl 34199fdb9d9SAdrian Prantl unsigned BucketOffset = BucketBase + Bucket * 4; 34299fdb9d9SAdrian Prantl unsigned Index = AccelSection.getU32(&BucketOffset); 34399fdb9d9SAdrian Prantl 34499fdb9d9SAdrian Prantl // Search through all hashes in the bucket. 345394e8056SPavel Labath for (unsigned HashIdx = Index; HashIdx < Hdr.HashCount; ++HashIdx) { 34699fdb9d9SAdrian Prantl unsigned HashOffset = HashesBase + HashIdx * 4; 34799fdb9d9SAdrian Prantl unsigned OffsetsOffset = OffsetsBase + HashIdx * 4; 34899fdb9d9SAdrian Prantl uint32_t Hash = AccelSection.getU32(&HashOffset); 34999fdb9d9SAdrian Prantl 350394e8056SPavel Labath if (Hash % Hdr.BucketCount != Bucket) 35199fdb9d9SAdrian Prantl // We are already in the next bucket. 35299fdb9d9SAdrian Prantl break; 35399fdb9d9SAdrian Prantl 35499fdb9d9SAdrian Prantl unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); 35599fdb9d9SAdrian Prantl unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset); 35699fdb9d9SAdrian Prantl if (!StringOffset) 35799fdb9d9SAdrian Prantl break; 35899fdb9d9SAdrian Prantl 35999fdb9d9SAdrian Prantl // Finally, compare the key. 36099fdb9d9SAdrian Prantl if (Key == StringSection.getCStr(&StringOffset)) 36199fdb9d9SAdrian Prantl return make_range({*this, DataOffset}, ValueIterator()); 36299fdb9d9SAdrian Prantl } 36399fdb9d9SAdrian Prantl return make_range(ValueIterator(), ValueIterator()); 36499fdb9d9SAdrian Prantl } 3653c9a918cSPavel Labath 3663c9a918cSPavel Labath void DWARFDebugNames::Header::dump(ScopedPrinter &W) const { 3673c9a918cSPavel Labath DictScope HeaderScope(W, "Header"); 3683c9a918cSPavel Labath W.printHex("Length", UnitLength); 3693c9a918cSPavel Labath W.printNumber("Version", Version); 3703c9a918cSPavel Labath W.printHex("Padding", Padding); 3713c9a918cSPavel Labath W.printNumber("CU count", CompUnitCount); 3723c9a918cSPavel Labath W.printNumber("Local TU count", LocalTypeUnitCount); 3733c9a918cSPavel Labath W.printNumber("Foreign TU count", ForeignTypeUnitCount); 3743c9a918cSPavel Labath W.printNumber("Bucket count", BucketCount); 3753c9a918cSPavel Labath W.printNumber("Name count", NameCount); 3763c9a918cSPavel Labath W.printHex("Abbreviations table size", AbbrevTableSize); 3773c9a918cSPavel Labath W.startLine() << "Augmentation: '" << AugmentationString << "'\n"; 3783c9a918cSPavel Labath } 3793c9a918cSPavel Labath 3803c9a918cSPavel Labath llvm::Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS, 3813c9a918cSPavel Labath uint32_t *Offset) { 3823c9a918cSPavel Labath // Check that we can read the fixed-size part. 383e7264106SPavel Labath if (!AS.isValidOffset(*Offset + sizeof(HeaderPOD) - 1)) 384*cba595daSVictor Leschuk return createStringError(errc::illegal_byte_sequence, 385*cba595daSVictor Leschuk "Section too small: cannot read header."); 3863c9a918cSPavel Labath 3873c9a918cSPavel Labath UnitLength = AS.getU32(Offset); 3883c9a918cSPavel Labath Version = AS.getU16(Offset); 3893c9a918cSPavel Labath Padding = AS.getU16(Offset); 3903c9a918cSPavel Labath CompUnitCount = AS.getU32(Offset); 3913c9a918cSPavel Labath LocalTypeUnitCount = AS.getU32(Offset); 3923c9a918cSPavel Labath ForeignTypeUnitCount = AS.getU32(Offset); 3933c9a918cSPavel Labath BucketCount = AS.getU32(Offset); 3943c9a918cSPavel Labath NameCount = AS.getU32(Offset); 3953c9a918cSPavel Labath AbbrevTableSize = AS.getU32(Offset); 396ea0f841cSPavel Labath AugmentationStringSize = alignTo(AS.getU32(Offset), 4); 3973c9a918cSPavel Labath 3983c9a918cSPavel Labath if (!AS.isValidOffsetForDataOfSize(*Offset, AugmentationStringSize)) 399*cba595daSVictor Leschuk return createStringError( 400*cba595daSVictor Leschuk errc::illegal_byte_sequence, 401*cba595daSVictor Leschuk "Section too small: cannot read header augmentation."); 4023c9a918cSPavel Labath AugmentationString.resize(AugmentationStringSize); 4033c9a918cSPavel Labath AS.getU8(Offset, reinterpret_cast<uint8_t *>(AugmentationString.data()), 4043c9a918cSPavel Labath AugmentationStringSize); 4053c9a918cSPavel Labath return Error::success(); 4063c9a918cSPavel Labath } 4073c9a918cSPavel Labath 4083c9a918cSPavel Labath void DWARFDebugNames::Abbrev::dump(ScopedPrinter &W) const { 4093c9a918cSPavel Labath DictScope AbbrevScope(W, ("Abbreviation 0x" + Twine::utohexstr(Code)).str()); 4109025f955SPavel Labath W.startLine() << formatv("Tag: {0}\n", Tag); 4113c9a918cSPavel Labath 4129025f955SPavel Labath for (const auto &Attr : Attributes) 4139025f955SPavel Labath W.startLine() << formatv("{0}: {1}\n", Attr.Index, Attr.Form); 4143c9a918cSPavel Labath } 4153c9a918cSPavel Labath 4163c9a918cSPavel Labath static constexpr DWARFDebugNames::AttributeEncoding sentinelAttrEnc() { 4173c9a918cSPavel Labath return {dwarf::Index(0), dwarf::Form(0)}; 4183c9a918cSPavel Labath } 4193c9a918cSPavel Labath 4203c9a918cSPavel Labath static bool isSentinel(const DWARFDebugNames::AttributeEncoding &AE) { 4213c9a918cSPavel Labath return AE == sentinelAttrEnc(); 4223c9a918cSPavel Labath } 4233c9a918cSPavel Labath 4243c9a918cSPavel Labath static DWARFDebugNames::Abbrev sentinelAbbrev() { 4253c9a918cSPavel Labath return DWARFDebugNames::Abbrev(0, dwarf::Tag(0), {}); 4263c9a918cSPavel Labath } 4273c9a918cSPavel Labath 4283c9a918cSPavel Labath static bool isSentinel(const DWARFDebugNames::Abbrev &Abbr) { 4293c9a918cSPavel Labath return Abbr.Code == 0; 4303c9a918cSPavel Labath } 4313c9a918cSPavel Labath 4323c9a918cSPavel Labath DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getEmptyKey() { 4333c9a918cSPavel Labath return sentinelAbbrev(); 4343c9a918cSPavel Labath } 4353c9a918cSPavel Labath 4363c9a918cSPavel Labath DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getTombstoneKey() { 4373c9a918cSPavel Labath return DWARFDebugNames::Abbrev(~0, dwarf::Tag(0), {}); 4383c9a918cSPavel Labath } 4393c9a918cSPavel Labath 4403c9a918cSPavel Labath Expected<DWARFDebugNames::AttributeEncoding> 4413c9a918cSPavel Labath DWARFDebugNames::NameIndex::extractAttributeEncoding(uint32_t *Offset) { 4423c9a918cSPavel Labath if (*Offset >= EntriesBase) { 443*cba595daSVictor Leschuk return createStringError(errc::illegal_byte_sequence, 444*cba595daSVictor Leschuk "Incorrectly terminated abbreviation table."); 4453c9a918cSPavel Labath } 4463c9a918cSPavel Labath 4473c9a918cSPavel Labath uint32_t Index = Section.AccelSection.getULEB128(Offset); 4483c9a918cSPavel Labath uint32_t Form = Section.AccelSection.getULEB128(Offset); 4493c9a918cSPavel Labath return AttributeEncoding(dwarf::Index(Index), dwarf::Form(Form)); 4503c9a918cSPavel Labath } 4513c9a918cSPavel Labath 4523c9a918cSPavel Labath Expected<std::vector<DWARFDebugNames::AttributeEncoding>> 4533c9a918cSPavel Labath DWARFDebugNames::NameIndex::extractAttributeEncodings(uint32_t *Offset) { 4543c9a918cSPavel Labath std::vector<AttributeEncoding> Result; 4553c9a918cSPavel Labath for (;;) { 4563c9a918cSPavel Labath auto AttrEncOr = extractAttributeEncoding(Offset); 4573c9a918cSPavel Labath if (!AttrEncOr) 4583c9a918cSPavel Labath return AttrEncOr.takeError(); 4593c9a918cSPavel Labath if (isSentinel(*AttrEncOr)) 4603c9a918cSPavel Labath return std::move(Result); 4613c9a918cSPavel Labath 4623c9a918cSPavel Labath Result.emplace_back(*AttrEncOr); 4633c9a918cSPavel Labath } 4643c9a918cSPavel Labath } 4653c9a918cSPavel Labath 4663c9a918cSPavel Labath Expected<DWARFDebugNames::Abbrev> 4673c9a918cSPavel Labath DWARFDebugNames::NameIndex::extractAbbrev(uint32_t *Offset) { 4683c9a918cSPavel Labath if (*Offset >= EntriesBase) { 469*cba595daSVictor Leschuk return createStringError(errc::illegal_byte_sequence, 470*cba595daSVictor Leschuk "Incorrectly terminated abbreviation table."); 4713c9a918cSPavel Labath } 4723c9a918cSPavel Labath 4733c9a918cSPavel Labath uint32_t Code = Section.AccelSection.getULEB128(Offset); 4743c9a918cSPavel Labath if (Code == 0) 4753c9a918cSPavel Labath return sentinelAbbrev(); 4763c9a918cSPavel Labath 4773c9a918cSPavel Labath uint32_t Tag = Section.AccelSection.getULEB128(Offset); 4783c9a918cSPavel Labath auto AttrEncOr = extractAttributeEncodings(Offset); 4793c9a918cSPavel Labath if (!AttrEncOr) 4803c9a918cSPavel Labath return AttrEncOr.takeError(); 4813c9a918cSPavel Labath return Abbrev(Code, dwarf::Tag(Tag), std::move(*AttrEncOr)); 4823c9a918cSPavel Labath } 4833c9a918cSPavel Labath 4843c9a918cSPavel Labath Error DWARFDebugNames::NameIndex::extract() { 4853c9a918cSPavel Labath const DWARFDataExtractor &AS = Section.AccelSection; 4863c9a918cSPavel Labath uint32_t Offset = Base; 4873c9a918cSPavel Labath if (Error E = Hdr.extract(AS, &Offset)) 4883c9a918cSPavel Labath return E; 4893c9a918cSPavel Labath 4903c9a918cSPavel Labath CUsBase = Offset; 4913c9a918cSPavel Labath Offset += Hdr.CompUnitCount * 4; 4923c9a918cSPavel Labath Offset += Hdr.LocalTypeUnitCount * 4; 4933c9a918cSPavel Labath Offset += Hdr.ForeignTypeUnitCount * 8; 4943c9a918cSPavel Labath BucketsBase = Offset; 4953c9a918cSPavel Labath Offset += Hdr.BucketCount * 4; 4963c9a918cSPavel Labath HashesBase = Offset; 4973c9a918cSPavel Labath if (Hdr.BucketCount > 0) 4983c9a918cSPavel Labath Offset += Hdr.NameCount * 4; 4993c9a918cSPavel Labath StringOffsetsBase = Offset; 5003c9a918cSPavel Labath Offset += Hdr.NameCount * 4; 5013c9a918cSPavel Labath EntryOffsetsBase = Offset; 5023c9a918cSPavel Labath Offset += Hdr.NameCount * 4; 5033c9a918cSPavel Labath 5043c9a918cSPavel Labath if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize)) 505*cba595daSVictor Leschuk return createStringError(errc::illegal_byte_sequence, 506*cba595daSVictor Leschuk "Section too small: cannot read abbreviations."); 5073c9a918cSPavel Labath 5083c9a918cSPavel Labath EntriesBase = Offset + Hdr.AbbrevTableSize; 5093c9a918cSPavel Labath 5103c9a918cSPavel Labath for (;;) { 5113c9a918cSPavel Labath auto AbbrevOr = extractAbbrev(&Offset); 5123c9a918cSPavel Labath if (!AbbrevOr) 5133c9a918cSPavel Labath return AbbrevOr.takeError(); 5143c9a918cSPavel Labath if (isSentinel(*AbbrevOr)) 5153c9a918cSPavel Labath return Error::success(); 5163c9a918cSPavel Labath 517*cba595daSVictor Leschuk if (!Abbrevs.insert(std::move(*AbbrevOr)).second) 518*cba595daSVictor Leschuk return createStringError(errc::invalid_argument, 519*cba595daSVictor Leschuk "Duplicate abbreviation code."); 5203c9a918cSPavel Labath } 5213c9a918cSPavel Labath } 522d99072bcSPavel Labath DWARFDebugNames::Entry::Entry(const NameIndex &NameIdx, const Abbrev &Abbr) 523d99072bcSPavel Labath : NameIdx(&NameIdx), Abbr(&Abbr) { 5243c9a918cSPavel Labath // This merely creates form values. It is up to the caller 5253c9a918cSPavel Labath // (NameIndex::getEntry) to populate them. 5263c9a918cSPavel Labath Values.reserve(Abbr.Attributes.size()); 5273c9a918cSPavel Labath for (const auto &Attr : Abbr.Attributes) 5283c9a918cSPavel Labath Values.emplace_back(Attr.Form); 5293c9a918cSPavel Labath } 5303c9a918cSPavel Labath 531d99072bcSPavel Labath Optional<DWARFFormValue> 532d99072bcSPavel Labath DWARFDebugNames::Entry::lookup(dwarf::Index Index) const { 533d99072bcSPavel Labath assert(Abbr->Attributes.size() == Values.size()); 534d99072bcSPavel Labath for (const auto &Tuple : zip_first(Abbr->Attributes, Values)) { 535d99072bcSPavel Labath if (std::get<0>(Tuple).Index == Index) 536d99072bcSPavel Labath return std::get<1>(Tuple); 537d99072bcSPavel Labath } 538d99072bcSPavel Labath return None; 539d99072bcSPavel Labath } 5403c9a918cSPavel Labath 54147c3472cSPavel Labath Optional<uint64_t> DWARFDebugNames::Entry::getDIEUnitOffset() const { 542d99072bcSPavel Labath if (Optional<DWARFFormValue> Off = lookup(dwarf::DW_IDX_die_offset)) 5432d1fc437SPavel Labath return Off->getAsReferenceUVal(); 544d99072bcSPavel Labath return None; 545d99072bcSPavel Labath } 546d99072bcSPavel Labath 547d99072bcSPavel Labath Optional<uint64_t> DWARFDebugNames::Entry::getCUIndex() const { 548d99072bcSPavel Labath if (Optional<DWARFFormValue> Off = lookup(dwarf::DW_IDX_compile_unit)) 549d99072bcSPavel Labath return Off->getAsUnsignedConstant(); 55047c3472cSPavel Labath // In a per-CU index, the entries without a DW_IDX_compile_unit attribute 55147c3472cSPavel Labath // implicitly refer to the single CU. 55247c3472cSPavel Labath if (NameIdx->getCUCount() == 1) 55347c3472cSPavel Labath return 0; 554d99072bcSPavel Labath return None; 555d99072bcSPavel Labath } 556d99072bcSPavel Labath 557d99072bcSPavel Labath Optional<uint64_t> DWARFDebugNames::Entry::getCUOffset() const { 558d99072bcSPavel Labath Optional<uint64_t> Index = getCUIndex(); 559d99072bcSPavel Labath if (!Index || *Index >= NameIdx->getCUCount()) 560d99072bcSPavel Labath return None; 561d99072bcSPavel Labath return NameIdx->getCUOffset(*Index); 562d99072bcSPavel Labath } 563d99072bcSPavel Labath 564d99072bcSPavel Labath void DWARFDebugNames::Entry::dump(ScopedPrinter &W) const { 565d99072bcSPavel Labath W.printHex("Abbrev", Abbr->Code); 5669025f955SPavel Labath W.startLine() << formatv("Tag: {0}\n", Abbr->Tag); 567d99072bcSPavel Labath assert(Abbr->Attributes.size() == Values.size()); 568d99072bcSPavel Labath for (const auto &Tuple : zip_first(Abbr->Attributes, Values)) { 5699025f955SPavel Labath W.startLine() << formatv("{0}: ", std::get<0>(Tuple).Index); 570d99072bcSPavel Labath std::get<1>(Tuple).dump(W.getOStream()); 5713c9a918cSPavel Labath W.getOStream() << '\n'; 5723c9a918cSPavel Labath } 5733c9a918cSPavel Labath } 5743c9a918cSPavel Labath 5753c9a918cSPavel Labath char DWARFDebugNames::SentinelError::ID; 5763c9a918cSPavel Labath std::error_code DWARFDebugNames::SentinelError::convertToErrorCode() const { 5773c9a918cSPavel Labath return inconvertibleErrorCode(); 5783c9a918cSPavel Labath } 5793c9a918cSPavel Labath 5803c9a918cSPavel Labath uint32_t DWARFDebugNames::NameIndex::getCUOffset(uint32_t CU) const { 5813c9a918cSPavel Labath assert(CU < Hdr.CompUnitCount); 5823c9a918cSPavel Labath uint32_t Offset = CUsBase + 4 * CU; 5833c9a918cSPavel Labath return Section.AccelSection.getRelocatedValue(4, &Offset); 5843c9a918cSPavel Labath } 5853c9a918cSPavel Labath 5863c9a918cSPavel Labath uint32_t DWARFDebugNames::NameIndex::getLocalTUOffset(uint32_t TU) const { 5873c9a918cSPavel Labath assert(TU < Hdr.LocalTypeUnitCount); 5883c9a918cSPavel Labath uint32_t Offset = CUsBase + Hdr.CompUnitCount * 4; 5893c9a918cSPavel Labath return Section.AccelSection.getRelocatedValue(4, &Offset); 5903c9a918cSPavel Labath } 5913c9a918cSPavel Labath 592d99072bcSPavel Labath uint64_t DWARFDebugNames::NameIndex::getForeignTUSignature(uint32_t TU) const { 5933c9a918cSPavel Labath assert(TU < Hdr.ForeignTypeUnitCount); 5943c9a918cSPavel Labath uint32_t Offset = CUsBase + (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) * 4; 5953c9a918cSPavel Labath return Section.AccelSection.getU64(&Offset); 5963c9a918cSPavel Labath } 5973c9a918cSPavel Labath 5983c9a918cSPavel Labath Expected<DWARFDebugNames::Entry> 5993c9a918cSPavel Labath DWARFDebugNames::NameIndex::getEntry(uint32_t *Offset) const { 6003c9a918cSPavel Labath const DWARFDataExtractor &AS = Section.AccelSection; 6013c9a918cSPavel Labath if (!AS.isValidOffset(*Offset)) 602*cba595daSVictor Leschuk return createStringError(errc::illegal_byte_sequence, 603*cba595daSVictor Leschuk "Incorrectly terminated entry list."); 6043c9a918cSPavel Labath 6053c9a918cSPavel Labath uint32_t AbbrevCode = AS.getULEB128(Offset); 6063c9a918cSPavel Labath if (AbbrevCode == 0) 6073c9a918cSPavel Labath return make_error<SentinelError>(); 6083c9a918cSPavel Labath 6093c9a918cSPavel Labath const auto AbbrevIt = Abbrevs.find_as(AbbrevCode); 6103c9a918cSPavel Labath if (AbbrevIt == Abbrevs.end()) 611*cba595daSVictor Leschuk return createStringError(errc::invalid_argument, "Invalid abbreviation."); 6123c9a918cSPavel Labath 613d99072bcSPavel Labath Entry E(*this, *AbbrevIt); 6143c9a918cSPavel Labath 615322711f5SPavel Labath dwarf::FormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32}; 6163c9a918cSPavel Labath for (auto &Value : E.Values) { 6173c9a918cSPavel Labath if (!Value.extractValue(AS, Offset, FormParams)) 618*cba595daSVictor Leschuk return createStringError(errc::io_error, 619*cba595daSVictor Leschuk "Error extracting index attribute values."); 6203c9a918cSPavel Labath } 6213c9a918cSPavel Labath return std::move(E); 6223c9a918cSPavel Labath } 6233c9a918cSPavel Labath 6243c9a918cSPavel Labath DWARFDebugNames::NameTableEntry 6253c9a918cSPavel Labath DWARFDebugNames::NameIndex::getNameTableEntry(uint32_t Index) const { 6263c9a918cSPavel Labath assert(0 < Index && Index <= Hdr.NameCount); 6273c9a918cSPavel Labath uint32_t StringOffsetOffset = StringOffsetsBase + 4 * (Index - 1); 6283c9a918cSPavel Labath uint32_t EntryOffsetOffset = EntryOffsetsBase + 4 * (Index - 1); 6293c9a918cSPavel Labath const DWARFDataExtractor &AS = Section.AccelSection; 6303c9a918cSPavel Labath 6313c9a918cSPavel Labath uint32_t StringOffset = AS.getRelocatedValue(4, &StringOffsetOffset); 6323c9a918cSPavel Labath uint32_t EntryOffset = AS.getU32(&EntryOffsetOffset); 6333c9a918cSPavel Labath EntryOffset += EntriesBase; 634d6ca0639SPavel Labath return {Section.StringSection, Index, StringOffset, EntryOffset}; 6353c9a918cSPavel Labath } 6363c9a918cSPavel Labath 6373c9a918cSPavel Labath uint32_t 6383c9a918cSPavel Labath DWARFDebugNames::NameIndex::getBucketArrayEntry(uint32_t Bucket) const { 6393c9a918cSPavel Labath assert(Bucket < Hdr.BucketCount); 6403c9a918cSPavel Labath uint32_t BucketOffset = BucketsBase + 4 * Bucket; 6413c9a918cSPavel Labath return Section.AccelSection.getU32(&BucketOffset); 6423c9a918cSPavel Labath } 6433c9a918cSPavel Labath 6443c9a918cSPavel Labath uint32_t DWARFDebugNames::NameIndex::getHashArrayEntry(uint32_t Index) const { 6453c9a918cSPavel Labath assert(0 < Index && Index <= Hdr.NameCount); 6463c9a918cSPavel Labath uint32_t HashOffset = HashesBase + 4 * (Index - 1); 6473c9a918cSPavel Labath return Section.AccelSection.getU32(&HashOffset); 6483c9a918cSPavel Labath } 6493c9a918cSPavel Labath 6503c9a918cSPavel Labath // Returns true if we should continue scanning for entries, false if this is the 6513c9a918cSPavel Labath // last (sentinel) entry). In case of a parsing error we also return false, as 6523c9a918cSPavel Labath // it's not possible to recover this entry list (but the other lists may still 6533c9a918cSPavel Labath // parse OK). 6543c9a918cSPavel Labath bool DWARFDebugNames::NameIndex::dumpEntry(ScopedPrinter &W, 6553c9a918cSPavel Labath uint32_t *Offset) const { 6563c9a918cSPavel Labath uint32_t EntryId = *Offset; 6573c9a918cSPavel Labath auto EntryOr = getEntry(Offset); 6583c9a918cSPavel Labath if (!EntryOr) { 6593c9a918cSPavel Labath handleAllErrors(EntryOr.takeError(), [](const SentinelError &) {}, 6603c9a918cSPavel Labath [&W](const ErrorInfoBase &EI) { EI.log(W.startLine()); }); 6613c9a918cSPavel Labath return false; 6623c9a918cSPavel Labath } 6633c9a918cSPavel Labath 6643c9a918cSPavel Labath DictScope EntryScope(W, ("Entry @ 0x" + Twine::utohexstr(EntryId)).str()); 6653c9a918cSPavel Labath EntryOr->dump(W); 6663c9a918cSPavel Labath return true; 6673c9a918cSPavel Labath } 6683c9a918cSPavel Labath 669d6ca0639SPavel Labath void DWARFDebugNames::NameIndex::dumpName(ScopedPrinter &W, 670d6ca0639SPavel Labath const NameTableEntry &NTE, 6713c9a918cSPavel Labath Optional<uint32_t> Hash) const { 672d6ca0639SPavel Labath DictScope NameScope(W, ("Name " + Twine(NTE.getIndex())).str()); 6733c9a918cSPavel Labath if (Hash) 6743c9a918cSPavel Labath W.printHex("Hash", *Hash); 6753c9a918cSPavel Labath 676d6ca0639SPavel Labath W.startLine() << format("String: 0x%08x", NTE.getStringOffset()); 677d6ca0639SPavel Labath W.getOStream() << " \"" << NTE.getString() << "\"\n"; 6783c9a918cSPavel Labath 679d6ca0639SPavel Labath uint32_t EntryOffset = NTE.getEntryOffset(); 680d6ca0639SPavel Labath while (dumpEntry(W, &EntryOffset)) 6813c9a918cSPavel Labath /*empty*/; 6823c9a918cSPavel Labath } 6833c9a918cSPavel Labath 6843c9a918cSPavel Labath void DWARFDebugNames::NameIndex::dumpCUs(ScopedPrinter &W) const { 6853c9a918cSPavel Labath ListScope CUScope(W, "Compilation Unit offsets"); 6863c9a918cSPavel Labath for (uint32_t CU = 0; CU < Hdr.CompUnitCount; ++CU) 6873c9a918cSPavel Labath W.startLine() << format("CU[%u]: 0x%08x\n", CU, getCUOffset(CU)); 6883c9a918cSPavel Labath } 6893c9a918cSPavel Labath 6903c9a918cSPavel Labath void DWARFDebugNames::NameIndex::dumpLocalTUs(ScopedPrinter &W) const { 6913c9a918cSPavel Labath if (Hdr.LocalTypeUnitCount == 0) 6923c9a918cSPavel Labath return; 6933c9a918cSPavel Labath 6943c9a918cSPavel Labath ListScope TUScope(W, "Local Type Unit offsets"); 6953c9a918cSPavel Labath for (uint32_t TU = 0; TU < Hdr.LocalTypeUnitCount; ++TU) 6963c9a918cSPavel Labath W.startLine() << format("LocalTU[%u]: 0x%08x\n", TU, getLocalTUOffset(TU)); 6973c9a918cSPavel Labath } 6983c9a918cSPavel Labath 6993c9a918cSPavel Labath void DWARFDebugNames::NameIndex::dumpForeignTUs(ScopedPrinter &W) const { 7003c9a918cSPavel Labath if (Hdr.ForeignTypeUnitCount == 0) 7013c9a918cSPavel Labath return; 7023c9a918cSPavel Labath 7033c9a918cSPavel Labath ListScope TUScope(W, "Foreign Type Unit signatures"); 7043c9a918cSPavel Labath for (uint32_t TU = 0; TU < Hdr.ForeignTypeUnitCount; ++TU) { 7053c9a918cSPavel Labath W.startLine() << format("ForeignTU[%u]: 0x%016" PRIx64 "\n", TU, 706d99072bcSPavel Labath getForeignTUSignature(TU)); 7073c9a918cSPavel Labath } 7083c9a918cSPavel Labath } 7093c9a918cSPavel Labath 7103c9a918cSPavel Labath void DWARFDebugNames::NameIndex::dumpAbbreviations(ScopedPrinter &W) const { 7113c9a918cSPavel Labath ListScope AbbrevsScope(W, "Abbreviations"); 7123c9a918cSPavel Labath for (const auto &Abbr : Abbrevs) 7133c9a918cSPavel Labath Abbr.dump(W); 7143c9a918cSPavel Labath } 7153c9a918cSPavel Labath 7163c9a918cSPavel Labath void DWARFDebugNames::NameIndex::dumpBucket(ScopedPrinter &W, 7173c9a918cSPavel Labath uint32_t Bucket) const { 7183c9a918cSPavel Labath ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str()); 7193c9a918cSPavel Labath uint32_t Index = getBucketArrayEntry(Bucket); 7203c9a918cSPavel Labath if (Index == 0) { 7213c9a918cSPavel Labath W.printString("EMPTY"); 7223c9a918cSPavel Labath return; 7233c9a918cSPavel Labath } 7243c9a918cSPavel Labath if (Index > Hdr.NameCount) { 7253c9a918cSPavel Labath W.printString("Name index is invalid"); 7263c9a918cSPavel Labath return; 7273c9a918cSPavel Labath } 7283c9a918cSPavel Labath 7293c9a918cSPavel Labath for (; Index <= Hdr.NameCount; ++Index) { 7303c9a918cSPavel Labath uint32_t Hash = getHashArrayEntry(Index); 7313c9a918cSPavel Labath if (Hash % Hdr.BucketCount != Bucket) 7323c9a918cSPavel Labath break; 7333c9a918cSPavel Labath 734d6ca0639SPavel Labath dumpName(W, getNameTableEntry(Index), Hash); 7353c9a918cSPavel Labath } 7363c9a918cSPavel Labath } 7373c9a918cSPavel Labath 7383c9a918cSPavel Labath LLVM_DUMP_METHOD void DWARFDebugNames::NameIndex::dump(ScopedPrinter &W) const { 7393c9a918cSPavel Labath DictScope UnitScope(W, ("Name Index @ 0x" + Twine::utohexstr(Base)).str()); 7403c9a918cSPavel Labath Hdr.dump(W); 7413c9a918cSPavel Labath dumpCUs(W); 7423c9a918cSPavel Labath dumpLocalTUs(W); 7433c9a918cSPavel Labath dumpForeignTUs(W); 7443c9a918cSPavel Labath dumpAbbreviations(W); 7453c9a918cSPavel Labath 7463c9a918cSPavel Labath if (Hdr.BucketCount > 0) { 7473c9a918cSPavel Labath for (uint32_t Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) 7483c9a918cSPavel Labath dumpBucket(W, Bucket); 7493c9a918cSPavel Labath return; 7503c9a918cSPavel Labath } 7513c9a918cSPavel Labath 7523c9a918cSPavel Labath W.startLine() << "Hash table not present\n"; 753d6ca0639SPavel Labath for (NameTableEntry NTE : *this) 754d6ca0639SPavel Labath dumpName(W, NTE, None); 7553c9a918cSPavel Labath } 7563c9a918cSPavel Labath 7573c9a918cSPavel Labath llvm::Error DWARFDebugNames::extract() { 7583c9a918cSPavel Labath uint32_t Offset = 0; 7593c9a918cSPavel Labath while (AccelSection.isValidOffset(Offset)) { 7603c9a918cSPavel Labath NameIndex Next(*this, Offset); 7613c9a918cSPavel Labath if (llvm::Error E = Next.extract()) 7623c9a918cSPavel Labath return E; 7633c9a918cSPavel Labath Offset = Next.getNextUnitOffset(); 7643c9a918cSPavel Labath NameIndices.push_back(std::move(Next)); 7653c9a918cSPavel Labath } 7663c9a918cSPavel Labath return Error::success(); 7673c9a918cSPavel Labath } 7683c9a918cSPavel Labath 76980827f10SPavel Labath iterator_range<DWARFDebugNames::ValueIterator> 77080827f10SPavel Labath DWARFDebugNames::NameIndex::equal_range(StringRef Key) const { 77180827f10SPavel Labath return make_range(ValueIterator(*this, Key), ValueIterator()); 77280827f10SPavel Labath } 77380827f10SPavel Labath 7743c9a918cSPavel Labath LLVM_DUMP_METHOD void DWARFDebugNames::dump(raw_ostream &OS) const { 7753c9a918cSPavel Labath ScopedPrinter W(OS); 7763c9a918cSPavel Labath for (const NameIndex &NI : NameIndices) 7773c9a918cSPavel Labath NI.dump(W); 7783c9a918cSPavel Labath } 779d99072bcSPavel Labath 780d99072bcSPavel Labath Optional<uint32_t> 781d99072bcSPavel Labath DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() { 782d99072bcSPavel Labath const Header &Hdr = CurrentIndex->Hdr; 783d99072bcSPavel Labath if (Hdr.BucketCount == 0) { 784d99072bcSPavel Labath // No Hash Table, We need to search through all names in the Name Index. 785d6ca0639SPavel Labath for (NameTableEntry NTE : *CurrentIndex) { 786d6ca0639SPavel Labath if (NTE.getString() == Key) 787d6ca0639SPavel Labath return NTE.getEntryOffset(); 788d99072bcSPavel Labath } 789d99072bcSPavel Labath return None; 790d99072bcSPavel Labath } 791d99072bcSPavel Labath 792d99072bcSPavel Labath // The Name Index has a Hash Table, so use that to speed up the search. 793d99072bcSPavel Labath // Compute the Key Hash, if it has not been done already. 794d99072bcSPavel Labath if (!Hash) 795d99072bcSPavel Labath Hash = caseFoldingDjbHash(Key); 796d99072bcSPavel Labath uint32_t Bucket = *Hash % Hdr.BucketCount; 797d99072bcSPavel Labath uint32_t Index = CurrentIndex->getBucketArrayEntry(Bucket); 798d99072bcSPavel Labath if (Index == 0) 799d99072bcSPavel Labath return None; // Empty bucket 800d99072bcSPavel Labath 801d99072bcSPavel Labath for (; Index <= Hdr.NameCount; ++Index) { 802d99072bcSPavel Labath uint32_t Hash = CurrentIndex->getHashArrayEntry(Index); 803d99072bcSPavel Labath if (Hash % Hdr.BucketCount != Bucket) 804d99072bcSPavel Labath return None; // End of bucket 805d99072bcSPavel Labath 806d99072bcSPavel Labath NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index); 807d6ca0639SPavel Labath if (NTE.getString() == Key) 808d6ca0639SPavel Labath return NTE.getEntryOffset(); 809d99072bcSPavel Labath } 810d99072bcSPavel Labath return None; 811d99072bcSPavel Labath } 812d99072bcSPavel Labath 813d99072bcSPavel Labath bool DWARFDebugNames::ValueIterator::getEntryAtCurrentOffset() { 814d99072bcSPavel Labath auto EntryOr = CurrentIndex->getEntry(&DataOffset); 815d99072bcSPavel Labath if (!EntryOr) { 816d99072bcSPavel Labath consumeError(EntryOr.takeError()); 817d99072bcSPavel Labath return false; 818d99072bcSPavel Labath } 819d99072bcSPavel Labath CurrentEntry = std::move(*EntryOr); 820d99072bcSPavel Labath return true; 821d99072bcSPavel Labath } 822d99072bcSPavel Labath 823d99072bcSPavel Labath bool DWARFDebugNames::ValueIterator::findInCurrentIndex() { 824d99072bcSPavel Labath Optional<uint32_t> Offset = findEntryOffsetInCurrentIndex(); 825d99072bcSPavel Labath if (!Offset) 826d99072bcSPavel Labath return false; 827d99072bcSPavel Labath DataOffset = *Offset; 828d99072bcSPavel Labath return getEntryAtCurrentOffset(); 829d99072bcSPavel Labath } 830d99072bcSPavel Labath 831d99072bcSPavel Labath void DWARFDebugNames::ValueIterator::searchFromStartOfCurrentIndex() { 832d99072bcSPavel Labath for (const NameIndex *End = CurrentIndex->Section.NameIndices.end(); 833d99072bcSPavel Labath CurrentIndex != End; ++CurrentIndex) { 834d99072bcSPavel Labath if (findInCurrentIndex()) 835d99072bcSPavel Labath return; 836d99072bcSPavel Labath } 837d99072bcSPavel Labath setEnd(); 838d99072bcSPavel Labath } 839d99072bcSPavel Labath 840d99072bcSPavel Labath void DWARFDebugNames::ValueIterator::next() { 841d99072bcSPavel Labath assert(CurrentIndex && "Incrementing an end() iterator?"); 842d99072bcSPavel Labath 843d99072bcSPavel Labath // First try the next entry in the current Index. 844d99072bcSPavel Labath if (getEntryAtCurrentOffset()) 845d99072bcSPavel Labath return; 846d99072bcSPavel Labath 84759870af6SPavel Labath // If we're a local iterator or we have reached the last Index, we're done. 84859870af6SPavel Labath if (IsLocal || CurrentIndex == &CurrentIndex->Section.NameIndices.back()) { 84980827f10SPavel Labath setEnd(); 85080827f10SPavel Labath return; 85180827f10SPavel Labath } 85280827f10SPavel Labath 85380827f10SPavel Labath // Otherwise, try the next index. 854d99072bcSPavel Labath ++CurrentIndex; 855d99072bcSPavel Labath searchFromStartOfCurrentIndex(); 856d99072bcSPavel Labath } 857d99072bcSPavel Labath 858d99072bcSPavel Labath DWARFDebugNames::ValueIterator::ValueIterator(const DWARFDebugNames &AccelTable, 859d99072bcSPavel Labath StringRef Key) 86080827f10SPavel Labath : CurrentIndex(AccelTable.NameIndices.begin()), IsLocal(false), Key(Key) { 861d99072bcSPavel Labath searchFromStartOfCurrentIndex(); 862d99072bcSPavel Labath } 863d99072bcSPavel Labath 86480827f10SPavel Labath DWARFDebugNames::ValueIterator::ValueIterator( 86580827f10SPavel Labath const DWARFDebugNames::NameIndex &NI, StringRef Key) 86680827f10SPavel Labath : CurrentIndex(&NI), IsLocal(true), Key(Key) { 86780827f10SPavel Labath if (!findInCurrentIndex()) 86880827f10SPavel Labath setEnd(); 86980827f10SPavel Labath } 87080827f10SPavel Labath 871d99072bcSPavel Labath iterator_range<DWARFDebugNames::ValueIterator> 872d99072bcSPavel Labath DWARFDebugNames::equal_range(StringRef Key) const { 873d99072bcSPavel Labath if (NameIndices.empty()) 874d99072bcSPavel Labath return make_range(ValueIterator(), ValueIterator()); 875d99072bcSPavel Labath return make_range(ValueIterator(*this, Key), ValueIterator()); 876d99072bcSPavel Labath } 87780827f10SPavel Labath 87880827f10SPavel Labath const DWARFDebugNames::NameIndex * 87980827f10SPavel Labath DWARFDebugNames::getCUNameIndex(uint32_t CUOffset) { 88080827f10SPavel Labath if (CUToNameIndex.size() == 0 && NameIndices.size() > 0) { 88180827f10SPavel Labath for (const auto &NI : *this) { 88280827f10SPavel Labath for (uint32_t CU = 0; CU < NI.getCUCount(); ++CU) 88380827f10SPavel Labath CUToNameIndex.try_emplace(NI.getCUOffset(CU), &NI); 88480827f10SPavel Labath } 88580827f10SPavel Labath } 88680827f10SPavel Labath return CUToNameIndex.lookup(CUOffset); 88780827f10SPavel Labath } 888