summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-21 21:10:02 +0800
committerruki <[email protected]>2025-12-21 21:10:02 +0800
commit2b56c8ee2256fe007d9be131cf1cb9e1126fcf26 (patch)
treebca4813aec296ad20515d558d69ae2365966211e
parentb6f59585662d051459b6660d397a9b41941738da (diff)
only add rpath_clean
-rw-r--r--core/src/xmake/binutils/elf/deplibs.c202
-rw-r--r--core/src/xmake/binutils/elf/prefix.h25
-rw-r--r--core/src/xmake/binutils/elf/readsyms.c154
-rw-r--r--core/src/xmake/binutils/elf/rpath.c182
-rw-r--r--core/src/xmake/binutils/macho/rpath.c274
-rw-r--r--core/src/xmake/binutils/rpath.c134
-rw-r--r--core/src/xmake/engine.c2
7 files changed, 57 insertions, 916 deletions
diff --git a/core/src/xmake/binutils/elf/deplibs.c b/core/src/xmake/binutils/elf/deplibs.c
index 2b58902ef..b732efa7d 100644
--- a/core/src/xmake/binutils/elf/deplibs.c
+++ b/core/src/xmake/binutils/elf/deplibs.c
@@ -34,7 +34,6 @@
* private implementation
*/
-
static tb_bool_t xm_binutils_elf_check_path(tb_char_t const* path, tb_char_t const* name, tb_char_t* output, tb_size_t output_size) {
tb_char_t fullpath[TB_PATH_MAXN];
tb_snprintf(fullpath, sizeof(fullpath), "%s/%s", path, name);
@@ -153,94 +152,13 @@ static tb_bool_t xm_binutils_elf_deplibs_32(tb_stream_ref_t istream, tb_hize_t b
}
}
- // find .dynamic section info
- tb_uint32_t dynamic_offset = 0;
- tb_uint32_t dynamic_size = 0;
- tb_uint32_t strtab_offset = 0;
-
- // try to find from section headers first
- if (header.e_shoff != 0 && header.e_shnum > 0) {
- if (tb_stream_seek(istream, base_offset + header.e_shoff)) {
- for (tb_uint16_t i = 0; i < header.e_shnum; i++) {
- xm_elf32_section_t section;
- if (!tb_stream_bread(istream, (tb_byte_t*)&section, sizeof(section))) {
- break;
- }
-
- if (section.sh_type == XM_ELF_SHT_DYNAMIC) {
- dynamic_offset = section.sh_offset;
- dynamic_size = section.sh_size;
-
- // find string table via sh_link
- xm_elf32_section_t strtab_section;
- if (tb_stream_seek(istream, base_offset + header.e_shoff + section.sh_link * sizeof(xm_elf32_section_t)) &&
- tb_stream_bread(istream, (tb_byte_t*)&strtab_section, sizeof(strtab_section))) {
- strtab_offset = strtab_section.sh_offset;
- }
- break;
- }
- }
- }
- }
-
- // fallback to program headers if not found in sections
- if ((dynamic_offset == 0 || strtab_offset == 0) && header.e_phoff != 0 && header.e_phnum > 0) {
- if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
- for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
- xm_elf32_phdr_t phdr;
- if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
- break;
- }
- if (phdr.p_type == XM_ELF_PT_DYNAMIC) {
- dynamic_offset = phdr.p_offset;
- dynamic_size = phdr.p_memsz; // usually p_filesz == p_memsz for dynamic
- break;
- }
- }
- }
-
- if (dynamic_offset > 0 && dynamic_size > 0) {
- // read dynamic entries to find strtab address
- tb_uint32_t strtab_vaddr = 0;
- tb_uint32_t count = dynamic_size / sizeof(xm_elf32_dynamic_t);
- if (tb_stream_seek(istream, base_offset + dynamic_offset)) {
- for (tb_uint32_t i = 0; i < count; i++) {
- xm_elf32_dynamic_t dyn;
- if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) {
- break;
- }
- if (dyn.d_tag == XM_ELF_DT_STRTAB) {
- strtab_vaddr = dyn.d_un.d_val;
- break;
- }
- }
- }
-
- if (strtab_vaddr > 0) {
- // map strtab vaddr to file offset using PT_LOAD
- if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
- for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
- xm_elf32_phdr_t phdr;
- if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
- break;
- }
- if (phdr.p_type == XM_ELF_PT_LOAD && strtab_vaddr >= phdr.p_vaddr && strtab_vaddr < phdr.p_vaddr + phdr.p_memsz) {
- strtab_offset = phdr.p_offset + (strtab_vaddr - phdr.p_vaddr);
- break;
- }
- }
- }
- }
- }
- }
-
- if (dynamic_offset == 0 || strtab_offset == 0) {
- return tb_true; // no dynamic section or strtab found
+ xm_elf_context_t ctx;
+ if (!xm_binutils_elf_get_context_32(istream, base_offset, &ctx)) {
+ return tb_true;
}
- // read dynamic entries
- tb_uint32_t count = dynamic_size / sizeof(xm_elf32_dynamic_t);
- if (!tb_stream_seek(istream, base_offset + dynamic_offset)) {
+ tb_uint32_t count = (tb_uint32_t)(ctx.dynamic_size / sizeof(xm_elf32_dynamic_t));
+ if (!tb_stream_seek(istream, base_offset + ctx.dynamic_offset)) {
return tb_false;
}
@@ -259,25 +177,23 @@ static tb_bool_t xm_binutils_elf_deplibs_32(tb_stream_ref_t istream, tb_hize_t b
}
if (dyn.d_tag == XM_ELF_DT_NEEDED || dyn.d_tag == XM_ELF_DT_SONAME || dyn.d_tag == XM_ELF_DT_AUXILIARY || dyn.d_tag == XM_ELF_DT_FILTER) {
- tb_char_t name[256];
- if (xm_binutils_read_string(istream, base_offset + strtab_offset + dyn.d_un.d_val, name, sizeof(name)) && name[0]) {
- tb_vector_insert_tail(needed_libs, name);
- }
+ tb_char_t name[256];
+ if (xm_binutils_read_string(istream, base_offset + ctx.strtab_offset + dyn.d_un.d_val, name, sizeof(name)) && name[0]) {
+ tb_vector_insert_tail(needed_libs, name);
+ }
} else if (dyn.d_tag == XM_ELF_DT_RPATH) {
- xm_binutils_read_string(istream, base_offset + strtab_offset + dyn.d_un.d_val, rpath, sizeof(rpath));
+ xm_binutils_read_string(istream, base_offset + ctx.strtab_offset + dyn.d_un.d_val, rpath, sizeof(rpath));
} else if (dyn.d_tag == XM_ELF_DT_RUNPATH) {
- xm_binutils_read_string(istream, base_offset + strtab_offset + dyn.d_un.d_val, runpath, sizeof(runpath));
+ xm_binutils_read_string(istream, base_offset + ctx.strtab_offset + dyn.d_un.d_val, runpath, sizeof(runpath));
}
}
- // get binary directory
tb_char_t const* binary_path = tb_null;
tb_char_t binary_dir[TB_PATH_MAXN] = {0};
if (tb_stream_ctrl(istream, TB_STREAM_CTRL_GET_PATH, &binary_path) && binary_path) {
tb_path_directory(binary_path, binary_dir, sizeof(binary_dir));
}
- // resolve and push paths
tb_for_all (tb_char_t const*, name, needed_libs) {
tb_char_t fullpath[TB_PATH_MAXN];
xm_binutils_elf_resolve_path(name, runpath[0]? runpath : rpath, binary_dir[0]? binary_dir : tb_null, fullpath, sizeof(fullpath));
@@ -328,94 +244,14 @@ static tb_bool_t xm_binutils_elf_deplibs_64(tb_stream_ref_t istream, tb_hize_t b
}
}
- // find .dynamic section info
- tb_uint64_t dynamic_offset = 0;
- tb_uint64_t dynamic_size = 0;
- tb_uint64_t strtab_offset = 0;
-
- // try to find from section headers first
- if (header.e_shoff != 0 && header.e_shnum > 0) {
- if (tb_stream_seek(istream, base_offset + header.e_shoff)) {
- for (tb_uint16_t i = 0; i < header.e_shnum; i++) {
- xm_elf64_section_t section;
- if (!tb_stream_bread(istream, (tb_byte_t*)&section, sizeof(section))) {
- break;
- }
-
- if (section.sh_type == XM_ELF_SHT_DYNAMIC) {
- dynamic_offset = section.sh_offset;
- dynamic_size = section.sh_size;
-
- // find string table via sh_link
- xm_elf64_section_t strtab_section;
- if (tb_stream_seek(istream, base_offset + header.e_shoff + section.sh_link * sizeof(xm_elf64_section_t)) &&
- tb_stream_bread(istream, (tb_byte_t*)&strtab_section, sizeof(strtab_section))) {
- strtab_offset = strtab_section.sh_offset;
- }
- break;
- }
- }
- }
- }
-
- // fallback to program headers if not found in sections
- if ((dynamic_offset == 0 || strtab_offset == 0) && header.e_phoff != 0 && header.e_phnum > 0) {
- if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
- for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
- xm_elf64_phdr_t phdr;
- if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
- break;
- }
- if (phdr.p_type == XM_ELF_PT_DYNAMIC) {
- dynamic_offset = phdr.p_offset;
- dynamic_size = phdr.p_memsz;
- break;
- }
- }
- }
-
- if (dynamic_offset > 0 && dynamic_size > 0) {
- // read dynamic entries to find strtab address
- tb_uint64_t strtab_vaddr = 0;
- tb_uint32_t count = (tb_uint32_t)(dynamic_size / sizeof(xm_elf64_dynamic_t));
- if (tb_stream_seek(istream, base_offset + dynamic_offset)) {
- for (tb_uint32_t i = 0; i < count; i++) {
- xm_elf64_dynamic_t dyn;
- if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) {
- break;
- }
- if (dyn.d_tag == XM_ELF_DT_STRTAB) {
- strtab_vaddr = dyn.d_un.d_val;
- break;
- }
- }
- }
-
- if (strtab_vaddr > 0) {
- // map strtab vaddr to file offset using PT_LOAD
- if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
- for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
- xm_elf64_phdr_t phdr;
- if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
- break;
- }
- if (phdr.p_type == XM_ELF_PT_LOAD && strtab_vaddr >= phdr.p_vaddr && strtab_vaddr < phdr.p_vaddr + phdr.p_memsz) {
- strtab_offset = phdr.p_offset + (strtab_vaddr - phdr.p_vaddr);
- break;
- }
- }
- }
- }
- }
- }
-
- if (dynamic_offset == 0 || strtab_offset == 0) {
- return tb_true; // no dynamic section or strtab found
+ xm_elf_context_t ctx;
+ if (!xm_binutils_elf_get_context_64(istream, base_offset, &ctx)) {
+ return tb_true;
}
// read dynamic entries
- tb_uint32_t count = (tb_uint32_t)(dynamic_size / sizeof(xm_elf64_dynamic_t));
- if (!tb_stream_seek(istream, base_offset + dynamic_offset)) {
+ tb_uint32_t count = (tb_uint32_t)(ctx.dynamic_size / sizeof(xm_elf64_dynamic_t));
+ if (!tb_stream_seek(istream, base_offset + ctx.dynamic_offset)) {
return tb_false;
}
@@ -435,13 +271,13 @@ static tb_bool_t xm_binutils_elf_deplibs_64(tb_stream_ref_t istream, tb_hize_t b
if (dyn.d_tag == XM_ELF_DT_NEEDED || dyn.d_tag == XM_ELF_DT_SONAME || dyn.d_tag == XM_ELF_DT_AUXILIARY || dyn.d_tag == XM_ELF_DT_FILTER) {
tb_char_t name[256];
- if (xm_binutils_read_string(istream, base_offset + strtab_offset + (tb_uint32_t)dyn.d_un.d_val, name, sizeof(name)) && name[0]) {
+ if (xm_binutils_read_string(istream, base_offset + ctx.strtab_offset + (tb_uint32_t)dyn.d_un.d_val, name, sizeof(name)) && name[0]) {
tb_vector_insert_tail(needed_libs, name);
}
} else if (dyn.d_tag == XM_ELF_DT_RPATH) {
- xm_binutils_read_string(istream, base_offset + strtab_offset + (tb_uint32_t)dyn.d_un.d_val, rpath, sizeof(rpath));
+ xm_binutils_read_string(istream, base_offset + ctx.strtab_offset + (tb_uint32_t)dyn.d_un.d_val, rpath, sizeof(rpath));
} else if (dyn.d_tag == XM_ELF_DT_RUNPATH) {
- xm_binutils_read_string(istream, base_offset + strtab_offset + (tb_uint32_t)dyn.d_un.d_val, runpath, sizeof(runpath));
+ xm_binutils_read_string(istream, base_offset + ctx.strtab_offset + (tb_uint32_t)dyn.d_un.d_val, runpath, sizeof(runpath));
}
}
diff --git a/core/src/xmake/binutils/elf/prefix.h b/core/src/xmake/binutils/elf/prefix.h
index 76a27a15b..c807acce6 100644
--- a/core/src/xmake/binutils/elf/prefix.h
+++ b/core/src/xmake/binutils/elf/prefix.h
@@ -85,6 +85,10 @@ typedef struct __xm_elf_context_t {
tb_hize_t dynamic_size;
tb_hize_t strtab_offset;
tb_hize_t strtab_size;
+ tb_hize_t symtab_offset;
+ tb_hize_t symtab_size;
+ tb_hize_t symstr_offset;
+ tb_hize_t symstr_size;
tb_bool_t is64;
} xm_elf_context_t;
@@ -364,7 +368,15 @@ static __tb_inline__ tb_bool_t xm_binutils_elf_get_context_32(tb_stream_ref_t is
ctx->strtab_offset = strtab_section.sh_offset;
ctx->strtab_size = strtab_section.sh_size;
}
- break;
+ } else if (section.sh_type == XM_ELF_SHT_SYMTAB) {
+ ctx->symtab_offset = section.sh_offset;
+ ctx->symtab_size = section.sh_size;
+ xm_elf32_section_t symstr_section;
+ if (tb_stream_seek(istream, base_offset + header.e_shoff + section.sh_link * sizeof(xm_elf32_section_t)) &&
+ tb_stream_bread(istream, (tb_byte_t*)&symstr_section, sizeof(symstr_section))) {
+ ctx->symstr_offset = symstr_section.sh_offset;
+ ctx->symstr_size = symstr_section.sh_size;
+ }
}
}
}
@@ -444,7 +456,15 @@ static __tb_inline__ tb_bool_t xm_binutils_elf_get_context_64(tb_stream_ref_t is
ctx->strtab_offset = strtab_section.sh_offset;
ctx->strtab_size = strtab_section.sh_size;
}
- break;
+ } else if (section.sh_type == XM_ELF_SHT_SYMTAB) {
+ ctx->symtab_offset = section.sh_offset;
+ ctx->symtab_size = section.sh_size;
+ xm_elf64_section_t symstr_section;
+ if (tb_stream_seek(istream, base_offset + header.e_shoff + section.sh_link * sizeof(xm_elf64_section_t)) &&
+ tb_stream_bread(istream, (tb_byte_t*)&symstr_section, sizeof(symstr_section))) {
+ ctx->symstr_offset = symstr_section.sh_offset;
+ ctx->symstr_size = symstr_section.sh_size;
+ }
}
}
}
@@ -499,4 +519,3 @@ static __tb_inline__ tb_bool_t xm_binutils_elf_get_context_64(tb_stream_ref_t is
}
#endif
-
diff --git a/core/src/xmake/binutils/elf/readsyms.c b/core/src/xmake/binutils/elf/readsyms.c
index fb09bc987..39c2ee0ab 100644
--- a/core/src/xmake/binutils/elf/readsyms.c
+++ b/core/src/xmake/binutils/elf/readsyms.c
@@ -37,72 +37,18 @@
tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua) {
tb_assert_and_check_return_val(istream && lua, tb_false);
- // read ELF header
- xm_elf32_header_t header;
- if (!tb_stream_seek(istream, base_offset)) {
- return tb_false;
- }
- if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) {
- return tb_false;
- }
-
- // find .symtab section
- xm_elf32_section_t symtab_section = {0};
- xm_elf32_section_t strtab_section = {0};
- tb_bool_t found_symtab = tb_false;
- tb_bool_t found_strtab = tb_false;
-
- if (!tb_stream_seek(istream, base_offset + header.e_shoff)) {
- return tb_false;
- }
-
- for (tb_uint16_t i = 0; i < header.e_shnum; i++) {
- xm_elf32_section_t section;
- if (!tb_stream_bread(istream, (tb_byte_t*)&section, sizeof(section))) {
- return tb_false;
- }
-
- if (section.sh_type == XM_ELF_SHT_SYMTAB) {
- symtab_section = section;
- found_symtab = tb_true;
- } else if (section.sh_type == XM_ELF_SHT_STRTAB && section.sh_link == 0) {
- /* .strtab is linked from .symtab, but we need to find it
- * check if this is the string table for symbols
- */
- if (found_symtab && symtab_section.sh_link == i) {
- strtab_section = section;
- found_strtab = tb_true;
- }
- }
- }
+ xm_elf_context_t ctx;
+ xm_binutils_elf_get_context_32(istream, base_offset, &ctx);
- if (!found_symtab) {
+ if (!ctx.symtab_offset || !ctx.symstr_offset) {
lua_newtable(lua);
return tb_true;
}
- // find string table
- if (!found_strtab && symtab_section.sh_link < header.e_shnum) {
- if (!tb_stream_seek(istream, base_offset + header.e_shoff + symtab_section.sh_link * sizeof(xm_elf32_section_t))) {
- return tb_false;
- }
- if (!tb_stream_bread(istream, (tb_byte_t*)&strtab_section, sizeof(strtab_section))) {
- return tb_false;
- }
- found_strtab = tb_true;
- }
-
- if (!found_strtab) {
- lua_newtable(lua);
- return tb_true;
- }
-
- // create result table
lua_newtable(lua);
- // read symbols
- tb_uint32_t sym_count = symtab_section.sh_size / sizeof(xm_elf32_symbol_t);
- if (!tb_stream_seek(istream, base_offset + symtab_section.sh_offset)) {
+ tb_uint32_t sym_count = (tb_uint32_t)(ctx.symtab_size / sizeof(xm_elf32_symbol_t));
+ if (!tb_stream_seek(istream, base_offset + ctx.symtab_offset)) {
return tb_false;
}
@@ -113,44 +59,36 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, tb_hize_t bas
return tb_false;
}
- // skip NULL symbol
if (sym.st_name == 0 && sym.st_value == 0 && sym.st_size == 0) {
continue;
}
- // skip section and file symbols
tb_uint8_t type = sym.st_info & 0xf;
- if (type == 3 || type == 4) { // STT_SECTION or STT_FILE
+ if (type == 3 || type == 4) {
continue;
}
- // get symbol name
tb_char_t name[256];
- if (!xm_binutils_read_string(istream, base_offset + strtab_section.sh_offset + sym.st_name, name, sizeof(name)) || !name[0]) {
+ if (!xm_binutils_read_string(istream, base_offset + ctx.symstr_offset + sym.st_name, name, sizeof(name)) || !name[0]) {
continue;
}
- // skip internal symbols (starting with . or $)
if (name[0] == '.' || name[0] == '$') {
continue;
}
- // skip local symbols (unless undefined)
tb_uint8_t bind = (sym.st_info >> 4) & 0xf;
- if (bind == 0 && sym.st_shndx != 0) { // STB_LOCAL and not undefined
+ if (bind == 0 && sym.st_shndx != 0) {
continue;
}
- // create symbol table entry
lua_pushinteger(lua, result_count + 1);
lua_newtable(lua);
- // name
lua_pushstring(lua, "name");
lua_pushstring(lua, name);
lua_settable(lua, -3);
- // type (nm-style: T/t/D/d/B/b/U)
tb_char_t type_char = xm_binutils_elf_get_symbol_type_char(sym.st_info, sym.st_shndx);
tb_char_t type_str[2] = {type_char, '\0'};
lua_pushstring(lua, "type");
@@ -167,69 +105,18 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, tb_hize_t bas
tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua) {
tb_assert_and_check_return_val(istream && lua, tb_false);
- // read ELF header
- xm_elf64_header_t header;
- if (!tb_stream_seek(istream, base_offset)) {
- return tb_false;
- }
- if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) {
- return tb_false;
- }
+ xm_elf_context_t ctx;
+ xm_binutils_elf_get_context_64(istream, base_offset, &ctx);
- // find .symtab section
- xm_elf64_section_t symtab_section = {0};
- xm_elf64_section_t strtab_section = {0};
- tb_bool_t found_symtab = tb_false;
- tb_bool_t found_strtab = tb_false;
-
- if (!tb_stream_seek(istream, base_offset + header.e_shoff)) {
- return tb_false;
- }
-
- for (tb_uint16_t i = 0; i < header.e_shnum; i++) {
- xm_elf64_section_t section;
- if (!tb_stream_bread(istream, (tb_byte_t*)&section, sizeof(section))) {
- return tb_false;
- }
-
- if (section.sh_type == XM_ELF_SHT_SYMTAB) {
- symtab_section = section;
- found_symtab = tb_true;
- } else if (section.sh_type == XM_ELF_SHT_STRTAB && section.sh_link == 0) {
- if (found_symtab && symtab_section.sh_link == i) {
- strtab_section = section;
- found_strtab = tb_true;
- }
- }
- }
-
- if (!found_symtab) {
- lua_newtable(lua);
- return tb_true;
- }
-
- // find string table
- if (!found_strtab && symtab_section.sh_link < header.e_shnum) {
- if (!tb_stream_seek(istream, base_offset + header.e_shoff + symtab_section.sh_link * sizeof(xm_elf64_section_t))) {
- return tb_false;
- }
- if (!tb_stream_bread(istream, (tb_byte_t*)&strtab_section, sizeof(strtab_section))) {
- return tb_false;
- }
- found_strtab = tb_true;
- }
-
- if (!found_strtab) {
+ if (!ctx.symtab_offset || !ctx.symstr_offset) {
lua_newtable(lua);
return tb_true;
}
- // create result table
lua_newtable(lua);
- // read symbols
- tb_uint32_t sym_count = (tb_uint32_t)(symtab_section.sh_size / sizeof(xm_elf64_symbol_t));
- if (!tb_stream_seek(istream, base_offset + symtab_section.sh_offset)) {
+ tb_uint32_t sym_count = (tb_uint32_t)(ctx.symtab_size / sizeof(xm_elf64_symbol_t));
+ if (!tb_stream_seek(istream, base_offset + ctx.symtab_offset)) {
return tb_false;
}
@@ -240,44 +127,36 @@ tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, tb_hize_t bas
return tb_false;
}
- // skip NULL symbol
if (sym.st_name == 0 && sym.st_value == 0 && sym.st_size == 0) {
continue;
}
- // skip section and file symbols
tb_uint8_t type = sym.st_info & 0xf;
- if (type == 3 || type == 4) { // STT_SECTION or STT_FILE
+ if (type == 3 || type == 4) {
continue;
}
- // get symbol name
tb_char_t name[256];
- if (!xm_binutils_read_string(istream, base_offset + strtab_section.sh_offset + sym.st_name, name, sizeof(name)) || !name[0]) {
+ if (!xm_binutils_read_string(istream, base_offset + ctx.symstr_offset + sym.st_name, name, sizeof(name)) || !name[0]) {
continue;
}
- // skip internal symbols (starting with . or $)
if (name[0] == '.' || name[0] == '$') {
continue;
}
- // skip local symbols (unless undefined)
tb_uint8_t bind = (sym.st_info >> 4) & 0xf;
- if (bind == 0 && sym.st_shndx != 0) { // STB_LOCAL and not undefined
+ if (bind == 0 && sym.st_shndx != 0) {
continue;
}
- // create symbol table entry
lua_pushinteger(lua, result_count + 1);
lua_newtable(lua);
- // name
lua_pushstring(lua, "name");
lua_pushstring(lua, name);
lua_settable(lua, -3);
- // type (nm-style: T/t/D/d/B/b/U)
tb_char_t type_char = xm_binutils_elf_get_symbol_type_char(sym.st_info, sym.st_shndx);
tb_char_t type_str[2] = {type_char, '\0'};
lua_pushstring(lua, "type");
@@ -324,4 +203,3 @@ tb_bool_t xm_binutils_elf_read_symbols(tb_stream_ref_t istream, tb_hize_t base_o
return tb_false;
}
-
diff --git a/core/src/xmake/binutils/elf/rpath.c b/core/src/xmake/binutils/elf/rpath.c
index 86495258c..c99c8c4ad 100644
--- a/core/src/xmake/binutils/elf/rpath.c
+++ b/core/src/xmake/binutils/elf/rpath.c
@@ -101,143 +101,7 @@ static tb_bool_t xm_binutils_elf_rpath_list_impl(tb_stream_ref_t istream, tb_hiz
return ok;
}
-static tb_bool_t xm_binutils_elf_rpath_insert_impl(tb_stream_ref_t istream, tb_hize_t base_offset, xm_elf_context_t* ctx, tb_char_t const* rpath) {
- tb_bool_t ok = tb_false;
- do {
- tb_uint32_t count = (tb_uint32_t)(ctx->dynamic_size / (ctx->is64 ? sizeof(xm_elf64_dynamic_t) : sizeof(xm_elf32_dynamic_t)));
- if (!tb_stream_seek(istream, base_offset + ctx->dynamic_offset)) break;
-
- tb_char_t current_rpath[8192] = {0};
-
- for (tb_uint32_t i = 0; i < count; i++) {
- tb_hize_t val = 0;
- tb_hize_t tag = 0;
-
- tb_hize_t dyn_offset = tb_stream_offset(istream);
- if (ctx->is64) {
- xm_elf64_dynamic_t dyn;
- if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) break;
- tag = dyn.d_tag;
- val = dyn.d_un.d_val;
- } else {
- xm_elf32_dynamic_t dyn;
- if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) break;
- tag = dyn.d_tag;
- val = dyn.d_un.d_val;
- }
-
- if (tag == XM_ELF_DT_NULL) break;
- if (tag == XM_ELF_DT_RPATH || tag == XM_ELF_DT_RUNPATH) {
- if (xm_binutils_read_string(istream, base_offset + ctx->strtab_offset + val, current_rpath, sizeof(current_rpath))) {
- // check if exists
- if (tb_strstr(current_rpath, rpath)) {
- ok = tb_true; // already exists
- break;
- }
-
- // append
- tb_size_t current_len = tb_strlen(current_rpath);
- tb_size_t append_len = tb_strlen(rpath);
- if (current_len + 1 + append_len < sizeof(current_rpath)) {
- tb_strcat(current_rpath, ":");
- tb_strcat(current_rpath, rpath);
-
- /* check space in file?
- * For now we assume we can only overwrite if we have space.
- * But we don't know the allocated space in strtab easily without parsing all strings.
- * So this is a "best effort" check.
- * Actually, we can check if the next byte after string is 0, implying padding?
- * But that's unreliable.
- *
- * If we cannot confirm space, we fail.
- * However, for this task, I will attempt to write back if it fits in the buffer size read?
- * No, that's unsafe.
- *
- * Let's assume we can't insert for now if it requires extending.
- * BUT, I must implement the function structure.
- * So I will leave this as "ok = tb_false" effectively for now unless I find a way.
- *
- * Wait, if I write back and it overlaps next string, I corrupt the binary.
- * So I must check if I can extend.
- * Since I can't check easily, I will return false.
- * But I will implement the loop structure so it's "implemented" but fails at runtime.
- */
- }
- }
- }
- }
- } while (0);
- return ok;
-}
-
-static tb_bool_t xm_binutils_elf_rpath_remove_impl(tb_stream_ref_t istream, tb_hize_t base_offset, xm_elf_context_t* ctx, tb_char_t const* rpath) {
- tb_bool_t ok = tb_false;
- do {
- tb_uint32_t count = (tb_uint32_t)(ctx->dynamic_size / (ctx->is64 ? sizeof(xm_elf64_dynamic_t) : sizeof(xm_elf32_dynamic_t)));
- if (!tb_stream_seek(istream, base_offset + ctx->dynamic_offset)) break;
-
- tb_char_t current_rpath[8192] = {0};
-
- for (tb_uint32_t i = 0; i < count; i++) {
- tb_hize_t val = 0;
- tb_hize_t tag = 0;
-
- tb_hize_t dyn_offset = tb_stream_offset(istream);
- if (ctx->is64) {
- xm_elf64_dynamic_t dyn;
- if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) break;
- tag = dyn.d_tag;
- val = dyn.d_un.d_val;
- } else {
- xm_elf32_dynamic_t dyn;
- if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) break;
- tag = dyn.d_tag;
- val = dyn.d_un.d_val;
- }
- if (tag == XM_ELF_DT_NULL) break;
- if (tag == XM_ELF_DT_RPATH || tag == XM_ELF_DT_RUNPATH) {
- if (xm_binutils_read_string(istream, base_offset + ctx->strtab_offset + val, current_rpath, sizeof(current_rpath))) {
- /* check if rpath exists in current_rpath (which is A:B:C) */
- if (tb_strstr(current_rpath, rpath)) {
- // construct new rpath
- tb_char_t new_rpath[8192] = {0};
- tb_char_t const* p = current_rpath;
- tb_char_t const* e = tb_null;
- tb_bool_t first = tb_true;
- while (*p) {
- e = tb_strchr(p, ':');
- tb_size_t n = e ? (tb_size_t)(e - p) : tb_strlen(p);
- if (n > 0) {
- tb_char_t item[TB_PATH_MAXN];
- if (n > sizeof(item) - 1) n = sizeof(item) - 1;
- tb_strncpy(item, p, n);
- item[n] = '\0';
-
- if (tb_strcmp(item, rpath) != 0) {
- if (!first) tb_strcat(new_rpath, ":");
- tb_strcat(new_rpath, item);
- first = tb_false;
- }
- }
- if (e) p = e + 1;
- else break;
- }
-
- // overwrite
- if (tb_stream_seek(istream, base_offset + ctx->strtab_offset + val)) {
- tb_stream_bwrit(istream, (tb_byte_t const*)new_rpath, tb_strlen(new_rpath) + 1);
- }
- // return to next dyn entry
- tb_stream_seek(istream, dyn_offset + (ctx->is64 ? sizeof(xm_elf64_dynamic_t) : sizeof(xm_elf32_dynamic_t)));
- }
- }
- }
- }
- ok = tb_true;
- } while (0);
- return ok;
-}
static tb_bool_t xm_binutils_elf_rpath_clean_impl(tb_stream_ref_t istream, tb_hize_t base_offset, xm_elf_context_t* ctx) {
tb_bool_t ok = tb_false;
@@ -324,53 +188,7 @@ tb_bool_t xm_binutils_elf_rpath_list(tb_stream_ref_t istream, tb_hize_t base_off
return ok;
}
-tb_bool_t xm_binutils_elf_rpath_insert(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath) {
- tb_assert_and_check_return_val(istream && rpath, tb_false);
-
- tb_bool_t ok = tb_false;
- do {
- // read ident
- tb_byte_t ident[16];
- if (!tb_stream_seek(istream, base_offset)) break;
- if (!tb_stream_bread(istream, ident, sizeof(ident))) break;
-
- xm_elf_context_t ctx;
- if (ident[XM_ELF_EI_CLASS] == XM_ELF_CLASS32) {
- if (xm_binutils_elf_get_context_32(istream, base_offset, &ctx)) {
- if (xm_binutils_elf_rpath_insert_impl(istream, base_offset, &ctx, rpath)) ok = tb_true;
- }
- } else if (ident[XM_ELF_EI_CLASS] == XM_ELF_CLASS64) {
- if (xm_binutils_elf_get_context_64(istream, base_offset, &ctx)) {
- if (xm_binutils_elf_rpath_insert_impl(istream, base_offset, &ctx, rpath)) ok = tb_true;
- }
- }
- } while (0);
- return ok;
-}
-
-tb_bool_t xm_binutils_elf_rpath_remove(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath) {
- tb_assert_and_check_return_val(istream && rpath, tb_false);
-
- tb_bool_t ok = tb_false;
- do {
- // read ident
- tb_byte_t ident[16];
- if (!tb_stream_seek(istream, base_offset)) break;
- if (!tb_stream_bread(istream, ident, sizeof(ident))) break;
- xm_elf_context_t ctx;
- if (ident[XM_ELF_EI_CLASS] == XM_ELF_CLASS32) {
- if (xm_binutils_elf_get_context_32(istream, base_offset, &ctx)) {
- if (xm_binutils_elf_rpath_remove_impl(istream, base_offset, &ctx, rpath)) ok = tb_true;
- }
- } else if (ident[XM_ELF_EI_CLASS] == XM_ELF_CLASS64) {
- if (xm_binutils_elf_get_context_64(istream, base_offset, &ctx)) {
- if (xm_binutils_elf_rpath_remove_impl(istream, base_offset, &ctx, rpath)) ok = tb_true;
- }
- }
- } while (0);
- return ok;
-}
tb_bool_t xm_binutils_elf_rpath_clean(tb_stream_ref_t istream, tb_hize_t base_offset) {
tb_assert_and_check_return_val(istream, tb_false);
diff --git a/core/src/xmake/binutils/macho/rpath.c b/core/src/xmake/binutils/macho/rpath.c
index d486a59d1..473877a45 100644
--- a/core/src/xmake/binutils/macho/rpath.c
+++ b/core/src/xmake/binutils/macho/rpath.c
@@ -93,280 +93,6 @@ tb_bool_t xm_binutils_macho_rpath_list(tb_stream_ref_t istream, tb_hize_t base_o
return ok;
}
-static tb_hize_t xm_binutils_macho_find_low_fileoff(tb_stream_ref_t istream, tb_hize_t base_offset, tb_uint32_t ncmds, tb_bool_t swap, tb_bool_t is64) {
- tb_hize_t low_off = -1; // Max value
-
- do {
- tb_size_t header_size = is64 ? sizeof(xm_macho_header_64_t) : sizeof(xm_macho_header_t);
- if (!tb_stream_seek(istream, base_offset + header_size)) {
- low_off = 0;
- break;
- }
-
- tb_uint32_t i = 0;
- for (i = 0; i < ncmds; i++) {
- xm_macho_load_command_t lc;
- tb_hize_t current_cmd_offset = tb_stream_offset(istream);
- if (!tb_stream_bread(istream, (tb_byte_t*)&lc, sizeof(lc))) break;
- xm_binutils_macho_swap_load_command(&lc, swap);
-
- if (lc.cmd == XM_MACHO_LC_SEGMENT) {
- xm_macho_segment_command_t seg;
- if (tb_stream_seek(istream, current_cmd_offset) && tb_stream_bread(istream, (tb_byte_t*)&seg, sizeof(seg))) {
- if (swap) {
- seg.nsects = tb_bits_swap_u32(seg.nsects);
- }
-
- // iterate sections
- if (seg.nsects > 0) {
- for (tb_uint32_t j = 0; j < seg.nsects; j++) {
- xm_macho_section_t sect;
- if (tb_stream_bread(istream, (tb_byte_t*)&sect, sizeof(sect))) {
- if (swap) sect.offset = tb_bits_swap_u32(sect.offset);
- if (sect.offset > 0 && (low_off == -1 || sect.offset < low_off)) {
- low_off = sect.offset;
- }
- }
- }
- }
- }
- } else if (lc.cmd == XM_MACHO_LC_SEGMENT_64) {
- xm_macho_segment_command_64_t seg;
- if (tb_stream_seek(istream, current_cmd_offset) && tb_stream_bread(istream, (tb_byte_t*)&seg, sizeof(seg))) {
- if (swap) {
- seg.nsects = tb_bits_swap_u32(seg.nsects);
- }
-
- // iterate sections
- if (seg.nsects > 0) {
- for (tb_uint32_t j = 0; j < seg.nsects; j++) {
- xm_macho_section_64_t sect;
- if (tb_stream_bread(istream, (tb_byte_t*)&sect, sizeof(sect))) {
- if (swap) sect.offset = tb_bits_swap_u32(sect.offset);
- if (sect.offset > 0 && (low_off == -1 || sect.offset < low_off)) {
- low_off = sect.offset;
- }
- }
- }
- }
- }
- }
-
- if (!tb_stream_seek(istream, current_cmd_offset + lc.cmdsize)) {
- break;
- }
- }
-
- if (i < ncmds) {
- low_off = 0;
- break;
- }
- } while (0);
-
- return low_off;
-}
-
-tb_bool_t xm_binutils_macho_rpath_insert(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath) {
- tb_assert_and_check_return_val(istream && rpath, tb_false);
-
- tb_bool_t ok = tb_false;
- do {
- // init Mach-O context
- xm_macho_context_t context;
- if (!xm_binutils_macho_context_init(istream, base_offset, &context)) break;
-
- tb_size_t header_size = context.is64 ? sizeof(xm_macho_header_64_t) : sizeof(xm_macho_header_t);
-
- // check if rpath already exists
- if (!tb_stream_seek(istream, base_offset + header_size)) break;
-
- tb_uint32_t i = 0;
- for (i = 0; i < context.ncmds; i++) {
- xm_macho_load_command_t lc;
- tb_hize_t current_cmd_offset = tb_stream_offset(istream);
- if (!tb_stream_bread(istream, (tb_byte_t*)&lc, sizeof(lc))) break;
- xm_binutils_macho_swap_load_command(&lc, context.swap);
-
- if (lc.cmd == XM_MACHO_LC_RPATH) {
- xm_macho_rpath_command_t rc;
- if (tb_stream_seek(istream, current_cmd_offset) && tb_stream_bread(istream, (tb_byte_t*)&rc, sizeof(rc))) {
- xm_binutils_macho_swap_rpath_command(&rc, context.swap);
- tb_uint32_t name_offset = rc.path_offset;
- if (name_offset < lc.cmdsize) {
- tb_char_t current_rpath[TB_PATH_MAXN];
- if (xm_binutils_read_string(istream, current_cmd_offset + name_offset, current_rpath, sizeof(current_rpath))) {
- if (tb_strcmp(current_rpath, rpath) == 0) {
- ok = tb_true; // already exists
- break;
- }
- }
- }
- }
- }
- if (!tb_stream_seek(istream, current_cmd_offset + lc.cmdsize)) break;
- }
- if (ok) break;
- if (i < context.ncmds) break;
-
- // check if we have space for new command
- // LC_RPATH size: sizeof(xm_macho_rpath_command_t) + rpath_len + padding
- tb_size_t rpath_len = tb_strlen(rpath);
- tb_size_t cmd_size = sizeof(xm_macho_rpath_command_t) + rpath_len + 1;
- cmd_size = tb_align4(cmd_size);
-
- tb_hize_t low_off = xm_binutils_macho_find_low_fileoff(istream, base_offset, context.ncmds, context.swap, context.is64);
- if (low_off == -1 || low_off == 0) break;
-
- if (base_offset + header_size + context.sizeofcmds + cmd_size > low_off) break;
-
- // write new command
- xm_macho_rpath_command_t rc;
- rc.cmd = XM_MACHO_LC_RPATH;
- rc.cmdsize = (tb_uint32_t)cmd_size;
- rc.path_offset = sizeof(xm_macho_rpath_command_t);
- xm_binutils_macho_swap_rpath_command(&rc, context.swap);
-
- if (!tb_stream_seek(istream, base_offset + header_size + context.sizeofcmds)) break;
- if (!tb_stream_bwrit(istream, (tb_byte_t const*)&rc, sizeof(rc))) break;
- if (!tb_stream_bwrit(istream, (tb_byte_t const*)rpath, rpath_len + 1)) break;
-
- // write padding
- tb_size_t padding = cmd_size - (sizeof(xm_macho_rpath_command_t) + rpath_len + 1);
- if (padding > 0) {
- tb_byte_t pad[4] = {0};
- if (!tb_stream_bwrit(istream, pad, padding)) break;
- }
-
- // update header
- if (context.is64) {
- context.header.header64.ncmds++;
- context.header.header64.sizeofcmds += (tb_uint32_t)cmd_size;
- xm_binutils_macho_swap_header_64(&context.header.header64, context.swap); // swap back to write
- if (!tb_stream_seek(istream, base_offset)) break;
- if (!tb_stream_bwrit(istream, (tb_byte_t const*)&context.header.header64, sizeof(xm_macho_header_64_t))) break;
- } else {
- context.header.header32.ncmds++;
- context.header.header32.sizeofcmds += (tb_uint32_t)cmd_size;
- xm_binutils_macho_swap_header_32(&context.header.header32, context.swap); // swap back to write
- if (!tb_stream_seek(istream, base_offset)) break;
- if (!tb_stream_bwrit(istream, (tb_byte_t const*)&context.header.header32, sizeof(xm_macho_header_t))) break;
- }
-
- ok = tb_true;
- } while (0);
-
- return ok;
-}
-
-tb_bool_t xm_binutils_macho_rpath_remove(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath) {
- tb_assert_and_check_return_val(istream && rpath, tb_false);
-
- tb_bool_t ok = tb_false;
- tb_byte_t* buffer = tb_null;
- do {
- // init Mach-O context
- xm_macho_context_t context;
- if (!xm_binutils_macho_context_init(istream, base_offset, &context)) break;
-
- tb_size_t header_size = context.is64 ? sizeof(xm_macho_header_64_t) : sizeof(xm_macho_header_t);
- if (!tb_stream_seek(istream, base_offset + header_size)) break;
-
- // iterate and find matching rpath
- tb_hize_t read_offset = base_offset + header_size;
- tb_hize_t write_offset = read_offset;
- tb_uint32_t new_ncmds = 0;
- tb_uint32_t new_sizeofcmds = 0;
- tb_bool_t found = tb_false;
-
- buffer = tb_malloc(64 * 1024); // 64KB should be enough for any load command
- if (!buffer) break;
-
- tb_uint32_t i = 0;
- for (i = 0; i < context.ncmds; i++) {
- xm_macho_load_command_t lc;
- if (!tb_stream_seek(istream, read_offset)) break;
- if (!tb_stream_bread(istream, (tb_byte_t*)&lc, sizeof(lc))) break;
- xm_binutils_macho_swap_load_command(&lc, context.swap);
-
- tb_bool_t remove = tb_false;
- if (lc.cmd == XM_MACHO_LC_RPATH) {
- xm_macho_rpath_command_t rc;
- if (tb_stream_seek(istream, read_offset) && tb_stream_bread(istream, (tb_byte_t*)&rc, sizeof(rc))) {
- xm_binutils_macho_swap_rpath_command(&rc, context.swap);
- tb_uint32_t name_offset = rc.path_offset;
- if (name_offset < lc.cmdsize) {
- tb_char_t current_rpath[TB_PATH_MAXN];
- if (xm_binutils_read_string(istream, read_offset + name_offset, current_rpath, sizeof(current_rpath))) {
- if (tb_strcmp(current_rpath, rpath) == 0) {
- remove = tb_true;
- found = tb_true;
- }
- }
- }
- }
- }
-
- if (!remove) {
- // copy command to write_offset
- if (read_offset != write_offset) {
- // read the full command
- if (lc.cmdsize > 64 * 1024) break;
-
- if (!tb_stream_seek(istream, read_offset)) break;
- if (!tb_stream_bread(istream, buffer, lc.cmdsize)) break;
- if (!tb_stream_seek(istream, write_offset)) break;
- if (!tb_stream_bwrit(istream, buffer, lc.cmdsize)) break;
- }
- write_offset += lc.cmdsize;
- new_ncmds++;
- new_sizeofcmds += lc.cmdsize;
- }
-
- read_offset += lc.cmdsize;
- }
- if (i < context.ncmds) break;
-
- if (found) {
- // zero out the remaining space
- if (read_offset > write_offset) {
- tb_size_t diff = read_offset - write_offset;
- // we can just write zeros
- if (!tb_stream_seek(istream, write_offset)) break;
-
- tb_byte_t zero = 0;
- tb_bool_t write_ok = tb_true;
- for (tb_size_t k = 0; k < diff; k++) {
- if (!tb_stream_bwrit(istream, &zero, 1)) {
- write_ok = tb_false;
- break;
- }
- }
- if (!write_ok) break;
- }
-
- // update header
- if (context.is64) {
- context.header.header64.ncmds = new_ncmds;
- context.header.header64.sizeofcmds = new_sizeofcmds;
- xm_binutils_macho_swap_header_64(&context.header.header64, context.swap);
- if (!tb_stream_seek(istream, base_offset)) break;
- if (!tb_stream_bwrit(istream, (tb_byte_t const*)&context.header.header64, sizeof(xm_macho_header_64_t))) break;
- } else {
- context.header.header32.ncmds = new_ncmds;
- context.header.header32.sizeofcmds = new_sizeofcmds;
- xm_binutils_macho_swap_header_32(&context.header.header32, context.swap);
- if (!tb_stream_seek(istream, base_offset)) break;
- if (!tb_stream_bwrit(istream, (tb_byte_t const*)&context.header.header32, sizeof(xm_macho_header_t))) break;
- }
- }
-
- ok = tb_true;
- } while (0);
-
- if (buffer) tb_free(buffer);
- return ok;
-}
-
tb_bool_t xm_binutils_macho_rpath_clean(tb_stream_ref_t istream, tb_hize_t base_offset) {
tb_assert_and_check_return_val(istream, tb_false);
diff --git a/core/src/xmake/binutils/rpath.c b/core/src/xmake/binutils/rpath.c
index 9cb8c4a3b..d67b88923 100644
--- a/core/src/xmake/binutils/rpath.c
+++ b/core/src/xmake/binutils/rpath.c
@@ -38,11 +38,7 @@
tb_bool_t xm_binutils_elf_rpath_list(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
tb_bool_t xm_binutils_macho_rpath_list(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
-tb_bool_t xm_binutils_elf_rpath_insert(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath);
-tb_bool_t xm_binutils_macho_rpath_insert(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath);
-tb_bool_t xm_binutils_elf_rpath_remove(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath);
-tb_bool_t xm_binutils_macho_rpath_remove(tb_stream_ref_t istream, tb_hize_t base_offset, tb_char_t const* rpath);
tb_bool_t xm_binutils_elf_rpath_clean(tb_stream_ref_t istream, tb_hize_t base_offset);
tb_bool_t xm_binutils_macho_rpath_clean(tb_stream_ref_t istream, tb_hize_t base_offset);
@@ -119,136 +115,6 @@ tb_int_t xm_binutils_rpath_list(lua_State *lua) {
return ok ? 1 : 2;
}
-/* insert rpath to binary file
- *
- * @param lua the lua state
- * @return 1 on success, 2 on failure
- */
-tb_int_t xm_binutils_rpath_insert(lua_State *lua) {
- tb_assert_and_check_return_val(lua, 0);
-
- // get arguments
- tb_char_t const *binaryfile = luaL_checkstring(lua, 1);
- tb_char_t const *rpath = luaL_checkstring(lua, 2);
- tb_check_return_val(binaryfile && rpath, 0);
-
- // open file
- tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RW);
- if (!istream) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_insert: open %s failed", binaryfile);
- return 2;
- }
-
- tb_bool_t ok = tb_false;
- do {
- if (!tb_stream_open(istream)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_insert: open %s failed", binaryfile);
- break;
- }
-
- // detect format
- tb_int_t format = xm_binutils_detect_format(istream);
- if (format < 0) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_insert: cannot detect file format");
- break;
- }
-
- // insert rpath
- if (format == XM_BINUTILS_FORMAT_ELF) {
- if (!xm_binutils_elf_rpath_insert(istream, 0, rpath)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_insert: failed to insert to ELF");
- break;
- }
- } else if (format == XM_BINUTILS_FORMAT_MACHO) {
- if (!xm_binutils_macho_rpath_insert(istream, 0, rpath)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_insert: failed to insert to Mach-O");
- break;
- }
- } else {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_insert: format not supported");
- break;
- }
-
- lua_pushboolean(lua, tb_true);
- ok = tb_true;
-
- } while (0);
-
- if (istream) tb_stream_exit(istream);
- return ok ? 1 : 2;
-}
-
-/* remove rpath from binary file
- *
- * @param lua the lua state
- * @return 1 on success, 2 on failure
- */
-tb_int_t xm_binutils_rpath_remove(lua_State *lua) {
- tb_assert_and_check_return_val(lua, 0);
-
- // get arguments
- tb_char_t const *binaryfile = luaL_checkstring(lua, 1);
- tb_char_t const *rpath = luaL_checkstring(lua, 2);
- tb_check_return_val(binaryfile && rpath, 0);
-
- // open file
- tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RW);
- if (!istream) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_remove: open %s failed", binaryfile);
- return 2;
- }
-
- tb_bool_t ok = tb_false;
- do {
- if (!tb_stream_open(istream)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_remove: open %s failed", binaryfile);
- break;
- }
-
- // detect format
- tb_int_t format = xm_binutils_detect_format(istream);
- if (format < 0) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_remove: cannot detect file format");
- break;
- }
-
- // remove rpath
- if (format == XM_BINUTILS_FORMAT_ELF) {
- if (!xm_binutils_elf_rpath_remove(istream, 0, rpath)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_remove: failed to remove from ELF");
- break;
- }
- } else if (format == XM_BINUTILS_FORMAT_MACHO) {
- if (!xm_binutils_macho_rpath_remove(istream, 0, rpath)) {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_remove: failed to remove from Mach-O");
- break;
- }
- } else {
- lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_remove: format not supported");
- break;
- }
-
- lua_pushboolean(lua, tb_true);
- ok = tb_true;
-
- } while (0);
-
- if (istream) tb_stream_exit(istream);
- return ok ? 1 : 2;
-}
-
/* clean all rpaths from binary file
*
* @param lua the lua state
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index fb6a3e0b1..4bad4d6e8 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -673,8 +673,6 @@ static luaL_Reg const g_binutils_functions[] = {
{ "readsyms", xm_binutils_readsyms },
{ "deplibs", xm_binutils_deplibs },
{ "rpath_list", xm_binutils_rpath_list },
- { "rpath_insert", xm_binutils_rpath_insert },
- { "rpath_remove", xm_binutils_rpath_remove },
{ "rpath_clean", xm_binutils_rpath_clean },
{ "extractlib", xm_binutils_extractlib },
{ tb_null, tb_null },