From fc12ed58bec1808bfbc4272a18b8343b24d23c0b Mon Sep 17 00:00:00 2001 From: xolatile Date: Thu, 11 Jul 2024 12:25:18 -0400 Subject: [PATCH] Integrated unix package into EAXHLA, fixed some tiny bugs, tested... --- Makefile | 2 +- source/assembler.c | 12 +++++------ source/assembler.h | 4 ++-- source/main.c | 43 ++++++++++++++------------------------ source/unix.c | 51 ++++++++++++++++++++++------------------------ source/unix.h | 35 ++++++++++++++++++------------- test/heyo.asm | 19 +++++++++++++++++ test/heyo.eax | 47 ++++++++++++++++++++++++++++++++++++++++++ tool/test.c | 8 ++++---- tool/xfat.c | 8 ++++---- 10 files changed, 143 insertions(+), 86 deletions(-) create mode 100644 test/heyo.asm create mode 100644 test/heyo.eax diff --git a/Makefile b/Makefile index 34b6cc5..1883ecc 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ OUT := eaxhla SOURCE.d := source OBJECT.d := object -SOURCE := main.c assembler.c eaxhla.c +SOURCE := main.c assembler.c eaxhla.c unix.c OBJECT := $(addprefix ${OBJECT.d}/,${SOURCE}) OBJECT := ${OBJECT:.c=.o} diff --git a/source/assembler.c b/source/assembler.c index 4967b8c..88cae26 100644 --- a/source/assembler.c +++ b/source/assembler.c @@ -32,9 +32,9 @@ static next * imbue_size;*/ static void place (form when, byte data) { /* */ - token_array [token_count] = data; + text_sector_byte [text_sector_size] = data; - token_count += (next) when; + text_sector_size += (next) when; } static void print (form when, @@ -269,7 +269,7 @@ static void build_move (size_index size, build_register_redirection ((to == REG) && (from == MEM), destination); build_register_redirection ((to == MEM) && (from == REG), source); - place ((to == REG) && (from == IMM), (byte) (0xb0 + place ((to == REG) && (from == IMM), (byte) (0xb8 + 0x08 * (size != D8) + 0x01 * (destination & 0x07))); @@ -277,14 +277,14 @@ static void build_move (size_index size, place ((to == MEM) && (from == IMM), (byte) (0x05)); print ((to == REG) && (from == MEM), D32, (next) ~0); - print ((to == REG) && (from == IMM), size, source); + print ((to == REG) && (from == IMM), D32, source); print ((to == MEM) && (from == REG), D32, (next) ~0); print ((to == MEM) && (from == IMM), D32, (next) ~0); print ((to == MEM) && (from == IMM), size, source); } -next token_count = 0; -byte * token_array = NULL; +next text_sector_size = 0; +byte * text_sector_byte = NULL; int was_instruction_array_empty = 0; diff --git a/source/assembler.h b/source/assembler.h index 2b3a1e1..e1c20c3 100644 --- a/source/assembler.h +++ b/source/assembler.h @@ -64,8 +64,8 @@ typedef enum { R12, R13, R14, R15, } operand_index; -extern next token_count; -extern byte * token_array; +extern next text_sector_size; +extern byte * text_sector_byte; extern int was_instruction_array_empty; diff --git a/source/main.c b/source/main.c index 3d161bf..59aa219 100644 --- a/source/main.c +++ b/source/main.c @@ -14,6 +14,7 @@ static void dump (const char * file_name); #include "eaxhla.yy.h" #include "eaxhla.tab.h" #include "assembler.h" +#include "unix.h" extern void yyfree_leftovers(void); @@ -25,7 +26,7 @@ signed main(int argc, char * argv[]) { return 1; } - token_array = calloc (1440UL, sizeof (* token_array)); + text_sector_byte = calloc (1440UL, sizeof (* text_sector_byte)); t_array = calloc (1440UL, sizeof (* t_array)); #if DEBUG == 1 @@ -56,7 +57,7 @@ signed main(int argc, char * argv[]) { eaxhla_destroy(); - free (token_array); + free (text_sector_byte); free (t_array); return has_encountered_error; @@ -83,36 +84,22 @@ void append_instruction_t1 (int t1) { } void dump (const char * file_name) { -/* DO NOT TRY THIS AT HOME! */ -#define hackery \ -"\x7F\x45\x4C\x46\x02\x01\x01\x03"\ -"\x00\x00\x00\x00\x00\x00\x00\x00"\ -"\x02\x00\x3E\x00\x01\x00\x00\x00"\ -"\xB0\x00\x40\x00\x00\x00\x00\x00"\ -"\x40\x00\x00\x00\x00\x00\x00\x00"\ -"\x00\x00\x00\x00\x00\x00\x00\x00"\ -"\x00\x00\x00\x00\x40\x00\x38\x00"\ -"\x02\x00\x40\x00\x00\x00\x00\x00"\ -"\x01\x00\x00\x00\x05\x00\x00\x00"\ -"\x00\x00\x00\x00\x00\x00\x00\x00"\ -"\x00\x00\x40\x00\x00\x00\x00\x00"\ -"\x00\x00\x40\x00\x00\x00\x00\x00"\ -"\xC5\x00\x00\x00\x00\x00\x00\x00"\ -"\xC5\x00\x00\x00\x00\x00\x00\x00"\ -"\x00\x10\x00\x00\x00\x00\x00\x00"\ -"\x01\x00\x00\x00\x06\x00\x00\x00"\ -"\xC5\x00\x00\x00\x00\x00\x00\x00"\ -"\xC5\x10\x40\x00\x00\x00\x00\x00"\ -"\xC5\x10\x40\x00\x00\x00\x00\x00"\ -"\x0C\x00\x00\x00\x00\x00\x00\x00"\ -"\x0C\x00\x00\x00\x00\x00\x00\x00"\ -"\x00\x10\x00\x00\x00\x00\x00\x00" + elf_main_header (1, 1, 1, 0); + elf_text_sector (34); + elf_data_sector (34, 12); char meme [1024] = ""; FILE * file = fopen (file_name, "w"); - fwrite (hackery, 1UL, 64UL + 2UL * 56UL, file); - fwrite (token_array, sizeof (* token_array), (size_t) token_count, file); + fwrite (elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file); + fwrite (elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file); + fwrite (elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file); + + //text + fwrite (text_sector_byte, sizeof (* text_sector_byte), + (size_t) text_sector_size, file); + + // data fwrite ("heyo world!\n", 1UL, 12UL, file); snprintf (meme, 1023UL, "chmod +x %s", file_name); diff --git a/source/unix.c b/source/unix.c index fc6fbbf..270cac8 100644 --- a/source/unix.c +++ b/source/unix.c @@ -1,9 +1,6 @@ #include "unix.h" -static const uint32_t softcore = 0x1000; // I DON'T KNOW WHAT IS THIS... -static const uint32_t hardcore = 0x400000; // I DON'T KNOW WHAT IS THIS... - -uint8_t main_header_byte [MAIN_HEADER_SIZE] = { +uint8_t elf_main_header_byte [ELF_MAIN_HEADER_SIZE] = { 0X7F, 0X45, 0X4C, 0X46, 0X02, 0X01, 0X01, 0X03, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X02, 0X00, 0X3E, 0X00, 0X01, 0X00, 0X00, 0X00, @@ -14,7 +11,7 @@ uint8_t main_header_byte [MAIN_HEADER_SIZE] = { 0X02, 0X00, 0X40, 0X00, 0X00, 0X00, 0X00, 0X00, }; -uint8_t text_sector_byte [TEXT_SECTOR_SIZE]; = { +uint8_t elf_text_sector_byte [ELF_TEXT_SECTOR_SIZE] = { 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X40, 0X00, 0X00, 0X00, 0X00, 0X00, @@ -24,7 +21,7 @@ uint8_t text_sector_byte [TEXT_SECTOR_SIZE]; = { 0X00, 0X10, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, }; -uint8_t data_sector_byte [DATA_SECTOR_SIZE] = { +uint8_t elf_data_sector_byte [ELF_DATA_SECTOR_SIZE] = { 0X01, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, // HDR + TXT 0X00, 0X10, 0X40, 0X00, 0X00, 0X00, 0X00, 0X00, // +++ ^ @@ -34,40 +31,40 @@ uint8_t data_sector_byte [DATA_SECTOR_SIZE] = { 0X00, 0X10, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, }; -void main_header (uint8_t has_program, - uint8_t for_linux, - uint8_t for_x86_64, - uint64_t entry_point) { +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 - main_header_bytes [16] = (has_program) ? 0x02 : 0x03; // else linkable - main_header_bytes [ 7] = (for_linux) ? 0x03 : 0x00; // else system v - main_header_bytes [18] = (for_x86_64) ? 0x3e : 0x00; // else ? + 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 (& main_header_bytes [24], & enter, sizeof (enter)); + memcpy (& elf_main_header_byte [24], & enter, sizeof (enter)); } } -void text_sector (uint64_t text_size) { +void elf_text_sector (uint64_t text_size) { /* */ - uint64_t text = text_size; // TEST + uint64_t text = ELF_HEADER_SIZE + text_size; // TEST - memcpy (& text_sector_byte [32], & text, sizeof (text)); - memcpy (& text_sector_byte [40], & text, sizeof (text)); + memcpy (& elf_text_sector_byte [32], & text, sizeof (text)); + memcpy (& elf_text_sector_byte [40], & text, sizeof (text)); } -void data_sector (uint64_t text_size, - uint64_t data_size) { +void elf_data_sector (uint64_t text_size, + uint64_t data_size) { /* */ uint64_t data = data_size; // TEST - uint64_t main = HEADER_SIZE + text_size; - uint64_t move = 0x401000 + main; + uint64_t core = ELF_HEADER_SIZE + text_size; + uint64_t move = 0x401000 + core; - memcpy (& data_sector_byte [ 8], & main, sizeof (main)); - memcpy (& data_sector_byte [16], & move, sizeof (move)); - memcpy (& data_sector_byte [24], & move, sizeof (move)); - memcpy (& data_sector_byte [32], & data, sizeof (data)); - memcpy (& data_sector_byte [40], & data, sizeof (data)); + memcpy (& elf_data_sector_byte [ 8], & core, sizeof (core)); + memcpy (& elf_data_sector_byte [16], & move, sizeof (move)); + memcpy (& elf_data_sector_byte [24], & move, sizeof (move)); + memcpy (& elf_data_sector_byte [32], & data, sizeof (data)); + memcpy (& elf_data_sector_byte [40], & data, sizeof (data)); } diff --git a/source/unix.h b/source/unix.h index 4c87520..02e5304 100644 --- a/source/unix.h +++ b/source/unix.h @@ -1,22 +1,29 @@ +#ifndef UNIX_H + #include #include -#define MAIN_HEADER_SIZE (0x40) -#define TEXT_SECTOR_SIZE (0x38) -#define DATA_SECTOR_SIZE (0x38) +#define ELF_MAIN_HEADER_SIZE (0x40) +#define ELF_TEXT_SECTOR_SIZE (0x38) +#define ELF_DATA_SECTOR_SIZE (0x38) -#define HEADER_SIZE (MAIN_HEADER_SIZE + TEXT_SECTOR_SIZE + DATA_SECTOR_SIZE) +#define ELF_HEADER_SIZE (ELF_MAIN_HEADER_SIZE \ + + ELF_TEXT_SECTOR_SIZE \ + + ELF_DATA_SECTOR_SIZE) -extern uint8_t main_header_byte [MAIN_HEADER_SIZE]; -extern uint8_t text_sector_byte [TEXT_SECTOR_SIZE]; -extern uint8_t data_sector_byte [DATA_SECTOR_SIZE]; +extern uint8_t elf_main_header_byte [ELF_MAIN_HEADER_SIZE]; +extern uint8_t elf_text_sector_byte [ELF_TEXT_SECTOR_SIZE]; +extern uint8_t elf_data_sector_byte [ELF_DATA_SECTOR_SIZE]; -extern void main_header (uint8_t has_program, - uint8_t for_linux, - uint8_t for_x86_64, - uint64_t entry_point); +extern void elf_main_header (uint8_t has_program, + uint8_t for_linux, + uint8_t for_x86_64, + uint64_t entry_point); -extern void text_sector (uint64_t text_size); +extern void elf_text_sector (uint64_t text_size); -extern void data_sector (uint64_t text_size, - uint64_t data_size); +extern void elf_data_sector (uint64_t text_size, + uint64_t data_size); + +#define UNIX_H +#endif diff --git a/test/heyo.asm b/test/heyo.asm new file mode 100644 index 0000000..52ed719 --- /dev/null +++ b/test/heyo.asm @@ -0,0 +1,19 @@ +format ELF64 executable 3 + +segment readable executable + +entry $ + + mov eax, 1 + mov edi, 1 + mov esi, heyo + mov edx, 12 + syscall + + mov eax, 60 + mov edi, 0 + syscall + +segment readable writable + +heyo: db "heyo world!", 10 diff --git a/test/heyo.eax b/test/heyo.eax new file mode 100644 index 0000000..5853ae3 --- /dev/null +++ b/test/heyo.eax @@ -0,0 +1,47 @@ +program heyo_world + +begin + + mov eax 1 + mov edi 1 + mov esi 4198610 + mov edx 12 + syscall + + mov eax 60 + mov edi 0 + syscall + +end program + +/* +7F 45 4C 46 02 01 01 03 +00 00 00 00 00 00 00 00 +02 00 3E 00 01 00 00 00 +B0 00 40 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 + +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 +22 00 00 00 00 00 00 00 +22 00 00 00 00 00 00 00 +00 10 00 00 00 00 00 00 + +01 00 00 00 06 00 00 00 +D2 00 00 00 00 00 00 00 +D2 10 40 00 00 00 00 00 +D2 10 40 00 00 00 00 00 +0C 00 00 00 00 00 00 00 +0C 00 00 00 00 00 00 00 +00 10 00 00 00 00 00 00 + +B0 01 00 00 00 B7 01 00 00 00 B6 D2 10 40 00 B2 0C 00 00 00 +0F 05 B0 3C 00 00 00 B7 00 00 00 00 0F 05 + +68 65 79 6F 20 77 6F 72 6C 64 21 0A +*/ diff --git a/tool/test.c b/tool/test.c index 450fa1d..7af8d2a 100644 --- a/tool/test.c +++ b/tool/test.c @@ -11,15 +11,15 @@ static unsigned int array [] = { int main (void) { unsigned int index; - token_array = malloc (1024UL * 1024UL * sizeof (* token_array)); + text_sector_byte = malloc (1024UL * 1024UL * sizeof (* text_sector_byte)); assemble ((unsigned int) (sizeof (array) / sizeof (array [0])), array); - for (index = 0; index < token_count; ++index) { - printf ("%c%02X", (token_array [index] == 0x90) ? '\n' : ' ', token_array [index]); + for (index = 0; index < text_sector_size; ++index) { + printf ("%c%02X", (text_sector_byte [index] == 0x90) ? '\n' : ' ', text_sector_byte [index]); } - free (token_array); + free (text_sector_byte); return (0); } diff --git a/tool/xfat.c b/tool/xfat.c index 6ac43dc..8f4377e 100644 --- a/tool/xfat.c +++ b/tool/xfat.c @@ -13,11 +13,11 @@ "};\n" \ "int main (void) {\n" \ " unsigned int index;\n" \ -" token_array = malloc (144UL * sizeof (* token_array));\n" \ +" text_sector_byte = malloc (144UL * sizeof (* text_sector_byte));\n" \ " assemble ((unsigned int) (sizeof (array) / sizeof (array [0])), array);\n" \ -" for (index = 0; index < token_count; ++index)\n" \ -" printf (\"%02X \", token_array [index]);\n" \ -" free (token_array);\n" \ +" for (index = 0; index < text_sector_size; ++index)\n" \ +" printf (\"%02X \", text_sector_byte [index]);\n" \ +" free (text_sector_byte);\n" \ " return (0);\n" \ "}\n"