diff options
author | Sven Gothel <[email protected]> | 2023-11-26 09:49:12 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-11-26 09:49:12 +0100 |
commit | c7efca6d9b0db7305f5352ebf15d915ae5a1fa24 (patch) | |
tree | 7ba72733c9296aea5ea339b3fd81d264b49d3e8c /src/java/jogamp/common/os | |
parent | aea14464d521dca28165498ffe943ef1122fc2e3 (diff) |
Bug 1479: NativeLibrary: Add getNativeLibraryPath() returning queried used native library path, supported throughout DynamicLibraryBundle[Info]
Motivation: It is helpful to retrieve the actually used native library pathname,
since loading a library w/o absolute path but lookup through LD_LIBRARY_PATH
may render it hard for the user to determine which library is used.
+++
+++
Windows implementation simply can use GetModuleFileNameA() with the native library handle.
POSIX implementation may utilize a symbol-name to retrieve its address within the
loading native library used to retrieved the library information
via dladdr().
To support this feature throughout DynamicLibraryBundle and DynamicLibraryBundleInfo,
the custom DynamicLibraryBundleInfo specializations shall provide
optional symbol-names per each tool-library-name for the POSIX implementation,
see above.
public interface DynamicLibraryBundleInfo {
...
/**
* Returns optional list of optional symbol names per {@link #getToolLibNames()}
* in same order for an OS which requires the symbol's address to retrieve
* the path of the containing library.
*/
public List<String> getSymbolForToolLibPath();
...
}
Diffstat (limited to 'src/java/jogamp/common/os')
-rw-r--r-- | src/java/jogamp/common/os/DynamicLinkerImpl.java | 13 | ||||
-rw-r--r-- | src/java/jogamp/common/os/UnixDynamicLinkerImpl.java | 14 | ||||
-rw-r--r-- | src/java/jogamp/common/os/WindowsDynamicLinkerImpl.java | 9 |
3 files changed, 36 insertions, 0 deletions
diff --git a/src/java/jogamp/common/os/DynamicLinkerImpl.java b/src/java/jogamp/common/os/DynamicLinkerImpl.java index 5ce94c1..13b08d7 100644 --- a/src/java/jogamp/common/os/DynamicLinkerImpl.java +++ b/src/java/jogamp/common/os/DynamicLinkerImpl.java @@ -45,6 +45,7 @@ import com.jogamp.common.util.SecurityUtil; /** * @throws SecurityException if user is not granted global access */ + @Override public final void claimAllLinkPermission() throws SecurityException { synchronized( secSync ) { allLinkPermissionGranted = true; @@ -54,6 +55,7 @@ import com.jogamp.common.util.SecurityUtil; /** * @throws SecurityException if user is not granted global access */ + @Override public final void releaseAllLinkPermission() throws SecurityException { synchronized( secSync ) { allLinkPermissionGranted = false; @@ -120,6 +122,17 @@ import com.jogamp.common.util.SecurityUtil; protected abstract long openLibraryLocalImpl(final String pathname) throws SecurityException; @Override + public final String lookupLibraryPathname(final long libraryHandle, final String symbolName) throws SecurityException { + checkLinkPermission(libraryHandle); + final String fname = lookupLibraryPathnameImpl(libraryHandle, symbolName); + if(DEBUG_LOOKUP) { + System.err.println("DynamicLinkerImpl.lookupLibraryPathname(0x"+Long.toHexString(libraryHandle)+", "+symbolName+") -> '"+fname+"'"); + } + return fname; + } + protected abstract String lookupLibraryPathnameImpl(final long libraryHandle, String symbolName) throws SecurityException; + + @Override public final long lookupSymbolGlobal(final String symbolName) throws SecurityException { checkAllLinkPermission(); final long addr = lookupSymbolGlobalImpl(symbolName); diff --git a/src/java/jogamp/common/os/UnixDynamicLinkerImpl.java b/src/java/jogamp/common/os/UnixDynamicLinkerImpl.java index ddaeea2..4a0aeb4 100644 --- a/src/java/jogamp/common/os/UnixDynamicLinkerImpl.java +++ b/src/java/jogamp/common/os/UnixDynamicLinkerImpl.java @@ -47,6 +47,20 @@ package jogamp.common.os; /** Interface to C language function: <br> <code> void * dlsym(void * , const char * ); </code> */ protected static native long dlsym(long arg0, java.lang.String arg1); + /** Interface to C language function: <br> <code> int dladdr(void * , Dl_info *); </code>, returning the <code>Dl_info.dli_fname</code> */ + protected static native java.lang.String dladdr_fname(long arg0); + + @Override + protected final String lookupLibraryPathnameImpl(final long libraryHandle, final String symbolName) throws SecurityException { + if( 0 != libraryHandle && null != symbolName && symbolName.length() > 0 ) { + final long addr = dlsym(libraryHandle, symbolName); + if( 0 != addr ) { + return dladdr_fname(addr); + } + } + return null; + } + @Override protected final long lookupSymbolLocalImpl(final long libraryHandle, final String symbolName) throws SecurityException { return 0 != libraryHandle ? dlsym(libraryHandle, symbolName) : 0; diff --git a/src/java/jogamp/common/os/WindowsDynamicLinkerImpl.java b/src/java/jogamp/common/os/WindowsDynamicLinkerImpl.java index 04f13fb..a99cb35 100644 --- a/src/java/jogamp/common/os/WindowsDynamicLinkerImpl.java +++ b/src/java/jogamp/common/os/WindowsDynamicLinkerImpl.java @@ -41,6 +41,9 @@ public final class WindowsDynamicLinkerImpl extends DynamicLinkerImpl { /** Interface to C language function: <br> <code> HANDLE LoadLibraryW(LPCWSTR lpLibFileName); </code> */ private static native long LoadLibraryW(java.lang.String lpLibFileName); + /** Interface to C language function: <br> <code> PROC GetModuleFileNameA(HANDLE hModule, LPSTR lpFilename, DWORD nSize); </code> */ + private static native java.lang.String GetModuleFileNameA(long hModule); + @Override protected final long openLibraryLocalImpl(final String libraryName) throws SecurityException { // How does that work under Windows ? @@ -54,6 +57,12 @@ public final class WindowsDynamicLinkerImpl extends DynamicLinkerImpl { } @Override + protected final String lookupLibraryPathnameImpl(final long libraryHandle, final String symbolName) throws SecurityException { + // symbolName is not required + return 0 != libraryHandle ? GetModuleFileNameA(libraryHandle) : null; + } + + @Override protected final long lookupSymbolGlobalImpl(final String symbolName) throws SecurityException { if(DEBUG_LOOKUP) { System.err.println("lookupSymbolGlobal: Not supported on Windows"); |