"""The standalone build's lift-time pieces (lift.py --standalone): which SDK functions are replaced by the native runtime in host/, the stubs for the OS-facing ones it does not have yet, and the boot constants hos_main.c reads. Adapted from a64-kit's build.py (TOTKPort e88744a: implemented, gen_stubs, OS_NAMESPACES, nested_name, os_facing, thread_hooks, write_image_c), for MKW's Eden-layout image in the 64-bit window. The model is TOTKPort's: the SDK is replaced at the nn:: boundary where it talks to system services. A function the runtime defines as `HLE()` / `void imp_(Ctx *c)` is hooked where the game imports it and at the sdk's own definition. Every other free function of an OS-facing namespace the game imports gets a generated stub that logs its first call and returns 0. The rest of the SDK (libc, nn::util, nn::mem, std, the NVN driver) runs as lifted code. """ import os import re import struct import subprocess import sys import kit import toolchain HOST = os.path.join(kit.HERE, "..", "host") # a64-kit's list (TOTK), plus the Switch 2 SDK's service namespaces MKW imports (imports.py) OS_NAMESPACES = {"audio", "hid", "oe", "prepo", "nfp", "time", "account", "vi", "swkbd", "pl", "err", "codec", "diag", "detail", "settings", "ns", "applet", "album", "friends", "nifm", "ssl", "socket", "ec", "bcat", "web", "ldn", "irsensor", "aoc", "olsc", "pctl", "apm", "ro", "chat", "camera", "gs", "movie", "npns", "news", "fatal", "lbl", "capture", "syncpt"} def implemented(host=HOST): """C names of the HLE functions in host/*.c: `void imp_x(Ctx` definitions and `HLE(x)` macros""" names = set() for f in os.listdir(host): if f.endswith(".c"): src = open(os.path.join(host, f), errors="replace").read() names |= set(re.findall(r"^void (imp_\w+)\(Ctx", src, re.M)) names |= {"imp_" + n for n in re.findall(r"^HLE\((\w+)\)", src, re.M)} return names def nested_name(sym): """The components of an Itanium nested name (_ZN[K]...E), or None if it is not that simple shape""" m = re.match(r"_ZNK?", sym) if not m: return None i, parts = m.end(), [] while i < len(sym) and sym[i].isdigit(): j = i while sym[j].isdigit(): j += 1 n = int(sym[i:j]) parts.append(sym[j:j + n]) i = j + n return parts if i < len(sym) and sym[i] == "E" else None def os_facing(sym): """A free function of an OS-facing namespace: nn::::f (class members and data stay lifted)""" parts = nested_name(sym) # (the Switch 2 SDK keeps many service calls a level down: nn::camera::sdk::CreateCameraContext) return bool(parts) and parts[0] == "nn" and parts[1:2] and parts[1] in OS_NAMESPACES and ( len(parts) == 3 or (len(parts) == 4 and parts[2] == "sdk")) def nvn_exports(modules): """Every NVN function the sdk defines (the console driver's C entry points): all are hooked to host/nvn2.c's procs (gen/nvn2_exports.c), so the driver never runs, whether the game reaches a function through nvnDeviceGetProcAddress or imports it (nvnNativeImageInitialize, for one)""" sdk = next(m for m in modules if m.name == "sdk") have = {n[4:] for n in implemented()} return sorted(s["name"] for s in sdk.symbols if s["defined"] and s["name"] and nvn_proc_name(s["name"]) and s["type"] == "func" and s["name"] not in have) def nvn_proc_name(sym): """The NVN proc a symbol is: a C name (nvnFoo), or a C++ one (_ZnvnFoo: the Switch 2 driver exports some, nvnNativeImageInitialize among them); None for anything else""" if sym.startswith("nvn"): return sym m = re.match(r"_Z(\d+)(nvn\w+)", sym) return m.group(2)[:int(m.group(1))] if m else None def write_nvn_exports(gen, names): lines = ["/* Generated by nx2/lift.py --standalone: the sdk's NVN functions, routed to host/nvn2.c. */", '#include "a64.h"', "void nvn2_dispatch(Ctx *c, const char *name, void **cache);", ""] for n in names: lines.append('void imp_%s(Ctx *A64_NOESC c) { static void *k[2]; nvn2_dispatch(c, "%s", k); }' % (n, nvn_proc_name(n))) text = "\n".join(lines) + "\n" path = os.path.join(gen, "nvn2_exports.c") if not os.path.exists(path) or open(path).read() != text: open(path, "w", newline="\n").write(text) def hle_set(modules): """(HLE'd symbols, stubbed symbols): the runtime's imp_ functions some module has, the sdk's NVN functions, and the OS-facing functions the game modules import that it does not define""" hle = {n[4:] for n in implemented()} known = {s["name"] for m in modules for s in m.symbols if s["name"]} # (TOTK's SDK functions that MKW's newer SDK no longer has: their HLE stays unused; a typo shows here too) missing = sorted(hle - known) if missing: print("host/ defines %d imp_ functions for symbols no module has (unused):\n %s" % (len(missing), "\n ".join(missing))) hle -= set(missing) kind = {s["name"]: s["type"] for m in modules for s in m.symbols if s["defined"] and s["name"]} stubbed = set() for m in modules: if m.name not in ("sdk", "rtld"): # (every game module: main and the subsdk* the exefs has) stubbed |= {s["name"] for s in m.symbols if not s["defined"] and s["name"] and os_facing(s["name"]) and kind.get(s["name"], "func") == "func"} return hle | set(nvn_exports(modules)), stubbed - hle def demangle(syms): try: out = subprocess.run([toolchain.find("llvm-cxxfilt")], input="\n".join(syms), capture_output=True, text=True).stdout.splitlines() return dict(zip(syms, out)) if len(out) == len(syms) else {s: s for s in syms} except (OSError, TypeError): return {s: s for s in syms} def gen_stubs(gen, meta): have = implemented() | {"imp_" + h["sym"] for h in meta["hooks"] if nvn_proc_name(h["sym"])} hooks = [h for h in meta["hooks"] if h["c"] not in have] dm = demangle([h["sym"] for h in hooks]) lines = ['#include "a64.h"', "void a64_unimplemented(Ctx *A64_NOESC c, const char *sym, int *seen);", ""] for h in hooks: lines.append("/* %s */" % dm[h["sym"]].replace("*/", "* /")) lines.append('void %s(Ctx *A64_NOESC c) { static int seen; a64_unimplemented(c, "%s", &seen); }' % (h["c"], h["sym"])) text = "\n".join(lines) + "\n" path = os.path.join(gen, "a64_stubs.c") if not os.path.exists(path) or open(path).read() != text: open(path, "w", newline="\n").write(text) return len(hooks) def thread_hooks(img, meta): """The SDK thread entry's start and exit hooks (nnmusl's per-thread TLS setup and teardown): the entry starts `mrs x1, tpidrro_el0; add x1, x1, #0x1f8; str x0, [x1]` and loads each hook through a GOT slot""" for m in meta["modules"]: for pc in range(m["base"], m["base"] + m["size"] - 12, 4): if not img.in_code(pc) or img.word(pc) != 0xD53BD061: continue if img.word(pc + 4) != 0x9107E021 or img.word(pc + 8) != 0xF9000020: continue hooks, page = [], {} for q in range(pc + 12, pc + 12 + 4 * 40, 4): w = img.word(q) if (w & 0x9F000000) == 0x90000000: # ADRP imm = ((w >> 3) & 0x1FFFFC) | ((w >> 29) & 3) imm = imm - (1 << 21) if imm & (1 << 20) else imm page[w & 31] = (q & ~0xFFF) + (imm << 12) elif (w & 0xFFC00000) == 0xF9400000 and ((w >> 5) & 31) in page: # LDR Xt, [Xn, #imm] got = page.pop((w >> 5) & 31) + ((w >> 10) & 0xFFF) * 8 hooks.append(struct.unpack("", "const uint64_t hos_main_stack_size = %#xULL;" % stack, "const int hos_main_priority = %d, hos_main_core = %d;" % (prio, core), "const uint64_t hos_image_base = %#xULL, hos_image_end = %#xULL;" % (meta["base"], meta["end"]), "const int hos_module_count = %d;" % len(mods), "const uint64_t hos_module_init[] = {%s};" % ", ".join("%#xULL" % m["init"] for m in mods), "const char *const hos_module_name[] = {%s};" % ", ".join('"%s"' % m["name"] for m in mods), "const uint64_t hos_entry_nninitStartup = %#xULL;" % ex.get("nninitStartup", 0), "const uint64_t hos_entry_nnMain = %#xULL;" % ex.get("nnMain", 0), "const uint64_t hos_program_id = %#xULL; /* (main.npdm's ACI0) */" % program_id, 'const char hos_main_build_id[] = "%s";' % main_build] for sym in ("_init_libc0", "_init_libc1", "_init_libc2", "nnosInitialize", "nninitInitializeSdkModule", "nvnBootstrapLoaderInternal"): lines.append("const uint64_t hos_entry_%s = %#xULL;" % (sym.lstrip("_"), ex.get(sym, 0))) start_hook, exit_hook = thread_hooks(img, meta) lines.append("const uint64_t hos_thread_start_hook = %#xULL, hos_thread_exit_hook = %#xULL;" % (start_hook, exit_hook)) text = "\n".join(lines) + "\n" path = os.path.join(gen, "hos_image.c") if not os.path.exists(path) or open(path).read() != text: open(path, "w", newline="\n").write(text) return start_hook, exit_hook