diff options
| author | ruki <[email protected]> | 2025-12-09 00:36:15 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-09 00:36:15 +0800 |
| commit | 2ce4dfa1c8029498cfa6b99ed28daca6c2e350d4 (patch) | |
| tree | f9039bb89eb1fcc98924d495e2dc1e6e305ef0ee /core/src/xmake/binutils/elf | |
| parent | 333af0b66dfe73fabb2c900bce00ba2d50bfe622 (diff) | |
improve symbol type
Diffstat (limited to 'core/src/xmake/binutils/elf')
| -rw-r--r-- | core/src/xmake/binutils/elf/prefix.h | 36 | ||||
| -rw-r--r-- | core/src/xmake/binutils/elf/readsyms.c | 34 |
2 files changed, 55 insertions, 15 deletions
diff --git a/core/src/xmake/binutils/elf/prefix.h b/core/src/xmake/binutils/elf/prefix.h index 97d05a661..7ddc0ef85 100644 --- a/core/src/xmake/binutils/elf/prefix.h +++ b/core/src/xmake/binutils/elf/prefix.h @@ -303,21 +303,35 @@ static __tb_inline__ tb_bool_t xm_binutils_elf_read_string(tb_stream_ref_t istre return tb_true; } -/* get symbol type string from ELF symbol info +/* get symbol type character (nm-style) from ELF symbol * - * @param st_info the symbol info byte - * @return the type string + * @param st_info the symbol info byte + * @param st_shndx the section index (0 = undefined) + * @return the type character (T/t/D/d/B/b/U) */ -static __tb_inline__ tb_char_t const *xm_binutils_elf_get_symbol_type(tb_uint8_t st_info) { +static __tb_inline__ tb_char_t xm_binutils_elf_get_symbol_type_char(tb_uint8_t st_info, tb_uint16_t st_shndx) { + // undefined symbol + if (st_shndx == 0) { + return 'U'; + } + + // check bind (global = uppercase, local = lowercase) + tb_uint8_t bind = (st_info >> 4) & 0xf; + tb_bool_t is_global = (bind == 1); // STB_GLOBAL + + // check type tb_uint8_t type = st_info & 0xf; - switch (type) { - case 0: return "notype"; - case 1: return "object"; - case 2: return "func"; - case 3: return "section"; - case 4: return "file"; - default: return "unknown"; + if (type == 2) { // STT_FUNC + return is_global ? 'T' : 't'; // text (function) + } else if (type == 1) { // STT_OBJECT + // For object symbols, we need section info to determine data/bss + // For simplicity, we'll use 'D' for data, 'B' for bss + // This is a heuristic - in practice, we'd need to check section flags + return is_global ? 'D' : 'd'; // data (assume data section) } + + // other types + return is_global ? 'S' : 's'; // other section } /* get symbol bind string from ELF symbol info diff --git a/core/src/xmake/binutils/elf/readsyms.c b/core/src/xmake/binutils/elf/readsyms.c index 06c17a697..1afc80fc6 100644 --- a/core/src/xmake/binutils/elf/readsyms.c +++ b/core/src/xmake/binutils/elf/readsyms.c @@ -117,11 +117,22 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, lua_State *lu continue; } + // skip section and file symbols + tb_uint8_t type = sym.st_info & 0xf; + if (type == 3 || type == 4) { // STT_SECTION or STT_FILE + continue; + } + // get symbol name tb_char_t name[256]; if (!xm_binutils_elf_read_string(istream, strtab_section.sh_offset, sym.st_name, name, sizeof(name)) || !name[0]) { continue; } + + // skip internal symbols (starting with .) + if (name[0] == '.') { + continue; + } // create symbol table entry lua_pushinteger(lua, result_count + 1); @@ -147,9 +158,11 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, lua_State *lu lua_pushinteger(lua, sym.st_shndx); lua_settable(lua, -3); - // type + // 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"); - lua_pushstring(lua, xm_binutils_elf_get_symbol_type(sym.st_info)); + lua_pushstring(lua, type_str); lua_settable(lua, -3); // bind @@ -245,11 +258,22 @@ tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, lua_State *lu continue; } + // skip section and file symbols + tb_uint8_t type = sym.st_info & 0xf; + if (type == 3 || type == 4) { // STT_SECTION or STT_FILE + continue; + } + // get symbol name tb_char_t name[256]; if (!xm_binutils_elf_read_string(istream, strtab_section.sh_offset, sym.st_name, name, sizeof(name)) || !name[0]) { continue; } + + // skip internal symbols (starting with .) + if (name[0] == '.') { + continue; + } // create symbol table entry lua_pushinteger(lua, result_count + 1); @@ -275,9 +299,11 @@ tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, lua_State *lu lua_pushinteger(lua, sym.st_shndx); lua_settable(lua, -3); - // type + // 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"); - lua_pushstring(lua, xm_binutils_elf_get_symbol_type(sym.st_info)); + lua_pushstring(lua, type_str); lua_settable(lua, -3); // bind |
