On Fri, 25 Apr 2025 11:31:50 GMT, Jaikiran Pai <j...@openjdk.org> wrote:
> Hello Jiangli, if I understand this change correctly, then this is forcing a > non-JNI library to be a JNI library for it to work on static JDK. Is that a > requirement for static JDK builds? Or is it just a convenient way to use > existing library loading mechanism in the > `jdk.internal.loader.NativeLibraries`? > Hi @jaikiran Good questions. I kind of expected questions like those might be raised with the addition of `DEF_STATIC_JNI_OnLoad` to `libsyslookup`, when I made the change. The original static built-in JNI library support in JDK was added by JEP 178 (https://openjdk.org/jeps/178) "Statically-Linked JNI Libraries" work. It's on top of the JNI library support. Both spec & implementation expected `JNI_OnLoad_L` (L as the library name) in a statically linked JNI library. From JNI spec (https://docs.oracle.com/en/java/javase/24/docs/specs/jni/invocation.html#jni_onload_l): JNI_OnLoad_L jint JNI_Onload_<L>(JavaVM *vm, void *reserved); Mandatory function that must be defined by statically linked libraries . If a library, named 'L', is statically linked, then upon the first invocation of System.loadLibrary("L") or equivalent API, a JNI_OnLoad_L function will be invoked with the same arguments and expected return value as specified for the JNI_OnLoad function. JNI_OnLoad_L must return the JNI version needed by the native library. This version must be JNI_VERSION_1_8 or later. If the VM does not recognize the version number returned by JNI_OnLoad_L, the VM will act as if the library was never loaded. LINKAGE: Exported from statically linked native libraries that contain native method implementations. PARAMETERS: vm: a pointer to the current VM structure. reserved: unused pointer. RETURNS: Return the required JNI_VERSION constant (see also GetVersion). The minimum version returned being at least JNI_VERSION_1_8. SINCE: JDK/JRE 1.8 JNI_OnUnload_L void JNI_OnUnload_<L>(JavaVM *vm, void *reserved); Optional function defined by statically linked libraries. When the class loader containing a statically linked native library 'L' is garbage collected, the VM will invoke the JNI_OnUnload_L function of the library if such a function is exported. This function can be used to perform cleanup operations. Because this function is called in an unknown context (such as from a finalizer), the programmer should be conservative on using Java VM services, and refrain from arbitrary Java call-backs. LINKAGE: Exported from statically linked native libraries that contain native method implementations. PARAMETERS: vm: a pointer to the current VM structure. reserved: unused pointer. SINCE: JDK/JRE 1.8 Informational Note: The act of loading a native library is the complete process of making the library and its native entry points known and registered to the Java VM and runtime. Note that simply performing operating system level operations to load a native library, such as dlopen on a UNIX(R) system, does not fully accomplish this goal. A native function is normally called from the Java class loader to perform a call to the host operating system that will load the library into memory and return a handle to the native library. This handle will be stored and used in subsequent searches for native library entry points. The Java native class loader will complete the load process once the handle is successfully returned to register the library. You are right that this PR change turns the `libsyslookup` from a "non-JNI" native library to JNI native library. The `DEF_STATIC_JNI_OnLoad` macro adds `JNI_OnLoad_syslookup` in libsyslookup. The `JNI_OnLoad_syslookup` symbol can be used by `Java_jdk_internal_loader_NativeLibraries_findBuiltinLib` (https://github.com/openjdk/jdk/blob/4b880299881c9413038d647123e3b658999c6f8f/src/java.base/share/native/libjava/NativeLibraries.c#L235) to find the statically linked library. > Are there other similar libraries shipped in JAVA_HOME/lib that would require > a similar change/treatment? Yes. Our current static/hermetic work has found any missing `DEF_STATIC_JNI_OnLoad` in the JDK JNI libraries and added them. ------------- PR Comment: https://git.openjdk.org/jdk/pull/24801#issuecomment-2831236496