From: yu.dongliang <18588496441@163.com> Date: Tue, 18 Apr 2023 07:33:41 +0000 (+0800) Subject: fix linker can't find libc.so.6 in fedora, and update the main()'s argv parser X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=f23ce949c872d0081f7bd458d6b83e405c489fe8;p=scf.git fix linker can't find libc.so.6 in fedora, and update the main()'s argv parser --- diff --git a/elf/scf_elf_arm64_so.c b/elf/scf_elf_arm64_so.c index 6745089..59f687b 100644 --- a/elf/scf_elf_arm64_so.c +++ b/elf/scf_elf_arm64_so.c @@ -539,7 +539,7 @@ int __arm64_elf_add_dyn (elf_native_t* arm64) Elf64_Dyn* dyns = (Elf64_Dyn*)arm64->dynamic->data; - size_t prefix = strlen("../lib/arm64"); + size_t prefix = strlen("../lib/arm64/"); for (i = 0; i < arm64->dyn_needs->size; i++) { scf_string_t* needed = arm64->dyn_needs->data[i]; diff --git a/elf/scf_elf_naja_so.c b/elf/scf_elf_naja_so.c index 6e2fb8c..46471be 100644 --- a/elf/scf_elf_naja_so.c +++ b/elf/scf_elf_naja_so.c @@ -540,7 +540,7 @@ int __naja_elf_add_dyn (elf_native_t* naja) Elf64_Dyn* dyns = (Elf64_Dyn*)naja->dynamic->data; - size_t prefix = strlen("../lib/arm64"); + size_t prefix = strlen("../lib/arm64/"); for (i = 0; i < naja->dyn_needs->size; i++) { scf_string_t* needed = naja->dyn_needs->data[i]; diff --git a/elf/scf_elf_x64_so.c b/elf/scf_elf_x64_so.c index 2a92e9a..ab4f21d 100644 --- a/elf/scf_elf_x64_so.c +++ b/elf/scf_elf_x64_so.c @@ -543,13 +543,17 @@ int __x64_elf_add_dyn (elf_native_t* x64) Elf64_Dyn* dyns = (Elf64_Dyn*)x64->dynamic->data; + size_t prefix = strlen("../lib/x64/"); + for (i = 0; i < x64->dyn_needs->size; i++) { scf_string_t* needed = x64->dyn_needs->data[i]; dyns[i].d_tag = DT_NEEDED; dyns[i].d_un.d_val = str->len; - scf_string_cat_cstr_len(str, needed->data, needed->len + 1); + scf_logw("i: %d, %s, %s\n", i, needed->data, needed->data + prefix); + + scf_string_cat_cstr_len(str, needed->data + prefix, needed->len - prefix + 1); } dyns[i].d_tag = DT_STRTAB; diff --git a/lib/arm64/lib/aarch64-linux-gnu/libc.so.6 b/lib/arm64/libc.so.6 similarity index 100% rename from lib/arm64/lib/aarch64-linux-gnu/libc.so.6 rename to lib/arm64/libc.so.6 diff --git a/lib/x64/lib64/ld-linux-x86-64.so.2 b/lib/x64/lib64/ld-linux-x86-64.so.2 new file mode 100755 index 0000000..a8cb029 Binary files /dev/null and b/lib/x64/lib64/ld-linux-x86-64.so.2 differ diff --git a/lib/x64/libc.so.6 b/lib/x64/libc.so.6 new file mode 100755 index 0000000..f5b26e3 Binary files /dev/null and b/lib/x64/libc.so.6 differ diff --git a/parse/main.c b/parse/main.c index aae7583..2ee3400 100644 --- a/parse/main.c +++ b/parse/main.c @@ -12,8 +12,8 @@ static char* __objs[] = static char* __sofiles[] = { - "/lib64/ld-linux-x86-64.so.2", - "/lib/x86_64-linux-gnu/libc.so.6", + "../lib/x64//lib64/ld-linux-x86-64.so.2", + "../lib/x64/libc.so.6", }; static char* __arm64_objs[] = @@ -23,53 +23,66 @@ static char* __arm64_objs[] = static char* __arm64_sofiles[] = { - "../lib/arm64/lib/ld-linux-aarch64.so.1", - "../lib/arm64/lib/aarch64-linux-gnu/libc.so.6", + "../lib/arm64//lib/ld-linux-aarch64.so.1", + "../lib/arm64/libc.so.6", }; void usage(char* path) { - fprintf(stderr, "Usage: %s [-c] [-a arch] src0 [src1] [-o out]\n\n", path); + fprintf(stderr, "Usage: %s [-c] [-a arch] [-o out] src0 [src1]\n\n", path); fprintf(stderr, "-c: only compile, not link\n"); fprintf(stderr, "-a: select cpu arch (x64 or arm64), default is x64\n"); } int main(int argc, char* argv[]) { - int opt; - int link = 1; + scf_vector_t* afiles = scf_vector_alloc(); + scf_vector_t* sofiles = scf_vector_alloc(); + scf_vector_t* srcs = scf_vector_alloc(); + scf_vector_t* objs = scf_vector_alloc(); + char* out = NULL; char* arch = "x64"; + int link = 1; - while ((opt = getopt(argc, argv, "coa:")) != -1) { - switch (opt) { - case 'c': + int i; + + for (i = 1; i < argc; i++) { + + if ('-' == argv[i][0]) { + + if ('c' == argv[i][1]) { link = 0; - break; - case 'o': - out = optarg; - break; - case 'a': - arch = optarg; - break; - default: - usage(argv[0]); - break; - } - } + continue; + } - if (optind >= argc) { - usage(argv[0]); - return -1; - } + if ('a' == argv[i][1]) { - scf_vector_t* afiles = scf_vector_alloc(); - scf_vector_t* sofiles = scf_vector_alloc(); - scf_vector_t* srcs = scf_vector_alloc(); - scf_vector_t* objs = scf_vector_alloc(); + if (++i >= argc) { + usage(argv[0]); + return -EINVAL; + } + + arch = argv[i]; + continue; + } + + if ('o' == argv[i][1]) { - while (optind < argc) { - char* fname = argv[optind]; + if (++i >= argc) { + usage(argv[0]); + return -EINVAL; + } + + out = argv[i]; + continue; + } + + usage(argv[0]); + return -EINVAL; + } + + char* fname = argv[i]; size_t len = strlen(fname); if (len < 3) { @@ -77,6 +90,8 @@ int main(int argc, char* argv[]) return -1; } + scf_loge("fname: %s\n", fname); + scf_vector_t* vec; if (!strcmp(fname + len - 2, ".a")) @@ -97,8 +112,6 @@ int main(int argc, char* argv[]) if (scf_vector_add(vec, fname) < 0) return -ENOMEM; - - optind++; } scf_parse_t* parse = NULL; @@ -108,7 +121,6 @@ int main(int argc, char* argv[]) return -1; } - int i; for (i = 0; i < srcs->size; i++) { char* file = srcs->data[i]; diff --git a/vm/scf_vm_naja.c b/vm/scf_vm_naja.c index 1641d96..de0eb16 100644 --- a/vm/scf_vm_naja.c +++ b/vm/scf_vm_naja.c @@ -2,8 +2,8 @@ static const char* somaps[][3] = { - {"x64", "/lib/ld-linux-aarch64.so.1", "/lib64/ld-linux-x86-64.so.2"}, - {"x64", "/lib/aarch64-linux-gnu/libc.so.6", "/lib/x86_64-linux-gnu/libc.so.6"}, + {"x64", "/lib/ld-linux-aarch64.so.1", "/lib64/ld-linux-x86-64.so.2"}, + {"x64", "libc.so.6", "/lib/x86_64-linux-gnu/libc.so.6"}, }; typedef int (*dyn_func_pt)(uint64_t r0,