diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-06-12 02:50:25 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-06-12 02:50:25 +0200 |
commit | 7bb61696dda1e00512c67abbea4193ad660c0bb7 (patch) | |
tree | 35cfa57d0ad288a4adc973a6d64a1defb4446be0 /bxgen.c | |
parent | 0158fc479e3be59f1e2e8bfea4d0021e135a589e (diff) | |
download | bxgen-7bb61696dda1e00512c67abbea4193ad660c0bb7.zip |
Fix io open; Cleanup
Diffstat (limited to 'bxgen.c')
-rwxr-xr-x | bxgen.c | 71 |
1 files changed, 45 insertions, 26 deletions
@@ -180,7 +180,8 @@ enum { // IO dispatch operations // - IO_OPEN = 0, + IO_OPEN_READ = 0, + IO_OPEN_WRITE, IO_CLOSE, IO_SEEK, IO_READ, @@ -336,7 +337,8 @@ void unit_set_name(pool_t *pool, i64 unit, i64 name_size, c8 *name); void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc); void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_id, void *io_user_data); -i64 io_open(i64 name_size, c8 *name, void *user_data); +i64 io_open_read(i64 name_size, c8 *name, void *user_data); +i64 io_open_write(i64 name_size, c8 *name, void *user_data); void io_close(i64 f, void *user_data); i64 io_seek(i64 f, i64 offset, u16 origin, void *user_data); i64 io_read(i64 f, i64 size, void *data, void *user_data); @@ -716,6 +718,13 @@ void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc) { #include <stdlib.h> // TEMP void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_data) { + // Docs + // + // AR https://man.freebsd.org/cgi/man.cgi?query=ar&sektion=5 + // ELF https://man7.org/linux/man-pages/man5/elf.5.html + // + + assert(pool != NULL && pool->entities != NULL); assert(pool->entities[unit].is_enabled); assert(pool->entities[unit].unit.entry_point_index != UNDEFINED); @@ -737,7 +746,7 @@ void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_da assert(l->type == UNIT_LIBRARY_STATIC); assert(l->name_size > 0 && l->name_size <= MAX_NAME_SIZE); - i64 f = io_open(l->name_size, l->name, io_user_data); + i64 f = io_open_read(l->name_size, l->name, io_user_data); c8 buf0_[MAX_NAME_SIZE + 1] = { 0 }; memcpy(buf0_, l->name, l->name_size); @@ -870,12 +879,12 @@ void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_da } } - #define WRITE(x, n) io_write( io_out, n, x, io_user_data ) - #define WRITE_V(...) io_write( io_out, sizeof((u8[]) {__VA_ARGS__}), (u8[]) {__VA_ARGS__}, io_user_data ) - #define WRITE_DUP(x, n) io_write( io_out, n, (u8[n]) { 0 }, io_user_data ) - #define WRITE_2(x) io_write( io_out, 2, &(u16) { x }, io_user_data ) - #define WRITE_4(x) io_write( io_out, 4, &(u32) { x }, io_user_data ) - #define WRITE_8(x) io_write( io_out, 8, &(u64) { x }, io_user_data ) + #define WRITE(x, n) io_write( io_out, n, x, io_user_data ) + #define WRITE_V(...) io_write( io_out, sizeof((u8[]) {__VA_ARGS__}), (u8[]) {__VA_ARGS__}, io_user_data ) + #define WRITE_DUP(x, n) io_write( io_out, n, (u8[n]) { 0 }, io_user_data ) + #define WRITE_2(x) io_write( io_out, 2, &(u16) { x }, io_user_data ) + #define WRITE_4(x) io_write( io_out, 4, &(u32) { x }, io_user_data ) + #define WRITE_8(x) io_write( io_out, 8, &(u64) { x }, io_user_data ) u16 ehs = 64; u16 shs = 0; @@ -889,14 +898,17 @@ void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_da 0x0f, 0x05, // syscall }; - u64 code_offset = (ehs + phs + (align - 1)) & (~(align - 1)); - u64 code_size = sizeof code; + u64 code_offset = ehs + phs; + u64 code_size = sizeof code; + u64 entry_offset = 0; + + u64 base_address = 0x400000; // x86_64 base address + u64 code_address = base_address + code_offset; + u64 entry = code_address + entry_offset; assert((code_offset % align) == 0); assert((code_size % align) == 0); - u64 entry = 0x400000 + ehs + phs; - // ELF header // @@ -927,14 +939,14 @@ void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_da // Program header // - WRITE_4( 1 ); // type (PT_LOAD) - WRITE_4( 5 ); // flags (PF_X | PF_R) - WRITE_8( code_offset ); // offset - WRITE_8( entry ); // vaddr - WRITE_8( entry ); // paddr - WRITE_8( code_size ); // filesz - WRITE_8( code_size ); // memsz - WRITE_8( 8 ); // align + WRITE_4( 1 ); // type (PT_LOAD) + WRITE_4( 5 ); // flags (PF_X | PF_R) + WRITE_8( code_offset ); // offset + WRITE_8( code_address ); // vaddr + WRITE_8( code_address ); // paddr + WRITE_8( code_size ); // filesz + WRITE_8( code_size ); // memsz + WRITE_8( 8 ); // align // Code // @@ -952,9 +964,15 @@ void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_da #undef WRITE } -i64 io_open(i64 name_size, c8 *name, void *user_data) { +i64 io_open_read(i64 name_size, c8 *name, void *user_data) { + i64 f; + io_dispatch(IO_OPEN_READ, &f, &name_size, name, user_data); + return f; +} + +i64 io_open_write(i64 name_size, c8 *name, void *user_data) { i64 f; - io_dispatch(IO_OPEN, &f, &name_size, name, user_data); + io_dispatch(IO_OPEN_WRITE, &f, &name_size, name, user_data); return f; } @@ -1008,13 +1026,14 @@ void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data) { c8 buf[MAX_NAME_SIZE] = { 0 }; switch (op) { - case IO_OPEN: + case IO_OPEN_READ: + case IO_OPEN_WRITE: assert(size != NULL); assert(*size > 0 && *size < MAX_NAME_SIZE); assert(data != NULL); memcpy(buf, data, *size); - *f = fopen(buf, "rb"); + *f = fopen(buf, op == IO_OPEN_READ ? "rb" : "wb"); assert(*f != NULL); break; @@ -1128,7 +1147,7 @@ void u_entry_point(i64 unit, i64 proc) { void u_elf_x86_64(i64 unit, c8 *output_file_name) { printf("Writing ELF x86_64 executable...\n"); - i64 out = io_open(strlen(output_file_name), output_file_name, NULL); + i64 out = io_open_write(strlen(output_file_name), output_file_name, NULL); unit_write(&g_pool, unit, FORMAT_ELF | ARCH_X86_64, out, NULL); |