I'm trying to learn about the elf standard by writing an elf from scratch. Elf32 does not present a big problem using this code:
BITS 32
org 0x08048000
ehdr: ; Elf32_Ehdr
db 0x7F, "ELF", 1, 1, 1, 2 ; 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 $ - $$
Source: http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
I can compile this into an executable with nasm, and it runs just fine:
$ nasm -f bin -o test32 template32.asm
$ chmod +x test32
$ ./test32 ; echo $?
42
Then I tried to do the same with 64 bit. I read the differences that I found here: https://www.uclibc.org/docs/elf-64-gen.pdf
Here is the result of the changes:
BITS 64
org 0x08048000
ehdr: ; Elf64_Ehdr
db 0x7F, "ELF", 2, 1, 1, 2 ; e_ident
times 7 db 0
db 0x10 ; e_nindent
dw 2 ; e_type
dw 3 ; e_machine
dd 1 ; e_version
dq _start ; e_entry
dq phdr - $$ ; e_phoff
dq 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: ; Elf64_Phdr
dd 1 ; p_type
dd 5 ; p_flags
dq 0 ; p_offset
dq $$ ; p_vaddr
dq $$ ; p_paddr
dq filesize ; p_filesz
dq filesize ; p_memsz
dq 0x1000 ; p_align
phdrsize equ $ - phdr
_start:
mov bl, 42
xor eax, eax
inc eax
int 0x80
filesize equ $ - $$
Using the same commands as above, I get this error:
./test64: cannot execute binary file: Exec format error
-, : test32 test64 , . , ... 010 ELF, .
EDIT:
32- :
7f45 4c46 0101 0102 0000 0000 0000 0000
0200 0300 0100 0000 5480 0408 3400 0000
0000 0000 0000 0000 3400 2000 0100 0000
0000 0000 0100 0000 0000 0000 0080 0408
0080 0408 5b00 0000 5b00 0000 0500 0000
0010 0000 b32a 31c0 40cd 80
64- :
7f45 4c46 0201 0102 0000 0000 0000 0010
0200 0300 0100 0000 7880 0408 0000 0000
4000 0000 0000 0000 0000 0000 0000 0000
0000 0000 4000 3800 0100 0000 0000 0000
0100 0000 0500 0000 0000 0000 0000 0000
0080 0408 0000 0000 0080 0408 0000 0000
8000 0000 0000 0000 8000 0000 0000 0000
0010 0000 0000 0000 b32a 31c0 ffc0 cd80