I am trying add a custom native method(free) to OpenJDK source, so I can call that method as System.free() from inside the user Application.
I referred online resources to do so and did following changes :
{JDK13ROOT}/src/java.base/share/classes/java/lang/Runtime.java
public static native void free();
{JDK13ROOT}/src/java.base/share/classes/java/lang/System.java
public static void free() {
Runtime.getRuntime().free();
}
{JDK13ROOT}/src/java.base/share/native/libjava/Runtime.c
JNIEXPORT void JNICALL
Java_java_lang_Runtime_free(){
printf("Caught !\n");
}
After above changes, I was able to successfully compile the Driver program which directly calls System.free(), and prints Caught ! after execution.
I want to call JVM_Free() inside Java_java_lang_Runtime_free() as below:
JNIEXPORT void JNICALL
Java_java_lang_Runtime_free(){
printf("Caught !\n");
JVM_Free();
}
I have included the declaration in {JDK13ROOT}/src/hotspot/share/include/jvm.h
JNIEXPORT void JNICALL
JVM_Free(void);
And the definition of same in {JDK13ROOT}/src/hotspot/share/prims/jvm.cpp
JVM_ENTRY_NO_ENV(void, JVM_Free(void))
JVMWrapper("JVM_Free");
printf("SUCCESS\n");
JVM_END
I took reference of implementation of GC() and did the same changes., but when I build the program I get the following error.
Updating support/modules_libs/java.base/libjava.so due to 1 file(s)
/usr/bin/ld: {JDK13ROOT}/build/linux-x86_64-server-slowdebug/support/native/java.base/libjava/Runtime.o: in function `Java_java_lang_Runtime_free':
{JDK13ROOT}src/java.base/share/native/libjava/Runtime.c:71: undefined reference to `JVM_Free'
I'm not able to figure out why am I not able to call any functions from Runtime.c.
PS: I also tried adding the native method from System.c instead of Runtime.c and I see the same build failure. What am I missing or doing wrong here ?.
Your symbol (JVM_Free
) is not exported. Take a look here (t vs. T)
> nm libjvm.dylib | grep JVM_GC
000000000041508c T _JVM_GC
vs.
> nm libjvm.dylib | grep JVM_Free
000000000041517a t _JVM_Free
in order to export your new symbol, you have to add it here
{JDK13ROOT}/make/hotspot/symbols/symbols-unix
once it's there, you will be able to "see" it from the outside of libjvm.dylib
(or .so
).