diff options
| author | ruki <[email protected]> | 2025-12-12 00:45:57 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-12 09:00:45 +0800 |
| commit | eeb70e956f65c2dd4581ead1581dfa7c8e64ed00 (patch) | |
| tree | cc100e55f11af35053cd6829e823511acd603540 /core/src | |
| parent | bf35be31ee869cf75a9a32e52069867326e23cda (diff) | |
fix some codes
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/binutils/ar/extractlib.c | 27 | ||||
| -rw-r--r-- | core/src/xmake/binutils/ar/readsyms.c | 40 | ||||
| -rw-r--r-- | core/src/xmake/binutils/mslib/extractlib.c | 10 | ||||
| -rw-r--r-- | core/src/xmake/binutils/mslib/readsyms.c | 32 |
4 files changed, 53 insertions, 56 deletions
diff --git a/core/src/xmake/binutils/ar/extractlib.c b/core/src/xmake/binutils/ar/extractlib.c index 32ec4e547..4ae349544 100644 --- a/core/src/xmake/binutils/ar/extractlib.c +++ b/core/src/xmake/binutils/ar/extractlib.c @@ -48,12 +48,13 @@ static tb_bool_t xm_binutils_ar_get_member_name(tb_stream_ref_t istream, xm_ar_h tb_assert_and_check_return_val(istream && header && name && name_size > 0 && name_len && bytes_read, tb_false); *bytes_read = 0; - // check for extended name format (#N/L or #1/N) - // In BSD AR format: - // - #1/N means name is directly after header, N is total length (including name) - // - #N/L means name length is N, total length is L - // - #1/N can also mean name is in long name table at offset 1 - // We'll try to read the name directly from stream first + /* check for extended name format (#N/L or #1/N) + * In BSD AR format: + * - #1/N means name is directly after header, N is total length (including name) + * - #N/L means name length is N, total length is L + * - #1/N can also mean name is in long name table at offset 1 + * We'll try to read the name directly from stream first + */ if (header->name[0] == '#') { // find the '/' separator tb_size_t slash_pos = 0; @@ -74,9 +75,10 @@ static tb_bool_t xm_binutils_ar_get_member_name(tb_stream_ref_t istream, xm_ar_h return tb_false; } - // In BSD AR format, extended name is directly after header - // The name data starts immediately after the header, no newline - // Read exactly total_length bytes for the name section + /* In BSD AR format, extended name is directly after header + * The name data starts immediately after the header, no newline + * Read exactly total_length bytes for the name section + */ tb_byte_t c; tb_size_t name_bytes = 0; tb_hize_t bytes_read_so_far = 0; @@ -270,7 +272,6 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu tb_char_t output_name[512] = {0}; tb_char_t output_path_check[1024] = {0}; if (outputdir_len + 1 + name_len >= sizeof(output_path_check)) { - tb_trace_e("output path is too long!"); ok = tb_false; break; } @@ -287,7 +288,6 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu break; } if (outputdir_len + 1 + output_name_len >= sizeof(output_path_check)) { - tb_trace_e("output path is too long!"); ok = tb_false; break; } @@ -310,7 +310,6 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu // build output path tb_char_t output_path[1024] = {0}; if (outputdir_len + 1 + output_name_len >= sizeof(output_path)) { - tb_trace_e("output path is too long!"); ok = tb_false; break; } @@ -355,9 +354,7 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu tb_stream_clos(ostream); tb_stream_exit(ostream); - if (!ok) { - break; - } + tb_check_break(ok); // align to 2-byte boundary (AR format requirement) if (member_size % 2) { diff --git a/core/src/xmake/binutils/ar/readsyms.c b/core/src/xmake/binutils/ar/readsyms.c index f60460efd..307b465df 100644 --- a/core/src/xmake/binutils/ar/readsyms.c +++ b/core/src/xmake/binutils/ar/readsyms.c @@ -188,11 +188,11 @@ static tb_bool_t xm_binutils_ar_parse_bsd_symdef(tb_stream_ref_t istream, tb_hiz // read ranlib array tb_size_t num_ranlibs = ranlib_size / 8; - + // allocate buffers tb_uint32_t* ran_strx = tb_nalloc_type(num_ranlibs, tb_uint32_t); tb_uint32_t* ran_off = tb_nalloc_type(num_ranlibs, tb_uint32_t); - + if (!ran_strx || !ran_off) { if (ran_strx) tb_free(ran_strx); if (ran_off) tb_free(ran_off); @@ -240,10 +240,10 @@ static tb_bool_t xm_binutils_ar_parse_bsd_symdef(tb_stream_ref_t istream, tb_hiz for (i = 0; i < num_ranlibs; i++) { tb_uint32_t off = ran_off[i]; tb_uint32_t strx = ran_strx[i]; - + if (strx < strtab_size) { tb_char_t* name = strtab + strx; - + // add to map: map[off] = { {name=name, type="T"}, ... } lua_pushinteger(lua, off); lua_rawget(lua, map_idx); @@ -254,16 +254,16 @@ static tb_bool_t xm_binutils_ar_parse_bsd_symdef(tb_stream_ref_t istream, tb_hiz lua_pushvalue(lua, -2); lua_rawset(lua, map_idx); } - + int count = (int)lua_objlen(lua, -1); lua_newtable(lua); lua_pushstring(lua, "name"); lua_pushstring(lua, name); lua_settable(lua, -3); lua_pushstring(lua, "type"); - lua_pushstring(lua, "T"); + lua_pushstring(lua, "T"); lua_settable(lua, -3); - + lua_rawseti(lua, -2, count + 1); lua_pop(lua, 1); // pop list } @@ -314,7 +314,7 @@ static tb_bool_t xm_binutils_ar_parse_sysv_symdef(tb_stream_ref_t istream, tb_hi // read string table tb_hize_t current = tb_stream_offset(istream); tb_hize_t strtab_size = member_size - (current - start_pos); - + tb_char_t* strtab = (tb_char_t*)tb_malloc_bytes((tb_size_t)strtab_size); if (!strtab) { tb_free(offsets); @@ -331,16 +331,16 @@ static tb_bool_t xm_binutils_ar_parse_sysv_symdef(tb_stream_ref_t istream, tb_hi // populate map tb_char_t* p = strtab; tb_char_t* end = strtab + strtab_size; - + for (i = 0; i < num_symbols; i++) { if (p >= end) break; - + tb_char_t* name = p; tb_size_t len = tb_strlen(name); p += len + 1; - + tb_uint32_t off = offsets[i]; - + // add to map lua_pushinteger(lua, off); lua_rawget(lua, map_idx); @@ -351,16 +351,16 @@ static tb_bool_t xm_binutils_ar_parse_sysv_symdef(tb_stream_ref_t istream, tb_hi lua_pushvalue(lua, -2); lua_rawset(lua, map_idx); } - + int count = (int)lua_objlen(lua, -1); lua_newtable(lua); lua_pushstring(lua, "name"); lua_pushstring(lua, name); lua_settable(lua, -3); lua_pushstring(lua, "type"); - lua_pushstring(lua, "T"); + lua_pushstring(lua, "T"); lua_settable(lua, -3); - + lua_rawseti(lua, -2, count + 1); lua_pop(lua, 1); // pop list } @@ -429,9 +429,9 @@ tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_of if (xm_binutils_ar_is_symbol_table(member_name)) { /* parse symbol table * - * The symbol table in the archive only contains symbol names and their offsets, + * The symbol table in the archive only contains symbol names and their offsets, * but lacks detailed symbol type information (e.g., distinguishing between code and data). - * However, for object files that cannot be parsed (e.g., LTO bitcode) or unknown formats, + * However, for object files that cannot be parsed (e.g., LTO bitcode) or unknown formats, * parsing the symbol table serves as a robust fallback to ensure symbols are extracted. */ tb_hize_t current = tb_stream_offset(istream); @@ -461,7 +461,7 @@ tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_of // save current position tb_hize_t current_pos = tb_stream_offset(istream); - + // detect format tb_int_t format = xm_binutils_detect_format(istream); if (format != XM_BINUTILS_FORMAT_AR) { @@ -489,9 +489,9 @@ tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_of * * If parsing the object file fails (e.g. for LTO bitcode or unsupported formats), * we fall back to using the symbols parsed from the archive symbol table. - * Although the type information is less accurate (defaulting to "T"), + * Although the type information is less accurate (defaulting to "T"), * it guarantees that symbols are not lost. - * + * * cast to lua_Integer to avoid warning C4244 on 32-bit MSVC * member_header_pos is tb_hize_t (64-bit), but AR offsets are usually 32-bit */ diff --git a/core/src/xmake/binutils/mslib/extractlib.c b/core/src/xmake/binutils/mslib/extractlib.c index e6569ac3e..1be374355 100644 --- a/core/src/xmake/binutils/mslib/extractlib.c +++ b/core/src/xmake/binutils/mslib/extractlib.c @@ -148,10 +148,9 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou } if (is_longname_table) { - if (longnames) tb_free(longnames); - longnames = (tb_char_t*)tb_malloc_bytes((tb_size_t)member_size + 1); - if (!longnames || !tb_stream_bread(istream, (tb_byte_t*)longnames, (tb_size_t)member_size)) { - ok = tb_false; + longnames = (tb_char_t*)tb_ralloc(longnames, (tb_size_t)member_size + 1); + if (!longnames || !tb_stream_bread(istream, (tb_byte_t*)longnames, (tb_size_t)member_size)) { + ok = tb_false; break; } longnames[member_size] = '\0'; @@ -159,7 +158,7 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou // align if (member_size % 2) { - if (!tb_stream_skip(istream, 1)) { + if (!tb_stream_skip(istream, 1)) { ok = tb_false; break; } @@ -179,6 +178,7 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou ok = tb_false; break; } + // align if (member_size % 2) { if (!tb_stream_skip(istream, 1)) { diff --git a/core/src/xmake/binutils/mslib/readsyms.c b/core/src/xmake/binutils/mslib/readsyms.c index 50d7aa416..6eb847bf6 100644 --- a/core/src/xmake/binutils/mslib/readsyms.c +++ b/core/src/xmake/binutils/mslib/readsyms.c @@ -43,7 +43,7 @@ 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); - + // read number of members tb_uint32_t num_members = 0; if (!tb_stream_bread_u32_le(istream, &num_members)) return tb_false; @@ -51,7 +51,7 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream // sanity check if (num_members == 0 || num_members > 65536 || num_members * 4 >= member_size) { tb_stream_seek(istream, start_pos); - return tb_false; + return tb_false; } // read offsets @@ -60,7 +60,7 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream 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])) { @@ -77,8 +77,8 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream tb_stream_seek(istream, start_pos); return tb_false; } - - if (num_symbols == 0 || num_symbols > 1000000) { + + if (num_symbols == 0 || num_symbols > 1000000) { tb_free(offsets); tb_stream_seek(istream, start_pos); return tb_false; @@ -104,7 +104,7 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream // 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); @@ -124,10 +124,10 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream // 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; @@ -135,7 +135,7 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream 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)) { @@ -231,8 +231,7 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base } if (is_longname_table) { - if (longnames) tb_free(longnames); - longnames = (tb_char_t*)tb_malloc_bytes((tb_size_t)member_size + 1); + longnames = (tb_char_t*)tb_ralloc(longnames, (tb_size_t)member_size + 1); if (!longnames || !tb_stream_bread(istream, (tb_byte_t*)longnames, (tb_size_t)member_size)) { ok = tb_false; break; @@ -242,7 +241,7 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base // align if (member_size % 2) { - if (!tb_stream_skip(istream, 1)) { + if (!tb_stream_skip(istream, 1)) { ok = tb_false; break; } @@ -262,6 +261,7 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base ok = tb_false; break; } + // align if (member_size % 2) { if (!tb_stream_skip(istream, 1)) { @@ -294,7 +294,7 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base // 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); if (format != XM_BINUTILS_FORMAT_UNKNOWN && format != XM_BINUTILS_FORMAT_AR) { @@ -343,11 +343,11 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base lua_pushstring(lua, "name"); lua_pushstring(lua, name); lua_settable(lua, -3); - + lua_pushstring(lua, "type"); lua_pushstring(lua, "T"); lua_settable(lua, -3); - + lua_rawseti(lua, -3, i); } lua_pop(lua, 1); // pop name @@ -382,7 +382,7 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base break; } } - + // align to 2-byte boundary if (member_size % 2) { if (!tb_stream_skip(istream, 1)) { |
