Skip to content

Commit 25d6325

Browse files
authored
Fix crash on Linux by DllImporting correct version of libdl (#1764)
* DllImport correct version of libdl * Fix macOS
1 parent 3c31179 commit 25d6325

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/Runtime/SymbolResolver.cs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,37 @@ public static class SymbolResolver
3535

3636
static SymbolResolver()
3737
{
38-
switch (Environment.OSVersion.Platform)
38+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
3939
{
40-
case PlatformID.Unix:
41-
case PlatformID.MacOSX:
42-
loadImage = dlopen;
43-
resolveSymbol = dlsym;
44-
formats = new[] {
40+
loadImage = LoadLibrary;
41+
resolveSymbol = GetProcAddress;
42+
formats = new[] { "{0}", "{0}.dll" };
43+
}
44+
else
45+
{
46+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
47+
{
48+
loadImage = dlopen_linux;
49+
resolveSymbol = dlsym_linux;
50+
}
51+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
52+
{
53+
loadImage = dlopen_macos;
54+
resolveSymbol = dlsym_macos;
55+
}
56+
else
57+
{
58+
throw new NotImplementedException();
59+
}
60+
61+
formats = new[] {
4562
"{0}",
4663
"{0}.so",
4764
"{0}.dylib",
4865
"lib{0}.so",
4966
"lib{0}.dylib",
5067
"{0}.bundle"
5168
};
52-
break;
53-
default:
54-
loadImage = LoadLibrary;
55-
resolveSymbol = GetProcAddress;
56-
formats = new[] { "{0}", "{0}.dll" };
57-
break;
5869
}
5970
}
6071

@@ -113,16 +124,35 @@ public static IntPtr ResolveSymbol(IntPtr image, string symbol)
113124

114125
private const int RTLD_LAZY = 0x1;
115126

116-
static IntPtr dlopen(string path)
127+
#region LINUX
128+
129+
static IntPtr dlopen_linux(string path)
117130
{
118-
return dlopen(path, RTLD_LAZY);
131+
return dlopen_linux(path, RTLD_LAZY);
119132
}
120133

121-
[DllImport("dl", CharSet = CharSet.Ansi)]
122-
static extern IntPtr dlopen(string path, int flags);
134+
[DllImport("dl.so.2", EntryPoint = "dlopen", CharSet = CharSet.Ansi)]
135+
static extern IntPtr dlopen_linux(string path, int flags);
123136

124-
[DllImport("dl", CharSet = CharSet.Ansi)]
125-
static extern IntPtr dlsym(IntPtr handle, string symbol);
137+
[DllImport("dl.so.2", EntryPoint = "dlsym", CharSet = CharSet.Ansi)]
138+
static extern IntPtr dlsym_linux(IntPtr handle, string symbol);
139+
140+
#endregion
141+
142+
#region MACOS
143+
144+
static IntPtr dlopen_macos(string path)
145+
{
146+
return dlopen_macos(path, RTLD_LAZY);
147+
}
148+
149+
[DllImport("dl", EntryPoint = "dlopen", CharSet = CharSet.Ansi)]
150+
static extern IntPtr dlopen_macos(string path, int flags);
151+
152+
[DllImport("dl", EntryPoint = "dlsym", CharSet = CharSet.Ansi)]
153+
static extern IntPtr dlsym_macos(IntPtr handle, string symbol);
154+
155+
#endregion
126156

127157
#endregion
128158

0 commit comments

Comments
 (0)