From: yu.dongliang <18588496441@163.com> Date: Mon, 2 Sep 2024 08:52:12 +0000 (+0800) Subject: support [-s sysroot] of main() argv to select the sysroot dir X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=a0bf4fbc4dc3fc880abb41bf156796eeb86dcecd;p=scf.git support [-s sysroot] of main() argv to select the sysroot dir --- diff --git a/elf/scf_elf.c b/elf/scf_elf.c index c1ba4c7..238d2a2 100644 --- a/elf/scf_elf.c +++ b/elf/scf_elf.c @@ -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; } - diff --git a/elf/scf_elf.h b/elf/scf_elf.h index cc9717f..4face48 100644 --- a/elf/scf_elf.h +++ b/elf/scf_elf.h @@ -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 - diff --git a/elf/scf_elf_arm32.c b/elf/scf_elf_arm32.c index cdedc55..dcbae44 100644 --- a/elf/scf_elf_arm32.c +++ b/elf/scf_elf_arm32.c @@ -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, }; - diff --git a/elf/scf_elf_arm32.h b/elf/scf_elf_arm32.h index bc606f3..b6901fd 100644 --- a/elf/scf_elf_arm32.h +++ b/elf/scf_elf_arm32.h @@ -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); diff --git a/elf/scf_elf_arm32_so.c b/elf/scf_elf_arm32_so.c index 60f078b..3a3dbd1 100644 --- a/elf/scf_elf_arm32_so.c +++ b/elf/scf_elf_arm32_so.c @@ -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]; diff --git a/elf/scf_elf_arm64.c b/elf/scf_elf_arm64.c index f8e14ef..07a5901 100644 --- a/elf/scf_elf_arm64.c +++ b/elf/scf_elf_arm64.c @@ -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, }; - diff --git a/elf/scf_elf_arm64.h b/elf/scf_elf_arm64.h index 7eae07f..df68ea0 100644 --- a/elf/scf_elf_arm64.h +++ b/elf/scf_elf_arm64.h @@ -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); diff --git a/elf/scf_elf_arm64_so.c b/elf/scf_elf_arm64_so.c index 59f687b..0f4a0f2 100644 --- a/elf/scf_elf_arm64_so.c +++ b/elf/scf_elf_arm64_so.c @@ -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]; diff --git a/elf/scf_elf_link.c b/elf/scf_elf_link.c index 15a999a..311982f 100644 --- a/elf/scf_elf_link.c +++ b/elf/scf_elf_link.c @@ -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; } - diff --git a/elf/scf_elf_link.h b/elf/scf_elf_link.h index e750703..d976dcc 100644 --- a/elf/scf_elf_link.h +++ b/elf/scf_elf_link.h @@ -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 - diff --git a/elf/scf_elf_naja.c b/elf/scf_elf_naja.c index e62bbe6..dd20336 100644 --- a/elf/scf_elf_naja.c +++ b/elf/scf_elf_naja.c @@ -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, }; - diff --git a/elf/scf_elf_naja.h b/elf/scf_elf_naja.h index ab586b7..52eb915 100644 --- a/elf/scf_elf_naja.h +++ b/elf/scf_elf_naja.h @@ -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); diff --git a/elf/scf_elf_naja_so.c b/elf/scf_elf_naja_so.c index 46471be..31690c5 100644 --- a/elf/scf_elf_naja_so.c +++ b/elf/scf_elf_naja_so.c @@ -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]; diff --git a/elf/scf_elf_x64.c b/elf/scf_elf_x64.c index 41e6945..752645f 100644 --- a/elf/scf_elf_x64.c +++ b/elf/scf_elf_x64.c @@ -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; } diff --git a/elf/scf_elf_x64.h b/elf/scf_elf_x64.h index ddc828d..ee1aa19 100644 --- a/elf/scf_elf_x64.h +++ b/elf/scf_elf_x64.h @@ -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); diff --git a/elf/scf_elf_x64_so.c b/elf/scf_elf_x64_so.c index b468560..a764444 100644 --- a/elf/scf_elf_x64_so.c +++ b/elf/scf_elf_x64_so.c @@ -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 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 index 0b4da7b..0000000 --- a/lib/scf_abort.s +++ /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 index 0f6c879..0000000 Binary files a/lib/scf_syscall.o and /dev/null differ diff --git a/lib/_start.o b/lib/x64/_start.o similarity index 100% rename from lib/_start.o rename to lib/x64/_start.o diff --git a/lib/_start.s b/lib/x64/_start.s similarity index 100% rename from lib/_start.s rename to lib/x64/_start.s diff --git a/lib/scf_atomic.o b/lib/x64/scf_atomic.o similarity index 100% rename from lib/scf_atomic.o rename to lib/x64/scf_atomic.o diff --git a/lib/scf_atomic.s b/lib/x64/scf_atomic.s similarity index 100% rename from lib/scf_atomic.s rename to lib/x64/scf_atomic.s diff --git a/lib/scf_object.o b/lib/x64/scf_object.o similarity index 100% rename from lib/scf_object.o rename to lib/x64/scf_object.o diff --git a/lib/scf_syscall.s b/lib/x64/scf_syscall.s similarity index 100% rename from lib/scf_syscall.s rename to lib/x64/scf_syscall.s diff --git a/parse/main.c b/parse/main.c index 8f7bb54..cd3a7d1 100644 --- a/parse/main.c +++ b/parse/main.c @@ -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; }