├── .gitignore ├── LICENSE ├── Package.swift ├── README.md └── Sources ├── Arm64ToSim └── Arm64ToSim.swift └── arm64-to-sim └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Bogo Giertler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.4 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "arm64-to-sim", 6 | platforms: [ 7 | .macOS(.v11), 8 | ], 9 | products: [ 10 | .library( 11 | name: "Arm64ToSim", 12 | targets: ["Arm64ToSim"] 13 | ), 14 | .executable( 15 | name: "arm64-to-sim", 16 | targets: ["arm64-to-sim"] 17 | ), 18 | ], 19 | targets: [ 20 | .target( 21 | name: "Arm64ToSim" 22 | ), 23 | .executableTarget( 24 | name: "arm64-to-sim", 25 | dependencies: [ 26 | .target(name: "Arm64ToSim"), 27 | ] 28 | ), 29 | ] 30 | ) 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arm64-to-sim 2 | 3 | A simple command-line tool for hacking native ARM64 binaries to run on the Apple Silicon iOS Simulator. 4 | -------------------------------------------------------------------------------- /Sources/Arm64ToSim/Arm64ToSim.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import MachO 3 | 4 | // support checking for Mach-O `cmd` and `cmdsize` properties 5 | extension Data { 6 | var loadCommand: UInt32 { 7 | let lc: load_command = withUnsafeBytes { $0.load(as: load_command.self) } 8 | return lc.cmd 9 | } 10 | 11 | var commandSize: Int { 12 | let lc: load_command = withUnsafeBytes { $0.load(as: load_command.self) } 13 | return Int(lc.cmdsize) 14 | } 15 | 16 | func asStruct(fromByteOffset offset: Int = 0) -> T { 17 | return withUnsafeBytes { $0.load(fromByteOffset: offset, as: T.self) } 18 | } 19 | } 20 | 21 | extension Array where Element == Data { 22 | func merge() -> Data { 23 | return reduce(into: Data()) { $0.append($1) } 24 | } 25 | } 26 | 27 | // support peeking at Data contents 28 | extension FileHandle { 29 | func peek(upToCount count: Int) throws -> Data? { 30 | // persist the current offset, since `upToCount` doesn't guarantee all bytes will be read 31 | let originalOffset = offsetInFile 32 | let data = try read(upToCount: count) 33 | try seek(toOffset: originalOffset) 34 | return data 35 | } 36 | } 37 | 38 | public struct FileOpenError: LocalizedError { 39 | public let path: String 40 | 41 | public var errorDescription: String? { 42 | "Cannot open a handle for the file at \(path). Aborting." 43 | } 44 | } 45 | 46 | public struct IncorrectBinaryError: LocalizedError { 47 | public var errorDescription: String? { 48 | "The file is not a correct arm64 binary. Try thinning (via lipo) or unarchiving (via ar) first." 49 | } 50 | } 51 | 52 | public struct LcBuildVersionCommandAlreadyExistError: LocalizedError { 53 | public var errorDescription: String? { 54 | "This arm64 binary already contains an LC_BUILD_VERSION load command!" 55 | } 56 | } 57 | 58 | enum Transmogrifier { 59 | private static func readBinary(atPath path: String) throws -> (Data, [Data], Data) { 60 | guard let handle = FileHandle(forReadingAtPath: path) else { 61 | throw FileOpenError(path: path) 62 | } 63 | 64 | // chop up the file into a relevant number of segments 65 | let headerData = try handle.read(upToCount: MemoryLayout.stride)! 66 | 67 | let header: mach_header_64 = headerData.asStruct() 68 | if header.magic != MH_MAGIC_64 || header.cputype != CPU_TYPE_ARM64 { 69 | throw IncorrectBinaryError() 70 | } 71 | 72 | let loadCommandsData: [Data] = try (0...stride) 74 | return try handle.read(upToCount: Int(loadCommandPeekData!.commandSize))! 75 | } 76 | 77 | // discard 8 empty bytes that should exist here 78 | let bytesToDiscard = abs(MemoryLayout.stride - MemoryLayout.stride) 79 | _ = handle.readData(ofLength: bytesToDiscard) 80 | 81 | let programData = try handle.readToEnd()! 82 | 83 | try handle.close() 84 | 85 | return (headerData, loadCommandsData, programData) 86 | } 87 | 88 | private static func updateVersionMin(_ data: Data, _ offset: UInt32, _ platform: UInt32) -> Data { 89 | var command = build_version_command(cmd: UInt32(LC_BUILD_VERSION), 90 | cmdsize: UInt32(MemoryLayout.stride), 91 | platform: platform, 92 | minos: 13 << 16 | 0 << 8 | 0, 93 | sdk: 13 << 16 | 0 << 8 | 0, 94 | ntools: 0) 95 | 96 | return Data(bytes: &command, count: MemoryLayout.stride) 97 | } 98 | 99 | static func processBinary(atPath path: String) throws { 100 | let (headerData, loadCommandsData, programData) = try readBinary(atPath: path) 101 | 102 | // `offset` is kind of a magic number here, since we know that's the only meaningful change to binary size 103 | // having a dynamic `offset` requires two passes over the load commands and is left as an exercise to the reader 104 | let offset = UInt32(abs(MemoryLayout.stride - MemoryLayout.stride)) 105 | 106 | let editedCommandsData = try loadCommandsData 107 | .map { (lc) -> Data in 108 | switch lc.loadCommand { 109 | case UInt32(LC_VERSION_MIN_IPHONEOS): 110 | return updateVersionMin(lc, offset, UInt32(PLATFORM_IOSSIMULATOR)) 111 | case UInt32(LC_VERSION_MIN_TVOS): 112 | return updateVersionMin(lc, offset, UInt32(PLATFORM_TVOSSIMULATOR)) 113 | case UInt32(LC_BUILD_VERSION): 114 | throw LcBuildVersionCommandAlreadyExistError() 115 | default: 116 | return lc 117 | } 118 | } 119 | .merge() 120 | 121 | var header: mach_header_64 = headerData.asStruct() 122 | header.sizeofcmds = UInt32(editedCommandsData.count) 123 | 124 | // reassemble the binary 125 | let reworkedData = [ 126 | Data(bytes: &header, count: MemoryLayout.stride), 127 | editedCommandsData, 128 | programData 129 | ].merge() 130 | 131 | // save back to disk 132 | try reworkedData.write(to: URL(fileURLWithPath: path)) 133 | } 134 | } 135 | 136 | public func arm64ToSim(_ path: String) throws { 137 | try Transmogrifier.processBinary(atPath: path) 138 | } 139 | -------------------------------------------------------------------------------- /Sources/arm64-to-sim/main.swift: -------------------------------------------------------------------------------- 1 | import Arm64ToSim 2 | 3 | try arm64ToSim(CommandLine.arguments[1]) 4 | --------------------------------------------------------------------------------