From 9d783a6df723fbbe221a5b77412a3418fdffbaeb Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 3 May 2020 20:42:29 +0800 Subject: [PATCH] improve to detect elf arch --- src/lni/main.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/lua/main.lua | 19 ++++++++++++------- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/lni/main.cpp b/src/lni/main.cpp index df07a53..7417236 100644 --- a/src/lni/main.cpp +++ b/src/lni/main.cpp @@ -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::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) { @@ -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); diff --git a/src/lua/main.lua b/src/lua/main.lua index 020b751..ecfa0f1 100644 --- a/src/lua/main.lua +++ b/src/lua/main.lua @@ -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" @@ -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