Skip to content

ida

apply_program(program)

Apply the program to the IDA current database.

Returns:

Type Description
int

The number of errors encountered while applying the changes.

Source code in bindings/python/quokka/backends/ida.py
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
def apply_program(program: Program) -> int:
    """Apply the program to the IDA current database.

    Returns:
        The number of errors encountered while applying the changes.
    """

    til = ida_typeinf.get_idati()        
    errors_count = 0

    for fun_addr, function in program.items():
        edits = function.proto.edits

        # Set name if it has been edited
        if edits.name_set:
            res = "x"
            if ida_name.set_name(fun_addr, function.name):
                res = "✓"
            else:
                errors_count += 1
            print(f"[{res}] set name of function at 0x{fun_addr:x} to {function.name}")

        if edits.prototype_set:
            flg = ida_typeinf.TINFO_DEFINITE
            res = "x"
            if ida_typeinf.apply_cdecl(til, fun_addr, function.prototype, flg):
                res = "✓"
            else:
                errors_count += 1
            print(f"[{res}] set prototype of function at 0x{fun_addr:x} to {function.prototype}")

        if edits.comments:
            cmts = "\n".join(function.proto.comments[x] for x in edits.comments)
            res = "x"
            if ida_funcs.set_func_cmt(fun_addr, cmts, True):  # set it repeatable
                res = "✓"
            else:
                errors_count += 1
            print(f"[{res}] set comment of function at 0x{fun_addr:x}")

        if edits.edges:
            for edge in (function.proto.edges[x] for x in edits.edges):
                src_addr = function._index_to_address[edge.source]
                dst_addr = function._index_to_address[edge.destination]
                src, dst = function[src_addr], function[dst_addr]
                res = "x"
                if ida_xref.add_cref(src, dst, ida_xref.fl_USobsolete):
                    res = "✓"
                else:
                    errors_count += 1
                print(f"[{res}] add edge from 0x{src:x} to 0x{dst:x}")

    for data in program.data:
        edits = data.proto.edits

        # Set name if it has been edited
        if edits.name_set:
            res = "x"
            if ida_name.set_name(data.address, data.name):
                res = "✓"
            else:
                errors_count += 1
            print(f"[{res}] set name of data at 0x{data.address:x} to {data.name}")

        if edits.type_str:
            res = "x"
            if ida_typeinf.apply_cdecl(til, data.address, edits.type_str, ida_typeinf.TINFO_DEFINITE):
                res = "✓"
            else:
                errors_count += 1
            print(f"[{res}] set type of data at 0x{data.address:x} to {edits.type_str}")

        if edits.comments:
            cmts = "\n".join(data.proto.comments[x] for x in edits.comments)
            res = "x"
            if ida_bytes.set_cmt(data.address, cmts, True):  # set it repeatable
                res = "✓"
            else:
                errors_count += 1
            print(f"[{res}] set comment of data at 0x{data.address:x}")

    return errors_count