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:
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:
remove "ld-linux-x86-64.so.2" in link.res or linkxxx.res
open "ppas.sh" and change the /usr/bin/ld line to:
/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:
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:
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.