From 41807a7f1e7c2336196636ad1a72b1d9c32a8ea5 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Thu, 25 Jul 2024 14:47:50 +0200 Subject: Add library full path search proc --- bxgen.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/bxgen.c b/bxgen.c index eb1887c..3200fc6 100755 --- a/bxgen.c +++ b/bxgen.c @@ -499,6 +499,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); #endif // ================================================================ @@ -2609,8 +2610,10 @@ i64 unit_write_in_memory( BX_LOG(VERBOSE, ".rwdata - %lld bytes", rwdata_size); BX_LOG(VERBOSE, ".rodata - %lld bytes", rodata_size); - BX_CHECK(sym_printf.name_size != 0, "Undefined symbol: printf",); - BX_LOG(VERBOSE, "Found printf: %08llx", sym_printf.address); + if (sym_printf.name_size != 0) + BX_LOG(VERBOSE, "Found printf: %08llx", sym_printf.address); + else + BX_LOG(ERROR, "Undefined symbol: printf"); BX_LOG(VERBOSE, "Writing ELF x86_64 executable"); @@ -3194,11 +3197,38 @@ 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); - unit_set_name(&g_pool, l, bx_str_len(static_library, static_library + MAX_PATH_SIZE), static_library); + i64 l = unit_init(&g_pool, UNIT_LIBRARY_STATIC); + c8 *path = l_find(static_library); + i64 len = bx_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) { + // Find the full path to a library + + BX_CHECK(name != NULL, "Invalid argument", ""); + BX_CHECK(bx_str_len(name, name + MAX_PATH_SIZE) < MAX_PATH_SIZE, "Invalid argument", ""); + + static c8 buf[MAX_PATH_SIZE]; // FIXME + + #define TRY_(template) \ + do { \ + snprintf(buf, sizeof buf, (template), name); \ + FILE *f = fopen(buf, "rb"); \ + if (f == NULL) break; \ + fclose(f); \ + return buf; \ + } while (0) + + TRY_("%s"); + TRY_("lib%s.a"); + TRY_("/lib/x86_64-linux-gnu/%s"); + TRY_("/lib/x86_64-linux-gnu/lib%s.a"); + + BX_FAIL("Library not found", ""); +} + #endif // ================================================================ @@ -3256,8 +3286,8 @@ int main(int argc, char **argv) { // Compile and link // Add a static library. - l_static(u, "/lib/x86_64-linux-gnu/libc.a"); - // l_static(u, "libtest.a"); + l_static(u, "c"); + // l_static(u, "test"); // Write the compilation unit into an executable file. u_elf_x86_64(u, "test_foo"); -- cgit v1.2.3