Recent

Author Topic: musl-libc on Linux works with Alpine Linux and FPC 3.2.2 but not 3.3.1  (Read 1957 times)

Bogen85

  • Hero Member
  • *****
  • Posts: 595
This is somewhat related to this: https://forum.lazarus.freepascal.org/index.php/topic,63680.msg

I filed an issue on this here: https://gitlab.com/freepascal.org/fpc/source/-/issues/40437

FPC 3.2.2 on alpine will compile and link dynamically linked executables that work as expected.

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$h+}
  3. {$codepage utf8}
  4.  
  5. program hello_libc;
  6.  
  7. uses ctypes;
  8.  
  9. function c_write (const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external 'libc' name 'write';
  10.  
  11. var
  12.         s: string = 'hello ';
  13. begin
  14.         c_write(1, pchar(s), length(s));
  15.         writeln('alpine musl!');
  16. end.
  17.  

Code: Text  [Select][+][-]
  1. $ fpc hello_libc.pas
  2. Free Pascal Compiler version 3.2.2 [2022/08/07] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling hello_libc.pas
  6. Linking hello_libc
  7. Warning: "crtbegin.o" not found, this will probably cause a linking failure
  8. Warning: "crtend.o" not found, this will probably cause a linking failure
  9. 16 lines compiled, 0.0 sec
  10. 2 warning(s) issued
  11.  
  12. $ file hello_libc
  13. hello_libc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, for GNU/Linux 2.4.0, stripped
  14.  
  15. $ ldd hello_libc
  16. /lib/ld-musl-x86_64.so.1 (0x7ff02d918000)
  17. libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7ff02d918000)
  18.  
  19. $ ./hello_libc
  20. hello alpine musl!
  21.  

So it works on 3.2.2, but not on 3.3.1, causing the build of FPC 3.3.1 on Alpine Linux to fail (hence the issue report I filed).


Bogen85

  • Hero Member
  • *****
  • Posts: 595
[SOLVED] musl-libc on Linux works with Alpine and FPC 3.2.2 but not 3.3.1
« Reply #1 on: September 10, 2023, 05:45:31 pm »
I filed an issue on this here: https://gitlab.com/freepascal.org/fpc/source/-/issues/40437

FPC 3.2.2 on alpine will compile and link dynamically linked executables that work as expected.

For 3.3.1 it turned out to be shared object pathing issue.

So it works on 3.2.2, but not on 3.3.1, causing the build of FPC 3.3.1 on Alpine Linux to fail (hence the issue report I filed).

I'll work on proper patch for it, but for now I can use both FPC 3.2.2 and the latest FPC 3.3.1 on Alpine Linux with musl libc.

« Last Edit: September 10, 2023, 06:11:29 pm by Bogen85 »

Joanna

  • Hero Member
  • *****
  • Posts: 755
Re: musl-libc on Linux works with Alpine Linux and FPC 3.2.2 but not 3.3.1
« Reply #2 on: September 11, 2023, 01:36:15 am »
Congrats  :D
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: musl-libc on Linux works with Alpine Linux and FPC 3.2.2 but not 3.3.1
« Reply #3 on: September 19, 2023, 03:28:14 am »
i have installed musl into /projects/musl/usr.local.musl
with the
/projects/musl > ls usr.local.musl/
bin  include  lib

/projects/musl > ls usr.local.musl/lib
crt1.o  crtn.o  libcrypt.a  libdl.a  libpthread.a  librt.a    libxnet.a       rcrt1.o
crti.o  libc.a  libc.so     libm.a   libresolv.a   libutil.a  musl-gcc.specs  Scrt1.o

---

i have musl static and libc static working with linklib and libc dynamic working with function external lib

but none of the function 'external lib'  work for musl
but none of musl dynamic work  either with function 'external lib' or linklib work

could you look at the code and tell me what i am doing wrong?

Code: Pascal  [Select][+][-]
  1. program musl; // from https://forum.lazarus.freepascal.org/index.php/topic,64570.0.html
  2.  
  3. uses ctypes;
  4.  
  5. var s : ansistring = 'write : ';
  6.  
  7. // glibc dynamic linked  good
  8. //function c_write(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external 'libc' name 'write';
  9.  
  10. // glibc static linked
  11. {$linklib /lib/libc.a} // good
  12. function c_write(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external name 'write';
  13.  
  14. // glibc dynamic linked
  15. //{$linklib /lib/libc.so} // fails
  16. //{$linklib libc} // good
  17. //function c_write(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external name 'write';
  18.  
  19.  
  20. // musl static linked
  21. {$linklib /projects/musl/usr.local.musl/lib/libc.a} // good
  22. function c_write1(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external name 'write';
  23.  
  24. // musl dynamic linked
  25. //{$linklib /projects/musl/usr.local.musl/lib/libc.so} // fails
  26. //function c_write1(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external name 'write';
  27.  
  28. // /usr/local/bin/ld: cannot find -l/projects/musl/usr.local.musl/lib/libc.a
  29. //function c_write2(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external '/projects/musl/usr.local.musl/lib/libc.a' name 'write';
  30.  
  31. // /usr/local/bin/ld: cannot find -l/projects/musl/usr.local.musl/lib/libc
  32. //function c_write2(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external '/projects/musl/usr.local.musl/lib/libc.so' name 'write';
  33.  
  34.  
  35. begin
  36.  
  37. writeln;
  38.  
  39. // glibc
  40. c_write(1, pchar(s), length(s));
  41. writeln('libc');
  42.  
  43. // musl
  44. c_write1(1, pchar(s), length(s));
  45. writeln('musl1');
  46.  
  47.  
  48. // external musl method static and dynamic both fail
  49. //c_write2(1, pchar(s), length(s));
  50. //writeln('musli2');
  51.  
  52. writeln;
  53.  
  54. end.
  55.  

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: musl-libc on Linux works with Alpine Linux and FPC 3.2.2 but not 3.3.1
« Reply #4 on: September 19, 2023, 05:44:05 am »
could you look at the code and tell me what i am doing wrong?

What compiler are you using?
No official compiler for FPC supports musl-libc.
There is a hack on the alpine package for fpc to make it work on alpine.

I have a patch for x86_64 mainline FPC and musl libc, but I need to do some more work on it to make it work for the other architectures and build a compiler for it when other libc are present.

https://gitlab.com/bogen85/source/-/compare/main...main?from_project_id=28644964#81870c36041af85b22d9720de972572a2d733365

But there is a bit more work to do on it: https://gitlab.com/freepascal.org/fpc/source/-/issues/40437
« Last Edit: September 19, 2023, 06:44:21 am by Bogen85 »

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: musl-libc on Linux works with Alpine Linux and FPC 3.2.2 but not 3.3.1
« Reply #5 on: September 19, 2023, 08:19:46 pm »
What compiler are you using?
fpc-3.3.1

No official compiler for FPC supports musl-libc.
i'm pretty sure the static musl one works with fpc

// musl static linked
{$linklib /projects/musl/usr.local.musl/lib/libc.a} // good
function c_write1(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external name 'write';
// musl
c_write1(1, pchar(s), length(s));
writeln('musl1');

----




i'm having trouble getting it to use the dynamic library with the same linklib code
// musl dynamic linked
//{$linklib /projects/musl/usr.local.musl/lib/libc.so} // fails
//function c_write1(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external name 'write';

or using  extenal fullpath with both musl static or dynamic
// /usr/local/bin/ld: cannot find -l/projects/musl/usr.local.musl/lib/libc.a
//function c_write2(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external '/projects/musl/usr.local.musl/lib/libc.a' name 'write';

// /usr/local/bin/ld: cannot find -l/projects/musl/usr.local.musl/lib/libc
//function c_write2(const fd: cint; const buffer: pchar; const count: cint): cint; cdecl; external '/projects/musl/usr.local.musl/lib/libc.so' name 'write';







 

TinyPortal © 2005-2018