Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling for libdl.so.2 #19

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions NativeLibraryLoader/Libdl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,62 @@ namespace NativeLibraryLoader
{
internal static class Libdl
{
private const string LibName = "libdl";
private static class Libdl1
{
private const string LibName = "libdl";

[DllImport(LibName)]
public static extern IntPtr dlopen(string fileName, int flags);

[DllImport(LibName)]
public static extern IntPtr dlsym(IntPtr handle, string name);

[DllImport(LibName)]
public static extern int dlclose(IntPtr handle);

[DllImport(LibName)]
public static extern string dlerror();
}

private static class Libdl2
{
private const string LibName = "libdl.so.2";

[DllImport(LibName)]
public static extern IntPtr dlopen(string fileName, int flags);

[DllImport(LibName)]
public static extern IntPtr dlsym(IntPtr handle, string name);

[DllImport(LibName)]
public static extern int dlclose(IntPtr handle);

[DllImport(LibName)]
public static extern string dlerror();
}

static Libdl()
{
try
{
Libdl1.dlerror();
m_useLibdl1 = true;
}
catch
{
}
}

private static bool m_useLibdl1;

public const int RTLD_NOW = 0x002;

[DllImport(LibName)]
public static extern IntPtr dlopen(string fileName, int flags);
public static IntPtr dlopen(string fileName, int flags) => m_useLibdl1 ? Libdl1.dlopen(fileName, flags) : Libdl2.dlopen(fileName, flags);

[DllImport(LibName)]
public static extern IntPtr dlsym(IntPtr handle, string name);
public static IntPtr dlsym(IntPtr handle, string name) => m_useLibdl1 ? Libdl1.dlsym(handle, name) : Libdl2.dlsym(handle, name);

[DllImport(LibName)]
public static extern int dlclose(IntPtr handle);
public static int dlclose(IntPtr handle) => m_useLibdl1 ? Libdl1.dlclose(handle) : Libdl2.dlclose(handle);

[DllImport(LibName)]
public static extern string dlerror();
public static string dlerror() => m_useLibdl1 ? Libdl1.dlerror() : Libdl2.dlerror();
}
}