35 return OS <<
"DW_ATOM_unknown_" <<
format(
"%x",
A.Value);
49 "Section too small: cannot read header.");
63 Hdr.BucketCount * 4 + Hdr.HashCount * 8 - 1))
66 "Section too small: cannot read buckets and hashes.");
71 for (
unsigned i = 0; i < NumAtoms; ++i) {
74 HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
85 return Hdr.HeaderDataLength;
103 FormValue.
getForm() == dwarf::DW_FORM_sdata)
113std::pair<uint64_t, dwarf::Tag>
122 switch (Atom.first) {
133 return {DieOffset, DieTag};
136void AppleAcceleratorTable::Header::dump(
ScopedPrinter &W)
const {
138 W.printHex(
"Magic", Magic);
139 W.printHex(
"Version", Version);
140 W.printHex(
"Hash function", HashFunction);
141 W.printNumber(
"Bucket count", BucketCount);
142 W.printNumber(
"Hashes count", HashCount);
143 W.printNumber(
"HeaderData length", HeaderDataLength);
146std::optional<uint64_t> AppleAcceleratorTable::HeaderData::extractOffset(
147 std::optional<DWARFFormValue>
Value)
const {
151 switch (
Value->getForm()) {
152 case dwarf::DW_FORM_ref1:
153 case dwarf::DW_FORM_ref2:
154 case dwarf::DW_FORM_ref4:
155 case dwarf::DW_FORM_ref8:
156 case dwarf::DW_FORM_ref_udata:
157 return Value->getRawUValue() + DIEOffsetBase;
159 return Value->getAsSectionOffset();
169 W.printString(
"Incorrectly terminated list.");
177 W.startLine() <<
format(
"String: 0x%08" PRIx64, StringOffset);
184 for (
auto &Atom : AtomForms) {
185 W.startLine() <<
format(
"Atom[%d]: ", i);
187 Atom.dump(
W.getOStream());
188 if (std::optional<uint64_t> Val = Atom.getAsUnsignedConstant()) {
191 W.getOStream() <<
" (" << Str <<
")";
194 W.getOStream() <<
"Error extracting the value";
195 W.getOStream() <<
"\n";
210 W.printNumber(
"DIE offset base", HdrData.DIEOffsetBase);
211 W.printNumber(
"Number of atoms",
uint64_t(HdrData.Atoms.size()));
216 for (
const auto &Atom : HdrData.Atoms) {
218 W.startLine() <<
"Type: " <<
formatAtom(Atom.first) <<
'\n';
219 W.startLine() <<
"Form: " <<
formatv(
"{0}", Atom.second) <<
'\n';
227 uint64_t OffsetsBase = HashesBase + Hdr.HashCount * 4;
229 for (
unsigned Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket) {
233 if (
Index == UINT32_MAX) {
234 W.printString(
"EMPTY");
238 for (
unsigned HashIdx =
Index; HashIdx < Hdr.HashCount; ++HashIdx) {
239 uint64_t HashOffset = HashesBase + HashIdx*4;
240 uint64_t OffsetsOffset = OffsetsBase + HashIdx*4;
243 if (Hash % Hdr.BucketCount != Bucket)
249 W.printString(
"Invalid section offset");
252 while (dumpName(W, AtomForms, &DataOffset))
258AppleAcceleratorTable::Entry::Entry(
259 const AppleAcceleratorTable::HeaderData &HdrData)
260 : HdrData(&HdrData) {
261 Values.reserve(HdrData.Atoms.size());
262 for (
const auto &Atom : HdrData.Atoms)
266void AppleAcceleratorTable::Entry::extract(
275std::optional<DWARFFormValue>
277 assert(HdrData &&
"Dereferencing end iterator?");
278 assert(HdrData->Atoms.size() == Values.size());
279 for (
auto Tuple :
zip_first(HdrData->Atoms, Values)) {
280 if (std::get<0>(Tuple).first == Atom)
281 return std::get<1>(Tuple);
286std::optional<uint64_t>
299 if (std::optional<uint64_t>
Value =
Tag->getAsUnsignedConstant())
307 if (!
AccelTable.AccelSection.isValidOffsetForDataOfSize(DataOffset, 4))
311 NumData =
AccelTable.AccelSection.getU32(&DataOffset);
315void AppleAcceleratorTable::ValueIterator::Next() {
316 assert(NumData > 0 &&
"attempted to increment iterator past the end");
318 if (
Data >= NumData ||
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;
340 uint64_t BucketOffset = BucketBase + Bucket * 4;
344 for (
unsigned HashIdx =
Index; HashIdx < Hdr.HashCount; ++HashIdx) {
345 uint64_t HashOffset = HashesBase + HashIdx * 4;
346 uint64_t OffsetsOffset = OffsetsBase + HashIdx * 4;
349 if (Hash % Hdr.BucketCount != Bucket)
367 W.printHex(
"Length", UnitLength);
369 W.printNumber(
"Version", Version);
370 W.printNumber(
"CU count", CompUnitCount);
371 W.printNumber(
"Local TU count", LocalTypeUnitCount);
372 W.printNumber(
"Foreign TU count", ForeignTypeUnitCount);
373 W.printNumber(
"Bucket count", BucketCount);
374 W.printNumber(
"Name count", NameCount);
375 W.printHex(
"Abbreviations table size", AbbrevTableSize);
376 W.startLine() <<
"Augmentation: '" << AugmentationString <<
"'\n";
383 "parsing .debug_names header at 0x%" PRIx64
": %s",
393 LocalTypeUnitCount = AS.
getU32(
C);
394 ForeignTypeUnitCount = AS.
getU32(
C);
397 AbbrevTableSize = AS.
getU32(
C);
401 return HeaderError(
C.takeError());
405 "cannot read header augmentation"));
406 AugmentationString.resize(AugmentationStringSize);
407 AS.
getU8(
C,
reinterpret_cast<uint8_t *
>(AugmentationString.data()),
408 AugmentationStringSize);
410 return C.takeError();
418 W.startLine() <<
formatv(
"{0}: {1}\n", Attr.Index, Attr.Form);
434 return Abbr.
Code == 0;
446DWARFDebugNames::NameIndex::extractAttributeEncoding(
uint64_t *
Offset) {
447 if (*
Offset >= EntriesBase) {
449 "Incorrectly terminated abbreviation table.");
458DWARFDebugNames::NameIndex::extractAttributeEncodings(
uint64_t *
Offset) {
459 std::vector<AttributeEncoding>
Result;
461 auto AttrEncOr = extractAttributeEncoding(
Offset);
463 return AttrEncOr.takeError();
465 return std::move(Result);
467 Result.emplace_back(*AttrEncOr);
473 if (*
Offset >= EntriesBase) {
475 "Incorrectly terminated abbreviation table.");
483 auto AttrEncOr = extractAttributeEncodings(
Offset);
485 return AttrEncOr.takeError();
497 Offset += Hdr.CompUnitCount * SectionOffsetSize;
498 Offset += Hdr.LocalTypeUnitCount * SectionOffsetSize;
499 Offset += Hdr.ForeignTypeUnitCount * 8;
501 Offset += Hdr.BucketCount * 4;
503 if (Hdr.BucketCount > 0)
504 Offset += Hdr.NameCount * 4;
505 StringOffsetsBase =
Offset;
506 Offset += Hdr.NameCount * SectionOffsetSize;
507 EntryOffsetsBase =
Offset;
508 Offset += Hdr.NameCount * SectionOffsetSize;
512 "Section too small: cannot read abbreviations.");
514 EntriesBase =
Offset + Hdr.AbbrevTableSize;
517 auto AbbrevOr = extractAbbrev(&
Offset);
519 return AbbrevOr.takeError();
523 if (!Abbrevs.insert(std::move(*AbbrevOr)).second)
525 "Duplicate abbreviation code.");
530 : NameIdx(&NameIdx), Abbr(&Abbr) {
535 Values.emplace_back(Attr.Form);
538std::optional<DWARFFormValue>
543 return std::get<1>(Tuple);
549 if (std::optional<DWARFFormValue> Off =
lookup(dwarf::DW_IDX_die_offset))
550 return Off->getAsReferenceUVal();
555 if (std::optional<DWARFFormValue> Off =
lookup(dwarf::DW_IDX_compile_unit))
556 return Off->getAsUnsignedConstant();
565 std::optional<uint64_t>
Index = getCUIndex();
572 W.printHex(
"Abbrev", Abbr->
Code);
573 W.startLine() <<
formatv(
"Tag: {0}\n", Abbr->
Tag);
576 W.startLine() <<
formatv(
"{0}: ", std::get<0>(Tuple).
Index);
577 std::get<1>(Tuple).dump(W.getOStream());
578 W.getOStream() <<
'\n';
591 return Section.AccelSection.getRelocatedValue(SectionOffsetSize, &
Offset);
595 assert(TU < Hdr.LocalTypeUnitCount);
597 uint64_t Offset = CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
598 return Section.AccelSection.getRelocatedValue(SectionOffsetSize, &
Offset);
602 assert(TU < Hdr.ForeignTypeUnitCount);
606 SectionOffsetSize * (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) + 8 * TU;
607 return Section.AccelSection.getU64(&
Offset);
615 "Incorrectly terminated entry list.");
619 return make_error<SentinelError>();
621 const auto AbbrevIt = Abbrevs.find_as(AbbrevCode);
622 if (AbbrevIt == Abbrevs.end())
625 Entry E(*
this, *AbbrevIt);
628 for (
auto &
Value :
E.Values) {
631 "Error extracting index attribute values.");
641 StringOffsetsBase + SectionOffsetSize * (
Index - 1);
643 EntryOffsetsBase + SectionOffsetSize * (
Index - 1);
647 AS.getRelocatedValue(SectionOffsetSize, &StringOffsetOffset);
648 uint64_t EntryOffset = AS.getUnsigned(&EntryOffsetOffset, SectionOffsetSize);
649 EntryOffset += EntriesBase;
650 return {Section.StringSection,
Index, StringOffset, EntryOffset};
655 assert(Bucket < Hdr.BucketCount);
656 uint64_t BucketOffset = BucketsBase + 4 * Bucket;
657 return Section.AccelSection.getU32(&BucketOffset);
663 return Section.AccelSection.getU32(&HashOffset);
673 auto EntryOr = getEntry(
Offset);
686 const NameTableEntry &NTE,
687 std::optional<uint32_t> Hash)
const {
690 W.printHex(
"Hash", *Hash);
692 W.startLine() <<
format(
"String: 0x%08" PRIx64, NTE.getStringOffset());
693 W.getOStream() <<
" \"" << NTE.getString() <<
"\"\n";
695 uint64_t EntryOffset = NTE.getEntryOffset();
696 while (dumpEntry(W, &EntryOffset))
700void DWARFDebugNames::NameIndex::dumpCUs(
ScopedPrinter &W)
const {
701 ListScope CUScope(W,
"Compilation Unit offsets");
703 W.startLine() <<
format(
"CU[%u]: 0x%08" PRIx64
"\n",
CU, getCUOffset(
CU));
706void DWARFDebugNames::NameIndex::dumpLocalTUs(
ScopedPrinter &W)
const {
707 if (Hdr.LocalTypeUnitCount == 0)
710 ListScope TUScope(W,
"Local Type Unit offsets");
711 for (
uint32_t TU = 0; TU < Hdr.LocalTypeUnitCount; ++TU)
712 W.startLine() <<
format(
"LocalTU[%u]: 0x%08" PRIx64
"\n", TU,
713 getLocalTUOffset(TU));
716void DWARFDebugNames::NameIndex::dumpForeignTUs(
ScopedPrinter &W)
const {
717 if (Hdr.ForeignTypeUnitCount == 0)
720 ListScope TUScope(W,
"Foreign Type Unit signatures");
721 for (
uint32_t TU = 0; TU < Hdr.ForeignTypeUnitCount; ++TU) {
722 W.startLine() <<
format(
"ForeignTU[%u]: 0x%016" PRIx64
"\n", TU,
723 getForeignTUSignature(TU));
727void DWARFDebugNames::NameIndex::dumpAbbreviations(
ScopedPrinter &W)
const {
728 ListScope AbbrevsScope(W,
"Abbreviations");
729 for (
const auto &Abbr : Abbrevs)
738 W.printString(
"EMPTY");
741 if (
Index > Hdr.NameCount) {
742 W.printString(
"Name index is invalid");
748 if (Hash % Hdr.BucketCount != Bucket)
751 dumpName(W, getNameTableEntry(
Index), Hash);
761 dumpAbbreviations(W);
763 if (Hdr.BucketCount > 0) {
764 for (
uint32_t Bucket = 0; Bucket < Hdr.BucketCount; ++Bucket)
765 dumpBucket(W, Bucket);
769 W.startLine() <<
"Hash table not present\n";
771 dumpName(W, NTE, std::nullopt);
781 NameIndices.push_back(std::move(Next));
797std::optional<uint64_t>
798DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() {
799 const Header &Hdr = CurrentIndex->Hdr;
800 if (Hdr.BucketCount == 0) {
802 for (
const NameTableEntry &NTE : *CurrentIndex) {
803 if (NTE.getString() == Key)
804 return NTE.getEntryOffset();
813 uint32_t Bucket = *Hash % Hdr.BucketCount;
820 if (Hash % Hdr.BucketCount != Bucket)
823 NameTableEntry NTE = CurrentIndex->getNameTableEntry(
Index);
824 if (NTE.getString() == Key)
825 return NTE.getEntryOffset();
830bool DWARFDebugNames::ValueIterator::getEntryAtCurrentOffset() {
831 auto EntryOr = CurrentIndex->getEntry(&DataOffset);
836 CurrentEntry = std::move(*EntryOr);
840bool DWARFDebugNames::ValueIterator::findInCurrentIndex() {
841 std::optional<uint64_t>
Offset = findEntryOffsetInCurrentIndex();
845 return getEntryAtCurrentOffset();
848void DWARFDebugNames::ValueIterator::searchFromStartOfCurrentIndex() {
849 for (
const NameIndex *
End = CurrentIndex->Section.NameIndices.end();
850 CurrentIndex !=
End; ++CurrentIndex) {
851 if (findInCurrentIndex())
857void DWARFDebugNames::ValueIterator::next() {
858 assert(CurrentIndex &&
"Incrementing an end() iterator?");
861 if (getEntryAtCurrentOffset())
865 if (IsLocal || CurrentIndex == &CurrentIndex->Section.NameIndices.back()) {
872 searchFromStartOfCurrentIndex();
879 searchFromStartOfCurrentIndex();
885 if (!findInCurrentIndex())
891 if (NameIndices.empty())
898 if (CUToNameIndex.size() == 0 && NameIndices.size() > 0) {
899 for (
const auto &NI : *
this) {
901 CUToNameIndex.try_emplace(NI.getCUOffset(
CU), &NI);
904 return CUToNameIndex.lookup(CUOffset);
#define offsetof(TYPE, MEMBER)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
static constexpr DWARFDebugNames::AttributeEncoding sentinelAttrEnc()
static bool isSentinel(const DWARFDebugNames::AttributeEncoding &AE)
static DWARFDebugNames::Abbrev sentinelAbbrev()
static Atom formatAtom(unsigned Atom)
This file contains constants used for implementing Dwarf debug support.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
std::optional< DWARFFormValue > lookup(HeaderData::AtomType Atom) const
Returns the value of the Atom in this Accelerator Entry, if the Entry contains such Atom.
std::optional< uint64_t > getDIESectionOffset() const
Returns the Section Offset of the Debug Info Entry associated with this Accelerator Entry or std::nul...
std::optional< dwarf::Tag > getTag() const override
Returns the Tag of the Debug Info Entry associated with this Accelerator Entry or std::nullopt if the...
std::optional< uint64_t > getCUOffset() const override
Returns the Offset of the Compilation Unit associated with this Accelerator Entry or std::nullopt if ...
ValueIterator()=default
End marker.
This implements the Apple accelerator table format, a precursor of the DWARF 5 accelerator table form...
uint32_t getHeaderDataLength()
std::pair< uint64_t, dwarf::Tag > readAtoms(uint64_t *HashDataOffset)
Return information related to the DWARF DIE we're looking for when performing a lookup by name.
iterator_range< ValueIterator > equal_range(StringRef Key) const
Look up all entries in the accelerator table matching Key.
void dump(raw_ostream &OS) const override
ArrayRef< std::pair< HeaderData::AtomType, HeaderData::Form > > getAtomsDesc()
Return the Atom description, which can be used to interpret the raw values of the Accelerator Entries...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
SmallVector< DWARFFormValue, 3 > Values
DataExtractor StringSection
virtual ~DWARFAcceleratorTable()
DWARFDataExtractor AccelSection
DWARF v5-specific implementation of an Accelerator Entry.
std::optional< uint64_t > getCUIndex() const
Returns the Index into the Compilation Unit list of the owning Name Index or std::nullopt if this Acc...
std::optional< uint64_t > getCUOffset() const override
Returns the Offset of the Compilation Unit associated with this Accelerator Entry or std::nullopt if ...
std::optional< uint64_t > getDIEUnitOffset() const
Returns the Offset of the DIE within the containing CU or TU.
std::optional< DWARFFormValue > lookup(dwarf::Index Index) const
Returns the value of the Index Attribute in this Accelerator Entry, if the Entry contains such Attrib...
void dump(ScopedPrinter &W) const
Represents a single accelerator table within the DWARF v5 .debug_names section.
uint32_t getHashArrayEntry(uint32_t Index) const
Reads an entry in the Hash Array for the given Index.
uint64_t getLocalTUOffset(uint32_t TU) const
Reads offset of local type unit TU, TU is 0-based.
uint32_t getBucketArrayEntry(uint32_t Bucket) const
Reads an entry in the Bucket Array for the given Bucket.
uint64_t getNextUnitOffset() const
void dump(ScopedPrinter &W) const
uint32_t getCUCount() const
iterator_range< ValueIterator > equal_range(StringRef Key) const
Look up all entries in this Name Index matching Key.
uint64_t getCUOffset(uint32_t CU) const
Reads offset of compilation unit CU. CU is 0-based.
Expected< Entry > getEntry(uint64_t *Offset) const
NameTableEntry getNameTableEntry(uint32_t Index) const
Reads an entry in the Name Table for the given Index.
uint64_t getForeignTUSignature(uint32_t TU) const
Reads signature of foreign type unit TU. TU is 0-based.
A single entry in the Name Table (DWARF v5 sect.
Error returned by NameIndex::getEntry to report it has reached the end of the entry list.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
ValueIterator()=default
End marker.
.debug_names section consists of one or more units.
const NameIndex * getCUNameIndex(uint64_t CUOffset)
Return the Name Index covering the compile unit at CUOffset, or nullptr if there is no Name Index cov...
const_iterator begin() const
iterator_range< ValueIterator > equal_range(StringRef Key) const
Look up all entries in the accelerator table matching Key.
void dump(raw_ostream &OS) const override
Base class for error info classes.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static Twine utohexstr(const uint64_t &Val)
LLVM Value Representation.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
StringRef AtomTypeString(unsigned Atom)
StringRef FormatString(DwarfFormat Format)
@ C
The default llvm calling convention, compatible with C.
StringRef AtomValueString(uint16_t Atom, unsigned Val)
Returns the symbolic string representing Val when used as a value for atom Atom.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
const uint32_t DW_INVALID_OFFSET
Identifier of an invalid DIE offset in the .debug_info section.
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
@ DW_ATOM_die_offset
Marker as the end of a list of atoms.
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
uint32_t caseFoldingDjbHash(StringRef Buffer, uint32_t H=5381)
Computes the Bernstein hash after folding the input according to the Dwarf 5 standard case folding ru...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
uint32_t djbHash(StringRef Buffer, uint32_t H=5381)
The Bernstein hash function used by the DWARF accelerator tables.
void consumeError(Error Err)
Consume a Error without doing anything.
Abbreviation describing the encoding of Name Index entries.
void dump(ScopedPrinter &W) const
uint32_t Code
Abbreviation code.
std::vector< AttributeEncoding > Attributes
List of index attributes.
dwarf::Tag Tag
Dwarf Tag of the described entity.
Index attribute and its encoding.