Shared libraries problems on FreeBSD
Original Reporter info from Mantis: kaukas
-
Reporter name:
Original Reporter info from Mantis: kaukas
- Reporter name:
Description:
Shared libraries built with fpc on fbsd has these problems:
- Does not load if there is no exported dummy "main" function.
- Internal PASCALMAIN function is not called on loading, thus code in units initialization/finalization is also never called.
1st problem can be solved by making empty procedure called "main" and exporting it. But this is not normal behavior. See at still not resolved bug: 0007833.
2nd problem can be hacked to. Example:
library BsdLib;
{$IFDEF FPC}
{$MODE ObjFpc}{$H+}
{$ENDIF}
uses
SysUtils,
Classes;
{$IFDEF FREEBSD}
var
initialstkptr: Pointer; external name '__stkptr';
operatingsystem_parameter_argc: PtrUint; external name 'operatingsystem_parameter_argc';
operatingsystem_parameter_argv: PPChar; external name 'operatingsystem_parameter_argv';
operatingsystem_parameter_envp: PPChar; external name 'operatingsystem_parameter_envp';
{$ENDIF}
// FreeBSD library hack
{$IFDEF FREEBSD}
procedure PASCALMAIN; external name 'PASCALMAIN';
{$asmmode ATT}
function get1eipasebx: Pointer; compilerproc; nostackframe; assembler;
asm
movl (%esp), %ebx
ret
end;
procedure _FPC_LIB_START(argc: DWORD; argv, envp: Pointer); cdecl public
name 'FPC_LIB_START'; //public name '_start';
begin
// These not working... Maybe freebsd specific bug.
{ operatingsystem_parameter_argc := argc;
operatingsystem_parameter_argv := argv;
operatingsystem_parameter_envp := envp;}
IsLibrary := True;
asm
{ Save initial stackpointer }
{$IFDEF FPC_PIC}
call get1eipasebx
addl $_GLOBAL_OFFSET_TABLE_, %ebx
movl initialstkptr@GOT(%ebc), %ecx
movl %esp, (%ecx)
{$ELSE}
movl %esp, initialstkptr
{$ENDIF}
end;
PASCALMAIN;
end;
procedure main;
begin
end;
exports
main;
{$ENDIF}
begin
// code here executes ok.
end.
This hack is copied from rtl/linux/i386/si_dll.inc. The interesting thing in bsd is that there linker -init parameter is called with no existent function "FPC_LIB_START". I think the best would be to introduce dllprt as in linux. I don't know much about fpc internals so it would be better if someone maintaining bsd part could look at this.
Mantis conversion info:
- Mantis ID: 15422
- OS: FreeBSD
- Version: 2.5.1