The smallest ELF executable (2021)
15 hours ago
- #ELF Minimization
- #Linux Executables
- #Assembly Programming
- A response to community feedback noted that the program size can be reduced to 105 bytes with a full 'Hello, world!' output, crediting Josh Triplett for the 105-byte version.
- The article draws inspiration from a historical article on creating a minimal 45-byte Linux binary, which influenced the author's career by focusing on stripping non-essential components from ELF executables.
- The goal is to create the smallest ELF file that runs on modern Linux (kernel 5.14), using nasm assembler for 64-bit systems, with a focus on minimalism while maintaining functionality.
- ELF files consist of headers, sections, and segments, but for minimal executables, many components like section headers can be omitted, reducing file size significantly.
- An initial 'proper' ELF version with sections was 383 bytes, but removing section information brought it down to 173 bytes, making tools like objdump less functional but still runnable.
- Code optimization reduced the assembly from 39 to 23 bytes by using shorter instructions (e.g., replacing 'mov' with 'xor' and 'inc'), bringing the total to 157 bytes.
- Further minimization involved overlapping code and the 'Hello, world!' string with unused or unvalidated bytes in the ELF and program headers, reducing the file to 126 bytes.
- Final optimization overlapped the ELF header and program header by six bytes, resulting in a 120-byte executable, with an option to reduce to 114 bytes by shortening the string.
- The article concludes that while a 45-byte executable from the past is no longer feasible due to stricter ELF loading in modern Linux, 120 bytes represents a significant achievement in minimizing bloat.