You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Linux] DllImport should invoke with RTLD_GLOBAL flag #11901
The CoreCLR behavior seems to be behaving as if library is loaded as RTLD_LOCAL despite the fact that the actual code resolves them with RTLD_LAZY flag when using dlopen. This causes some difficulty of using CoreCLR for P/Invoke code that since current P/Invoke API currently cannot access global library variables (at least cleanly), we would normally use dlopen/dlsym to access the global library variables. The problem is that it behaves in a way that it seems as if there are 2 instances of the same library. It has persisted for more than 2 years now.
RTLD_GLOBAL will make some libraries work, but it will break others. There is no one right default. We have chosen RTLD_LOCAL to be the default since others (e.g. Python) made the same choice. Also, it matches the behavior on Windows - there is not even concept of global on Windows.
Additionally, when using LoadLibraryEx, it is possible to explicitly load multiple instances of a given module by specifying it is a datafile or image resource. In this scenario, it does not call DllMain and you must do that (and similar setup) yourself (this is similar to the LOCAL loading state in dlopen).
@tannergooding This has not much to do with refcounting or loading multiple instance of the same .dlls.
On Unix, there is basically a process global hashtable of all exported symbols. When a shared library is loaded using RTLD_GLOBAL, its symbols are added to this hashtable and other shared libraries can resolve them, without specifying where the symbol should come from in its import tables. There is no such a thing on Windows.
Do you happen to have a sample how one can use NativeLibrary.SetDllImportResolver to load with RTLD_GLOBAL ?
Here's a sample:
usingSystem.Runtime.InteropServices;voidTest(){DllImportResolverresolver=(stringlibraryName,Assemblyasm,DllImportSearchPath?dllImportSearchPath)=>Interop.Libdl.dlopen(libraryName,RTLD_GLOBAL);NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly();,resolver);// .. Then try the pInvoke ... }
Activity
jkotas commentedon Jan 26, 2019
This issue was discussed in dotnet/coreclr#18628 .
RTLD_GLOBALwill make some libraries work, but it will break others. There is no one right default. We have chosenRTLD_LOCALto be the default since others (e.g. Python) made the same choice. Also, it matches the behavior on Windows - there is not even concept of global on Windows.We have introduced a callback in .NET Core 3.0 that allows customizing how the libraries are loaded for DllImport:
NativeLibrary.SetDllImportResolver. More details are in https://github.com/dotnet/corefx/issues/32015.@swaroop-sridhar Do you happen to have a sample how one can use
NativeLibrary.SetDllImportResolverto load withRTLD_GLOBAL?tannergooding commentedon Jan 26, 2019
@jkotas, this is not the case.
LoadLibrary(andLoadLibraryEx, by extension) ref counts modules on a per-process basis and Windows (see https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-loadlibraryw#remarks for more details), using the same code will return (as it does on Mono fordlopen):Here is an end-to-end sample showing the behavior on Windows: repro.zip
tannergooding commentedon Jan 26, 2019
Additionally, when using
LoadLibraryEx, it is possible to explicitly load multiple instances of a given module by specifying it is a datafile or image resource. In this scenario, it does not call DllMain and you must do that (and similar setup) yourself (this is similar to the LOCAL loading state in dlopen).jkotas commentedon Jan 26, 2019
@tannergooding This has not much to do with refcounting or loading multiple instance of the same .dlls.
On Unix, there is basically a process global hashtable of all exported symbols. When a shared library is loaded using
RTLD_GLOBAL, its symbols are added to this hashtable and other shared libraries can resolve them, without specifying where the symbol should come from in its import tables. There is no such a thing on Windows.swaroop-sridhar commentedon Jan 28, 2019
Here's a sample: