Created
June 25, 2025 20:43
-
-
Save rrampage/74586d0a0a451f43b546b169d460cb96 to your computer and use it in GitHub Desktop.
Handcrafted Elf Binary from assembly (without linker)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Teensy Elf using GNU as and objcopy | |
as -o teensy.o teensy.S && objcopy -O binary teensy.o teensy.bin && chmod +x teensy.bin | |
References: | |
Elf Format details: https://gist.github.com/x0nu11byt3/bcb35c3de461e5fb66173071a2379779 | |
Build a Teensy Elf (x86) file: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html | |
Adding Elf headers using GNU as: https://stackoverflow.com/q/77244360 | |
*/ | |
.equ ADDR, 0x400000 | |
.section .rodata | |
.global _start | |
ehdr: | |
.byte 0x7F | |
.byte 0x45 // E | |
.byte 0x4c // L | |
.byte 0x46 // F | |
.byte 0x02 // 64 bit ; CLASS | |
.byte 0x01 // lsb ; DATA | |
.byte 0x01 // ; VERSION | |
.byte 0x00 // None/Sytem V ; OS ABI | |
.8byte 0x0 // ABI VERSION + PADDING | |
.2byte 0x02 // ET_EXEC ; E_TYPE | |
.2byte 0xb7 // ARM64 ; E_MACHINE | |
.4byte 0x01 // 1 ; E_VERSION | |
.8byte ADDR + start_offset // ; E_ENTRY | |
.8byte phdr - ehdr // offset into program header ; E_PHOFF | |
.8byte 0x00 // offset into section header ; E_SHOFF | |
.4byte 0x00 // flag ; E_FLAGS | |
.2byte ehdr_size // ELF header size ; E_EHSIZE | |
.2byte phdr_size // Program header size ; E_PHSIZE | |
.2byte 0x01 // Number of program headers ; E_PHNUM | |
.2byte 0x00 // Section header size ; E_SHENTSIZE | |
.2byte 0x00 // Number of section headers ; E_SHNUM | |
.2byte 0x00 // Section header string table index ; E_SHSTRNDX | |
DECLARE_ELF_HEADER_SIZE: | |
.set ehdr_size, DECLARE_ELF_HEADER_SIZE - ehdr | |
phdr: | |
.4byte 0x01 // PT_LOAD ; P_TYPE | |
.4byte 5 // R-X ; P_FLAGS | |
.8byte 0 // located at offset 0 ; P_OFFSET | |
.8byte ADDR // ; P_VADDR | |
.8byte ADDR // ; P_PADDR | |
.8byte file_size // ; P_FILESIZE | |
.8byte file_size // ; P_MEMSZ | |
.8byte 0x1000 // P_ALIGN | |
DECLARE_PHEADER_SIZE: | |
.set phdr_size, DECLARE_PHEADER_SIZE - phdr | |
DECLARE_START_OFFSET: | |
.set start_offset, DECLARE_START_OFFSET - ehdr | |
.org start_offset | |
_start: | |
mov x0, #42 /* status := 42!! */ | |
mov w8, #93 /* exit is syscall #93 */ | |
svc #0 /* invoke syscall */ | |
DECLARE_FILE_SIZE: | |
.set file_size, DECLARE_FILE_SIZE - ehdr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment