1. Android系統載入JNI Lib的方法
Android系統載入JNI Lib的方法有如下兩種:
1) 通過JNI_OnLoad
2) 如果JNI Lib沒有定義JNI_OnLoad,則dvm調用dvmResolveNativeMethod進行動態解析
2. JNI_OnLoad
System.loadLibrary調用流程如下所示:
System.loadLibrary->
Runtime.loadLibrary->(Java)
nativeLoad->(C: java_lang_Runtime.cpp)
Dalvik_java_lang_Runtime_nativeLoad->
dvmLoadNativeCode-> (dalvik/vm/Native.cpp)
1) dlopen(pathName, RTLD_LAZY) (把.so mmap到進程空間,並把func等相關資訊填充到soinfo中)
2) dlsym(handle, "JNI_OnLoad")
3) JNI_OnLoad->
RegisterNatives->
dvmRegisterJNIMethod(ClassObject* clazz, const char* methodName,
const char* signature, void* fnPtr)->
dvmUseJNIBridge(method, fnPtr)-> (method->nativeFunc = func)
JNI函數在進程空間中的起始地址被儲存在ClassObject->directMethods中。
struct ClassObject : Object { /* static, private, and <init> methods */ int directMethodCount; Method* directMethods; /* virtual methods defined in this class; invoked through vtable */ int virtualMethodCount; Method* virtualMethods;}
此ClassObject通過gDvm.jniGlobalRefTable或gDvm.jniWeakGlobalRefLock擷取。
3. dvmResolveNativeMethod延遲解析機制
如果JNI Lib中沒有JNI_OnLoad,即在執行System.loadLibrary時,無法把此JNI Lib實現的函數在進程中的地址增加到ClassObject->directMethods。則直到需要調用的時候才會解析這些javah風格的函數 。這樣的函數dvmResolveNativeMethod(dalvik/vm/Native.cpp)來進行解析,其執行流程如下所示:
void dvmResolveNativeMethod(const u4* args, JValue* pResult,
const Method* method, Thread* self) --> (Resolve a native method and invoke it.)
1) void* func = lookupSharedLibMethod(method)(根據signature在所有已經開啟的.so中尋找此函數實現)
dvmHashForeach(gDvm.nativeLibs, findMethodInLib,(void*) method)->
findMethodInLib(void* vlib, void* vmethod)->
dlsym(pLib->handle, mangleCM)
2) dvmUseJNIBridge((Method*) method, func);
3) (*method->nativeFunc)(args, pResult, method, self); (調用執行)
參考: http://liview.cn/discuz/forum.php?mod=viewthread&tid=5