runtime: Alpine-Linux: System.DllNotFoundException when calling native library via P/Invoke
From @rjsathome on November 12, 2018 11:54
I have a nuget package containing native code which is called via p/invoke fromt the .NET Core assembly.
When using this on Alpine Linux I get:
vmalpine64 (1)$ dotnet run
Using launch settings from /home/rjs/src/pdflib-9.1.x/dist/PDFlib/PDFlib-9.1.2p8-Alpine-Linux-x86_64-.NET-Core/bind/dotnetcore/csharp/hello/Properties/launchSettings.json...
Unhandled Exception: System.DllNotFoundException: Unable to load shared library 'PDFlib_dotnetcore_native' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: Error loading shared library libPDFlib_dotnetcore_native: No such file or directory
at PDFlib_dotnet.PDFlib.PInvoke.PDF_new()
at PDFlib_dotnet.PDFlib..ctor()
at Hello.Main(String[] args) in /home/rjs/src/pdflib-9.1.x/dist/PDFlib/PDFlib-9.1.2p8-Alpine-Linux-x86_64-.NET-Core/bind/dotnetcore/csharp/hello/hello.cs:line 15
The minmal code to reproduce is:
using PDFlib_dotnet;
class Hello {
static void Main(string[] args) {
PDFlib p;
p = new PDFlib();
}
}
The Nuget package content is:
vmalpine64 (1)$ unzip -l ../../PDFlib_dotnet.1.2.8.nupkg
Archive: ../../PDFlib_dotnet.1.2.8.nupkg
Length Date Time Name
--------- ---------- ----- ----
503 11-06-2018 15:00 _rels/.rels
748 11-06-2018 15:00 PDFlib_dotnet.nuspec
35840 11-06-2018 14:00 lib/netstandard2.0/PDFlib_dotnet.dll
36 10-22-2018 18:47 readme.txt
8617128 11-06-2018 14:00 runtimes/alpine-x64/native/PDFlib_dotnetcore_native.so
582 11-06-2018 15:00 [Content_Types].xml
706 11-06-2018 15:00 package/services/metadata/core-properties/5c87229948e04d93aab4633ed8c7fcc5.psmdcp
-------- -------
8655543 7 files
I would expect that dotnet run
finds the nativ library at runtimes/alpine-x64/native/PDFlib_dotnetcore_native.so in the nuget package.
When I check with strace where dotnet searches I find:
vmalpine64 (1)$ strace -f -o strace.txt dotnet run
Using launch settings from /home/rjs/src/pdflib-9.1.x/dist/PDFlib/PDFlib-9.1.2p8-Alpine-Linux-x86_64-.NET-Core/bind/dotnetcore/csharp/hello/Properties/launchSettings.json...
Unhandled Exception: System.DllNotFoundException: Unable to load shared library 'PDFlib_dotnetcore_native' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: Error loading shared library libPDFlib_dotnetcore_native: No such file or directory
at PDFlib_dotnet.PDFlib.PInvoke.PDF_new()
at PDFlib_dotnet.PDFlib..ctor()
at Hello.Main(String[] args) in /home/rjs/src/pdflib-9.1.x/dist/PDFlib/PDFlib-9.1.2p8-Alpine-Linux-x86_64-.NET-Core/bind/dotnetcore/csharp/hello/hello.cs:line 7
vmalpine64 (1)$ grep dotnetcore_native strace.txt
dotnet/runtime#852 open("/home/rjs/dotnet/shared/Microsoft.NETCore.App/2.1.2/PDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/home/rjs/.nuget/packages/pdflib_dotnet/1.2.8/lib/netstandard2.0/PDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/lib/PDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/local/lib/PDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/lib/PDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/home/rjs/dotnet/shared/Microsoft.NETCore.App/2.1.2/libPDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/home/rjs/.nuget/packages/pdflib_dotnet/1.2.8/lib/netstandard2.0/libPDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/lib/libPDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/local/lib/libPDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/lib/libPDFlib_dotnetcore_native.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/home/rjs/dotnet/shared/Microsoft.NETCore.App/2.1.2/PDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/home/rjs/.nuget/packages/pdflib_dotnet/1.2.8/lib/netstandard2.0/PDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/lib/PDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/local/lib/PDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/lib/PDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/home/rjs/dotnet/shared/Microsoft.NETCore.App/2.1.2/libPDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/home/rjs/.nuget/packages/pdflib_dotnet/1.2.8/lib/netstandard2.0/libPDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/lib/libPDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/local/lib/libPDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
16906 open("/usr/lib/libPDFlib_dotnetcore_native", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
On Alpine-Linux donet run
only looks in the “/home/rjs/.nuget/packages/pdflib_dotnet/1.2.8/lib/netstandard2.0/” directory, not int the “runtimes/alpine-x64/native/” directory.
The dotnet version is:
vmalpine64 (1)$ dotnet --version
2.1.400
I also tested it with the 2.1.403 version.
Alpine LInux is:
vmalpine64 (1)$ uname -a
Linux vmalpine64 4.14.69-0-vanilla dotnet/coreclr#1-Alpine SMP Mon Sep 10 19:33:23 UTC 2018 x86_64 Linux
vmalpine64 (1)$ cat /etc/alpine-release
3.8.1
On macOS or Ubuntu it is working as expected.
Copied from original issue: dotnet/corefx#33409
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (10 by maintainers)
Thanks a lot @rjsathome for the confirmation. In theory you should be able to “hack” it to work even on 3.8. Go to the location where the shared framework Microsoft.NETCore.App is store on the machine, it should have
Microsoft.NETCore.App.deps.json
file in it. Open that and near the end of the file it has the RID fallback graph. Add alpine.3.8 by basically copying the 3.7 entries. And retry - that should make it work. Obviously not a solution, but if you need to make it work on one specific machine…