diff options
| author | ruki <[email protected]> | 2025-12-09 00:32:47 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-09 00:32:47 +0800 |
| commit | acd4bfbcf3b73349f6b56197f3c0f27f204cc135 (patch) | |
| tree | 9ffcdd48359e77a4dc6f62938b0e8f164c1af3e9 | |
| parent | 7b24dfe374f14c8682d8ea2b66d8892b104c7a12 (diff) | |
improve dump
| -rw-r--r-- | core/src/xmake/binutils/coff/readsyms.c | 34 | ||||
| -rw-r--r-- | core/src/xmake/binutils/elf/readsyms.c | 96 | ||||
| -rw-r--r-- | core/src/xmake/binutils/readsyms.c | 16 | ||||
| -rw-r--r-- | xmake/modules/utils/binary/readsyms.lua | 68 |
4 files changed, 125 insertions, 89 deletions
diff --git a/core/src/xmake/binutils/coff/readsyms.c b/core/src/xmake/binutils/coff/readsyms.c index edc5cc3ee..d863c2fdf 100644 --- a/core/src/xmake/binutils/coff/readsyms.c +++ b/core/src/xmake/binutils/coff/readsyms.c @@ -36,7 +36,7 @@ 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)) { @@ -45,24 +45,24 @@ tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, lua_State *lua) if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) { return tb_false; } - + // check if there are symbols if (header.nsyms == 0 || header.symtabofs == 0) { lua_newtable(lua); return tb_true; } - + // create result table lua_newtable(lua); - + // read string table offset (after symbol table) tb_uint32_t strtab_offset = header.symtabofs + header.nsyms * 18; // each symbol is 18 bytes - + // read symbols if (!tb_stream_seek(istream, header.symtabofs)) { return tb_false; } - + tb_uint32_t sym_index = 0; tb_uint32_t sym_count = 0; while (sym_index < header.nsyms) { @@ -71,7 +71,7 @@ tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, lua_State *lua) if (!tb_stream_bread(istream, (tb_byte_t*)&sym, sizeof(sym))) { return tb_false; } - + // get symbol name tb_char_t name[256]; if (!xm_binutils_coff_get_symbol_name(istream, &sym, strtab_offset, name, sizeof(name)) || !name[0]) { @@ -84,41 +84,41 @@ tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, lua_State *lua) } continue; } - + // create symbol table entry lua_pushinteger(lua, sym_count + 1); lua_newtable(lua); - + // name lua_pushstring(lua, "name"); lua_pushstring(lua, name); lua_settable(lua, -3); - + // value lua_pushstring(lua, "value"); lua_pushinteger(lua, sym.value); lua_settable(lua, -3); - + // section lua_pushstring(lua, "section"); lua_pushinteger(lua, sym.sect); lua_settable(lua, -3); - + // type lua_pushstring(lua, "type"); lua_pushstring(lua, xm_binutils_coff_get_symbol_type(sym.scl)); lua_settable(lua, -3); - + // storage class lua_pushstring(lua, "storage_class"); lua_pushinteger(lua, sym.scl); lua_settable(lua, -3); - + lua_settable(lua, -3); - + sym_count++; sym_index++; - + // skip auxiliary entries if (sym.naux > 0) { sym_index += sym.naux; @@ -128,6 +128,6 @@ tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, lua_State *lua) } } } - + return tb_true; } diff --git a/core/src/xmake/binutils/elf/readsyms.c b/core/src/xmake/binutils/elf/readsyms.c index ce6653bfe..06c17a697 100644 --- a/core/src/xmake/binutils/elf/readsyms.c +++ b/core/src/xmake/binutils/elf/readsyms.c @@ -36,7 +36,7 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, lua_State *lua) { tb_assert_and_check_return_val(istream && lua, tb_false); - + // read ELF header xm_elf32_header_t header; if (!tb_stream_seek(istream, 0)) { @@ -45,23 +45,23 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, lua_State *lu if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) { return tb_false; } - + // find .symtab section xm_elf32_section_t symtab_section; xm_elf32_section_t strtab_section; tb_bool_t found_symtab = tb_false; tb_bool_t found_strtab = tb_false; - + if (!tb_stream_seek(istream, header.e_shoff)) { return tb_false; } - + for (tb_uint16_t i = 0; i < header.e_shnum; i++) { xm_elf32_section_t section; if (!tb_stream_bread(istream, (tb_byte_t*)§ion, sizeof(section))) { return tb_false; } - + if (section.sh_type == XM_ELF_SHT_SYMTAB) { symtab_section = section; found_symtab = tb_true; @@ -74,12 +74,12 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, lua_State *lu } } } - + if (!found_symtab) { lua_newtable(lua); return tb_true; } - + // find string table if (!found_strtab && symtab_section.sh_link < header.e_shnum) { if (!tb_stream_seek(istream, header.e_shoff + symtab_section.sh_link * sizeof(xm_elf32_section_t))) { @@ -90,83 +90,83 @@ tb_bool_t xm_binutils_elf_read_symbols_32(tb_stream_ref_t istream, lua_State *lu } found_strtab = tb_true; } - + if (!found_strtab) { lua_newtable(lua); return tb_true; } - + // create result table lua_newtable(lua); - + // read symbols tb_uint32_t sym_count = symtab_section.sh_size / sizeof(xm_elf32_symbol_t); if (!tb_stream_seek(istream, symtab_section.sh_offset)) { return tb_false; } - + tb_uint32_t result_count = 0; for (tb_uint32_t i = 0; i < sym_count; i++) { xm_elf32_symbol_t sym; if (!tb_stream_bread(istream, (tb_byte_t*)&sym, sizeof(sym))) { return tb_false; } - + // skip NULL symbol if (sym.st_name == 0 && sym.st_value == 0 && sym.st_size == 0) { continue; } - + // get symbol name tb_char_t name[256]; if (!xm_binutils_elf_read_string(istream, strtab_section.sh_offset, sym.st_name, name, sizeof(name)) || !name[0]) { continue; } - + // create symbol table entry lua_pushinteger(lua, result_count + 1); lua_newtable(lua); - + // name lua_pushstring(lua, "name"); lua_pushstring(lua, name); lua_settable(lua, -3); - + // value lua_pushstring(lua, "value"); lua_pushinteger(lua, sym.st_value); lua_settable(lua, -3); - + // size lua_pushstring(lua, "size"); lua_pushinteger(lua, sym.st_size); lua_settable(lua, -3); - + // section lua_pushstring(lua, "section"); lua_pushinteger(lua, sym.st_shndx); lua_settable(lua, -3); - + // type lua_pushstring(lua, "type"); lua_pushstring(lua, xm_binutils_elf_get_symbol_type(sym.st_info)); lua_settable(lua, -3); - + // bind lua_pushstring(lua, "bind"); lua_pushstring(lua, xm_binutils_elf_get_symbol_bind(sym.st_info)); lua_settable(lua, -3); - + lua_settable(lua, -3); result_count++; } - + return tb_true; } tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, lua_State *lua) { tb_assert_and_check_return_val(istream && lua, tb_false); - + // read ELF header xm_elf64_header_t header; if (!tb_stream_seek(istream, 0)) { @@ -175,23 +175,23 @@ tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, lua_State *lu if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) { return tb_false; } - + // find .symtab section xm_elf64_section_t symtab_section; xm_elf64_section_t strtab_section; tb_bool_t found_symtab = tb_false; tb_bool_t found_strtab = tb_false; - + if (!tb_stream_seek(istream, header.e_shoff)) { return tb_false; } - + for (tb_uint16_t i = 0; i < header.e_shnum; i++) { xm_elf64_section_t section; if (!tb_stream_bread(istream, (tb_byte_t*)§ion, sizeof(section))) { return tb_false; } - + if (section.sh_type == XM_ELF_SHT_SYMTAB) { symtab_section = section; found_symtab = tb_true; @@ -202,12 +202,12 @@ tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, lua_State *lu } } } - + if (!found_symtab) { lua_newtable(lua); return tb_true; } - + // find string table if (!found_strtab && symtab_section.sh_link < header.e_shnum) { if (!tb_stream_seek(istream, header.e_shoff + symtab_section.sh_link * sizeof(xm_elf64_section_t))) { @@ -218,83 +218,83 @@ tb_bool_t xm_binutils_elf_read_symbols_64(tb_stream_ref_t istream, lua_State *lu } found_strtab = tb_true; } - + if (!found_strtab) { lua_newtable(lua); return tb_true; } - + // create result table lua_newtable(lua); - + // read symbols tb_uint32_t sym_count = (tb_uint32_t)(symtab_section.sh_size / sizeof(xm_elf64_symbol_t)); if (!tb_stream_seek(istream, symtab_section.sh_offset)) { return tb_false; } - + tb_uint32_t result_count = 0; for (tb_uint32_t i = 0; i < sym_count; i++) { xm_elf64_symbol_t sym; if (!tb_stream_bread(istream, (tb_byte_t*)&sym, sizeof(sym))) { return tb_false; } - + // skip NULL symbol if (sym.st_name == 0 && sym.st_value == 0 && sym.st_size == 0) { continue; } - + // get symbol name tb_char_t name[256]; if (!xm_binutils_elf_read_string(istream, strtab_section.sh_offset, sym.st_name, name, sizeof(name)) || !name[0]) { continue; } - + // create symbol table entry lua_pushinteger(lua, result_count + 1); lua_newtable(lua); - + // name lua_pushstring(lua, "name"); lua_pushstring(lua, name); lua_settable(lua, -3); - + // value lua_pushstring(lua, "value"); lua_pushinteger(lua, sym.st_value); lua_settable(lua, -3); - + // size lua_pushstring(lua, "size"); lua_pushinteger(lua, sym.st_size); lua_settable(lua, -3); - + // section lua_pushstring(lua, "section"); lua_pushinteger(lua, sym.st_shndx); lua_settable(lua, -3); - + // type lua_pushstring(lua, "type"); lua_pushstring(lua, xm_binutils_elf_get_symbol_type(sym.st_info)); lua_settable(lua, -3); - + // bind lua_pushstring(lua, "bind"); lua_pushstring(lua, xm_binutils_elf_get_symbol_bind(sym.st_info)); lua_settable(lua, -3); - + lua_settable(lua, -3); result_count++; } - + return tb_true; } tb_bool_t xm_binutils_elf_read_symbols(tb_stream_ref_t istream, lua_State *lua) { tb_assert_and_check_return_val(istream && lua, tb_false); - + // read and check ELF magic tb_uint8_t magic[4]; if (!xm_binutils_read_magic(istream, magic, 4)) { @@ -303,7 +303,7 @@ tb_bool_t xm_binutils_elf_read_symbols(tb_stream_ref_t istream, lua_State *lua) if (magic[0] != 0x7f || magic[1] != 'E' || magic[2] != 'L' || magic[3] != 'F') { return tb_false; } - + // check ELF class (32-bit or 64-bit) tb_uint8_t elf_class; if (!tb_stream_seek(istream, 4)) { @@ -312,13 +312,13 @@ tb_bool_t xm_binutils_elf_read_symbols(tb_stream_ref_t istream, lua_State *lua) if (!tb_stream_bread(istream, (tb_byte_t*)&elf_class, 1)) { return tb_false; } - + if (elf_class == 1) { return xm_binutils_elf_read_symbols_32(istream, lua); } else if (elf_class == 2) { return xm_binutils_elf_read_symbols_64(istream, lua); } - + return tb_false; } diff --git a/core/src/xmake/binutils/readsyms.c b/core/src/xmake/binutils/readsyms.c index dbb8e5957..ae261782f 100644 --- a/core/src/xmake/binutils/readsyms.c +++ b/core/src/xmake/binutils/readsyms.c @@ -51,11 +51,11 @@ extern tb_bool_t xm_binutils_macho_read_symbols(tb_stream_ref_t istream, lua_Sta */ tb_int_t xm_binutils_readsyms(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - + // get the object file path tb_char_t const *objectfile = luaL_checkstring(lua, 1); tb_check_return_val(objectfile, 0); - + // open file tb_stream_ref_t istream = tb_stream_init_from_file(objectfile, TB_FILE_MODE_RO); if (!istream) { @@ -63,7 +63,7 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) { lua_pushfstring(lua, "readsyms: open %s failed", objectfile); return 2; } - + tb_bool_t ok = tb_false; do { if (!tb_stream_open(istream)) { @@ -71,7 +71,7 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) { lua_pushfstring(lua, "readsyms: open %s failed", objectfile); break; } - + // detect format tb_int_t format = xm_binutils_detect_format(istream); if (format < 0) { @@ -79,7 +79,7 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) { lua_pushfstring(lua, "readsyms: cannot detect file format"); break; } - + // read symbols based on format if (format == XM_BINUTILS_FORMAT_COFF) { // COFF @@ -108,15 +108,15 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) { lua_pushfstring(lua, "readsyms: unsupported or unknown file format"); break; } - + ok = tb_true; } while (0); - + if (istream) { tb_stream_clos(istream); } istream = tb_null; - + return ok ? 1 : 2; } diff --git a/xmake/modules/utils/binary/readsyms.lua b/xmake/modules/utils/binary/readsyms.lua index 212c99401..899b43f71 100644 --- a/xmake/modules/utils/binary/readsyms.lua +++ b/xmake/modules/utils/binary/readsyms.lua @@ -40,30 +40,66 @@ end function dump(binaryfile) local symbols = _get_symbols(binaryfile) if symbols and #symbols > 0 then - print("") - print("Symbols:") + -- calculate column widths for alignment + local max_value_len = 0 + local max_name_len = 0 + local max_type_len = 0 + local max_bind_len = 0 + local max_section_len = 0 + local max_size_len = 0 + for i, sym in ipairs(symbols) do - local value_str = "" if sym.value then - value_str = string.format("0x%x", sym.value) - end - local size_str = "" - if sym.size then - size_str = string.format(" size=%d", sym.size) + local value_str = string.format("0x%x", sym.value) + max_value_len = math.max(max_value_len, #value_str) end - local section_str = "" - if sym.section and sym.section > 0 then - section_str = string.format(" section=%d", sym.section) + if sym.name then + max_name_len = math.max(max_name_len, #sym.name) end - local type_str = "" if sym.type then - type_str = string.format(" type=%s", sym.type) + max_type_len = math.max(max_type_len, #sym.type) end - local bind_str = "" if sym.bind then - bind_str = string.format(" bind=%s", sym.bind) + max_bind_len = math.max(max_bind_len, #sym.bind) + end + if sym.section then + local section_str = tostring(sym.section) + max_section_len = math.max(max_section_len, #section_str) + end + if sym.size then + local size_str = tostring(sym.size) + max_size_len = math.max(max_size_len, #size_str) end - print(string.format(" %s %s%s%s%s%s", sym.name or "", value_str, size_str, section_str, type_str, bind_str)) + end + + -- calculate column widths + local value_width = math.max(max_value_len, 10) + local type_width = math.max(max_type_len, 4) + local bind_width = math.max(max_bind_len, 4) + local section_width = math.max(max_section_len, 7) + local size_width = math.max(max_size_len, 4) + + -- print header + print("") + print("Symbols:") + local header_format = string.format(" %%-%ds %%-%ds %%-%ds %%-%ds %%-%ds %%s", + value_width, type_width, bind_width, section_width, size_width) + print(string.format(header_format, "VALUE", "TYPE", "BIND", "SECTION", "SIZE", "NAME")) + print(string.rep("-", 80)) + + -- print symbols + local format_str = string.format(" %%-%ds %%-%ds %%-%ds %%-%ds %%-%ds %%s", + value_width, type_width, bind_width, section_width, size_width) + + for i, sym in ipairs(symbols) do + local value_str = sym.value and string.format("0x%x", sym.value) or "" + local type_str = sym.type or "" + local bind_str = sym.bind or "" + local section_str = sym.section and sym.section > 0 and tostring(sym.section) or "" + local size_str = sym.size and tostring(sym.size) or "" + local name_str = sym.name or "" + + print(string.format(format_str, value_str, type_str, bind_str, section_str, size_str, name_str)) end print("") cprint("${bright}%d symbols found!", #symbols) |
