Golfing Zig ELF Binaries (2025)

https://lobste.rs/rss Hits: 19
Summary

index How much can we feasibly strip from a zig binary? Starting from a normal zig program that does absolutely nothing: zig build-exe main.zig -target x86_64-linux-gnu 2180K for a binary that does nothing. Given that the smallest possible executable ELF file is around 80 bytes, 2180K is quite a bit of bloat. What happens when we strip out debug info? zig build-exe main.zig -target x86_64-linux-gnu -fstrip Saved 1988K just by stripping out debugging information. However 192K is still quite far from our 80 byte goal. We are still compiling in Debug mode, so let’s switch to ReleaseSmall (equivalent to -Os for gcc/clang as far as I can tell). zig build-exe main.zig -target x86_64-linux-gnu -fstrip -OReleaseSmall Now we’re at 12K! Saved 180K just by switching from Debug to ReleaseSmall. Next step is to enable function and data sections to allow the linker to strip away unreferenced functions or data. zig build-exe main.zig -target x86_64-linux-gnu -fstrip -OReleaseSmall -ffunction-sections -fdata-sections --gc-sections …and that did nothing. I guess ReleaseSmall already handles this optimization. Taking a peek at the ELF sections shows quite a few unnecessary sections: There are 9 section headers, starting at offset 0x2068: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .rodata PROGBITS 00000000010001c8 000001c8 0000000000000954 0000000000000000 AMS 0 0 8 [ 2] .eh_frame_hdr PROGBITS 0000000001000b1c 00000b1c 00000000000000bc 0000000000000000 A 0 0 4 [ 3] .eh_frame PROGBITS 0000000001000bd8 00000bd8 00000000000003d4 0000000000000000 A 0 0 8 [ 4] .text PROGBITS 0000000001001fac 00000fac 0000000000001041 0000000000000000 AX 0 0 4 [ 5] .tbss NOBITS 0000000001002ff0 00001ff0 000000000000000d 0000000000000000 WAT 0 0 8 [ 6] .bss NOBITS 0000000001004000 00002000 0000000000003108 0000000000000000 WA 0 0 4096 [ 7] .comment PROGBITS 0000000000000000 00002000 000000000000001c 0000000...

First seen: 2026-05-20 15:41

Last seen: 2026-05-21 09:56