From 96d027d40b360f115fecbcfcbceac4ee70751de0 Mon Sep 17 00:00:00 2001 From: "Michael C. Fanning" Date: Thu, 22 Jun 2023 14:23:17 -0700 Subject: [PATCH 1/2] Add dlopen command. --- Src/RE2.Managed/NativeMethods.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Src/RE2.Managed/NativeMethods.cs b/Src/RE2.Managed/NativeMethods.cs index fe1de84b..455694da 100644 --- a/Src/RE2.Managed/NativeMethods.cs +++ b/Src/RE2.Managed/NativeMethods.cs @@ -16,10 +16,7 @@ static NativeMethods() string platform = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "linux"; - if (platform == "linux") - { - return; - } + bool isLinux = platform == "linux"; // Strictly speaking we don't need any platform-specific code here. I leave // it just in case we find out that we *do* need to perform the Linux @@ -34,7 +31,7 @@ static NativeMethods() if (File.Exists(filePath)) { - LoadLibrary(filePath); + if (isLinux) { LoadLibrary(filePath); } else { LinuxMethods.dlopen(filePath, LinuxMethods.RTLD_NOW); } } } else if (platform == "win") @@ -45,7 +42,7 @@ static NativeMethods() string dllAdjacent = Path.Combine(driverDirectory, dllName); if (File.Exists(dllAdjacent)) { - LoadLibrary(dllAdjacent); + if (isLinux) { LoadLibrary(dllAdjacent); } else { LinuxMethods.dlopen(dllAdjacent, LinuxMethods.RTLD_NOW); } } // Load if in runtimes subdirectory @@ -53,7 +50,7 @@ static NativeMethods() string dllInRuntime = Path.Combine(driverDirectory, runtimeFolder, dllName); if (File.Exists(dllInRuntime)) { - LoadLibrary(dllInRuntime); + if (isLinux) { LoadLibrary(dllInRuntime); } else { LinuxMethods.dlopen(dllInRuntime, LinuxMethods.RTLD_NOW); } } } } @@ -148,6 +145,12 @@ public static unsafe void MatchesCaptureGroupsDispose(MatchesCaptureGroupsOutput private static class LinuxMethods { + public const int RTLD_NOW = 0x002; + + [SuppressUnmanagedCodeSecurity] + [DllImport("libdl", ExactSpelling = true)] + public static unsafe extern IntPtr dlopen(string fileName, int flags); + [SuppressUnmanagedCodeSecurity] [DllImport("libre2.so", PreserveSig = true, CallingConvention = CallingConvention.Cdecl)] public static unsafe extern int Test(); From b67212b0e72056a63c8518076bec31b3f9c77bc0 Mon Sep 17 00:00:00 2001 From: Shaopeng Li Date: Fri, 23 Jun 2023 09:36:51 -0700 Subject: [PATCH 2/2] Fix Linux loading Windows bin and Windows loading Linux bin --- Src/RE2.Managed/NativeMethods.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Src/RE2.Managed/NativeMethods.cs b/Src/RE2.Managed/NativeMethods.cs index 455694da..31373117 100644 --- a/Src/RE2.Managed/NativeMethods.cs +++ b/Src/RE2.Managed/NativeMethods.cs @@ -16,7 +16,7 @@ static NativeMethods() string platform = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "linux"; - bool isLinux = platform == "linux"; + bool isWindows = platform == "win"; // Strictly speaking we don't need any platform-specific code here. I leave // it just in case we find out that we *do* need to perform the Linux @@ -31,10 +31,10 @@ static NativeMethods() if (File.Exists(filePath)) { - if (isLinux) { LoadLibrary(filePath); } else { LinuxMethods.dlopen(filePath, LinuxMethods.RTLD_NOW); } + if (isWindows) { LoadLibrary(filePath); } else { LinuxMethods.dlopen(filePath, LinuxMethods.RTLD_NOW); } } } - else if (platform == "win") + else { string driverDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); @@ -42,7 +42,7 @@ static NativeMethods() string dllAdjacent = Path.Combine(driverDirectory, dllName); if (File.Exists(dllAdjacent)) { - if (isLinux) { LoadLibrary(dllAdjacent); } else { LinuxMethods.dlopen(dllAdjacent, LinuxMethods.RTLD_NOW); } + if (isWindows) { LoadLibrary(dllAdjacent); } else { LinuxMethods.dlopen(dllAdjacent, LinuxMethods.RTLD_NOW); } } // Load if in runtimes subdirectory @@ -50,7 +50,7 @@ static NativeMethods() string dllInRuntime = Path.Combine(driverDirectory, runtimeFolder, dllName); if (File.Exists(dllInRuntime)) { - if (isLinux) { LoadLibrary(dllInRuntime); } else { LinuxMethods.dlopen(dllInRuntime, LinuxMethods.RTLD_NOW); } + if (isWindows) { LoadLibrary(dllInRuntime); } else { LinuxMethods.dlopen(dllInRuntime, LinuxMethods.RTLD_NOW); } } } }