Skip to content

Commit 01d7dcf

Browse files
authored
[lldb] Switch to llvm::DWARFUnitHeader (#89808)
These are now close enough that they can be swapped out.
1 parent b2098db commit 01d7dcf

File tree

4 files changed

+48
-165
lines changed

4 files changed

+48
-165
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DWARFCompileUnit : public DWARFUnit {
3232

3333
private:
3434
DWARFCompileUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
35-
const DWARFUnitHeader &header,
35+
const llvm::DWARFUnitHeader &header,
3636
const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
3737
DIERef::Section section, bool is_dwo)
3838
: DWARFUnit(dwarf, uid, header, abbrevs, section, is_dwo) {}

lldb/source/Plugins/SymbolFile/DWARF/DWARFTypeUnit.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class DWARFTypeUnit : public DWARFUnit {
2424

2525
void Dump(Stream *s) const override;
2626

27-
uint64_t GetTypeHash() { return m_header.GetTypeHash(); }
27+
uint64_t GetTypeHash() { return m_header.getTypeHash(); }
2828

29-
dw_offset_t GetTypeOffset() { return GetOffset() + m_header.GetTypeOffset(); }
29+
dw_offset_t GetTypeOffset() { return GetOffset() + m_header.getTypeOffset(); }
3030

3131
static bool classof(const DWARFUnit *unit) { return unit->IsTypeUnit(); }
3232

3333
private:
3434
DWARFTypeUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
35-
const DWARFUnitHeader &header,
35+
const llvm::DWARFUnitHeader &header,
3636
const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
3737
DIERef::Section section, bool is_dwo)
3838
: DWARFUnit(dwarf, uid, header, abbrevs, section, is_dwo) {}

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp

Lines changed: 34 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ using namespace lldb_private::plugin::dwarf;
3333
extern int g_verbose;
3434

3535
DWARFUnit::DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
36-
const DWARFUnitHeader &header,
36+
const llvm::DWARFUnitHeader &header,
3737
const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
3838
DIERef::Section section, bool is_dwo)
3939
: UserID(uid), m_dwarf(dwarf), m_header(header), m_abbrevs(&abbrevs),
4040
m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo),
41-
m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.GetDWOId()) {}
41+
m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.getDWOId()) {}
4242

4343
DWARFUnit::~DWARFUnit() = default;
4444

@@ -345,7 +345,7 @@ void DWARFUnit::ExtractDIEsRWLocked() {
345345
void DWARFUnit::SetDwoStrOffsetsBase() {
346346
lldb::offset_t baseOffset = 0;
347347

348-
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
348+
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.getIndexEntry()) {
349349
if (const auto *contribution =
350350
entry->getContribution(llvm::DW_SECT_STR_OFFSETS))
351351
baseOffset = contribution->getOffset();
@@ -489,7 +489,7 @@ ParseListTableHeader(const llvm::DWARFDataExtractor &data, uint64_t offset,
489489

490490
void DWARFUnit::SetLoclistsBase(dw_addr_t loclists_base) {
491491
uint64_t offset = 0;
492-
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
492+
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.getIndexEntry()) {
493493
const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
494494
if (!contribution) {
495495
GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
@@ -533,7 +533,7 @@ DWARFDataExtractor DWARFUnit::GetLocationData() const {
533533
DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext();
534534
const DWARFDataExtractor &data =
535535
GetVersion() >= 5 ? Ctx.getOrLoadLocListsData() : Ctx.getOrLoadLocData();
536-
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
536+
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.getIndexEntry()) {
537537
if (const auto *contribution = entry->getContribution(
538538
GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
539539
return DWARFDataExtractor(data, contribution->getOffset(),
@@ -546,7 +546,7 @@ DWARFDataExtractor DWARFUnit::GetLocationData() const {
546546
DWARFDataExtractor DWARFUnit::GetRnglistData() const {
547547
DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext();
548548
const DWARFDataExtractor &data = Ctx.getOrLoadRngListsData();
549-
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
549+
if (const llvm::DWARFUnitIndex::Entry *entry = m_header.getIndexEntry()) {
550550
if (const auto *contribution =
551551
entry->getContribution(llvm::DW_SECT_RNGLISTS))
552552
return DWARFDataExtractor(data, contribution->getOffset(),
@@ -924,111 +924,42 @@ const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
924924
return *m_func_aranges_up;
925925
}
926926

927-
llvm::Error DWARFUnitHeader::ApplyIndexEntry(
928-
const llvm::DWARFUnitIndex::Entry *index_entry) {
929-
// We should only be calling this function when the index entry is not set and
930-
// we have a valid one to set it to.
931-
assert(index_entry);
932-
assert(!m_index_entry);
933-
934-
if (m_abbr_offset)
935-
return llvm::createStringError(
936-
llvm::inconvertibleErrorCode(),
937-
"Package unit with a non-zero abbreviation offset");
938-
939-
auto *unit_contrib = index_entry->getContribution();
940-
if (!unit_contrib || unit_contrib->getLength32() != m_length + 4)
941-
return llvm::createStringError(llvm::inconvertibleErrorCode(),
942-
"Inconsistent DWARF package unit index");
943-
944-
auto *abbr_entry = index_entry->getContribution(llvm::DW_SECT_ABBREV);
945-
if (!abbr_entry)
946-
return llvm::createStringError(
947-
llvm::inconvertibleErrorCode(),
948-
"DWARF package index missing abbreviation column");
949-
950-
m_abbr_offset = abbr_entry->getOffset();
951-
m_index_entry = index_entry;
952-
return llvm::Error::success();
953-
}
954-
955-
llvm::Expected<DWARFUnitHeader>
956-
DWARFUnitHeader::extract(const DWARFDataExtractor &data,
957-
DIERef::Section section, DWARFContext &context,
958-
lldb::offset_t *offset_ptr) {
959-
DWARFUnitHeader header;
960-
header.m_offset = *offset_ptr;
961-
header.m_length = data.GetDWARFInitialLength(offset_ptr);
962-
header.m_version = data.GetU16(offset_ptr);
963-
if (header.m_version == 5) {
964-
header.m_unit_type = data.GetU8(offset_ptr);
965-
header.m_addr_size = data.GetU8(offset_ptr);
966-
header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
967-
if (header.m_unit_type == llvm::dwarf::DW_UT_skeleton ||
968-
header.m_unit_type == llvm::dwarf::DW_UT_split_compile)
969-
header.m_dwo_id = data.GetU64(offset_ptr);
970-
} else {
971-
header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
972-
header.m_addr_size = data.GetU8(offset_ptr);
973-
header.m_unit_type =
974-
section == DIERef::Section::DebugTypes ? DW_UT_type : DW_UT_compile;
975-
}
976-
977-
if (header.IsTypeUnit()) {
978-
header.m_type_hash = data.GetU64(offset_ptr);
979-
header.m_type_offset = data.GetDWARFOffset(offset_ptr);
980-
}
981-
982-
bool length_OK = data.ValidOffset(header.GetNextUnitOffset() - 1);
983-
bool version_OK = SymbolFileDWARF::SupportedVersion(header.m_version);
984-
bool addr_size_OK = (header.m_addr_size == 2) || (header.m_addr_size == 4) ||
985-
(header.m_addr_size == 8);
986-
bool type_offset_OK =
987-
!header.IsTypeUnit() || (header.m_type_offset <= header.GetLength());
988-
989-
if (!length_OK)
990-
return llvm::make_error<llvm::object::GenericBinaryError>(
991-
"Invalid unit length");
992-
if (!version_OK)
993-
return llvm::make_error<llvm::object::GenericBinaryError>(
994-
"Unsupported unit version");
995-
if (!addr_size_OK)
996-
return llvm::make_error<llvm::object::GenericBinaryError>(
997-
"Invalid unit address size");
998-
if (!type_offset_OK)
999-
return llvm::make_error<llvm::object::GenericBinaryError>(
1000-
"Type offset out of range");
1001-
1002-
return header;
1003-
}
1004-
1005927
llvm::Expected<DWARFUnitSP>
1006928
DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
1007929
const DWARFDataExtractor &debug_info,
1008930
DIERef::Section section, lldb::offset_t *offset_ptr) {
1009931
assert(debug_info.ValidOffset(*offset_ptr));
1010932

1011933
DWARFContext &context = dwarf.GetDWARFContext();
1012-
auto expected_header =
1013-
DWARFUnitHeader::extract(debug_info, section, context, offset_ptr);
1014-
if (!expected_header)
1015-
return expected_header.takeError();
934+
935+
// FIXME: Either properly map between DIERef::Section and
936+
// llvm::DWARFSectionKind or switch to llvm's definition entirely.
937+
llvm::DWARFSectionKind section_kind_llvm =
938+
section == DIERef::Section::DebugInfo
939+
? llvm::DWARFSectionKind::DW_SECT_INFO
940+
: llvm::DWARFSectionKind::DW_SECT_EXT_TYPES;
941+
942+
llvm::DWARFDataExtractor debug_info_llvm = debug_info.GetAsLLVMDWARF();
943+
llvm::DWARFUnitHeader header;
944+
if (llvm::Error extract_err = header.extract(
945+
context.GetAsLLVM(), debug_info_llvm, offset_ptr, section_kind_llvm))
946+
return std::move(extract_err);
1016947

1017948
if (context.isDwo()) {
1018949
const llvm::DWARFUnitIndex::Entry *entry = nullptr;
1019-
const llvm::DWARFUnitIndex &index = expected_header->IsTypeUnit()
950+
const llvm::DWARFUnitIndex &index = header.isTypeUnit()
1020951
? context.GetAsLLVM().getTUIndex()
1021952
: context.GetAsLLVM().getCUIndex();
1022953
if (index) {
1023-
if (expected_header->IsTypeUnit())
1024-
entry = index.getFromHash(expected_header->GetTypeHash());
1025-
else if (auto dwo_id = expected_header->GetDWOId())
954+
if (header.isTypeUnit())
955+
entry = index.getFromHash(header.getTypeHash());
956+
else if (auto dwo_id = header.getDWOId())
1026957
entry = index.getFromHash(*dwo_id);
1027958
}
1028959
if (!entry)
1029-
entry = index.getFromOffset(expected_header->GetOffset());
960+
entry = index.getFromOffset(header.getOffset());
1030961
if (entry)
1031-
if (llvm::Error err = expected_header->ApplyIndexEntry(entry))
962+
if (llvm::Error err = header.applyIndexEntry(entry))
1032963
return std::move(err);
1033964
}
1034965

@@ -1039,13 +970,13 @@ DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
1039970

1040971
bool abbr_offset_OK =
1041972
dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
1042-
expected_header->GetAbbrOffset());
973+
header.getAbbrOffset());
1043974
if (!abbr_offset_OK)
1044975
return llvm::make_error<llvm::object::GenericBinaryError>(
1045976
"Abbreviation offset for unit is not valid");
1046977

1047978
llvm::Expected<const llvm::DWARFAbbreviationDeclarationSet *> abbrevs_or_err =
1048-
abbr->getAbbreviationDeclarationSet(expected_header->GetAbbrOffset());
979+
abbr->getAbbreviationDeclarationSet(header.getAbbrOffset());
1049980
if (!abbrevs_or_err)
1050981
return abbrevs_or_err.takeError();
1051982

@@ -1055,11 +986,11 @@ DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
1055986
"No abbrev exists at the specified offset.");
1056987

1057988
bool is_dwo = dwarf.GetDWARFContext().isDwo();
1058-
if (expected_header->IsTypeUnit())
1059-
return DWARFUnitSP(new DWARFTypeUnit(dwarf, uid, *expected_header, *abbrevs,
1060-
section, is_dwo));
1061-
return DWARFUnitSP(new DWARFCompileUnit(dwarf, uid, *expected_header,
1062-
*abbrevs, section, is_dwo));
989+
if (header.isTypeUnit())
990+
return DWARFUnitSP(
991+
new DWARFTypeUnit(dwarf, uid, header, *abbrevs, section, is_dwo));
992+
return DWARFUnitSP(
993+
new DWARFCompileUnit(dwarf, uid, header, *abbrevs, section, is_dwo));
1063994
}
1064995

1065996
const lldb_private::DWARFDataExtractor &DWARFUnit::GetData() const {
@@ -1069,7 +1000,7 @@ const lldb_private::DWARFDataExtractor &DWARFUnit::GetData() const {
10691000
}
10701001

10711002
uint32_t DWARFUnit::GetHeaderByteSize() const {
1072-
switch (m_header.GetUnitType()) {
1003+
switch (m_header.getUnitType()) {
10731004
case llvm::dwarf::DW_UT_compile:
10741005
case llvm::dwarf::DW_UT_partial:
10751006
return GetVersion() < 5 ? 11 : 12;
@@ -1106,7 +1037,7 @@ DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) {
11061037
llvm::DWARFDataExtractor data = GetRnglistData().GetAsLLVMDWARF();
11071038

11081039
// As DW_AT_rnglists_base may be missing we need to call setAddressSize.
1109-
data.setAddressSize(m_header.GetAddressByteSize());
1040+
data.setAddressSize(m_header.getAddressByteSize());
11101041
auto range_list_or_error = GetRnglistTable()->findList(data, offset);
11111042
if (!range_list_or_error)
11121043
return range_list_or_error.takeError();

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,54 +38,6 @@ enum DWARFProducer {
3838
eProducerOther
3939
};
4040

41-
/// Base class describing the header of any kind of "unit." Some information
42-
/// is specific to certain unit types. We separate this class out so we can
43-
/// parse the header before deciding what specific kind of unit to construct.
44-
class DWARFUnitHeader {
45-
dw_offset_t m_offset = 0;
46-
dw_offset_t m_length = 0;
47-
uint16_t m_version = 0;
48-
dw_offset_t m_abbr_offset = 0;
49-
50-
const llvm::DWARFUnitIndex::Entry *m_index_entry = nullptr;
51-
52-
uint8_t m_unit_type = 0;
53-
uint8_t m_addr_size = 0;
54-
55-
uint64_t m_type_hash = 0;
56-
uint32_t m_type_offset = 0;
57-
58-
std::optional<uint64_t> m_dwo_id;
59-
60-
DWARFUnitHeader() = default;
61-
62-
public:
63-
dw_offset_t GetOffset() const { return m_offset; }
64-
uint16_t GetVersion() const { return m_version; }
65-
uint16_t GetAddressByteSize() const { return m_addr_size; }
66-
dw_offset_t GetLength() const { return m_length; }
67-
dw_offset_t GetAbbrOffset() const { return m_abbr_offset; }
68-
uint8_t GetUnitType() const { return m_unit_type; }
69-
const llvm::DWARFUnitIndex::Entry *GetIndexEntry() const {
70-
return m_index_entry;
71-
}
72-
uint64_t GetTypeHash() const { return m_type_hash; }
73-
dw_offset_t GetTypeOffset() const { return m_type_offset; }
74-
std::optional<uint64_t> GetDWOId() const { return m_dwo_id; }
75-
bool IsTypeUnit() const {
76-
return m_unit_type == llvm::dwarf::DW_UT_type ||
77-
m_unit_type == llvm::dwarf::DW_UT_split_type;
78-
}
79-
dw_offset_t GetNextUnitOffset() const { return m_offset + m_length + 4; }
80-
81-
llvm::Error ApplyIndexEntry(const llvm::DWARFUnitIndex::Entry *index_entry);
82-
83-
static llvm::Expected<DWARFUnitHeader> extract(const DWARFDataExtractor &data,
84-
DIERef::Section section,
85-
DWARFContext &dwarf_context,
86-
lldb::offset_t *offset_ptr);
87-
};
88-
8941
class DWARFUnit : public UserID {
9042
using die_iterator_range =
9143
llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
@@ -105,7 +57,7 @@ class DWARFUnit : public UserID {
10557
/// the DWO ID in the compile unit header and we sometimes only want to access
10658
/// this cheap value without causing the more expensive attribute fetches that
10759
/// GetDWOId() uses.
108-
std::optional<uint64_t> GetHeaderDWOId() { return m_header.GetDWOId(); }
60+
std::optional<uint64_t> GetHeaderDWOId() { return m_header.getDWOId(); }
10961
void ExtractUnitDIEIfNeeded();
11062
void ExtractUnitDIENoDwoIfNeeded();
11163
void ExtractDIEsIfNeeded();
@@ -143,7 +95,7 @@ class DWARFUnit : public UserID {
14395
uint32_t GetHeaderByteSize() const;
14496

14597
// Offset of the initial length field.
146-
dw_offset_t GetOffset() const { return m_header.GetOffset(); }
98+
dw_offset_t GetOffset() const { return m_header.getOffset(); }
14799
/// Get the size in bytes of the length field in the header.
148100
///
149101
/// In DWARF32 this is just 4 bytes
@@ -159,15 +111,15 @@ class DWARFUnit : public UserID {
159111
dw_offset_t GetFirstDIEOffset() const {
160112
return GetOffset() + GetHeaderByteSize();
161113
}
162-
dw_offset_t GetNextUnitOffset() const { return m_header.GetNextUnitOffset(); }
114+
dw_offset_t GetNextUnitOffset() const { return m_header.getNextUnitOffset(); }
163115
// Size of the CU data (without initial length and without header).
164116
size_t GetDebugInfoSize() const;
165117
// Size of the CU data incl. header but without initial length.
166-
dw_offset_t GetLength() const { return m_header.GetLength(); }
167-
uint16_t GetVersion() const { return m_header.GetVersion(); }
118+
dw_offset_t GetLength() const { return m_header.getLength(); }
119+
uint16_t GetVersion() const { return m_header.getVersion(); }
168120
const llvm::DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
169121
dw_offset_t GetAbbrevOffset() const;
170-
uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); }
122+
uint8_t GetAddressByteSize() const { return m_header.getAddressByteSize(); }
171123
dw_addr_t GetAddrBase() const { return m_addr_base.value_or(0); }
172124
dw_addr_t GetBaseAddress() const { return m_base_addr; }
173125
dw_offset_t GetLineTableOffset();
@@ -250,8 +202,8 @@ class DWARFUnit : public UserID {
250202

251203
DIERef::Section GetDebugSection() const { return m_section; }
252204

253-
uint8_t GetUnitType() const { return m_header.GetUnitType(); }
254-
bool IsTypeUnit() const { return m_header.IsTypeUnit(); }
205+
uint8_t GetUnitType() const { return m_header.getUnitType(); }
206+
bool IsTypeUnit() const { return m_header.isTypeUnit(); }
255207
/// Note that this check only works for DWARF5+.
256208
bool IsSkeletonUnit() const {
257209
return GetUnitType() == llvm::dwarf::DW_UT_skeleton;
@@ -320,7 +272,7 @@ class DWARFUnit : public UserID {
320272

321273
protected:
322274
DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
323-
const DWARFUnitHeader &header,
275+
const llvm::DWARFUnitHeader &header,
324276
const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
325277
DIERef::Section section, bool is_dwo);
326278

@@ -352,7 +304,7 @@ class DWARFUnit : public UserID {
352304

353305
SymbolFileDWARF &m_dwarf;
354306
std::shared_ptr<DWARFUnit> m_dwo;
355-
DWARFUnitHeader m_header;
307+
llvm::DWARFUnitHeader m_header;
356308
const llvm::DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
357309
lldb_private::CompileUnit *m_lldb_cu = nullptr;
358310
// If this is a DWO file, we have a backlink to our skeleton compile unit.

0 commit comments

Comments
 (0)