Skip to content

Commit

Permalink
improve to detect elf arch
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed May 3, 2020
1 parent 7a85a8f commit 9d783a6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
40 changes: 40 additions & 0 deletions src/lni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,45 @@ static tb_int_t lni_elf_add_libraries(lua_State* lua)
return 1;
}

// elf.detect_arch("libx.so")
static tb_int_t lni_elf_detect_arch(lua_State* lua)
{
try
{
// get arguments
tb_char_t const* inputfile = luaL_checkstring(lua, 1);
if (!inputfile) throw "invalid arguments!";

// get arch
auto elf_binary = std::unique_ptr<LIEF::ELF::Binary>{LIEF::ELF::Parser::parse(inputfile)};
switch (elf_binary->header().machine_type())
{
case LIEF::ELF::ARCH::EM_AARCH64:
lua_pushliteral(lua, "arm64-v8a");
break;
case LIEF::ELF::ARCH::EM_ARM:
lua_pushliteral(lua, "armeabi-v7a");
break;
case LIEF::ELF::ARCH::EM_X86_64:
lua_pushliteral(lua, "x86_64");
break;
case LIEF::ELF::ARCH::EM_386:
lua_pushliteral(lua, "x86");
break;
default:
lua_pushliteral(lua, "armeabi");
break;
}
}
catch (std::exception const& e)
{
lua_pushnil(lua);
lua_pushstring(lua, e.what());
return 2;
}
return 1;
}

// macho.add_libraries("libx.so", {"liba.dylib", "libb.dylib"})
static tb_int_t lni_macho_add_libraries(lua_State* lua)
{
Expand Down Expand Up @@ -154,6 +193,7 @@ static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua)
static luaL_Reg const lni_elf_funcs[] =
{
{"add_libraries", lni_elf_add_libraries}
, {"detect_arch", lni_elf_detect_arch}
, {tb_null, tb_null}
};
xm_engine_register(engine, "elf", lni_elf_funcs);
Expand Down
19 changes: 12 additions & 7 deletions src/lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,20 @@ function _inject_apk(inputfile, outputfile, libraries)
raise("extract failed!")
end

-- detect architecture
local arch
for _, library in ipairs(libraries) do
arch = os.isfile(library) and elf.detect_arch(library)
if arch then
break
end
end
arch = arch or "armeabi-v7a"
print("%s found!", arch)

-- remove META-INF
os.tryrm(path.join(tmpdir, "META-INF"))

-- get arch and library directory
local arch = "armeabi-v7a"
local result = try {function () return os.iorunv("file", {inputfile}) end}
if result and result:find("aarch64", 1, true) then
arch = "arm64-v8a"
end
local libdir = path.join(tmpdir, "lib", arch)
if not os.isdir(libdir) then
arch = "armeabi"
Expand All @@ -177,7 +182,7 @@ function _inject_apk(inputfile, outputfile, libraries)
table.insert(libnames, path.filename(library))
end
for _, libfile in ipairs(os.files(path.join(libdir, (option.get("pattern") or "*") .. ".so"))) do
print("inject to %s", path.filename(libfile))
print("inject to %s/%s", path.filename(path.directory(libfile)), path.filename(libfile))
elf.add_libraries(libfile, libfile, libnames)
end

Expand Down

0 comments on commit 9d783a6

Please sign in to comment.