summaryrefslogtreecommitdiff
path: root/core/src/xmake/binutils/elf
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/src/xmake/binutils/elf
parent7b24dfe374f14c8682d8ea2b66d8892b104c7a12 (diff)
improve dump
Diffstat (limited to 'core/src/xmake/binutils/elf')
-rw-r--r--core/src/xmake/binutils/elf/readsyms.c96
1 files changed, 48 insertions, 48 deletions
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;
}