summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibDebug/Dwarf/AddressRanges.cpp
blob: c43ad1e46870ed5fad4495220ef14839f7c6250b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (c) 2020-2021, Itamar S. <itamar8910@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "AddressRanges.h"
#include "DwarfTypes.h"
#include <AK/LEB128.h>
#include <LibCore/Stream.h>

namespace Debug::Dwarf {

AddressRangesV5::AddressRangesV5(NonnullOwnPtr<AK::Stream> range_lists_stream, CompilationUnit const& compilation_unit)
    : m_range_lists_stream(move(range_lists_stream))
    , m_compilation_unit(compilation_unit)
{
}

ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
{
    // Dwarf version 5, section 2.17.3 "Non-Contiguous Address Ranges"

    Optional<FlatPtr> current_base_address;
    while (!m_range_lists_stream->is_eof()) {
        auto entry_type = TRY(m_range_lists_stream->read_value<u8>());
        switch (static_cast<RangeListEntryType>(entry_type)) {
        case RangeListEntryType::BaseAddress: {
            current_base_address = TRY(m_range_lists_stream->read_value<FlatPtr>());
            break;
        }
        case RangeListEntryType::BaseAddressX: {
            FlatPtr index;
            TRY(LEB128::read_unsigned(*m_range_lists_stream, index));
            current_base_address = TRY(m_compilation_unit.get_address(index));
            break;
        }
        case RangeListEntryType::OffsetPair: {
            Optional<FlatPtr> base_address = current_base_address;
            if (!base_address.has_value()) {
                base_address = TRY(m_compilation_unit.base_address());
            }

            if (!base_address.has_value())
                return Error::from_string_literal("Expected base_address for rangelist");

            size_t start_offset, 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;
            TRY(LEB128::read_unsigned(*m_range_lists_stream, length));
            callback(Range { start, start + length });
            break;
        }
        case RangeListEntryType::StartXEndX: {
            size_t start, 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;
            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;
        }
        case RangeListEntryType::EndOfList:
            return {};
        default:
            dbgln("unsupported range list entry type: 0x{:x}", entry_type);
            return Error::from_string_literal("Unsupported range list entry type");
        }
    }

    return {};
}

AddressRangesV4::AddressRangesV4(NonnullOwnPtr<AK::Stream> ranges_stream, CompilationUnit const& compilation_unit)
    : m_ranges_stream(move(ranges_stream))
    , m_compilation_unit(compilation_unit)
{
}

ErrorOr<void> AddressRangesV4::for_each_range(Function<void(Range)> callback)
{
    // Dwarf version 4, section 2.17.3 "Non-Contiguous Address Ranges"

    Optional<FlatPtr> current_base_address;
    while (!m_ranges_stream->is_eof()) {
        auto begin = TRY(m_ranges_stream->read_value<FlatPtr>());
        auto end = TRY(m_ranges_stream->read_value<FlatPtr>());

        if (begin == 0 && end == 0) {
            // end of list entry
            return {};
        } else if (begin == explode_byte(0xff)) {
            current_base_address = end;
        } else {
            FlatPtr base = current_base_address.value_or(TRY(m_compilation_unit.base_address()).value_or(0));
            callback({ base + begin, base + end });
        }
    }

    return {};
}

}