#!/usr/bin/env python3 # 把改写后的模块映像直接写成一个苹果格式的动态库 Guest.framework/Guest:只装段,没有符号,没有代码之外的任何东西。 # 只用 Python 标准库,不调用编译器、链接器或任何苹果工具,在 macOS / Linux / Windows 上产出逐字节相同的文件。 # 给谁用:壳安装包(ios/tools/shell_build.sh)不含游戏程序;使用者用自己的游戏文件改写出模块映像后,由 d2rm_pack.py 调本脚本 # 写出这个库、放进安装包的 Frameworks/,再用自签工具整包签名。宿主的装载器(host/loader.c 的 DM_SHELL 分支)加载它并按节名取段。 # # 节的布局与内嵌做法(ios/tools/gen_embed.py)一致,装载器不用分两套: # __TEXT,__dm<名>x0 原 text:[0, 圆整16K(TEXT_END)) # __DM<名>W,__img 可写段:[圆整16K(TEXT_END), PATCH_OFF) # __TEXT,__dm<名>x1 补丁区:[PATCH_OFF, 圆整16K(文件尾)) # 文件格式上要注意的三点(都是真机 / Mac 上实测出来的): # · 必须声明依赖 /usr/lib/libSystem.B.dylib,否则系统拒绝加载(「must link with at least libSystem.dylib」); # · 每个节按 16K 对齐、整页大小:装载器用 vm_remap 按整页取; # · __LINKEDIT 放在最后并留在文件末尾:签名工具把签名追加在它后面。 # 用法:guest_dylib.py <产物目录> <输出目录> <模块名...> 输出 <输出目录>/Guest.framework/{Guest,Info.plist} # Write the rewritten module images directly as one Apple-format dynamic library Guest.framework/Guest: it holds only segments, no symbols, and nothing other than the code. # Uses only the Python standard library, calls no compiler, linker or any Apple tool, and produces byte-for-byte identical files on macOS / Linux / Windows. # Who it is for: the shell install package (ios/tools/shell_build.sh) contains no game program; after users rewrite the module images from their own game files, d2rm_pack.py calls this script # to write this library and put it into the install package's Frameworks/, and then the whole package is signed with a sideloading tool. The host's loader (the DM_SHELL branch of host/loader.c) loads it and fetches the segments by section name. # The section layout matches the embedded approach (ios/tools/gen_embed.py), so the loader does not need two variants: # __TEXT,__dmx0 original text: [0, round16K(TEXT_END)) # __DMW,__img writable segment: [round16K(TEXT_END), PATCH_OFF) # __TEXT,__dmx1 patch area: [PATCH_OFF, round16K(end of file)) # Three points to note about the file format (all found by measurement on a real device / Mac): # · a dependency on /usr/lib/libSystem.B.dylib must be declared, otherwise the system refuses to load it ("must link with at least libSystem.dylib"); # · every section is aligned to 16K and is a whole number of pages: the loader takes whole pages with vm_remap; # · __LINKEDIT goes last and stays at the end of the file: the signing tool appends the signature after it. # Usage: guest_dylib.py writes /Guest.framework/{Guest,Info.plist} import hashlib, os, struct, sys PG = 0x4000 MIN_IOS = 26 << 16 # 与工程的最低系统版本一致(ios/project.yml) | matches the project's minimum system version (ios/project.yml) def r16(x): return (x + PG - 1) & ~(PG - 1) def s16(s): b = s.encode(); assert len(b) <= 16, s; return b.ljust(16, b'\0') def segment(name, addr, size, fileoff, filesize, prot, sects): b = struct.pack(' CFBundleExecutableGuest CFBundleIdentifier%s CFBundleInfoDictionaryVersion6.0 CFBundleNameGuest CFBundlePackageTypeFMWK CFBundleShortVersionString1.0 CFBundleVersion1 MinimumOSVersion26.0 CFBundleSupportedPlatformsiPhoneOS ''' def write(out, dst, names, bundle_id='local.guest'): fw = os.path.join(dst, 'Guest.framework'); os.makedirs(fw, exist_ok=True) data = build(out, names) with open(os.path.join(fw, 'Guest'), 'wb') as f: f.write(data) with open(os.path.join(fw, 'Info.plist'), 'w', encoding='utf-8', newline='\n') as f: f.write(INFO_PLIST % bundle_id) return fw, len(data) if __name__ == '__main__': if len(sys.argv) < 4: sys.exit(__doc__ or '用法:guest_dylib.py <产物目录> <输出目录> <模块名...>') fw, n = write(sys.argv[1], sys.argv[2], sys.argv[3:]) print('%s:%d 字节' % (fw, n))