Platform:Android/Native libraries and JNI
How native code in an Android app is built and bound: lib/<abi>/*.so, JNI_OnLoad and RegisterNatives, Java_ symbol mangling, JNI type signatures, the JNIEnv function table, the NDK C++ runtime and the .note.android.ident note.
Where the native code is
An Format:APK carries native libraries as lib/<abi>/lib<name>.so, each an Format:ELF (Executable and Linkable Format) shared object. NDK ABI directory names: armeabi-v7a (32-bit ARM), arm64-v8a (64-bit ARM; the NDK docs table prints it by mistake as aarch64-v8a), x86, x86_64, and since NDK r27 an experimental RISC-V sysroot (riscv64). Older NDKs also had armeabi and mips/mips64 (the NDK r19 changelog already tells of mips64 toolchains being gone; the older ones are not in the source read here). NDK triples: arm-linux-androideabi, aarch64-linux-android, i686-linux-android, x86_64-linux-android. An app can ship several ABIs; the installer picks one directory, so a reverser should analyse the arm64-v8a copy first. Java loads a library with System.loadLibrary("foo") (file libfoo.so) or System.load(path); the loader then calls JNI_OnLoad if the library exports it.
Binding Java methods to native functions
Two mechanisms, often mixed in one library:
- Static binding by name: the VM looks up an exported symbol
Java_<mangled class>_<mangled method>the first time anativemethod is called. Mangling (Oracle JNI specification):.or/in the class name become_; a literal_becomes_1;;becomes_2;[becomes_3; any other non-alphanumeric or non-ASCII UTF-16 unit becomes_0xxxx(lower-case hex). If the method is overloaded a second symbol with__plus the mangled argument signature (without return type) is also searched. Example:com.example.Foo.bar_baz(int)isJava_com_example_Foo_bar_1baz(andJava_com_example_Foo_bar_1baz__Ifor the long form). The first two parameters are alwaysJNIEnv *envandjobject thiz(orjclassfor a static method). Such libraries show readable JNI exports in the dynamic symbol table, so the Java signature can be recovered from the name. - Dynamic binding with RegisterNatives:
JNI_OnLoad(JavaVM *vm, void *reserved)callsvm->GetEnv(&env, JNI_VERSION_1_6),FindClass, thenRegisterNatives(clazz, methods, count)with an array ofJNINativeMethod {const char *name; const char *signature; void *fnPtr;}(see Fact:Android/struct/JNINativeMethod). The functions are not exported; the table sits in.data.rel.ro(relocated pointers, see Format:ELF (Executable and Linkable Format)/Android packed relocations and RELR) and is found by searching for the strings of a JNI signature next to a function pointer. Android's helperjniRegisterNativeMethods(env, className, methods, count)in libnativehelper's JNIHelp.h wraps the FindClass and RegisterNatives pair.JNI_OnLoadmust return the JNI version the library needs (JNI_VERSION_1_20x00010002,_1_40x00010004,_1_60x00010006);JNI_ERR(-1) makes loading fail. Obfuscated libraries often build the name and signature strings at run time, so a table in memory is easier to read than one in the file.
JNI type signatures
A method signature is (<argument types>)<return type> with field descriptors: Z boolean, B byte, C char, S short, I int, J long, F float, D double, V void (return only), L<class/with/slashes>; object, [<type> array. Example: (ILjava/lang/String;)V is void f(int, String); ([B)[B takes and returns byte[]; <init> is the constructor name and <clinit> the static initializer. Strings in the native code use the slash form java/lang/String with FindClass, but the dotted form with Class.forName.
The JNIEnv function table
JNIEnv *env points to a pointer to a table of function pointers: the C call (*env)->FindClass(env, name) loads the table from *env and calls the slot. The table has 233 slots (4 reserved, then GetVersion at index 4 up to GetObjectRefType at 232); on a 64-bit target the byte offset is index x 8, on a 32-bit target index x 4. Frequently seen: FindClass index 6 (0x30 on arm64), GetMethodID 33 (0x108), CallObjectMethod 34 (0x110), NewStringUTF 167 (0x538), GetStringUTFChars 169 (0x548), RegisterNatives 215 (0x6B8). A decompiler that does not know the type shows (*(code **)(*env + 0x30))(env, ...); applying a JNIEnv type (many tools ship jni.h types) turns those into names. The full list is in Fact:Android/enum/JNINativeInterface; the JavaVM table is in Fact:Android/struct/JNIInvokeInterface. Return codes and versions: Fact:Android/enum/JNI return codes and Fact:Android/enum/JNI versions.
C++ runtime inside the library
The NDK's STL is LLVM libc++ (llvm-project/libcxx). It comes as a shared libc++_shared.so (the app must ship it in lib/<abi>/, installed from <NDK>/sysroot/usr/lib/<triple>) or as a static library linked into each .so (clang's -static-libstdc++ selects static; the NDK docs say the shared variant is the default of the NDK's own build systems, with the static one chosen through the Gradle/CMake ANDROID_STL setting). The NDK builds libc++ with the inline ABI namespace std::__ndk1 instead of the upstream std::__1, so mangled names look like _ZNSt6__ndk112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE; see Fact:Android/identification/libc++ NDK namespace. A library with many __ndk1 symbols but no libc++_shared.so in its DT_NEEDED list has libc++ linked statically (its code is then part of the file and worth skipping by signature). Old NDKs (before r18, unverified) offered gnustl (GNU libstdc++, namespace std:: plain and __gnu_cxx) and STLport; apps built with them have libgnustl_shared.so or libstlport_shared.so in lib/<abi>/. Two shared-STL pitfalls from the NDK docs: several libraries in one app must share a single libc++_shared.so, and exceptions cannot cross static-libc++ boundaries.
Build identification: .note.android.ident
Bionic's C runtime objects add a .note.android.ident ELF note (name Android, type 1) whose descriptor starts with the target API level as a 32-bit integer; see Fact:Android/struct/note.android.ident. NDK-built binaries are said to carry more (the NDK version strings and build number) but only the API-level form was read in source here. Unverified: with readelf -n or llvm-readelf -n the note shows as Android / NT_VERSION-like, and the ndk_version and ndk_build_number strings give the exact NDK release that built the library (r25, r26 ...). Related: .note.android.pad_segment (type 5, descriptor 1) marks segment padding for 16 KiB page-size support (NDK r27/r28 changelogs: arm64-v8a devices may use 16 KiB pages, so segments must be 16 KiB aligned).
Reversing checklist
- Unzip the APK, pick
lib/arm64-v8a, listDT_NEEDED(which STL, which Android libs such asliblog.so,libandroid.so). - Look for exported
JNI_OnLoadandJava_*symbols; if there are none, the library registers natives dynamically. - Match the Java side:
nativemethods of the class in Format:DEX and their descriptors against theJNINativeMethodtable. - Apply the
JNIEnvtype to the first argument of every JNI function so table calls get names. - If relocation entries look odd (no
.rela.dynsection but a large dynamic section) it uses packed relocations or RELR.
Sources
- AOSP libnativehelper
include_jni/jni.h(Apache-2.0) at commit aef2939781fc. - AOSP bionic
libc/arch-common/bionic/crtbrand.Sandcrt_pad_segment.Sat 731631f300 (license in file: 3-clause BSD). - android/ndk
docs/BuildSystemMaintainers.mdanddocs/changelogs, llvm-projectlibcxx/CMakeLists.txtat 86483b49182a. - The Oracle JNI specification (documentation) for name mangling and signatures; those two points were written from it, not from the repositories.