Skip to content

Commit dd76375

Browse files
committed
[lldb][NFCI] Apply IndexEntry to DWARFUnitHeader outside of extraction
I plan on replacing LLDB's DWARFUnitHeader implementation with LLVM's. LLVM's DWARFUnitHeader::extract applies the DWARFUnitIndex::Entry to a given DWARFUnitHeader outside of the extraction because the index entry is only relevant to one place where we may parse DWARFUnitHeaders (specifically when we're creating a DWARFUnit in a DWO context). To ease the transition, I've reshaped LLDB's implementation to look closer to LLVM's. Reviewed By: aprantl, fdeazeve Differential Revision: https://reviews.llvm.org/D151919
1 parent 6f43d28 commit dd76375

File tree

2 files changed

+54
-43
lines changed

2 files changed

+54
-43
lines changed

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

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -877,11 +877,37 @@ const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
877877
return *m_func_aranges_up;
878878
}
879879

880-
llvm::Expected<DWARFUnitHeader>
881-
DWARFUnitHeader::extract(const DWARFDataExtractor &data,
882-
DIERef::Section section,
883-
lldb_private::DWARFContext &context,
884-
lldb::offset_t *offset_ptr) {
880+
llvm::Error DWARFUnitHeader::ApplyIndexEntry(
881+
const llvm::DWARFUnitIndex::Entry *index_entry) {
882+
// We should only be calling this function when the index entry is not set and
883+
// we have a valid one to set it to.
884+
assert(index_entry);
885+
assert(!m_index_entry);
886+
887+
if (m_abbr_offset)
888+
return llvm::createStringError(
889+
llvm::inconvertibleErrorCode(),
890+
"Package unit with a non-zero abbreviation offset");
891+
892+
auto *unit_contrib = index_entry->getContribution();
893+
if (!unit_contrib || unit_contrib->getLength32() != m_length + 4)
894+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
895+
"Inconsistent DWARF package unit index");
896+
897+
auto *abbr_entry = index_entry->getContribution(llvm::DW_SECT_ABBREV);
898+
if (!abbr_entry)
899+
return llvm::createStringError(
900+
llvm::inconvertibleErrorCode(),
901+
"DWARF package index missing abbreviation column");
902+
903+
m_abbr_offset = abbr_entry->getOffset();
904+
m_index_entry = index_entry;
905+
return llvm::Error::success();
906+
}
907+
908+
llvm::Expected<DWARFUnitHeader> DWARFUnitHeader::extract(
909+
const DWARFDataExtractor &data, DIERef::Section section,
910+
lldb_private::DWARFContext &context, lldb::offset_t *offset_ptr) {
885911
DWARFUnitHeader header;
886912
header.m_offset = *offset_ptr;
887913
header.m_length = data.GetDWARFInitialLength(offset_ptr);
@@ -905,42 +931,6 @@ DWARFUnitHeader::extract(const DWARFDataExtractor &data,
905931
header.m_type_offset = data.GetDWARFOffset(offset_ptr);
906932
}
907933

908-
if (context.isDwo()) {
909-
const llvm::DWARFUnitIndex *Index;
910-
if (header.IsTypeUnit()) {
911-
Index = &context.GetAsLLVM().getTUIndex();
912-
if (*Index)
913-
header.m_index_entry = Index->getFromHash(header.m_type_hash);
914-
} else {
915-
Index = &context.GetAsLLVM().getCUIndex();
916-
if (*Index && header.m_version >= 5 && header.m_dwo_id)
917-
header.m_index_entry = Index->getFromHash(*header.m_dwo_id);
918-
}
919-
if (!header.m_index_entry)
920-
header.m_index_entry = Index->getFromOffset(header.m_offset);
921-
}
922-
923-
if (header.m_index_entry) {
924-
if (header.m_abbr_offset) {
925-
return llvm::createStringError(
926-
llvm::inconvertibleErrorCode(),
927-
"Package unit with a non-zero abbreviation offset");
928-
}
929-
auto *unit_contrib = header.m_index_entry->getContribution();
930-
if (!unit_contrib || unit_contrib->getLength32() != header.m_length + 4) {
931-
return llvm::createStringError(llvm::inconvertibleErrorCode(),
932-
"Inconsistent DWARF package unit index");
933-
}
934-
auto *abbr_entry =
935-
header.m_index_entry->getContribution(llvm::DW_SECT_ABBREV);
936-
if (!abbr_entry) {
937-
return llvm::createStringError(
938-
llvm::inconvertibleErrorCode(),
939-
"DWARF package index missing abbreviation column");
940-
}
941-
header.m_abbr_offset = abbr_entry->getOffset();
942-
}
943-
944934
bool length_OK = data.ValidOffset(header.GetNextUnitOffset() - 1);
945935
bool version_OK = SymbolFileDWARF::SupportedVersion(header.m_version);
946936
bool addr_size_OK = (header.m_addr_size == 2) || (header.m_addr_size == 4) ||
@@ -970,11 +960,30 @@ DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
970960
DIERef::Section section, lldb::offset_t *offset_ptr) {
971961
assert(debug_info.ValidOffset(*offset_ptr));
972962

973-
auto expected_header = DWARFUnitHeader::extract(
974-
debug_info, section, dwarf.GetDWARFContext(), offset_ptr);
963+
DWARFContext &context = dwarf.GetDWARFContext();
964+
auto expected_header =
965+
DWARFUnitHeader::extract(debug_info, section, context, offset_ptr);
975966
if (!expected_header)
976967
return expected_header.takeError();
977968

969+
if (context.isDwo()) {
970+
const llvm::DWARFUnitIndex::Entry *entry = nullptr;
971+
const llvm::DWARFUnitIndex &index = expected_header->IsTypeUnit()
972+
? context.GetAsLLVM().getTUIndex()
973+
: context.GetAsLLVM().getCUIndex();
974+
if (index) {
975+
if (expected_header->IsTypeUnit())
976+
entry = index.getFromHash(expected_header->GetTypeHash());
977+
else if (auto dwo_id = expected_header->GetDWOId())
978+
entry = index.getFromHash(*dwo_id);
979+
}
980+
if (!entry)
981+
entry = index.getFromOffset(expected_header->GetOffset());
982+
if (entry)
983+
if (llvm::Error err = expected_header->ApplyIndexEntry(entry))
984+
return err;
985+
}
986+
978987
const llvm::DWARFDebugAbbrev *abbr = dwarf.DebugAbbrev();
979988
if (!abbr)
980989
return llvm::make_error<llvm::object::GenericBinaryError>(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class DWARFUnitHeader {
7676
}
7777
uint32_t GetNextUnitOffset() const { return m_offset + m_length + 4; }
7878

79+
llvm::Error ApplyIndexEntry(const llvm::DWARFUnitIndex::Entry *index_entry);
80+
7981
static llvm::Expected<DWARFUnitHeader>
8082
extract(const lldb_private::DWARFDataExtractor &data, DIERef::Section section,
8183
lldb_private::DWARFContext &dwarf_context,

0 commit comments

Comments
 (0)