"""The lift driver: image (we are rtld) -> function discovery -> C, on the vendored a64-kit plus the Switch 2
additions (nso2: DT_RELR, insn: ARMv8.1-8.5 forms, lift_fixes: jump tables).
Usage: python -I nx2/lift.py [--exefs
] [--work ] [--write] [--max-func N] [--layout eden|a64] [--aslr 0]
--layout eden (default): Eglantine's addresses, rtld included, from 0x80000000 + --aslr (image.py; an emulator
run must use the same offset: rng_seed_enabled=true, rng_seed=> 12>);
--layout a64: a64-kit's own layout from 0x08000000 (its native runtime, below 4 GB)
Always: builds /image (image.bin, image.json), discovers every function of rtld-less main + subsdk* + sdk,
and prints coverage: functions, instructions, jump tables, indirect branches, and the functions whose every
instruction translates (coverage.txt has the untranslated words by site count).
--write: also writes the lifted C to /gen (about a gigabyte; game code: stays local, never committed).
--standalone: the standalone build's lift (/sa/image, /sa/gen): the SDK's OS-facing functions hooked
to the native runtime in host/ or to generated stubs, plus hos_image.c (nx2/standalone.py). It shares the
chunk boundaries of /image, so its unhooked chunks are the emulator build's, byte for byte.
"""
import argparse
import collections
import glob
import json
import os
import struct
import sys
import time
import capstone
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 insn
import lift_fixes
import fastvec
import fma_soft
import fma_steps
import vecfp
import cmpflags
import fastfp
import dead_flags
import walk
import discover
import chunks
import standalone
import adr_entries # noqa: E402
import nso
import poll
import nx_image
import image as eden_image
import a64emit
import a64_lift
import a64_rtgen
insn.install()
fastfp.install() # ((Triforce) fast float regions, with A64_FASTFP=1, hot or check: its emit_function in the kit's place, so before lift_fixes' and poll's wrappers of it: nx2/fastfp.py)
lift_fixes.install()
fastvec.install() # ((Triforce, 11-21, LIFT-02) TBL, EXT, 128-bit loads and DUP as one instruction each: nx2/fastvec.py)
fma_soft.install() # ((Triforce, 11-22, LIFT-06) the exact FMA bodies' short way out where the CPU has no FMA3: nx2/fma_soft.py)
fma_steps.install() # ((Triforce, 11-22, LIFT-05) FRECPS and FRSQRTS S as one fused instruction where the build has FMA3: nx2/fma_steps.py)
vecfp.install() # ((Triforce, 11-22, LIFT-03) SSE fast paths before float forms lifted lane by lane, with A64_VECFP=1: nx2/vecfp.py)
cmpflags.install() # ((Triforce) a subtraction's V flag in a form the compiler folds, with A64_SUB_V=1 or ovf: nx2/cmpflags.py)
dead_flags.install() # ((Triforce, 11-24) the kit's dead_flags answered at once for a function that writes no flag: nx2/dead_flags.py)
walk.install() # ((Triforce, 11-24) the kit's walk, one shared entry per plain op's word: nx2/walk.py)
def module_of(img, pc):
for lo, hi, name in img.code:
if lo <= pc < hi:
return name
return "?"
def coverage(L, img, out_dir):
"""Per-module: functions, instructions, untranslated words; and the share of functions that translate fully."""
keys = {}
for body, _ in L.funcs.values():
for pc, (k, w) in body.items():
if k[0] == "op":
keys.setdefault(w, pc)
words = list(keys)
_, flags = a64emit.translate([(keys[w], w) for w in words])
bad = {w for w, f in zip(words, flags) if f & a64emit.UNHANDLED}
# UDF and unallocated encodings trap on the hardware too (code falls into them after noreturn calls): the
# lifter's trap is their exact translation, so they do not count against coverage
md = capstone.Cs(capstone.CS_ARCH_ARM64, capstone.CS_MODE_ARM)
undefined = set()
for w in bad:
ins = next(md.disasm(struct.pack("