Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2001-07

Library linking with C explanation

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sergey Korshunoff

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,@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