"""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 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() lift_fixes.install() 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("