Skip to content

Commit 43f1fa9

Browse files
authored
[LLVM][DebugInfo] Refactor some code for easier sharing. (#82153)
Refactor the code that calculates the offsets for the various pieces of the DWARF .debug_names index section, to make it easier to share the code with other tools, such as LLD.
1 parent 0107c88 commit 43f1fa9

File tree

2 files changed

+65
-34
lines changed

2 files changed

+65
-34
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,17 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
562562
uint64_t getEntryOffset() const { return EntryOffset; }
563563
};
564564

565+
/// Offsets for the start of various important tables from the start of the
566+
/// section.
567+
struct DWARFDebugNamesOffsets {
568+
uint64_t CUsBase;
569+
uint64_t BucketsBase;
570+
uint64_t HashesBase;
571+
uint64_t StringOffsetsBase;
572+
uint64_t EntryOffsetsBase;
573+
uint64_t EntriesBase;
574+
};
575+
565576
/// Represents a single accelerator table within the DWARF v5 .debug_names
566577
/// section.
567578
class NameIndex {
@@ -572,12 +583,7 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
572583
// Base of the whole unit and of various important tables, as offsets from
573584
// the start of the section.
574585
uint64_t Base;
575-
uint64_t CUsBase;
576-
uint64_t BucketsBase;
577-
uint64_t HashesBase;
578-
uint64_t StringOffsetsBase;
579-
uint64_t EntryOffsetsBase;
580-
uint64_t EntriesBase;
586+
DWARFDebugNamesOffsets Offsets;
581587

582588
void dumpCUs(ScopedPrinter &W) const;
583589
void dumpLocalTUs(ScopedPrinter &W) const;
@@ -638,7 +644,7 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
638644
/// Returns the Entry at the relative `Offset` from the start of the Entry
639645
/// pool.
640646
Expected<Entry> getEntryAtRelativeOffset(uint64_t Offset) const {
641-
auto OffsetFromSection = Offset + this->EntriesBase;
647+
auto OffsetFromSection = Offset + this->Offsets.EntriesBase;
642648
return getEntry(&OffsetFromSection);
643649
}
644650

@@ -793,6 +799,12 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
793799
const NameIndex *getCUNameIndex(uint64_t CUOffset);
794800
};
795801

802+
/// Calculates the starting offsets for various sections within the
803+
/// .debug_names section.
804+
void findDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
805+
uint64_t HdrSize, const dwarf::DwarfFormat Format,
806+
const DWARFDebugNames::Header &Hdr);
807+
796808
/// If `Name` is the name of a templated function that includes template
797809
/// parameters, returns a substring of `Name` containing no template
798810
/// parameters.

llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getTombstoneKey() {
510510

511511
Expected<DWARFDebugNames::AttributeEncoding>
512512
DWARFDebugNames::NameIndex::extractAttributeEncoding(uint64_t *Offset) {
513-
if (*Offset >= EntriesBase) {
513+
if (*Offset >= Offsets.EntriesBase) {
514514
return createStringError(errc::illegal_byte_sequence,
515515
"Incorrectly terminated abbreviation table.");
516516
}
@@ -536,7 +536,7 @@ DWARFDebugNames::NameIndex::extractAttributeEncodings(uint64_t *Offset) {
536536

537537
Expected<DWARFDebugNames::Abbrev>
538538
DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
539-
if (*Offset >= EntriesBase) {
539+
if (*Offset >= Offsets.EntriesBase) {
540540
return createStringError(errc::illegal_byte_sequence,
541541
"Incorrectly terminated abbreviation table.");
542542
}
@@ -552,32 +552,50 @@ DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
552552
return Abbrev(Code, dwarf::Tag(Tag), AbbrevOffset, std::move(*AttrEncOr));
553553
}
554554

555+
void llvm::findDebugNamesOffsets(
556+
DWARFDebugNames::DWARFDebugNamesOffsets &Offsets, uint64_t HdrSize,
557+
dwarf::DwarfFormat Format, const DWARFDebugNames::Header &Hdr) {
558+
uint32_t DwarfSize = (Format == llvm::dwarf::DwarfFormat::DWARF64) ? 8 : 4;
559+
uint64_t Offset = HdrSize;
560+
Offsets.CUsBase = Offset;
561+
Offset += Hdr.CompUnitCount * DwarfSize;
562+
Offset += Hdr.LocalTypeUnitCount * DwarfSize;
563+
Offset += Hdr.ForeignTypeUnitCount * 8;
564+
565+
Offsets.BucketsBase = Offset;
566+
Offset += Hdr.BucketCount * 4;
567+
568+
Offsets.HashesBase = Offset;
569+
if (Hdr.BucketCount > 0)
570+
Offset += Hdr.NameCount * 4;
571+
572+
Offsets.StringOffsetsBase = Offset;
573+
Offset += Hdr.NameCount * DwarfSize;
574+
575+
Offsets.EntryOffsetsBase = Offset;
576+
Offset += Hdr.NameCount * DwarfSize;
577+
578+
Offset += Hdr.AbbrevTableSize;
579+
Offsets.EntriesBase = Offset;
580+
}
581+
555582
Error DWARFDebugNames::NameIndex::extract() {
556583
const DWARFDataExtractor &AS = Section.AccelSection;
557-
uint64_t Offset = Base;
558-
if (Error E = Hdr.extract(AS, &Offset))
584+
uint64_t hdrSize = Base;
585+
if (Error E = Hdr.extract(AS, &hdrSize))
559586
return E;
560587

561588
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
562-
CUsBase = Offset;
563-
Offset += Hdr.CompUnitCount * SectionOffsetSize;
564-
Offset += Hdr.LocalTypeUnitCount * SectionOffsetSize;
565-
Offset += Hdr.ForeignTypeUnitCount * 8;
566-
BucketsBase = Offset;
567-
Offset += Hdr.BucketCount * 4;
568-
HashesBase = Offset;
569-
if (Hdr.BucketCount > 0)
570-
Offset += Hdr.NameCount * 4;
571-
StringOffsetsBase = Offset;
572-
Offset += Hdr.NameCount * SectionOffsetSize;
573-
EntryOffsetsBase = Offset;
574-
Offset += Hdr.NameCount * SectionOffsetSize;
589+
findDebugNamesOffsets(Offsets, hdrSize, Hdr.Format, Hdr);
590+
591+
uint64_t Offset =
592+
Offsets.EntryOffsetsBase + (Hdr.NameCount * SectionOffsetSize);
575593

576594
if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize))
577595
return createStringError(errc::illegal_byte_sequence,
578596
"Section too small: cannot read abbreviations.");
579597

580-
EntriesBase = Offset + Hdr.AbbrevTableSize;
598+
Offsets.EntriesBase = Offset + Hdr.AbbrevTableSize;
581599

582600
for (;;) {
583601
auto AbbrevOr = extractAbbrev(&Offset);
@@ -679,7 +697,7 @@ void DWARFDebugNames::Entry::dumpParentIdx(
679697
return;
680698
}
681699

682-
auto AbsoluteOffset = NameIdx->EntriesBase + FormValue.getRawUValue();
700+
auto AbsoluteOffset = NameIdx->Offsets.EntriesBase + FormValue.getRawUValue();
683701
W.getOStream() << "Entry @ 0x" + Twine::utohexstr(AbsoluteOffset);
684702
}
685703

@@ -708,22 +726,23 @@ std::error_code DWARFDebugNames::SentinelError::convertToErrorCode() const {
708726
uint64_t DWARFDebugNames::NameIndex::getCUOffset(uint32_t CU) const {
709727
assert(CU < Hdr.CompUnitCount);
710728
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
711-
uint64_t Offset = CUsBase + SectionOffsetSize * CU;
729+
uint64_t Offset = Offsets.CUsBase + SectionOffsetSize * CU;
712730
return Section.AccelSection.getRelocatedValue(SectionOffsetSize, &Offset);
713731
}
714732

715733
uint64_t DWARFDebugNames::NameIndex::getLocalTUOffset(uint32_t TU) const {
716734
assert(TU < Hdr.LocalTypeUnitCount);
717735
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
718-
uint64_t Offset = CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
736+
uint64_t Offset =
737+
Offsets.CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
719738
return Section.AccelSection.getRelocatedValue(SectionOffsetSize, &Offset);
720739
}
721740

722741
uint64_t DWARFDebugNames::NameIndex::getForeignTUSignature(uint32_t TU) const {
723742
assert(TU < Hdr.ForeignTypeUnitCount);
724743
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
725744
uint64_t Offset =
726-
CUsBase +
745+
Offsets.CUsBase +
727746
SectionOffsetSize * (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) + 8 * TU;
728747
return Section.AccelSection.getU64(&Offset);
729748
}
@@ -759,28 +778,28 @@ DWARFDebugNames::NameIndex::getNameTableEntry(uint32_t Index) const {
759778
assert(0 < Index && Index <= Hdr.NameCount);
760779
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
761780
uint64_t StringOffsetOffset =
762-
StringOffsetsBase + SectionOffsetSize * (Index - 1);
781+
Offsets.StringOffsetsBase + SectionOffsetSize * (Index - 1);
763782
uint64_t EntryOffsetOffset =
764-
EntryOffsetsBase + SectionOffsetSize * (Index - 1);
783+
Offsets.EntryOffsetsBase + SectionOffsetSize * (Index - 1);
765784
const DWARFDataExtractor &AS = Section.AccelSection;
766785

767786
uint64_t StringOffset =
768787
AS.getRelocatedValue(SectionOffsetSize, &StringOffsetOffset);
769788
uint64_t EntryOffset = AS.getUnsigned(&EntryOffsetOffset, SectionOffsetSize);
770-
EntryOffset += EntriesBase;
789+
EntryOffset += Offsets.EntriesBase;
771790
return {Section.StringSection, Index, StringOffset, EntryOffset};
772791
}
773792

774793
uint32_t
775794
DWARFDebugNames::NameIndex::getBucketArrayEntry(uint32_t Bucket) const {
776795
assert(Bucket < Hdr.BucketCount);
777-
uint64_t BucketOffset = BucketsBase + 4 * Bucket;
796+
uint64_t BucketOffset = Offsets.BucketsBase + 4 * Bucket;
778797
return Section.AccelSection.getU32(&BucketOffset);
779798
}
780799

781800
uint32_t DWARFDebugNames::NameIndex::getHashArrayEntry(uint32_t Index) const {
782801
assert(0 < Index && Index <= Hdr.NameCount);
783-
uint64_t HashOffset = HashesBase + 4 * (Index - 1);
802+
uint64_t HashOffset = Offsets.HashesBase + 4 * (Index - 1);
784803
return Section.AccelSection.getU32(&HashOffset);
785804
}
786805

0 commit comments

Comments
 (0)