2020-05-04 12:14:39 +03:00
|
|
|
/*
|
2021-06-19 11:50:24 +03:00
|
|
|
* Copyright (c) 2020-2021, Itamar S. <itamar8910@gmail.com>
|
2020-05-04 12:14:39 +03:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-04 12:14:39 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "DwarfInfo.h"
|
2021-09-28 13:35:28 +03:00
|
|
|
#include "AddressRanges.h"
|
2021-06-19 11:34:59 +03:00
|
|
|
#include "AttributeValue.h"
|
2021-06-19 11:50:24 +03:00
|
|
|
#include "CompilationUnit.h"
|
2020-05-04 12:14:39 +03:00
|
|
|
|
2022-01-27 13:22:45 +03:30
|
|
|
#include <AK/ByteReader.h>
|
2023-01-22 00:37:38 +01:00
|
|
|
#include <AK/LEB128.h>
|
2023-01-25 20:19:05 +01:00
|
|
|
#include <AK/MemoryStream.h>
|
2021-06-19 11:50:24 +03:00
|
|
|
#include <LibDebug/DebugInfo.h>
|
2020-08-05 10:55:36 +02:00
|
|
|
|
2020-08-25 04:33:07 +01:00
|
|
|
namespace Debug::Dwarf {
|
2020-05-04 12:14:39 +03:00
|
|
|
|
2021-06-19 15:33:03 +03:00
|
|
|
DwarfInfo::DwarfInfo(ELF::Image const& elf)
|
2020-05-04 12:14:39 +03:00
|
|
|
: m_elf(elf)
|
|
|
|
|
{
|
2021-05-30 23:44:02 -07:00
|
|
|
m_debug_info_data = section_data(".debug_info"sv);
|
|
|
|
|
m_abbreviation_data = section_data(".debug_abbrev"sv);
|
|
|
|
|
m_debug_strings_data = section_data(".debug_str"sv);
|
2021-06-18 15:25:27 +03:00
|
|
|
m_debug_line_data = section_data(".debug_line"sv);
|
2021-05-30 23:44:02 -07:00
|
|
|
m_debug_line_strings_data = section_data(".debug_line_str"sv);
|
2021-09-28 13:35:28 +03:00
|
|
|
m_debug_range_lists_data = section_data(".debug_rnglists"sv);
|
2021-10-09 17:38:26 +02:00
|
|
|
m_debug_str_offsets_data = section_data(".debug_str_offsets"sv);
|
|
|
|
|
m_debug_addr_data = section_data(".debug_addr"sv);
|
2021-12-05 08:45:05 +01:00
|
|
|
m_debug_ranges_data = section_data(".debug_ranges"sv);
|
2020-05-04 12:14:39 +03:00
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
populate_compilation_units().release_value_but_fixme_should_propagate_errors();
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
|
2022-09-14 10:39:32 +02:00
|
|
|
DwarfInfo::~DwarfInfo() = default;
|
|
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
ReadonlyBytes DwarfInfo::section_data(StringView section_name) const
|
2020-05-04 12:14:39 +03:00
|
|
|
{
|
2020-12-25 02:14:56 +01:00
|
|
|
auto section = m_elf.lookup_section(section_name);
|
2021-05-15 00:13:44 +02:00
|
|
|
if (!section.has_value())
|
2020-05-23 14:55:39 +03:00
|
|
|
return {};
|
2021-05-15 00:13:44 +02:00
|
|
|
return section->bytes();
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
ErrorOr<void> DwarfInfo::populate_compilation_units()
|
2020-05-04 12:14:39 +03:00
|
|
|
{
|
2020-12-19 13:14:00 +01:00
|
|
|
if (!m_debug_info_data.data())
|
2023-01-22 00:32:08 +01:00
|
|
|
return {};
|
2020-08-05 10:55:36 +02:00
|
|
|
|
2023-01-30 11:05:43 +01:00
|
|
|
FixedMemoryStream debug_info_stream { m_debug_info_data };
|
|
|
|
|
FixedMemoryStream line_info_stream { m_debug_line_data };
|
2021-06-18 15:25:27 +03:00
|
|
|
|
2023-01-30 11:05:43 +01:00
|
|
|
while (!debug_info_stream.is_eof()) {
|
|
|
|
|
auto unit_offset = TRY(debug_info_stream.tell());
|
2020-05-04 12:14:39 +03:00
|
|
|
|
2023-01-30 11:05:43 +01:00
|
|
|
auto compilation_unit_header = TRY(debug_info_stream.read_value<CompilationUnitHeader>());
|
2021-04-28 22:13:58 +02:00
|
|
|
VERIFY(compilation_unit_header.common.version <= 5);
|
2021-07-13 18:16:36 +02:00
|
|
|
VERIFY(compilation_unit_header.address_size() == sizeof(FlatPtr));
|
2020-05-04 12:14:39 +03:00
|
|
|
|
2021-04-28 22:13:58 +02:00
|
|
|
u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
|
2021-06-18 15:25:27 +03:00
|
|
|
|
2023-01-30 11:05:43 +01:00
|
|
|
auto line_program = make<LineProgram>(*this, line_info_stream);
|
2021-06-18 15:25:27 +03:00
|
|
|
|
2021-10-09 17:47:43 +02:00
|
|
|
// 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.
|
2022-10-04 15:04:13 -04:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2023-01-12 14:07:27 +01:00
|
|
|
if (line_program->looks_like_embedded_resource()) {
|
2023-01-30 11:05:43 +01:00
|
|
|
TRY(debug_info_stream.seek(unit_offset));
|
2021-10-09 17:47:43 +02:00
|
|
|
} else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header, move(line_program)));
|
2023-01-30 11:05:43 +01:00
|
|
|
TRY(debug_info_stream.discard(length_after_header));
|
2021-10-09 17:47:43 +02:00
|
|
|
}
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
2021-06-18 15:25:27 +03:00
|
|
|
|
2023-01-30 11:05:43 +01:00
|
|
|
VERIFY(line_info_stream.is_eof());
|
2023-01-22 00:32:08 +01:00
|
|
|
return {};
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
ErrorOr<AttributeValue> DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
|
2023-01-22 05:09:11 +01:00
|
|
|
SeekableStream& debug_info_stream, CompilationUnit const* unit) const
|
2021-04-28 20:12:21 +02:00
|
|
|
{
|
|
|
|
|
AttributeValue value;
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_form = form;
|
|
|
|
|
value.m_compilation_unit = unit;
|
2021-04-28 20:12:21 +02:00
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
auto assign_raw_bytes_value = [&](size_t length) -> ErrorOr<void> {
|
2023-01-22 00:37:38 +01:00
|
|
|
value.m_data.as_raw_bytes = { debug_info_data().offset_pointer(TRY(debug_info_stream.tell())), length };
|
|
|
|
|
TRY(debug_info_stream.discard(length));
|
2023-01-22 00:32:08 +01:00
|
|
|
return {};
|
2021-04-28 20:12:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
switch (form) {
|
|
|
|
|
case AttributeDataForm::StringPointer: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto offset = TRY(debug_info_stream.read_value<u32>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::String;
|
2021-04-28 20:12:21 +02:00
|
|
|
|
|
|
|
|
auto strings_data = debug_strings_data();
|
2022-04-01 20:58:27 +03:00
|
|
|
value.m_data.as_string = bit_cast<char const*>(strings_data.offset_pointer(offset));
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Data1: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto data = TRY(debug_info_stream.read_value<u8>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::UnsignedNumber;
|
|
|
|
|
value.m_data.as_unsigned = data;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Data2: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto data = TRY(debug_info_stream.read_value<u16>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::UnsignedNumber;
|
|
|
|
|
value.m_data.as_signed = data;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Addr: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto address = TRY(debug_info_stream.read_value<FlatPtr>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::Address;
|
|
|
|
|
value.m_data.as_addr = address;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::SData: {
|
2023-01-30 00:02:38 +01:00
|
|
|
i64 data = TRY(debug_info_stream.read_value<LEB128<i64>>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::SignedNumber;
|
|
|
|
|
value.m_data.as_signed = data;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-28 22:15:51 +02:00
|
|
|
case AttributeDataForm::UData: {
|
2023-01-30 00:02:38 +01:00
|
|
|
u64 data = TRY(debug_info_stream.read_value<LEB128<u64>>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::UnsignedNumber;
|
|
|
|
|
value.m_data.as_unsigned = data;
|
2021-04-28 22:15:51 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-28 20:12:21 +02:00
|
|
|
case AttributeDataForm::SecOffset: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto data = TRY(debug_info_stream.read_value<u32>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::SecOffset;
|
|
|
|
|
value.m_data.as_unsigned = data;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Data4: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto data = TRY(debug_info_stream.read_value<u32>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::UnsignedNumber;
|
|
|
|
|
value.m_data.as_unsigned = data;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Data8: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto data = TRY(debug_info_stream.read_value<u64>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::UnsignedNumber;
|
|
|
|
|
value.m_data.as_unsigned = data;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-10-09 17:44:35 +02:00
|
|
|
case AttributeDataForm::Data16: {
|
|
|
|
|
value.m_type = AttributeValue::Type::RawBytes;
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(assign_raw_bytes_value(16));
|
2021-10-09 17:44:35 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-28 20:12:21 +02:00
|
|
|
case AttributeDataForm::Ref4: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto data = TRY(debug_info_stream.read_value<u32>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::DieReference;
|
2021-04-28 20:12:21 +02:00
|
|
|
VERIFY(unit);
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_data.as_unsigned = data + unit->offset();
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::FlagPresent: {
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::Boolean;
|
|
|
|
|
value.m_data.as_bool = true;
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::ExprLoc: {
|
2023-01-30 00:02:38 +01:00
|
|
|
size_t length = TRY(debug_info_stream.read_value<LEB128<size_t>>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::DwarfExpression;
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(assign_raw_bytes_value(length));
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::String: {
|
2023-01-22 00:37:38 +01:00
|
|
|
u32 str_offset = TRY(debug_info_stream.tell());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::String;
|
2022-04-01 20:58:27 +03:00
|
|
|
value.m_data.as_string = bit_cast<char const*>(debug_info_data().offset_pointer(str_offset));
|
2023-01-22 00:37:38 +01:00
|
|
|
TRY(debug_info_stream.discard(strlen(value.m_data.as_string) + 1));
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Block1: {
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::RawBytes;
|
2023-01-22 00:37:38 +01:00
|
|
|
auto length = TRY(debug_info_stream.read_value<u8>());
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(assign_raw_bytes_value(length));
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Block2: {
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::RawBytes;
|
2023-01-22 00:37:38 +01:00
|
|
|
auto length = TRY(debug_info_stream.read_value<u16>());
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(assign_raw_bytes_value(length));
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Block4: {
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::RawBytes;
|
2023-01-22 00:37:38 +01:00
|
|
|
auto length = TRY(debug_info_stream.read_value<u32>());
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(assign_raw_bytes_value(length));
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::Block: {
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::RawBytes;
|
2023-01-30 00:02:38 +01:00
|
|
|
size_t length = TRY(debug_info_stream.read_value<LEB128<size_t>>());
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(assign_raw_bytes_value(length));
|
2021-04-28 20:12:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-28 22:15:51 +02:00
|
|
|
case AttributeDataForm::LineStrP: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto offset = TRY(debug_info_stream.read_value<u32>());
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::String;
|
2021-04-28 22:15:51 +02:00
|
|
|
|
|
|
|
|
auto strings_data = debug_line_strings_data();
|
2022-04-01 20:58:27 +03:00
|
|
|
value.m_data.as_string = bit_cast<char const*>(strings_data.offset_pointer(offset));
|
2021-04-28 22:15:51 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-29 00:51:54 +02:00
|
|
|
case AttributeDataForm::ImplicitConst: {
|
|
|
|
|
/* Value is part of the abbreviation record. */
|
2021-10-09 16:58:48 +02:00
|
|
|
value.m_type = AttributeValue::Type::SignedNumber;
|
|
|
|
|
value.m_data.as_signed = implicit_const_value;
|
2021-04-29 00:51:54 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-10-09 17:38:26 +02:00
|
|
|
case AttributeDataForm::StrX1: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto index = TRY(debug_info_stream.read_value<u8>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::String;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::StrX2: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto index = TRY(debug_info_stream.read_value<u16>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::String;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::StrX4: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto index = TRY(debug_info_stream.read_value<u32>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::String;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::StrX: {
|
2023-01-30 00:02:38 +01:00
|
|
|
size_t index = TRY(debug_info_stream.read_value<LEB128<size_t>>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::String;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::AddrX1: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto index = TRY(debug_info_stream.read_value<u8>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::Address;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::AddrX2: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto index = TRY(debug_info_stream.read_value<u16>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::Address;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::AddrX4: {
|
2023-01-22 00:37:38 +01:00
|
|
|
auto index = TRY(debug_info_stream.read_value<u32>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::Address;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::AddrX: {
|
2023-01-30 00:02:38 +01:00
|
|
|
size_t index = TRY(debug_info_stream.read_value<LEB128<size_t>>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::Address;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AttributeDataForm::RngListX: {
|
2023-01-30 00:02:38 +01:00
|
|
|
size_t index = TRY(debug_info_stream.read_value<LEB128<size_t>>());
|
2021-10-09 17:38:26 +02:00
|
|
|
value.m_type = AttributeValue::Type::UnsignedNumber;
|
|
|
|
|
value.m_data.as_unsigned = index;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-04-28 20:12:21 +02:00
|
|
|
default:
|
2022-01-27 04:51:17 +03:30
|
|
|
dbgln("Unimplemented AttributeDataForm: {}", to_underlying(form));
|
2021-04-28 20:12:21 +02:00
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
ErrorOr<void> DwarfInfo::build_cached_dies() const
|
2021-06-19 11:50:24 +03:00
|
|
|
{
|
2021-06-19 15:33:03 +03:00
|
|
|
auto insert_to_cache = [this](DIE const& die, DIERange const& range) {
|
2021-06-19 11:50:24 +03:00
|
|
|
m_cached_dies_by_range.insert(range.start_address, DIEAndRange { die, range });
|
|
|
|
|
m_cached_dies_by_offset.insert(die.offset(), die);
|
|
|
|
|
};
|
2023-01-21 12:50:04 +01:00
|
|
|
auto get_ranges_of_die = [this](DIE const& die) -> ErrorOr<Vector<DIERange>> {
|
2023-01-22 00:32:08 +01:00
|
|
|
auto ranges = TRY(die.get_attribute(Attribute::Ranges));
|
2021-09-28 13:35:28 +03:00
|
|
|
if (ranges.has_value()) {
|
2021-10-09 17:45:48 +02:00
|
|
|
size_t offset;
|
|
|
|
|
if (ranges->form() == AttributeDataForm::SecOffset) {
|
|
|
|
|
offset = ranges->as_unsigned();
|
|
|
|
|
} else {
|
|
|
|
|
auto index = ranges->as_unsigned();
|
2023-01-22 00:32:08 +01:00
|
|
|
auto base = TRY(die.compilation_unit().range_lists_base());
|
2021-10-09 17:45:48 +02:00
|
|
|
// FIXME: This assumes that the format is DWARf32
|
2022-01-27 13:22:45 +03:30
|
|
|
auto offsets = debug_range_lists_data().slice(base);
|
|
|
|
|
offset = ByteReader::load32(offsets.offset_pointer(index * sizeof(u32))) + base;
|
2021-10-09 17:45:48 +02:00
|
|
|
}
|
2021-12-05 08:45:05 +01:00
|
|
|
|
2021-09-28 13:35:28 +03:00
|
|
|
Vector<DIERange> entries;
|
2021-12-05 08:45:05 +01:00
|
|
|
if (die.compilation_unit().dwarf_version() == 5) {
|
2023-01-30 11:05:43 +01:00
|
|
|
auto range_lists_stream = TRY(try_make<FixedMemoryStream>(debug_range_lists_data()));
|
2023-01-21 12:50:04 +01:00
|
|
|
TRY(range_lists_stream->seek(offset));
|
|
|
|
|
AddressRangesV5 address_ranges(move(range_lists_stream), die.compilation_unit());
|
|
|
|
|
TRY(address_ranges.for_each_range([&entries](auto range) {
|
2021-12-05 08:45:05 +01:00
|
|
|
entries.empend(range.start, range.end);
|
2023-01-21 12:50:04 +01:00
|
|
|
}));
|
2021-12-05 08:45:05 +01:00
|
|
|
} else {
|
2023-01-30 11:05:43 +01:00
|
|
|
auto ranges_stream = TRY(try_make<FixedMemoryStream>(debug_ranges_data()));
|
2023-01-21 12:50:04 +01:00
|
|
|
TRY(ranges_stream->seek(offset));
|
|
|
|
|
AddressRangesV4 address_ranges(move(ranges_stream), die.compilation_unit());
|
|
|
|
|
TRY(address_ranges.for_each_range([&entries](auto range) {
|
2021-12-05 08:45:05 +01:00
|
|
|
entries.empend(range.start, range.end);
|
2023-01-21 12:50:04 +01:00
|
|
|
}));
|
2021-12-05 08:45:05 +01:00
|
|
|
}
|
2021-09-28 13:35:28 +03:00
|
|
|
return entries;
|
|
|
|
|
}
|
2021-06-19 11:50:24 +03:00
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
auto start = TRY(die.get_attribute(Attribute::LowPc));
|
|
|
|
|
auto end = TRY(die.get_attribute(Attribute::HighPc));
|
2021-06-19 11:50:24 +03:00
|
|
|
|
|
|
|
|
if (!start.has_value() || !end.has_value())
|
2023-01-21 12:50:04 +01:00
|
|
|
return Vector<DIERange> {};
|
2021-06-19 11:50:24 +03:00
|
|
|
|
2021-10-09 16:58:48 +02:00
|
|
|
VERIFY(start->type() == Dwarf::AttributeValue::Type::Address);
|
2021-06-19 11:50:24 +03:00
|
|
|
|
|
|
|
|
// DW_AT_high_pc attribute can have different meanings depending on the attribute form.
|
|
|
|
|
// (Dwarf version 5, section 2.17.2).
|
|
|
|
|
|
|
|
|
|
uint32_t range_end = 0;
|
2021-10-09 16:58:48 +02:00
|
|
|
if (end->form() == Dwarf::AttributeDataForm::Addr)
|
2023-01-22 00:32:08 +01:00
|
|
|
range_end = TRY(end->as_addr());
|
2021-06-19 11:50:24 +03:00
|
|
|
else
|
2023-01-22 00:32:08 +01:00
|
|
|
range_end = TRY(start->as_addr()) + end->as_unsigned();
|
2021-06-19 11:50:24 +03:00
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
return Vector<DIERange> { DIERange { TRY(start.value().as_addr()), range_end } };
|
2021-06-19 11:50:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// If we simply use a lambda, type deduction fails because it's used recursively.
|
2023-01-22 00:32:08 +01:00
|
|
|
Function<ErrorOr<void>(DIE const& die)> insert_to_cache_recursively;
|
|
|
|
|
insert_to_cache_recursively = [&](DIE const& die) -> ErrorOr<void> {
|
2021-06-19 11:50:24 +03:00
|
|
|
if (die.offset() == 0 || die.parent_offset().has_value()) {
|
2023-01-22 00:32:08 +01:00
|
|
|
auto ranges = TRY(get_ranges_of_die(die));
|
2021-06-19 11:50:24 +03:00
|
|
|
for (auto& range : ranges) {
|
|
|
|
|
insert_to_cache(die, range);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(die.for_each_child([&](DIE const& child) -> ErrorOr<void> {
|
2021-06-19 11:50:24 +03:00
|
|
|
if (!child.is_null()) {
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(insert_to_cache_recursively(child));
|
2021-06-19 11:50:24 +03:00
|
|
|
}
|
2023-01-22 00:32:08 +01:00
|
|
|
return {};
|
|
|
|
|
}));
|
|
|
|
|
return {};
|
2021-06-19 11:50:24 +03:00
|
|
|
};
|
|
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(for_each_compilation_unit([&](CompilationUnit const& compilation_unit) -> ErrorOr<void> {
|
|
|
|
|
TRY(insert_to_cache_recursively(compilation_unit.root_die()));
|
|
|
|
|
return {};
|
|
|
|
|
}));
|
2021-06-19 11:50:24 +03:00
|
|
|
|
|
|
|
|
m_built_cached_dies = true;
|
2023-01-22 00:32:08 +01:00
|
|
|
return {};
|
2021-06-19 11:50:24 +03:00
|
|
|
}
|
|
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
ErrorOr<Optional<DIE>> DwarfInfo::get_die_at_address(FlatPtr address) const
|
2021-06-19 11:59:48 +03:00
|
|
|
{
|
|
|
|
|
if (!m_built_cached_dies)
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(build_cached_dies());
|
2021-06-19 11:59:48 +03:00
|
|
|
|
|
|
|
|
auto iter = m_cached_dies_by_range.find_largest_not_above_iterator(address);
|
|
|
|
|
while (!iter.is_end() && !iter.is_begin() && iter->range.end_address < address) {
|
|
|
|
|
--iter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (iter.is_end())
|
2023-01-22 00:32:08 +01:00
|
|
|
return Optional<DIE> {};
|
2021-06-19 11:59:48 +03:00
|
|
|
|
|
|
|
|
if (iter->range.start_address > address || iter->range.end_address < address) {
|
2023-01-22 00:32:08 +01:00
|
|
|
return Optional<DIE> {};
|
2021-06-19 11:59:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return iter->die;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 00:32:08 +01:00
|
|
|
ErrorOr<Optional<DIE>> DwarfInfo::get_cached_die_at_offset(FlatPtr offset) const
|
2021-06-19 12:03:14 +03:00
|
|
|
{
|
|
|
|
|
if (!m_built_cached_dies)
|
2023-01-22 00:32:08 +01:00
|
|
|
TRY(build_cached_dies());
|
2021-06-19 12:03:14 +03:00
|
|
|
|
|
|
|
|
auto* die = m_cached_dies_by_offset.find(offset);
|
|
|
|
|
if (!die)
|
2023-01-22 00:32:08 +01:00
|
|
|
return Optional<DIE> {};
|
2021-06-19 12:03:14 +03:00
|
|
|
return *die;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|