"""Instruction census: every distinct word in each module's .text, through the lifter's classify() and suyu's translator (a64emit), counted by capstone mnemonic. Counts occurrences, so data in .text and padding are included; the per-function numbers come from lift.py (discovered code only). Usage: python -I nx2/census.py [--exefs ] [--modules main,sdk] [--top 40] [--nx2] (with the Switch 2 forms) """ import argparse import collections import os import struct import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # (python -I leaves the script folder off) import kit import nso2 # noqa: F401 (DT_RELR) import nso import a64emit import a64_lift import capstone def main(): p = argparse.ArgumentParser() p.add_argument("--exefs", default=kit.EXEFS) p.add_argument("--modules", default="") p.add_argument("--top", type=int, default=40) p.add_argument("--nx2", action="store_true", help="apply the Switch 2 instruction extension first") a = p.parse_args() a.exefs = a.exefs or kit.need_exefs() if a.nx2: import insn insn.install() md = capstone.Cs(capstone.CS_ARCH_ARM64, capstone.CS_MODE_ARM) want = set(a.modules.split(",")) if a.modules else None total = collections.Counter() for m in nso.load_exefs(a.exefs): if want and m.name not in want: continue lo, hi = m.code_range() words = struct.unpack_from("<%dI" % ((hi - lo) // 4), m.image, lo) count = collections.Counter(words) ops = [w for w in count if a64_lift.classify(w, 0x10000)[0] == "op"] _, flags = a64emit.translate([(0x10000, w) for w in ops]) bad = collections.Counter() bad_words = collections.Counter() for w, f in zip(ops, flags): if f & a64emit.UNHANDLED: ins = next(md.disasm(struct.pack("