Skip to content

Instantly share code, notes, and snippets.

@bric3
Created September 25, 2021 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bric3/23e63345de7f8b995cdbd8cb1280c7fb to your computer and use it in GitHub Desktop.
Save bric3/23e63345de7f8b995cdbd8cb1280c7fb to your computer and use it in GitHub Desktop.
Issue with swift and symbol lookup
// 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
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