-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathLibDL.cs
More file actions
109 lines (86 loc) · 4.1 KB
/
LibDL.cs
File metadata and controls
109 lines (86 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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();
}
}