From 1d9cb7358b7f51c247a66b73920572d9f7db5257 Mon Sep 17 00:00:00 2001 From: Pixelstorm Date: Sun, 24 Sep 2023 13:33:39 +0100 Subject: [PATCH 1/2] DllImport correct version of libdl --- src/Runtime/SymbolResolver.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Runtime/SymbolResolver.cs b/src/Runtime/SymbolResolver.cs index 19e1645bc..d11e1ab69 100644 --- a/src/Runtime/SymbolResolver.cs +++ b/src/Runtime/SymbolResolver.cs @@ -118,10 +118,10 @@ static IntPtr dlopen(string path) return dlopen(path, RTLD_LAZY); } - [DllImport("dl", CharSet = CharSet.Ansi)] + [DllImport("dl.so.2", CharSet = CharSet.Ansi)] static extern IntPtr dlopen(string path, int flags); - [DllImport("dl", CharSet = CharSet.Ansi)] + [DllImport("dl.so.2", CharSet = CharSet.Ansi)] static extern IntPtr dlsym(IntPtr handle, string symbol); #endregion From a838410e015ac4276592364a76a3425481363c58 Mon Sep 17 00:00:00 2001 From: Pixelstorm Date: Sun, 24 Sep 2023 14:57:08 +0100 Subject: [PATCH 2/2] Fix macOS --- src/Runtime/SymbolResolver.cs | 66 +++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/src/Runtime/SymbolResolver.cs b/src/Runtime/SymbolResolver.cs index d11e1ab69..68fc2b049 100644 --- a/src/Runtime/SymbolResolver.cs +++ b/src/Runtime/SymbolResolver.cs @@ -35,13 +35,30 @@ public static class SymbolResolver static SymbolResolver() { - switch (Environment.OSVersion.Platform) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - case PlatformID.Unix: - case PlatformID.MacOSX: - loadImage = dlopen; - resolveSymbol = dlsym; - formats = new[] { + loadImage = LoadLibrary; + resolveSymbol = GetProcAddress; + formats = new[] { "{0}", "{0}.dll" }; + } + else + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + loadImage = dlopen_linux; + resolveSymbol = dlsym_linux; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + loadImage = dlopen_macos; + resolveSymbol = dlsym_macos; + } + else + { + throw new NotImplementedException(); + } + + formats = new[] { "{0}", "{0}.so", "{0}.dylib", @@ -49,12 +66,6 @@ static SymbolResolver() "lib{0}.dylib", "{0}.bundle" }; - break; - default: - loadImage = LoadLibrary; - resolveSymbol = GetProcAddress; - formats = new[] { "{0}", "{0}.dll" }; - break; } } @@ -113,16 +124,35 @@ public static IntPtr ResolveSymbol(IntPtr image, string symbol) private const int RTLD_LAZY = 0x1; - static IntPtr dlopen(string path) + #region LINUX + + static IntPtr dlopen_linux(string path) { - return dlopen(path, RTLD_LAZY); + return dlopen_linux(path, RTLD_LAZY); } - [DllImport("dl.so.2", CharSet = CharSet.Ansi)] - static extern IntPtr dlopen(string path, int flags); + [DllImport("dl.so.2", EntryPoint = "dlopen", CharSet = CharSet.Ansi)] + static extern IntPtr dlopen_linux(string path, int flags); - [DllImport("dl.so.2", CharSet = CharSet.Ansi)] - static extern IntPtr dlsym(IntPtr handle, string symbol); + [DllImport("dl.so.2", EntryPoint = "dlsym", CharSet = CharSet.Ansi)] + static extern IntPtr dlsym_linux(IntPtr handle, string symbol); + + #endregion + + #region MACOS + + static IntPtr dlopen_macos(string path) + { + return dlopen_macos(path, RTLD_LAZY); + } + + [DllImport("dl", EntryPoint = "dlopen", CharSet = CharSet.Ansi)] + static extern IntPtr dlopen_macos(string path, int flags); + + [DllImport("dl", EntryPoint = "dlsym", CharSet = CharSet.Ansi)] + static extern IntPtr dlsym_macos(IntPtr handle, string symbol); + + #endregion #endregion