summaryrefslogtreecommitdiff
path: root/python/ins_collect.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/ins_collect.py')
-rw-r--r--python/ins_collect.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/python/ins_collect.py b/python/ins_collect.py
new file mode 100644
index 0000000..f48a49e
--- /dev/null
+++ b/python/ins_collect.py
@@ -0,0 +1,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)