|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | + |
| 5 | +namespace Python.Runtime.Platform |
| 6 | +{ |
| 7 | + interface ILibraryLoader |
| 8 | + { |
| 9 | + IntPtr Load(string dllToLoad); |
| 10 | + |
| 11 | + IntPtr GetFunction(IntPtr hModule, string procedureName); |
| 12 | + |
| 13 | + void Free(IntPtr hModule); |
| 14 | + } |
| 15 | + |
| 16 | + static class LibraryLoader |
| 17 | + { |
| 18 | + public static ILibraryLoader Get(OperatingSystemType os) |
| 19 | + { |
| 20 | + switch (os) |
| 21 | + { |
| 22 | + case OperatingSystemType.Windows: |
| 23 | + return new WindowsLoader(); |
| 24 | + case OperatingSystemType.Darwin: |
| 25 | + return new DarwinLoader(); |
| 26 | + case OperatingSystemType.Linux: |
| 27 | + return new LinuxLoader(); |
| 28 | + default: |
| 29 | + throw new PlatformNotSupportedException($"This operating system ({os}) is not supported"); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + class LinuxLoader : ILibraryLoader |
| 35 | + { |
| 36 | + private static int RTLD_NOW = 0x2; |
| 37 | + private static int RTLD_GLOBAL = 0x100; |
| 38 | + private static IntPtr RTLD_DEFAULT = IntPtr.Zero; |
| 39 | + private const string NativeDll = "libdl.so"; |
| 40 | + |
| 41 | + public IntPtr Load(string dllToLoad) |
| 42 | + { |
| 43 | + var filename = $"lib{dllToLoad}.so"; |
| 44 | + ClearError(); |
| 45 | + var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL); |
| 46 | + if (res == IntPtr.Zero) |
| 47 | + { |
| 48 | + var err = GetError(); |
| 49 | + throw new DllNotFoundException($"Could not load {filename} with flags RTLD_NOW | RTLD_GLOBAL: {err}"); |
| 50 | + } |
| 51 | + |
| 52 | + return res; |
| 53 | + } |
| 54 | + |
| 55 | + public void Free(IntPtr handle) |
| 56 | + { |
| 57 | + dlclose(handle); |
| 58 | + } |
| 59 | + |
| 60 | + public IntPtr GetFunction(IntPtr dllHandle, string name) |
| 61 | + { |
| 62 | + // look in the exe if dllHandle is NULL |
| 63 | + if (dllHandle == IntPtr.Zero) |
| 64 | + { |
| 65 | + dllHandle = RTLD_DEFAULT; |
| 66 | + } |
| 67 | + |
| 68 | + ClearError(); |
| 69 | + IntPtr res = dlsym(dllHandle, name); |
| 70 | + if (res == IntPtr.Zero) |
| 71 | + { |
| 72 | + var err = GetError(); |
| 73 | + throw new MissingMethodException($"Failed to load symbol {name}: {err}"); |
| 74 | + } |
| 75 | + return res; |
| 76 | + } |
| 77 | + |
| 78 | + void ClearError() |
| 79 | + { |
| 80 | + dlerror(); |
| 81 | + } |
| 82 | + |
| 83 | + string GetError() |
| 84 | + { |
| 85 | + var res = dlerror(); |
| 86 | + if (res != IntPtr.Zero) |
| 87 | + return Marshal.PtrToStringAnsi(res); |
| 88 | + else |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] |
| 93 | + public static extern IntPtr dlopen(string fileName, int flags); |
| 94 | + |
| 95 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] |
| 96 | + private static extern IntPtr dlsym(IntPtr handle, string symbol); |
| 97 | + |
| 98 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] |
| 99 | + private static extern int dlclose(IntPtr handle); |
| 100 | + |
| 101 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] |
| 102 | + private static extern IntPtr dlerror(); |
| 103 | + } |
| 104 | + |
| 105 | + class DarwinLoader : ILibraryLoader |
| 106 | + { |
| 107 | + private static int RTLD_NOW = 0x2; |
| 108 | + private static int RTLD_GLOBAL = 0x8; |
| 109 | + private const string NativeDll = "/usr/lib/libSystem.dylib"; |
| 110 | + private static IntPtr RTLD_DEFAULT = new IntPtr(-2); |
| 111 | + |
| 112 | + public IntPtr Load(string dllToLoad) |
| 113 | + { |
| 114 | + var filename = $"lib{dllToLoad}.dylib"; |
| 115 | + ClearError(); |
| 116 | + var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL); |
| 117 | + if (res == IntPtr.Zero) |
| 118 | + { |
| 119 | + var err = GetError(); |
| 120 | + throw new DllNotFoundException($"Could not load {filename} with flags RTLD_NOW | RTLD_GLOBAL: {err}"); |
| 121 | + } |
| 122 | + |
| 123 | + return res; |
| 124 | + } |
| 125 | + |
| 126 | + public void Free(IntPtr handle) |
| 127 | + { |
| 128 | + dlclose(handle); |
| 129 | + } |
| 130 | + |
| 131 | + public IntPtr GetFunction(IntPtr dllHandle, string name) |
| 132 | + { |
| 133 | + // look in the exe if dllHandle is NULL |
| 134 | + if (dllHandle == IntPtr.Zero) |
| 135 | + { |
| 136 | + dllHandle = RTLD_DEFAULT; |
| 137 | + } |
| 138 | + |
| 139 | + ClearError(); |
| 140 | + IntPtr res = dlsym(dllHandle, name); |
| 141 | + if (res == IntPtr.Zero) |
| 142 | + { |
| 143 | + var err = GetError(); |
| 144 | + throw new MissingMethodException($"Failed to load symbol {name}: {err}"); |
| 145 | + } |
| 146 | + return res; |
| 147 | + } |
| 148 | + |
| 149 | + void ClearError() |
| 150 | + { |
| 151 | + dlerror(); |
| 152 | + } |
| 153 | + |
| 154 | + string GetError() |
| 155 | + { |
| 156 | + var res = dlerror(); |
| 157 | + if (res != IntPtr.Zero) |
| 158 | + return Marshal.PtrToStringAnsi(res); |
| 159 | + else |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] |
| 164 | + public static extern IntPtr dlopen(String fileName, int flags); |
| 165 | + |
| 166 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] |
| 167 | + private static extern IntPtr dlsym(IntPtr handle, String symbol); |
| 168 | + |
| 169 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] |
| 170 | + private static extern int dlclose(IntPtr handle); |
| 171 | + |
| 172 | + [DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] |
| 173 | + private static extern IntPtr dlerror(); |
| 174 | + } |
| 175 | + |
| 176 | + class WindowsLoader : ILibraryLoader |
| 177 | + { |
| 178 | + private const string NativeDll = "kernel32.dll"; |
| 179 | + |
| 180 | + |
| 181 | + public IntPtr Load(string dllToLoad) |
| 182 | + { |
| 183 | + var res = WindowsLoader.LoadLibrary(dllToLoad); |
| 184 | + if (res == IntPtr.Zero) |
| 185 | + throw new DllNotFoundException($"Could not load {dllToLoad}", new Win32Exception()); |
| 186 | + return res; |
| 187 | + } |
| 188 | + |
| 189 | + public IntPtr GetFunction(IntPtr hModule, string procedureName) |
| 190 | + { |
| 191 | + var res = WindowsLoader.GetProcAddress(hModule, procedureName); |
| 192 | + if (res == IntPtr.Zero) |
| 193 | + throw new MissingMethodException($"Failed to load symbol {procedureName}", new Win32Exception()); |
| 194 | + return res; |
| 195 | + } |
| 196 | + |
| 197 | + public void Free(IntPtr hModule) => WindowsLoader.FreeLibrary(hModule); |
| 198 | + |
| 199 | + [DllImport(NativeDll, SetLastError = true)] |
| 200 | + static extern IntPtr LoadLibrary(string dllToLoad); |
| 201 | + |
| 202 | + [DllImport(NativeDll, SetLastError = true)] |
| 203 | + static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); |
| 204 | + |
| 205 | + [DllImport(NativeDll)] |
| 206 | + static extern bool FreeLibrary(IntPtr hModule); |
| 207 | + } |
| 208 | +} |
0 commit comments