我有一个用于 m1 arm cpu 的最小 c 程序,返回 42:
void _start() {
asm("mov x0, #42;");
asm("mov x16, #1;");
asm("svc 0x80;");
}
此代码在告诉 clang 使用 _start 符号后进行编译并返回正确的值。
clang -Wl,-e, -Wl,__start test.c -o dyn.out
./dyn.out ; echo $?
42
然而,根据 otool,这个二进制文件仍然具有动态链接:
otool -L ./dyn.out
./dyn.out:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
在告诉 clang 生成未签名的静态二进制文件后,macOS 在尝试运行时立即杀死该二进制文件。
clang -Wl,-e, -Wl,__start -static -nostdlib test.c -o static_no_sign.out
zsh: killed ./static_no_sign.out
在运行之前对二进制文件进行签名也会产生同样的问题。
clang -Wl,-e, -Wl,__start -static -nostdlib test.c -o static_sign.out
codesign -s - static_sign.out
./static_sign.out
zsh: killed ./static_sign.out
控制台中会生成以下消息:
taskgated: UNIX error exception: 3
taskgated: no signature for pid=93166 (cannot make code: UNIX[No such process])
但是codesign可以验证签名
codesign -v -v static_sign.out
static_sign.out: valid on disk
static_sign.out: satisfies its Designated Requirement
谁能澄清为什么 macOS 决定终止 clang 生成的二进制文件?