From c5895ba93ea679a130ea97a67c2f56b07697bbaa Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Fri, 21 Dec 2018 12:58:07 +0100 Subject: Shown basic blocks of a given function. --- python/basic_blocks.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 python/basic_blocks.py 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) -- cgit v0.11.2-87-g4458