20 readonly
static LibIkvm libikvm = LibIkvm.Instance;
33 if (Path.HasExtension(name))
39 return "lib" + name +
".dylib";
41 return "lib" + name +
".so";
50 static NativeLibraryHandle LoadImpl(
string path)
53 var h = libikvm.dl_open(path);
55 System.
Runtime.InteropServices.NativeLibrary.TryLoad(path, typeof(
NativeLibrary).
Assembly, DllImportSearchPath.SafeDirectories | DllImportSearchPath.UseDllDirectoryForDependencies, out nint h);
58 return h != IntPtr.Zero ?
new NativeLibraryHandle(h) : null;
66 public static NativeLibraryHandle
Load(
string nameOrPath)
68 if (Path.IsPathRooted(nameOrPath))
69 return LoadImpl(nameOrPath);
72 if (LoadImpl(nameOrPath) is NativeLibraryHandle h1)
76 var asml = typeof(
NativeLibrary).Assembly.Location is
string s && !
string.IsNullOrEmpty(s) ? Path.GetDirectoryName(s) :
null;
77 var root = Environment.GetEnvironmentVariable(
"IKVM_LIBRARY_PATH") ?? asml;
80 if (
string.IsNullOrEmpty(root))
85 if (File.Exists(lib1) && LoadImpl(lib1) is NativeLibraryHandle h2)
90 if (Path.Combine(root,
"runtimes", rid,
"native",
MapLibraryName(nameOrPath)) is
string lib2)
91 if (File.Exists(lib2) && LoadImpl(lib2) is NativeLibraryHandle h3)
102 internal static void Free(nint handle)
105 libikvm.dl_close(handle);
117 static string MangleWin32ExportName(
string name,
int argl)
119 return argl == -1 ? name : $
"_{name}@{argl}";
128 internal static string MangleExportName(
string name,
int argl)
130 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && RuntimeInformation.ProcessArchitecture == Architecture.X86)
131 return MangleWin32ExportName(name, argl);
143 internal static nint GetExport(nint handle,
string name,
int argl = -1)
145 name = MangleExportName(name, argl);
148 return libikvm.dl_sym(handle, name);
150 return System.
Runtime.InteropServices.NativeLibrary.TryGetExport(handle, name, out var h) ? h : 0;