diff --git a/blue.asm b/blue.asm index 74b822a..b33359b 100644 --- a/blue.asm +++ b/blue.asm @@ -1,13 +1,13 @@ format elf64 executable 3 -include "elf.inc" - segment readable writeable include "defs.inc" +include "elf_template.inc" segment readable executable +include "elf.inc" include "linux.inc" include "code_buffer.inc" diff --git a/elf.inc b/elf.inc index 3a3c0ab..751ef63 100644 --- a/elf.inc +++ b/elf.inc @@ -1,113 +1,4 @@ -segment readable writeable - -; -; adapted from https://kevinboone.me/elfdemo.html -; - -elf_binary: - .base_address = 0x400000 - -elf_binary_headers: -elf_header: - db 0x7f, 0x45, 0x4c, 0x46 ; magic number - db 0x02 ; 64 bit - db 0x01 ; little endian - db 0x01 ; elf version - db 0x00 ; target abi - dq 0x00 ; target abi version + 7 bytes undefined - dw 0x02 ; executable binary - dw 0x3e ; amd64 architecture - dd 0x01 ; elf version - .start_address: - dq -0x01 ; start address - dq 0x40 ; offset to program header - .section_header_offset: - dq -0x01 ; offset to section header - dd 0x00 ; architecture flags - dw 0x40 ; size of header - dw 0x38 ; size of program header - dw 0x01 ; number of program headers - dw 0x40 ; size of section header - dw 0x03 ; number of section headers - dw 0x02 ; index of strtab section header - - .length = $ - elf_header - assert .length = 0x40 - -program_header: - dd 0x01 ; entry type: loadable segment - dd 0x05 ; segment flags: RX - dq 0x00 ; offset within file - dq elf_binary.base_address ; load position in virtual memory - dq elf_binary.base_address ; load position in physical memory - .sizes: - .size_in_file: - dq -0x01 ; size of the loaded section (file) - .size_in_memory: - dq -0x01 ; size of the loaded section (memory) - dq 0x200000 ; alignment boundary for sections - - .length = $ - program_header - assert .length = 0x38 - -shstrtab: - db ".shstrtab" - db 0x00 - db ".text" - db 0x00 - - .length = $ - shstrtab - assert .length = 0x10 - -elf_binary_section_headers: -section_header_0: - dq 0x00, 0x00, 0x00, 0x00 ; 64 bytes of 0s - dq 0x00, 0x00, 0x00, 0x00 - - .length = $ - section_header_0 - assert .length = 0x40 - -program_code_section_header: - dd 0x0a ; offset to name in shstrtab - dd 0x01 ; type: program data - dq 0x06 ; flags - executable | in memory - .address: - dq -0x01 ; addr in virtual memory of section - .offset: - dq -0x01 ; offset in the file of this section - .size: - dq -0x01 ; size of this section in the file - dq 0x00 ; sh_link - not used - dq 0x01 ; alignment code (default??) - dq 0x00 ; sh_entsize - not used - - .length = $ - program_code_section_header - assert .length = 0x40 - -shstrtab_section_header: - dd 0x00 ; offset to name in shstrtab - dd 0x03 ; type: string table - dq 0x00 ; flags - none - dq 0x00 ; addr in virtual memory of section - not used - .offset: - dq -0x01 ; offset in the file of this section - .size: - dq -0x01 ; size of this section in the file - dq 0x00 ; sh_link - not used - dq 0x01 ; alignment code (default??) - dq 0x00 ; sh_entsize - not used - - .length = $ - shstrtab_section_header - assert .length = 0x40 - -elf_binary_wrapper_length = $ - elf_binary -elf_binary_headers_length = elf_header.length + program_header.length -elf_binary_section_headers_length = $ - elf_binary_section_headers -elf_binary_section_headers_offset = elf_binary_section_headers - elf_binary - -segment readable executable - ; ; expects ; - program code entry offset in eax diff --git a/elf_template.inc b/elf_template.inc new file mode 100644 index 0000000..7f4b424 --- /dev/null +++ b/elf_template.inc @@ -0,0 +1,105 @@ + +; +; adapted from https://kevinboone.me/elfdemo.html +; + +elf_binary: + .base_address = 0x400000 + +elf_binary_headers: +elf_header: + db 0x7f, 0x45, 0x4c, 0x46 ; magic number + db 0x02 ; 64 bit + db 0x01 ; little endian + db 0x01 ; elf version + db 0x00 ; target abi + dq 0x00 ; target abi version + 7 bytes undefined + dw 0x02 ; executable binary + dw 0x3e ; amd64 architecture + dd 0x01 ; elf version + .start_address: + dq -0x01 ; start address + dq 0x40 ; offset to program header + .section_header_offset: + dq -0x01 ; offset to section header + dd 0x00 ; architecture flags + dw 0x40 ; size of header + dw 0x38 ; size of program header + dw 0x01 ; number of program headers + dw 0x40 ; size of section header + dw 0x03 ; number of section headers + dw 0x02 ; index of strtab section header + + .length = $ - elf_header + assert .length = 0x40 + +program_header: + dd 0x01 ; entry type: loadable segment + dd 0x05 ; segment flags: RX + dq 0x00 ; offset within file + dq elf_binary.base_address ; load position in virtual memory + dq elf_binary.base_address ; load position in physical memory + .sizes: + .size_in_file: + dq -0x01 ; size of the loaded section (file) + .size_in_memory: + dq -0x01 ; size of the loaded section (memory) + dq 0x200000 ; alignment boundary for sections + + .length = $ - program_header + assert .length = 0x38 + +shstrtab: + db ".shstrtab" + db 0x00 + db ".text" + db 0x00 + + .length = $ - shstrtab + assert .length = 0x10 + +elf_binary_section_headers: +section_header_0: + dq 0x00, 0x00, 0x00, 0x00 ; 64 bytes of 0s + dq 0x00, 0x00, 0x00, 0x00 + + .length = $ - section_header_0 + assert .length = 0x40 + +program_code_section_header: + dd 0x0a ; offset to name in shstrtab + dd 0x01 ; type: program data + dq 0x06 ; flags - executable | in memory + .address: + dq -0x01 ; addr in virtual memory of section + .offset: + dq -0x01 ; offset in the file of this section + .size: + dq -0x01 ; size of this section in the file + dq 0x00 ; sh_link - not used + dq 0x01 ; alignment code (default??) + dq 0x00 ; sh_entsize - not used + + .length = $ - program_code_section_header + assert .length = 0x40 + +shstrtab_section_header: + dd 0x00 ; offset to name in shstrtab + dd 0x03 ; type: string table + dq 0x00 ; flags - none + dq 0x00 ; addr in virtual memory of section - not used + .offset: + dq -0x01 ; offset in the file of this section + .size: + dq -0x01 ; size of this section in the file + dq 0x00 ; sh_link - not used + dq 0x01 ; alignment code (default??) + dq 0x00 ; sh_entsize - not used + + .length = $ - shstrtab_section_header + assert .length = 0x40 + +elf_binary_wrapper_length = $ - elf_binary +elf_binary_headers_length = elf_header.length + program_header.length +elf_binary_section_headers_length = $ - elf_binary_section_headers +elf_binary_section_headers_offset = elf_binary_section_headers - elf_binary diff --git a/elf_test.asm b/elf_test.asm index ec5a0f7..fc4e61b 100644 --- a/elf_test.asm +++ b/elf_test.asm @@ -1,9 +1,12 @@ format elf64 executable 3 -include "elf.inc" +segment readable writeable + +include "elf_template.inc" segment readable executable +include "elf.inc" include "linux.inc" program_code: diff --git a/elf_test_hello_world.asm b/elf_test_hello_world.asm index 2b125f8..1e853ff 100644 --- a/elf_test_hello_world.asm +++ b/elf_test_hello_world.asm @@ -1,16 +1,17 @@ format elf64 executable 3 -include "linux.inc" -include "elf.inc" - segment readable writeable +include "elf_template.inc" + fstat_buffer: rb 48 .file_size: rq 1 rb 88 +segment readable executable + program_code: .entry_offset = $ - program_code db 0x48, 0xc7, 0xc0 ; mov rax, 1 - sys_write @@ -32,7 +33,8 @@ program_code: .length = $ - program_code -segment readable executable +include "elf.inc" +include "linux.inc" expected_output_size = elf_binary_wrapper_length + program_code.length