Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/src/xmake/binutils/ar/prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#include "../coff/prefix.h"
#include "../elf/prefix.h"
#include "../macho/prefix.h"
#include "../wasm/prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* forward declarations
*/
extern tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
extern tb_bool_t xm_binutils_elf_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
extern tb_bool_t xm_binutils_macho_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
extern tb_bool_t xm_binutils_wasm_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);

/* //////////////////////////////////////////////////////////////////////////////////////
* types
Expand Down Expand Up @@ -157,6 +159,18 @@ static __tb_inline__ tb_bool_t xm_binutils_ar_get_member_name(tb_stream_ref_t is
}
}

if (header->name[0] == '/') {
if (header->name[1] == '/') {
tb_strlcpy(name, "//", name_size);
*name_len = 2;
} else {
tb_strlcpy(name, "/", name_size);
*name_len = 1;
}
*bytes_read = 0;
return tb_true;
}

// regular name (null-terminated or space-padded)
tb_size_t i = 0;
for (i = 0; i < 16 && i < name_size - 1; i++) {
Expand Down
28 changes: 27 additions & 1 deletion core/src/xmake/binutils/ar/readsyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
static tb_int_t xm_binutils_ar_detect_member_format(tb_stream_ref_t istream, tb_hize_t base_offset) {
tb_uint8_t magic[8] = {0};
if (!tb_stream_seek(istream, base_offset)) {
return -1;
}
if (!tb_stream_bread(istream, magic, 8)) {
tb_stream_seek(istream, base_offset);
return -1;
}
tb_stream_seek(istream, base_offset);

if (magic[0] == XM_WASM_MAGIC0 && magic[1] == XM_WASM_MAGIC1 && magic[2] == XM_WASM_MAGIC2 && magic[3] == XM_WASM_MAGIC3) {
return XM_BINUTILS_FORMAT_WASM;
}
if (magic[0] == XM_ELF_MAGIC0 && magic[1] == XM_ELF_MAGIC1 && magic[2] == XM_ELF_MAGIC2 && magic[3] == XM_ELF_MAGIC3) {
return XM_BINUTILS_FORMAT_ELF;
}
tb_uint32_t macho_magic = tb_bits_get_u32_be(magic);
if (macho_magic == XM_MACHO_MAGIC_32 || macho_magic == XM_MACHO_MAGIC_64 ||
macho_magic == XM_MACHO_MAGIC_32_BE || macho_magic == XM_MACHO_MAGIC_64_BE) {
return XM_BINUTILS_FORMAT_MACHO;
}
return XM_BINUTILS_FORMAT_UNKNOWN;
}

/* parse BSD symbol table (__.SYMDEF or __.SYMDEF SORTED)
*
Expand Down Expand Up @@ -346,7 +370,7 @@ tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_of
tb_hize_t current_pos = tb_stream_offset(istream);

// detect format
tb_int_t format = xm_binutils_format_detect(istream);
tb_int_t format = xm_binutils_ar_detect_member_format(istream, current_pos);
if (format != XM_BINUTILS_FORMAT_AR) {
// create entry table
lua_newtable(lua);
Expand All @@ -365,6 +389,8 @@ tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_of
read_ok = xm_binutils_elf_read_symbols(istream, current_pos, lua);
} else if (format == XM_BINUTILS_FORMAT_MACHO) {
read_ok = xm_binutils_macho_read_symbols(istream, current_pos, lua);
} else if (format == XM_BINUTILS_FORMAT_WASM) {
read_ok = xm_binutils_wasm_read_symbols(istream, current_pos, lua);
}

if (!read_ok) {
Expand Down
22 changes: 11 additions & 11 deletions core/src/xmake/binutils/deplibs.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ tb_int_t xm_binutils_deplibs(lua_State *lua) {
tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO);
if (!istream) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: open %s failed", binaryfile);
lua_pushfstring(lua, "open %s failed", binaryfile);
return 2;
}

tb_bool_t ok = tb_false;
do {
if (!tb_stream_open(istream)) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: open %s failed", binaryfile);
lua_pushfstring(lua, "open %s failed", binaryfile);
break;
}

// detect format
tb_int_t format = xm_binutils_format_detect(istream);
if (format < 0) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: cannot detect file format");
lua_pushfstring(lua, "cannot detect file format");
break;
}

Expand All @@ -88,15 +88,15 @@ tb_int_t xm_binutils_deplibs(lua_State *lua) {
if (!xm_binutils_coff_deplibs(istream, 0, lua)) {
lua_pop(lua, 1); // pop table
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: failed to parse COFF");
lua_pushfstring(lua, "failed to parse COFF");
break;
}
} else if (format == XM_BINUTILS_FORMAT_PE) {
// seek to e_lfanew
if (!tb_stream_seek(istream, 0x3c)) {
lua_pop(lua, 1); // pop table
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: failed to seek to e_lfanew");
lua_pushfstring(lua, "failed to seek to e_lfanew");
break;
}

Expand All @@ -105,7 +105,7 @@ tb_int_t xm_binutils_deplibs(lua_State *lua) {
if (!tb_stream_bread(istream, (tb_byte_t*)&e_lfanew, 4)) {
lua_pop(lua, 1); // pop table
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: failed to read e_lfanew");
lua_pushfstring(lua, "failed to read e_lfanew");
break;
}

Expand All @@ -116,31 +116,31 @@ tb_int_t xm_binutils_deplibs(lua_State *lua) {
if (!xm_binutils_coff_deplibs(istream, e_lfanew + 4, lua)) {
lua_pop(lua, 1); // pop table
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: failed to parse PE/COFF");
lua_pushfstring(lua, "failed to parse PE/COFF");
break;
}
} else if (format == XM_BINUTILS_FORMAT_MACHO) {
if (!xm_binutils_macho_deplibs(istream, 0, lua)) {
lua_pop(lua, 1); // pop table
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: failed to parse Mach-O");
lua_pushfstring(lua, "failed to parse Mach-O");
break;
}
} else if (format == XM_BINUTILS_FORMAT_ELF) {
if (!xm_binutils_elf_deplibs(istream, 0, lua)) {
lua_pop(lua, 1); // pop table
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: failed to parse ELF");
lua_pushfstring(lua, "failed to parse ELF");
break;
}
} else if (format == XM_BINUTILS_FORMAT_WASM) {
} else {
lua_pop(lua, 1); // pop table
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "deplibs: unsupported format %d", format);
lua_pushfstring(lua, "unsupported format %d", format);
break;
}


ok = tb_true;

} while (0);
Expand Down
5 changes: 5 additions & 0 deletions core/src/xmake/binutils/elf/prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
#define XM_ELF_MAGIC0 0x7f
#define XM_ELF_MAGIC1 'E'
#define XM_ELF_MAGIC2 'L'
#define XM_ELF_MAGIC3 'F'

// ELF class
#define XM_ELF_EI_CLASS 4
#define XM_ELF_CLASS32 1
Expand Down
6 changes: 3 additions & 3 deletions core/src/xmake/binutils/extractlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ tb_int_t xm_binutils_extractlib(lua_State *lua) {
tb_stream_ref_t istream = tb_stream_init_from_file(libraryfile, TB_FILE_MODE_RO);
if (!istream) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "extractlib: open %s failed", libraryfile);
lua_pushfstring(lua, "open %s failed", libraryfile);
return 2;
}

Expand Down Expand Up @@ -139,9 +139,9 @@ tb_int_t xm_binutils_extractlib(lua_State *lua) {
} else {
lua_pushboolean(lua, tb_false);
if (error_msg) {
lua_pushfstring(lua, "extractlib: %s %s", error_msg, libraryfile);
lua_pushfstring(lua, "%s %s", error_msg, libraryfile);
} else {
lua_pushfstring(lua, "extractlib: unknown error for %s", libraryfile);
lua_pushfstring(lua, "unknown error for %s", libraryfile);
}
return 2;
}
Expand Down
10 changes: 10 additions & 0 deletions core/src/xmake/binutils/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ static __tb_inline__ tb_bool_t xm_binutils_format_is_ape(tb_byte_t const* first8
first8[4] == 'p' && first8[5] == 'D';
}

static __tb_inline__ tb_bool_t xm_binutils_format_is_wasm(tb_byte_t const* first8) {
return first8[0] == 0x00 && first8[1] == 0x61 && first8[2] == 0x73 && first8[3] == 0x6d;
}

static __tb_inline__ tb_bool_t xm_binutils_format_is_elf(tb_byte_t const* first8) {
return first8[0] == 0x7f && first8[1] == 'E' && first8[2] == 'L' && first8[3] == 'F';
}
Expand Down Expand Up @@ -164,6 +168,11 @@ tb_int_t xm_binutils_format_detect(tb_stream_ref_t istream) {
break;
}

if (xm_binutils_format_is_wasm(p)) {
format = XM_BINUTILS_FORMAT_WASM;
break;
}

if (xm_binutils_format_is_elf(p)) {
format = XM_BINUTILS_FORMAT_ELF;
break;
Expand Down Expand Up @@ -237,6 +246,7 @@ tb_int_t xm_binutils_format(lua_State *lua) {
case XM_BINUTILS_FORMAT_PE: lua_pushliteral(lua, "pe"); break;
case XM_BINUTILS_FORMAT_SHEBANG: lua_pushliteral(lua, "shebang"); break;
case XM_BINUTILS_FORMAT_APE: lua_pushliteral(lua, "ape"); break;
case XM_BINUTILS_FORMAT_WASM: lua_pushliteral(lua, "wasm"); break;
default: lua_pushliteral(lua, "unknown"); break;
}

Expand Down
1 change: 1 addition & 0 deletions core/src/xmake/binutils/prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#define XM_BINUTILS_FORMAT_PE 5
#define XM_BINUTILS_FORMAT_SHEBANG 6
#define XM_BINUTILS_FORMAT_APE 7
#define XM_BINUTILS_FORMAT_WASM 8
#define XM_BINUTILS_FORMAT_UNKNOWN 0

// COFF machine types (for format detection)
Expand Down
15 changes: 9 additions & 6 deletions core/src/xmake/binutils/readsyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern tb_bool_t xm_binutils_elf_read_symbols(tb_stream_ref_t istream, tb_hize_t
extern tb_bool_t xm_binutils_macho_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
extern tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
extern tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
extern tb_bool_t xm_binutils_wasm_read_symbols(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);

/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
Expand All @@ -62,23 +63,23 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) {
tb_stream_ref_t istream = tb_stream_init_from_file(objectfile, TB_FILE_MODE_RO);
if (!istream) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "readsyms: open %s failed", objectfile);
lua_pushfstring(lua, "open %s failed", objectfile);
return 2;
}

tb_bool_t ok = tb_false;
do {
if (!tb_stream_open(istream)) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "readsyms: open %s failed", objectfile);
lua_pushfstring(lua, "open %s failed", objectfile);
break;
}

// detect format
tb_int_t format = xm_binutils_format_detect(istream);
if (format < 0) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "readsyms: cannot detect file format");
lua_pushfstring(lua, "cannot detect file format");
break;
}

Expand All @@ -101,14 +102,14 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) {
// fallback to ar
if (!xm_binutils_ar_read_symbols(istream, 0, lua)) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "readsyms: read AR/MSLIB archive symbols failed");
lua_pushfstring(lua, "read AR/MSLIB archive symbols failed");
break;
}
}
} else {
if (!xm_binutils_ar_read_symbols(istream, 0, lua)) {
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "readsyms: read AR archive symbols failed");
lua_pushfstring(lua, "read AR archive symbols failed");
break;
}
}
Expand Down Expand Up @@ -140,6 +141,8 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) {
read_ok = xm_binutils_elf_read_symbols(istream, 0, lua);
} else if (format == XM_BINUTILS_FORMAT_MACHO) {
read_ok = xm_binutils_macho_read_symbols(istream, 0, lua);
} else if (format == XM_BINUTILS_FORMAT_WASM) {
read_ok = xm_binutils_wasm_read_symbols(istream, 0, lua);
}

if (read_ok) {
Expand All @@ -148,7 +151,7 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) {
} else {
lua_pop(lua, 2); // pop entry table and result list
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "readsyms: read symbols failed");
lua_pushfstring(lua, "read symbols failed");
break;
}
}
Expand Down
Loading
Loading