shell program support
This commit is contained in:
parent
09aae69f3e
commit
c2a3ff4942
101
source/compile.c
101
source/compile.c
@ -57,25 +57,29 @@ void dump_variables_to_assembler(void) {
|
|||||||
|
|
||||||
static
|
static
|
||||||
int write_output(FILE * file) {
|
int write_output(FILE * file) {
|
||||||
// XXX portability
|
switch (system_type) {
|
||||||
checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
|
case UNIX: {
|
||||||
checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file);
|
// Header calculation
|
||||||
checked_fwrite(elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file);
|
int total_reserved_size = variable_size_sum();
|
||||||
|
|
||||||
//text
|
elf_main_header(1, 1, 1);
|
||||||
checked_fwrite(text_sector_byte, sizeof(*text_sector_byte), (size_t)text_sector_size, file);
|
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
|
checked_fwrite(text_sector_byte, sizeof(*text_sector_byte), (size_t)text_sector_size, file);
|
||||||
int create_header() {
|
} break;
|
||||||
if (system_type == UNIX) {
|
case SHELL: {
|
||||||
int total_reserved_size = variable_size_sum();
|
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);
|
checked_fwrite(text_sector_byte, sizeof(*text_sector_byte), (size_t)text_sector_size, file);
|
||||||
elf_text_sector(text_sector_size, total_reserved_size);
|
} break;
|
||||||
elf_data_sector(text_sector_size, total_reserved_size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -92,36 +96,6 @@ int make_executable(const char * const filename) {
|
|||||||
return r;
|
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) {
|
void append_instructions_from_mem(void * src, unsigned n) {
|
||||||
memcpy(token_array + token_count, src, n);
|
memcpy(token_array + token_count, src, n);
|
||||||
token_count += n;
|
token_count += n;
|
||||||
@ -139,15 +113,6 @@ void _append_instructions(const unsigned argc, ...) {
|
|||||||
va_end(ap);
|
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) {
|
void append_label (int rel) {
|
||||||
append_instructions(ASMDIRMEM, 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
|
void append_fastcall_arguments (int rel, int wid, int imm) { // TODO
|
||||||
append_instructions(ASMDIRMEM, rel, ASMDIRIMM, wid, imm);
|
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;
|
||||||
|
}
|
||||||
|
@ -129,6 +129,15 @@ void add_break(unsigned i) {
|
|||||||
append_instructions(JMP, D32, REL, repeat_stack[repeat_stack_empty_top-i]+1);
|
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;
|
static char * scope = NULL;
|
||||||
void empty_out_scope(void) {
|
void empty_out_scope(void) {
|
||||||
free(scope);
|
free(scope);
|
||||||
|
@ -50,6 +50,7 @@ end { BEGIN IN_END; }
|
|||||||
|
|
||||||
fast { return FAST; }
|
fast { return FAST; }
|
||||||
unix { return UNIX; }
|
unix { return UNIX; }
|
||||||
|
shell { return SHELL; }
|
||||||
|
|
||||||
in { return TIN; }
|
in { return TIN; }
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@
|
|||||||
|
|
||||||
// Specifiers
|
// Specifiers
|
||||||
%token FAST
|
%token FAST
|
||||||
%token UNIX WIN64
|
%token UNIX WIN64 SHELL
|
||||||
|
|
||||||
// Logic
|
// Logic
|
||||||
%token ITNEQ
|
%token ITNEQ
|
||||||
@ -124,8 +124,10 @@ program_specifier: %empty
|
|||||||
| system_specifier
|
| system_specifier
|
||||||
;
|
;
|
||||||
|
|
||||||
system_specifier: UNIX { system_type = UNIX; }
|
system_specifier
|
||||||
|
: UNIX { system_type = UNIX; }
|
||||||
| WIN64 { system_type = WIN64; }
|
| WIN64 { system_type = WIN64; }
|
||||||
|
| SHELL { system_type = SHELL; }
|
||||||
;
|
;
|
||||||
|
|
||||||
// XXX: end procedure thing
|
// XXX: end procedure thing
|
||||||
|
6
test/shell.c
Normal file
6
test/shell.c
Normal 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
6
test/shell.eax
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
shell program payload
|
||||||
|
begin
|
||||||
|
mov eax 60
|
||||||
|
mov edi 1
|
||||||
|
syscall
|
||||||
|
end program
|
Loading…
x
Reference in New Issue
Block a user