DWARF: Add ability to reference debug info coming from multiple sections

Summary:
This patch adds the ability to precisely address debug info in
situations when a single file can have more than one debug-info-bearing
sections (as is the case with type units in DWARF v4).

The changes here can be classified into roughly three categories:
- the code which addresses a debug info by offset gets an additional
  argument, which specifies the section one should look into.
- the DIERef class also gets an additional member variable specifying
  the section. This way, code dealing with DIERefs can know which
  section is the object referring to.
- the user_id_t encoding steals one bit from the dwarf_id field to store
  the section. This means the total number of separate object files
  (apple .o, or normal .dwo) is limited to 2 billion, but that is fine
  as it's not possible to hit that number without switching to DWARF64
  anyway.

This patch is functionally equivalent to (and inspired by) the two
patches (D61503 and D61504) by Jan Kratochvil, but there are differences
in the implementation:
- it uses an enum instead of a bool flag to differentiate the sections
- it increases the size of DIERef struct instead of reducing the amount
  of addressable debug info
- it sets up DWARFDebugInfo to store the units in a single vector
  instead of two. This sets us up for the future in which type units can
  also live in the debug_info section, and I believe it's cleaner
  because there's no need for unit index remapping

There are no tests with this patch as this is essentially NFC until
we start parsing type units from the debug_types section.

Reviewers: JDevlieghere, clayborg, aprantl

Subscribers: arphaman, jankratochvil, lldb-commits

Differential Revision: https://reviews.llvm.org/D61908

llvm-svn: 360872
This commit is contained in:
Pavel Labath 2019-05-16 11:07:58 +00:00
parent 472c6ef8b0
commit f4014e116e
18 changed files with 79 additions and 58 deletions

View File

@ -9,8 +9,8 @@
# RUN: ld.lld %t.o -o %t
# RUN: lldb-test symbols %t | FileCheck %s
# CHECK: Variable{0xffffffff0000001e}, name = "X"
# CHECK-SAME: type = {ffffffff00000033} 0x{{[0-9a-f]*}} (char [56])
# CHECK: Variable{0x7fffffff0000001e}, name = "X"
# CHECK-SAME: type = {7fffffff00000033} 0x{{[0-9a-f]*}} (char [56])
# Generated from "char X[47];"

View File

@ -4,7 +4,7 @@
# RUN: ld.lld -m elf_x86_64 %t.o -o %t
# RUN: lldb-test symbols %t | FileCheck %s
# CHECK: Variable{0xffffffff00000011}, name = "color"
# CHECK: Variable{0x7fffffff00000011}, name = "color"
# CHECK-SAME: location = DW_OP_addrx(0x0)
.text

View File

@ -13,12 +13,12 @@
#include "SymbolFileDWARF.h"
#include "SymbolFileDWARFDebugMap.h"
DIERef::DIERef(const DWARFFormValue &form_value)
: cu_offset(DW_INVALID_OFFSET), die_offset(DW_INVALID_OFFSET) {
DIERef::DIERef(const DWARFFormValue &form_value) {
if (form_value.IsValid()) {
DWARFDIE die = form_value.Reference();
die_offset = die.GetOffset();
if (die) {
section = die.GetCU()->GetDebugSection();
if (die.GetCU()->GetBaseObjOffset() != DW_INVALID_OFFSET)
cu_offset = die.GetCU()->GetBaseObjOffset();
else

View File

@ -16,9 +16,12 @@ class DWARFFormValue;
class SymbolFileDWARF;
struct DIERef {
enum Section : uint8_t { DebugInfo, DebugTypes };
DIERef() = default;
DIERef(dw_offset_t c, dw_offset_t d) : cu_offset(c), die_offset(d) {}
DIERef(Section s, dw_offset_t c, dw_offset_t d)
: section(s), cu_offset(c), die_offset(d) {}
explicit DIERef(const DWARFFormValue &form_value);
@ -30,6 +33,7 @@ struct DIERef {
return cu_offset != DW_INVALID_OFFSET || die_offset != DW_INVALID_OFFSET;
}
Section section = Section::DebugInfo;
dw_offset_t cu_offset = DW_INVALID_OFFSET;
dw_offset_t die_offset = DW_INVALID_OFFSET;
};

View File

@ -24,7 +24,7 @@ DIERef DWARFBaseDIE::GetDIERef() const {
dw_offset_t cu_offset = m_cu->GetOffset();
if (m_cu->GetBaseObjOffset() != DW_INVALID_OFFSET)
cu_offset = m_cu->GetBaseObjOffset();
return DIERef(cu_offset, m_die->GetOffset());
return DIERef(m_cu->GetDebugSection(), cu_offset, m_die->GetOffset());
}
dw_tag_t DWARFBaseDIE::Tag() const {

View File

@ -34,6 +34,10 @@ public:
/// Byte size of the compile unit header
uint32_t GetHeaderByteSize() const override;
DIERef::Section GetDebugSection() const override {
return DIERef::Section::DebugInfo;
}
private:
DWARFCompileUnit(SymbolFileDWARF *dwarf2Data, lldb::user_id_t uid);
DISALLOW_COPY_AND_ASSIGN(DWARFCompileUnit);

View File

@ -153,7 +153,8 @@ DWARFDIE::LookupDeepestBlock(lldb::addr_t file_addr) const {
return DWARFDIE(cu, block_die);
else
return DWARFDIE(dwarf->DebugInfo()->GetUnit(
DIERef(cu->GetOffset(), block_die->GetOffset())),
DIERef(cu->GetDebugSection(), cu->GetOffset(),
block_die->GetOffset())),
block_die);
}
}

View File

@ -116,26 +116,28 @@ DWARFUnit *DWARFDebugInfo::GetUnitAtIndex(user_id_t idx) {
return cu;
}
bool DWARFDebugInfo::OffsetLessThanUnitOffset(dw_offset_t offset,
const DWARFUnitSP &cu_sp) {
return offset < cu_sp->GetOffset();
}
uint32_t DWARFDebugInfo::FindUnitIndex(dw_offset_t offset) {
uint32_t DWARFDebugInfo::FindUnitIndex(DIERef::Section section,
dw_offset_t offset) {
ParseUnitHeadersIfNeeded();
// llvm::lower_bound is not used as for DIE offsets it would still return
// index +1 and GetOffset() returning index itself would be a special case.
auto pos = llvm::upper_bound(m_units, offset, OffsetLessThanUnitOffset);
auto pos = llvm::upper_bound(
m_units, std::make_pair(section, offset),
[](const std::pair<DIERef::Section, dw_offset_t> &lhs,
const DWARFUnitSP &rhs) {
return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset());
});
uint32_t idx = std::distance(m_units.begin(), pos);
if (idx == 0)
return DW_INVALID_OFFSET;
return idx - 1;
}
DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(dw_offset_t cu_offset,
DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(DIERef::Section section,
dw_offset_t cu_offset,
uint32_t *idx_ptr) {
uint32_t idx = FindUnitIndex(cu_offset);
uint32_t idx = FindUnitIndex(section, cu_offset);
DWARFUnit *result = GetUnitAtIndex(idx);
if (result && result->GetOffset() != cu_offset) {
result = nullptr;
@ -148,13 +150,15 @@ DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(dw_offset_t cu_offset,
DWARFUnit *DWARFDebugInfo::GetUnit(const DIERef &die_ref) {
if (die_ref.cu_offset == DW_INVALID_OFFSET)
return GetUnitContainingDIEOffset(die_ref.die_offset);
return GetUnitContainingDIEOffset(die_ref.section, die_ref.die_offset);
else
return GetUnitAtOffset(die_ref.cu_offset);
return GetUnitAtOffset(die_ref.section, die_ref.cu_offset);
}
DWARFUnit *DWARFDebugInfo::GetUnitContainingDIEOffset(dw_offset_t die_offset) {
uint32_t idx = FindUnitIndex(die_offset);
DWARFUnit *
DWARFDebugInfo::GetUnitContainingDIEOffset(DIERef::Section section,
dw_offset_t die_offset) {
uint32_t idx = FindUnitIndex(section, die_offset);
DWARFUnit *result = GetUnitAtIndex(idx);
if (result && !result->ContainsDIEOffset(die_offset))
return nullptr;
@ -162,8 +166,9 @@ DWARFUnit *DWARFDebugInfo::GetUnitContainingDIEOffset(dw_offset_t die_offset) {
}
DWARFDIE
DWARFDebugInfo::GetDIEForDIEOffset(dw_offset_t die_offset) {
DWARFUnit *cu = GetUnitContainingDIEOffset(die_offset);
DWARFDebugInfo::GetDIEForDIEOffset(DIERef::Section section,
dw_offset_t die_offset) {
DWARFUnit *cu = GetUnitContainingDIEOffset(section, die_offset);
if (cu)
return cu->GetDIE(die_offset);
return DWARFDIE();

View File

@ -41,10 +41,13 @@ public:
size_t GetNumUnits();
DWARFUnit *GetUnitAtIndex(lldb::user_id_t idx);
DWARFUnit *GetUnitAtOffset(dw_offset_t cu_offset, uint32_t *idx_ptr = NULL);
DWARFUnit *GetUnitContainingDIEOffset(dw_offset_t die_offset);
DWARFUnit *GetUnitAtOffset(DIERef::Section section,
dw_offset_t cu_offset, uint32_t *idx_ptr = NULL);
DWARFUnit *GetUnitContainingDIEOffset(DIERef::Section section,
dw_offset_t die_offset);
DWARFUnit *GetUnit(const DIERef &die_ref);
DWARFDIE GetDIEForDIEOffset(dw_offset_t die_offset);
DWARFDIE GetDIEForDIEOffset(DIERef::Section section,
dw_offset_t die_offset);
DWARFDIE GetDIE(const DIERef &die_ref);
enum {
@ -57,9 +60,6 @@ public:
llvm::Expected<DWARFDebugAranges &> GetCompileUnitAranges();
protected:
static bool OffsetLessThanUnitOffset(dw_offset_t offset,
const DWARFUnitSP &cu_sp);
typedef std::vector<DWARFUnitSP> UnitColl;
// Member variables
@ -74,7 +74,7 @@ private:
// accessors are called.
void ParseUnitHeadersIfNeeded();
uint32_t FindUnitIndex(dw_offset_t offset);
uint32_t FindUnitIndex(DIERef::Section section, dw_offset_t offset);
DISALLOW_COPY_AND_ASSIGN(DWARFDebugInfo);
};

View File

@ -567,7 +567,7 @@ DWARFDIE DWARFFormValue::Reference() const {
case DW_FORM_ref_addr: {
DWARFUnit *ref_cu =
m_unit->GetSymbolFileDWARF()->DebugInfo()->GetUnitContainingDIEOffset(
value);
DIERef::Section::DebugInfo, value);
if (!ref_cu) {
m_unit->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError(
"DW_FORM_ref_addr DIE reference 0x%" PRIx64 " has no matching CU",

View File

@ -167,6 +167,8 @@ public:
return die_iterator_range(m_die_array.begin(), m_die_array.end());
}
virtual DIERef::Section GetDebugSection() const = 0;
protected:
DWARFUnit(SymbolFileDWARF *dwarf, lldb::user_id_t uid);

View File

@ -55,7 +55,7 @@ DIERef DebugNamesDWARFIndex::ToDIERef(const DebugNames::Entry &entry) {
if (!cu_offset)
return DIERef();
DWARFUnit *cu = m_debug_info.GetUnitAtOffset(*cu_offset);
DWARFUnit *cu = m_debug_info.GetUnitAtOffset(DIERef::Section::DebugInfo, *cu_offset);
if (!cu)
return DIERef();
@ -66,7 +66,7 @@ DIERef DebugNamesDWARFIndex::ToDIERef(const DebugNames::Entry &entry) {
uint64_t die_bias = cu->GetDwoSymbolFile() ? 0 : *cu_offset;
if (llvm::Optional<uint64_t> die_offset = entry.getDIEUnitOffset())
return DIERef(*cu_offset, die_bias + *die_offset);
return DIERef(DIERef::Section::DebugInfo, *cu_offset, die_bias + *die_offset);
return DIERef();
}
@ -164,7 +164,8 @@ void DebugNamesDWARFIndex::GetCompleteObjCClass(ConstString class_name,
if (!ref)
continue;
DWARFUnit *cu = m_debug_info.GetUnitAtOffset(ref.cu_offset);
DWARFUnit *cu =
m_debug_info.GetUnitAtOffset(DIERef::Section::DebugInfo, ref.cu_offset);
if (!cu || !cu->Supports_DW_AT_APPLE_objc_complete_type()) {
incomplete_types.push_back(ref);
continue;

View File

@ -78,8 +78,7 @@ void DWARFMappedHash::ExtractClassOrStructDIEArray(
return;
} else {
// Put the one true definition as the first entry so it matches first
die_offsets.emplace(die_offsets.begin(), die_info_array[i].cu_offset,
die_info_array[i].offset);
die_offsets.emplace(die_offsets.begin(), die_info_array[i]);
}
} else {
die_offsets.emplace_back(die_info_array[i]);
@ -119,12 +118,12 @@ const char *DWARFMappedHash::GetAtomTypeName(uint16_t atom) {
}
DWARFMappedHash::DIEInfo::DIEInfo()
: cu_offset(DW_INVALID_OFFSET), offset(DW_INVALID_OFFSET), tag(0),
type_flags(0), qualified_name_hash(0) {}
: tag(0), type_flags(0), qualified_name_hash(0) {}
DWARFMappedHash::DIEInfo::DIEInfo(dw_offset_t c, dw_offset_t o, dw_tag_t t,
uint32_t f, uint32_t h)
: cu_offset(c), offset(o), tag(t), type_flags(f), qualified_name_hash(h) {}
: die_ref(DIERef::Section::DebugInfo, c, o), tag(t), type_flags(f),
qualified_name_hash(h) {}
DWARFMappedHash::Prologue::Prologue(dw_offset_t _die_base_offset)
: die_base_offset(_die_base_offset), atoms(), atom_mask(0),
@ -272,7 +271,7 @@ bool DWARFMappedHash::Header::Read(const lldb_private::DWARFDataExtractor &data,
switch (header_data.atoms[i].type) {
case eAtomTypeDIEOffset: // DIE offset, check form for encoding
hash_data.offset =
hash_data.die_ref.die_offset =
DWARFFormValue::IsDataForm(form_value.Form())
? form_value.Unsigned()
: form_value.Reference(header_data.die_base_offset);
@ -507,10 +506,10 @@ size_t DWARFMappedHash::MemoryTable::AppendAllDIEsInRange(
for (uint32_t i = 0; i < count; ++i) {
DIEInfo die_info;
if (m_header.Read(m_data, &hash_data_offset, die_info)) {
if (die_info.offset == 0)
if (die_info.die_ref.die_offset == 0)
done = true;
if (die_offset_start <= die_info.offset &&
die_info.offset < die_offset_end)
if (die_offset_start <= die_info.die_ref.die_offset &&
die_info.die_ref.die_offset < die_offset_end)
die_info_array.push_back(die_info);
}
}

View File

@ -48,8 +48,7 @@ public:
};
struct DIEInfo {
dw_offset_t cu_offset;
dw_offset_t offset; // The DIE offset
DIERef die_ref;
dw_tag_t tag;
uint32_t type_flags; // Any flags for this DIEInfo
uint32_t qualified_name_hash; // A 32 bit hash of the fully qualified name
@ -57,7 +56,7 @@ public:
DIEInfo();
DIEInfo(dw_offset_t c, dw_offset_t o, dw_tag_t t, uint32_t f, uint32_t h);
explicit operator DIERef() const { return {cu_offset, offset}; }
explicit operator DIERef() const { return die_ref; }
};
struct Atom {

View File

@ -245,7 +245,7 @@ void ManualDWARFIndex::IndexUnitImpl(
}
}
DIERef ref(cu_offset, die.GetOffset());
DIERef ref(unit.GetDebugSection(), cu_offset, die.GetOffset());
switch (tag) {
case DW_TAG_inlined_subroutine:
case DW_TAG_subprogram:

View File

@ -349,10 +349,10 @@ SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
}
SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile)
: SymbolFile(objfile), UserID(uint64_t(DW_INVALID_OFFSET)
<< 32), // Used by SymbolFileDWARFDebugMap to
// when this class parses .o files to
// contain the .o file index/ID
: SymbolFile(objfile),
UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
// when this class parses .o files to
// contain the .o file index/ID
m_debug_map_module_wp(), m_debug_map_symfile(NULL),
m_context(*objfile->GetModule()), m_data_debug_abbrev(),
m_data_debug_frame(), m_data_debug_info(), m_data_debug_line(),
@ -1257,9 +1257,11 @@ SymbolFileDWARF::DecodedUID SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) {
SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex(
debug_map->GetOSOIndexFromUserID(uid));
return {dwarf, {DW_INVALID_OFFSET, dw_offset_t(uid)}};
return {dwarf, {DIERef::Section::DebugInfo, DW_INVALID_OFFSET, dw_offset_t(uid)}};
}
uint32_t dwarf_id = uid >> 32;
DIERef::Section section =
uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo;
uint32_t dwarf_id = uid >> 32 & 0x7fffffff;
dw_offset_t die_offset = uid;
if (die_offset == DW_INVALID_OFFSET)
@ -1272,7 +1274,7 @@ SymbolFileDWARF::DecodedUID SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) {
dwarf = unit->GetDwoSymbolFile();
}
}
return {dwarf, {DW_INVALID_OFFSET, die_offset}};
return {dwarf, {section, DW_INVALID_OFFSET, die_offset}};
}
DWARFDIE
@ -1765,7 +1767,8 @@ uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
}
} else {
uint32_t cu_idx = DW_INVALID_INDEX;
DWARFUnit *dwarf_cu = debug_info->GetUnitAtOffset(cu_offset, &cu_idx);
DWARFUnit *dwarf_cu = debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo,
cu_offset, &cu_idx);
if (dwarf_cu) {
sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
if (sc.comp_unit) {

View File

@ -287,10 +287,13 @@ public:
DWARFDIE GetDIE(lldb::user_id_t uid);
lldb::user_id_t GetUID(const DWARFBaseDIE &die) {
return GetID() | die.GetOffset();
return GetUID(die.GetDIERef());
}
lldb::user_id_t GetUID(const DIERef &ref) { return GetID() | ref.die_offset; }
lldb::user_id_t GetUID(const DIERef &ref) {
return GetID() | ref.die_offset |
(lldb::user_id_t(ref.section == DIERef::Section::DebugTypes) << 63);
}
virtual std::unique_ptr<SymbolFileDWARFDwo>
GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,

View File

@ -160,5 +160,5 @@ DWARFDIE
SymbolFileDWARFDwo::GetDIE(const DIERef &die_ref) {
lldbassert(die_ref.cu_offset == m_base_dwarf_cu->GetOffset() ||
die_ref.cu_offset == DW_INVALID_OFFSET);
return DebugInfo()->GetDIEForDIEOffset(die_ref.die_offset);
return DebugInfo()->GetDIEForDIEOffset(die_ref.section, die_ref.die_offset);
}