16

我试图弄清楚 JNI_OnLoad 是如何在内部调用的。我最终想通了下面的教程,但它并没有说明代码部分实际上将 JNI_OnLoad 作为内部函数调用。请帮助我找到显式调用 JNI_OnLoad 的链接函数。我观察到 System.loadLibrary 调用运行时再次调用类加载器。但是还是找不到原生链接。

我对 OnLoad.cpp (android/platform_frameworks_base/blob/master/services/jni/onload.cpp) 中的那个特别感兴趣

JNI_OnLoad

jint JNI_OnLoad(JavaVM *vm, void *reserved);

The VM calls JNI_OnLoad when the native library is loaded (for example, through 
System.loadLibrary). JNI_OnLoad must return the JNI version needed by the native library.
In order to use any of the new JNI functions, a native library must export a JNI_OnLoad function that returns JNI_VERSION_1_2. If the native library does not export a JNI_OnLoad function, the VM assumes that the library only requires JNI version JNI_VERSION_1_1. If the VM does not recognize the version number returned by JNI_OnLoad, the native library cannot be loaded.

编辑:基于@Code Painters 响应的我的文件跟踪如下:

       System.loadLibrary("android_servers");
            |
            |The call System.loadLibrary(name) is effectively equivalent
            |  to the call
            |
            V
        Runtime.getRuntime().loadLibrary(name)
            |
            |public static Runtime getRuntime() {
            |        return currentRuntime;}
            |
            | // Here, also,Classloader.loadlibrary is called, 
            | but this is over-ridden (?)
            | by the Native function of Runtime.java below
            V
        /dalvik/vm/native/java_lang_Runtime.cpp (The jni native
        implementation of Runtime.java):
        /*
         * static String nativeLoad(String filename, ClassLoader loader)
         *
         * Load the specified full path as a dynamic library filled with
         * JNI-compatible methods. Returns null on success, or a failure
         * message on failure.
         */
        static void Dalvik_java_lang_Runtime_nativeLoad{
        //
        success = dvmLoadNativeCode(fileName, classLoader, &reason);
        }

我现在明白Runtime.loadlibrary 被 Dalvik_java_lang_Runtime_nativeLoad 本机函数重载,并且 Classloader.loadlibrary没有被唤起。如果我错了,请纠正我。

4

1 回答 1

24

对于 Android,您应该查看dalvik/vm/Native.c,它定义了 JNI 接口。

最相关的功能是这个:

bool dvmLoadNativeCode(const char* pathName, Object* classLoader);

这是图书馆所在的地方dlopen()。其中最有趣的部分是:

    vonLoad = dlsym(handle, "JNI_OnLoad");
    if (vonLoad == NULL) {
        LOGD("No JNI_OnLoad found in %s %p\n", pathName, classLoader);
    } else {
        /*
         * Call JNI_OnLoad.  We have to override the current class
         * loader, which will always be "null" since the stuff at the
         * top of the stack is around Runtime.loadLibrary().  (See
         * the comments in the JNI FindClass function.)
         */
        OnLoadFunc func = vonLoad;
        Object* prevOverride = self->classLoaderOverride;

        self->classLoaderOverride = classLoader;
        oldStatus = dvmChangeStatus(self, THREAD_NATIVE);
        LOGV("+++ calling JNI_OnLoad(%s)\n", pathName);
        version = (*func)(gDvm.vmList, NULL);
        dvmChangeStatus(self, oldStatus);
        self->classLoaderOverride = prevOverride;

如您所见,简单地使用返回的指针进行JNI_OnLoad解析和调用。dlsym()这个代码部分的其余部分是检查返回的值JNI_OnLoad,没什么令人兴奋的。

I believe it should look pretty much the same for other VMs - so just grep for dlopen() and dlsym() - after all it's just plain shared library loading and symbol resolution.

Edit: Speaking of the exact file you mention, Android.mk in the same directory compiles and links this file into libandroid_servers shared library. Grepping around for this library name reveals services/java/com/android/server/SystemServer.java.

What's relevant:

public static void main(String[] args) {
    // ...
    System.loadLibrary("android_servers");
    // ...
}

So, the loading of library (and thus a call to JNI_OnLoad() in onload.cpp) is performed in the context of Android's system service startup. If you want to know more about how/when the system service loads, I recommend this presentation.

于 2013-02-27T08:36:58.587 回答