[llvm] [LLVM][DebugInfo] Refactor some code for easier sharing. (PR #82153)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 20 16:04:13 PST 2024
https://github.com/cmtice updated https://github.com/llvm/llvm-project/pull/82153
>From a25058ab15b886a46959479c751839f35e9b4ca5 Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice at google.com>
Date: Sat, 17 Feb 2024 21:14:01 -0800
Subject: [PATCH 1/4] [LLVM][DebugInfo] Refactor some code for easier sharing.
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.
---
.../DebugInfo/DWARF/DWARFAcceleratorTable.h | 28 ++++++--
.../DebugInfo/DWARF/DWARFAcceleratorTable.cpp | 71 ++++++++++++-------
2 files changed, 67 insertions(+), 32 deletions(-)
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
index a26c44bf7e9c29..0dd5ed55efe702 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
@@ -562,6 +562,17 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
uint64_t getEntryOffset() const { return EntryOffset; }
};
+ // Offsets for the start of various important tables from the start of the
+ // section.
+ struct DWARFDebugNamesOffsets {
+ uint64_t CUsBase;
+ uint64_t BucketsBase;
+ uint64_t HashesBase;
+ uint64_t StringOffsetsBase;
+ uint64_t EntryOffsetsBase;
+ uint64_t EntriesBase;
+ };
+
/// Represents a single accelerator table within the DWARF v5 .debug_names
/// section.
class NameIndex {
@@ -572,12 +583,7 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
// Base of the whole unit and of various important tables, as offsets from
// the start of the section.
uint64_t Base;
- uint64_t CUsBase;
- uint64_t BucketsBase;
- uint64_t HashesBase;
- uint64_t StringOffsetsBase;
- uint64_t EntryOffsetsBase;
- uint64_t EntriesBase;
+ DWARFDebugNamesOffsets Offsets;
void dumpCUs(ScopedPrinter &W) const;
void dumpLocalTUs(ScopedPrinter &W) const;
@@ -638,7 +644,7 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
/// Returns the Entry at the relative `Offset` from the start of the Entry
/// pool.
Expected<Entry> getEntryAtRelativeOffset(uint64_t Offset) const {
- auto OffsetFromSection = Offset + this->EntriesBase;
+ auto OffsetFromSection = Offset + this->Offsets.EntriesBase;
return getEntry(&OffsetFromSection);
}
@@ -793,6 +799,14 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
const NameIndex *getCUNameIndex(uint64_t CUOffset);
};
+// Calculates the starting offsets for various sections within the
+// debug_names section. This has been pulled out of DWARFDebugNames, to
+// allow easier sharing of the code with other tools, such as LLD.
+uint64_t FindDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
+ uint64_t HdrSize,
+ const dwarf::DwarfFormat Format,
+ const DWARFDebugNames::Header &Hdr);
+
/// If `Name` is the name of a templated function that includes template
/// parameters, returns a substring of `Name` containing no template
/// parameters.
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
index 78f819dd052aac..68adad517f9364 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
@@ -510,7 +510,7 @@ DWARFDebugNames::Abbrev DWARFDebugNames::AbbrevMapInfo::getTombstoneKey() {
Expected<DWARFDebugNames::AttributeEncoding>
DWARFDebugNames::NameIndex::extractAttributeEncoding(uint64_t *Offset) {
- if (*Offset >= EntriesBase) {
+ if (*Offset >= Offsets.EntriesBase) {
return createStringError(errc::illegal_byte_sequence,
"Incorrectly terminated abbreviation table.");
}
@@ -536,7 +536,7 @@ DWARFDebugNames::NameIndex::extractAttributeEncodings(uint64_t *Offset) {
Expected<DWARFDebugNames::Abbrev>
DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
- if (*Offset >= EntriesBase) {
+ if (*Offset >= Offsets.EntriesBase) {
return createStringError(errc::illegal_byte_sequence,
"Incorrectly terminated abbreviation table.");
}
@@ -552,6 +552,37 @@ DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
return Abbrev(Code, dwarf::Tag(Tag), AbbrevOffset, std::move(*AttrEncOr));
}
+uint64_t llvm::FindDebugNamesOffsets(
+ DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
+ uint64_t HdrSize,
+ dwarf::DwarfFormat Format,
+ const DWARFDebugNames::Header &Hdr) {
+ uint32_t DwarfSize = (Format == llvm::dwarf::DwarfFormat::DWARF32) ? 4 : 8;
+ uint64_t Offset = HdrSize;
+ Offsets.CUsBase = Offset;
+ Offset += Hdr.CompUnitCount * DwarfSize;
+ Offset += Hdr.LocalTypeUnitCount * DwarfSize;
+ Offset += Hdr.ForeignTypeUnitCount * 8;
+
+ Offsets.BucketsBase = Offset;
+ Offset += Hdr.BucketCount * 4;
+
+ Offsets.HashesBase = Offset;
+ if (Hdr.BucketCount > 0)
+ Offset += Hdr.NameCount * 4;
+
+ Offsets.StringOffsetsBase = Offset;
+ Offset += Hdr.NameCount * DwarfSize;
+
+ Offsets.EntryOffsetsBase = Offset;
+ Offset += Hdr.NameCount * DwarfSize;
+
+ Offset += Hdr.AbbrevTableSize;
+ Offsets.EntriesBase = Offset;
+
+ return Offset;
+}
+
Error DWARFDebugNames::NameIndex::extract() {
const DWARFDataExtractor &AS = Section.AccelSection;
uint64_t Offset = Base;
@@ -559,25 +590,15 @@ Error DWARFDebugNames::NameIndex::extract() {
return E;
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
- CUsBase = Offset;
- Offset += Hdr.CompUnitCount * SectionOffsetSize;
- Offset += Hdr.LocalTypeUnitCount * SectionOffsetSize;
- 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 * SectionOffsetSize;
- EntryOffsetsBase = Offset;
- Offset += Hdr.NameCount * SectionOffsetSize;
+ Offset = FindDebugNamesOffsets(Offsets, Offset, Hdr.Format, Hdr);
+
+ Offset = Offsets.EntryOffsetsBase + (Hdr.NameCount * SectionOffsetSize);
if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize))
return createStringError(errc::illegal_byte_sequence,
"Section too small: cannot read abbreviations.");
- EntriesBase = Offset + Hdr.AbbrevTableSize;
+ Offsets.EntriesBase = Offset + Hdr.AbbrevTableSize;
for (;;) {
auto AbbrevOr = extractAbbrev(&Offset);
@@ -679,7 +700,7 @@ void DWARFDebugNames::Entry::dumpParentIdx(
return;
}
- auto AbsoluteOffset = NameIdx->EntriesBase + FormValue.getRawUValue();
+ auto AbsoluteOffset = NameIdx->Offsets.EntriesBase + FormValue.getRawUValue();
W.getOStream() << "Entry @ 0x" + Twine::utohexstr(AbsoluteOffset);
}
@@ -708,14 +729,14 @@ std::error_code DWARFDebugNames::SentinelError::convertToErrorCode() const {
uint64_t DWARFDebugNames::NameIndex::getCUOffset(uint32_t CU) const {
assert(CU < Hdr.CompUnitCount);
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
- uint64_t Offset = CUsBase + SectionOffsetSize * CU;
+ uint64_t Offset = Offsets.CUsBase + SectionOffsetSize * CU;
return Section.AccelSection.getRelocatedValue(SectionOffsetSize, &Offset);
}
uint64_t DWARFDebugNames::NameIndex::getLocalTUOffset(uint32_t TU) const {
assert(TU < Hdr.LocalTypeUnitCount);
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
- uint64_t Offset = CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
+ uint64_t Offset = Offsets.CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
return Section.AccelSection.getRelocatedValue(SectionOffsetSize, &Offset);
}
@@ -723,7 +744,7 @@ uint64_t DWARFDebugNames::NameIndex::getForeignTUSignature(uint32_t TU) const {
assert(TU < Hdr.ForeignTypeUnitCount);
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
uint64_t Offset =
- CUsBase +
+ Offsets.CUsBase +
SectionOffsetSize * (Hdr.CompUnitCount + Hdr.LocalTypeUnitCount) + 8 * TU;
return Section.AccelSection.getU64(&Offset);
}
@@ -759,28 +780,28 @@ DWARFDebugNames::NameIndex::getNameTableEntry(uint32_t Index) const {
assert(0 < Index && Index <= Hdr.NameCount);
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
uint64_t StringOffsetOffset =
- StringOffsetsBase + SectionOffsetSize * (Index - 1);
+ Offsets.StringOffsetsBase + SectionOffsetSize * (Index - 1);
uint64_t EntryOffsetOffset =
- EntryOffsetsBase + SectionOffsetSize * (Index - 1);
+ Offsets.EntryOffsetsBase + SectionOffsetSize * (Index - 1);
const DWARFDataExtractor &AS = Section.AccelSection;
uint64_t StringOffset =
AS.getRelocatedValue(SectionOffsetSize, &StringOffsetOffset);
uint64_t EntryOffset = AS.getUnsigned(&EntryOffsetOffset, SectionOffsetSize);
- EntryOffset += EntriesBase;
+ EntryOffset += Offsets.EntriesBase;
return {Section.StringSection, Index, StringOffset, EntryOffset};
}
uint32_t
DWARFDebugNames::NameIndex::getBucketArrayEntry(uint32_t Bucket) const {
assert(Bucket < Hdr.BucketCount);
- uint64_t BucketOffset = BucketsBase + 4 * Bucket;
+ uint64_t BucketOffset = Offsets.BucketsBase + 4 * Bucket;
return Section.AccelSection.getU32(&BucketOffset);
}
uint32_t DWARFDebugNames::NameIndex::getHashArrayEntry(uint32_t Index) const {
assert(0 < Index && Index <= Hdr.NameCount);
- uint64_t HashOffset = HashesBase + 4 * (Index - 1);
+ uint64_t HashOffset = Offsets.HashesBase + 4 * (Index - 1);
return Section.AccelSection.getU32(&HashOffset);
}
>From 710675732d3106671050aa7c1106b41308875118 Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice at google.com>
Date: Sat, 17 Feb 2024 22:42:15 -0800
Subject: [PATCH 2/4] [LLVM][DebugInfo] Refactor some code for easier sharing.
Clang format fixes.
---
llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
index 68adad517f9364..a271468a9da2f1 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
@@ -552,11 +552,10 @@ DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
return Abbrev(Code, dwarf::Tag(Tag), AbbrevOffset, std::move(*AttrEncOr));
}
-uint64_t llvm::FindDebugNamesOffsets(
- DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
- uint64_t HdrSize,
- dwarf::DwarfFormat Format,
- const DWARFDebugNames::Header &Hdr) {
+uint64_t
+llvm::FindDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
+ uint64_t HdrSize, dwarf::DwarfFormat Format,
+ const DWARFDebugNames::Header &Hdr) {
uint32_t DwarfSize = (Format == llvm::dwarf::DwarfFormat::DWARF32) ? 4 : 8;
uint64_t Offset = HdrSize;
Offsets.CUsBase = Offset;
@@ -736,7 +735,8 @@ uint64_t DWARFDebugNames::NameIndex::getCUOffset(uint32_t CU) const {
uint64_t DWARFDebugNames::NameIndex::getLocalTUOffset(uint32_t TU) const {
assert(TU < Hdr.LocalTypeUnitCount);
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
- uint64_t Offset = Offsets.CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
+ uint64_t Offset =
+ Offsets.CUsBase + SectionOffsetSize * (Hdr.CompUnitCount + TU);
return Section.AccelSection.getRelocatedValue(SectionOffsetSize, &Offset);
}
>From 7c3d097508bf231cc4281e65fdb0b7aec2e9624a Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice at google.com>
Date: Tue, 20 Feb 2024 15:46:54 -0800
Subject: [PATCH 3/4] [LLVM][DebugInfo] Refactor some code for easier sharing.
Change 'FindDebugNamesOffsets' function: make the return type 'void' (return
nothing); update the name to start with lowercase letter. Break 'offset'
variable in NameIndex::extract into two parts: hdrSize (to be passed to
Header::extract and findDebugNamesOffsets) and 'Offset', to be used for
offsets after call to findDebugNamesOffsets.
Change comments in header file to doxygen comments.
---
.../DebugInfo/DWARF/DWARFAcceleratorTable.h | 17 ++++++++---------
.../DebugInfo/DWARF/DWARFAcceleratorTable.cpp | 16 +++++++---------
2 files changed, 15 insertions(+), 18 deletions(-)
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
index 0dd5ed55efe702..f99d7ef91ade31 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
@@ -562,8 +562,8 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
uint64_t getEntryOffset() const { return EntryOffset; }
};
- // Offsets for the start of various important tables from the start of the
- // section.
+ /// Offsets for the start of various important tables from the start of the
+ /// section.
struct DWARFDebugNamesOffsets {
uint64_t CUsBase;
uint64_t BucketsBase;
@@ -799,13 +799,12 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
const NameIndex *getCUNameIndex(uint64_t CUOffset);
};
-// Calculates the starting offsets for various sections within the
-// debug_names section. This has been pulled out of DWARFDebugNames, to
-// allow easier sharing of the code with other tools, such as LLD.
-uint64_t FindDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
- uint64_t HdrSize,
- const dwarf::DwarfFormat Format,
- const DWARFDebugNames::Header &Hdr);
+/// Calculates the starting offsets for various sections within the
+/// .debug_names section.
+void findDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
+ uint64_t HdrSize,
+ const dwarf::DwarfFormat Format,
+ const DWARFDebugNames::Header &Hdr);
/// If `Name` is the name of a templated function that includes template
/// parameters, returns a substring of `Name` containing no template
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
index a271468a9da2f1..a057a9cabed7f5 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
@@ -552,11 +552,11 @@ DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
return Abbrev(Code, dwarf::Tag(Tag), AbbrevOffset, std::move(*AttrEncOr));
}
-uint64_t
-llvm::FindDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
+void
+llvm::findDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
uint64_t HdrSize, dwarf::DwarfFormat Format,
const DWARFDebugNames::Header &Hdr) {
- uint32_t DwarfSize = (Format == llvm::dwarf::DwarfFormat::DWARF32) ? 4 : 8;
+ uint32_t DwarfSize = (Format == llvm::dwarf::DwarfFormat::DWARF64) ? 8 : 4;
uint64_t Offset = HdrSize;
Offsets.CUsBase = Offset;
Offset += Hdr.CompUnitCount * DwarfSize;
@@ -578,20 +578,18 @@ llvm::FindDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
Offset += Hdr.AbbrevTableSize;
Offsets.EntriesBase = Offset;
-
- return Offset;
}
Error DWARFDebugNames::NameIndex::extract() {
const DWARFDataExtractor &AS = Section.AccelSection;
- uint64_t Offset = Base;
- if (Error E = Hdr.extract(AS, &Offset))
+ uint64_t hdrSize = Base;
+ if (Error E = Hdr.extract(AS, &hdrSize))
return E;
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
- Offset = FindDebugNamesOffsets(Offsets, Offset, Hdr.Format, Hdr);
+ findDebugNamesOffsets(Offsets, hdrSize, Hdr.Format, Hdr);
- Offset = Offsets.EntryOffsetsBase + (Hdr.NameCount * SectionOffsetSize);
+ uint64_t Offset = Offsets.EntryOffsetsBase + (Hdr.NameCount * SectionOffsetSize);
if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize))
return createStringError(errc::illegal_byte_sequence,
>From 2a4edad097cc9ca1b4b8bac707ae2f349ffd4f86 Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice at google.com>
Date: Tue, 20 Feb 2024 16:03:15 -0800
Subject: [PATCH 4/4] [LLVM][DebugInfo] Refactor some code for easier sharing.
Clang format fixes.
---
.../llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h | 3 +--
llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp | 10 +++++-----
2 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
index f99d7ef91ade31..d368c7e0ece8f3 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h
@@ -802,8 +802,7 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
/// Calculates the starting offsets for various sections within the
/// .debug_names section.
void findDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
- uint64_t HdrSize,
- const dwarf::DwarfFormat Format,
+ uint64_t HdrSize, const dwarf::DwarfFormat Format,
const DWARFDebugNames::Header &Hdr);
/// If `Name` is the name of a templated function that includes template
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
index a057a9cabed7f5..9c65d85985f1bb 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
@@ -552,10 +552,9 @@ DWARFDebugNames::NameIndex::extractAbbrev(uint64_t *Offset) {
return Abbrev(Code, dwarf::Tag(Tag), AbbrevOffset, std::move(*AttrEncOr));
}
-void
-llvm::findDebugNamesOffsets(DWARFDebugNames::DWARFDebugNamesOffsets &Offsets,
- uint64_t HdrSize, dwarf::DwarfFormat Format,
- const DWARFDebugNames::Header &Hdr) {
+void llvm::findDebugNamesOffsets(
+ DWARFDebugNames::DWARFDebugNamesOffsets &Offsets, uint64_t HdrSize,
+ dwarf::DwarfFormat Format, const DWARFDebugNames::Header &Hdr) {
uint32_t DwarfSize = (Format == llvm::dwarf::DwarfFormat::DWARF64) ? 8 : 4;
uint64_t Offset = HdrSize;
Offsets.CUsBase = Offset;
@@ -589,7 +588,8 @@ Error DWARFDebugNames::NameIndex::extract() {
const unsigned SectionOffsetSize = dwarf::getDwarfOffsetByteSize(Hdr.Format);
findDebugNamesOffsets(Offsets, hdrSize, Hdr.Format, Hdr);
- uint64_t Offset = Offsets.EntryOffsetsBase + (Hdr.NameCount * SectionOffsetSize);
+ uint64_t Offset =
+ Offsets.EntryOffsetsBase + (Hdr.NameCount * SectionOffsetSize);
if (!AS.isValidOffsetForDataOfSize(Offset, Hdr.AbbrevTableSize))
return createStringError(errc::illegal_byte_sequence,
More information about the llvm-commits
mailing list