Skip to content

Commit 9d783a6

Browse files
committed
improve to detect elf arch
1 parent 7a85a8f commit 9d783a6

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/lni/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,45 @@ static tb_int_t lni_elf_add_libraries(lua_State* lua)
100100
return 1;
101101
}
102102

103+
// elf.detect_arch("libx.so")
104+
static tb_int_t lni_elf_detect_arch(lua_State* lua)
105+
{
106+
try
107+
{
108+
// get arguments
109+
tb_char_t const* inputfile = luaL_checkstring(lua, 1);
110+
if (!inputfile) throw "invalid arguments!";
111+
112+
// get arch
113+
auto elf_binary = std::unique_ptr<LIEF::ELF::Binary>{LIEF::ELF::Parser::parse(inputfile)};
114+
switch (elf_binary->header().machine_type())
115+
{
116+
case LIEF::ELF::ARCH::EM_AARCH64:
117+
lua_pushliteral(lua, "arm64-v8a");
118+
break;
119+
case LIEF::ELF::ARCH::EM_ARM:
120+
lua_pushliteral(lua, "armeabi-v7a");
121+
break;
122+
case LIEF::ELF::ARCH::EM_X86_64:
123+
lua_pushliteral(lua, "x86_64");
124+
break;
125+
case LIEF::ELF::ARCH::EM_386:
126+
lua_pushliteral(lua, "x86");
127+
break;
128+
default:
129+
lua_pushliteral(lua, "armeabi");
130+
break;
131+
}
132+
}
133+
catch (std::exception const& e)
134+
{
135+
lua_pushnil(lua);
136+
lua_pushstring(lua, e.what());
137+
return 2;
138+
}
139+
return 1;
140+
}
141+
103142
// macho.add_libraries("libx.so", {"liba.dylib", "libb.dylib"})
104143
static tb_int_t lni_macho_add_libraries(lua_State* lua)
105144
{
@@ -154,6 +193,7 @@ static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua)
154193
static luaL_Reg const lni_elf_funcs[] =
155194
{
156195
{"add_libraries", lni_elf_add_libraries}
196+
, {"detect_arch", lni_elf_detect_arch}
157197
, {tb_null, tb_null}
158198
};
159199
xm_engine_register(engine, "elf", lni_elf_funcs);

src/lua/main.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,20 @@ function _inject_apk(inputfile, outputfile, libraries)
155155
raise("extract failed!")
156156
end
157157

158+
-- detect architecture
159+
local arch
160+
for _, library in ipairs(libraries) do
161+
arch = os.isfile(library) and elf.detect_arch(library)
162+
if arch then
163+
break
164+
end
165+
end
166+
arch = arch or "armeabi-v7a"
167+
print("%s found!", arch)
168+
158169
-- remove META-INF
159170
os.tryrm(path.join(tmpdir, "META-INF"))
160171

161-
-- get arch and library directory
162-
local arch = "armeabi-v7a"
163-
local result = try {function () return os.iorunv("file", {inputfile}) end}
164-
if result and result:find("aarch64", 1, true) then
165-
arch = "arm64-v8a"
166-
end
167172
local libdir = path.join(tmpdir, "lib", arch)
168173
if not os.isdir(libdir) then
169174
arch = "armeabi"
@@ -177,7 +182,7 @@ function _inject_apk(inputfile, outputfile, libraries)
177182
table.insert(libnames, path.filename(library))
178183
end
179184
for _, libfile in ipairs(os.files(path.join(libdir, (option.get("pattern") or "*") .. ".so"))) do
180-
print("inject to %s", path.filename(libfile))
185+
print("inject to %s/%s", path.filename(path.directory(libfile)), path.filename(libfile))
181186
elf.add_libraries(libfile, libfile, libnames)
182187
end
183188

0 commit comments

Comments
 (0)