Recent

Author Topic: FreeBSD shared library problems  (Read 1776 times)

bbrtj

  • New Member
  • *
  • Posts: 12
FreeBSD shared library problems
« on: May 01, 2025, 02:42:38 pm »
Some time ago I created this issue in fpc's bugtracker: https://gitlab.com/freepascal.org/fpc/source/-/issues/39427
The patch I supplied works (although it needs to be applied manually now), but now I spotted a new problem with it. Including an unit like cwstring causes the library to be unloadable in other programs. Attempting to load such library crashes the program with Bus Error.
These shared library issues are not only problematic when developing pascal. For example, if you use doublecmd on FreeBSD, it's close to impossible to get working plugins (like zip.wcx).

I'd like to ask for help getting these issues resolved (as the issue had no activity at all). I'm pretty sure it can be resolved by modifying .as files in rtl/freebsd directory of the fpc source. I moved linux dllprt0.as to freebsd, added the operatingsystem_parameters patch to it and it somehow seems to work with cwstring now, but still not enough to compile working doublecmd plugins (now Access Violation).

Edit: The problem is with compiling shared libraries to be used in other programs, not with loading shared libraries in a program. Sorry if it was not clear enough.
« Last Edit: May 01, 2025, 03:40:41 pm by bbrtj »

Fred vS

  • Hero Member
  • *****
  • Posts: 3629
    • StrumPract is the musicians best friend
Re: FreeBSD shared library problems
« Reply #1 on: May 01, 2025, 03:26:46 pm »
Sadly it seems that BSD (FreeBSD, OpenBSD, NetBSD) is no longer cherished by FPC devs.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

AmatCoder

  • Jr. Member
  • **
  • Posts: 67
    • My site
Re: FreeBSD shared library problems
« Reply #2 on: May 01, 2025, 03:34:54 pm »
Do you link the executable with "-export-dynamic" option?
As far as I can remember(*) FreeBSD executables need to be linked using the "-export-dynamic" flag to load dynamic libraries/plugins with dlopen().

(*) At least in C language.

bbrtj

  • New Member
  • *
  • Posts: 12
Re: FreeBSD shared library problems
« Reply #3 on: May 01, 2025, 03:46:36 pm »
Do you link the executable with "-export-dynamic" option?

Actually I link no executables, I'm loading the library in Perl through FFI. My doublecmd is installed from pkg and is able to load plugins (which are libraries). The problem lies entirely in creation of shared libraries by FPC, I excluded all other options.

Fred vS

  • Hero Member
  • *****
  • Posts: 3629
    • StrumPract is the musicians best friend
Re: FreeBSD shared library problems
« Reply #4 on: May 01, 2025, 05:05:05 pm »
Do you link the executable with "-export-dynamic" option?

Actually I link no executables, I'm loading the library in Perl through FFI. My doublecmd is installed from pkg and is able to load plugins (which are libraries). The problem lies entirely in creation of shared libraries by FPC, I excluded all other options.

Hello.

What is the result for this?:
Code: Pascal  [Select][+][-]
  1. $ readelf -d <your_fpc_lib>

Did you add the -fPIC -Sh parameters at compilation?
Also, better use cmem with FreeBSD, place it as first unit in the uses clause.
And finally, add this at end of code:
Code: Pascal  [Select][+][-]
  1. initialization
  2.   Set8087CW($133f); // Mask all FPU exceptions




« Last Edit: May 01, 2025, 05:46:36 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12339
  • FPC developer.
Re: FreeBSD shared library problems
« Reply #5 on: May 01, 2025, 07:22:51 pm »
And test a native version for your FreeBSD, that means in practice, using FIXES.

bbrtj

  • New Member
  • *
  • Posts: 12
Re: FreeBSD shared library problems
« Reply #6 on: May 01, 2025, 07:28:39 pm »
What is the result for this?:
Code: Pascal  [Select][+][-]
  1. $ readelf -d <your_fpc_lib>
When compiling with cwstring (Bus error):
Code: Pascal  [Select][+][-]
  1. Dynamic section at offset 0x1ce2d8 contains 23 entries:
  2.   Tag                Type                  Name/Value
  3.  0x0000000000000001 NEEDED               Shared library: [libc.so.7]
  4.  0x000000000000000e SONAME               Library soname: [libtags.so]
  5.  0x000000000000000c INIT                 0xa2220
  6.  0x000000000000000d FINI                 0xb8040
  7.  0x0000000000000019 INIT_ARRAY
  8.  0x000000000000001b INIT_ARRAYSZ         8 (bytes)
  9.  0x0000000000000004 HASH                 0x158
  10.  0x0000000000000005 STRTAB               0xc98
  11.  0x0000000000000006 SYMTAB               0x3e0
  12.  0x000000000000000a STRSZ                745 (bytes)
  13.  0x000000000000000b SYMENT               24 (bytes)
  14.  0x0000000000000003 PLTGOT               0x1cf498
  15.  0x0000000000000002 PLTRELSZ             2136 (bytes)
  16.  0x0000000000000014 PLTREL               RELA
  17.  0x0000000000000017 JMPREL               0xa1400
  18.  0x0000000000000007 RELA                 0x1080
  19.  0x0000000000000008 RELASZ               656256 (bytes)
  20.  0x0000000000000009 RELAENT              24 (bytes)
  21.  0x000000006ffffffe VERNEED              0x1040
  22.  0x000000006fffffff VERNEEDNUM           1
  23.  0x000000006ffffff0 VERSYM               0xf82
  24.  0x000000006ffffff9 RELACOUNT            27342
  25.  0x0000000000000000 NULL                 0x0
When compiling without it (works):
Code: Pascal  [Select][+][-]
  1. Dynamic section at offset 0x134030 contains 23 entries:
  2.   Tag                Type                  Name/Value
  3.  0x0000000000000001 NEEDED               Shared library: [libc.so.7]
  4.  0x000000000000000e SONAME               Library soname: [libtags.so]
  5.  0x000000000000000c INIT                 0x5e380
  6.  0x000000000000000d FINI                 0x741a0
  7.  0x0000000000000019 INIT_ARRAY
  8.  0x000000000000001b INIT_ARRAYSZ         8 (bytes)
  9.  0x0000000000000004 HASH                 0x158
  10.  0x0000000000000005 STRTAB               0xaa0
  11.  0x0000000000000006 SYMTAB               0x398
  12.  0x000000000000000a STRSZ                572 (bytes)
  13.  0x000000000000000b SYMENT               24 (bytes)
  14.  0x0000000000000003 PLTGOT               0x1341f0
  15.  0x0000000000000002 PLTRELSZ             1704 (bytes)
  16.  0x0000000000000014 PLTREL               RELA
  17.  0x0000000000000017 JMPREL               0x5d838
  18.  0x0000000000000007 RELA                 0xda8
  19.  0x0000000000000008 RELASZ               379536 (bytes)
  20.  0x0000000000000009 RELAENT              24 (bytes)
  21.  0x000000006ffffffe VERNEED              0xd78
  22.  0x000000006fffffff VERNEEDNUM           1
  23.  0x000000006ffffff0 VERSYM               0xcdc
  24.  0x000000006ffffff9 RELACOUNT            15812
  25.  0x0000000000000000 NULL                 0x0


Did you add the -fPIC -Sh parameters at compilation?
I am compiling through lazbuild, made a shared library project in lazarus. I don't think I had these options, I added them now as custom options in the project, but no change.


Also, better use cmem with FreeBSD, place it as first unit in the uses clause.
And finally, add this at end of code:
Code: Pascal  [Select][+][-]
  1. initialization
  2.   Set8087CW($133f); // Mask all FPU exceptions
Tried that, but no difference.


And test a native version for your FreeBSD, that means in practice, using FIXES.
Not sure what you mean by that, I compile FPC myself from the fixes-3.2 branch using fpcupdeluxe.

« Last Edit: May 01, 2025, 07:30:21 pm by bbrtj »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12339
  • FPC developer.
Re: FreeBSD shared library problems
« Reply #7 on: May 01, 2025, 07:32:18 pm »
Not sure what you mean by that, I compile FPC myself from the fixes-3.2 branch using fpcupdeluxe.

Ok. But check the ELF ident with readelf/elfdump to check that the binaries generated are really match the (major) version that you use.

Fred vS

  • Hero Member
  • *****
  • Posts: 3629
    • StrumPract is the musicians best friend
Re: FreeBSD shared library problems
« Reply #8 on: May 01, 2025, 07:53:55 pm »
When compiling with cwstring (Bus error):

Where is cwstring in your uses section?
Usually it is placed just after cthreads:
Quote
{$ifdef unix}cthreads, cwstring,{$endif}

Also you may add this at begin of code:
Code: Pascal  [Select][+][-]
  1. {$interfaces corba}

(Maybe not useful but you may try).
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3629
    • StrumPract is the musicians best friend
Re: FreeBSD shared library problems
« Reply #9 on: May 01, 2025, 08:11:42 pm »
Do you absolutely need cwstring?

You may replace WideString with PChar or AnsiString.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

bbrtj

  • New Member
  • *
  • Posts: 12
Re: FreeBSD shared library problems
« Reply #10 on: May 01, 2025, 08:36:44 pm »
Ok. But check the ELF ident with readelf/elfdump to check that the binaries generated are really match the (major) version that you use.


I don't know these tools, but I think it does.

Code: [Select]
$ readelf -h libtags.so
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 09 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            FreeBSD
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices x86-64
  Version:                           0x1
  Entry point address:               0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          1556016 (bytes into file)
  Flags:                             0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         5
  Size of section headers:           64 (bytes)
  Number of section headers:         29
  Section header string table index: 28
$ file libtags.so
libtags.so: ELF 64-bit LSB shared object, x86-64, version 1 (FreeBSD), dynamically linked, for FreeBSD 14.2, stripped
$ uname -v
FreeBSD 14.2-RELEASE-p1 GENERIC


Also you may add this at begin of code:
Code: Pascal  [Select][+][-]
  1. {$interfaces corba}


Sadly, this did not help.


Where is cwstring in your uses section?

Do you absolutely need cwstring?

You may replace WideString with PChar or AnsiString.


I don't want cwstring and I am not using it directly in my code (though if I do that instead, the end result is the same). I am using some other code which pulls LazUtils, which pull LazUtf8, which pulls cwstring. I tried replacing it with fpwidestring directly in lazarus source (based on information found in this non-English thread: https://doublecmd.h1n.ru/viewtopic.php?t=3323), but it did not help. So I think there may be some other unit included somewhere which pulls some unit which forces clib, as I did not find more cwstring occurences in lazarus code.

Edit: Actually, I just repeated the modification of lazarus code to replace cwstring with fpwidestring and it works now. Not sure why it did not work before, maybe not everything got cleaned before build. So I guess I can compile it now, but it's a pain... since I think I have to modify lazarus source back to cwstring for normal programs. Unless I can maybe put a compiler directive there to detect shared libraries? Anyway, doublecmd plugins compiled after that change still crash the program with Bus error.
« Last Edit: May 01, 2025, 08:47:45 pm by bbrtj »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12339
  • FPC developer.
Re: FreeBSD shared library problems
« Reply #11 on: May 01, 2025, 08:50:16 pm »
try "elfdump -n"

bbrtj

  • New Member
  • *
  • Posts: 12
Re: FreeBSD shared library problems
« Reply #12 on: May 01, 2025, 08:52:23 pm »
try "elfdump -n"

Looks fine to me

Code: [Select]
$ elfdump -n libtags.so

note (.note.tag):
        FreeBSD 1402000 (NT_FREEBSD_ABI_TAG)

Fred vS

  • Hero Member
  • *****
  • Posts: 3629
    • StrumPract is the musicians best friend
Re: FreeBSD shared library problems
« Reply #13 on: May 01, 2025, 09:16:14 pm »
I suppose you did try to debug the lib compiled with -gl parameter and to run doublecmd with gdb.

Code: Pascal  [Select][+][-]
  1. $ gdb --args doublecmd

Does it give infos when crashing after use your lib?
« Last Edit: May 01, 2025, 09:18:18 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

bbrtj

  • New Member
  • *
  • Posts: 12
Re: FreeBSD shared library problems
« Reply #14 on: May 01, 2025, 09:23:37 pm »
I suppose you did try to debug the lib compiled with -gl parameter and to run doublecmd with gdb.

Code: Pascal  [Select][+][-]
  1. $ gdb --args doublecmd

Does it give infos when crashing after use your lib?

Hmm, I think my method of cleaning + compiling stuff with lazarus is faulty. I literally re-compiled it 3 times with cleaning before to make sure I am looking at the current version. But now after I enabled debugging in the lib and loading it, the problem no longer occurs. Not even after disabling -gl again. I thought "Clean up and build..." option with everything selected is a sure way to get everything recompiled?

 

TinyPortal © 2005-2018