2020-05-04 12:14:39 +03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
|
|
|
|
*
|
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"
|
|
|
|
|
|
2020-09-01 11:43:32 +02:00
|
|
|
#include <AK/MemoryStream.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
|
|
|
|
2020-12-25 02:14:56 +01:00
|
|
|
DwarfInfo::DwarfInfo(const ELF::Image& elf)
|
2020-05-04 12:14:39 +03:00
|
|
|
: m_elf(elf)
|
|
|
|
|
{
|
|
|
|
|
m_debug_info_data = section_data(".debug_info");
|
|
|
|
|
m_abbreviation_data = section_data(".debug_abbrev");
|
|
|
|
|
m_debug_strings_data = section_data(".debug_str");
|
|
|
|
|
|
|
|
|
|
populate_compilation_units();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 13:14:00 +01:00
|
|
|
ReadonlyBytes DwarfInfo::section_data(const String& 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);
|
2020-05-23 14:55:39 +03:00
|
|
|
if (section.is_undefined())
|
|
|
|
|
return {};
|
2020-12-19 13:14:00 +01:00
|
|
|
return section.bytes();
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DwarfInfo::populate_compilation_units()
|
|
|
|
|
{
|
2020-12-19 13:14:00 +01:00
|
|
|
if (!m_debug_info_data.data())
|
2020-05-23 14:55:39 +03:00
|
|
|
return;
|
2020-08-05 10:55:36 +02:00
|
|
|
|
2020-09-01 11:43:32 +02:00
|
|
|
InputMemoryStream stream { m_debug_info_data };
|
2020-08-05 10:55:36 +02:00
|
|
|
while (!stream.eof()) {
|
2020-05-04 12:14:39 +03:00
|
|
|
auto unit_offset = stream.offset();
|
|
|
|
|
CompilationUnitHeader compilation_unit_header {};
|
|
|
|
|
|
2020-08-05 10:55:36 +02:00
|
|
|
stream >> Bytes { &compilation_unit_header, sizeof(compilation_unit_header) };
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(compilation_unit_header.address_size == sizeof(u32));
|
|
|
|
|
VERIFY(compilation_unit_header.version <= 4);
|
2020-05-04 12:14:39 +03:00
|
|
|
|
|
|
|
|
u32 length_after_header = compilation_unit_header.length - (sizeof(CompilationUnitHeader) - offsetof(CompilationUnitHeader, version));
|
|
|
|
|
m_compilation_units.empend(*this, unit_offset, compilation_unit_header);
|
2020-08-05 10:55:36 +02:00
|
|
|
stream.discard_or_error(length_after_header);
|
2020-05-04 12:14:39 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|