Skip to content

Commit

Permalink
Don't assume label for code in elf.inc (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbirddog authored Jun 17, 2024
1 parent ea894ef commit 9a20d2f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions blue.asm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ entry $
syscall

mov rdi, rax
mov rsi, program_code
mov eax, program_code.entry_offset
mov ecx, program_code.length
call elf_binary_write
Expand Down
14 changes: 8 additions & 6 deletions elf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ shstrtab_section_header:
.length = $ - shstrtab_section_header
assert .length = 0x40
elf_binary_length = $ - elf_binary
elf_binary_wrapper_length = $ - elf_binary
elf_binary_headers_length = elf_header.length + program_header.length
elf_binary_section_headers_length = elf_binary_length - elf_binary_section_headers
elf_binary_section_headers_length = $ - elf_binary_section_headers
elf_binary_section_headers_offset = elf_binary_section_headers - elf_binary

segment readable executable
Expand Down Expand Up @@ -145,11 +145,14 @@ elf_binary_calculate_fields:
ret
;
; expects
; - program code start in rsi
; - program code entry offset in eax
; - program code length in ecx
; - file descriptor in edi
;
elf_binary_write:
push rsi
push rcx
push rdi
call elf_binary_calculate_fields
pop rdi
Expand All @@ -159,8 +162,9 @@ elf_binary_write:
mov eax, SYS_WRITE
syscall

mov rsi, program_code
mov rdx, program_code.length
pop rcx
pop rsi
mov edx, ecx
mov eax, SYS_WRITE
syscall

Expand All @@ -175,5 +179,3 @@ elf_binary_write:
syscall

ret

23 changes: 23 additions & 0 deletions elf_test_hello_world.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ include "elf.inc"

segment readable writeable

fstat_buffer:
rb 48
.file_size:
rq 1
rb 88

program_code:
.entry_offset = $ - program_code
db 0x48, 0xc7, 0xc0 ; mov rax, 1 - sys_write
Expand All @@ -28,6 +34,10 @@ program_code:

segment readable executable

expected_output_size = elf_binary_wrapper_length + program_code.length

assert expected_output_size = 384

output_file:
db "elf_test_hello_world.out"
db 0x00
Expand All @@ -44,9 +54,17 @@ entry $
syscall

mov rdi, rax
mov rsi, program_code
mov eax, program_code.entry_offset
mov ecx, program_code.length
call elf_binary_write

mov rsi, fstat_buffer
mov eax, SYS_FSTAT
syscall
cmp qword [fstat_buffer.file_size], expected_output_size
jne failure
mov eax, SYS_CLOSE
syscall
Expand All @@ -56,3 +74,8 @@ entry $
xor rdx, rdx
mov eax, SYS_EXECVE
syscall

failure:
mov rdi, 1
mov eax, SYS_EXIT
syscall
1 change: 1 addition & 0 deletions linux.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
SYS_WRITE = 1
SYS_OPEN = 2
SYS_CLOSE = 3
SYS_FSTAT = 5
SYS_EXECVE = 59
SYS_EXIT = 60

0 comments on commit 9a20d2f

Please sign in to comment.