@@ -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"})
104143static 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);
0 commit comments