Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Try libdl.so.2 on Linux before libdl.so#1425

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
filmor merged 1 commit intopythonnet:masterfromlosttech:bugs/libdl.so.2
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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
109 changes: 109 additions & 0 deletionssrc/runtime/platform/LibDL.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
#pragma warning disable IDE1006 // Naming Styles (interface for native functions)
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime.Platform
{
interface ILibDL
{
IntPtr dlopen(string fileName, int flags);
IntPtr dlsym(IntPtr handle, string symbol);
int dlclose(IntPtr handle);
IntPtr dlerror();

int RTLD_NOW { get; }
int RTLD_GLOBAL { get; }
IntPtr RTLD_DEFAULT { get; }
}

class LinuxLibDL : ILibDL
{
private const string NativeDll = "libdl.so";

public int RTLD_NOW => 0x2;
public int RTLD_GLOBAL => 0x100;
public IntPtr RTLD_DEFAULT => IntPtr.Zero;

public static ILibDL GetInstance()
{
try
{
ILibDL libdl2 = new LinuxLibDL2();
// call dlerror to ensure library is resolved
libdl2.dlerror();
return libdl2;
} catch (DllNotFoundException)
{
return new LinuxLibDL();
}
}

IntPtr ILibDL.dlopen(string fileName, int flags) => dlopen(fileName, flags);
IntPtr ILibDL.dlsym(IntPtr handle, string symbol) => dlsym(handle, symbol);
int ILibDL.dlclose(IntPtr handle) => dlclose(handle);
IntPtr ILibDL.dlerror() => dlerror();

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlopen(string fileName, int flags);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlsym(IntPtr handle, string symbol);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern int dlclose(IntPtr handle);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlerror();
}

class LinuxLibDL2 : ILibDL
{
private const string NativeDll = "libdl.so.2";

public int RTLD_NOW => 0x2;
public int RTLD_GLOBAL => 0x100;
public IntPtr RTLD_DEFAULT => IntPtr.Zero;

IntPtr ILibDL.dlopen(string fileName, int flags) => dlopen(fileName, flags);
IntPtr ILibDL.dlsym(IntPtr handle, string symbol) => dlsym(handle, symbol);
int ILibDL.dlclose(IntPtr handle) => dlclose(handle);
IntPtr ILibDL.dlerror() => dlerror();

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlopen(string fileName, int flags);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlsym(IntPtr handle, string symbol);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern int dlclose(IntPtr handle);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlerror();
}

class MacLibDL : ILibDL
{
public int RTLD_NOW => 0x2;
public int RTLD_GLOBAL => 0x8;
const string NativeDll = "/usr/lib/libSystem.dylib";
public IntPtr RTLD_DEFAULT => new(-2);

IntPtr ILibDL.dlopen(string fileName, int flags) => dlopen(fileName, flags);
IntPtr ILibDL.dlsym(IntPtr handle, string symbol) => dlsym(handle, symbol);
int ILibDL.dlclose(IntPtr handle) => dlclose(handle);
IntPtr ILibDL.dlerror() => dlerror();

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlopen(string fileName, int flags);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlsym(IntPtr handle, string symbol);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern int dlclose(IntPtr handle);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlerror();
}
}
104 changes: 12 additions & 92 deletionssrc/runtime/platform/LibraryLoader.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,9 +28,9 @@ public static ILibraryLoader Instance
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
_instance = new WindowsLoader();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
_instance = newLinuxLoader();
_instance = newPosixLoader(LinuxLibDL.GetInstance());
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
_instance = newDarwinLoader();
_instance = newPosixLoader(new MacLibDL());
else
throw new PlatformNotSupportedException(
"This operating system is not supported"
Expand All@@ -42,87 +42,19 @@ public static ILibraryLoader Instance
}
}

classLinuxLoader : ILibraryLoader
classPosixLoader : ILibraryLoader
{
private static int RTLD_NOW = 0x2;
private static int RTLD_GLOBAL = 0x100;
private static IntPtr RTLD_DEFAULT = IntPtr.Zero;
private const string NativeDll = "libdl.so";
private readonly ILibDL libDL;

public IntPtr Load(string dllToLoad)
{
ClearError();
var res = dlopen(dllToLoad, RTLD_NOW | RTLD_GLOBAL);
if (res == IntPtr.Zero)
{
var err = GetError();
throw new DllNotFoundException($"Could not load {dllToLoad} with flags RTLD_NOW | RTLD_GLOBAL: {err}");
}

return res;
}

public void Free(IntPtr handle)
{
dlclose(handle);
}

public IntPtr GetFunction(IntPtr dllHandle, string name)
{
// look in the exe if dllHandle is NULL
if (dllHandle == IntPtr.Zero)
{
dllHandle = RTLD_DEFAULT;
}

ClearError();
IntPtr res = dlsym(dllHandle, name);
if (res == IntPtr.Zero)
{
var err = GetError();
throw new MissingMethodException($"Failed to load symbol {name}: {err}");
}
return res;
}

void ClearError()
public PosixLoader(ILibDL libDL)
{
dlerror();
}

string GetError()
{
var res = dlerror();
if (res != IntPtr.Zero)
return Marshal.PtrToStringAnsi(res);
else
return null;
this.libDL = libDL ?? throw new ArgumentNullException(nameof(libDL));
}

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr dlopen(string fileName, int flags);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlsym(IntPtr handle, string symbol);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern int dlclose(IntPtr handle);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlerror();
}

class DarwinLoader : ILibraryLoader
{
private static int RTLD_NOW = 0x2;
private static int RTLD_GLOBAL = 0x8;
private const string NativeDll = "/usr/lib/libSystem.dylib";
private static IntPtr RTLD_DEFAULT = new IntPtr(-2);

public IntPtr Load(string dllToLoad)
{
ClearError();
var res = dlopen(dllToLoad, RTLD_NOW | RTLD_GLOBAL);
var res =libDL.dlopen(dllToLoad,libDL.RTLD_NOW |libDL.RTLD_GLOBAL);
if (res == IntPtr.Zero)
{
var err = GetError();
Expand All@@ -134,19 +66,19 @@ public IntPtr Load(string dllToLoad)

public void Free(IntPtr handle)
{
dlclose(handle);
libDL.dlclose(handle);
}

public IntPtr GetFunction(IntPtr dllHandle, string name)
{
// look in the exe if dllHandle is NULL
if (dllHandle == IntPtr.Zero)
{
dllHandle = RTLD_DEFAULT;
dllHandle =libDL.RTLD_DEFAULT;
}

ClearError();
IntPtr res = dlsym(dllHandle, name);
IntPtr res =libDL.dlsym(dllHandle, name);
if (res == IntPtr.Zero)
{
var err = GetError();
Expand All@@ -157,29 +89,17 @@ public IntPtr GetFunction(IntPtr dllHandle, string name)

void ClearError()
{
dlerror();
libDL.dlerror();
}

string GetError()
{
var res = dlerror();
var res =libDL.dlerror();
if (res != IntPtr.Zero)
return Marshal.PtrToStringAnsi(res);
else
return null;
}

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr dlopen(String fileName, int flags);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr dlsym(IntPtr handle, String symbol);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern int dlclose(IntPtr handle);

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlerror();
}

class WindowsLoader : ILibraryLoader
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp