1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 09:28:13 +00:00

AK: Port LEB128 to the new AK::Stream

This commit is contained in:
Tim Schumacher 2023-01-29 13:09:25 +01:00 committed by Andrew Kaster
parent 31f59855b4
commit 787f4d639a
10 changed files with 120 additions and 209 deletions

View file

@ -23,18 +23,17 @@ ErrorOr<void> AbbreviationsMap::populate_map()
{
auto abbreviation_stream = TRY(FixedMemoryStream::construct(m_dwarf_info.abbreviation_data()));
TRY(abbreviation_stream->discard(m_offset));
Core::Stream::WrapInAKInputStream wrapped_abbreviation_stream { *abbreviation_stream };
while (!abbreviation_stream->is_eof()) {
size_t abbreviation_code = 0;
LEB128::read_unsigned(wrapped_abbreviation_stream, abbreviation_code);
TRY(LEB128::read_unsigned(*abbreviation_stream, abbreviation_code));
// An abbreviation code of 0 marks the end of the
// abbreviations for a given compilation unit
if (abbreviation_code == 0)
break;
size_t tag {};
LEB128::read_unsigned(wrapped_abbreviation_stream, tag);
TRY(LEB128::read_unsigned(*abbreviation_stream, tag));
auto has_children = TRY(abbreviation_stream->read_value<u8>());
@ -46,15 +45,15 @@ ErrorOr<void> AbbreviationsMap::populate_map()
do {
size_t attribute_value = 0;
size_t form_value = 0;
LEB128::read_unsigned(wrapped_abbreviation_stream, attribute_value);
LEB128::read_unsigned(wrapped_abbreviation_stream, form_value);
TRY(LEB128::read_unsigned(*abbreviation_stream, attribute_value));
TRY(LEB128::read_unsigned(*abbreviation_stream, form_value));
current_attribute_specification.attribute = static_cast<Attribute>(attribute_value);
current_attribute_specification.form = static_cast<AttributeDataForm>(form_value);
if (current_attribute_specification.form == AttributeDataForm::ImplicitConst) {
ssize_t data_value;
LEB128::read_signed(wrapped_abbreviation_stream, data_value);
TRY(LEB128::read_signed(*abbreviation_stream, data_value));
current_attribute_specification.value = data_value;
}

View file

@ -21,8 +21,6 @@ ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
{
// Dwarf version 5, section 2.17.3 "Non-Contiguous Address Ranges"
Core::Stream::WrapInAKInputStream wrapped_range_lists_stream { *m_range_lists_stream };
Optional<FlatPtr> current_base_address;
while (!m_range_lists_stream->is_eof()) {
auto entry_type = TRY(m_range_lists_stream->read_value<u8>());
@ -33,7 +31,7 @@ ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
}
case RangeListEntryType::BaseAddressX: {
FlatPtr index;
LEB128::read_unsigned(wrapped_range_lists_stream, index);
TRY(LEB128::read_unsigned(*m_range_lists_stream, index));
current_base_address = TRY(m_compilation_unit.get_address(index));
break;
}
@ -47,29 +45,29 @@ ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
return Error::from_string_literal("Expected base_address for rangelist");
size_t start_offset, end_offset;
LEB128::read_unsigned(wrapped_range_lists_stream, start_offset);
LEB128::read_unsigned(wrapped_range_lists_stream, end_offset);
TRY(LEB128::read_unsigned(*m_range_lists_stream, start_offset));
TRY(LEB128::read_unsigned(*m_range_lists_stream, end_offset));
callback(Range { start_offset + *base_address, end_offset + *base_address });
break;
}
case RangeListEntryType::StartLength: {
auto start = TRY(m_range_lists_stream->read_value<FlatPtr>());
size_t length;
LEB128::read_unsigned(wrapped_range_lists_stream, length);
TRY(LEB128::read_unsigned(*m_range_lists_stream, length));
callback(Range { start, start + length });
break;
}
case RangeListEntryType::StartXEndX: {
size_t start, end;
LEB128::read_unsigned(wrapped_range_lists_stream, start);
LEB128::read_unsigned(wrapped_range_lists_stream, end);
TRY(LEB128::read_unsigned(*m_range_lists_stream, start));
TRY(LEB128::read_unsigned(*m_range_lists_stream, end));
callback(Range { TRY(m_compilation_unit.get_address(start)), TRY(m_compilation_unit.get_address(end)) });
break;
}
case RangeListEntryType::StartXLength: {
size_t start, length;
LEB128::read_unsigned(wrapped_range_lists_stream, start);
LEB128::read_unsigned(wrapped_range_lists_stream, length);
TRY(LEB128::read_unsigned(*m_range_lists_stream, start));
TRY(LEB128::read_unsigned(*m_range_lists_stream, length));
auto start_addr = TRY(m_compilation_unit.get_address(start));
callback(Range { start_addr, start_addr + length });
break;

View file

@ -26,8 +26,7 @@ ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
auto stream = TRY(FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data()));
// Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
TRY(stream->seek(m_offset));
Core::Stream::WrapInAKInputStream wrapped_stream { *stream };
LEB128::read_unsigned(wrapped_stream, m_abbreviation_code);
TRY(LEB128::read_unsigned(*stream, m_abbreviation_code));
m_data_offset = TRY(stream->tell());
if (m_abbreviation_code == 0) {

View file

@ -121,18 +121,14 @@ ErrorOr<AttributeValue> DwarfInfo::get_attribute_value(AttributeDataForm form, s
}
case AttributeDataForm::SData: {
i64 data;
Core::Stream::WrapInAKInputStream wrapped_debug_info_stream { debug_info_stream };
LEB128::read_signed(wrapped_debug_info_stream, data);
VERIFY(!wrapped_debug_info_stream.has_any_error());
TRY(LEB128::read_signed(debug_info_stream, data));
value.m_type = AttributeValue::Type::SignedNumber;
value.m_data.as_signed = data;
break;
}
case AttributeDataForm::UData: {
u64 data;
Core::Stream::WrapInAKInputStream wrapped_debug_info_stream { debug_info_stream };
LEB128::read_unsigned(wrapped_debug_info_stream, data);
VERIFY(!wrapped_debug_info_stream.has_any_error());
TRY(LEB128::read_unsigned(debug_info_stream, data));
value.m_type = AttributeValue::Type::UnsignedNumber;
value.m_data.as_unsigned = data;
break;
@ -174,9 +170,7 @@ ErrorOr<AttributeValue> DwarfInfo::get_attribute_value(AttributeDataForm form, s
}
case AttributeDataForm::ExprLoc: {
size_t length;
Core::Stream::WrapInAKInputStream wrapped_debug_info_stream { debug_info_stream };
LEB128::read_unsigned(wrapped_debug_info_stream, length);
VERIFY(!wrapped_debug_info_stream.has_any_error());
TRY(LEB128::read_unsigned(debug_info_stream, length));
value.m_type = AttributeValue::Type::DwarfExpression;
TRY(assign_raw_bytes_value(length));
break;
@ -209,9 +203,7 @@ ErrorOr<AttributeValue> DwarfInfo::get_attribute_value(AttributeDataForm form, s
case AttributeDataForm::Block: {
value.m_type = AttributeValue::Type::RawBytes;
size_t length;
Core::Stream::WrapInAKInputStream wrapped_debug_info_stream { debug_info_stream };
LEB128::read_unsigned(wrapped_debug_info_stream, length);
VERIFY(!wrapped_debug_info_stream.has_any_error());
TRY(LEB128::read_unsigned(debug_info_stream, length));
TRY(assign_raw_bytes_value(length));
break;
}
@ -249,9 +241,7 @@ ErrorOr<AttributeValue> DwarfInfo::get_attribute_value(AttributeDataForm form, s
}
case AttributeDataForm::StrX: {
size_t index;
Core::Stream::WrapInAKInputStream wrapped_debug_info_stream { debug_info_stream };
LEB128::read_unsigned(wrapped_debug_info_stream, index);
VERIFY(!wrapped_debug_info_stream.has_any_error());
TRY(LEB128::read_unsigned(debug_info_stream, index));
value.m_type = AttributeValue::Type::String;
value.m_data.as_unsigned = index;
break;
@ -276,18 +266,14 @@ ErrorOr<AttributeValue> DwarfInfo::get_attribute_value(AttributeDataForm form, s
}
case AttributeDataForm::AddrX: {
size_t index;
Core::Stream::WrapInAKInputStream wrapped_debug_info_stream { debug_info_stream };
LEB128::read_unsigned(wrapped_debug_info_stream, index);
VERIFY(!wrapped_debug_info_stream.has_any_error());
TRY(LEB128::read_unsigned(debug_info_stream, index));
value.m_type = AttributeValue::Type::Address;
value.m_data.as_unsigned = index;
break;
}
case AttributeDataForm::RngListX: {
size_t index;
Core::Stream::WrapInAKInputStream wrapped_debug_info_stream { debug_info_stream };
LEB128::read_unsigned(wrapped_debug_info_stream, index);
VERIFY(!wrapped_debug_info_stream.has_any_error());
TRY(LEB128::read_unsigned(debug_info_stream, index));
value.m_type = AttributeValue::Type::UnsignedNumber;
value.m_data.as_unsigned = index;
break;

View file

@ -38,8 +38,6 @@ ErrorOr<void> LineProgram::parse_unit_header()
ErrorOr<void> LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type)
{
Core::Stream::WrapInAKInputStream wrapped_stream { m_stream };
if (m_unit_header.version() >= 5) {
auto path_entry_format_count = TRY(m_stream.read_value<u8>());
@ -47,16 +45,16 @@ ErrorOr<void> LineProgram::parse_path_entries(Function<void(PathEntry& entry)> c
for (u8 i = 0; i < path_entry_format_count; i++) {
UnderlyingType<ContentType> content_type;
LEB128::read_unsigned(wrapped_stream, content_type);
TRY(LEB128::read_unsigned(m_stream, content_type));
UnderlyingType<AttributeDataForm> data_form;
LEB128::read_unsigned(wrapped_stream, data_form);
TRY(LEB128::read_unsigned(m_stream, data_form));
format_descriptions.empend(static_cast<ContentType>(content_type), static_cast<AttributeDataForm>(data_form));
}
size_t paths_count = 0;
LEB128::read_unsigned(wrapped_stream, paths_count);
TRY(LEB128::read_unsigned(m_stream, paths_count));
for (size_t i = 0; i < paths_count; i++) {
PathEntry entry;
@ -88,10 +86,10 @@ ErrorOr<void> LineProgram::parse_path_entries(Function<void(PathEntry& entry)> c
entry.path = path;
if (list_type == PathListType::Filenames) {
size_t directory_index = 0;
LEB128::read_unsigned(wrapped_stream, directory_index);
TRY(LEB128::read_unsigned(m_stream, directory_index));
size_t _unused = 0;
LEB128::read_unsigned(wrapped_stream, _unused); // skip modification time
LEB128::read_unsigned(wrapped_stream, _unused); // skip file size
TRY(LEB128::read_unsigned(m_stream, _unused)); // skip modification time
TRY(LEB128::read_unsigned(m_stream, _unused)); // skip file size
entry.directory_index = directory_index;
dbgln_if(DWARF_DEBUG, "file: {}, directory index: {}", path, directory_index);
}
@ -99,7 +97,6 @@ ErrorOr<void> LineProgram::parse_path_entries(Function<void(PathEntry& entry)> c
}
}
VERIFY(!wrapped_stream.has_any_error());
return {};
}
@ -160,11 +157,8 @@ void LineProgram::reset_registers()
ErrorOr<void> LineProgram::handle_extended_opcode()
{
Core::Stream::WrapInAKInputStream wrapped_stream { m_stream };
size_t length = 0;
LEB128::read_unsigned(wrapped_stream, length);
TRY(wrapped_stream.try_handle_any_error());
TRY(LEB128::read_unsigned(m_stream, length));
auto sub_opcode = TRY(m_stream.read_value<u8>());
@ -183,8 +177,7 @@ ErrorOr<void> LineProgram::handle_extended_opcode()
case ExtendedOpcodes::SetDiscriminator: {
dbgln_if(DWARF_DEBUG, "SetDiscriminator");
size_t discriminator;
LEB128::read_unsigned(wrapped_stream, discriminator);
TRY(wrapped_stream.try_handle_any_error());
TRY(LEB128::read_unsigned(m_stream, discriminator));
break;
}
default:
@ -196,8 +189,6 @@ ErrorOr<void> LineProgram::handle_extended_opcode()
}
ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
{
Core::Stream::WrapInAKInputStream wrapped_stream { m_stream };
switch (opcode) {
case StandardOpcodes::Copy: {
append_to_line_info();
@ -205,8 +196,7 @@ ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
}
case StandardOpcodes::AdvancePc: {
size_t operand = 0;
LEB128::read_unsigned(wrapped_stream, operand);
TRY(wrapped_stream.try_handle_any_error());
TRY(LEB128::read_unsigned(m_stream, operand));
size_t delta = operand * m_unit_header.min_instruction_length();
dbgln_if(DWARF_DEBUG, "AdvancePC by: {} to: {:p}", delta, m_address + delta);
m_address += delta;
@ -214,8 +204,7 @@ ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
}
case StandardOpcodes::SetFile: {
size_t new_file_index = 0;
LEB128::read_unsigned(wrapped_stream, new_file_index);
TRY(wrapped_stream.try_handle_any_error());
TRY(LEB128::read_unsigned(m_stream, new_file_index));
dbgln_if(DWARF_DEBUG, "SetFile: new file index: {}", new_file_index);
m_file_index = new_file_index;
break;
@ -224,15 +213,13 @@ ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
// not implemented
dbgln_if(DWARF_DEBUG, "SetColumn");
size_t new_column;
LEB128::read_unsigned(wrapped_stream, new_column);
TRY(wrapped_stream.try_handle_any_error());
TRY(LEB128::read_unsigned(m_stream, new_column));
break;
}
case StandardOpcodes::AdvanceLine: {
ssize_t line_delta;
LEB128::read_signed(wrapped_stream, line_delta);
TRY(wrapped_stream.try_handle_any_error());
TRY(LEB128::read_signed(m_stream, line_delta));
VERIFY(line_delta >= 0 || m_line >= (size_t)(-line_delta));
m_line += line_delta;
dbgln_if(DWARF_DEBUG, "AdvanceLine: {}", m_line);
@ -253,8 +240,7 @@ ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
}
case StandardOpcodes::SetIsa: {
size_t isa;
LEB128::read_unsigned(wrapped_stream, isa);
TRY(wrapped_stream.try_handle_any_error());
TRY(LEB128::read_unsigned(m_stream, isa));
dbgln_if(DWARF_DEBUG, "SetIsa: {}", isa);
break;
}