Integrated unix package into EAXHLA, fixed some tiny bugs, tested...

This commit is contained in:
xolatile
2024-07-11 12:25:18 -04:00
parent 5ad57f9262
commit fc12ed58be
10 changed files with 143 additions and 86 deletions

View File

@ -26,7 +26,7 @@ OUT := eaxhla
SOURCE.d := source SOURCE.d := source
OBJECT.d := object 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 := $(addprefix ${OBJECT.d}/,${SOURCE})
OBJECT := ${OBJECT:.c=.o} OBJECT := ${OBJECT:.c=.o}

View File

@ -32,9 +32,9 @@ static next * imbue_size;*/
static void place (form when, static void place (form when,
byte data) { 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, 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 == REG) && (from == MEM), destination);
build_register_redirection ((to == MEM) && (from == REG), source); 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) + 0x08 * (size != D8)
+ 0x01 * (destination & 0x07))); + 0x01 * (destination & 0x07)));
@ -277,14 +277,14 @@ static void build_move (size_index size,
place ((to == MEM) && (from == IMM), (byte) (0x05)); place ((to == MEM) && (from == IMM), (byte) (0x05));
print ((to == REG) && (from == MEM), D32, (next) ~0); 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 == REG), D32, (next) ~0);
print ((to == MEM) && (from == IMM), D32, (next) ~0); print ((to == MEM) && (from == IMM), D32, (next) ~0);
print ((to == MEM) && (from == IMM), size, source); print ((to == MEM) && (from == IMM), size, source);
} }
next token_count = 0; next text_sector_size = 0;
byte * token_array = NULL; byte * text_sector_byte = NULL;
int was_instruction_array_empty = 0; int was_instruction_array_empty = 0;

View File

@ -64,8 +64,8 @@ typedef enum {
R12, R13, R14, R15, R12, R13, R14, R15,
} operand_index; } operand_index;
extern next token_count; extern next text_sector_size;
extern byte * token_array; extern byte * text_sector_byte;
extern int was_instruction_array_empty; extern int was_instruction_array_empty;

View File

@ -14,6 +14,7 @@ static void dump (const char * file_name);
#include "eaxhla.yy.h" #include "eaxhla.yy.h"
#include "eaxhla.tab.h" #include "eaxhla.tab.h"
#include "assembler.h" #include "assembler.h"
#include "unix.h"
extern void yyfree_leftovers(void); extern void yyfree_leftovers(void);
@ -25,7 +26,7 @@ signed main(int argc, char * argv[]) {
return 1; 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)); t_array = calloc (1440UL, sizeof (* t_array));
#if DEBUG == 1 #if DEBUG == 1
@ -56,7 +57,7 @@ signed main(int argc, char * argv[]) {
eaxhla_destroy(); eaxhla_destroy();
free (token_array); free (text_sector_byte);
free (t_array); free (t_array);
return has_encountered_error; return has_encountered_error;
@ -83,36 +84,22 @@ void append_instruction_t1 (int t1) {
} }
void dump (const char * file_name) { void dump (const char * file_name) {
/* DO NOT TRY THIS AT HOME! */ elf_main_header (1, 1, 1, 0);
#define hackery \ elf_text_sector (34);
"\x7F\x45\x4C\x46\x02\x01\x01\x03"\ elf_data_sector (34, 12);
"\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"
char meme [1024] = ""; char meme [1024] = "";
FILE * file = fopen (file_name, "w"); FILE * file = fopen (file_name, "w");
fwrite (hackery, 1UL, 64UL + 2UL * 56UL, file); fwrite (elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
fwrite (token_array, sizeof (* token_array), (size_t) token_count, 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); fwrite ("heyo world!\n", 1UL, 12UL, file);
snprintf (meme, 1023UL, "chmod +x %s", file_name); snprintf (meme, 1023UL, "chmod +x %s", file_name);

View File

@ -1,9 +1,6 @@
#include "unix.h" #include "unix.h"
static const uint32_t softcore = 0x1000; // I DON'T KNOW WHAT IS THIS... uint8_t elf_main_header_byte [ELF_MAIN_HEADER_SIZE] = {
static const uint32_t hardcore = 0x400000; // I DON'T KNOW WHAT IS THIS...
uint8_t main_header_byte [MAIN_HEADER_SIZE] = {
0X7F, 0X45, 0X4C, 0X46, 0X02, 0X01, 0X01, 0X03, 0X7F, 0X45, 0X4C, 0X46, 0X02, 0X01, 0X01, 0X03,
0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
0X02, 0X00, 0X3E, 0X00, 0X01, 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, 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, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00,
0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
0X00, 0X00, 0X40, 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, 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, 0X01, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00,
0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, // HDR + TXT 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, // HDR + TXT
0X00, 0X10, 0X40, 0X00, 0X00, 0X00, 0X00, 0X00, // +++ ^ 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, 0X00, 0X10, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
}; };
void main_header (uint8_t has_program, void elf_main_header (uint8_t has_program,
uint8_t for_linux, uint8_t for_linux,
uint8_t for_x86_64, uint8_t for_x86_64,
uint64_t entry_point) { uint64_t entry_point) {
/* */ /* */
uint64_t enter = entry_point; // TEST uint64_t enter = entry_point; // TEST
main_header_bytes [16] = (has_program) ? 0x02 : 0x03; // else linkable elf_main_header_byte [16] = (has_program) ? 0x02 : 0x03; // library
main_header_bytes [ 7] = (for_linux) ? 0x03 : 0x00; // else system v elf_main_header_byte [ 7] = (for_linux) ? 0x03 : 0x00; // system v
main_header_bytes [18] = (for_x86_64) ? 0x3e : 0x00; // else ? elf_main_header_byte [18] = (for_x86_64) ? 0x3e : 0x00;
if (entry_point != 0) { 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 (& elf_text_sector_byte [32], & text, sizeof (text));
memcpy (& text_sector_byte [40], & text, sizeof (text)); memcpy (& elf_text_sector_byte [40], & text, sizeof (text));
} }
void data_sector (uint64_t text_size, void elf_data_sector (uint64_t text_size,
uint64_t data_size) { uint64_t data_size) {
/* */ /* */
uint64_t data = data_size; // TEST uint64_t data = data_size; // TEST
uint64_t main = HEADER_SIZE + text_size; uint64_t core = ELF_HEADER_SIZE + text_size;
uint64_t move = 0x401000 + main; uint64_t move = 0x401000 + core;
memcpy (& data_sector_byte [ 8], & main, sizeof (main)); memcpy (& elf_data_sector_byte [ 8], & core, sizeof (core));
memcpy (& data_sector_byte [16], & move, sizeof (move)); memcpy (& elf_data_sector_byte [16], & move, sizeof (move));
memcpy (& data_sector_byte [24], & move, sizeof (move)); memcpy (& elf_data_sector_byte [24], & move, sizeof (move));
memcpy (& data_sector_byte [32], & data, sizeof (data)); memcpy (& elf_data_sector_byte [32], & data, sizeof (data));
memcpy (& data_sector_byte [40], & data, sizeof (data)); memcpy (& elf_data_sector_byte [40], & data, sizeof (data));
} }

View File

@ -1,22 +1,29 @@
#ifndef UNIX_H
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#define MAIN_HEADER_SIZE (0x40) #define ELF_MAIN_HEADER_SIZE (0x40)
#define TEXT_SECTOR_SIZE (0x38) #define ELF_TEXT_SECTOR_SIZE (0x38)
#define DATA_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 elf_main_header_byte [ELF_MAIN_HEADER_SIZE];
extern uint8_t text_sector_byte [TEXT_SECTOR_SIZE]; extern uint8_t elf_text_sector_byte [ELF_TEXT_SECTOR_SIZE];
extern uint8_t data_sector_byte [DATA_SECTOR_SIZE]; extern uint8_t elf_data_sector_byte [ELF_DATA_SECTOR_SIZE];
extern void main_header (uint8_t has_program, extern void elf_main_header (uint8_t has_program,
uint8_t for_linux, uint8_t for_linux,
uint8_t for_x86_64, uint8_t for_x86_64,
uint64_t entry_point); 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, extern void elf_data_sector (uint64_t text_size,
uint64_t data_size); uint64_t data_size);
#define UNIX_H
#endif

19
test/heyo.asm Normal file
View File

@ -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

47
test/heyo.eax Normal file
View File

@ -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
*/

View File

@ -11,15 +11,15 @@ static unsigned int array [] = {
int main (void) { int main (void) {
unsigned int index; 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); assemble ((unsigned int) (sizeof (array) / sizeof (array [0])), array);
for (index = 0; index < token_count; ++index) { for (index = 0; index < text_sector_size; ++index) {
printf ("%c%02X", (token_array [index] == 0x90) ? '\n' : ' ', token_array [index]); printf ("%c%02X", (text_sector_byte [index] == 0x90) ? '\n' : ' ', text_sector_byte [index]);
} }
free (token_array); free (text_sector_byte);
return (0); return (0);
} }

View File

@ -13,11 +13,11 @@
"};\n" \ "};\n" \
"int main (void) {\n" \ "int main (void) {\n" \
" unsigned int index;\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" \ " assemble ((unsigned int) (sizeof (array) / sizeof (array [0])), array);\n" \
" for (index = 0; index < token_count; ++index)\n" \ " for (index = 0; index < text_sector_size; ++index)\n" \
" printf (\"%02X \", token_array [index]);\n" \ " printf (\"%02X \", text_sector_byte [index]);\n" \
" free (token_array);\n" \ " free (text_sector_byte);\n" \
" return (0);\n" \ " return (0);\n" \
"}\n" "}\n"