Loading pascal DSO is broken in musl

Original Reporter info from Mantis: volo-zyko
  • Reporter name: Volo

Description:

When building inside this docker container https://hub.docker.com/r/megahertz/fpc-trunk/ with the latest FPC I get a broken DSO. If the same code is run without using DSO then it works as expected.

Steps to reproduce:

  1. I have an example Pascal code for creating DSO - subs.pas:

&LtPos;code pascal>
library subs;

function SubStr(CString: PChar; FromPos,ToPos: Longint): PChar; cdecl;
var
  Length: Integer;
begin
  Length := StrLen(CString);
  SubStr := CString + Length;
  if (FromPos > 0) and (ToPos >= FromPos) then
  begin
    if Length >= FromPos then
      SubStr := CString + FromPos - 1;
    if Length > ToPos then
    CString[ToPos] := #0;
  end;
end;

function Sum(A1, A2: Longint): Longint; cdecl;
begin
  Sum := A1 + A2;
end;

exports
  SubStr,
  Sum;

end.
</code>

It compiles with:
%%fpc -Tlinux -Px86_64 -Sd -fPIC -FL/lib/ld-musl-x86_64.so.1 -g subs.pas%%

I have this simplistic code:

<code pascal>
program test;

function SubStr(CString: PChar; FromPos,ToPos: Longint): PChar; cdecl; external 'libsubs.so' name 'SubStr';
function Sum(A1, A2: Longint): Longint; cdecl; external 'libsubs.so' name 'Sum';

var
    arg: PChar;

begin
    writeln('begin');
    writeln(Sum(11, 2));
    arg := '12345678';
    writeln(SubStr(arg, 1, 2));
    writeln('end');
end.
</code>

It compiles with:
%%fpc -Tlinux -Px86_64 -g -Sd -FL/lib/ld-musl-x86_64.so.1 -k-rpath='$ORIGIN' test.pas%%

Now when I run the test it repeats printing %%Runtime error 216 at $00007F4AB909FB40%% until it crashes.

Additional information:

If I add %%writeln%% inside any of the functions the called function crashes immediately.

Mantis conversion info:

  • Mantis ID: 36730
  • OS: Alpine
  • OS Build: 3.10
  • Build: 3.3.1 [2019/12/02] for x86_64
  • Platform: Linux
  • Version: 3.3.1