"""The image builder's relocations (third_party/a64-kit/hos/nx_image.py, NxImage._relocate) on authored modules. We are rtld, so each relocation must write what Horizon's rtld writes: RELATIVE is the module base + A, and ABS64, GLOB_DAT and JUMP_SLOT are the symbol's address + A (AArch64 ELF ABI). Symbols bind across modules, an undefined weak symbol is 0, and any other undefined symbol binds to a trampoline that names it. No game is used. """ import os import struct import sys from types import ModuleType, SimpleNamespace import unittest # nx_image imports tools/nso.py, which imports lz4.block to decompress NSO segments. _relocate decompresses nothing, # and run_all installs no packages (CI has no lz4): an empty stand-in lets the test run there; a real lz4 is used when # present. try: import lz4.block # noqa: F401 except ImportError: sys.modules["lz4"] = ModuleType("lz4") sys.modules["lz4.block"] = sys.modules["lz4"].block = ModuleType("lz4.block") sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "third_party", "a64-kit", "hos")) import nx_image # noqa: E402 MAIN, SDK = nx_image.MAIN_BASE, nx_image.MAIN_BASE + 0x100000 def module(name, base, symbols, relocs): return SimpleNamespace(name=name, base=base, symbols=symbols, relocs=relocs) def sym(name, value=0, defined=True, bind="global"): return {"name": name, "value": value, "defined": defined, "bind": bind} def rel(offset, kind, addend, name=None): return {"offset": offset, "type": kind, "addend": addend, "sym": name} def relocated(modules): """NxImage._relocate on these modules: each symbol is defined by at most one of them""" img = object.__new__(nx_image.NxImage) img.modules = modules img.mem = bytearray(0x200000) img.hle, img.hooks, img.hook_syms, img.tramp, img.unresolved = set(), {}, {}, {}, {} img.exports = {s["name"]: (m, s) for m in modules for s in m.symbols if s["defined"]} img._relocate() return img, lambda base, offset: struct.unpack_from("