dlload() a library from JDK's lib/ not knowing its full path

Anthony Petrov anthony.petrov at oracle.com
Mon Dec 5 05:23:13 PST 2011


On 12/04/11 21:51, Mike Swingler wrote:
>> I'm currently working on MACOSX_PORT-176 (AWT Splashscreen support), and I need to be able to dlopen() the splash screen dynamic library from the native code. The library normally goes into the lib/ subdirectory of the jdk (or jre).
>>
>> I've grep'ed macosx native code for other dlopen() calls and found out that we either load '0' (i.e. the current process itself), or use full path names to load a couple of common libs (notably, JavaRuntimeSupport and libGL.dylib).
>>
>> Apparently, in the former case we simply assume that all the necessary libs have already been loaded from Java code with a System.loadLibrary() call or an equivalent. In the case of the splash screen we can't load the library from Java because the JVM isn't started yet - the splash screen initialization code is triggered from the Java launcher code.
>>
>> Using dlopen("libsplashscreen.dylib"...) fails since the lib/ directory isn't in the search path for the dlopen() function.
>>
>> Any suggestions on how to load the library? Could I somehow retrieve the executable's path (which must be<real_full_path_to_java>/bin/java or whatever other launcher is used) and calculate the full path for the library off it? Do I use argv[0] for that, or anything else specific to the Java launcher machinery (which I'm unfamiliar with, unfortunately)? Any other options?
>
> Since you can't assume that */bin/java is always going to be the primary executable (in the case of running from another process that started the JVM through JNI), you have you use an existing library you know the path of, and get dladdr()/dlsym() to do the hard work for you.
>
> At the point you are parsing the splashscreen arguments is libjava already loaded (or is this code going into libjava)? You can simply dladdr() a function in libjava (perhaps even the same function you are in), get the full path to libjava from the DL_Info, chop off the last path component and snsprint() append libsplashscreen.dylib to that path.
>
> Does this make sense?

Yep. This is happening in libjli.dylib actually but otherwise this 
approach should work fine. Thanks for the tip!


--
best regards,
Anthony


More information about the macosx-port-dev mailing list