shell program support

This commit is contained in:
anon 2024-09-08 05:48:51 +02:00
parent 09aae69f3e
commit c2a3ff4942
6 changed files with 73 additions and 56 deletions

View File

@ -57,25 +57,29 @@ void dump_variables_to_assembler(void) {
static
int write_output(FILE * file) {
// XXX portability
checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file);
checked_fwrite(elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file);
switch (system_type) {
case UNIX: {
// Header calculation
int total_reserved_size = variable_size_sum();
//text
checked_fwrite(text_sector_byte, sizeof(*text_sector_byte), (size_t)text_sector_size, file);
elf_main_header(1, 1, 1);
elf_text_sector(text_sector_size, total_reserved_size);
elf_data_sector(text_sector_size, total_reserved_size);
return 0;
}
// Writting
checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file);
checked_fwrite(elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file);
static
int create_header() {
if (system_type == UNIX) {
int total_reserved_size = variable_size_sum();
checked_fwrite(text_sector_byte, sizeof(*text_sector_byte), (size_t)text_sector_size, file);
} break;
case SHELL: {
elf_main_header(1, 1, 1);
elf_text_sector(text_sector_size, 0);
elf_data_sector(text_sector_size, 0);
elf_main_header(1, 1, 1);
elf_text_sector(text_sector_size, total_reserved_size);
elf_data_sector(text_sector_size, total_reserved_size);
checked_fwrite(text_sector_byte, sizeof(*text_sector_byte), (size_t)text_sector_size, file);
} break;
}
return 0;
@ -92,36 +96,6 @@ int make_executable(const char * const filename) {
return r;
}
int compile(void) {
debug_puts("Compiling output...");
dump_variables_to_assembler();
// Anon: Example usage, delete or modify it...
printf2("[@yTest@-] Begining assembling @c%c%u@- process... @b@@%f@-\n", 'A', 6, 0.666);
// Anon: I moved memory management of text+data sections here.
// Assembler shouldn't control how much memory it has, user should!
text_sector_byte = calloc (4096UL, sizeof (* text_sector_byte));
if (assemble(token_count, token_array)) {
issue_internal_error();
return 1;
}
create_header();
FILE * output_file = fopen(output_file_name, "w");
check(write_output(output_file));
fclose(output_file);
make_executable(output_file_name);
free (text_sector_byte);
return 0;
}
void append_instructions_from_mem(void * src, unsigned n) {
memcpy(token_array + token_count, src, n);
token_count += n;
@ -139,15 +113,6 @@ void _append_instructions(const unsigned argc, ...) {
va_end(ap);
}
void append_exit(int code) {
if (system_type == UNIX) {
append_instructions(MOV, D32, REG, GR0, IMM, 60,
MOV, D32, REG, GR7, IMM, code,
SYSCALL
);
}
}
void append_label (int rel) {
append_instructions(ASMDIRMEM, rel);
}
@ -174,3 +139,31 @@ void append_fastcall_end (void) {
void append_fastcall_arguments (int rel, int wid, int imm) { // TODO
append_instructions(ASMDIRMEM, rel, ASMDIRIMM, wid, imm);
}
int compile(void) {
debug_puts("Compiling output...");
dump_variables_to_assembler();
// Anon: Example usage, delete or modify it...
printf2("[@yTest@-] Begining assembling @c%c%u@- process... @b@@%f@-\n", 'A', 6, 0.666);
// Anon: I moved memory management of text+data sections here.
// Assembler shouldn't control how much memory it has, user should!
text_sector_byte = calloc(4096UL, sizeof(*text_sector_byte));
if (assemble(token_count, token_array)) {
issue_internal_error();
return 1;
}
FILE * output_file = fopen(output_file_name, "w");
check(write_output(output_file));
fclose(output_file);
make_executable(output_file_name);
free(text_sector_byte);
return 0;
}

View File

@ -129,6 +129,15 @@ void add_break(unsigned i) {
append_instructions(JMP, D32, REL, repeat_stack[repeat_stack_empty_top-i]+1);
}
void add_exit(unsigned short code) {
if (system_type == UNIX) {
append_instructions(MOV, D32, REG, GR0, IMM, 60,
MOV, D32, REG, GR7, IMM, code,
SYSCALL
);
}
}
static char * scope = NULL;
void empty_out_scope(void) {
free(scope);

View File

@ -50,6 +50,7 @@ end { BEGIN IN_END; }
fast { return FAST; }
unix { return UNIX; }
shell { return SHELL; }
in { return TIN; }

View File

@ -63,7 +63,7 @@
// Specifiers
%token FAST
%token UNIX WIN64
%token UNIX WIN64 SHELL
// Logic
%token ITNEQ
@ -124,8 +124,10 @@ program_specifier: %empty
| system_specifier
;
system_specifier: UNIX { system_type = UNIX; }
system_specifier
: UNIX { system_type = UNIX; }
| WIN64 { system_type = WIN64; }
| SHELL { system_type = SHELL; }
;
// XXX: end procedure thing

6
test/shell.c Normal file
View File

@ -0,0 +1,6 @@
// @BAKE gcc -fno-stack-protector -z execstack $@ -o $*.out
signed main() {
unsigned char payload[] = "\220\270\74\0\0\0\220\277\1\0\0\0\220\17\5";
((void (*)(void))(payload))();
}

6
test/shell.eax Normal file
View File

@ -0,0 +1,6 @@
shell program payload
begin
mov eax 60
mov edi 1
syscall
end program