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;
}
-
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 {
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
-
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;
}
.write_rel = _arm32_elf_write_rel,
.write_exec = _arm32_elf_write_exec,
};
-
#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);
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;
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];
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;
}
.write_rel = _arm64_elf_write_rel,
.write_exec = _arm64_elf_write_exec,
};
-
#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);
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--) {
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];
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;
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;
return 0;
}
-
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
-
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;
}
.write_rel = _naja_elf_write_rel,
.write_exec = _naja_elf_write_exec,
};
-
#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);
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--) {
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];
}
}
-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;
}
#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);
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;
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];
+++ /dev/null
-.text
-.global scf__abort
-
-scf__abort:
- xorq %rax, %rax
- mov %rax, (%rax)
- ret
-.fill 1, 1, 0
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[])
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;
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) {
#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)
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;
}