๐๏ธ Binary golfing - Introduction

So tmp.out - once again - got me. Especially netspooky, which wrote about golfing binaries. I was instantly caught in golfing.
Binary golfing?
Crafting the smallest binary which does a particular task.
Why someone would do this?
- Learn about binary executables and format parsers
- Flex on muggles
My notes on ELF format
It is recommanded to code in assembly in order to manage your headers as you wish.
We can handcraft binaries, because GCC
is a bit messy when compiling code and linking stuff. We can then make sections or headers overlap to save more space.
In brief, the section header table is for use by the compiler and linker, while the program header table is for use by the program loader. The program header table is optionnal and never present in practice, the section header table is also optional but always present.
- Brian Raiter, “A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux”
Compile
Classic way:
|
|
Better:
|
|
To directly craft a binary from NASM file.
Header
The smallest valid header I can think of might be:
|
|
Considering the following:
??
: garbage, so you can just use those bytes for your code!!!! !!!!
: total size of the header, so it will depend on your codedead beef
: address of the entrypoint
Here is a template I modified from here, so all creds to the author:
|
|
And the version for 64 bits (taken from here):
|
|
Unethical stuff
Declaring variables in the wild
๐ Nothing forbidens to declare variables anywhere, to save some space you can skip using the .rodata
section.
|
|
Use header as code section
๐ง Big brain move here: put code in the header
|
|
Golfing resources
A while ago I created a repo containing some random assembly programs I did, I added my try to make a tiny Hello world
binary: https://github.com/OxNinja/nasm_/tree/main/elf-golfing
https://codegolf.stackexchange.com/questions/5696/shortest-elf-for-hello-world-n