diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-11-18 18:19:07 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-11-18 18:19:07 +0100 |
commit | 5a5b8e0b1e061b9f5f1b28999d4fbee7cdc6df55 (patch) | |
tree | 75cc5c37b849054dd07c14955a674f1b77b7e5f3 /bxgen.c | |
parent | 394a9698b7cfeb263a8b8fce5d268c80b73a2c87 (diff) | |
download | bxgen-5a5b8e0b1e061b9f5f1b28999d4fbee7cdc6df55.zip |
Add arch detection; Fix page size
Diffstat (limited to 'bxgen.c')
-rwxr-xr-x | bxgen.c | 77 |
1 files changed, 64 insertions, 13 deletions
@@ -117,6 +117,7 @@ gcc \ -Wno-unused-variable \ -Wno-unused-but-set-variable \ -Wno-unused-parameter \ + -Wno-enum-compare \ -O0 \ -fsanitize=undefined,address,leak \ -o $BIN $SRC && \ @@ -215,7 +216,7 @@ enum { MAX_NUM_ENTITIES = 16 * 1024, // For indices - UNDEFINED = -1, + UNDEFINED = -1, // FIXME Make it 0. // Sea of Nodes flow type // @@ -280,7 +281,8 @@ enum { // Formats // - FORMAT_ELF = 1, + FORMAT_UNKNOWN = 0, + FORMAT_ELF, FORMAT_COFF, FORMAT_PE, FORMAT_OMF, @@ -289,7 +291,8 @@ enum { // Architecture // - ARCH_RISC_V = 1, + ARCH_UNKNOWN = 0, + ARCH_RISC_V, ARCH_I386, ARCH_X86_64, ARCH_ARM32, @@ -594,7 +597,7 @@ void u_elf_x86_64(i64 unit, c8 *output_file_name); void l_code(i64 unit, i64 link_unit); void l_object(i64 unit, c8 *object_library); void l_static(i64 unit, c8 *static_library); -c8 * l_find(c8 *name); +c8 * l_find(c8 *name, b8 silent); #define ARGS(...) \ (sizeof((Var[]) { __VA_ARGS__ }) / sizeof(Var)), \ @@ -1557,12 +1560,24 @@ enum { HOST_OS = OS_Unknown, #endif + #if defined(__x86_64) || defined(__amd_64) || defined(_M_AMD64) + HOST_ARCH = ARCH_X86_64, + #elif defined(__i386) || defined(_M_I86) || defined(_X86_) + HOST_ARCH = ARCH_I386, + #elif defined(__aarch64__) + HOST_ARCH = ARCH_ARM64, + #elif defined(__arm__) || defined(__thumb__) || defined(__M_ARM) || defined(__M_ARMT) + HOST_ARCH = ARCH_ARM32, + #else + HOST_ARCH = ARCH_UNKNOWN, + #endif + // x86_64 constants // X86_64_BASE_ADDRESS = 0x400000, X86_64_ALIGNMENT = 8, - X86_64_PAGE_SIZE = 4 * 1024, + X86_64_PAGE_SIZE = 16 * 1024, // 4 * 1024 // ELF format constants // @@ -1575,6 +1590,7 @@ enum { ELF_ABI_VERSION = 0, ELF_RELOCATABLE = 1, ELF_EXECUTABLE = 2, + ELF_AARCH64 = 183, ELF_X86_64 = 62, ELF_HEADER_SIZE = 64, @@ -2598,6 +2614,9 @@ void elf_checks(Buffer_Context b) { u8 *begin = b.begin + b.elf.offset; u8 *end = begin + b.elf.size; + // TODO + CHECK(read_u16(LE, begin + 18, end) != ELF_AARCH64, "ARM64 not implemented",); + u8 osabi = read_u8(LE, begin + 7, end); CHECK( read_u8 (LE, begin, end) == ELF_MAGIC[0], "Invalid ELF file",); @@ -4206,7 +4225,7 @@ void l_code(i64 unit, i64 link_unit) { void l_object(i64 unit, c8 *object_library) { i64 l = unit_init(&g_pool, UNIT_LIBRARY_OBJECT); - c8 *path = l_find(object_library); + c8 *path = l_find(object_library, 0); i64 len = str_len(path, path + MAX_PATH_SIZE); unit_set_name(&g_pool, l, len, path); unit_link_add(&g_pool, unit, l); @@ -4214,15 +4233,17 @@ void l_object(i64 unit, c8 *object_library) { void l_static(i64 unit, c8 *static_library) { i64 l = unit_init(&g_pool, UNIT_LIBRARY_STATIC); - c8 *path = l_find(static_library); + c8 *path = l_find(static_library, 0); i64 len = str_len(path, path + MAX_PATH_SIZE); unit_set_name(&g_pool, l, len, path); unit_link_add(&g_pool, unit, l); } -c8 *l_find(c8 *name) { +c8 *l_find(c8 *name, b8 silent) { // Find the full path to a library + // FIXME: This does not take into account cross-compilation. + CHECK(name != NULL, "Invalid argument", ""); i64 len = str_len(name, name + MAX_PATH_SIZE); @@ -4237,17 +4258,40 @@ c8 *l_find(c8 *name) { FILE *f = fopen(buf, "rb"); \ if (f == NULL) break; \ fclose(f); \ - LOG(VERBOSE, "Found library: %s", buf); \ + if (!silent) \ + LOG(VERBOSE, "Found library: %s", buf); \ return buf; \ } while (0) TRY_("%s"); - TRY_("/lib/x86_64-linux-gnu/%s"); + + if (HOST_OS == OS_Linux) { + TRY_("/lib/%s"); + + if (HOST_ARCH == ARCH_X86_64 || HOST_ARCH == ARCH_ARM64) + TRY_("/lib64/%s"); + + if (HOST_ARCH == ARCH_X86_64) + TRY_("/lib/x86_64-linux-gnu/%s"); + } TRY_("lib%s.a"); TRY_("%s.o"); - TRY_("/lib/x86_64-linux-gnu/lib%s.a"); - TRY_("/lib/x86_64-linux-gnu/%s.o"); + + if (HOST_OS == OS_Linux) { + TRY_("/lib/lib%s.a"); + TRY_("/lib/%s.o"); + + if (HOST_ARCH == ARCH_X86_64 || HOST_ARCH == ARCH_ARM64) { + TRY_("/lib64/lib%s.a"); + TRY_("/lib64/%s.o"); + } + + if (HOST_ARCH == ARCH_X86_64) { + TRY_("/lib/x86_64-linux-gnu/lib%s.a"); + TRY_("/lib/x86_64-linux-gnu/%s.o"); + } + } #undef TRY_ @@ -4294,10 +4338,17 @@ b8 link_with_libc(void) { // ============================================================ // // Compile and link + // + // TODO // Add dependencies - l_static(u, "c"); + + if (l_find("c_nonshared", 1)[0] != '\0') + l_static(u, "c_nonshared"); + else + l_static(u, "c"); l_object(u, "crt1"); + // l_object(u, "crti"); // l_object(u, "crtn"); |