diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp index 4fc3866a3b608f..7c253553d57b48 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp @@ -37,19 +37,29 @@ llvm::DenseSet DebugNamesDWARFIndex::GetUnits(const DebugNames &debug_names) { llvm::DenseSet result; for (const DebugNames::NameIndex &ni : debug_names) { - for (uint32_t cu = 0; cu < ni.getCUCount(); ++cu) + const uint32_t num_cus = ni.getCUCount(); + for (uint32_t cu = 0; cu < num_cus; ++cu) result.insert(ni.getCUOffset(cu)); + const uint32_t num_tus = ni.getLocalTUCount(); + for (uint32_t tu = 0; tu < num_tus; ++tu) + result.insert(ni.getLocalTUOffset(tu)); } return result; } std::optional DebugNamesDWARFIndex::ToDIERef(const DebugNames::Entry &entry) { - std::optional cu_offset = entry.getCUOffset(); - if (!cu_offset) - return std::nullopt; + // Look for a DWARF unit offset (CU offset or local TU offset) as they are + // both offsets into the .debug_info section. + std::optional unit_offset = entry.getCUOffset(); + if (!unit_offset) { + unit_offset = entry.getLocalTUOffset(); + if (!unit_offset) + return std::nullopt; + } - DWARFUnit *cu = m_debug_info.GetUnitAtOffset(DIERef::Section::DebugInfo, *cu_offset); + DWARFUnit *cu = + m_debug_info.GetUnitAtOffset(DIERef::Section::DebugInfo, *unit_offset); if (!cu) return std::nullopt; diff --git a/lldb/test/Shell/SymbolFile/DWARF/x86/debug-types-debug-names.cpp b/lldb/test/Shell/SymbolFile/DWARF/x86/debug-types-debug-names.cpp new file mode 100644 index 00000000000000..2b7a928c89a8f7 --- /dev/null +++ b/lldb/test/Shell/SymbolFile/DWARF/x86/debug-types-debug-names.cpp @@ -0,0 +1,48 @@ +// Test that we can use .debug_names to lookup a type that is only referenced +// from within a type unit. In the code below the type named "stype" is only +// referenced within the type unit itself and when we enable .debug_names, we +// expect the have an entry for this and to be able to find this type when +// we do a lookup. + +// REQUIRES: lld + +// RUN: %clang %s -target x86_64-pc-linux -gdwarf-5 -fdebug-types-section \ +// RUN: -gpubnames -fno-limit-debug-info -c -o %t.o +// RUN: ld.lld %t.o -o %t +// RUN: %lldb %t -o "type lookup stype" -b | FileCheck %s --check-prefix=BASE +// RUN: %lldb %t -o "type lookup bar::stype" -b | FileCheck %s --check-prefix=PART +// RUN: %lldb %t -o "type lookup foo::bar::stype" -b | FileCheck %s --check-prefix=FULL + +// BASE: (lldb) type lookup stype +// BASE-NEXT: int + +// PART: (lldb) type lookup bar::stype +// PART-NEXT: int + +// FULL: (lldb) type lookup foo::bar::stype +// FULL-NEXT: int + +namespace foo { +class bar { +public: + typedef unsigned utype; + // This type is only referenced from within the type unit and we need to + // make sure we can find it with the new type unit support in .debug_names. + typedef int stype; + +private: + utype m_unsigned; + +public: + bar(utype u) : m_unsigned(u) {} + + utype get() const { return m_unsigned; } + void set(utype u) { m_unsigned = u; } + stype gets() const { return (stype)m_unsigned; } +}; +} // namespace foo + +int main() { + foo::bar b(12); + return 0; +} diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h index 1ba555a061904c..b89536bc0c7230 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h @@ -56,6 +56,14 @@ class DWARFAcceleratorTable { /// recorded in this Accelerator Entry. virtual std::optional getCUOffset() const = 0; + /// Returns the Offset of the Type Unit associated with this + /// Accelerator Entry or std::nullopt if the Type Unit offset is not + /// recorded in this Accelerator Entry. + virtual std::optional getLocalTUOffset() const { + // Default return for accelerator tables that don't support type units. + return std::nullopt; + } + /// Returns the Tag of the Debug Info Entry associated with this /// Accelerator Entry or std::nullopt if the Tag is not recorded in this /// Accelerator Entry. @@ -424,6 +432,7 @@ class DWARFDebugNames : public DWARFAcceleratorTable { public: std::optional getCUOffset() const override; + std::optional getLocalTUOffset() const override; std::optional getTag() const override { return tag(); } /// Returns the Index into the Compilation Unit list of the owning Name @@ -433,9 +442,17 @@ class DWARFDebugNames : public DWARFAcceleratorTable { /// which will handle that check itself). Note that entries in NameIndexes /// which index just a single Compilation Unit are implicitly associated /// with that unit, so this function will return 0 even without an explicit - /// DW_IDX_compile_unit attribute. + /// DW_IDX_compile_unit attribute, unless there is a DW_IDX_type_unit + /// attribute. std::optional getCUIndex() const; + /// Returns the Index into the Local Type Unit list of the owning Name + /// Index or std::nullopt if this Accelerator Entry does not have an + /// associated Type Unit. It is up to the user to verify that the + /// returned Index is valid in the owning NameIndex (or use + /// getLocalTUOffset(), which will handle that check itself). + std::optional getLocalTUIndex() const; + /// .debug_names-specific getter, which always succeeds (DWARF v5 index /// entries always have a tag). dwarf::Tag tag() const { return Abbr->Tag; } diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp index 8302cbbf231aed..0f9c8ef485d456 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp @@ -621,7 +621,10 @@ std::optional DWARFDebugNames::Entry::getCUIndex() const { if (std::optional Off = lookup(dwarf::DW_IDX_compile_unit)) return Off->getAsUnsignedConstant(); // In a per-CU index, the entries without a DW_IDX_compile_unit attribute - // implicitly refer to the single CU. + // implicitly refer to the single CU, but only if we don't have a + // DW_IDX_type_unit. + if (lookup(dwarf::DW_IDX_type_unit).has_value()) + return std::nullopt; if (NameIdx->getCUCount() == 1) return 0; return std::nullopt; @@ -634,6 +637,19 @@ std::optional DWARFDebugNames::Entry::getCUOffset() const { return NameIdx->getCUOffset(*Index); } +std::optional DWARFDebugNames::Entry::getLocalTUOffset() const { + std::optional Index = getLocalTUIndex(); + if (!Index || *Index >= NameIdx->getLocalTUCount()) + return std::nullopt; + return NameIdx->getLocalTUOffset(*Index); +} + +std::optional DWARFDebugNames::Entry::getLocalTUIndex() const { + if (std::optional Off = lookup(dwarf::DW_IDX_type_unit)) + return Off->getAsUnsignedConstant(); + return std::nullopt; +} + void DWARFDebugNames::Entry::dump(ScopedPrinter &W) const { W.startLine() << formatv("Abbrev: {0:x}\n", Abbr->Code); W.startLine() << formatv("Tag: {0}\n", Abbr->Tag);