Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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
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
17 changes: 17 additions & 0 deletions tests/modules/binutils/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ function test_format(t)
io.writefile(ar, "!<arch>\n")
t:are_equal(binutils.format(ar), "ar")

local wasmso = path.join(tempdir, "libfoo.so")
io.writefile(wasmso, string.char(0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00))
t:are_equal(binutils.format(wasmso), "wasm")

local elf = path.join(tempdir, "a.elf")
io.writefile(elf, string.char(0x7f, string.byte("E"), string.byte("L"), string.byte("F"), 0, 0, 0, 0))
t:are_equal(binutils.format(elf), "elf")
Expand Down Expand Up @@ -76,3 +80,16 @@ function test_format(t)

os.tryrm(tempdir)
end

function test_deplibs(t)
local tempdir = "temp/binutils_deplibs"
os.tryrm(tempdir)
os.mkdir(tempdir)

local wasmso = path.join(tempdir, "libfoo.so")
io.writefile(wasmso, string.char(0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00))
local libs = binutils.deplibs(wasmso)
t:are_equal(#libs, 0)

os.tryrm(tempdir)
end
2 changes: 0 additions & 2 deletions xmake/core/sandbox/modules/import/core/base/binutils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ function sandbox_core_base_binutils.rpath_list(binaryfile)
end
end

-- insert rpath to binary file (auto-detect format: ELF or Mach-O)

-- clean rpaths from binary file (auto-detect format: ELF or Mach-O)
function sandbox_core_base_binutils.rpath_clean(binaryfile)
local ok, errors = binutils.rpath_clean(binaryfile)
Expand Down
Loading