summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-12 00:49:38 +0800
committerruki <[email protected]>2025-12-12 09:00:46 +0800
commit21f760f085fb09467770d28efdade3ab47ef38e6 (patch)
tree1278daae2daa2f70b39a935594d36ca733669101 /core/src
parent12e8ee1a5e81efd4e32405171172423698e1b640 (diff)
improve mslib
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/binutils/mslib/extractlib.c26
-rw-r--r--core/src/xmake/binutils/mslib/readsyms.c158
2 files changed, 71 insertions, 113 deletions
diff --git a/core/src/xmake/binutils/mslib/extractlib.c b/core/src/xmake/binutils/mslib/extractlib.c
index 1be374355..dce564b41 100644
--- a/core/src/xmake/binutils/mslib/extractlib.c
+++ b/core/src/xmake/binutils/mslib/extractlib.c
@@ -95,7 +95,6 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou
}
tb_bool_t ok = tb_true;
- tb_byte_t* buffer = tb_null;
tb_char_t* longnames = tb_null;
tb_size_t longnames_size = 0;
@@ -234,7 +233,8 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou
}
conflict_id++;
}
- if (!ok || conflict_id >= 10000) {
+ tb_check_break(ok);
+ if (conflict_id >= 10000) {
ok = tb_false;
break;
}
@@ -278,28 +278,11 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou
}
// copy data
- if (!buffer) buffer = tb_malloc_bytes(TB_STREAM_BLOCK_MAXN);
- if (!buffer) {
- tb_stream_exit(ostream);
+ if (!xm_binutils_stream_copy(istream, ostream, member_size)) {
ok = tb_false;
- break;
- }
-
- tb_hize_t remaining = member_size;
- while (remaining > 0) {
- tb_size_t to_read = (tb_size_t)tb_min(remaining, TB_STREAM_BLOCK_MAXN);
- if (!tb_stream_bread(istream, buffer, to_read)) {
- ok = tb_false;
- break;
- }
- if (!tb_stream_bwrit(ostream, buffer, to_read)) {
- ok = tb_false;
- break;
- }
- remaining -= to_read;
}
tb_stream_exit(ostream);
- if (!ok) break;
+ tb_check_break(ok);
// align to 2-byte boundary
if (member_size % 2) {
@@ -310,7 +293,6 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou
}
}
- if (buffer) tb_free(buffer);
if (longnames) tb_free(longnames);
return ok;
}
diff --git a/core/src/xmake/binutils/mslib/readsyms.c b/core/src/xmake/binutils/mslib/readsyms.c
index 6eb847bf6..188fda297 100644
--- a/core/src/xmake/binutils/mslib/readsyms.c
+++ b/core/src/xmake/binutils/mslib/readsyms.c
@@ -43,119 +43,95 @@ extern tb_bool_t xm_binutils_macho_read_symbols(tb_stream_ref_t istream, tb_hize
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);
+ tb_uint32_t* offsets = tb_null;
+ tb_uint16_t* indices = tb_null;
+ tb_char_t* string_table = tb_null;
+ tb_bool_t ok = tb_false;
- // read number of members
- tb_uint32_t num_members = 0;
- if (!tb_stream_bread_u32_le(istream, &num_members)) return tb_false;
+ do {
+ // read number of members
+ tb_uint32_t num_members = 0;
+ if (!tb_stream_bread_u32_le(istream, &num_members)) break;
- // sanity check
- if (num_members == 0 || num_members > 65536 || num_members * 4 >= member_size) {
- tb_stream_seek(istream, start_pos);
- return tb_false;
- }
+ // sanity check
+ if (num_members == 0 || num_members > 65536 || num_members * 4 >= member_size) break;
- // 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;
- }
+ // read offsets
+ offsets = tb_nalloc_type(num_members, tb_uint32_t);
+ tb_check_break(offsets);
- 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;
+ tb_size_t i;
+ for (i = 0; i < num_members; i++) {
+ if (!tb_stream_bread_u32_le(istream, &offsets[i])) break;
}
- }
+ if (i < num_members) break;
- // 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;
- }
+ // read number of symbols
+ tb_uint32_t num_symbols = 0;
+ if (!tb_stream_bread_u32_le(istream, &num_symbols)) break;
- if (num_symbols == 0 || num_symbols > 1000000) {
- tb_free(offsets);
- tb_stream_seek(istream, start_pos);
- return tb_false;
- }
+ if (num_symbols == 0 || num_symbols > 1000000) break;
- // 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;
- }
+ // read indices
+ indices = tb_nalloc_type(num_symbols, tb_uint16_t);
+ tb_check_break(indices);
- 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;
+ for (i = 0; i < num_symbols; i++) {
+ if (!tb_stream_bread_u16_le(istream, &indices[i])) break;
}
- }
+ if (i < num_symbols) break;
- // read string table
- tb_hize_t current = tb_stream_offset(istream);
- tb_hize_t string_table_size = member_size - (current - start_pos);
+ // 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;
- }
+ string_table = (tb_char_t*)tb_malloc_bytes((tb_size_t)string_table_size);
+ tb_check_break(string_table);
- 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;
- }
+ if (!tb_stream_bread(istream, (tb_byte_t*)string_table, (tb_size_t)string_table_size)) break;
- // populate map
- tb_char_t* p = string_table;
- tb_char_t* end = string_table + string_table_size;
+ // 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;
+ 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_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];
+ 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);
+ 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
}
- int count = (int)lua_objlen(lua, -1);
- lua_pushstring(lua, sym_name);
- lua_rawseti(lua, -2, count + 1);
- lua_pop(lua, 1); // pop list
}
- }
+ ok = tb_true;
+
+ } while (0);
+
+ if (offsets) tb_free(offsets);
+ if (indices) tb_free(indices);
+ if (string_table) tb_free(string_table);
- tb_free(string_table);
- tb_free(indices);
- tb_free(offsets);
- return tb_true;
+ if (!ok) {
+ tb_stream_seek(istream, start_pos);
+ }
+ return ok;
}
/* read symbols from MSVC lib archive