Let's say I loaded a native library gg3 that has a function get_gg(), and would like to load another native library gg4 that also has a get_gg() function - how can I look one up in a "library aware" manner (for example, use the get_gg() from gg4 on certain occasions, and otherwise from gg3)?
I tried CLinker.systemCLinker().lookup() but that seems to just fetch the first symbol that is found in the loaded native libraries.
After searching in the internal classes, I found SystemLookup.libLookup() which seemed promising but isn't public, so I implemented a similar method to lookup a loaded native lib:
private static SymbolLookup libLookup(Function<NativeLibraries, NativeLibrary> loader) {
NativeLibrary lib = loader.apply(NativeLibraries.rawNativeLibraries(SystemLookup.class, false));
return name -> {
Objects.requireNonNull(name);
try {
long addr = lib.lookup(name);
return addr == 0 ?
Optional.empty() :
Optional.of(NativeSymbol.ofAddress(name, MemoryAddress.ofLong(addr), ResourceScope.globalScope()));
} catch (NoSuchMethodException e) {
return Optional.empty();
}
};
}
However, this apparently is prohibited, because I cannot load the library from another class loader (and I don't know what class loader was used originally):
java.lang.UnsatisfiedLinkError: Native Library already loaded in another classloader
So how can I either find & use the appropriate class loader, or manage to find the library without re-loading it, so I can look for a symbol specifically from it?