#!/usr/bin/env python3 """Genuine Apple-linker chained rebases and slid native image entry execution. The linked executable is unchanged. Original Arm runs with independently decoded rebases at two load bases. Native dispatch must reproduce all registers, complete DATA_CONST/DATA mappings and the FP/LR stack frame. This is an entry research ABI; dyld, dependency loading and complete macOS process execution remain unresolved. """ import argparse import hashlib import importlib import json from pathlib import Path import platform import struct import subprocess import tempfile from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM from unicorn import Uc, UC_ARCH_ARM64, UC_MODE_ARM, UC_HOOK_CODE from unicorn import arm64_const as arm from differential import capture_arm, initialize_arm, state_dict as complete_state from differential import ROOT, ARM_REGISTERS, MASK, states from evidence import write_evidence mapped = importlib.import_module('mapped-image-differential') require, digest, snapshot = mapped.require, mapped.digest, mapped.snapshot STOP, PAGE = mapped.STOP, mapped.PAGE STACK_BASE, STACK_BYTES, STACK_TOP = mapped.STACK_BASE, mapped.STACK_BYTES, mapped.STACK_TOP SEED = 0x9e3779b97f4a7c15 LOAD_BASES = (0x40000000000, 0x50000000000) FIXTURE = ROOT / 'tests/fixtures/chained-image-arm64.S' def parse_image(data): segments, sections, symbols = [], {}, {} symbol_table, chained, entry_offset = None, None, None for offset, command, body in mapped.commands(data): if command == 0x19: require(len(body) >= 72, 'truncated segment') _, _, name, address, size, fileoff, filesize, maximum, protection, count, flags = struct.unpack_from('= 28, 'truncated fixup header') version, starts, imports, strings, count, import_format, symbol_format = struct.unpack_from('<7I', payload) require(version == 0 and count == 0 and import_format == 1 and symbol_format == 0, 'unsupported imports or fixup-header format') require(28 <= starts and starts + 4 <= len(payload), 'starts table out of bounds') require(imports <= len(payload) and strings <= len(payload), 'imports/symbols offset out of bounds') segment_count = struct.unpack_from('= 22 + page_count * 2 and node + size <= len(payload), 'truncated segment starts node') require(page_size in (4096, 16384) and max_pointer == 0, 'unsupported page/max-pointer profile') require(pointer_format in (2, 6), 'authenticated/arm64e or unknown pointer format unsupported') formats.add(pointer_format) segment = info['segments'][index] require(segment['address'] - info['preferred_base'] == segment_offset, 'segment offset disagrees with Mach-O') require(page_count * page_size <= segment['bytes'], 'page count exceeds segment') for page_index in range(page_count): start = struct.unpack_from('> 63 == 0, 'bound pointer unsupported') require((raw >> 44) & 0x7f == 0, 'nonzero reserved pointer bits') target = raw & ((1 << 36) - 1) high8 = (raw >> 36) & 0xff require(high8 == 0, 'tagged pointer outside ordinary-RAM fixture profile') value = load_base + target if pointer_format == 6 else target + slide require(0 < value < (1 << 47), 'rebased target exceeds native user range') rows.append({'segment_index': index, 'file_offset': file_offset, 'guest_address': segment['address'] + slide + relative_slot, 'raw': raw, 'value': value, 'pointer_format': pointer_format}) next_words = (raw >> 51) & 0xfff if not next_words: break within += next_words * 4 return {'preferred_base': info['preferred_base'], 'load_base': load_base, 'pointer_formats': sorted(formats), 'rebases': rows} def initialized_segments(data, info, fixups): slide = fixups['load_base'] - info['preferred_base'] segments = {} for segment in info['segments']: if not segment['protection']: continue memory = bytearray(segment['bytes']) start = segment['file_offset'] memory[:segment['file_bytes']] = data[start:start + segment['file_bytes']] segments[segment['address'] + slide] = memory for row in fixups['rebases']: segment = info['segments'][row['segment_index']] start = segment['address'] + slide struct.pack_into('= memory['file_bytes'] and not any(data_bytes[8:]), 'expected zero-tail BSS output') struct.pack_into('