"""Relabelling iOS device objects for the arm64 iOS simulator, for objects that mac2ios can't relabel, and copying the code signature of one Mach-O file onto another.""" import struct from collections.abc import Iterator from dataclasses import dataclass from pathlib import Path from .common import BuildError MH_MAGIC_64 = 0xFEEDFACF MH_OBJECT = 1 MACH_HEADER_64 = "\n" AR_HEADER = 60 AR_SIZE = slice(48, 58) AR_LONG_NAME = b"#1/" AR_SYMBOL_TABLE = b"__.SYMDEF" def shift_offset(command: bytearray, at: int, field: str = " None: (value,) = struct.unpack_from(field, command, at) if value: struct.pack_into(field, command, at, value + SHIFT) def shift_command(cmd: int, command: bytearray) -> None: if cmd == LC_SEGMENT_64: shift_offset(command, SEGMENT_64_FILEOFF, " bytes: """obj with its LC_VERSION_MIN_IPHONEOS replaced by an LC_BUILD_VERSION for the simulator, or obj unchanged.""" magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags, reserved = struct.unpack_from(MACH_HEADER_64, obj) if magic != MH_MAGIC_64 or filetype != MH_OBJECT: return obj header = struct.calcsize(MACH_HEADER_64) commands, offset, replaced = [], header, False for _ in range(ncmds): cmd, size = struct.unpack_from(LOAD_COMMAND, obj, offset) command = bytearray(obj[offset : offset + size]) if cmd == LC_VERSION_MIN_IPHONEOS: _, _, minos, sdk = struct.unpack_from(" Iterator[tuple[bytes, bytes, bytes]]: """Each member's header, its BSD long name bytes (empty for short names) and its contents.""" if not data.startswith(AR_MAGIC): raise BuildError("not an ar archive") offset = len(AR_MAGIC) while offset < len(data): header = data[offset : offset + AR_HEADER] size = int(header[AR_SIZE].decode().strip()) body = data[offset + AR_HEADER : offset + AR_HEADER + size] name_length = int(header[3:16].decode().strip()) if header.startswith(AR_LONG_NAME) else 0 yield header, body[:name_length], body[name_length:] offset += AR_HEADER + size + (size & 1) def simulator_archive(lib: Path) -> int: """Rewrites lib's device objects for the simulator, without its symbol table, which ranlib must write again; returns how many objects changed.""" data = lib.read_bytes() out, changed = bytearray(AR_MAGIC), 0 for header, name, contents in archive_members(data): if name.startswith(AR_SYMBOL_TABLE) or header.startswith(AR_SYMBOL_TABLE): continue new = simulator_object(contents) changed += new is not contents size = len(name) + len(new) out += header[: AR_SIZE.start] + str(size).encode().ljust(AR_SIZE.stop - AR_SIZE.start) + header[AR_SIZE.stop :] out += name + new + (b"\n" if size & 1 else b"") lib.write_bytes(out) return changed LC_CODE_SIGNATURE = 0x1D LINKEDIT_DATA = " bool: return len(data) >= struct.calcsize(MACH_HEADER_64) and struct.unpack_from(" list[FatArch] | None: if len(data) < struct.calcsize(FAT_HEADER): return None magic, count = struct.unpack_from(FAT_HEADER, data) if magic != FAT_MAGIC or not 0 < count < JAVA_CLASS_MIN_VERSION: return None size = struct.calcsize(FAT_ARCH) return [FatArch(*struct.unpack_from(FAT_ARCH, data, struct.calcsize(FAT_HEADER) + i * size)) for i in range(count)] def is_macho(data: bytes) -> bool: return is_thin_macho(data) or fat_arches(data) is not None def load_commands(data: bytes) -> Iterator[tuple[int, int]]: """The offset and type of each load command of a thin 64-bit Mach-O file.""" ncmds = struct.unpack_from(" CodeSignature | None: for offset, cmd in load_commands(data): if cmd == LC_CODE_SIGNATURE: _, _, dataoff, datasize = struct.unpack_from(LINKEDIT_DATA, data, offset) return CodeSignature(offset, dataoff, datasize) return None def linkedit_command(data: bytes) -> int: for offset, cmd in load_commands(data): if cmd == LC_SEGMENT_64 and data[offset + SEGMENT_64_NAME.start : offset + SEGMENT_64_NAME.stop].rstrip(b"\0") == LINKEDIT: return offset raise BuildError("the Mach-O file has no __LINKEDIT segment") def copy_thin_signature(released: bytes, built: bytes) -> bytes: """built with released's signature in place of its own, or added after its __LINKEDIT data as codesign adds one, with released's LC_CODE_SIGNATURE and the __LINKEDIT sizes that cover the signature.""" signature = code_signature(released) if signature is None: return built size = struct.calcsize(LINKEDIT_DATA) out = bytearray(built) own = code_signature(built) if own: command = own.command del out[own.dataoff :] else: _, _, _, _, ncmds, sizeofcmds, _, _ = struct.unpack_from(MACH_HEADER_64, built) command = struct.calcsize(MACH_HEADER_64) + sizeofcmds # codesign adds the command in the free space after the others, which must not hide a difference of the code if any(built[command : command + size]): return built struct.pack_into(" bytes: """built signed as released is: each thin file, or each architecture of a universal one, with released's code signature, in the layout of released's architectures; built as it is when the two are not Mach-O files of the same architectures.""" if is_thin_macho(released) and is_thin_macho(built): return copy_thin_signature(released, built) released_arches, built_arches = fat_arches(released), fat_arches(built) if released_arches is None or built_arches is None: return built if [(a.cputype, a.cpusubtype) for a in released_arches] != [(a.cputype, a.cpusubtype) for a in built_arches]: return built out = bytearray(released[: struct.calcsize(FAT_HEADER) + len(released_arches) * struct.calcsize(FAT_ARCH)]) for ours, theirs in zip(built_arches, released_arches, strict=True): out += bytes(max(0, theirs.offset - len(out))) out += copy_thin_signature(released[theirs.offset : theirs.offset + theirs.size], built[ours.offset : ours.offset + ours.size]) return bytes(out) def same_code(released: bytes, built: bytes) -> bool: """Whether released is built, or built with released's code signature.""" if released == built: return True try: return is_macho(released) and copy_code_signature(released, built) == released except (struct.error, BuildError): return False