elf32.s 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Custom ELF header for the binary */
  2. /* Inspired by http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html */
  3. /* via https://github.com/kmcallister/tiny-rust-demo */
  4. /* via https://github.com/def-/nim-binary-size */
  5. ehdr:
  6. .byte 0x7f, 0x45, 0x4C, 0x46 /* ELFMAG */
  7. .byte 0x01 /* EI_CLASS = ELFCLASS32 */
  8. .byte ELF_data /* EI_DATA = ELFDATA2LSB or ELFDATA2MSB */
  9. .byte 0x01 /* EI_VERSION = EV_CURRENT */
  10. .byte 0x00 /* EI_OSABI = ELFOSABI_SYSV */
  11. /* This padding is a perfect place to put a string constant! */
  12. hello: .asciz "Hello!\n" /* must be *exactly* 8 bytes long */
  13. .short 2 /* e_type = executable */
  14. .short ELF_machine /* e_machine */
  15. .long 1 /* e_version */
  16. .long _start /* e_entry */
  17. .long phdr-ehdr /* e_phoff */
  18. .long 0 /* e_shoff */
  19. .long 0 /* e_flags */
  20. .short ehdrsize /* e_ehsize */
  21. .short phdrsize /* e_phentsize */
  22. .ifeq ELF_data - 2
  23. /* collapse-ehdr-onto-phdr trick does not work on bigendian arches */
  24. .short 1 /* e_phnum */
  25. .short 0 /* e_shentsize */
  26. .short 0 /* e_shnum */
  27. .short 0 /* e_shstrndx */
  28. .endif
  29. phdr:
  30. .long 1 /* p_type = loadable program segment & e_phnum,e_sh* */
  31. .long 0 /* p_offset */
  32. .long orig /* p_vaddr */
  33. .long orig /* p_paddr */
  34. .long filesize /* p_filesz */
  35. .long filesize /* p_memsz */
  36. .long 7 /* p_flags */
  37. .long 0x1000 /* p_align */
  38. .ifeq ELF_data - 2
  39. .equ ehdrsize, phdr-ehdr
  40. .else
  41. .equ ehdrsize, phdr-ehdr+8 /* including phdr overlap */
  42. .endif
  43. .equ phdrsize, .-phdr
  44. .equ length, 7
  45. .globl hello
  46. .globl length