0

At the middle of this very good article: A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux, the author shows how to directly construct yourself the ELF headers:

BITS 32

              org     0x08048000

ehdr:                                                 ; Elf32_Ehdr
              db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
      times 8 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

_start:
              mov     bl, 42
              xor     eax, eax
              inc     eax
              int     0x80

filesize      equ     $ - $$

He then proceeds to perform some tricky operations, such as moving the _start code in the last 8 bytes of e_ident, and the program assembles fine, using nasm.

I'd like to do the same, but on the aarch64 architecture, using the GAS assembler. Does it support this functionality?

Katoptriss
  • 107
  • 6
  • 1
    Yes and no. Yes, you can of course make the appropriate layout, but it will still be packed into an object file. You can then use other tools such as objcopy to extract it. – Jester May 17 '22 at 21:40
  • More or less a duplicate of [How to generate plain binaries like nasm -f bin with the GNU GAS assembler?](https://stackoverflow.com/q/6828631) which shows the tricky part, getting the bytes of a section into a separate file, not wrapped in metadata. Of course you use different directives, like `.byte`, `.short`, and `.long` instead of `db` / `dw` / `dd`. e.g. [data directive sizes in assembly](https://stackoverflow.com/q/63626874) – Peter Cordes May 17 '22 at 21:55

0 Answers0