From 18a35c1c6dcecac78a257ba72057b16bde926497 Mon Sep 17 00:00:00 2001 From: xolatile Date: Tue, 16 Jul 2024 19:42:26 -0400 Subject: [PATCH] Normal ELF64 headers without hacks... --- source/assembler.c | 5 +- source/assembler.h | 3 +- source/compile.c | 8 ++-- source/unix.c | 25 +++++----- source/unix.h | 6 +-- test/simple_procedure.asm | 97 +++++++++++++++++++++++++++++++++++++++ test/simple_procedure.eax | 32 ++++++------- 7 files changed, 138 insertions(+), 38 deletions(-) create mode 100644 test/simple_procedure.asm diff --git a/source/assembler.c b/source/assembler.c index 2287d70..49576e6 100644 --- a/source/assembler.c +++ b/source/assembler.c @@ -347,7 +347,8 @@ static void assemble_clean_up (void) { next text_sector_size = 0; byte * text_sector_byte = NULL; -int was_instruction_array_empty = 0; + int was_instruction_array_empty = 0; +unsigned int text_entry_point = 0; void assemble (next count, next * array) { @@ -433,6 +434,8 @@ void assemble (next count, } } + text_entry_point = empty_store [0]; + for (index = 0; index < empty_holes; ++index) { next set = 0, get = empty_array [index]; memcpy (& set, & text_sector_byte [get], sizeof (set)); diff --git a/source/assembler.h b/source/assembler.h index 9190c45..9fff3cb 100644 --- a/source/assembler.h +++ b/source/assembler.h @@ -71,7 +71,8 @@ typedef enum { extern next text_sector_size; extern byte * text_sector_byte; -extern int was_instruction_array_empty; +extern int was_instruction_array_empty; +extern unsigned int text_entry_point; extern void assemble (next count, next * array); diff --git a/source/compile.c b/source/compile.c index dd28174..861de98 100644 --- a/source/compile.c +++ b/source/compile.c @@ -54,9 +54,9 @@ void dump_variables_to_assembler(void) { static int write_output(FILE * file) { // XXX Where can i move these? - elf_main_header (1, 1, 1, 0); - elf_text_sector (text_sector_size); - elf_data_sector (text_sector_size, 12); + elf_main_header (1, 1, 1); + elf_text_sector (text_sector_size, 0x27); // HACK + elf_data_sector (text_sector_size, 0x27); // HACK checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file); checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file); @@ -71,7 +71,7 @@ int write_output(FILE * file) { static int make_executable(const char * const filename) { int r = 0; - + #if defined(__unix__) r = chmod(filename, 0755); #endif diff --git a/source/unix.c b/source/unix.c index 270cac8..5bee97b 100644 --- a/source/unix.c +++ b/source/unix.c @@ -1,3 +1,4 @@ +#include "assembler.h" #include "unix.h" uint8_t elf_main_header_byte [ELF_MAIN_HEADER_SIZE] = { @@ -33,23 +34,21 @@ uint8_t elf_data_sector_byte [ELF_DATA_SECTOR_SIZE] = { void elf_main_header (uint8_t has_program, uint8_t for_linux, - uint8_t for_x86_64, - uint64_t entry_point) { - /* */ - uint64_t enter = entry_point; // TEST + uint8_t for_x86_64) { + // + uint32_t enter = text_entry_point + 0x4000b0u; elf_main_header_byte [16] = (has_program) ? 0x02 : 0x03; // library elf_main_header_byte [ 7] = (for_linux) ? 0x03 : 0x00; // system v elf_main_header_byte [18] = (for_x86_64) ? 0x3e : 0x00; - if (entry_point != 0) { - memcpy (& elf_main_header_byte [24], & enter, sizeof (enter)); - } + memcpy (& elf_main_header_byte [24], & enter, sizeof (enter)); } -void elf_text_sector (uint64_t text_size) { - /* */ - uint64_t text = ELF_HEADER_SIZE + text_size; // TEST +void elf_text_sector (uint64_t text_size, + uint64_t data_size) { + // + uint64_t text = ELF_HEADER_SIZE + text_size - data_size; memcpy (& elf_text_sector_byte [32], & text, sizeof (text)); memcpy (& elf_text_sector_byte [40], & text, sizeof (text)); @@ -57,9 +56,9 @@ void elf_text_sector (uint64_t text_size) { void elf_data_sector (uint64_t text_size, uint64_t data_size) { - /* */ - uint64_t data = data_size; // TEST - uint64_t core = ELF_HEADER_SIZE + text_size; + // + uint64_t data = data_size; + uint64_t core = ELF_HEADER_SIZE + text_size - data_size; uint64_t move = 0x401000 + core; memcpy (& elf_data_sector_byte [ 8], & core, sizeof (core)); diff --git a/source/unix.h b/source/unix.h index 02e5304..a486efb 100644 --- a/source/unix.h +++ b/source/unix.h @@ -17,10 +17,10 @@ extern uint8_t elf_data_sector_byte [ELF_DATA_SECTOR_SIZE]; extern void elf_main_header (uint8_t has_program, uint8_t for_linux, - uint8_t for_x86_64, - uint64_t entry_point); + uint8_t for_x86_64); -extern void elf_text_sector (uint64_t text_size); +extern void elf_text_sector (uint64_t text_size, + uint64_t data_size); extern void elf_data_sector (uint64_t text_size, uint64_t data_size); diff --git a/test/simple_procedure.asm b/test/simple_procedure.asm new file mode 100644 index 0000000..c763ca1 --- /dev/null +++ b/test/simple_procedure.asm @@ -0,0 +1,97 @@ +; fasm proc.asm proc && chmod +x proc && ./proc + +format ELF64 executable 3 + +segment readable executable + +entry main + +heyo: + nop + mov eax, 1 + nop + mov edi, 1 + nop + mov esi, h + nop + mov edx, 12 + nop + syscall + ret + +cyaa: + nop + mov eax, 1 + nop + mov edi, 1 + nop + mov esi, c + nop + mov edx, 14 + nop + syscall + ret + +main: + nop + call heyo + nop + mov eax, 1 + nop + mov edi, 1 + nop + mov esi, m + nop + mov edx, 13 + nop + syscall + nop + call cyaa + nop + mov eax, 60 + nop + mov edi, 60 + nop + syscall + +segment readable writable + +h: db "Heyo world!", 10 +m: db "Meme world!!", 10 +c: db "Cyaa world!!!", 10 + +;~FASM EAXHLA +;~7F 45 4C 46 02 01 01 03 00 00 00 00 00 00 00 00 7F 45 4C 46 02 01 01 03 00 00 00 00 00 00 00 00 +;~02 00 3E 00 01 00 00 00 EA 00 40 00 00 00 00 00 02 00 3E 00 01 00 00 00 7C 00 00 00 00 00 00 00 +;~40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +;~00 00 00 00 40 00 38 00 02 00 40 00 00 00 00 00 00 00 00 00 40 00 38 00 02 00 40 00 00 00 00 00 +;~01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 +;~00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 +;~20 01 00 00 00 00 00 00 20 01 00 00 00 00 00 00 53 01 00 00 00 00 00 00 53 01 00 00 00 00 00 00 +;~00 10 00 00 00 00 00 00 01 00 00 00 06 00 00 00 00 10 00 00 00 00 00 00 01 00 00 00 06 00 00 00 +;~20 01 00 00 00 00 00 00 20 11 40 00 00 00 00 00 53 01 00 00 00 00 00 00 53 11 40 00 00 00 00 00 +;~20 11 40 00 00 00 00 00 27 00 00 00 00 00 00 00 53 11 40 00 00 00 00 00 0C 00 00 00 00 00 00 00 +;~27 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 +;~90 B8 01 00 00 00 90 48 B8 01 00 00 00 +;~90 BF 01 00 00 00 90 48 BF 01 00 00 00 +;~90 BE 20 11 40 00 90 48 BE 2C 11 40 00 +;~90 BA 0C 00 00 00 90 48 BA 0C 00 00 00 +;~90 0F 05 C3 90 0F 05 C3 +;~90 B8 01 00 00 00 90 48 B8 01 00 00 00 +;~90 BF 01 00 00 00 90 48 BF 01 00 00 00 +;~90 BE 39 11 40 00 90 48 BE 45 11 40 00 +;~90 BA 0E 00 00 00 90 48 BA 0E 00 00 00 +;~90 0F 05 C3 90 0F 05 C3 +;~90 E8 C0 FF FF FF 90 E8 BA FF FF FF +;~90 B8 01 00 00 00 90 48 B8 01 00 00 00 +;~90 BF 01 00 00 00 90 48 BF 01 00 00 00 +;~90 BE 2C 11 40 00 90 48 BE 38 11 40 00 +;~90 BA 0D 00 00 00 90 48 BA 0D 00 00 00 +;~90 0F 05 90 0F 05 +;~90 E8 BC FF FF FF 90 E8 B5 FF FF FF +;~90 B8 3C 00 00 00 90 48 B8 3C 00 00 00 +;~90 BF 3C 00 00 00 90 48 BF 3C 00 00 00 +;~90 0F 05 90 0F 05 +;~48 65 79 6F 20 77 6F 72 6C 64 21 0A 48 65 79 6F 20 77 6F 72 6C 64 21 0A +;~4D 65 6D 65 20 77 6F 72 6C 64 21 21 0A 4D 65 6D 65 20 77 6F 72 6C 64 21 21 0A +;~43 79 61 61 20 77 6F 72 6C 64 21 21 21 0A 43 79 61 61 20 77 6F 72 6C 64 21 21 21 0A diff --git a/test/simple_procedure.eax b/test/simple_procedure.eax index 03f7c5a..4749eb6 100644 --- a/test/simple_procedure.eax +++ b/test/simple_procedure.eax @@ -1,34 +1,34 @@ fast procedure heyo s8 <> h = "Heyo world!\n" begin - nop mov rax 1 - nop mov rdi 1 - nop mov rsi h - nop mov rdx 12 + nop mov eax 1 + nop mov edi 1 + nop mov esi h + nop mov edx 12 nop syscall end procedure fast procedure cyaa - s8 <> c = "Cyaa world!\n" + s8 <> c = "Cyaa world!!!\n" begin - nop mov rax 1 - nop mov rdi 1 - nop mov rsi c - nop mov rdx 12 + nop mov eax 1 + nop mov edi 1 + nop mov esi c + nop mov edx 14 nop syscall end procedure unix program main - s8 <> m = "Meme world!\n" + s8 <> m = "Meme world!!\n" begin nop fastcall heyo - nop mov rax 1 - nop mov rdi 1 - nop mov rsi m - nop mov rdx 12 + nop mov eax 1 + nop mov edi 1 + nop mov esi m + nop mov edx 13 nop syscall nop fastcall cyaa - nop mov rax 60 - nop mov rdi 60 + nop mov eax 60 + nop mov edi 60 nop syscall end program