support [-s sysroot] of main() argv to select the sysroot dir
authoryu.dongliang <18588496441@163.com>
Mon, 2 Sep 2024 08:52:12 +0000 (16:52 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 2 Sep 2024 08:52:12 +0000 (16:52 +0800)
26 files changed:
elf/scf_elf.c
elf/scf_elf.h
elf/scf_elf_arm32.c
elf/scf_elf_arm32.h
elf/scf_elf_arm32_so.c
elf/scf_elf_arm64.c
elf/scf_elf_arm64.h
elf/scf_elf_arm64_so.c
elf/scf_elf_link.c
elf/scf_elf_link.h
elf/scf_elf_naja.c
elf/scf_elf_naja.h
elf/scf_elf_naja_so.c
elf/scf_elf_x64.c
elf/scf_elf_x64.h
elf/scf_elf_x64_so.c
lib/scf_abort.o [deleted file]
lib/scf_abort.s [deleted file]
lib/scf_syscall.o [deleted file]
lib/x64/_start.o [moved from lib/_start.o with 100% similarity]
lib/x64/_start.s [moved from lib/_start.s with 100% similarity]
lib/x64/scf_atomic.o [moved from lib/scf_atomic.o with 100% similarity]
lib/x64/scf_atomic.s [moved from lib/scf_atomic.s with 100% similarity]
lib/x64/scf_object.o [moved from lib/scf_object.o with 100% similarity]
lib/x64/scf_syscall.s [moved from lib/scf_syscall.s with 100% similarity]
parse/main.c

index c1ba4c7982aecac1fbcdbe4f9ebc27179b85de2d..238d2a27eb0d65dd067b0f3036f68324315d4be9 100644 (file)
@@ -226,12 +226,11 @@ int scf_elf_write_rel(scf_elf_context_t* elf)
        return -1;
 }
 
-int scf_elf_write_exec(scf_elf_context_t* elf)
+int scf_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 {
-       if (elf && elf->ops && elf->ops->write_rel)
-               return elf->ops->write_exec(elf);
+       if (elf && elf->ops && elf->ops->write_rel && sysroot)
+               return elf->ops->write_exec(elf, sysroot);
 
        scf_loge("\n");
        return -1;
 }
-
index cc9717f193d696e9c255ccc6e4d2e204a7203b3d..4face4828b27e7b6fb9e8d5914cb45b4f648e452 100644 (file)
@@ -71,7 +71,7 @@ struct scf_elf_ops_s
        int                 (*add_dyn_rela)(scf_elf_context_t* elf, const scf_elf_rela_t* rela);
 
        int                             (*write_rel )(scf_elf_context_t* elf);
-       int                             (*write_exec)(scf_elf_context_t* elf);
+       int                             (*write_exec)(scf_elf_context_t* elf, const char* sysroot);
 };
 
 struct scf_elf_context_s {
@@ -106,7 +106,6 @@ int scf_elf_read_relas(scf_elf_context_t* elf, scf_vector_t* relas, const char*
 int scf_elf_read_phdrs(scf_elf_context_t* elf, scf_vector_t* phdrs);
 
 int scf_elf_write_rel( scf_elf_context_t* elf);
-int scf_elf_write_exec(scf_elf_context_t* elf);
+int scf_elf_write_exec(scf_elf_context_t* elf, const char* sysroot);
 
 #endif
-
index cdedc559310c30fab5cde96e0f4d27836e9156c1..dcbae44f49858f386c3479ae787b890ca88434a3 100644 (file)
@@ -207,13 +207,13 @@ static int _arm32_elf_link_sections(elf_native_t* arm32, uint32_t cs_index, uint
        return 0;
 }
 
-static int _arm32_elf_write_exec(scf_elf_context_t* elf)
+static int _arm32_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 {
        elf_native_t* arm32    = elf->priv;
        int               nb_phdrs = 3;
 
        if (arm32->dynsyms && arm32->dynsyms->size) {
-               __arm32_elf_add_dyn(arm32);
+               __arm32_elf_add_dyn(arm32, sysroot);
                nb_phdrs = 6;
        }
 
@@ -460,4 +460,3 @@ scf_elf_ops_t       elf_ops_arm32 =
        .write_rel            = _arm32_elf_write_rel,
        .write_exec       = _arm32_elf_write_exec,
 };
-
index bc606f318aefc6d7d9c5e1236f0273932d29fe65..b6901fd6c105f4f61eca9cec8ea464eb142f647a 100644 (file)
@@ -6,7 +6,7 @@
 #include"scf_vector.h"
 #include"scf_string.h"
 
-int __arm32_elf_add_dyn (elf_native_t* arm32);
+int __arm32_elf_add_dyn (elf_native_t* arm32, const char* sysroot);
 int __arm32_elf_post_dyn(elf_native_t* arm32, uint64_t rx_base, uint64_t rw_base, elf_section_t* cs);
 
 int __arm32_elf_write_phdr   (scf_elf_context_t* elf, uint64_t rx_base, uint64_t offset, uint32_t nb_phdrs);
index 60f078b6f9504d2e8757afef6ddd412f08b4ea8d..3a3dbd164c5d206042fca0f4c4037dcc1c756876 100644 (file)
@@ -420,7 +420,7 @@ static int _section_cmp(const void* v0, const void* v1)
        return 0;
 }
 
-int __arm32_elf_add_dyn (elf_native_t* arm32)
+int __arm32_elf_add_dyn (elf_native_t* arm32, const char* sysroot)
 {
        elf_section_t* s;
        elf_sym_t*     sym;
@@ -537,7 +537,12 @@ int __arm32_elf_add_dyn (elf_native_t* arm32)
 
        Elf32_Dyn* dyns = (Elf32_Dyn*)arm32->dynamic->data;
 
-       size_t prefix   = strlen("../lib/arm32/");
+       size_t prefix   = strlen(sysroot);
+
+       if ('/' != sysroot[prefix - 1])
+               prefix++;
+
+       prefix += strlen("arm32/");
 
        for (i = 0; i < arm32->dyn_needs->size; i++) {
                scf_string_t* needed = arm32->dyn_needs->data[i];
index f8e14ef0e0a003a28935f4a6e69a144c9b3b9092..07a590115c4eb37d6be7cece52a560ec9a617806 100644 (file)
@@ -220,13 +220,13 @@ static int _arm64_elf_link_sections(elf_native_t* arm64, uint32_t cs_index, uint
        return 0;
 }
 
-static int _arm64_elf_write_exec(scf_elf_context_t* elf)
+static int _arm64_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 {
        elf_native_t* arm64    = elf->priv;
        int               nb_phdrs = 3;
 
        if (arm64->dynsyms && arm64->dynsyms->size) {
-               __arm64_elf_add_dyn(arm64);
+               __arm64_elf_add_dyn(arm64, sysroot);
                nb_phdrs = 6;
        }
 
@@ -472,4 +472,3 @@ scf_elf_ops_t       elf_ops_arm64 =
        .write_rel            = _arm64_elf_write_rel,
        .write_exec       = _arm64_elf_write_exec,
 };
-
index 7eae07fe011d6ebcd28ed920ba461dd2854764a0..df68ea02d345dbc417c0948e11fc5d9f5ee25c5d 100644 (file)
@@ -6,7 +6,7 @@
 #include"scf_vector.h"
 #include"scf_string.h"
 
-int __arm64_elf_add_dyn (elf_native_t* arm64);
+int __arm64_elf_add_dyn (elf_native_t* arm64, const char* sysroot);
 int __arm64_elf_post_dyn(elf_native_t* arm64, uint64_t rx_base, uint64_t rw_base, elf_section_t* cs);
 
 int __arm64_elf_write_phdr   (scf_elf_context_t* elf, uint64_t rx_base, uint64_t offset, uint32_t nb_phdrs);
index 59f687b607f5b62ab609137fc0ee7b10a2512037..0f4a0f2d5bbc4d8b5823265015f41f545685f1ae 100644 (file)
@@ -422,11 +422,11 @@ static int _section_cmp(const void* v0, const void* v1)
        return 0;
 }
 
-int __arm64_elf_add_dyn (elf_native_t* arm64)
+int __arm64_elf_add_dyn(elf_native_t* arm64, const char* sysroot)
 {
        elf_section_t* s;
        elf_sym_t*     sym;
-       Elf64_Rela*            rela;
+       Elf64_Rela*    rela;
 
        int i;
        for (i  = arm64->symbols->size - 1; i >= 0; i--) {
@@ -539,7 +539,12 @@ 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(sysroot);
+
+       if ('/' != sysroot[prefix - 1])
+               prefix++;
+
+       prefix += strlen("arm64/");
 
        for (i = 0; i < arm64->dyn_needs->size; i++) {
                scf_string_t* needed = arm64->dyn_needs->data[i];
index 15a999a2cb66045784857a434caa789dcec1856f..311982f26d9cc19a15744c2a5957a40baf1bcb2f 100644 (file)
@@ -1036,7 +1036,7 @@ static int link_relas(scf_elf_file_t* exec, char* afiles[], int nb_afiles, char*
        return 0;
 }
 
-int scf_elf_link(scf_vector_t* objs, scf_vector_t* afiles, scf_vector_t* sofiles, const char* arch, const char* out)
+int scf_elf_link(scf_vector_t* objs, scf_vector_t* afiles, scf_vector_t* sofiles, const char* sysroot, const char* arch, const char* out)
 {
        scf_elf_file_t* exec = NULL;
        scf_elf_file_t* so   = NULL;
@@ -1171,7 +1171,7 @@ int scf_elf_link(scf_vector_t* objs, scf_vector_t* afiles, scf_vector_t* sofiles
        ADD_RELA_SECTION(debug_info, SCF_ELF_FILE_SHNDX(debug_info));
        ADD_RELA_SECTION(debug_line, SCF_ELF_FILE_SHNDX(debug_line));
 
-       ret = scf_elf_write_exec(exec->elf);
+       ret = scf_elf_write_exec(exec->elf, sysroot);
        if (ret < 0)
                return ret;
 
@@ -1179,4 +1179,3 @@ int scf_elf_link(scf_vector_t* objs, scf_vector_t* afiles, scf_vector_t* sofiles
 
        return 0;
 }
-
index e7507037b16cdeb6cd6ba60f78eaccc968272904..d976dcc30b6d79963a3b73bb76317afc1e4b290c 100644 (file)
@@ -61,7 +61,6 @@ typedef struct {
 
 int scf_elf_file_close(scf_elf_file_t* ef, void (*rela_free)(void*), void (*sym_free)(void*));
 
-int scf_elf_link(scf_vector_t* objs, scf_vector_t* afiles, scf_vector_t* sofiles, const char* arch, const char* out);
+int scf_elf_link(scf_vector_t* objs, scf_vector_t* afiles, scf_vector_t* sofiles, const char* sysroot, const char* arch, const char* out);
 
 #endif
-
index e62bbe619b4dea5e746c50f8f4b1ac2d5d06a838..dd20336d01b99a7c7957a8a7429a67475c24409f 100644 (file)
@@ -220,13 +220,13 @@ static int _naja_elf_link_sections(elf_native_t* naja, uint32_t cs_index, uint32
        return 0;
 }
 
-static int _naja_elf_write_exec(scf_elf_context_t* elf)
+static int _naja_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 {
        elf_native_t* naja    = elf->priv;
        int               nb_phdrs = 3;
 
        if (naja->dynsyms && naja->dynsyms->size) {
-               __naja_elf_add_dyn(naja);
+               __naja_elf_add_dyn(naja, sysroot);
                nb_phdrs = 6;
        }
 
@@ -473,4 +473,3 @@ scf_elf_ops_t       elf_ops_naja =
        .write_rel            = _naja_elf_write_rel,
        .write_exec       = _naja_elf_write_exec,
 };
-
index ab586b78c71484367f78924d412de91a9c2616ca..52eb9155a038157d86fe6ea894b27c1387d2b794 100644 (file)
@@ -6,7 +6,7 @@
 #include"scf_vector.h"
 #include"scf_string.h"
 
-int __naja_elf_add_dyn (elf_native_t* naja);
+int __naja_elf_add_dyn (elf_native_t* naja, const char* sysroot);
 int __naja_elf_post_dyn(elf_native_t* naja, uint64_t rx_base, uint64_t rw_base, elf_section_t* cs);
 
 int __naja_elf_write_phdr   (scf_elf_context_t* elf, uint64_t rx_base, uint64_t offset, uint32_t nb_phdrs);
index 46471be6b9fd5877514401f1a55aae94ffab5314..31690c5318c124ca908b0c877031931103232e21 100644 (file)
@@ -423,11 +423,11 @@ static int _section_cmp(const void* v0, const void* v1)
        return 0;
 }
 
-int __naja_elf_add_dyn (elf_native_t* naja)
+int __naja_elf_add_dyn(elf_native_t* naja, const char* sysroot)
 {
        elf_section_t* s;
        elf_sym_t*     sym;
-       Elf64_Rela*            rela;
+       Elf64_Rela*    rela;
 
        int i;
        for (i  = naja->symbols->size - 1; i >= 0; i--) {
@@ -540,7 +540,12 @@ 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(sysroot);
+
+       if ('/' != sysroot[prefix - 1])
+               prefix++;
+
+       prefix += strlen("arm64/");
 
        for (i = 0; i < naja->dyn_needs->size; i++) {
                scf_string_t* needed = naja->dyn_needs->data[i];
index 41e69451e1308cc4145943bdc00b939863ae88c6..752645fe0881de6ba8aaf8306b418899eb5d9682 100644 (file)
@@ -254,13 +254,13 @@ static void _x64_elf_process_syms(elf_native_t* x64, uint32_t cs_index)
        }
 }
 
-static int _x64_elf_write_exec(scf_elf_context_t* elf)
+static int _x64_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 {
        elf_native_t* x64      = elf->priv;
        int               nb_phdrs = 3;
 
        if (x64->dynsyms && x64->dynsyms->size) {
-               __x64_elf_add_dyn(x64);
+               __x64_elf_add_dyn(x64, sysroot);
                nb_phdrs = 6;
        }
 
index ddc828d1ffeac360cd8767ef081bfd9ac83419f1..ee1aa19cf51448a05762e119c0ed5bfaeaea6b13 100644 (file)
@@ -4,7 +4,7 @@
 #include"scf_elf.h"
 #include"scf_elf_native.h"
 
-int __x64_elf_add_dyn (elf_native_t* x64);
+int __x64_elf_add_dyn (elf_native_t* x64, const char* sysroot);
 int __x64_elf_post_dyn(elf_native_t* x64, uint64_t rx_base, uint64_t rw_base, elf_section_t* cs);
 
 int __x64_elf_write_phdr   (scf_elf_context_t* elf, uint64_t rx_base, uint64_t offset, uint32_t nb_phdrs);
index b4685601256cb3bf2a768603af3cc968bed2d128..a764444fe530988b0dcc3a5c02933567de41fc19 100644 (file)
@@ -426,7 +426,7 @@ static int _section_cmp(const void* v0, const void* v1)
        return 0;
 }
 
-int __x64_elf_add_dyn (elf_native_t* x64)
+int __x64_elf_add_dyn(elf_native_t* x64, const char* sysroot)
 {
        elf_section_t* s;
        elf_sym_t*     sym;
@@ -543,7 +543,12 @@ int __x64_elf_add_dyn (elf_native_t* x64)
 
        Elf64_Dyn* dyns = (Elf64_Dyn*)x64->dynamic->data;
 
-       size_t prefix   = strlen("../lib/x64/");
+       size_t prefix   = strlen(sysroot);
+
+       if ('/' != sysroot[prefix - 1])
+               prefix++;
+
+       prefix += strlen("x64/");
 
        for (i = 0; i < x64->dyn_needs->size; i++) {
                scf_string_t* needed = x64->dyn_needs->data[i];
diff --git a/lib/scf_abort.o b/lib/scf_abort.o
deleted file mode 100644 (file)
index 6971c0b..0000000
Binary files a/lib/scf_abort.o and /dev/null differ
diff --git a/lib/scf_abort.s b/lib/scf_abort.s
deleted file mode 100644 (file)
index 0b4da7b..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-.text
-.global scf__abort
-
-scf__abort:
-       xorq %rax,  %rax
-       mov  %rax, (%rax)
-       ret
-.fill 1, 1, 0
diff --git a/lib/scf_syscall.o b/lib/scf_syscall.o
deleted file mode 100644 (file)
index 0f6c879..0000000
Binary files a/lib/scf_syscall.o and /dev/null differ
similarity index 100%
rename from lib/_start.o
rename to lib/x64/_start.o
similarity index 100%
rename from lib/_start.s
rename to lib/x64/_start.s
similarity index 100%
rename from lib/scf_atomic.o
rename to lib/x64/scf_atomic.o
similarity index 100%
rename from lib/scf_atomic.s
rename to lib/x64/scf_atomic.s
similarity index 100%
rename from lib/scf_object.o
rename to lib/x64/scf_object.o
similarity index 100%
rename from lib/scf_syscall.s
rename to lib/x64/scf_syscall.s
index 8f7bb543f9465013ce22a7dc91df240f64f3bb79..cd3a7d128d25ce23819f375f228911af116f378c 100644 (file)
@@ -5,45 +5,88 @@
 
 static char* __objs[] =
 {
-       "../lib/_start.o",
-       "../lib/scf_object.o",
-       "../lib/scf_atomic.o",
+       "_start.o",
+       "scf_object.o",
+       "scf_atomic.o",
 };
 
 static char* __sofiles[] =
 {
-       "../lib/x64//lib64/ld-linux-x86-64.so.2",
-       "../lib/x64/libc.so.6",
+       "/lib64/ld-linux-x86-64.so.2",
+       "libc.so.6",
 };
 
 static char* __arm64_objs[] =
 {
-       "../lib/arm64/_start.o",
+       "_start.o",
 };
 
 static char* __arm64_sofiles[] =
 {
-       "../lib/arm64//lib/ld-linux-aarch64.so.1",
-       "../lib/arm64/libc.so.6",
+       "/lib/ld-linux-aarch64.so.1",
+       "libc.so.6",
 };
 
 static char* __arm32_objs[] =
 {
-       "../lib/arm32/_start.o",
+       "_start.o",
 };
 
 static char* __arm32_sofiles[] =
 {
-       "../lib/arm32//lib/ld-linux-armhf.so.3",
-       "../lib/arm32/libc.so.6",
+       "/lib/ld-linux-armhf.so.3",
+       "libc.so.6",
 };
 
 void usage(char* path)
 {
-       fprintf(stderr, "Usage: %s [-c] [-t] [-a arch] [-o out] src0 [src1]\n\n", path);
+       fprintf(stderr, "Usage: %s [-c] [-t] [-a arch] [-s sysroot] [-o out] src0 [src1]\n\n", path);
        fprintf(stderr, "-c: only compile,  not link\n");
        fprintf(stderr, "-t: only 3ac code, not compile\n");
        fprintf(stderr, "-a: select cpu arch (x64, arm64, naja, or eda), default is x64\n");
+       fprintf(stderr, "-s: sysroot dir, default is '../lib'\n");
+}
+
+int add_sys_files(scf_vector_t* vec, const char* sysroot, const char* arch, char* files[], int n_files)
+{
+       int len0 = strlen(sysroot);
+       int len1 = strlen(arch);
+       int i;
+
+       for (i = 0; i < n_files; i++) {
+
+               int len2 = strlen(files[i]);
+
+               char* fname = calloc(1, len0 + len1 + len2 + 3);
+               if (!fname)
+                       return -ENOMEM;
+
+               int len = len0;
+               memcpy(fname, sysroot, len0);
+               if (fname[len - 1] != '/') {
+                       fname[len]      = '/';
+                       len++;
+               }
+
+               memcpy(fname + len, arch, len1);
+               len += len1;
+               if (fname[len - 1] != '/') {
+                       fname[len]      = '/';
+                       len++;
+               }
+
+               memcpy(fname + len, files[i], len2);
+               len += len2;
+               fname[len] = '\0';
+
+               scf_logi("add file: %s\n", fname);
+
+               int ret = scf_vector_add(vec, fname);
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
 }
 
 int main(int argc, char* argv[])
@@ -58,10 +101,11 @@ int main(int argc, char* argv[])
        scf_vector_t* srcs    = scf_vector_alloc();
        scf_vector_t* objs    = scf_vector_alloc();
 
-       char* out  = NULL;
-       char* arch = "x64";
-       int   link = 1;
-       int   _3ac = 0;
+       char* sysroot = "../lib";
+       char* arch    = "x64";
+       char* out     = NULL;
+       int   link    = 1;
+       int   _3ac    = 0;
 
        int   i;
 
@@ -91,6 +135,17 @@ int main(int argc, char* argv[])
                                continue;
                        }
 
+                       if ('s' == argv[i][1]) {
+
+                               if (++i >= argc) {
+                                       usage(argv[0]);
+                                       return -EINVAL;
+                               }
+
+                               sysroot = argv[i];
+                               continue;
+                       }
+
                        if ('o' == argv[i][1]) {
 
                                if (++i >= argc) {
@@ -180,19 +235,13 @@ int main(int argc, char* argv[])
 
 #define MAIN_ADD_FILES(_objs, _sofiles) \
        do { \
-               for (i  = 0; i < sizeof(_objs) / sizeof(_objs[0]); i++) { \
-                       \
-                       int ret = scf_vector_add(objs, _objs[i]); \
-                       if (ret < 0) \
-                       return ret; \
-               } \
+               int ret = add_sys_files(objs, sysroot, arch, _objs, sizeof(_objs) / sizeof(_objs[0])); \
+               if (ret < 0) \
+                   return ret; \
                \
-               for (i  = 0; i < sizeof(_sofiles) / sizeof(_sofiles[0]); i++) { \
-                       \
-                       int ret = scf_vector_add(sofiles, _sofiles[i]); \
-                       if (ret < 0) \
-                       return ret; \
-               } \
+               ret = add_sys_files(sofiles, sysroot, arch, _sofiles, sizeof(_sofiles) / sizeof(_sofiles[0])); \
+               if (ret < 0) \
+                   return ret; \
        } while (0)
 
 
@@ -210,7 +259,7 @@ int main(int argc, char* argv[])
                return -1;
        }
 
-       if (scf_elf_link(objs, afiles, sofiles, arch, exec) < 0) {
+       if (scf_elf_link(objs, afiles, sofiles, sysroot, arch, exec) < 0) {
                scf_loge("\n");
                return -1;
        }