I thought of a cute problem: what is the smallest (size)./a.outbinary I can create?
Here are some rules the program should follow:
./a.outmust run successfully.$?must deterministically be0.- 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 (fromstat). 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 atfile:❯ 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 strippednot strippedlooks suspicious. Whatever it is, surely it is better if we can strip stuff out of our binary. It turns out thatgccprovides a-sflag 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
pull down to refresh
related posts