我有一个用于 m1 arm cpu 的最小 c 程序,返回 42:
void _start() {
asm("mov x0, #42;");
asm("mov x16, #1;");
asm("svc 0x80;");
}
Run Code Online (Sandbox Code Playgroud)
此代码在告诉 clang 使用 _start 符号后进行编译并返回正确的值。
clang -Wl,-e, -Wl,__start test.c -o dyn.out
./dyn.out ; echo $?
42
Run Code Online (Sandbox Code Playgroud)
然而,根据 otool,这个二进制文件仍然具有动态链接:
otool -L ./dyn.out
./dyn.out:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
Run Code Online (Sandbox Code Playgroud)
在告诉 clang 生成未签名的静态二进制文件后,macOS 在尝试运行时立即杀死该二进制文件。
clang -Wl,-e, -Wl,__start -static -nostdlib test.c -o static_no_sign.out
zsh: killed ./static_no_sign.out
Run Code Online (Sandbox Code Playgroud)
在运行之前对二进制文件进行签名也会产生同样的问题。
clang -Wl,-e, -Wl,__start -static -nostdlib test.c -o static_sign.out
codesign -s - static_sign.out
./static_sign.out
zsh: killed …Run Code Online (Sandbox Code Playgroud)