summaryrefslogtreecommitdiff
path: root/core/src/xmake/binutils/elf
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 /core/src/xmake/binutils/elf
parentb6f59585662d051459b6660d397a9b41941738da (diff)
only add rpath_clean
Diffstat (limited to 'core/src/xmake/binutils/elf')
-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
4 files changed, 57 insertions, 506 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);