[fpc-devel]Library linking with C explanation

Sergey Korshunoff Sergey.Korshunoff at p5.f2666.n5020.z2.fidonet.org
Wed Jul 4 00:14:21 CEST 2001


SK> Are You shure, that INIT function for *.so can get this args?
SK> I look sources from ld-linux.so. As I understand, this args are
SK> send to global constructors and not to *.so INIT function.

SK> I examine DCC behavior.
SK> There is no ParamCount and ParamStr avaible for library functions
SK> ( ParamCount result in DCC compiled example is 0).
SK> And GetEnvironmentVariable() in DCC use function from libc

SK> There is error in FPC 1.0.5 ParamCount:
SK>   it result is -1 when argc is 0

I discover how to init U_SYSLINUX_ENVP in dllprt0.as.

  xorl %eax, %eax
  movl %eax,U_SYSLINUX_ARGC
  movl %eax,U_SYSLINUX_ARGV

  movl _environ, %eax          # defined in ld-linux.so.2
  movl %eax,U_SYSLINUX_ENVP

Test program:
=================================
library p1;

uses
   SysUtils;

procedure pas1;cdecl;
begin
   writeln('Hello world!');
   Writeln('Home: ',GetEnvironmentVariable('HOME'));
end;

exports
   pas1;

begin
   writeln('Lib started!');
   writeln('ParamCount = ', ParamCount);  (* RTL error !!!: must be 0 *)
   (* writeln('ParamStr(0) = ', ParamStr(0)); *)
   (* RTL error !!!: ParamStr is trapped for argc = 0 *)
end.
=================================

Output of FPC made test program:
 Lib started!
 ParamCount = -1
 Home: /root

Output of KYLIX made test program:
 Lib started!
 ParamCount = 0
 Home: /root

And to allow FPC made libxxx.so work with FPC made main program
there is a need a patch for prt0.as:
        movl    %eax,U_SYSLINUX_ENVP /* Move the environment pointer */
+       movl    %eax,_environ

.................

+        .globl  _environ
+        .type   _environ, at object
+        .size   _environ,4
+_environ:
+        .long   0

And patch for dllprt0.as:
-        call    PASCALMAIN
+        call    LIB_PASCALMAIN

And in ld call we need to add a parm
--defsym LIB_PASCALMAIN=P1_main
for libp1.

Without this changes FPC made main program can not use
FPC made shared library (this is for linux):
   call    PASCALMAIN
from dllprt0.as resolves to FPC made main program symbol and
P1_main do not get control.

Regards,
     Sergey Korshunoff





More information about the fpc-devel mailing list