Skip to content

Instantly share code, notes, and snippets.

@MestreLion
Last active November 26, 2024 11:41
Show Gist options
  • Save MestreLion/c9456e4523c145fa9d62fb67512cfe6e to your computer and use it in GitHub Desktop.
Save MestreLion/c9456e4523c145fa9d62fb67512cfe6e to your computer and use it in GitHub Desktop.
127-byte Tiny Teensy X86_64 compliant ELF using standard tools
# tiny.s
# 127-byte fully compliant X86-64 ELF64 binary
# (the best you can get without handcrafting ELF headers)
# Inspired by:
# - https://www.muppetlabs.com/~breadbox/software/tiny/
# - https://ech0.re/building-the-smallest-elf-program/
# - https://nullprogram.com/blog/2016/11/17/
# See also:
# - https://stackoverflow.com/q/79225538/624066
# Restrictions:
# - True x86_64 ELF64 binary, not 32bit-binary-on-64bit-platform
# - No handcrafting ELF headers in ASM
# - No hex editing or directly manipulating binaries
# - Only standard GNU tools (nasm, ld, gcc, etc)
# Assumptions:
# - Zeroed registers at start, guaranteed by Linux ABI
# To "compile":
# - as tiny.s -o tiny.o # 624 bytes
# - ld -s -no-pie -z noseparate-code tiny.o -o tiny # 336 bytes
# (~13K without the flags!!!)
# - strip --strip-section-headers tiny # 127 bytes
.GLOBL _start
_start:
# _exit(42)
mov $60, %al # Select the _exit syscall (60 in Linux x64 ABI)
mov $42, %dil # Set the exit code argument for _exit
syscall # Perform the selected syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment