Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DlOpen implementation using the Foreign Linker API
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.incubator.foreign.CLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.ResourceScope;
import jdk.incubator.foreign.SymbolLookup;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.util.Optional;
class DlOpen {
static final MethodHandle DL_OPEN = CLinker.getInstance().downcallHandle(
CLinker.systemLookup().lookup("dlopen").get(),
MethodType.methodType(MemoryAddress.class, MemoryAddress.class, int.class),
FunctionDescriptor.of(CLinker.C_POINTER, CLinker.C_POINTER, CLinker.C_INT));
static final MethodHandle DL_SYM = CLinker.getInstance().downcallHandle(
CLinker.systemLookup().lookup("dlsym").get(),
MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemoryAddress.class),
FunctionDescriptor.of(CLinker.C_POINTER, CLinker.C_POINTER, CLinker.C_POINTER));
static final MethodHandle DL_CLOSE = CLinker.getInstance().downcallHandle(
CLinker.systemLookup().lookup("dlclose").get(),
MethodType.methodType(int.class, MemoryAddress.class),
FunctionDescriptor.of(CLinker.C_INT, CLinker.C_POINTER));
private static MemoryAddress dlopen(MemoryAddress libName, int dlopenOptions) {
try {
return (MemoryAddress)DL_OPEN.invokeExact(libName, dlopenOptions);
} catch (Throwable ex) {
throw new IllegalStateException();
}
}
private static MemoryAddress dlsym(MemoryAddress handle, MemoryAddress symbolName) {
try {
return (MemoryAddress)DL_SYM.invokeExact(handle, symbolName);
} catch (Throwable ex) {
throw new IllegalStateException();
}
}
private static int dlclose(MemoryAddress handle) {
try {
return (int)DL_CLOSE.invokeExact(handle);
} catch (Throwable ex) {
throw new IllegalStateException();
}
}
public static SymbolLookup lookup(String libraryName, ResourceScope scope) {
try (ResourceScope openScope = ResourceScope.newConfinedScope()) {
final MemoryAddress handle = dlopen(CLinker.toCString(libraryName, openScope).address(), 1);
if (handle == MemoryAddress.NULL) {
throw new IllegalArgumentException("Cannot find library: " + libraryName);
}
scope.addCloseAction(() -> dlclose(handle));
return name -> {
try (ResourceScope lookupScope = ResourceScope.newConfinedScope()) {
MemoryAddress sym = dlsym(handle, CLinker.toCString(name, lookupScope).address());
return sym == MemoryAddress.NULL ?
Optional.empty() : Optional.of(sym);
}
};
}
}
public static void main(String[] args) {
// quick test
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SymbolLookup clib = DlOpen.lookup("libc.so.6", scope);
System.out.println(clib.lookup("qsort"));
} // library unloaded here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment