[SOLVED] How to call a FPC compiled shared library under Alpine? lazarus / 32-Linux ---------------------------------------------------------------------- rarnu · 2021-06-11 17:36 I'm now using Alpine Linux for some development. I find that the shared library compiled with FPC can't be loaded by other programs. here's my library's code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i library mylib;{$mode objfpc}{$H+}uses  Classes, SysUtils; function add(x: integer; y: integer): integer; cdecl;begin  Result := x + y;end; exports  add; beginend.  These code compiled by command: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc mylib.lpr -FL/lib/libc.musl-x86_64.so.1  There is no "ld-linux-x86-64.so.2" in Alpine Linux, even glibc is also not included. Only musl-libc can be used. and here's my app for calling the lib: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program host;{$mode objfpc}{$H+} uses  Classes, SysUtils, dynlibs; type  TAdd = function (x: integer; y: integer): integer; cdecl; var  libPath: string;  lib: TLibHandle;  mAdd: TAdd;begin  libPath:= ExtractFilePath(ParamStr(0)) + 'libmylib.so';  lib := loadLibrary(libPath);  mAdd := TAdd(GetProcAddress(lib, 'add'));  WriteLn(mAdd(1, 2));end;  These code compiled by command: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc host.lpr -FL/lib/libc.musl-x86_64.so.1  now I get "libmylib.so" and "host" files, and I try to run "host", it crashed with the message: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i An unhandled exception occurred at $00007F4E99259B40:EAccessViolation:   $00007F4E99259B40  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84  $0000000000422D84 Segmentation fault  I can't solve the problem with the given message, needs help. And I have also tried, write these code with C and compiled with gcc, it works fine!! I want to know the reason and how to make it work. Help me please. thank you. ---------------------------------------------------------------------- MarkMLl · 2021-06-11 20:00 Quote from: rarnu on June 11, 2021, 07:36:43 pmThere is no "ld- linux-x86-64.so.2" in Alpine Linux, even glibc is also not included. Only musl-libc can be used. That's pretty weird, since generally speaking ld-linux-something.so is a prerequisite for a standard .elf file. Without volunteering that I have a solution but to keep the ball rolling, what is the output of this command $ readelf -d `which gcc` MarkMLl ---------------------------------------------------------------------- Fred vS · 2021-06-11 20:00 Hello Ramu. Not sure it could help but what about to use for export code in "mylib" ?: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...exports  add name 'add';  And for fpc command line (added "-fPIC") ?: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc mylib.lpr -fPIC -FL/lib/libc.musl-x86_64.so.1 And in "host" program, to localize the error ?: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ... lib := loadLibrary(libPath); if lib = nilhandle then   WriteLn(libpath + ' not loaded')elsebegin  mAdd := TAdd(GetProcAddress(lib, 'add'));  if assigned(mAdd)  WriteLn(mAdd(1, 2)) else  WriteLn('mAdd  not assigned')end; ---------------------------------------------------------------------- rarnu · 2021-06-12 01:02 Quote from: MarkMLl on June 11, 2021, 10:00:03 pmQuote from: rarnu on June 11, 2021, 07:36:43 pmThere is no "ld-linux-x86-64.so.2" in Alpine Linux, even glibc is also not included. Only musl-libc can be used. That's pretty weird, since generally speaking ld-linux-something.so is a prerequisite for a standard .elf file. Without volunteering that I have a solution but to keep the ball rolling, what is the output of this command $ readelf -d `which gcc` MarkMLl Thank you MarkMLI, here is my command result: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Dynamic section at offset 0xf49f8 contains 21 entries:  Tag        Type                       Name/Value 0x0000000000000001 (NEEDED)             Shared library: [libc.musl-x86_64.so.1] 0x000000000000000c (INIT)             0x403000 0x000000000000000d (FINI)               0x479e6f 0x0000000000000019 (INIT_ARRAY)         0x4f31d0 0x000000000000001b (INIT_ARRAYSZ)       112 (bytes) 0x000000006ffffef5 (GNU_HASH)           0x4002c8 0x0000000000000005 (STRTAB)             0x401090 0x0000000000000006 (SYMTAB)             0x400328 0x000000000000000a (STRSZ)              1097 (bytes) 0x000000000000000b (SYMENT)             24 (bytes) 0x0000000000000015 (DEBUG)              0x0 0x0000000000000003 (PLTGOT)             0x4f5b98 0x0000000000000002 (PLTRELSZ)           3144 (bytes) 0x0000000000000014 (PLTREL)             RELA 0x0000000000000017 (JMPREL)             0x4015b8 0x0000000000000007 (RELA)               0x4014e0 0x0000000000000008 (RELASZ)             216 (bytes) 0x0000000000000009 (RELAENT)            24 (bytes) 0x0000000000000018 (BIND_NOW)            0x000000006ffffffb (FLAGS_1)            Flags: NOW 0x0000000000000000 (NULL)             0x0 ---------------------------------------------------------------------- rarnu · 2021-06-12 01:08 Quote from: Fred vS on June 11, 2021, 10:00:43 pmHello Ramu. Not sure it could help but what about to use for export code in "mylib" ?: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...exports  add name 'add';  And for fpc command line (added "-fPIC") ?: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc mylib.lpr -fPIC -FL/lib/libc.musl-x86_64.so.1 And in "host" program, to localize the error ?: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ... lib := loadLibrary(libPath); if lib = nilhandle then   WriteLn(libpath + ' not loaded')elsebegin  mAdd := TAdd(GetProcAddress(lib, 'add'));  if assigned(mAdd)  WriteLn(mAdd(1, 2)) else  WriteLn('mAdd  not assigned')end; Thank you Fred, I have tried your code. I find the crash happens when calling "loadLibrary", I also tried "SafeLoadLibrary" still not work. And then I tried the C code like: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // add.hint add(int x, int y);  Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // add.c#include "add.h" int add(int x, int y) {  return x + y;}  and compiled with command Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ gcc -shared -o libmylib.so add.c  Now I get a library named "libmylib.so" and this library can be loaded by FPC's "loadLibrary" and it works fine. what's the difference between C's library and FPC's library? ---------------------------------------------------------------------- PierceNg · 2021-06-12 02:28 Quote from: rarnu on June 11, 2021, 07:36:43 pmThere is no "ld- linux-x86-64.so.2" in Alpine Linux, even glibc is also not included. Only musl-libc can be used. There is a libc-compat package on Alpine. All it does is to create a link to musl-libc. I've used this to run Ubuntu-compiled FPC programs on Alpine. Try installing libc-compat package and re-run your program. If that fails, try rebuilding your shared library and program on Ubuntu, say, then run on Alpine with libc-compat installed. If still doesn't work, glibc is also available as an Alpine package. ---------------------------------------------------------------------- rarnu · 2021-06-12 03:49 Quote from: PierceNg on June 12, 2021, 04:28:55 amQuote from: rarnu on June 11, 2021, 07:36:43 pmThere is no "ld-linux-x86-64.so.2" in Alpine Linux, even glibc is also not included. Only musl-libc can be used. There is a libc-compat package on Alpine. All it does is to create a link to musl-libc. I've used this to run Ubuntu-compiled FPC programs on Alpine. Try installing libc-compat package and re-run your program. If that fails, try rebuilding your shared library and program on Ubuntu, say, then run on Alpine with libc-compat installed. If still doesn't work, glibc is also available as an Alpine package. Thank you PierceNg, I have tried to install a compat lib of glibc. here's the results: with official libc6-compat, FPC library can't load, and the app with mysql(needs official libmysqlclient.so) inside works fine. with 3rd-party glibc 2.33, FPC library works fine, but the libmysqlclient.so can't load. I want my FPC library and mysql both work, but ... ---------------------------------------------------------------------- Fred vS · 2021-06-12 04:09 Hello Rarnu. What do you get with this console command to check the methods of the fpc library? Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ nm -D libmylib.so ---------------------------------------------------------------------- rarnu · 2021-06-12 05:09 Quote from: Fred vS on June 12, 2021, 06:09:59 amHello Rarnu. What do you get with this console command to check the methods of the fpc library? Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ nm -D libmylib.so yep, I think this is the key point. FPC's library's exported methods is different with C's Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 00000000000470a0 T add  FPC exports only one method, but C's library exports more: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                w _ITM_deregisterTMCloneTable                 w _ITM_registerTMCloneTable                 w __cxa_finalize               w __deregister_frame_info                 w __register_frame_info0000000000001175 T _fini0000000000001000 T _init000000000000113f T add  here are "_init" and "_fini", I think my lib must have them exported, but how? ---------------------------------------------------------------------- Fred vS · 2021-06-12 12:57 Quotehere are "_init" and "_fini", I think my lib must have them exported, but how? Hum, I think you will need a guru. Maybe you can try to add those "_init" and "_fini" in your lib and export it. https://docs.oracle.com/cd/E88353_01/html/E37854/u-fini-9e.html Sorry, I dont use Alpine so I cannot test. Fre;D ---------------------------------------------------------------------- rarnu · 2021-06-13 02:51 Thank you all for the help. I find the final solution. first of all, official libc6-compat must be installed into Alpine. Do NEVER install the 3rd-party glibc, it will override ld- linux-x86-64.so.2 and break other packages in Alpine because almost all packages in Alpine are built by musl-libc, and the original ld- linux-x86-64.so.2 is a symlink from libc.musl-x86_64.so . so my solution is to remove the dependency of ld-linux-x86-64.so.2 from my lib. use the command for make a like.res: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc -Cn mylib.lpr  It will generate the link.res and ppas.sh. Open link.res, find "INPUT (ld-linux-x86-64.so.2)" and then remove it. run ppas.sh for build a shared library. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ ./ppas.sh  here can get a libmylib.so without ld-linux-x86-64.so.2 required. use "readelf -d libmylib.so" to check it. And one more step is to remove "dynlibs" from host app. In Alpine can't use that. I added a compile switch to it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Classes, SysUtils {$IFNDEF ALPINE}, dynlibs {$ENDIF};  and compile host app with the command: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc -dALPINE host.lpr  well, the problem is solved. ---------------------------------------------------------------------- MarkMLl · 2021-06-13 07:53 Congratulations on fixing it and thanks for keeping us updated. Since what you did involved changing FPC-generated files (i.e. wasn't just a fix to your Alpine configuration) I suggest it would be worthwhile posting it on Mantis so that the core developers are aware... not so much because they're expected to do support what can only be described as a minority platform but in case this could usefully be handled in a more general and widely-applicable way. MarkMLl ---------------------------------------------------------------------- Fred vS · 2021-06-13 10:57 Quote from: rarnu on June 13, 2021, 04:51:36 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Classes, SysUtils {$IFNDEF ALPINE}, dynlibs {$ENDIF};  well, the problem is solved.  Wow, congrats! May I ask you how do you load the library if you dont use dynlibs (LoadLibrary) ? Do you use instead "external" (and then no dynamic loading)? Fre;D ---------------------------------------------------------------------- rarnu · 2021-06-13 16:09 Quote from: Fred vS on June 13, 2021, 12:57:28 pmWow, congrats! May I ask you how do you load the library if you dont use dynlibs (LoadLibrary) ? Do you use instead "external" (and then no dynamic loading)? Fre;D I have not found the way to use a library dynamically. Just the simplest code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program host; {$mode objfpc}{$H+} uses classes, Sysutils{$IFNDEF ALPINE}, dynlibs {$ENDIF}; function add(x: integer; y: integer): integer; cdecl; external 'mylib'; begin  writeln(add(1, 2));end.  and execute it by: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ LD_LIBRARY_PATH=. ./host  or make libmylib.so a symlink to "/usr/lib" is also worked. And, I have tried something else to make it dynamic but failed. here is the code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program host; {$mode objfpc}{$H+} uses Classes, Sysutils, dl; type  TAdd = function (x: Integer; y: Integer): Integer; cdecl; var  libPath: string;  lib: Pointer;  mAdd: TAdd;begin  libPath:= ExtractFilePath(ParamStr(0)) + 'libmylib.so';  lib := dlopen(PChar(libPath), RTLD_NOW);  mAdd := TAdd(dlsym(lib, 'add'));  writeln(mAdd(1, 2));end.  this code works on my ubuntu and manjaro. But on Alpine it crashes when calling "dlopen" and no solution now. on Alpine, even the C code with "dlopen" is also not work. Strange... ---------------------------------------------------------------------- Fred vS · 2021-06-13 16:31 Quote from: rarnu on June 13, 2021, 06:09:26 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program host; {$mode objfpc}{$H+} uses Classes, Sysutils, dl; type  TAdd = function (x: Integer; y: Integer): Integer; cdecl; var  libPath: string;  lib: Pointer;  mAdd: TAdd;begin  libPath:= ExtractFilePath(ParamStr(0)) + 'libmylib.so';  lib := dlopen(PChar(libPath), RTLD_NOW);  mAdd := TAdd(dlsym(lib, 'add'));  writeln(mAdd(1, 2));end.  this code works on my ubuntu and manjaro. But on Alpine it crashes when calling "dlopen" and no solution now. on Alpine, even the C code with "dlopen" is also not work. Strange... Hum, strange indeed. In dl.pas, "dlopen" has this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function dlopen(Name : PChar; Flags : longint) : Pointer; cdecl; external libdl; And libdl is defined as: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const{$ifdef BSD}   // dlopen is in libc on FreeBSD.  LibDL = 'c';{$else}  {$ifdef HAIKU}    LibDL = 'root';  {$else}    LibDL = 'dl';  {$endif}{$endif}         Could you check if there is a /usr/lib/libdl.so in your system? Maybe you will have to create a symlink that point to the installed libdl.so.x of your system. [EDIT] Other maybe: It could be that in Alpine dlopen is part of a other library than libdl.so.x . If it is the case, imho, fpc should add a {$ifdef ALPINE} define (like MarkMLI proposed somebody insinuated). Fre;D ---------------------------------------------------------------------- engkin · 2021-06-13 18:00 Can you share the ppas.sh and link.res produced by FPC on your system? ---------------------------------------------------------------------- MarkMLl · 2021-06-13 18:29 Quote from: Fred vS on June 13, 2021, 06:31:55 pmIf it is the case, imho, fpc should add a {$ifdef ALPINE} define (like MarkMLI proposed). I didn't propose anything! Keep my name out of it!! :-) But more seriously... noting OP's most recent comment, I was tinkering with something a year or so ago which had a frontend/backend architecture connected in /both/ /directions/ by explicitly-loaded dynamic entry points, and having problems on 64-bit Debian. There might, possibly, be a fundamental OS problem lurking, since 32-bit was OK... I didn't report it as a problem pending more investigation and making sure I had exactly the same FPC and library versions. MarkMLl ---------------------------------------------------------------------- rarnu · 2021-06-13 19:05 Quote from: Fred vS on June 13, 2021, 06:31:55 pmCould you check if there is a /usr/lib/libdl.so in your system? Maybe you will have to create a symlink that point to the installed libdl.so.x of your system. [EDIT] Other maybe: It could be that in Alpine dlopen is part of a other library than libdl.so.x . If it is the case, imho, fpc should add a {$ifdef ALPINE} define (like MarkMLI proposed). There is no "libdl.so" or something like that in Alpine, and even no so file contains the dlopen function. I have use these command to find: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # cd /# find . - name "*.so.*" | xargs echo | xargs nm  But I find that there is a "libdl.a" under /usr/lib, and I tried this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program host; {$mode objfpc}{$H+}{$linklib /usr/lib/libdl.a} uses classes, Sysutils; function dlopen(Name : PChar; Flags : longint) : Pointer; cdecl; external;function dlsym(Lib : Pointer; Name : Pchar) : Pointer; cdecl; external; type  TAdd = function (x: Integer; y: Integer): Integer; cdecl; var  libPath: string;  lib: Pointer;  mAdd: TAdd;begin  libPath:= ExtractFilePath(ParamStr(0)) + 'libmylib.so';  lib := dlopen(PChar(libPath), 2);  mAdd := TAdd(dlsym(lib, 'add'));  writeln(mAdd(1, 2));end.  still crash when calling dlopen. It seems that dlopen is not contains in that static library (many many libraries in Alpine has no symbol exported, seems stripped). and the same C code is here, can't work for the same reason Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #include #include  #define LIB_PATH "./libmylib.so" typedef int (*add) (int, int); int main() {  void* handle = dlopen(LIB_PATH, RTLD_NOW);  add mAdd = (add)dlsym(handle, "add");  int n = mAdd(1, 2);  printf("%d\n", n);  return 0;}  build executable with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ gcc host.c -ldl  the command executed successfully and generated a.out . Then, I use ldd for checking the libraries, both FPC & C executables returns the same: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i /lib/ld- musl-x86_64.so.1 (0x7f6e73691000)libc.musl-x86_64.so.1 => /lib/ld- musl-x86_64.so.1 (0x7f6e73691000)  checked ld-musl-x86_64.so.1 and no dlopen in it. ---------------------------------------------------------------------- rarnu · 2021-06-13 19:10 Quote from: engkin on June 13, 2021, 08:00:23 pmCan you share the ppas.sh and link.res produced by FPC on your system? yes, attached. ---------------------------------------------------------------------- rarnu · 2021-06-13 19:21 And up to now, I have recompiled FPC 3.3.1 from SVN under Alpine 3.12 But the "dynamic load" problem is still. ---------------------------------------------------------------------- Fred vS · 2021-06-13 19:53 Hello. I am not (yet) a Alpine guru, but there it seems that dlopen is in package libtool: https://pkgs.alpinelinux.org/contents?file=&path=&name=libtool&branch= edge&repo=main&arch=x86 Quote/usr/share/libtool/loaders/dlopen.c ---------------------------------------------------------------------- Fred vS · 2021-06-13 20:23 Re-hello. I think that dlopen() is in libltdl.so library. https://pkgs.alpinelinux.org/package/edge/main/x86/libltdl Maybe you  may create a a symlink /usr/lib/libdl.so > /usr/lib/libltdl.so (or where is the library). ---------------------------------------------------------------------- engkin · 2021-06-13 21:03 In reply #4 you indicated that the problem is in the library generated by FPC because a gcc generated library did work: Quote from: rarnu on June 12, 2021, 03:08:25 amNow I get a library named "libmylib.so" and this library can be loaded by FPC's "loadLibrary" and it works fine. what's the difference between C's library and FPC's library? Can you share the output of: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i gcc -v -shared -o libmylib.so add.c and the assembly files generated from FPC when you add -al switch. Edit: I noticed from the files you shared the code is stripped. Probably it is a good idea to deactivate stripping for now: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .../usr/bin/strip --discard-all --strip-debug libmylib.so... Also, while it probably will not work, try to remove the initialization call: Quote/usr/bin/ld -b elf64-x86-64 -m elf_x86_64  -init FPC_SHARED_LIB_START -fini FPC_LIB_EXIT -soname libmylib.so   -shared  -L. -o libmylib.so -T link4764.res ---------------------------------------------------------------------- rarnu · 2021-06-14 03:05 Quote from: engkin on June 13, 2021, 11:03:00 pmIn reply #4 you indicated that the problem is in the library generated by FPC because a gcc generated library did work: Quote from: rarnu on June 12, 2021, 03:08:25 amNow I get a library named "libmylib.so" and this library can be loaded by FPC's "loadLibrary" and it works fine. what's the difference between C's library and FPC's library? Can you share the output of: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i gcc -v -shared -o libmylib.so add.c and the assembly files generated from FPC when you add -al switch. Edit: I noticed from the files you shared the code is stripped. Probably it is a good idea to deactivate stripping for now: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .../usr/bin/strip --discard-all --strip-debug libmylib.so... Also, while it probably will not work, try to remove the initialization call: Quote/usr/bin/ld -b elf64-x86-64 -m elf_x86_64  -init FPC_SHARED_LIB_START -fini FPC_LIB_EXIT -soname libmylib.so   -shared  -L. -o libmylib.so -T link4764.res Thank you engkin, the problem of static load is solved by remove ld- linux-x86-64.so.2 from link.res. and as you say, remove FPC's init and fini will be more friendly for C callers. I will do that then. result of command "gcc -v -shared -o libmylib.so add.c" is here: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Using built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86 _64-alpine-linux-musl/9.3.0/lto-wrapperTarget: x86_64-alpine-linux- muslConfigured with: /home/buildozer/aports/main/gcc/src/gcc-9.3.0/configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --build=x86_64-alpine-linux-musl --host=x86_64-alpine-linux-musl --target=x86_64-alpine-linux-musl --with-pkgversion='Alpine 9.3.0' --enable-checking=release --disable-fixed-point --disable-libstdcxx- pch --disable-multilib --disable-nls --disable-werror --disable- symvers --enable-__cxa_atexit --enable-default-pie --enable-default- ssp --enable-cloog-backend --enable-languages=c,c++,d,objc,fortran,ada --disable-libssp --disable-libmpx --disable-libmudflap --disable- libsanitizer --enable-shared --enable-threads --enable-tls --with- system-zlib --with-linker-hash-style=gnuThread model: posixgcc version 9.3.0 (Alpine 9.3.0) COLLECT_GCC_OPTIONS='-v' '-shared' '-o' 'libmylib.so' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-alpine-linux-musl/9.3.0/cc1 -quiet -v add.c -quiet -dumpbase add.c -mtune=generic -march=x86-64 -auxbase add -version -o /tmp/ccpbCFBd.sGNU C17 (Alpine 9.3.0) version 9.3.0 (x86_64-alpine-linux-musl)        compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.18-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072ignoring nonexistent directory "/usr/local/include"ignoring nonexistent directory "/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../../x86_64-alpine-linux-musl/include"#include "..." search starts here:#include <...> search starts here: /usr/include/fortify /usr/include /usr/lib/gcc/x86_64-alpine- linux-musl/9.3.0/includeEnd of search list.GNU C17 (Alpine 9.3.0) version 9.3.0 (x86_64-alpine-linux-musl)        compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.18-GMP GGC heuristics: --param ggc-min- expand=100 --param ggc-min-heapsize=131072Compiler executable checksum: 501c05d615d8f797a5937479b6573636COLLECT_GCC_OPTIONS='-v' '-shared' '-o' 'libmylib.so' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../../x86_64-alpine-linux-musl/bin/as -v --64 -o /tmp/ccbClpjJ.o /tmp/ccpbCFBd.sGNU assembler version 2.34 (x86_64-alpine-linux-musl) using BFD version (GNU Binutils) 2.34COMPILER_PATH=/usr/libexec/gcc/x86_64-alpine-linux- musl/9.3.0/:/usr/libexec/gcc/x86_64-alpine-linux- musl/9.3.0/:/usr/libexec/gcc/x86_64-alpine-linux- musl/:/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/:/usr/lib/gcc/x86_64-alpine-linux- musl/:/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../../x86_64-alpine-linux- musl/bin/LIBRARY_PATH=/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/:/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../../x86_64-alpine-linux- musl/lib/../lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../ ../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-alpine- linux-musl/9.3.0/../../../../x86_64-alpine-linux- musl/lib/:/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../:/lib/:/usr/lib/COLLECT_GCC_OPTIONS='-v' '-shared' '-o' 'libmylib.so' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-alpine-linux- musl/9.3.0/collect2 -plugin /usr/libexec/gcc/x86_64-alpine-linux- musl/9.3.0/liblto_plugin.so -plugin- opt=/usr/libexec/gcc/x86_64-alpine-linux-musl/9.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccPjfibM.res -plugin-opt=-pass- through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass- through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass- through=-lgcc_s --eh-frame-hdr --hash-style=gnu -m elf_x86_64 --as- needed -shared -z relro -z now -o libmylib.so /usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../../lib/crti.o /usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/crtbeginS.o -L/usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0 -L/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../../x86_64-alpine-linux-musl/lib/../lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../../x86_64-alpine-linux-musl/lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../.. /tmp/ccbClpjJ.o -lssp_nonshared -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/crtendS.o /usr/lib/gcc/x86_64-alpine-linux- musl/9.3.0/../../../../lib/crtn.oCOLLECT_GCC_OPTIONS='-v' '-shared' '-o' 'libmylib.so' '-mtune=generic' '-march=x86-64' ---------------------------------------------------------------------- rarnu · 2021-06-14 03:10 Quote from: Fred vS on June 13, 2021, 10:23:25 pmRe-hello. I think that dlopen() is in libltdl.so library. https://pkgs.alpinelinux.org/package/edge/main/x86/libltdl Maybe you  may create a a symlink /usr/lib/libdl.so > /usr/lib/libltdl.so (or where is the library). Yes, there is a "lt_dlopen" in libltdl.so, in "ltdl.h" there are some exports: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i LT_SCOPE lt_dlhandle lt_dlopen          (const char *filename);           LT_SCOPE lt_dlhandle lt_dlopenext       (const char *filename);        LT_SCOPE lt_dlhandle lt_dlopenadvise    (const char *filename,  lt_dladvise advise);          LT_SCOPE void *     lt_dlsym            (lt_dlhandle handle, const char *name);LT_SCOPE const char *lt_dlerror         (void);                                LT_SCOPE int        lt_dlclose          (lt_dlhandle handle);             here is my C code with ltdl: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #include #include  #define LIB_PATH "./libmylib.so" typedef int (*add) (int, int); int main() {  lt_dlinit();  lt_dlhandle handle = lt_dlopen(LIB_PATH);  add mAdd = (add)lt_dlsym(handle, "add");  int n = mAdd(1, 2);  printf("%d\n", n);  return 0;}  compiled with: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ gcc host.c -lltdl  if the so is compiled by C, it works, and if not, it crashes. ---------------------------------------------------------------------- rarnu · 2021-06-14 06:36 Again, thanks for all helps. Now I find the final worked solution. here all the code must be compiled under Alpine, no other linux. the lib's code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i library mylib; {$mode objfpc}{$H+} uses Classes, Sysutils; function add(x: integer; y: integer): integer; cdecl;begin  Exit(x + y);end; exports  add; end.  compile it with command: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc -Cn mylib.lpr  remove "ld-linux-x86-64.so.2" in link.res or linkxxx.res open "ppas.sh" and change the /usr/bin/ld line to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i /usr/bin/ld -b elf64-x86-64 -m elf_x86_64 -soname libmylib.so   -shared  -L. -o libmylib.so -T link.res  now execute ppas.sh and get the libmylib.so ---------------------------------------------------------------------- --- here's my host app's code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program host; {$mode objfpc}{$H+} {$linklib ltdl}{$linklib c} uses classes, Sysutils; const  LIB_PATH = './libmylib.so'; function lt_dlinit(): integer; cdecl; external;function lt_dlopen(const filename : PChar) : Pointer; cdecl; external;function lt_dlsym(Lib : Pointer; const name : Pchar) : Pointer; cdecl; external; type  TAdd = function (x: Integer; y: Integer): Integer; cdecl; var  r: integer;  lib: Pointer;  mAdd: TAdd;begin  lt_dlinit();  lib := lt_dlopen(LIB_PATH);  mAdd := TAdd(lt_dlsym(lib, 'add'));  writeln(mAdd(1, 2));end.  compile it with command: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc -Cn host.lpr  open "ppas.sh" and change the param "--dynamic-linker" from "ld- linux-x86-64.so.2" to "ld-musl-x86_64.so.1" now execute ppas.sh and get host executable. ---------------------------------------------------------------------- --- that's all, now I can run host to load libmylib.so dynamically. and the previous way for static load is also worked. Thanks you all, a lot. ---------------------------------------------------------------------- Fred vS · 2021-06-14 11:43 Hello Rarnu. Wow, congrats and thanks for your perseverance (I love 'no giving-up' people'). Your last post should be transferred into a fpc wiki page. Fre;D ---------------------------------------------------------------------- MarkMLl · 2021-06-14 13:08 Quote from: Fred vS on June 14, 2021, 01:43:38 pmWow, congrats and thanks for your perseverance (I love 'no giving-up' people'). I agree. However I think there's a lesson to be learned here: some of the initial problems were caused by a demi-borked system, and assuming that the distro maintainers are competent that can only have been caused by a part-successful installation of an "alien" package. Assuming that's correct, I'd mention that it's the second problem of this nature in a week: I'd suggest that however much we are repelled by the "string and chewinggum" implementation of Docker it really is quite serviceable at providing a sandbox in which slightly-suspect apps can be run. MarkMLl