如何计算 Linux ELF 二进制文件中的总基本块数

How to calculate the total basic block number in a Linux ELF binary

我想看看在常见的使用场景下,有多少代码块对于普通软件来说是不需要的。有没有静态二进制分析工具可以算出一个Linux ELF的总基本块数呢?

好的,我用Angr写了一个脚本来获取所有的基本块:

#!/usr/bin/env python3
# A script to dump function and basic block locations, size, etc.
# Install angr (https://docs.angr.io/introductory-errata/install) before use it.
# @author: xiaogw (https://whosebug.com/users/1267984/xiaogw)
import angr
import sys

def dump_functions_bbs(p, cfg):
  for key in cfg.kb.functions:
    for bb in cfg.kb.functions[key].blocks:
      print("%s: %s" % (hex(bb.addr), hex(bb.size)))

def main(argv):
  if (len(argv) < 2):
    print("Usage %s <BIN>" % argv[0])
    return 1
  path_to_binary = argv[1]
  p = angr.Project(path_to_binary, load_options={'auto_load_libs': False})
  cfg = p.analyses.CFGFast()
  dump_functions_bbs(p, cfg)

  return 0

if __name__ == '__main__':
  main(sys.argv)