diff options
| author | ruki <[email protected]> | 2025-12-11 23:31:50 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-12 09:00:44 +0800 |
| commit | 409b21826872503ef479848321003d7ca4c1aad8 (patch) | |
| tree | 6abd52d42fb5d3163425bbbe22a2c52248b62574 | |
| parent | 0b8a0363ed4c21be9795e0b6256d4f1dcd52d722 (diff) | |
format code
| -rw-r--r-- | core/src/xmake/binutils/coff/prefix.h | 2 | ||||
| -rw-r--r-- | core/src/xmake/binutils/coff/readsyms.c | 112 | ||||
| -rw-r--r-- | core/src/xmake/binutils/elf/readsyms.c | 8 | ||||
| -rw-r--r-- | core/src/xmake/binutils/mslib/extractlib.c | 16 |
4 files changed, 69 insertions, 69 deletions
diff --git a/core/src/xmake/binutils/coff/prefix.h b/core/src/xmake/binutils/coff/prefix.h index 19ae9bc7b..60f6ded96 100644 --- a/core/src/xmake/binutils/coff/prefix.h +++ b/core/src/xmake/binutils/coff/prefix.h @@ -76,8 +76,6 @@ typedef struct __xm_coff_anon_header_t { tb_uint32_t size; // size of data } __tb_packed__ xm_coff_anon_header_t; - - typedef struct __xm_coff_section_t { tb_char_t name[8]; tb_uint32_t vsize; diff --git a/core/src/xmake/binutils/coff/readsyms.c b/core/src/xmake/binutils/coff/readsyms.c index 5fac470e2..836fa9962 100644 --- a/core/src/xmake/binutils/coff/readsyms.c +++ b/core/src/xmake/binutils/coff/readsyms.c @@ -34,46 +34,38 @@ * private implementation */ -tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, lua_State *lua) { - tb_assert_and_check_return_val(istream && lua, tb_false); - - // read COFF header - xm_coff_header_t header; - if (!tb_stream_seek(istream, 0)) { - return tb_false; - } - if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) { - return tb_false; - } +static tb_bool_t xm_binutils_coff_read_import_symbols(tb_stream_ref_t istream, lua_State *lua, xm_coff_header_t const* header) { - // check if it is an import object - if (header.machine == 0 && header.nsects == 0xffff) { - // create result table - lua_newtable(lua); + // create result table + lua_newtable(lua); - // check version - // version is the low 16 bits of the time field (offset 4) - // xm_coff_header_t: machine(2), nsects(2), time(4) - // xm_coff_import_header_t: sig1(2), sig2(2), version(2), machine(2) - tb_uint16_t version = header.time & 0xffff; - if (version == 1) { - // anonymous object header (used for CLSID) - xm_coff_anon_header_t anon_header; - if (!tb_stream_seek(istream, 0)) { - return tb_false; - } - if (!tb_stream_bread(istream, (tb_byte_t*)&anon_header, sizeof(anon_header))) { - return tb_false; - } - } else { - // import header - xm_coff_import_header_t import_header; - if (!tb_stream_seek(istream, 0)) { - return tb_false; - } - if (!tb_stream_bread(istream, (tb_byte_t*)&import_header, sizeof(import_header))) { - return tb_false; - } + /* check version + * version is the low 16 bits of the time field (offset 4) + * xm_coff_header_t: machine(2), nsects(2), time(4) + * xm_coff_import_header_t: sig1(2), sig2(2), version(2), machine(2) + */ + tb_uint16_t version = header->time & 0xffff; + if (version == 1) { + // anonymous object header (used for CLSID) + /* + * @note we can not read symbols from the anonymous object (LTO/GL/LTCG), + * because it does not contain the symbol table. + */ + xm_coff_anon_header_t anon_header; + if (!tb_stream_seek(istream, 0)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&anon_header, sizeof(anon_header))) { + return tb_false; + } + } else { + // import header + xm_coff_import_header_t import_header; + if (!tb_stream_seek(istream, 0)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&import_header, sizeof(import_header))) { + return tb_false; } // read symbol name (it follows the header) @@ -92,22 +84,40 @@ tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, lua_State *lua) name[pos] = '\0'; if (name[0]) { - lua_pushinteger(lua, 1); - lua_newtable(lua); + lua_pushinteger(lua, 1); + lua_newtable(lua); - // name - lua_pushstring(lua, "name"); - lua_pushstring(lua, name); - lua_settable(lua, -3); + // name + lua_pushstring(lua, "name"); + lua_pushstring(lua, name); + lua_settable(lua, -3); - // type - lua_pushstring(lua, "type"); - lua_pushstring(lua, "I"); - lua_settable(lua, -3); - - lua_settable(lua, -3); + // type + lua_pushstring(lua, "type"); + lua_pushstring(lua, "I"); + lua_settable(lua, -3); + + lua_settable(lua, -3); } - return tb_true; + } + return tb_true; +} + +tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, lua_State *lua) { + tb_assert_and_check_return_val(istream && lua, tb_false); + + // read COFF header + xm_coff_header_t header; + if (!tb_stream_seek(istream, 0)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) { + return tb_false; + } + + // check if it is an import object + if (header.machine == 0 && header.nsects == 0xffff) { + return xm_binutils_coff_read_import_symbols(istream, lua, &header); } // check if there are symbols diff --git a/core/src/xmake/binutils/elf/readsyms.c b/core/src/xmake/binutils/elf/readsyms.c index f0d0a1061..3aa489381 100644 --- a/core/src/xmake/binutils/elf/readsyms.c +++ b/core/src/xmake/binutils/elf/readsyms.c @@ -128,12 +128,12 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, lua_State *lu 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 . or $) if (name[0] == '.' || name[0] == '$') { continue; } - + // skip local symbols (unless undefined) tb_uint8_t bind = (sym.st_info >> 4) & 0xf; if (bind == 0 && sym.st_shndx != 0) { // STB_LOCAL and not undefined @@ -255,12 +255,12 @@ tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, lua_State *lu 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 . or $) if (name[0] == '.' || name[0] == '$') { continue; } - + // skip local symbols (unless undefined) tb_uint8_t bind = (sym.st_info >> 4) & 0xf; if (bind == 0 && sym.st_shndx != 0) { // STB_LOCAL and not undefined diff --git a/core/src/xmake/binutils/mslib/extractlib.c b/core/src/xmake/binutils/mslib/extractlib.c index c09734468..f56d3eb3e 100644 --- a/core/src/xmake/binutils/mslib/extractlib.c +++ b/core/src/xmake/binutils/mslib/extractlib.c @@ -48,8 +48,6 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou return tb_false; } - tb_trace_d("extracting mslib to %s", outputdir); - // ensure output directory exists // check if directory already exists if (!tb_file_info(outputdir, tb_null)) { @@ -83,12 +81,11 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou // parse member name tb_char_t member_name[256] = {0}; tb_bool_t is_longname_table = tb_false; - + if (header.name[0] == '/') { if (header.name[1] == '/') { // long name table (//) is_longname_table = tb_true; - tb_trace_d("found longname table, size: %lld", member_size); } else if (tb_isdigit(header.name[1])) { // offset into long name table (/123) tb_long_t offset = xm_binutils_mslib_parse_decimal(header.name + 1, 15); @@ -120,7 +117,7 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou } longnames[member_size] = '\0'; longnames_size = (tb_size_t)member_size; - + // align if (member_size % 2) { if (!tb_stream_skip(istream, 1)) { @@ -132,12 +129,10 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou } // check if we should extract - // skip empty names, symbol tables (/), long name table (//) - handled above, + // skip empty names, symbol tables (/), 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 || tb_strncmp(member_name, "__.SYMDEF", 9) == 0) { - - tb_trace_d("skipping member: %s", member_name); // skip member data if (!tb_stream_skip(istream, member_size)) { @@ -164,13 +159,10 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou // check output path length tb_char_t output_path[1024]; if (tb_strlen(outputdir) + 1 + name_len >= sizeof(output_path)) { - tb_trace_e("output path is too long!"); ok = tb_false; break; } tb_snprintf(output_path, sizeof(output_path), "%s/%s", outputdir, member_name); - - tb_trace_d("extracting member: %s to %s", member_name, output_path); // ensure directory exists tb_char_t const* p = tb_strrchr(output_path, '/'); @@ -195,7 +187,7 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou ok = tb_false; break; } - + if (!tb_stream_open(ostream)) { tb_stream_exit(ostream); ok = tb_false; |
