Writing very small executable
- Posted by admin
- on Oct, 08, 2020
- in General
- Blog No Comments.
Many years ago I took part in a contest to create a small linux binary that output a specific string
Unfortunately the original contest page no longer exists but the Intenet Archive still holds a copy.
The original rules were quite simple:
- Is a valid ELF image.
- Runs successfully.
- Uses a stack-based approach (Preferably no fastcall binaries).
- Successfully outputs the string: “The deep gray mouse runs after the holy yellow cheese.” with a carriage return after it.
- Returns control to the operating system without causing any segmentation violations.
- The outputted binary does not exceed 2500 bytes.
This was my entry which compiled down to 155 bytes.
; Created by Michael MacDonald
;
; Thanks to: http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
for tips on shrinking the code.
BITS 32
org 0x00001000
ehdr: ; Elf32_Ehdr
db 0x7F, "ELF", 1, 1, 1 ; e_ident
times 9 db 0
dw 2 ; e_type
dw 3 ; e_machine
dd 1 ; e_version
dd _start ; e_entry
dd phdr - $$ ; e_phoff
dd 0 ; e_shoff
dd 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx
ehdrsize equ $ - ehdr
phdr: ; Elf32_Phdr
dd 1 ; p_type
dd 0 ; p_offset
dd $$ ; p_vaddr
dd $$ ; p_paddr
dd filesize ; p_filesz
dd filesize ; p_memsz
dd 5 ; p_flags
dd 0x1000 ; p_align
phdrsize equ $ - phdr
section data
msg db "The deep gray mouse runs after the holy yellow cheese.", 0x0A
len equ $-msg
_start:
mov dl, len ;message length
mov cx, msg ;message to write
mov bl, 1 ;file descriptor (stdout)
mov al, 0x4 ;system call number (sys_write)
int 0x80 ;system call
mov al,1 ;system call number (sys_exit)
int 0x80 ;system call
filesize equ $ - $$
Related
Comments & Responses
Recent Posts
- The shape of structured data
- Writing very small executable
- The challenge of type asserting []interface{} to string slices.
- Serializing/Deserializing a complex GO struct to store externally (i.e. in a database, redis, or text file)
- Fun with golang, Google Maps api, and proxying requests
