Android系统加载JNI Lib的方法有如下两种:
1) 通过JNI_OnLoad
2) 如果JNI Lib没有定义JNI_OnLoad,则dvm调用dvmResolveNativeMethod进行动态解析
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中。
[cpp] view plain copy 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获取。
如果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
转载于:https://www.cnblogs.com/samyboy/p/5604245.html