summaryrefslogtreecommitdiff
path: root/core/src/xmake/binutils/macho
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-09 00:36:15 +0800
committerruki <[email protected]>2025-12-09 00:36:15 +0800
commit2ce4dfa1c8029498cfa6b99ed28daca6c2e350d4 (patch)
treef9039bb89eb1fcc98924d495e2dc1e6e305ef0ee /core/src/xmake/binutils/macho
parent333af0b66dfe73fabb2c900bce00ba2d50bfe622 (diff)
improve symbol type
Diffstat (limited to 'core/src/xmake/binutils/macho')
-rw-r--r--core/src/xmake/binutils/macho/prefix.h38
-rw-r--r--core/src/xmake/binutils/macho/readsyms.c22
2 files changed, 49 insertions, 11 deletions
diff --git a/core/src/xmake/binutils/macho/prefix.h b/core/src/xmake/binutils/macho/prefix.h
index 39ac857f1..7664940b1 100644
--- a/core/src/xmake/binutils/macho/prefix.h
+++ b/core/src/xmake/binutils/macho/prefix.h
@@ -358,17 +358,41 @@ static __tb_inline__ tb_bool_t xm_binutils_macho_read_string(tb_stream_ref_t ist
return tb_true;
}
-/* get symbol type string from Mach-O symbol type
+/* get symbol type character (nm-style) from Mach-O symbol
*
- * @param type the symbol type byte
- * @return the type string
+ * @param type the symbol type byte
+ * @param sect the section number (0 = undefined)
+ * @return the type character (T/t/D/d/B/b/U)
*/
-static __tb_inline__ tb_char_t const *xm_binutils_macho_get_symbol_type(tb_uint8_t type) {
+static __tb_inline__ tb_char_t xm_binutils_macho_get_symbol_type_char(tb_uint8_t type, tb_uint8_t sect) {
+ // undefined symbol
+ if (sect == 0) {
+ return 'U';
+ }
+
+ // check if external
+ tb_bool_t is_external = (type & XM_MACHO_N_EXT) != 0;
+
+ // check if in section
tb_uint8_t n_type = type & XM_MACHO_N_TYPE_MASK;
- switch (n_type) {
- case XM_MACHO_N_TYPE_SECT: return "section";
- default: return "unknown";
+ if (n_type == XM_MACHO_N_TYPE_SECT) {
+ // section 1 is usually __TEXT,__text (text)
+ // section 2 is usually __DATA,__data (data)
+ // section 3 is usually __DATA,__bss (bss)
+ // For simplicity, we'll use section number to determine type
+ // This is a heuristic and may not be 100% accurate
+ if (sect == 1) {
+ return is_external ? 'T' : 't'; // text section
+ } else if (sect == 2) {
+ return is_external ? 'D' : 'd'; // data section
+ } else if (sect == 3) {
+ return is_external ? 'B' : 'b'; // bss section
+ } else {
+ return is_external ? 'S' : 's'; // other section
+ }
}
+
+ return '?'; // unknown
}
/* get symbol bind string from Mach-O symbol type
diff --git a/core/src/xmake/binutils/macho/readsyms.c b/core/src/xmake/binutils/macho/readsyms.c
index a78daaa5a..62596c45e 100644
--- a/core/src/xmake/binutils/macho/readsyms.c
+++ b/core/src/xmake/binutils/macho/readsyms.c
@@ -110,6 +110,11 @@ tb_bool_t xm_binutils_macho_read_symbols_32(tb_stream_ref_t istream, lua_State *
continue;
}
+ // skip internal symbols (starting with .)
+ if (name[0] == '.') {
+ continue;
+ }
+
// create symbol table entry
lua_pushinteger(lua, result_count + 1);
lua_newtable(lua);
@@ -129,9 +134,11 @@ tb_bool_t xm_binutils_macho_read_symbols_32(tb_stream_ref_t istream, lua_State *
lua_pushinteger(lua, nlist.sect);
lua_settable(lua, -3);
- // type
+ // type (nm-style: T/t/D/d/B/b/U)
+ tb_char_t type_char = xm_binutils_macho_get_symbol_type_char(nlist.type, nlist.sect);
+ tb_char_t type_str[2] = {type_char, '\0'};
lua_pushstring(lua, "type");
- lua_pushstring(lua, xm_binutils_macho_get_symbol_type(nlist.type));
+ lua_pushstring(lua, type_str);
lua_settable(lua, -3);
// bind
@@ -222,6 +229,11 @@ tb_bool_t xm_binutils_macho_read_symbols_64(tb_stream_ref_t istream, lua_State *
continue;
}
+ // skip internal symbols (starting with .)
+ if (name[0] == '.') {
+ continue;
+ }
+
// create symbol table entry
lua_pushinteger(lua, result_count + 1);
lua_newtable(lua);
@@ -241,9 +253,11 @@ tb_bool_t xm_binutils_macho_read_symbols_64(tb_stream_ref_t istream, lua_State *
lua_pushinteger(lua, nlist.sect);
lua_settable(lua, -3);
- // type
+ // type (nm-style: T/t/D/d/B/b/U)
+ tb_char_t type_char = xm_binutils_macho_get_symbol_type_char(nlist.type, nlist.sect);
+ tb_char_t type_str[2] = {type_char, '\0'};
lua_pushstring(lua, "type");
- lua_pushstring(lua, xm_binutils_macho_get_symbol_type(nlist.type));
+ lua_pushstring(lua, type_str);
lua_settable(lua, -3);
// bind