summaryrefslogtreecommitdiff
path: root/tools/libdol2asm/tools.py
blob: d195102912f69e5faabcad30c8d449c2bd7987a3 (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
116

from intervaltree import Interval, IntervalTree

from .data import *
from . import util

def calculate_function_alignments(context, libraries):
    # gather all functions
    function_list = []
    for lib in libraries:
        for tu in lib.translation_units.values():
            for sec in tu.sections.values():
                function_list.extend([symbol for symbol in sec.symbols if isinstance(symbol, Function)])

    # sort functions
    function_list.sort(key=lambda x: x.addr)

    # find function alignment
    for curr, next in util.mapOverlap(function_list, 2):
        if not curr or not next:
            continue

        assert isinstance(curr, Function)
        assert isinstance(next, Function)

        curr_end = curr.addr + curr.size
        next_start = next.addr
        if curr_end == next_start:
            continue

        for x in [32,16,8]:
            if (curr_end + x - 1) & ~(x - 1) == next_start:
                next.alignment = x
                context.debug(f"[function] {next.addr:08X} {next.identifier.name} aligned with {next.alignment} bytes")
                curr.padding = 0
                break

def caluclate_symbol_data_alignment(context, section):
    for curr, next in util.mapOverlap(section.symbols, 2):
        if not curr or not next:
            continue

        if isinstance(curr, Function):
            continue

        curr_end = curr.relative_addr + curr.size
        next_start = next.relative_addr
        if curr_end == next_start:
            continue

        for x in [64,32,16]:
            if x > section.alignment:
                continue
            if (curr_end + x - 1) & ~(x - 1) == next_start:
                next.alignment = x
                context.debug(f"[symbol] {next.identifier.name}: {next.addr:08X} ({next.relative_addr:06X}) aligned with {x} bytes")
                break



def merge_symbol_from_group(context, section, group):
    if len(group) == 1:
        return None

    if isinstance(group[0], ArbitraryData):
        struct = Structure.create(section, group)
        struct.set_mlts(group[0]._module,group[0]._library,group[0]._translation_unit,group[0]._section)
        return [struct]

    context.error(group[0])
    context.error(group[0].section.id)
    context.error(group)
    assert False

def merge_section_symbols(context, section, add_list, remove_list):
    group = []
    symbols = []

    def merge_group():
        assert group
        new_symbols = merge_symbol_from_group(context, section, group)
        if new_symbols != None:
            add_list.update(set(new_symbols))
            remove_list.update(set(group) - add_list)
            symbols.extend(new_symbols)
        else:
            symbols.extend(group)

    for old_symbol in section.symbols:
        is_unaligned = type(old_symbol).__name__ == "ArbitraryData" and old_symbol.addr % 4 != 0

        if is_unaligned:
            assert group
            group.append(old_symbol)
        else:
            if group:
                merge_group()
            else:
                symbols.extend(group)
            group = [old_symbol]

    if group:
        merge_group()

    section.symbols = symbols   

def merge_symbols(context, libraries):
    remove_list = set()
    add_list = set()
    for lib in libraries:
        for tu in lib.translation_units.values():
            for section in tu.sections.values():
                merge_section_symbols(context, section, add_list, remove_list)

    return add_list, remove_list