pull down to refresh

I thought of a cute problem: what is the smallest (size) ./a.out binary I can create?

Here are some rules the program should follow:
  • ./a.out must run successfully.
  • $? must deterministically be 0.
  • The binary must be produced by GCC only; no post-processing with objcopy, hex editors, or manual patching.


We begin with the simplest program possible:
// compiled with gcc empty.c
int main() {
return 0;
}
This gives us a file size of 15816 bytes (from stat). Not too shabby, but we will need four of the RAM used in the Apollo guidance computer to fit our binary that does nothing.

Looking at file:
❯ file a.out
a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter > /nix/store/jms7zxzm7w1whczwny5m3gkgdjghmi2r-glibc-2.42-51/lib/ld-linux-x86-64.so.2, for GNU/Linux 3.10.0, not stripped
not stripped looks suspicious. Whatever it is, surely it is better if we can strip stuff out of our binary. It turns out that gcc provides a -s flag that compiles the code without retaining any debugging information. We are now at 14352 bytes with our code stripped.

...read more at blog.weineng.me