aboutsummaryrefslogtreecommitdiffstats
path: root/src/steamvr_patches/architecture.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/steamvr_patches/architecture.py')
-rw-r--r--src/steamvr_patches/architecture.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/steamvr_patches/architecture.py b/src/steamvr_patches/architecture.py
new file mode 100644
index 0000000..2431069
--- /dev/null
+++ b/src/steamvr_patches/architecture.py
@@ -0,0 +1,92 @@
+# SPDX-FileCopyrightText: 2026 Tomasz Kramkowski <tomasz@kramkow.ski>
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+"""Architecture-specific tools and encodings for the compiled hooks."""
+
+import struct
+from collections.abc import Callable
+from dataclasses import dataclass
+
+from capstone import CS_ARCH_ARM64, CS_ARCH_X86, CS_MODE_64, CS_MODE_ARM
+from elftools.elf.enums import ENUM_RELOC_TYPE_AARCH64, ENUM_RELOC_TYPE_x64
+
+
+def x86_branch(address: int, target: int, call: bool) -> bytes:
+ displacement = target - address - 5
+ if not -(1 << 31) <= displacement < 1 << 31:
+ raise ValueError("hook target is outside relative jump range")
+ return struct.pack("<ci", (b"\xe8" if call else b"\xe9"), displacement)
+
+
+def arm64_branch(address: int, target: int, call: bool) -> bytes:
+ displacement = target - address
+ if displacement % 4 or not -(1 << 27) <= displacement < 1 << 27:
+ raise ValueError("hook target is outside relative jump range")
+ opcode = 0x94000000 if call else 0x14000000
+ return struct.pack("<I", opcode | ((displacement // 4) & 0x3FFFFFF))
+
+
+@dataclass(frozen=True)
+class Architecture:
+ compiler: tuple[str, ...]
+ linker: str
+ flags: tuple[str, ...]
+ disassembler: tuple[int, int]
+ branch: Callable[[int, int, bool], bytes]
+ hook_size: int
+ ins_alignment: int
+ nop: bytes
+ relative_relocs: frozenset[int]
+
+ def validate_site(self, address: int, length: int) -> None:
+ if length < self.hook_size:
+ raise ValueError(f"hook requires at least {self.hook_size} bytes")
+ if address % self.ins_alignment:
+ raise ValueError(
+ f"hook address requires {self.ins_alignment}-byte alignment"
+ )
+
+
+ARCHITECTURES = {
+ "EM_X86_64": Architecture(
+ compiler=("cc",),
+ linker="ld",
+ flags=("-mno-red-zone", "-fcf-protection=none"),
+ disassembler=(CS_ARCH_X86, CS_MODE_64),
+ branch=x86_branch,
+ hook_size=5,
+ ins_alignment=1,
+ nop=b"\x90",
+ relative_relocs=frozenset(
+ ENUM_RELOC_TYPE_x64[name] for name in ("R_X86_64_PC32", "R_X86_64_PLT32")
+ ),
+ ),
+ "EM_AARCH64": Architecture(
+ compiler=("clang", "--target=aarch64-linux-gnu"),
+ linker="ld.lld",
+ flags=("-mbranch-protection=none",),
+ disassembler=(CS_ARCH_ARM64, CS_MODE_ARM),
+ branch=arm64_branch,
+ hook_size=4,
+ ins_alignment=4,
+ nop=(0xD503201F).to_bytes(4, "little"),
+ # PC-relative branches and ADRP + low-12-bit address/load pairs.
+ relative_relocs=frozenset(
+ ENUM_RELOC_TYPE_AARCH64[name]
+ for name in (
+ "R_AARCH64_LD_PREL_LO19",
+ "R_AARCH64_ADR_PREL_LO21",
+ "R_AARCH64_ADR_PREL_PG_HI21",
+ "R_AARCH64_ADD_ABS_LO12_NC",
+ "R_AARCH64_LDST8_ABS_LO12_NC",
+ "R_AARCH64_TSTBR14",
+ "R_AARCH64_CONDBR19",
+ "R_AARCH64_JUMP26",
+ "R_AARCH64_CALL26",
+ "R_AARCH64_LDST16_ABS_LO12_NC",
+ "R_AARCH64_LDST32_ABS_LO12_NC",
+ "R_AARCH64_LDST64_ABS_LO12_NC",
+ )
+ ),
+ ),
+}