[fpc-devel] Safecall on Linux (and other unices?)

Joost van der Sluis joost at cnoc.nl
Wed Jul 21 12:46:32 CEST 2010

On Wed, 2010-07-21 at 11:58 +0200, José Mejuto wrote:

> Wednesday, July 21, 2010, 11:28:55 AM, you wrote:
> JvdS> I've finally managed to get xpcom/gecko to work (partly) on linux/i386.
> JvdS> The delphi/fpc gecko-headers uses the safecall calling convention a
> JvdS> lot. 
> I get it to work too, but missing safecall (unix version) makes it
> unusable as a lot of assembler is needed.

That's not true. I managed to get it to work without any assembler.
(Only used it to debug and some quick tests)

What you have to do is change definitions like:

function Bla(test: tsometype) : tresulttype; safecall;


function Bla(testl: tsometype; _result: tresulttype) : nsresult;

And define the extdecl macro to stdcall on Windows and cdecl on Linux.
Set the nsresult to the right error code to simulate an exception.

> JvdS> On Windows a safecall is in principle a stdcall with a 'hidden' function
> JvdS> result which contains a value (nsresult) to check if there was any
> JvdS> problem during the call of the procedure/function. 
> JvdS> I had to find out what calling convention is used by xpcom on Linux. And
> JvdS> in fact it's quite logical: it uses cdecl. (As most c-libraries do, use
> JvdS> stdcall on Windows, cdecl on others)
> It is not 100% cdecl, const records in safecall must be passed as
> reference while in cdecl a copy of the record is being passed.

I don't think so. The gecko-port-definitions are wrong in this regard.
They use 'const' but it should be 'var'. That this worked on Windows was
just a coincidence. I've replaced these 'const' parameter definitions to
'var' and now they work on Linux and Windows. The headers just weren't
translated right in this regard.

> Please check my bug report:
> http://bugs.freepascal.org/view.php?id=16685 and some messages in
> mailing list:

> http://lists.freepascal.org/lists/fpc-pascal/2010-June/025605.html
> http://lists.freepascal.org/lists/fpc-pascal/2010-June/025609.html
> http://lists.freepascal.org/lists/fpc-pascal/2010-June/025613.html
> http://lists.freepascal.org/lists/fpc-pascal/2010-June/025615.html

That was a good starting point, to find out what the problem was
precisely. Jonas last comment was where I started. But enabling the
exception-handling wasn't enough. Using Safecall on Linut, the caller
has to clean up the stack (on windows the callee does). I don't know yet
how to the latter. 

I don't think the parameter-passing should be changed. Just don't use

> In the other side, I was unable to make Gecko work correctly (windows)
> using Gecko > all 1.9.2.x versions raise a bunch of
> exceptions mostly related to Javascript :( I can run it removing some
> Javascript initialization scripts and disabling Javascript once
> loaded.

I don't know which version I used. Probably the one from the latest
Firefox release. I'll check at home. (tomorrow)


More information about the fpc-devel mailing list