summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-07-17 21:16:53 +0200
committerMitya Selivanov <automainint@guattari.tech>2024-07-17 21:16:53 +0200
commit640bf3fb685c174466bf453b3ad0dc1e9e3a6855 (patch)
treead65fc4cc7f28cbd14aa186a5c8d04649b45b1ab
parentfcccfe7dd006abb3833220f48837ab5e8b03f233 (diff)
downloadbxgen-640bf3fb685c174466bf453b3ad0dc1e9e3a6855.zip
Cleanup
-rwxr-xr-xbxgen.c115
1 files changed, 63 insertions, 52 deletions
diff --git a/bxgen.c b/bxgen.c
index af34b1a..226b1de 100755
--- a/bxgen.c
+++ b/bxgen.c
@@ -210,6 +210,24 @@ enum {
IO_SEEK_CURSOR = 0,
IO_SEEK_BEGIN,
IO_SEEK_END,
+
+ // Formats
+ //
+
+ FORMAT_ELF = 1,
+ FORMAT_COFF,
+ FORMAT_PE,
+ FORMAT_OMF,
+ FORMAT_MATCH_O,
+
+ // Architecture
+ //
+
+ ARCH_RISC_V = 64,
+ ARCH_I386,
+ ARCH_X86_64,
+ ARCH_ARM32,
+ ARCH_ARM64,
};
// TODO
@@ -332,6 +350,11 @@ typedef struct {
extern "C" {
#endif
+// Hooks. Shoud be implemented on the user side
+void bx_log(i32 log_level, u32 line, c8 *file, c8 *format, ...);
+void bx_assert(b8 condition, c8 *message, u32 line, c8 *file);
+void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data);
+
i64 pool_add(Pool *pool, Entity data);
void pool_remove(Pool *pool, i64 entity, i16 type);
@@ -368,9 +391,7 @@ i64 io_read(i64 f, i64 size, void *data, void *user_data);
i64 io_write(i64 f, i64 size, void *data, void *user_data);
void io_chmod_exe(i64 f, void *user_data);
-void bx_log(i32 log_level, u32 line, c8 *file, c8 *format, ...);
-void bx_assert(b8 condition, c8 *message, u32 line, c8 *file);
-void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data);
+// ================================================================
#ifndef DISABLE_HELPERS
i64 n_i64(i64 value);
@@ -482,10 +503,10 @@ b8 bx_mem_eq(void *a, void *b, i64 size) {
return 1;
}
-i64 bx_str_len(c8 *s, c8 *s_max) {
- BX_CHECK(s < s_max, "Buffer overflow", 0);
+i64 bx_str_len(c8 *s, c8 *s_end) {
+ BX_CHECK(s < s_end, "Buffer overflow", 0);
- for (i64 len = 0; s + len < s_max; ++len)
+ for (i64 len = 0; s + len < s_end; ++len)
if (s[len] == '\0')
return len;
@@ -510,6 +531,19 @@ c8 *bx_find_char(c8 *s, c8 *s_end, c8 c) {
return *s == c ? s : NULL;
}
+u64 bx_u64_from_str(c8 *s, c8 *s_end) {
+ BX_CHECK(s != NULL && s_end != NULL, "Invalid arguments", 0);
+ BX_CHECK(s < s_end, "Buffer overflow", 0);
+ BX_CHECK(*s >= '0' && *s <= '9', "Parsing failed", 0);
+
+ i64 x = 0;
+ for (; s < s_end && *s >= '0' && *s <= '9'; ++s)
+ x = (x * 10) + (*s - '0');
+
+ BX_LAX(s == s_end || *s == ' ' || *s == '\0', "Parsing failed");
+ return x;
+}
+
// ================================================================
//
// * Semantic graph
@@ -832,8 +866,9 @@ void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) {
//
// Terms
//
-// LO = little endian
-// BE = big endian
+// LE = little endian
+// BE = big endian
+// HO = host ordering
//
// byte = 8 bits
// word = 2 bytes
@@ -1151,9 +1186,6 @@ void write_f64(u32 ordering, f64 x, void *v, void *v_end) {
//
// ================================================================
-#include <stdio.h> // TEMP
-#include <stdlib.h> // TEMP
-
enum {
HOST_Unknown = 0,
HOST_Unix,
@@ -1176,24 +1208,6 @@ enum {
HOST = HOST_Unknown,
#endif
- // Formats
- //
-
- FORMAT_ELF = 1,
- FORMAT_COFF,
- FORMAT_PE,
- FORMAT_OMF,
- FORMAT_MATCH_O,
-
- // Architecture
- //
-
- ARCH_RISC_V = 64,
- ARCH_I386,
- ARCH_X86_64,
- ARCH_ARM32,
- ARCH_ARM64,
-
// x86_64 constants
//
@@ -1245,19 +1259,19 @@ enum {
BIND_WEAK,
};
-c8 ELF_MAGIC[4] = "\x7f" "ELF";
-c8 AR_MAGIC[8] = "!<arch>\n";
-c8 AR_SYMBOL_TABLE[] = "/ ";
-c8 AR_STRING_TABLE[] = "// ";
+c8 ELF_MAGIC[4] = "\x7f" "ELF";
+c8 AR_MAGIC[8] = "!<arch>\n";
+c8 AR_SYMBOL_TABLE[] = "/ ";
+c8 AR_STRING_TABLE[] = "// ";
-c8 SECTION_TEXT[] = ".text";
-c8 SECTION_RELA_TEXT[] = ".rela.text";
-c8 SECTION_DATA[] = ".data";
-c8 SECTION_BSS[] = ".bss";
-c8 SECTION_RODATA[] = ".rodata";
-c8 SECTION_SYMTAB[] = ".symtab";
-c8 SECTION_STRTAB[] = ".strtab";
-c8 SECTION_SHSTRTAB[] = ".shstrtab";
+c8 SECTION_TEXT[] = ".text";
+c8 SECTION_RELA_TEXT[] = ".rela.text";
+c8 SECTION_DATA[] = ".data";
+c8 SECTION_BSS[] = ".bss";
+c8 SECTION_RODATA[] = ".rodata";
+c8 SECTION_SYMTAB[] = ".symtab";
+c8 SECTION_STRTAB[] = ".strtab";
+c8 SECTION_SHSTRTAB[] = ".shstrtab";
typedef struct {
u64 offset;
@@ -1641,6 +1655,8 @@ c8 *elf_name_from_offset(
return begin;
}
+#include <stdio.h> // TEMP
+
void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data) {
BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",);
@@ -1782,35 +1798,30 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u8 *f_end = f_begin + 58;
u8 *f_data = f_begin + 60;
- i64 fn_size;
- {
- c8 buf[7] = {0};
- bx_mem_cpy(buf, f_size, 6);
- fn_size = atoi(buf);
- }
+ u64 size = bx_u64_from_str((c8 *) f_size, (c8 *) f_size + 10);
- fn_size = bx_align(fn_size, 2);
+ size = bx_align(size, 2);
BX_CHECK(bx_mem_eq(f_end, "\x60\x0a", 2), "Invalid AR file",);
- BX_CHECK(f_begin + fn_size <= ar_end, "Buffer overflow",);
+ BX_CHECK(f_begin + size <= ar_end, "Buffer overflow",);
if (!bx_mem_eq(f_id, AR_SYMBOL_TABLE, 16) &&
!bx_mem_eq(f_id, AR_STRING_TABLE, 16)) {
// Read ELF object file
- i64 delta_size = bx_align(fn_size, X86_64_ALIGNMENT);
+ i64 delta_size = bx_align(size, X86_64_ALIGNMENT);
BX_CHECK(im_size + delta_size < (i64) (sizeof im_buffer), "Out of memory",);
BX_CHECK(im_num + 1 < (i64) (sizeof im_offsets / sizeof *im_offsets), "Out of memory",);
- bx_mem_cpy(im_buffer + im_size, f_data, fn_size);
+ bx_mem_cpy(im_buffer + im_size, f_data, size);
im_offsets[im_num] = im_size;
im_size += delta_size;
im_offsets[++im_num] = im_size;
}
- f_begin = f_data + fn_size;
+ f_begin = f_data + size;
}
}