【发布时间】:2021-06-26 03:49:36
【问题描述】:
我已经编译了
#include <stdio.h>
int main() {
printf("Hello world");
return 0;
}
在 Mac 上,大小为 48k。但是,当我使用xxd 查看二进制文件时,大部分看起来是这样的:
...
0000b990: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000b9a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000b9b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
...
为什么会这样?
otool 告诉我:
otool -L hello
hello:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.0.0)
太好了,它再次动态链接 libSystem,它在 printf 所在的位置。
那为什么都是零呢?
【问题讨论】:
-
大部分可能是用零填充的4k页面对齐
-
可能相关:关于具有大量填充的最小可执行文件的 linux Q&A 解释了链接器选择这样做的一些原因:Minimal executable size now 10x larger after linking than 2 years ago, for tiny programs?
-
是否有编译器选项可以关闭它?例如获得尽可能小的可执行文件?
-
@scrrr 您必须手动编写可执行文件才能真正突破限制(现代 MacOS 中为 4k)。 stackoverflow.com/a/32659692/5329717 你不会完全摆脱零。
标签: c macos assembly executable mach-o