Dllimport does not find libraries outside of system default (Linux)
@furoraest commented on Sun May 20 2018
when doing PInvoke call, like [DllImport("mylib")] private static extern int some_external_func(); the dotnet does not find library, unless in its some system default (like /usr/lib) If to explicitly state the actual library name like "libmylib.so.0.0.0" it will find it ldconfig is finding the libraries correctly, using ld.so.conf and stating custom library folder. Temporary solution was to add LD_LIBRARY_PATH to the running environment pointing to the library folder, which should not be necessary if the ldconfig lists those libraries already. If already linked library tries to use other library (outside the system default) when the dotnet program will get error: dotnet: symbol lookup error: /usr/local/lib/libmylib.so: undefined symbol: _ZN2gr6some9awesome4functionEmmPcibi Used platform: Ubuntu 18.04 and SDK 2.1 RC1 My current solution is to handload the all required libraries, using this code: [DllImport("dl")] private static extern IntPtr dlopen (string file, int mode); and then in the function somewhere before first call of the PInvoked function, call out the libraries dlopen("libmyawesomelib-x.y.z.0.so.0.0.0", RTLD_LAZY|RTLD_GLOBAL); dlopen("libmyawesomelib2-x.y.z.0.so.0.0.0", RTLD_LAZY|RTLD_GLOBAL);
It looks like the loader is only passing the RTLD_LAZY flag and not the RTLD_GLOBAL. Since RTLD_GLOBAL isn't passed, RTLD_LOCAL is assumed. That is the only thing that is obviously different in this case. I don't fully understand the issue though. Is the problem library dependencies or the requested library itself?
@furoraest I am not sure I fully understand the issue. You are specifying mylib in the dll import, so we try to load mylib, mylib.so and libmylib.so. We would also load the library from the same directory where your app is located, provided it has one of these three names.
@AaronRobinsonMSFT I will check the flags. And sorry about explanation, maybe it should be 2 different issues. The first problem was it didnt find library outside the system "default" paths without giving explicit names, which will breaks your program in future if there are library version updates. Second problem was: if referenced and loaded library tried to use some other library in system I had to manually load it inside the dotnet core program beforehand, otherwise the program crashed. The downside in this design is I have to know all the libraries what are in use by subprograms(and their specific versions if they are outside system defaults) But i check the hints and come back with renewed information.
@furoraest Let me summarize to ensure we are on the same page.
Problem 1
If you have a library called foo.so and that is not on one of the default ld paths or next to the app, then it will fail to load?
~/bin/app.so
~/bin/lib/foo.so
ld path = /usr:/usr/lib
Problem 2
If you have a library called foo.so which has a dependency on bar.so and they are not next to each other and not on the default ld path, but foo.so loads, the loader will fail to find bar.so?
~/bin/app.so
~/bin/lib/foo.so
~/bin/lib/dep/bar.so
ld path = /usr:/usr/lib
I think I get your issue, but I need to be sure so I can try to reproduce it locally. Perhaps if you could provide a description or an example file system layout and the current ld paths would help to illustrate the issue better?
Thanks.
Yes, You got my problem ;) Problem1: I am trying to interface GNURadio C++ libraries, for helping i created my own simple c++ wrapper, Extract from ldconfig -v:
/usr/local/lib:
libgnuradio-wrapper-1.0.0git.so.0.0.0 -> libgnuradio-wrapper.so
testprogram:
using System;
using System.Runtime.InteropServices;
namespace testPInvoke
{
class Program
{
[DllImport("libgnuradio-wrapper")]
private static extern void print_line(string msg);
static void Main(string[] args)
{
print_line("Hello world from c++ wrapper!");
}
}
}
Tesprogram will fail on libgnuradio-wrapper, or libgnuradio-wrapper.so arguments but will not fail on full e.g. libgnuradio-wrapper-1.0.0git.so.0.0.0 argument if to explicity add PATH to /usr/local/lib to the enviroment running the program, the loader loads as expected. e.g
export "LD_LIBRARY_PATH=$PATH:/usr/local/lib"
"fixes" the behaviour I can provide LD_DEBUG output if needed, but this is pretty large.
Problem2: When wrapper calls out other functions, they need to be preloaded inside the dotnet core program with FULL name. Note that the wrapper lib argument is shortened, program is running with
export "LD_LIBRARY_PATH=$PATH:/usr/local/lib"
Extract from ldconfig:
/usr/local/lib:
libgnuradio-wrapper-1.0.0git.so.0.0.0 -> libgnuradio-wrapper.so
libgnuradio-analog-3.7.12.0.so.0.0.0 -> libgnuradio-analog.so
example working program:
using System;
using System.Runtime.InteropServices;
namespace testPInvoke
{
class Program
{
private const int RTLD_LAZY = 0x00001; //Only resolve symbols as needed
private const int RTLD_GLOBAL = 0x00100; //Make symbols available to libraries loaded later
[DllImport("dl")]
private static extern IntPtr dlopen (string file, int mode);
[DllImport("libgnuradio-wrapper")]
private static extern void print_line(string msg);
[DllImport("libgnuradio-wrapper")]
private static extern void test_gr();
static void Main(string[] args)
{
print_line("Hello world from c++ wrapper!");
dlopen("libgnuradio-analog-3.7.12.0.so.0.0.0", RTLD_LAZY|RTLD_GLOBAL);
test_gr();
}
}
}
Error message (when not preloading the library with dlopen):
dotnet: symbol lookup error: /usr/local/lib/libgnuradio-wrapper.so: undefined symbol: _ZN2gr6analog12sig_source_f4makeEdNS0_13gr_waveform_tEddf
So currently I can make it working if I reference full name of libraries, it means i have to have different approach to Windows, since in windows they are .dll files and it means i have to check always the library versions and provide them in config file.... also need to manually add the "custom" library to the PATH.
I had a similar issue when tried to port Mono Winform to Net Core it referenced libX11 (without the so too) and was not found... in any distro libX11 is apparently in a different place: https://github.com/yzrmn/System.CoreFX.Forms/issues/1
@fanol Can You try to preload libX11.so.6 and see if it will make any difference? In my case i just runned program and saw from error what library was unable to load and manually preloaded them...
You mean doing a p/invoke to dlopen()?
The fact is that Winform code is "giant" and does a lot of P/Invokes: all this worked in Mono without doing anything, this should work on CoreFx too.
"Should" is a relative term in software engineering.... I find it also annoying but perhaps the problem is everybody tries to write new cool stuff in Core and forgets there are literally billions lines of code written in C/C++ or some other cool language, compiled and totally reusable. It might be just there aren't many people faced with this issue just because there aren't many people who have been tried to use PInvoke in Core I would not reinvent a wheel where invention itself takes a lot of time and result probably would not be better.
@furoraest can you please run your tests in a version that doesn't work (and you'd expect it to) with LD_DEBUG set to all (don't export the LD_DEBUG as it would be then used for all consecutive commands you execute, just prefix your command line with LD_DEBUG=all) and then share the dump? The dump will contain all the information about where the shared library loader was looking for libraries, symbols, etc. The dump will be quite large, so I'd recommend sharing it via gist.
https://drive.google.com/open?id=13fsWeZbswD9-pbUsQrrsqmT3bGvHlF8q debug1 contains output from problem1, when full name of loaded library is not given debug2 contains output from problem2, when library what has been loaded is trying to make call to other library Hope this helps
@furoraest thank you for the logs. As for the issue dotnet/coreclr#1, looking at the log debug1.log.4385, I can see that the following locations and filenames were attempted to load:
4385: file=/usr/share/dotnet/shared/Microsoft.NETCore.App/2.1.1/libgnuradio-wrapper.so [0]; dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/2.1.1/libcoreclr.so [0]
4385: file=/home/tarmo/src/test/testPInvoke/bin/Debug/netcoreapp2.1/libgnuradio-wrapper.so [0]; dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/2.1.1/libcoreclr.so [0]
4385: file=libgnuradio-wrapper.so [0]; dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/2.1.1/libcoreclr.so [0]
4385: find library=libgnuradio-wrapper.so [0]; searching
4385: search cache=/etc/ld.so.cache
4385: search path=/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/tls:/usr/lib/x86_64-linux-gnu:/lib:/usr/lib (system search path)
4385: trying file=/lib/x86_64-linux-gnu/libgnuradio-wrapper.so
4385: trying file=/usr/lib/x86_64-linux-gnu/tls/libgnuradio-wrapper.so
4385: trying file=/usr/lib/x86_64-linux-gnu/libgnuradio-wrapper.so
4385: trying file=/lib/libgnuradio-wrapper.so
4385: trying file=/usr/lib/libgnuradio-wrapper.so
Then the same happens for permutations of the name, that means it tries it with the name with prepended lib - liblibgnuradio-wrapper.so, without the .so - libgnuradio-wrapper and with prepended lib nd without the .so - liblibgnuradio-wrapper.
It fails to find it in all of these locations. Please note that it also looked through the /etc/ld.so.cache and the search path, as you can see in the log above.
So it seems it may be that your ld.so.cache is not up to date. I've seen similar thing happening with some other Linux library in the past. Can you please try to rebuild the cache using the following:
rm /etc/ld.so.cache
ldconfig
That fixed the problem I've mentioned in the past for me.
@janvorli tried Your suggestion but no change
just for information, I am using Ubuntu 18.04 and latest and the greatest dotnet core SDK
It seems this has nothing to do with .NET core and it is rather a behavior of the dlopen that would happen the same way if you tried to do that from a C/C++ application. For versioned libraries, you need to ask for a specific version - the system doesn't really know which version you want - there can possibly be multiple ones installed on the same system.
Even if you build a C/C++ application and link it to a shared library, the application had the versioned name hardcoded in its list of dependencies - you can check that yourself by running ldd on any binary. The only thing is that at the build time of such an application, you don't specify the version. The dev versions of libraries contain unversioned name linked to the versioned one and the linker uses that to find the version it needs to embed into the binary.
For example ldd /usr/bin/curl:
linux-vdso.so.1 => (0x00007ffcb4fc5000)
libcurl-gnutls.so.4 => /usr/lib/x86_64-linux-gnu/libcurl-gnutls.so.4 (0x00007f3246432000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f3246218000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3245ffb000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3245c31000)
libidn.so.11 => /usr/lib/x86_64-linux-gnu/libidn.so.11 (0x00007f32459fe000)
librtmp.so.1 => /usr/lib/x86_64-linux-gnu/librtmp.so.1 (0x00007f32457e2000)
libnettle.so.6 => /usr/lib/x86_64-linux-gnu/libnettle.so.6 (0x00007f32455ac000)
libgnutls.so.30 => /usr/lib/x86_64-linux-gnu/libgnutls.so.30 (0x00007f324527c000)
libgssapi_krb5.so.2 => /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007f3245032000)
liblber-2.4.so.2 => /usr/lib/x86_64-linux-gnu/liblber-2.4.so.2 (0x00007f3244e23000)
libldap_r-2.4.so.2 => /usr/lib/x86_64-linux-gnu/libldap_r-2.4.so.2 (0x00007f3244bd2000)
/lib64/ld-linux-x86-64.so.2 (0x00007f32468ce000)
libhogweed.so.4 => /usr/lib/x86_64-linux-gnu/libhogweed.so.4 (0x00007f324499f000)
libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f324471f000)
libp11-kit.so.0 => /usr/lib/x86_64-linux-gnu/libp11-kit.so.0 (0x00007f32444bb000)
libtasn1.so.6 => /usr/lib/x86_64-linux-gnu/libtasn1.so.6 (0x00007f32442a8000)
libkrb5.so.3 => /usr/lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007f3243fd6000)
libk5crypto.so.3 => /usr/lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007f3243da7000)
libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007f3243ba3000)
libkrb5support.so.0 => /usr/lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007f3243998000)
libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f324377d000)
libsasl2.so.2 => /usr/lib/x86_64-linux-gnu/libsasl2.so.2 (0x00007f3243562000)
libgssapi.so.3 => /usr/lib/x86_64-linux-gnu/libgssapi.so.3 (0x00007f3243321000)
libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007f3243119000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f3242f15000)
libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007f3242d11000)
libheimntlm.so.0 => /usr/lib/x86_64-linux-gnu/libheimntlm.so.0 (0x00007f3242b08000)
libkrb5.so.26 => /usr/lib/x86_64-linux-gnu/libkrb5.so.26 (0x00007f324287e000)
libasn1.so.8 => /usr/lib/x86_64-linux-gnu/libasn1.so.8 (0x00007f32425dc000)
libhcrypto.so.4 => /usr/lib/x86_64-linux-gnu/libhcrypto.so.4 (0x00007f32423a9000)
libroken.so.18 => /usr/lib/x86_64-linux-gnu/libroken.so.18 (0x00007f3242193000)
libwind.so.0 => /usr/lib/x86_64-linux-gnu/libwind.so.0 (0x00007f3241f6a000)
libheimbase.so.1 => /usr/lib/x86_64-linux-gnu/libheimbase.so.1 (0x00007f3241d5b000)
libhx509.so.5 => /usr/lib/x86_64-linux-gnu/libhx509.so.5 (0x00007f3241b10000)
libsqlite3.so.0 => /usr/lib/x86_64-linux-gnu/libsqlite3.so.0 (0x00007f324183b000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f3241603000)
@furoraest Does @janvorli 's latest response make sense to you? I believe the work proposed in dotnet/runtime#4216 would help your situation. DllMap is something we are looking to work on in the near future, do you think that would mitigate your issue?
Thanks for clearing this, I also agree this is more fundamental problem than just .net core dll import. Very possibly the proposed work in dotnet/runtime#4216 will solve my current issues. Sadly, this wont make it for my current project but at least I have working solution (instead like some who would blame and then throw out all .net core solution) In the meantime, should i make my configuration as an example way how to solve this so others who might stumble on it can have more or less working solution? And should it be referenced before closing this issue?
Appreciate your understanding and glad you have a workaround.
This issue will still be searchable after it is closed so people should find it with out too much trouble. Feel free to add additional links to your mitigation if you like. I will make sure we bring this issue up as dotnet/runtime#4216 work gets under way.
Thanks.