summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-11 23:51:42 +0800
committerruki <[email protected]>2025-12-12 09:00:45 +0800
commitf6a9dadaf99d0370a966957a83d312954bc51c16 (patch)
tree5afa3ef8e9e8f18ffa11f8b0688e3ec3a6e1d83a
parent37c29cafa18e56d8b74b012665983c6ef132e28f (diff)
improve readsyms mslib/lto
-rw-r--r--core/src/xmake/binutils/mslib/readsyms.c190
-rw-r--r--temp_lua/lapi.c.objbin0 -> 98713 bytes
-rw-r--r--temp_lua/lauxlib.c.objbin0 -> 143996 bytes
-rw-r--r--temp_lua/lbaselib.c.objbin0 -> 120594 bytes
-rw-r--r--temp_lua/lcode.c.objbin0 -> 109013 bytes
-rw-r--r--temp_lua/lcorolib.c.objbin0 -> 92489 bytes
-rw-r--r--temp_lua/lctype.c.objbin0 -> 4121 bytes
-rw-r--r--temp_lua/ldblib.c.objbin0 -> 116461 bytes
-rw-r--r--temp_lua/ldebug.c.objbin0 -> 68613 bytes
-rw-r--r--temp_lua/ldo.c.objbin0 -> 68903 bytes
-rw-r--r--temp_lua/ldump.c.objbin0 -> 25539 bytes
-rw-r--r--temp_lua/lfunc.c.objbin0 -> 29146 bytes
-rw-r--r--temp_lua/lgc.c.objbin0 -> 163349 bytes
-rw-r--r--temp_lua/linit.c.objbin0 -> 82371 bytes
-rw-r--r--temp_lua/liolib.c.objbin0 -> 130873 bytes
-rw-r--r--temp_lua/llex.c.objbin0 -> 60112 bytes
-rw-r--r--temp_lua/lmathlib.c.objbin0 -> 119266 bytes
-rw-r--r--temp_lua/lmem.c.objbin0 -> 17664 bytes
-rw-r--r--temp_lua/loadlib.c.objbin0 -> 234645 bytes
-rw-r--r--temp_lua/lobject.c.objbin0 -> 132931 bytes
-rw-r--r--temp_lua/lopcodes.c.objbin0 -> 5290 bytes
-rw-r--r--temp_lua/loslib.c.objbin0 -> 110941 bytes
-rw-r--r--temp_lua/lparser.c.objbin0 -> 121736 bytes
-rw-r--r--temp_lua/lstate.c.objbin0 -> 44978 bytes
-rw-r--r--temp_lua/lstring.c.objbin0 -> 31889 bytes
-rw-r--r--temp_lua/lstrlib.c.objbin0 -> 188730 bytes
-rw-r--r--temp_lua/ltable.c.objbin0 -> 59139 bytes
-rw-r--r--temp_lua/ltablib.c.objbin0 -> 109876 bytes
-rw-r--r--temp_lua/ltests.c.objbin0 -> 93851 bytes
-rw-r--r--temp_lua/ltm.c.objbin0 -> 38474 bytes
-rw-r--r--temp_lua/lundump.c.objbin0 -> 39023 bytes
-rw-r--r--temp_lua/lutf8lib.c.objbin0 -> 99241 bytes
-rw-r--r--temp_lua/lvm.c.objbin0 -> 217873 bytes
-rw-r--r--temp_lua/lzio.c.objbin0 -> 18003 bytes
34 files changed, 187 insertions, 3 deletions
diff --git a/core/src/xmake/binutils/mslib/readsyms.c b/core/src/xmake/binutils/mslib/readsyms.c
index 74139ae5c..a50121acc 100644
--- a/core/src/xmake/binutils/mslib/readsyms.c
+++ b/core/src/xmake/binutils/mslib/readsyms.c
@@ -40,6 +40,123 @@ extern tb_bool_t xm_binutils_macho_read_symbols(tb_stream_ref_t istream, tb_hize
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
+static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream, tb_hize_t member_size, lua_State* lua, int map_idx) {
+ // try to parse as Second Linker Member (LE)
+ tb_hize_t start_pos = tb_stream_offset(istream);
+
+ // read number of members
+ tb_uint32_t num_members = 0;
+ if (!tb_stream_bread_u32_le(istream, &num_members)) return tb_false;
+
+ // sanity check
+ if (num_members == 0 || num_members > 65536 || num_members * 4 >= member_size) {
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+
+ // read offsets
+ tb_uint32_t* offsets = tb_nalloc_type(num_members, tb_uint32_t);
+ if (!offsets) {
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+
+ tb_size_t i;
+ for (i = 0; i < num_members; i++) {
+ if (!tb_stream_bread_u32_le(istream, &offsets[i])) {
+ tb_free(offsets);
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+ }
+
+ // read number of symbols
+ tb_uint32_t num_symbols = 0;
+ if (!tb_stream_bread_u32_le(istream, &num_symbols)) {
+ tb_free(offsets);
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+
+ if (num_symbols == 0 || num_symbols > 1000000) {
+ tb_free(offsets);
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+
+ // read indices
+ tb_uint16_t* indices = tb_nalloc_type(num_symbols, tb_uint16_t);
+ if (!indices) {
+ tb_free(offsets);
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+
+ for (i = 0; i < num_symbols; i++) {
+ if (!tb_stream_bread_u16_le(istream, &indices[i])) {
+ tb_free(indices);
+ tb_free(offsets);
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+ }
+
+ // read string table
+ tb_hize_t current = tb_stream_offset(istream);
+ tb_hize_t string_table_size = member_size - (current - start_pos);
+
+ tb_char_t* string_table = (tb_char_t*)tb_malloc_bytes((tb_size_t)string_table_size);
+ if (!string_table) {
+ tb_free(indices);
+ tb_free(offsets);
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+
+ if (!tb_stream_bread(istream, (tb_byte_t*)string_table, (tb_size_t)string_table_size)) {
+ tb_free(string_table);
+ tb_free(indices);
+ tb_free(offsets);
+ tb_stream_seek(istream, start_pos);
+ return tb_false;
+ }
+
+ // populate map
+ tb_char_t* p = string_table;
+ tb_char_t* end = string_table + string_table_size;
+
+ for (i = 0; i < num_symbols; i++) {
+ if (p >= end) break;
+
+ tb_char_t* sym_name = p;
+ tb_size_t sym_len = tb_strlen(sym_name);
+ p += sym_len + 1;
+
+ tb_uint16_t idx = indices[i];
+ if (idx > 0 && idx <= num_members) {
+ tb_uint32_t offset = offsets[idx - 1];
+
+ lua_pushinteger(lua, offset);
+ lua_rawget(lua, map_idx);
+ if (lua_isnil(lua, -1)) {
+ lua_pop(lua, 1);
+ lua_newtable(lua);
+ lua_pushinteger(lua, offset);
+ lua_pushvalue(lua, -2);
+ lua_rawset(lua, map_idx);
+ }
+ int count = (int)lua_objlen(lua, -1);
+ lua_pushstring(lua, sym_name);
+ lua_rawseti(lua, -2, count + 1);
+ lua_pop(lua, 1); // pop list
+ }
+ }
+
+ tb_free(string_table);
+ tb_free(indices);
+ tb_free(offsets);
+ return tb_true;
+}
/* read symbols from MSVC lib archive
*
@@ -56,6 +173,10 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base
return tb_false;
}
+ // create map table (offset -> symbols)
+ lua_newtable(lua);
+ int map_idx = lua_gettop(lua);
+
tb_bool_t ok = tb_true;
tb_size_t object_count = 0;
tb_char_t* longnames = tb_null;
@@ -128,9 +249,9 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base
}
// check if we should process
- // skip empty names, symbol tables (/), long name table (//) - handled above,
+ // skip empty names, long name table (//) - handled above,
// and __.SYMDEF (SysV/BSD style symbol table, just in case)
- if (member_name[0] == '\0' || tb_strcmp(member_name, "/") == 0 || tb_strcmp(member_name, "//") == 0 ||
+ if (member_name[0] == '\0' || tb_strcmp(member_name, "//") == 0 ||
tb_strncmp(member_name, "__.SYMDEF", 9) == 0) {
// skip member data
@@ -148,8 +269,28 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base
continue;
}
+ if (tb_strcmp(member_name, "/") == 0) {
+ // try to parse archive symbols
+ if (!xm_binutils_mslib_parse_archive_symbols(istream, (tb_hize_t)member_size, lua, map_idx)) {
+ // if failed, skip member data
+ if (!tb_stream_skip(istream, member_size)) {
+ ok = tb_false;
+ break;
+ }
+ }
+ // align
+ if (member_size % 2) {
+ if (!tb_stream_skip(istream, 1)) {
+ ok = tb_false;
+ break;
+ }
+ }
+ continue;
+ }
+
// save current position
tb_hize_t current_pos = tb_stream_offset(istream);
+ tb_hize_t header_offset = current_pos - sizeof(xm_mslib_header_t);
// detect format
tb_int_t format = xm_binutils_detect_format(istream);
@@ -173,9 +314,51 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base
read_ok = xm_binutils_macho_read_symbols(istream, current_pos, lua);
}
+ // if read failed or empty, try map
+ tb_bool_t has_symbols = tb_false;
if (read_ok) {
+ if (lua_objlen(lua, -1) > 0) {
+ has_symbols = tb_true;
+ } else {
+ lua_pop(lua, 1); // pop empty table
+ }
+ }
+
+ if (!has_symbols) {
+ // check map
+ lua_pushinteger(lua, header_offset);
+ lua_rawget(lua, map_idx);
+ if (lua_istable(lua, -1)) {
+ // convert list of names to list of {name=..., type="global"}
+ lua_newtable(lua); // result table
+ int count = (int)lua_objlen(lua, -2);
+ for (int i = 1; i <= count; i++) {
+ lua_rawgeti(lua, -2, i);
+ const char* name = lua_tostring(lua, -1);
+ if (name) {
+ lua_newtable(lua);
+ lua_pushstring(lua, "name");
+ lua_pushstring(lua, name);
+ lua_settable(lua, -3);
+
+ lua_pushstring(lua, "type");
+ lua_pushstring(lua, "global");
+ lua_settable(lua, -3);
+
+ lua_rawseti(lua, -3, i);
+ }
+ lua_pop(lua, 1); // pop name
+ }
+ lua_remove(lua, -2); // remove map entry list
+ has_symbols = tb_true;
+ } else {
+ lua_pop(lua, 1); // pop nil
+ }
+ }
+
+ if (has_symbols) {
lua_settable(lua, -3);
- lua_rawseti(lua, -2, (int)(++object_count));
+ lua_rawseti(lua, map_idx - 1, (int)(++object_count));
} else {
lua_pop(lua, 2); // pop symbols key and entry table
}
@@ -207,5 +390,6 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base
}
if (longnames) tb_free(longnames);
+ lua_remove(lua, map_idx);
return ok;
}
diff --git a/temp_lua/lapi.c.obj b/temp_lua/lapi.c.obj
new file mode 100644
index 000000000..8aa42030d
--- /dev/null
+++ b/temp_lua/lapi.c.obj
Binary files differ
diff --git a/temp_lua/lauxlib.c.obj b/temp_lua/lauxlib.c.obj
new file mode 100644
index 000000000..e5a558d91
--- /dev/null
+++ b/temp_lua/lauxlib.c.obj
Binary files differ
diff --git a/temp_lua/lbaselib.c.obj b/temp_lua/lbaselib.c.obj
new file mode 100644
index 000000000..3a38af9a0
--- /dev/null
+++ b/temp_lua/lbaselib.c.obj
Binary files differ
diff --git a/temp_lua/lcode.c.obj b/temp_lua/lcode.c.obj
new file mode 100644
index 000000000..cf94e9f85
--- /dev/null
+++ b/temp_lua/lcode.c.obj
Binary files differ
diff --git a/temp_lua/lcorolib.c.obj b/temp_lua/lcorolib.c.obj
new file mode 100644
index 000000000..0c4adb7ec
--- /dev/null
+++ b/temp_lua/lcorolib.c.obj
Binary files differ
diff --git a/temp_lua/lctype.c.obj b/temp_lua/lctype.c.obj
new file mode 100644
index 000000000..d5de2985a
--- /dev/null
+++ b/temp_lua/lctype.c.obj
Binary files differ
diff --git a/temp_lua/ldblib.c.obj b/temp_lua/ldblib.c.obj
new file mode 100644
index 000000000..fdf74211f
--- /dev/null
+++ b/temp_lua/ldblib.c.obj
Binary files differ
diff --git a/temp_lua/ldebug.c.obj b/temp_lua/ldebug.c.obj
new file mode 100644
index 000000000..0eabb7bc7
--- /dev/null
+++ b/temp_lua/ldebug.c.obj
Binary files differ
diff --git a/temp_lua/ldo.c.obj b/temp_lua/ldo.c.obj
new file mode 100644
index 000000000..405fa7cbb
--- /dev/null
+++ b/temp_lua/ldo.c.obj
Binary files differ
diff --git a/temp_lua/ldump.c.obj b/temp_lua/ldump.c.obj
new file mode 100644
index 000000000..68b60f71d
--- /dev/null
+++ b/temp_lua/ldump.c.obj
Binary files differ
diff --git a/temp_lua/lfunc.c.obj b/temp_lua/lfunc.c.obj
new file mode 100644
index 000000000..ae3388413
--- /dev/null
+++ b/temp_lua/lfunc.c.obj
Binary files differ
diff --git a/temp_lua/lgc.c.obj b/temp_lua/lgc.c.obj
new file mode 100644
index 000000000..31e3e39ae
--- /dev/null
+++ b/temp_lua/lgc.c.obj
Binary files differ
diff --git a/temp_lua/linit.c.obj b/temp_lua/linit.c.obj
new file mode 100644
index 000000000..0ae8e9e9d
--- /dev/null
+++ b/temp_lua/linit.c.obj
Binary files differ
diff --git a/temp_lua/liolib.c.obj b/temp_lua/liolib.c.obj
new file mode 100644
index 000000000..64c24b5d9
--- /dev/null
+++ b/temp_lua/liolib.c.obj
Binary files differ
diff --git a/temp_lua/llex.c.obj b/temp_lua/llex.c.obj
new file mode 100644
index 000000000..c0623fd2b
--- /dev/null
+++ b/temp_lua/llex.c.obj
Binary files differ
diff --git a/temp_lua/lmathlib.c.obj b/temp_lua/lmathlib.c.obj
new file mode 100644
index 000000000..88ca44d99
--- /dev/null
+++ b/temp_lua/lmathlib.c.obj
Binary files differ
diff --git a/temp_lua/lmem.c.obj b/temp_lua/lmem.c.obj
new file mode 100644
index 000000000..09a2aba8c
--- /dev/null
+++ b/temp_lua/lmem.c.obj
Binary files differ
diff --git a/temp_lua/loadlib.c.obj b/temp_lua/loadlib.c.obj
new file mode 100644
index 000000000..7d20ac41a
--- /dev/null
+++ b/temp_lua/loadlib.c.obj
Binary files differ
diff --git a/temp_lua/lobject.c.obj b/temp_lua/lobject.c.obj
new file mode 100644
index 000000000..d80831725
--- /dev/null
+++ b/temp_lua/lobject.c.obj
Binary files differ
diff --git a/temp_lua/lopcodes.c.obj b/temp_lua/lopcodes.c.obj
new file mode 100644
index 000000000..91c20ed66
--- /dev/null
+++ b/temp_lua/lopcodes.c.obj
Binary files differ
diff --git a/temp_lua/loslib.c.obj b/temp_lua/loslib.c.obj
new file mode 100644
index 000000000..a67559763
--- /dev/null
+++ b/temp_lua/loslib.c.obj
Binary files differ
diff --git a/temp_lua/lparser.c.obj b/temp_lua/lparser.c.obj
new file mode 100644
index 000000000..dcab37ee4
--- /dev/null
+++ b/temp_lua/lparser.c.obj
Binary files differ
diff --git a/temp_lua/lstate.c.obj b/temp_lua/lstate.c.obj
new file mode 100644
index 000000000..5d950083e
--- /dev/null
+++ b/temp_lua/lstate.c.obj
Binary files differ
diff --git a/temp_lua/lstring.c.obj b/temp_lua/lstring.c.obj
new file mode 100644
index 000000000..5a0ddcd9b
--- /dev/null
+++ b/temp_lua/lstring.c.obj
Binary files differ
diff --git a/temp_lua/lstrlib.c.obj b/temp_lua/lstrlib.c.obj
new file mode 100644
index 000000000..b6ff1b573
--- /dev/null
+++ b/temp_lua/lstrlib.c.obj
Binary files differ
diff --git a/temp_lua/ltable.c.obj b/temp_lua/ltable.c.obj
new file mode 100644
index 000000000..55cae3cf0
--- /dev/null
+++ b/temp_lua/ltable.c.obj
Binary files differ
diff --git a/temp_lua/ltablib.c.obj b/temp_lua/ltablib.c.obj
new file mode 100644
index 000000000..ab578e5b0
--- /dev/null
+++ b/temp_lua/ltablib.c.obj
Binary files differ
diff --git a/temp_lua/ltests.c.obj b/temp_lua/ltests.c.obj
new file mode 100644
index 000000000..f11c42dc0
--- /dev/null
+++ b/temp_lua/ltests.c.obj
Binary files differ
diff --git a/temp_lua/ltm.c.obj b/temp_lua/ltm.c.obj
new file mode 100644
index 000000000..46939ec33
--- /dev/null
+++ b/temp_lua/ltm.c.obj
Binary files differ
diff --git a/temp_lua/lundump.c.obj b/temp_lua/lundump.c.obj
new file mode 100644
index 000000000..b20a5d5ec
--- /dev/null
+++ b/temp_lua/lundump.c.obj
Binary files differ
diff --git a/temp_lua/lutf8lib.c.obj b/temp_lua/lutf8lib.c.obj
new file mode 100644
index 000000000..758fcd891
--- /dev/null
+++ b/temp_lua/lutf8lib.c.obj
Binary files differ
diff --git a/temp_lua/lvm.c.obj b/temp_lua/lvm.c.obj
new file mode 100644
index 000000000..b57ba4a1e
--- /dev/null
+++ b/temp_lua/lvm.c.obj
Binary files differ
diff --git a/temp_lua/lzio.c.obj b/temp_lua/lzio.c.obj
new file mode 100644
index 000000000..73b1fd65b
--- /dev/null
+++ b/temp_lua/lzio.c.obj
Binary files differ