sec / dotnet-core-freebsd-source-build

Collection of script to build .NET Core under FreeBSD OS (with binary releases)
MIT License
53 stars 4 forks source link

Libraries libdl & libidl not found (.so hell ?) #18

Closed devosalain closed 1 year ago

devosalain commented 1 year ago

dotnet --list-sdks 7.0.202 [/usr/share/dotnet/sdk]

Running a dotnet F# gtk application (running fine on Gentoo-Linux & WSL-Windows ) returns,

dotnet run Unhandled exception. System.TypeInitializationException: The type initializer for 'Gtk.Application' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'libdl.so.2' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: Cannot open "/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.4/libdl.so.2.so" Cannot open "/usr/home/x/Languages/fhashtut/02_gtk/bin/Debug/net7.0/libdl.so.2.so" Shared object "libdl.so.2.so" not found, required by "fsharp_gtk" Cannot open "/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.4/liblibdl.so.2.so" Cannot open "/usr/home/x/Languages/fhashtut/02_gtk/bin/Debug/net7.0/liblibdl.so.2.so" Shared object "liblibdl.so.2.so" not found, required by "fsharp_gtk" Cannot open "/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.4/libdl.so.2" Cannot open "/usr/home/x/Languages/fhashtut/02_gtk/bin/Debug/net7.0/libdl.so.2" Shared object "libdl.so.2" not found, required by "fsharp_gtk" Cannot open "/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.4/liblibdl.so.2" Cannot open "/usr/home/x/Languages/fhashtut/02_gtk/bin/Debug/net7.0/liblibdl.so.2" Shared object "liblibdl.so.2" not found, required by "fsharp_gtk"

at FuncLoader.Linux.dlopen(String path, Int32 flags) at FuncLoader.LoadLibrary(String libname) at GLibrary.TryGet(Library library, IntPtr& ret) at GLibrary.Load(Library library) at Gtk.Application..cctor() --- End of inner exception stack trace --- at Gtk.Application.Init() at Program.main(String[] a) in /usr/home/x/Languages/fhashtut/02_gtk/Program.fs:line 31

I tried to put a softlink " libidl.so.2 -> /usr/local/lib/libIDL-2.so.0.0.0" but that is not enough.

Note : /usr/lib contains libdl.so.1

sec commented 1 year ago

Yeah, maybe try to do something like ln -s /usr/lib/libdl.so /usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.4/libdl.so.2

I remember libdl issues back in mono days, but I was thinking it was sorted out. Can't tell if this is runtime issue or GTK library you're using - please try with that softlink in correct place, it should load, if yes, let's see what will fail later :)

Also if you could prepare some small project to reproduce the issue, I could investigate little more.

Thefrank commented 1 year ago

libdl is used for things like dlopen() which is from libc under FreeBSD. Linux uses a libdl.so.2 and in most cases, glibc libidl is something entirely different (https://www.freshports.org/devel/libIDL/)

     Other ELF platforms require linking with library "libdl" to provide
     dlopen() and other functions.  FreeBSD does not require linking with the
     library, but supports it for compatibility.

oh!

at FuncLoader.Linux.dlopen(String path, Int32 flags) yeah. linux. Linux and FreeBSD are not the same. The function should be similar enough but you should link against libc instead of glibc and dl

sec commented 1 year ago

I believe libdl should be linked correctly during build - as I don't even have glibc etc. so. From the logs, I don't see any libidl references

@devosalain please provide full logs or some sample repro product, as I'm quite lost.

devosalain commented 1 year ago

Placing a soft link from libl.so.2 to libdl.so.1 did not worked. Now i have gtk errors:

It gave the error: dotnet run
Unhandled exception. System.TypeInitializationException: The type initializer for 'Gtk.Application' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'libdl.so.2'

dotnet run Unhandled exception. System.TypeInitializationException: The type initializer for 'Gtk.Application' threw an exception. ---> System.DllNotFoundException: Gtk: libgtk-3-0.dll, libgtk-3.so.0, libgtk-3.0.dylib, gtk-3.dll at GLibrary.Load(Library library) at Gtk.Application..cctor() --- End of inner exception stack trace --- at Gtk.Application.Init() at Program.main(String[] a) in /usr/home/x/Languages/fhashtut/15_gtk_sql/Program.fs:line 45

Gtk cannot be initialised because of misssing dlls ?

devosalain commented 1 year ago

Source code of my project. Project file & F# file:

cat gtk_sql.fsproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GtkSharp" Version="3.24.24.*" />
    <PackageReference Include="Npgsql.FSharp" Version="5.6.0" />
  </ItemGroup>

</Project>
devosalain commented 1 year ago

cat Program.fs

open System
open Gtk
open Npgsql.FSharp

type User = {
    Datetime: string
    Host: string
    Program: string
    Pid: string
    Facility: string
    Priority: string
    Message: string
}

let OnDelete (sender:obj) (args:DeleteEventArgs) =
  Application.Quit()
  args.RetVal <- true

[<EntryPoint>]
let main a =
    let connectionString : string =
        Sql.host "127.0.0.1"
        |> Sql.database "syslogng"
        |> Sql.username "x"
        |> Sql.password "x"
        |> Sql.port 5432
        |> Sql.formatConnectionString

    let listall (connectionString: string)=
        connectionString
        |> Sql.connect
        |> Sql.query "select  * from messages_freebsd_20230322"
        |> Sql.execute (fun read ->
                            {
                                Datetime = read.text "datetime"
                                Host = read.text "host"
                                Program = read.text "program"
                                Pid = read.text "pid"
                                Facility = "_"
                                Priority = "_"
                                Message = read.text  "message"
                             })
    let lis= listall (connectionString)

    Application.Init();
    let wnd = new Window("F# Main Window")
    wnd.SetSizeRequest(500, 200)
    wnd.SetDefaultSize(500,200)
    let store = new TreeStore ([|typeof<String>|]);
    List.iter (fun row ->
                   let s =sprintf "%-20A %-20A %-20A %-20A %-20A %-20A %A" row.Datetime row.Host row.Program row.Pid row.Facility row.Priority row.Message
                   store.AppendValues (s) |> ignore
          ) lis
    let sw = new ScrolledWindow ();
    wnd.Add sw
    let tv = new TreeView ()
    tv.Model <- store
    tv.HeadersVisible <- true

    tv.AppendColumn ("Data", new CellRendererText (), "text", 0) |>ignore
    sw.Add tv
    sw.Show ()
    wnd.DeleteEvent.AddHandler(fun s a -> OnDelete s a) // fun still needed
    wnd.ShowAll()
    Application.Run()
    0
Thefrank commented 1 year ago

@devosalain


Disclaimer: I do not have any FreeBSD systems that are able to run a DE/GUI. This was done with the steps below. I will note I used the dotNET7 RTM SDK and had to install one package

pkg install gtk3 dotnet build gtk_sql.fsproj

MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  Restored /root/fsharp/gtk_sql.fsproj (in 1.96 sec).
  gtk_sql -> /root/fsharp/bin/Debug/net7.0/gtk_sql.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:19.28

It runs without having to make any symlinks :)

dotnet exec /root/fsharp/bin/Debug/net7.0/gtk_sql.dll
Unhandled exception. Npgsql.NpgsqlException (0x80004005): Failed to connect to 127.0.0.1:5432
 ---> System.Net.Sockets.SocketException (61): Connection refused
   at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
   at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|208_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
   at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.PoolingDataSource.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.PoolingDataSource.<Get>g__RentAsync|28_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|45_0(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.Open()
   at Npgsql.FSharp.Sql.execute[t](FSharpFunc`2 read, SqlProps props) in /Users/zaid/projects/Npgsql.FSharp/src/Npgsql.FSharp.fs:line 393
   at Program.listall@30.Invoke(String connectionString) in /root/fsharp/Program.fs:line 33
   at Program.main(String[] a) in /root/fsharp/Program.fs:line 43
Abort

This appears correct given I don't have syslog-ng running

devosalain commented 1 year ago
# freebsd-version -kru 
13.2-RC1
13.2-RC1
13.2-RC1

Because the connection to sql was not found the gtk-application was not launched and the message missing dll's was not given. Please try to run code below ( which only uses gtk, but does not depend on sql)

cat fsharp_gtk.fsproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GtkSharp" Version="3.24.24.*" />
  </ItemGroup>

</Project>

cat Program.fs

open System
open Gtk

let OnDelete (sender:obj) (args:DeleteEventArgs) =
  Application.Quit()
  args.RetVal <- true

[<EntryPoint>]
let main a =
   Application.Init();
   let wnd = new Window("F# Main Window")
   wnd.SetSizeRequest(500, 200)
   wnd.SetDefaultSize(500,200)
   let store = new TreeStore ([|typeof<String>; typeof<String>|]);
   for  i  in [|1..50|] do
      store.AppendValues (("Demo " + (string i)) , ("Data " + (string i))) |> ignore
   let sw = new ScrolledWindow ();
   wnd.Add sw
   let tv = new TreeView ()
   tv.Model <- store
   tv.HeadersVisible <- true
   tv.AppendColumn ("Demo", new CellRendererText (), "text", 0) |>ignore
   tv.AppendColumn ("Data", new CellRendererText (), "text", 1) |>ignore 
   sw.Add tv
   sw.Show ()
   wnd.DeleteEvent.AddHandler(fun s a -> OnDelete s a) // fun still needed
   wnd.ShowAll()
   Application.Run()
   0
Thefrank commented 1 year ago

GtkSharp does not understand FreeBSD and falls back to Linux https://github.com/GtkSharp/GtkSharp/blob/develop/Source/Libs/Shared/FuncLoader.cs

@devosalain As FreeBSD is not officially supported under dotNET it might be hard to get a PR merged that would add support for FreeBSD into GtkSharp

sec commented 1 year ago

Closing this as it's not related to .NET build directly.