Created
September 25, 2021 00:04
-
-
Save bric3/23e63345de7f8b995cdbd8cb1280c7fb to your computer and use it in GitHub Desktop.
Issue with swift and symbol lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swiftc lib.swift -emit-library -o libtouchidswift.dylib | |
@_cdecl("function") | |
public func function() { | |
} | |
// $ nm libswift.dylib | |
// 0000000000003fb0 T _$s5swift8functionyyF | |
// 0000000000003fb6 s ___swift_reflection_version | |
// 0000000000003fa0 T _function | |
// U dyld_stub_binder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jdk.incubator.foreign.CLinker; | |
import jdk.incubator.foreign.FunctionDescriptor; | |
import jdk.incubator.foreign.MemoryAddress; | |
import jdk.incubator.foreign.ResourceScope; | |
import java.lang.invoke.MethodHandle; | |
import java.lang.invoke.MethodType; | |
import java.util.HexFormat; | |
public class Main { | |
public static void main(String[] args) throws Throwable { | |
System.load("/Users/bric3/opensource/sandbox/libswift.dylib"); | |
var f = CLinker.getInstance() | |
.downcallHandle( | |
SymbolLookup.loaderLookup().lookup("_function").get(), // <= NPE, symbol not found | |
MethodType.methodType(void.class), | |
FunctionDescriptor.ofVoid() | |
); | |
try (var scope = ResourceScope.newConfinedScope()) { | |
f.invokeExact(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment