summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibDebug/Dwarf/AbbreviationsMap.cpp
blob: bbc00fdeb93ffe65f8b3cc845b90b9428c5858e1 (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
/*
 * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "AbbreviationsMap.h"
#include "DwarfInfo.h"

#include <AK/MemoryStream.h>

namespace Debug::Dwarf {

AbbreviationsMap::AbbreviationsMap(const DwarfInfo& dwarf_info, u32 offset)
    : m_dwarf_info(dwarf_info)
    , m_offset(offset)
{
    populate_map();
}

void AbbreviationsMap::populate_map()
{
    InputMemoryStream abbreviation_stream(m_dwarf_info.abbreviation_data());
    abbreviation_stream.discard_or_error(m_offset);

    while (!abbreviation_stream.eof()) {
        size_t abbreviation_code = 0;
        abbreviation_stream.read_LEB128_unsigned(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 {};
        abbreviation_stream.read_LEB128_unsigned(tag);

        u8 has_children = 0;
        abbreviation_stream >> has_children;

        AbbreviationEntry abbrevation_entry {};
        abbrevation_entry.tag = static_cast<EntryTag>(tag);
        abbrevation_entry.has_children = (has_children == 1);

        AttributeSpecification current_attribute_specification {};
        do {
            size_t attribute_value = 0;
            size_t form_value = 0;
            abbreviation_stream.read_LEB128_unsigned(attribute_value);
            abbreviation_stream.read_LEB128_unsigned(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;
                abbreviation_stream.read_LEB128_signed(data_value);
                current_attribute_specification.value = data_value;
            }

            if (current_attribute_specification.attribute != Attribute::None) {
                abbrevation_entry.attribute_specifications.append(current_attribute_specification);
            }
        } while (current_attribute_specification.attribute != Attribute::None || current_attribute_specification.form != AttributeDataForm::None);

        m_entries.set((u32)abbreviation_code, move(abbrevation_entry));
    }
}

Optional<AbbreviationsMap::AbbreviationEntry> AbbreviationsMap::get(u32 code) const
{
    return m_entries.get(code);
}

}