Index: include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h +++ include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h @@ -10,6 +10,8 @@ #ifndef LLVM_DEBUGINFO_DWARFACCELERATORTABLE_H #define LLVM_DEBUGINFO_DWARFACCELERATORTABLE_H +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/BinaryFormat/Dwarf.h" #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" @@ -20,11 +22,30 @@ namespace llvm { class raw_ostream; +class ScopedPrinter; + +class DWARFAcceleratorTable { +protected: + DWARFDataExtractor AccelSection; + DataExtractor StringSection; + +public: + DWARFAcceleratorTable(const DWARFDataExtractor &AccelSection, + DataExtractor StringSection) + : AccelSection(AccelSection), StringSection(StringSection) {} + virtual ~DWARFAcceleratorTable(); + + virtual llvm::Error extract() = 0; + virtual void dump(raw_ostream &OS) const = 0; + + DWARFAcceleratorTable(const DWARFAcceleratorTable &) = delete; + void operator=(const DWARFAcceleratorTable &) = delete; +}; /// This implements the Apple accelerator table format, a precursor of the /// DWARF 5 accelerator table format. /// TODO: Factor out a common base class for both formats. -class AppleAcceleratorTable { +class AppleAcceleratorTable : public DWARFAcceleratorTable { struct Header { uint32_t Magic; uint16_t Version; @@ -44,8 +65,6 @@ struct Header Hdr; struct HeaderData HdrData; - DWARFDataExtractor AccelSection; - DataExtractor StringSection; bool IsValid = false; public: @@ -85,12 +104,11 @@ } }; - AppleAcceleratorTable(const DWARFDataExtractor &AccelSection, DataExtractor StringSection) - : AccelSection(AccelSection), StringSection(StringSection) {} + : DWARFAcceleratorTable(AccelSection, StringSection) {} - llvm::Error extract(); + llvm::Error extract() override; uint32_t getNumBuckets(); uint32_t getNumHashes(); uint32_t getSizeHdr(); @@ -107,12 +125,155 @@ /// related to the input hash data offset. /// DieTag is the tag of the DIE std::pair readAtoms(uint32_t &HashDataOffset); - void dump(raw_ostream &OS) const; + void dump(raw_ostream &OS) const override; /// Look up all entries in the accelerator table matching \c Key. iterator_range equal_range(StringRef Key) const; }; +/// .debug_names section consists of one or more units. Each unit starts with a +/// header, which is followed by a list of compilation units, local and foreign +/// type units. +/// +/// These may be followed by an (optional) hash lookup table, which consists of +/// an array of buckets and hashes similar to the apple tables above. The only +/// difference is that the hashes array is 1-based, and consequently an empty +/// bucket is denoted by 0 and not UINT32_MAX. +/// +/// Next is the name table, which consists of an array of names and array of +/// entry offsets. This is different from the apple tables, which store names +/// next to the actual entries. +/// +/// The structure of the entries is described by an abbreviations table, which +/// comes after the name table. Unlike the apple tables, which have a uniform +/// entry structure described in the header, each .debug_names entry may have +/// different index attributes (DW_IDX_???) attached to it. +/// +/// The last segment consists of a list of entries, which is a 0-terminated list +/// referenced by the name table and interpreted with the help of the +/// abbreviation table. +class DWARFDebugNames : public DWARFAcceleratorTable { +public: + struct Header { + uint32_t UnitLength; + uint16_t Version; + uint16_t Padding; + uint32_t CompUnitCount; + uint32_t LocalTypeUnitCount; + uint32_t ForeignTypeUnitCount; + uint32_t BucketCount; + uint32_t NameCount; + uint32_t AbbrevTableSize; + uint32_t AugmentationStringSize; + SmallString<8> AugmentationString; + + Error extract(const DWARFDataExtractor &AS, uint32_t *Offset); + void dump(ScopedPrinter &W) const; + }; + struct Abbrev { + uint32_t Code; + dwarf::Tag Tag; + std::vector> Indices; + + explicit Abbrev(uint32_t Code) : Code(Code) {} + + void dump(ScopedPrinter &W) const; + }; + + struct Entry { + const Abbrev &Abbr; + std::vector Indices; + + Entry(const Abbrev &Abbr); + + void dump(ScopedPrinter &W) const; + }; + + class SentinelError: public ErrorInfo { + public: + static char ID; + + void log(raw_ostream &OS) const override { OS << "Sentinel"; } + std::error_code convertToErrorCode() const override; + }; + +private: + struct AbbrevMapInfo { + static Abbrev getEmptyKey() { return Abbrev(0); } + static Abbrev getTombstoneKey() { return Abbrev(~0); } + static unsigned getHashValue(uint32_t Code) { + return DenseMapInfo::getHashValue(Code); + } + static unsigned getHashValue(const Abbrev &Abbr) { + return getHashValue(Abbr.Code); + } + static bool isEqual(uint32_t LHS, const Abbrev &RHS) { + return LHS == RHS.Code; + } + static bool isEqual(const Abbrev &LHS, const Abbrev &RHS) { + return LHS.Code == RHS.Code; + } + }; + +public: + class NameIndex { + DenseSet Abbrevs; + struct Header Hdr; + const DWARFDebugNames &Section; + + // Base of the whole unit and of various important tables, as offsets from + // the start of the section. + uint32_t Base; + uint32_t CUsBase; + uint32_t BucketsBase; + uint32_t HashesBase; + uint32_t StringOffsetsBase; + uint32_t EntryOffsetsBase; + uint32_t EntriesBase; + + /// Reads Bucket Table entry at Bucket. The Bucket is 0-based. + uint32_t getBucket(uint32_t Bucket) const; + + /// Reads Hash Table entry at Index. The Index is 1-based. + uint32_t getHash(uint32_t Index) const; + + /// Reads Name Table entry at Index. Returns { String Offset, Entry Offset } + /// pair. The Entry Offset is relative to the start of the .debug_names + /// section. Index is 1-based. + std::pair getName(uint32_t Index) const; + + Expected getEntry(uint32_t *Offset) const; + + void dumpCUs(ScopedPrinter &W) const; + void dumpLocalTUs(ScopedPrinter &W) const; + void dumpForeignTUs(ScopedPrinter &W) const; + void dumpAbbreviations(ScopedPrinter &W) const; + bool dumpEntry(ScopedPrinter &W, uint32_t *Offset) const; + void dumpName(ScopedPrinter &W, uint32_t Index, + Optional Hash) const; + void dumpBucket(ScopedPrinter &W, uint32_t Bucket) const; + + public: + NameIndex(const DWARFDebugNames &Section, uint32_t Base) + : Section(Section), Base(Base) {} + + llvm::Error extract(); + uint32_t getNextUnitOffset() const { return Base + 4 + Hdr.UnitLength; } + void dump(ScopedPrinter &W) const; + }; + +private: + std::vector Units; + +public: + DWARFDebugNames(const DWARFDataExtractor &AccelSection, + DataExtractor StringSection) + : DWARFAcceleratorTable(AccelSection, StringSection) {} + + llvm::Error extract() override; + void dump(raw_ostream &OS) const override; +}; + } // end namespace llvm #endif // LLVM_DEBUGINFO_DWARFACCELERATORTABLE_H Index: include/llvm/DebugInfo/DWARF/DWARFContext.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFContext.h +++ include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -69,6 +69,7 @@ std::unique_ptr DebugFrame; std::unique_ptr EHFrame; std::unique_ptr Macro; + std::unique_ptr Names; std::unique_ptr AppleNames; std::unique_ptr AppleTypes; std::unique_ptr AppleNamespaces; @@ -242,6 +243,9 @@ /// Get a pointer to the parsed DebugMacro object. const DWARFDebugMacro *getDebugMacro(); + /// Get a reference to the parsed accelerator table object. + const DWARFDebugNames &getDebugNames(); + /// Get a reference to the parsed accelerator table object. const AppleAcceleratorTable &getAppleNames(); Index: include/llvm/DebugInfo/DWARF/DWARFObject.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFObject.h +++ include/llvm/DebugInfo/DWARF/DWARFObject.h @@ -67,6 +67,7 @@ virtual const DWARFSection &getAppleNamespacesSection() const { return Dummy; } + virtual const DWARFSection &getDebugNamesSection() const { return Dummy; } virtual const DWARFSection &getAppleObjCSection() const { return Dummy; } virtual StringRef getCUIndexSection() const { return ""; } virtual StringRef getGdbIndexSection() const { return ""; } Index: lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp +++ lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp @@ -14,6 +14,7 @@ #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/Format.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" #include #include @@ -21,6 +22,33 @@ using namespace llvm; +static std::string formatEnum(StringRef (*StringFn)(unsigned), StringRef Type, unsigned Value) { + std::string Str = StringFn(Value); + if (!Str.empty()) + return Str; + raw_string_ostream SS(Str); + SS << "DW_" << Type << "_Unknown_0x" << format("%x", Value); + return SS.str(); +} + +static std::string formatTag(unsigned Tag) { + return formatEnum(dwarf::TagString, "TAG", Tag); +} + +static std::string formatForm(unsigned Form) { + return formatEnum(dwarf::FormEncodingString, "FORM", Form); +} + +static std::string formatIndex(unsigned Idx) { + return formatEnum(dwarf::IndexString, "IDX", Idx); +} + +static std::string formatAtom(unsigned Atom) { + return formatEnum(dwarf::AtomTypeString, "IDX", Atom); +} + +DWARFAcceleratorTable::~DWARFAcceleratorTable() = default; + llvm::Error AppleAcceleratorTable::extract() { uint32_t Offset = 0; @@ -131,19 +159,8 @@ unsigned i = 0; SmallVector AtomForms; for (const auto &Atom: HdrData.Atoms) { - OS << format("Atom[%d] Type: ", i++); - auto TypeString = dwarf::AtomTypeString(Atom.first); - if (!TypeString.empty()) - OS << TypeString; - else - OS << format("DW_ATOM_Unknown_0x%x", Atom.first); - OS << " Form: "; - auto FormString = dwarf::FormEncodingString(Atom.second); - if (!FormString.empty()) - OS << FormString; - else - OS << format("DW_FORM_Unknown_0x%x", Atom.second); - OS << '\n'; + OS << format("Atom[%d] Type: ", i++) << formatAtom(Atom.first) + << " Form: " << formatForm(Atom.second) << '\n'; AtomForms.push_back(DWARFFormValue(Atom.second)); } @@ -266,3 +283,313 @@ } return make_range(ValueIterator(), ValueIterator()); } + +void DWARFDebugNames::Header::dump(ScopedPrinter &W) const { + DictScope HeaderScope(W, "Header"); + W.printHex("Length", UnitLength); + W.printNumber("Version", Version); + W.printHex("Padding", Padding); + W.printNumber("CU count", CompUnitCount); + W.printNumber("Local TU count", LocalTypeUnitCount); + W.printNumber("Foreign TU count", ForeignTypeUnitCount); + W.printNumber("Bucket count", BucketCount); + W.printNumber("Name count", NameCount); + W.printHex("Abbreviations table size", AbbrevTableSize); + W.startLine() << "Augmentation: '" << AugmentationString << "'\n"; +} + +llvm::Error DWARFDebugNames::Header::extract(const DWARFDataExtractor &AS, + uint32_t *Offset) { + // Check that we can read the fixed-size part. + if (!AS.isValidOffset(*Offset + sizeof(Header) - 1)) + return make_error("Section too small: cannot read header.", + inconvertibleErrorCode()); + + UnitLength = AS.getU32(Offset); + Version = AS.getU16(Offset); + Padding = AS.getU16(Offset); + CompUnitCount = AS.getU32(Offset); + LocalTypeUnitCount = AS.getU32(Offset); + ForeignTypeUnitCount = AS.getU32(Offset); + BucketCount = AS.getU32(Offset); + NameCount = AS.getU32(Offset); + AbbrevTableSize = AS.getU32(Offset); + AugmentationStringSize = AS.getU32(Offset); + + if (!AS.isValidOffsetForDataOfSize(*Offset, AugmentationStringSize)) + return make_error( + "Section too small: cannot read header augmentation.", + inconvertibleErrorCode()); + AugmentationString.resize(AugmentationStringSize); + AS.getU8(Offset, reinterpret_cast(AugmentationString.data()), + AugmentationStringSize); + *Offset = alignTo(*Offset, 4); + return Error::success(); +} + +void DWARFDebugNames::Abbrev::dump(ScopedPrinter &W) const { + DictScope AbbrevScope(W, ("Abbreviation 0x" + Twine::utohexstr(Code)).str()); + W.startLine() << "Tag: " << formatTag(Tag) << '\n'; + + for (const auto &IndexForm : Indices) { + W.startLine() << formatIndex(IndexForm.first) << ": " + << formatForm(IndexForm.second) << '\n'; + } +} + +static std::pair getULEB128Pair(const DWARFDataExtractor &E, + uint32_t *Offset) { + uint32_t One = E.getULEB128(Offset); + uint32_t Two = E.getULEB128(Offset); + return {One, Two}; +} + +llvm::Error DWARFDebugNames::NameIndex::extract() { + const DWARFDataExtractor &AS = Section.AccelSection; + uint32_t Offset = Base; + if (Error E = Hdr.extract(AS, &Offset)) + return E; + + CUsBase = Offset; + Offset += Hdr.CompUnitCount * 4; + Offset += Hdr.LocalTypeUnitCount * 4; + Offset += Hdr.ForeignTypeUnitCount * 8; + BucketsBase = Offset; + Offset += Hdr.BucketCount * 4; + HashesBase = Offset; + if (Hdr.BucketCount > 0) + Offset += Hdr.NameCount * 4; + StringOffsetsBase = Offset; + Offset += Hdr.NameCount * 4; + EntryOffsetsBase = Offset; + Offset += Hdr.NameCount * 4; + + if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize)) + return make_error( + "Section too small: cannot read abbreviations.", + inconvertibleErrorCode()); + + EntriesBase = Offset + Hdr.AbbrevTableSize; + + uint32_t Code; + while ((Code = AS.getULEB128(&Offset)) != 0) { + if (Abbrevs.find_as(Code) != Abbrevs.end()) + return make_error("Duplicate abbreviation code.", + inconvertibleErrorCode()); + Abbrev A(Code); + A.Tag = dwarf::Tag(AS.getULEB128(&Offset)); + std::pair IndexForm; + while (IndexForm = getULEB128Pair(AS, &Offset), + IndexForm.first || IndexForm.second) { + A.Indices.emplace_back(dwarf::Index(IndexForm.first), + dwarf::Form(IndexForm.second)); + } + Abbrevs.insert(std::move(A)); + } + + return Error::success(); +} + +DWARFDebugNames::Entry::Entry(const Abbrev &Abbr) : Abbr(Abbr) { + Indices.reserve(Abbr.Indices.size()); + for (const auto &IndexForm: Abbr.Indices) + Indices.emplace_back(IndexForm.second); +} + +void DWARFDebugNames::Entry::dump(ScopedPrinter &W) const { + W.printHex("Abbrev", Abbr.Code); + W.startLine() << "Tag: " << formatTag(Abbr.Tag) << "\n"; + + for (uint32_t I = 0, E = Indices.size(); I < E; ++I) { + W.startLine() << formatIndex(Abbr.Indices[I].first) << ": "; + Indices[I].dump(W.getOStream()); + W.getOStream() << '\n'; + } +} + +char DWARFDebugNames::SentinelError::ID; +std::error_code DWARFDebugNames::SentinelError::convertToErrorCode() const { + return inconvertibleErrorCode(); +} + +Expected +DWARFDebugNames::NameIndex::getEntry(uint32_t *Offset) const { + const DWARFDataExtractor &AS = Section.AccelSection; + if (!AS.isValidOffset(*Offset)) + return make_error("Incorrectly terminated entry list", + inconvertibleErrorCode()); + + uint32_t AbbrevCode = AS.getULEB128(Offset); + if (AbbrevCode == 0) + return make_error(); + + const auto AbbrevIt = Abbrevs.find_as(AbbrevCode); + if (AbbrevIt == Abbrevs.end()) + return make_error("Invalid abbreviation", inconvertibleErrorCode()); + + Entry E(*AbbrevIt); + + DWARFFormParams FormParams = {Hdr.Version, 0, dwarf::DwarfFormat::DWARF32}; + for (auto &IndexForm : E.Indices) { + if (!IndexForm.extractValue(AS, Offset, FormParams)) + return make_error("Error extracting index values", inconvertibleErrorCode()); + } + return std::move(E); +} + +std::pair DWARFDebugNames::NameIndex::getName(uint32_t Index) const { + uint32_t StringOffsetOffset = StringOffsetsBase + 4 * (Index - 1); + uint32_t EntryOffsetOffset = EntryOffsetsBase + 4 * (Index - 1); + const DWARFDataExtractor &AS = Section.AccelSection; + + uint32_t StringOffset = AS.getRelocatedValue(4, &StringOffsetOffset); + uint32_t EntryOffset = AS.getU32(&EntryOffsetOffset); + EntryOffset += EntriesBase; + return {StringOffset, EntryOffset}; +} + +uint32_t DWARFDebugNames::NameIndex::getBucket(uint32_t Bucket) const { + uint32_t BucketOffset = BucketsBase + 4 * Bucket; + return Section.AccelSection.getU32(&BucketOffset); +} + +uint32_t DWARFDebugNames::NameIndex::getHash(uint32_t Index) const { + uint32_t HashOffset = HashesBase + 4 * (Index - 1); + return Section.AccelSection.getU32(&HashOffset); +} + +// Returns true if we should continue scanning for entries, false if this is the +// last (sentinel) entry). In case of a parsing error we also return false, as +// it's not possible to recover this entry list (but the other lists may still +// parse OK). +bool DWARFDebugNames::NameIndex::dumpEntry(ScopedPrinter &W, uint32_t *Offset) const { + uint32_t EntryId = *Offset; + auto EntryOr = getEntry(Offset); + if (!EntryOr) { + handleAllErrors(EntryOr.takeError(), [](const SentinelError &) {}, + [&W](const ErrorInfoBase &EI) { EI.log(W.startLine()); }); + return false; + } + + DictScope EntryScope(W, ("Entry @ 0x" + Twine::utohexstr(EntryId)).str()); + EntryOr->dump(W); + return true; +} + +void DWARFDebugNames::NameIndex::dumpName(ScopedPrinter &W, uint32_t Index, + Optional Hash) const { + const DataExtractor &SS = Section.StringSection; + uint32_t StringOffset, EntryOffset; + std::tie(StringOffset, EntryOffset) = getName(Index); + + DictScope NameScope(W, ("Name " + Twine(Index)).str()); + if (Hash) + W.printHex("Hash", *Hash); + + W.startLine() << format("String: 0x%08x", StringOffset); + W.getOStream() << " \"" << SS.getCStr(&StringOffset) << "\"\n"; + + while (dumpEntry(W, &EntryOffset)) + /*empty*/; +} + +void DWARFDebugNames::NameIndex::dumpCUs(ScopedPrinter &W) const { + uint32_t Offset = CUsBase; + const DWARFDataExtractor &AS = Section.AccelSection; + ListScope CUScope(W, "Compilation Unit offsets"); + for (uint32_t CU = 0; CU < Hdr.CompUnitCount; ++CU) { + uint32_t CUOffset = AS.getRelocatedValue(4, &Offset); + W.startLine() << format("CU[%u]: 0x%08X\n", CU, CUOffset); + } +} + +void DWARFDebugNames::NameIndex::dumpLocalTUs(ScopedPrinter &W) const { + if (Hdr.LocalTypeUnitCount == 0) + return; + + uint32_t Offset = CUsBase + Hdr.CompUnitCount * 4; + const DWARFDataExtractor &AS = Section.AccelSection; + ListScope TUScope(W, "Local Type Unit offsets"); + for (uint32_t TU = 0; TU < Hdr.LocalTypeUnitCount; ++TU) { + uint32_t TUOffset = AS.getRelocatedValue(4, &Offset); + W.startLine() << format("LocalTU[%u]: 0x%08X\n", TU, TUOffset); + } +} + +void DWARFDebugNames::NameIndex::dumpForeignTUs(ScopedPrinter &W) const { + if (Hdr.ForeignTypeUnitCount == 0) + return; + + uint32_t Offset = CUsBase + (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) * 4; + const DWARFDataExtractor &AS = Section.AccelSection; + ListScope TUScope(W, "Foreign Type Unit signatures"); + for (uint32_t TU = 0; TU < Hdr.ForeignTypeUnitCount; ++TU) { + uint64_t TUOffset = AS.getU64(&Offset); + W.startLine() << format("ForeignTU[%u]: 0x%016" PRIx64 "\n", TU, TUOffset); + } +} + +void DWARFDebugNames::NameIndex::dumpAbbreviations(ScopedPrinter &W) const { + ListScope AbbrevsScope(W, "Abbreviations"); + for (const auto &Abbr : Abbrevs) + Abbr.dump(W); +} + +void DWARFDebugNames::NameIndex::dumpBucket(ScopedPrinter &W, + uint32_t Bucket) const { + ListScope BucketScope(W, ("Bucket " + Twine(Bucket)).str()); + uint32_t Index = getBucket(Bucket); + if (Index == 0) { + W.printString("EMPTY"); + return; + } + if (Index > Hdr.NameCount) { + W.printString("Name index is invalid"); + return; + } + + for (; Index <= Hdr.NameCount; ++Index) { + uint32_t Hash = getHash(Index); + if (Hash % Hdr.BucketCount != Bucket) + break; + + dumpName(W, Index, Hash); + } +} + +LLVM_DUMP_METHOD void DWARFDebugNames::NameIndex::dump(ScopedPrinter &W) const { + DictScope UnitScope(W, ("Name Index @ 0x" + Twine::utohexstr(Base)).str()); + Hdr.dump(W); + dumpCUs(W); + dumpLocalTUs(W); + dumpForeignTUs(W); + dumpAbbreviations(W); + + if (Hdr.BucketCount > 0) { + for (uint32_t Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) + dumpBucket(W, Bucket); + return; + } + + W.startLine() << "Hash table not present\n"; + for (uint32_t Index = 1; Index <= Hdr.NameCount; ++Index) + dumpName(W, Index, None); +} + +llvm::Error DWARFDebugNames::extract() { + uint32_t Offset = 0; + while (AccelSection.isValidOffset(Offset)) { + NameIndex Next(*this, Offset); + if (llvm::Error E = Next.extract()) + return E; + Offset = Next.getNextUnitOffset(); + Units.push_back(std::move(Next)); + } + return Error::success(); +} + +LLVM_DUMP_METHOD void DWARFDebugNames::dump(raw_ostream &OS) const { + ScopedPrinter W(OS); + for (const NameIndex &U : Units) + U.dump(W); +} Index: lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFContext.cpp +++ lib/DebugInfo/DWARF/DWARFContext.cpp @@ -535,6 +535,9 @@ if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC, DObj->getAppleObjCSection().Data)) getAppleObjC().dump(OS); + if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames, + DObj->getDebugNamesSection().Data)) + getDebugNames().dump(OS); } DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) { @@ -703,20 +706,25 @@ return Macro.get(); } -static AppleAcceleratorTable & -getAccelTable(std::unique_ptr &Cache, - const DWARFObject &Obj, const DWARFSection &Section, - StringRef StringSection, bool IsLittleEndian) { +template +static T &getAccelTable(std::unique_ptr &Cache, const DWARFObject &Obj, + const DWARFSection &Section, StringRef StringSection, + bool IsLittleEndian) { if (Cache) return *Cache; DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0); DataExtractor StrData(StringSection, IsLittleEndian, 0); - Cache.reset(new AppleAcceleratorTable(AccelSection, StrData)); + Cache.reset(new T(AccelSection, StrData)); if (Error E = Cache->extract()) llvm::consumeError(std::move(E)); return *Cache; } +const DWARFDebugNames &DWARFContext::getDebugNames() { + return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(), + DObj->getStringSection(), isLittleEndian()); +} + const AppleAcceleratorTable &DWARFContext::getAppleNames() { return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(), DObj->getStringSection(), isLittleEndian()); @@ -1157,6 +1165,7 @@ DWARFSectionMap AppleTypesSection; DWARFSectionMap AppleNamespacesSection; DWARFSectionMap AppleObjCSection; + DWARFSectionMap DebugNamesSection; DWARFSectionMap *mapNameToDWARFSection(StringRef Name) { return StringSwitch(Name) @@ -1168,6 +1177,7 @@ .Case("debug_info.dwo", &InfoDWOSection) .Case("debug_loc.dwo", &LocDWOSection) .Case("debug_line.dwo", &LineDWOSection) + .Case("debug_names", &DebugNamesSection) .Case("debug_str_offsets.dwo", &StringOffsetDWOSection) .Case("debug_addr", &AddrSection) .Case("apple_names", &AppleNamesSection) @@ -1473,6 +1483,9 @@ const DWARFSection &getAppleObjCSection() const override { return AppleObjCSection; } + const DWARFSection &getDebugNamesSection() const override { + return DebugNamesSection; + } StringRef getFileName() const override { return FileName; } uint8_t getAddressSize() const override { return AddressSize; } Index: test/DebugInfo/X86/dwarfdump-debug-names.s =================================================================== --- /dev/null +++ test/DebugInfo/X86/dwarfdump-debug-names.s @@ -0,0 +1,176 @@ +# RUN: llvm-mc -triple x86_64-pc-linux %s -filetype=obj -o - | llvm-dwarfdump -debug-names - | FileCheck %s + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "foo" +.Linfo_string1: + .asciz "_Z3foov" +.Linfo_string2: + .asciz "bar" + +# Fake .debug_info. We just need it for the offsets to two "compile units" and +# two "DIEs" + .section .debug_info,"",@progbits +.Lcu_begin0: + .byte 0 +.Ldie0: + .byte 0 +.Lcu_begin1: + .byte 0 +.Ldie1: + .byte 0 + + .section .debug_names,"",@progbits + .long .Lnames_end0-.Lnames_start0 # Header: contribution length +.Lnames_start0: + .short 5 # Header: version + .short 0 # Header: padding + .long 1 # Header: compilation unit count + .long 0 # Header: local type unit count + .long 0 # Header: foreign type unit count + .long 2 # Header: bucket count + .long 2 # Header: name count + .long .Lnames_abbrev_end0-.Lnames_abbrev_start0 # Header: abbreviation table size + .long 0 # Header: augmentation length + .long .Lcu_begin0 # Compilation unit 0 + .long 0 # Bucket 0 + .long 1 # Bucket 1 + .long 193491849 # Hash in Bucket 1 + .long -1257882357 # Hash in Bucket 1 + .long .Linfo_string0 # String in Bucket 1: foo + .long .Linfo_string1 # String in Bucket 1: _Z3foov + .long .Lnames0-.Lnames_entries0 # Offset in Bucket 1 + .long .Lnames1-.Lnames_entries0 # Offset in Bucket 1 +.Lnames_abbrev_start0: + .byte 46 # Abbrev code + .byte 46 # DW_TAG_subprogram + .byte 3 # DW_IDX_die_offset + .byte 6 # DW_FORM_data4 + .byte 0 # End of abbrev + .byte 0 # End of abbrev + .byte 0 # End of abbrev list +.Lnames_abbrev_end0: +.Lnames_entries0: +.Lnames0: + .byte 46 # Abbrev code + .long .Ldie0 # DW_IDX_die_offset + .long 0 # End of list: foo +.Lnames1: + .byte 46 # Abbrev code + .long .Ldie0 # DW_IDX_die_offset + .long 0 # End of list: _Z3foov + .p2align 2 +.Lnames_end0: + + .long .Lnames_end1-.Lnames_start1 # Header: contribution length +.Lnames_start1: + .short 5 # Header: version + .short 0 # Header: padding + .long 1 # Header: compilation unit count + .long 0 # Header: local type unit count + .long 0 # Header: foreign type unit count + .long 1 # Header: bucket count + .long 1 # Header: name count + .long .Lnames_abbrev_end1-.Lnames_abbrev_start1 # Header: abbreviation table size + .long 0 # Header: augmentation length + .long .Lcu_begin1 # Compilation unit 0 + .long 1 # Bucket 0 + .long 193487034 # Hash in Bucket 0 + .long .Linfo_string2 # String in Bucket 0: bar + .long .Lnames2-.Lnames_entries1 # Offset in Bucket 0 +.Lnames_abbrev_start1: + .byte 52 # Abbrev code + .byte 52 # DW_TAG_variable + .byte 3 # DW_IDX_die_offset + .byte 6 # DW_FORM_data4 + .byte 0 # End of abbrev + .byte 0 # End of abbrev + .byte 0 # End of abbrev list +.Lnames_abbrev_end1: +.Lnames_entries1: +.Lnames2: + .byte 52 # Abbrev code + .long .Ldie1 # DW_IDX_die_offset + .long 0 # End of list: bar + .p2align 2 +.Lnames_end1: +# CHECK: .debug_names contents: +# CHECK-NEXT: Name Index @ 0x0 { +# CHECK-NEXT: Header { +# CHECK-NEXT: Length: 0x60 +# CHECK-NEXT: Version: 5 +# CHECK-NEXT: Padding: 0x0 +# CHECK-NEXT: CU count: 1 +# CHECK-NEXT: Local TU count: 0 +# CHECK-NEXT: Foreign TU count: 0 +# CHECK-NEXT: Bucket count: 2 +# CHECK-NEXT: Name count: 2 +# CHECK-NEXT: Abbreviations table size: 0x7 +# CHECK-NEXT: Augmentation: '' +# CHECK-NEXT: } +# CHECK-NEXT: Compilation Unit offsets [ +# CHECK-NEXT: CU[0]: 0x00000000 +# CHECK-NEXT: ] +# CHECK-NEXT: Abbreviations [ +# CHECK-NEXT: Abbreviation 0x2e { +# CHECK-NEXT: Tag: DW_TAG_subprogram +# CHECK-NEXT: DW_IDX_die_offset: DW_FORM_data4 +# CHECK-NEXT: } +# CHECK-NEXT: ] +# CHECK-NEXT: Bucket 0 [ +# CHECK-NEXT: EMPTY +# CHECK-NEXT: ] +# CHECK-NEXT: Bucket 1 [ +# CHECK-NEXT: Name 1 { +# CHECK-NEXT: Hash: 0xB887389 +# CHECK-NEXT: String: 0x00000000 "foo" +# CHECK-NEXT: Entry @ 0x4f { +# CHECK-NEXT: Abbrev: 0x2E +# CHECK-NEXT: Tag: DW_TAG_subprogram +# CHECK-NEXT: DW_IDX_die_offset: 0x00000001 +# CHECK-NEXT: } +# CHECK-NEXT: } +# CHECK-NEXT: Name 2 { +# CHECK-NEXT: Hash: 0xB5063D0B +# CHECK-NEXT: String: 0x00000004 "_Z3foov" +# CHECK-NEXT: Entry @ 0x58 { +# CHECK-NEXT: Abbrev: 0x2E +# CHECK-NEXT: Tag: DW_TAG_subprogram +# CHECK-NEXT: DW_IDX_die_offset: 0x00000001 +# CHECK-NEXT: } +# CHECK-NEXT: } +# CHECK-NEXT: ] +# CHECK-NEXT: } +# CHECK-NEXT: Name Index @ 0x64 { +# CHECK-NEXT: Header { +# CHECK-NEXT: Length: 0x44 +# CHECK-NEXT: Version: 5 +# CHECK-NEXT: Padding: 0x0 +# CHECK-NEXT: CU count: 1 +# CHECK-NEXT: Local TU count: 0 +# CHECK-NEXT: Foreign TU count: 0 +# CHECK-NEXT: Bucket count: 1 +# CHECK-NEXT: Name count: 1 +# CHECK-NEXT: Abbreviations table size: 0x7 +# CHECK-NEXT: Augmentation: '' +# CHECK-NEXT: } +# CHECK-NEXT: Compilation Unit offsets [ +# CHECK-NEXT: CU[0]: 0x00000002 +# CHECK-NEXT: ] +# CHECK-NEXT: Abbreviations [ +# CHECK-NEXT: Abbreviation 0x34 { +# CHECK-NEXT: Tag: DW_TAG_variable +# CHECK-NEXT: DW_IDX_die_offset: DW_FORM_data4 +# CHECK-NEXT: } +# CHECK-NEXT: ] +# CHECK-NEXT: Bucket 0 [ +# CHECK-NEXT: Name 1 { +# CHECK-NEXT: Hash: 0xB8860BA +# CHECK-NEXT: String: 0x0000000c "bar" +# CHECK-NEXT: Entry @ 0xa3 { +# CHECK-NEXT: Abbrev: 0x34 +# CHECK-NEXT: Tag: DW_TAG_variable +# CHECK-NEXT: DW_IDX_die_offset: 0x00000003 +# CHECK-NEXT: } +# CHECK-NEXT: } +# CHECK-NEXT: ] +# CHECK-NEXT: } Index: tools/llvm-dwarfdump/llvm-dwarfdump.cpp =================================================================== --- tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -377,6 +377,7 @@ return DumpOffsets[DIDT_ID_DebugInfo] = *Offset; if (auto Offset = find(DICtx.getAppleNamespaces())) return DumpOffsets[DIDT_ID_DebugInfo] = *Offset; + // TODO: Add .debug_names support } return None; }();