#!/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 #misc = '*' if len(first.sources) == 0 else ' ' misc = ' [%u]' % blk.rank print('%s Block @ 0x%x: %s - %s' % (misc, 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)