summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-12-21 11:58:07 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-12-21 11:58:07 (GMT)
commitc5895ba93ea679a130ea97a67c2f56b07697bbaa (patch)
tree7b4fe2c7eef479521488d4b6d4cfad9aab443af3
parent370169058fc6f4a7ed6013d0f965ac75beff4f5e (diff)
Shown basic blocks of a given function.
-rw-r--r--python/basic_blocks.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/python/basic_blocks.py b/python/basic_blocks.py
new file mode 100644
index 0000000..f77808b
--- /dev/null
+++ b/python/basic_blocks.py
@@ -0,0 +1,68 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+
+import argparse
+import sys
+
+# from pychrysalide.features import *
+from pychrysalide.analysis.contents import FileContent
+from pychrysalide.analysis import StudyProject
+from pychrysalide.arch import ArchInstruction
+from pychrysalide.core import wait_for_all_global_works
+
+
+def link_type_to_str(t):
+
+ links = [ getattr(ArchInstruction, a) for a in dir(ArchInstruction) if a.startswith('ILT_') ]
+
+ return str(links[links.index(t)])[4:]
+
+
+def show_block(blk, grp):
+
+ first, last = blk.boundaries
+
+ print('Block @ 0x%x: %s - %s' % (first.range.addr.phys, first.keyword, last.keyword), end='')
+
+ for db, dt in blk.destinations:
+ print(' |-> 0x%x (%s)' % (db.boundaries[0].range.addr.phys, link_type_to_str(dt)), end='')
+
+ print()
+
+
+if __name__ == '__main__':
+
+ title = '%s - Show basic blocks of a given function.' % sys.argv[0]
+
+ parser = argparse.ArgumentParser(description=title, add_help=False)
+
+ parser.add_argument('-h', '--help', action='store_true', help='Display the command line options understood by %s.' % sys.argv[0])
+
+ parser.add_argument('binfile', type=str, help='The object file to be examined')
+ parser.add_argument('fname', type=str, help='The analyzed function to display')
+
+ args = parser.parse_args()
+
+ if args.help:
+ parser.print_help()
+ sys.exit(1)
+
+ prj = StudyProject()
+
+ cnt = FileContent(args.binfile)
+
+ prj.discover(cnt)
+
+ wait_for_all_global_works()
+
+ binary = prj.contents[0]
+
+ sym = binary.format.find_symbol_by_label(args.fname)
+
+ if not(sym):
+ print('Function "%s" not found!' % args.fname)
+ sys.exit(1)
+
+ for bb in sym.basic_blocks:
+ show_block(bb, sym.basic_blocks)