summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-09 00:32:47 +0800
committerruki <[email protected]>2025-12-09 00:32:47 +0800
commitacd4bfbcf3b73349f6b56197f3c0f27f204cc135 (patch)
tree9ffcdd48359e77a4dc6f62938b0e8f164c1af3e9 /core
parent7b24dfe374f14c8682d8ea2b66d8892b104c7a12 (diff)
improve dump
Diffstat (limited to 'core')
-rw-r--r--core/src/xmake/binutils/coff/readsyms.c34
-rw-r--r--core/src/xmake/binutils/elf/readsyms.c96
-rw-r--r--core/src/xmake/binutils/readsyms.c16
3 files changed, 73 insertions, 73 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*)&section, 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*)&section, 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;
}