Integrated unix package into EAXHLA, fixed some tiny bugs, tested...
This commit is contained in:
2
Makefile
2
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}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
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,
|
||||
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));
|
||||
}
|
||||
|
@ -1,22 +1,29 @@
|
||||
#ifndef UNIX_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#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,
|
||||
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,
|
||||
extern void elf_data_sector (uint64_t text_size,
|
||||
uint64_t data_size);
|
||||
|
||||
#define UNIX_H
|
||||
#endif
|
||||
|
19
test/heyo.asm
Normal file
19
test/heyo.asm
Normal 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
47
test/heyo.eax
Normal 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
|
||||
*/
|
@ -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);
|
||||
}
|
||||
|
@ -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"
|
||||
|
||||
|
Reference in New Issue
Block a user