Haha turns out you can save a third of the bytes still, I've edited my post. I don't think you can push much further beyond without overlapping program header into the unused tail of the ELF header, but that'd require hand-authoring bytes that satisfy both layouts at one i.e. manual patching.
not to mention APE, a POSIX-approved polyglot format that runs natively (with Cosmopolitan Libc) on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS on AMD64 and ARM64 with the best possible performance
Idk of this really counts since it's completely bypassing C compilation and just using gas. If the goal was to use gcc then there should at least be C compilation.
Looking at it a bit more, ld -z nosectionheader allows us to go down to 180B. Getting to 124 requires us to not produce the STACK program header (56B) which i don't think is possible without a custom linker script.
400B is A LOT. A few days ago I tried to do this just to see how many "tricks" I still know. You do need a custom linker script to properly trim the fat, though. I got to ~200B and I see /u/SharkyKesa564 beat my attempt, so I won't even try to brag.
127B is the theoretical minimum if you don't start manually packing bytes. Did you know that the ELF specification allows sections to overlap? There's enough room for the exit syscall to fit in the padding of the ELF header.
You can probably drop the base pointer with the right attributes/arguments, though you might get them back clearing edi; I'm not sure only setting dil is "deterministically" safe. The assembly also shows that dil is set by a mov though, so I'm not sure what's up with that.
Also, technically you can use pure assembly since gcc can compile it directly.
EDIT: This uses some dirty tricks, exploiting everythingn the linux kernel allows, like p_filesz being ignored, or p_memsz being too large for actual needs, just to overlap all of, the ELF header, the single program header, and the 7 bytes of actual instructions.
Don't know about ELF, but for a Windows .exe using NASM/YASM and ALINK, I get 1036 bytes (no data or bss sections, just code with a ret, but I can't seem to cull the reloc section).
It's not about the end state, it's about the journey. While getting to the smallest binary, if you learnt a bit about how ELFs structured, or that xor is encoded differently from mov, or some compiler flag you never heard about, you've learnt something you'd never be otherwise exposed to.
•
u/cpp-ModTeam Jun 06 '26
Interesting but off-topic for this subreddit.
72
u/SharkyKesa564 Jun 06 '26 edited Jun 06 '26
You can get 124B as follows:
tiny.c (raw asm so the compiler adds zero padding)
__asm__(".globl _start\n_start: mov $60,%al; syscall");tiny.ld (one
PT_LOADthat contains the ELF + program headers, so code follows with no page gap)ENTRY(_start)PHDRS { all PT_LOAD FILEHDR PHDRS FLAGS(5); }SECTIONS {. = 0x10000 + SIZEOF_HEADERS;.text : { *(.text .text.*) } :all/DISCARD/ : { *(.*) }}Build with
gcc tiny.c -nostdlib -nostartfiles -static -no-pie -fno-asynchronous-unwind-tables -fcf-protection=none -fno-stack-protector -Wl,--build-id=none -Wl,-T,tiny.ld -Wl,--no-dynamic-linker -Wl,-z,noseparate-code -Wl,-z,norelro -Wl,-z,nosectionheader -Wl,--strip-all -o a.outThis does rely on the Linux ABI guarantee that all GP registers except
%rsp/%rcxare zero at_start, so%rdiis already 0. Deterministic on Linux.However, if you want it to work regardless of entry state, prepend
xor %edi,%ediwhich makes it 126B.------
EDIT: Turns out you can do a lot better with a few more tricks:
tiny.c (empty)
anchor.s
.section .anchor,"ax"tiny.ld
ENTRY(_start)PHDRS { all PT_LOAD FILEHDR PHDRS FLAGS(5) AT(0x80cd01b0); }SECTIONS {. = 0x10000 + SIZEOF_HEADERS;.text : { *(.text .text.*) } KEEP(*(.anchor)) } :all_start = 0x10000 + 64;/DISCARD/ : { *(.*) }}Build with
gcc -m32 tiny.c anchor.s -nostdlib -nostartfiles -no-pie -fno-asynchronous-unwind-tables -fcf-protection=none -fno-stack-protector -Wl,--build-id=none -Wl,-T,tiny.ld -Wl,--no-dynamic-linker -Wl,-z,noseparate-code -Wl,-z,norelro -Wl,-z,nosectionheader -Wl,--strip-all -o a.outThis gets you down to 84 bytes. For x86-64, drop
-m32, useAT(0x050f3cb0)and_start = 0x10000 + 88, which gives a 120B build.31
u/_bstaletic Jun 06 '26
400B is A LOT. A few days ago I tried to do this just to see how many "tricks" I still know. You do need a custom linker script to properly trim the fat, though. I got to ~200B and I see /u/SharkyKesa564 beat my attempt, so I won't even try to brag.
127B is the theoretical minimum if you don't start manually packing bytes. Did you know that the ELF specification allows sections to overlap? There's enough room for the
exitsyscall to fit in the padding of the ELF header.Check out: https://github.com/tchajed/minimal-elf
The readme links to a ~50B i386 ELF, explains the 127B amd64 ELF in C and contains an amd64 ELF in rust.
6
u/kalmoc Jun 06 '26
Quite interesting writeup, but is that really still a C-Program. Seems like "the smallest Linux/gcc/elf binary" seems more appropriate.
3
u/darkmx0z Jun 06 '26
Related: the smallest PE (windows format) executable: http://www.phreedom.org/research/tinype/
1
u/TheRealSmolt Jun 06 '26
You can probably drop the base pointer with the right attributes/arguments, though you might get them back clearing
edi; I'm not sure only settingdilis "deterministically" safe. The assembly also shows thatdilis set by a mov though, so I'm not sure what's up with that.Also, technically you can use pure assembly since gcc can compile it directly.
1
u/max0x7ba https://github.com/max0x7ba Jun 06 '26
The author is unaware that support for
a.outformat was long removed from the toolchains and Linux kernel.a.outis actually an ELF file.26
u/[deleted] Jun 06 '26 edited Jun 06 '26
[removed] — view removed comment
-10
u/[deleted] Jun 06 '26
[removed] — view removed comment