summaryrefslogtreecommitdiff
path: root/python/ins_collect.py
blob: f48a49edff19b28dd21870a6b9c7558ee12c3dc6 (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
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
import pychrysalide
from pychrysalide.analysis.contents import FileContent
from pychrysalide.format.elf import ElfFormat
from pychrysalide.analysis import LoadedBinary


if len(sys.argv) != 2:
    print('Usage: %s <elf binary>' % sys.argv[0])
    sys.exit(1)


# Load the provided binary

cnt = FileContent(sys.argv[1])

fmt = ElfFormat(cnt)

binary = LoadedBinary(fmt)

binary.analyze_and_wait()


# Compute some stats

ins_count = 0

opsize_0 = 0
opsize_1 = 0

srcsz_0 = 0
srcsz_1 = 0

destsz_0 = 0
destsz_1 = 0

for ins in binary.processor.instrs:

    ins_count += 1

    size = len(ins.operands)

    if size == 0:
        opsize_0 += 1

    elif size == 1:
        opsize_1 += 1

    size = len(ins.sources)

    if size == 0:
        srcsz_0 += 1

    elif size == 1:
        srcsz_1 += 1

    size = len(ins.destinations)

    if size == 0:
        destsz_0 += 1

    elif size == 1:
        destsz_1 += 1


# Display the results

print('=== Collected %d instructions ===' % ins_count)

print('No operand: %u' % opsize_0)
print('One operand: %u' % opsize_1)

print('No source: %u' % srcsz_0)
print('One source: %u' % srcsz_1)

print('No destination: %u' % destsz_0)
print('One destination: %u' % destsz_1)