-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
220 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
blue | ||
a.out | ||
*.out | ||
|
||
elf_test | ||
elf_test_hello_world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
format elf64 executable 3 | ||
|
||
include "linux.inc" | ||
include "elf.inc" | ||
|
||
segment readable writeable | ||
|
||
program_code: | ||
.entry_offset = $ - program_code | ||
db 0x48, 0xc7, 0xc0 ; mov rax, 1 - sys_write | ||
dd 0x01 | ||
db 0x48, 0xc7, 0xc7 ; mov rdi, 1 - stdout fd | ||
dd 0x01 | ||
db 0x48, 0xc7, 0xc6 ; mov rsi, 0x4000a2 - location of string | ||
dd 0x4000a2 | ||
db 0x48, 0xc7, 0xc2 ; mov rdx, 13 - size of string | ||
dd 0x0d | ||
db 0x0f, 0x05 ; syscall | ||
db 0x48, 0xc7, 0xc0 ; mov rax, 60 - sys_exit | ||
dd 0x3c | ||
db 0x48, 0x31, 0xff ; xor rdi, rdi | ||
db 0x0f, 0x05 ; syscall | ||
|
||
db "Hello, world" | ||
db 0x0a, 0x00 | ||
|
||
.length = $ - program_code | ||
|
||
segment readable executable | ||
|
||
entry $ | ||
mov eax, program_code.entry_offset | ||
mov ecx, program_code.length | ||
call elf_binary_calculate_fields | ||
|
||
mov edi, 1 | ||
cmp qword [elf_header.start_address], 0x400078 | ||
jne exit | ||
|
||
mov edi, 2 | ||
cmp qword [elf_header.section_header_offset], 0xc0 | ||
jne exit | ||
|
||
mov edi, 3 | ||
cmp qword [program_header.size_in_file], 0xb0 | ||
jne exit | ||
|
||
mov edi, 4 | ||
cmp qword [program_header.size_in_memory], 0xb0 | ||
jne exit | ||
|
||
mov edi, 5 | ||
cmp qword [program_code_section_header.address], 0x400078 | ||
jne exit | ||
|
||
mov edi, 6 | ||
cmp qword [program_code_section_header.offset], 0x78 | ||
jne exit | ||
|
||
mov edi, 7 | ||
cmp qword [program_code_section_header.size], 0x38 | ||
jne exit | ||
|
||
mov edi, 8 | ||
cmp qword [shstrtab_section_header.offset], 0xb0 | ||
jne exit | ||
|
||
mov edi, 9 | ||
cmp qword [shstrtab_section_header.size], 0x10 | ||
jne exit | ||
|
||
; | ||
; exit cleanly | ||
; | ||
xor edi, edi | ||
|
||
exit: | ||
mov eax, SYS_EXIT | ||
syscall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
format elf64 executable 3 | ||
|
||
include "linux.inc" | ||
include "elf.inc" | ||
|
||
segment readable writeable | ||
|
||
program_code: | ||
.entry_offset = $ - program_code | ||
db 0x48, 0xc7, 0xc0 ; mov rax, 1 - sys_write | ||
dd 0x01 | ||
db 0x48, 0xc7, 0xc7 ; mov rdi, 1 - stdout fd | ||
dd 0x01 | ||
db 0x48, 0xc7, 0xc6 ; mov rsi, 0x4000a2 - location of string | ||
dd 0x4000a2 | ||
db 0x48, 0xc7, 0xc2 ; mov rdx, 13 - size of string | ||
dd 0x0d | ||
db 0x0f, 0x05 ; syscall | ||
db 0x48, 0xc7, 0xc0 ; mov rax, 60 - sys_exit | ||
dd 0x3c | ||
db 0x48, 0x31, 0xff ; xor rdi, rdi | ||
db 0x0f, 0x05 ; syscall | ||
|
||
db "Hello, world" | ||
db 0x0a, 0x00 | ||
|
||
.length = $ - program_code | ||
|
||
segment readable executable | ||
|
||
output_file: | ||
db "elf_test_hello_world.out" | ||
db 0x00 | ||
|
||
execve_args: | ||
dq output_file | ||
dq 0x0 | ||
|
||
entry $ | ||
mov rdi, output_file | ||
mov esi, 0x01 or 0x40 or 0x200 | ||
mov edx, 0x1ed | ||
mov eax, SYS_OPEN | ||
syscall | ||
|
||
mov rdi, rax | ||
mov eax, program_code.entry_offset | ||
mov ecx, program_code.length | ||
call elf_binary_write | ||
mov eax, SYS_CLOSE | ||
syscall | ||
|
||
mov rdi, output_file | ||
mov rsi, execve_args | ||
xor rdx, rdx | ||
mov eax, SYS_EXECVE | ||
syscall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
SYS_WRITE = 1 | ||
SYS_OPEN = 2 | ||
SYS_CLOSE = 3 | ||
SYS_EXECVE = 59 | ||
SYS_EXIT = 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
TESTS := \ | ||
elf_test \ | ||
elf_test_hello_world | ||
|
||
$(TESTS): | ||
echo "Testing $@... " && make WHAT=$@ compile run && echo "" | ||
|
||
tests: $(TESTS) | ||
@/bin/true | ||
|
||
.PHONY: tests $(TESTS) |