; tinytrue64.asm - by Pegasus Epsilon <pegasus@pimpninjas.org>
; (C) 2013 Distribute Unmodified
;
; build with nasm tinytrue64.asm -o tinytrue64 && chmod +x tinytrue64
; result should be 120 bytes
;
; TODO:
; make it check how it's being called and change its result accordingly
; so you can call it as "true" or "false" and get the right answer
;
; Note that this won't help anything on any filesystem without tails
; because any file occupies at least a full block on those filesystems,
; and this file's resulting binary is far tinier than a single block in
; any filesystem or drive that I've ever seen.
BITS 64
org 0x400000
elf_ident:
db 0x7F, "ELF" ; EI_MAG = ELF magic
db 2 ; EI_CLASS = ELFCLASS64 (elf64) -- clobberable
db 1 ; EI_DATA = ELFDATA2LSB (little-endian) -- clobberable
db 1 ; EI_VERSION = EV_CURRENT -- clobberable
db 0 ; null terminator -- clobberable
db 0 ; ABI version -- clobberable
db 0 ; alignment padding -- clobberable
start:
; eax = 0
inc eax ; exit with errorlevel
; ebx = 0, errorlevel = 0, true
int 0x80 ; syscall
dw 0 ; alignment padding -- two bytes left over -- clobberable
; 12 bytes total clobberable up here, but only
; 7 that won't break inspection tools (like readelf or objdump)
elf_hdr:
dw 2 ; Elf64_Half e_type = ET_EXEC
dw 0x3E ; Elf64_Half e_machine = AMD x64
dd 1 ; Elf64_Word e_version = 1
dq start ; Elf64_Addr e_entry = start offset
dq prog_hdr - $$ ; Elf64_Off e_phoff = prog_hdr offset
dq 0 ; Elf64_Off e_shoff = 0 (we don't have one)
dd 0 ; Elf64_Word e_flags = 0 (nothing special, i guess)
dw prog_hdr - elf_ident ; Elf64_Half e_ehsize = elf_hdr_size
dw end - prog_hdr ; Elf64_Half e_phentsize
dw 1 ; Elf64_Half e_phnum = 1 (we only need one)
; leaving these three words out saves 6 bytes and lets the headers overlap
; it doesn't break anything when run, but inspection tools freak out
; dw 0x40 ; Elf64_Half e_shentsize = 64 (required)
; dw 0 ; Elf64_Half e_shnum = 0 (we don't have any)
; dw 0 ; Elf64_Half e_shstrndx = 0 (we still don't have any)
prog_hdr:
dd 1 ; Elf64_Word p_type = 1
dd 5 ; Elf64_Word p_flags = PF_R | PF_X
dq 0 ; Elf64_Off p_offset = 0
dq $$ ; Elf64_Addr p_vaddr = org
dq $$ ; Elf64_Addr p_paddr = org
dq end - $$ ; Elf64_Xword p_filesz = size of file
dq end - $$ ; Elf64_Xword p_memsz = size of file
; this quadword is not used, but must be included anyway to finish out
; the prog_hdr. shame you can't overlap them on x64 like you can on x86.
dq 0x200000 ; Elf64_Xword p_align
end: ; nothing allowed after this line