diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-06-09 20:32:57 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-06-09 20:32:57 +0200 |
commit | 279e17589876bb1c4c262df847bfcc80564a42ca (patch) | |
tree | 98a2fb02312d24eb46c06bfa2adfd20ef74f89ff /binary_code_generation.c | |
parent | d35f6a6ee6da0c11063b6a88a3ce0f88346e8142 (diff) | |
download | bxgen-279e17589876bb1c4c262df847bfcc80564a42ca.zip |
Refactor
Diffstat (limited to 'binary_code_generation.c')
-rwxr-xr-x | binary_code_generation.c | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/binary_code_generation.c b/binary_code_generation.c index e0853ec..994f7d4 100755 --- a/binary_code_generation.c +++ b/binary_code_generation.c @@ -99,12 +99,6 @@ enum { OP_I64 = 0, OP_RET, - // Object file - UNIT_OBJ = 0, - - // Executable - UNIT_EXE, - // Entity types // @@ -156,7 +150,6 @@ typedef struct { // typedef struct { - i16 type; i64 entry_point; i64 proc_count; i64 procs[MAX_PROC_COUNT]; @@ -381,40 +374,52 @@ void unit_write(entity_pool_t *pool, i64 unit, i16 target, FILE *out) { // ELF header // - fwrite( (u8[4]) { 0x7f, 'E', 'L', 'F' }, 1, 4, out); // magic - - fwrite(&(u8) { 2 }, 1, 1, out); // elf64 - fwrite(&(u8) { 1 }, 1, 1, out); // 2's complement, little endian - fwrite(&(u8) { 1 }, 1, 1, out); // current version - fwrite(&(u8) { 0 }, 1, 1, out); // SysV - fwrite(&(u8) { 0 }, 1, 1, out); // ABI version - fwrite( (u8[7]) { 0 }, 1, 7, out); // padding - - fwrite(&(u16) { 2 }, 2, 1, out); // executable - fwrite(&(u16) { 62 }, 2, 1, out); // x86_64 - fwrite(&(u32) { 1 }, 4, 1, out); // current version - fwrite(&(u64) { entry }, 8, 1, out); // entry point address - fwrite(&(u64) { ehs }, 8, 1, out); // program header offset - fwrite(&(u64) { 0 }, 8, 1, out); // section header offset - fwrite(&(u32) { 0 }, 4, 1, out); // flags - fwrite(&(u16) { ehs }, 2, 1, out); // ELF header size - fwrite(&(u16) { phs }, 2, 1, out); // program header size - fwrite(&(u16) { 1 }, 2, 1, out); // program header count - fwrite(&(u16) { shs }, 2, 1, out); // section header size - fwrite(&(u16) { 0 }, 2, 1, out); // section header count - fwrite(&(u16) { 0 }, 2, 1, out); // string table section header index + #define WRITE_V(...) fwrite(&(u8[]) {__VA_ARGS__}, 1, sizeof((u8[]) {__VA_ARGS__}), out) + #define WRITE_DUP(x, n) fwrite( (u8[n]) { 0 }, 1, n, out) + #define WRITE_2(x) fwrite(&(u16) { x }, 2, 1, out) + #define WRITE_4(x) fwrite(&(u32) { x }, 4, 1, out) + #define WRITE_8(x) fwrite(&(u64) { x }, 8, 1, out) + + WRITE_V( 0x7f, 'E', 'L', 'F' ); // magic + + WRITE_V( 2 ); // elf64 + WRITE_V( 1 ); // 2's complement, little endian + WRITE_V( 1 ); // current version + WRITE_V( 0 ); // ABI = SysV + WRITE_V( 0 ); // ABI version + + WRITE_DUP(0, 7); // padding + + WRITE_2( 2 ); // executable + WRITE_2( 62 ); // x86_64 + WRITE_4( 1 ); // current version + WRITE_8( entry ); // entry point address + WRITE_8( ehs ); // program header offset + WRITE_8( 0 ); // section header offset + WRITE_4( 0 ); // flags + WRITE_2( ehs ); // ELF header size + WRITE_2( phs ); // program header size + WRITE_2( 1 ); // program header count + WRITE_2( shs ); // section header size + WRITE_2( 0 ); // section header count + WRITE_2( 0 ); // string table section header index // Program header // - fwrite(&(u32) { 1 }, 4, 1, out); // type (PT_LOAD) - fwrite(&(u32) { 5 }, 4, 1, out); // flags (PF_X | PF_R) - fwrite(&(u64) { code_offset }, 8, 1, out); // offset - fwrite(&(u64) { entry }, 8, 1, out); // vaddr - fwrite(&(u64) { entry }, 8, 1, out); // paddr - fwrite(&(u64) { code_size }, 8, 1, out); // filesz - fwrite(&(u64) { code_size }, 8, 1, out); // memsz - fwrite(&(u64) { 8 }, 8, 1, out); // align + 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 + + #undef WRITE_V + #undef WRITE_DUP + #undef WRITE_32 + #undef WRITE_64 // Code // @@ -425,7 +430,6 @@ void unit_write(entity_pool_t *pool, i64 unit, i16 target, FILE *out) { fwrite(code, 1, code_size, out); } - // ================================================================ // // Helpers |