Skip to content

Commit

Permalink
LibDebug: Don't assume compilation unit index == line program index
Browse files Browse the repository at this point in the history
Instead, use the `DW_AT_stmt_list` attribute of the compilation unit
entry to determine the corresponding line program.
  • Loading branch information
spholz authored and ADKaster committed May 7, 2024
1 parent 14ae040 commit 116f82d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
28 changes: 26 additions & 2 deletions Userland/Libraries/LibDebug/Dwarf/CompilationUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,46 @@

#include "CompilationUnit.h"
#include <AK/ByteReader.h>
#include <AK/MemoryStream.h>
#include <LibDebug/Dwarf/DIE.h>
#include <LibDebug/Dwarf/DwarfInfo.h>
#include <LibDebug/Dwarf/LineProgram.h>

namespace Debug::Dwarf {

CompilationUnit::CompilationUnit(DwarfInfo const& dwarf_info, u32 offset, CompilationUnitHeader const& header, NonnullOwnPtr<LineProgram>&& line_program)
CompilationUnit::CompilationUnit(DwarfInfo const& dwarf_info, u32 offset, CompilationUnitHeader const& header)
: m_dwarf_info(dwarf_info)
, m_offset(offset)
, m_header(header)
, m_abbreviations(dwarf_info, header.abbrev_offset())
, m_line_program(move(line_program))
{
VERIFY(header.version() < 5 || header.unit_type() == CompilationUnitType::Full);
}

ErrorOr<NonnullOwnPtr<CompilationUnit>> CompilationUnit::create(DwarfInfo const& dwarf_info, u32 offset, CompilationUnitHeader const& header, ReadonlyBytes debug_line_data)
{
auto compilation_unit = TRY(adopt_nonnull_own_or_enomem(new (nothrow) CompilationUnit(dwarf_info, offset, header)));
TRY(compilation_unit->populate_line_program(debug_line_data));
return compilation_unit;
}

ErrorOr<void> CompilationUnit::populate_line_program(ReadonlyBytes debug_line_data)
{
auto die = root_die();

auto res = TRY(die.get_attribute(Attribute::StmtList));
if (!res.has_value())
return EINVAL;
VERIFY(res->form() == AttributeDataForm::SecOffset);

FixedMemoryStream debug_line_stream { debug_line_data };
TRY(debug_line_stream.seek(res->as_unsigned()));

m_line_program = TRY(LineProgram::create(m_dwarf_info, debug_line_stream));

return {};
}

CompilationUnit::~CompilationUnit() = default;

DIE CompilationUnit::root_die() const
Expand Down
9 changes: 6 additions & 3 deletions Userland/Libraries/LibDebug/Dwarf/CompilationUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "AbbreviationsMap.h"
#include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/OwnPtr.h>
#include <AK/Types.h>

namespace Debug::Dwarf {
Expand All @@ -22,7 +22,7 @@ class CompilationUnit {
AK_MAKE_NONMOVABLE(CompilationUnit);

public:
CompilationUnit(DwarfInfo const& dwarf_info, u32 offset, CompilationUnitHeader const&, NonnullOwnPtr<LineProgram>&& line_program);
static ErrorOr<NonnullOwnPtr<CompilationUnit>> create(DwarfInfo const& dwarf_info, u32 offset, CompilationUnitHeader const&, ReadonlyBytes debug_line_data);
~CompilationUnit();

u32 offset() const { return m_offset; }
Expand All @@ -49,11 +49,14 @@ class CompilationUnit {
ErrorOr<u64> range_lists_base() const;

private:
CompilationUnit(DwarfInfo const& dwarf_info, u32 offset, CompilationUnitHeader const&);
ErrorOr<void> populate_line_program(ReadonlyBytes debug_line_data);

DwarfInfo const& m_dwarf_info;
u32 m_offset { 0 };
CompilationUnitHeader m_header;
AbbreviationsMap m_abbreviations;
NonnullOwnPtr<LineProgram> m_line_program;
OwnPtr<LineProgram> m_line_program;
mutable bool m_has_cached_base_address : 1 { false };
mutable bool m_has_cached_address_table_base : 1 { false };
mutable bool m_has_cached_string_offsets_base : 1 { false };
Expand Down
18 changes: 2 additions & 16 deletions Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ ErrorOr<void> DwarfInfo::populate_compilation_units()
return {};

FixedMemoryStream debug_info_stream { m_debug_info_data };
FixedMemoryStream line_info_stream { m_debug_line_data };

while (!debug_info_stream.is_eof()) {
auto unit_offset = TRY(debug_info_stream.tell());
Expand All @@ -59,23 +58,10 @@ ErrorOr<void> DwarfInfo::populate_compilation_units()

u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));

auto line_program = TRY(LineProgram::create(*this, line_info_stream));

// HACK: Clang generates line programs for embedded resource assembly files, but not compile units.
// Meaning that for graphical applications, some line info data would be unread, triggering the assertion below.
// As a fix, we don't create compilation units for line programs that come from resource files.
#if defined(AK_COMPILER_CLANG)
if (line_program->looks_like_embedded_resource()) {
TRY(debug_info_stream.seek(unit_offset));
} else
#endif
{
m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header, move(line_program)));
TRY(debug_info_stream.discard(length_after_header));
}
m_compilation_units.append(TRY(CompilationUnit::create(*this, unit_offset, compilation_unit_header, m_debug_line_data)));
TRY(debug_info_stream.discard(length_after_header));
}

VERIFY(line_info_stream.is_eof());
return {};
}

Expand Down

0 comments on commit 116f82d

Please sign in to comment.