Skip to content

Commit

Permalink
Fix crash on Linux by DllImporting correct version of libdl (#1764)
Browse files Browse the repository at this point in the history
* DllImport correct version of libdl

* Fix macOS
  • Loading branch information
BigWingBeat authored Sep 24, 2023
1 parent 3c31179 commit 25d6325
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions src/Runtime/SymbolResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,37 @@ 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",
"lib{0}.so",
"lib{0}.dylib",
"{0}.bundle"
};
break;
default:
loadImage = LoadLibrary;
resolveSymbol = GetProcAddress;
formats = new[] { "{0}", "{0}.dll" };
break;
}
}

Expand Down Expand Up @@ -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", 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", 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

Expand Down

0 comments on commit 25d6325

Please sign in to comment.