Sign in Join

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

Mailing lists / fpc-pascal / 2015-02

linking and using dynamic libraries on amd64

11 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Marc Santhoff

Hi,

when I try to run a program that dynamically links an external .so library it throws a SIGSEGV seems to happen while loading the library before any code of mine is exeuted.

The library is pulled into it by a header translation from C to Pascal. The functions are declared "...; stdcall; external libname name xxx'. This way it worked on i386.

When the error occurs the ASM window pops up, which is not very helpful for me.

There may be a minor change in the lib source itself, but compiling the header and linking works OK. I have no idea how to check besides getting the older versions source and try again. Will try if nothing else is fishy.

My questions:

Is there any difference in declaring external functions between 32 and 64 bit mode?

How can I debug what is happening? Will compiling the library having an external debug file work when debugging from Lazarus?

Any hints for diagnosing what's going wrong are very welcome.

Some data points:
fpc 2.6.2 x86_64
library is compiled to 64 bit
FreeBSD 9.1-STABLE amd64

Best regards,
Marc

Marc Santhoff

Reply by email

Marc Santhoff
to Marc Santhoff
Marc Santhoff wrote 17 lines
Hi, when I try to run a program that dynamically links an external .so library it throws a SIGSEGV seems to happen while loading the library before any code of mine is exeuted. The library is pulled into it by a header translation from C to Pascal. The functions are declared "...; stdcall; external libname name xxx'. This way it worked on i386. When the error occurs the ASM window pops up, which is not very helpful for me. There may be a minor change in the lib source itself, but compiling the header and linking works OK. I have no idea how to check besides getting the older versions source and try again. Will try if nothing else is fishy.

I did test the old library and the error is the same.

GDB tells me it happens when starting at BEGIN and running the init function of the library. That function does not have any arguments, I'm assuming "something else" then ...

Show quoted text 20 lines
My questions: Is there any difference in declaring external functions between 32 and 64 bit mode? How can I debug what is happening? Will compiling the library having an external debug file work when debugging from Lazarus? Any hints for diagnosing what's going wrong are very welcome. Some data points: fpc 2.6.2 x86_64 library is compiled to 64 bit FreeBSD 9.1-STABLE amd64 Best regards, Marc
Marc Santhoff

Reply by email

Tony Whyman
to Marc Santhoff

Marc,

If your running on a *nix system then you probably want "cdecl" rather than "stdcall"

Regards

Tony Whyman
MWA

Marc Santhoff wrote 44 lines
On Do, 2015-02-12 at 00:42 +0100, Marc Santhoff wrote: > Hi, > > when I try to run a program that dynamically links an external .so > library it throws a SIGSEGV seems to happen while > loading the library before any code of mine is exeuted. > > The library is pulled into it by a header translation from C to Pascal. > The functions are declared "...; stdcall; external libname name xxx'. > This way it worked on i386. > > When the error occurs the ASM window pops up, which is not very helpful > for me. > > There may be a minor change in the lib source itself, but compiling the > header and linking works OK. I have no idea how to check besides getting > the older versions source and try again. Will try if nothing else is > fishy. I did test the old library and the error is the same. GDB tells me it happens when starting at BEGIN and running the init function of the library. That function does not have any arguments, I'm assuming "something else" then ... > My questions: > > Is there any difference in declaring external functions between 32 and > 64 bit mode? > > How can I debug what is happening? Will compiling the library having an > external debug file work when debugging from Lazarus? > > Any hints for diagnosing what's going wrong are very welcome. > > > > Some data points: > fpc 2.6.2 x86_64 > library is compiled to 64 bit > FreeBSD 9.1-STABLE amd64 > > Best regards, > Marc >

Reply by email

Tony Whyman
to Marc Santhoff: I did test the old library and the error is the same. GDB tells me it happens when…

Marc,

If your running on a *nix system then you probably want "cdecl" rather than "stdcall"

Regards

Tony Whyman
MWA

Marc Santhoff wrote 44 lines
On Do, 2015-02-12 at 00:42 +0100, Marc Santhoff wrote: > Hi, > > when I try to run a program that dynamically links an external .so > library it throws a SIGSEGV seems to happen while > loading the library before any code of mine is exeuted. > > The library is pulled into it by a header translation from C to Pascal. > The functions are declared "...; stdcall; external libname name xxx'. > This way it worked on i386. > > When the error occurs the ASM window pops up, which is not very helpful > for me. > > There may be a minor change in the lib source itself, but compiling the > header and linking works OK. I have no idea how to check besides getting > the older versions source and try again. Will try if nothing else is > fishy. I did test the old library and the error is the same. GDB tells me it happens when starting at BEGIN and running the init function of the library. That function does not have any arguments, I'm assuming "something else" then ... > My questions: > > Is there any difference in declaring external functions between 32 and > 64 bit mode? > > How can I debug what is happening? Will compiling the library having an > external debug file work when debugging from Lazarus? > > Any hints for diagnosing what's going wrong are very welcome. > > > > Some data points: > fpc 2.6.2 x86_64 > library is compiled to 64 bit > FreeBSD 9.1-STABLE amd64 > > Best regards, > Marc >

Reply by email

Marc Santhoff
to Tony Whyman
Tony Whyman wrote
Marc, If your running on a *nix system then you probably want "cdecl" rather than "stdcall"

Err ... you're right and it is cdecl. I have mixed it up because it is an $if_n_def ...:

...;{$ifndef Win32}cdecl{$else}stdcall{$endif};external libname name 'xxx';

If I find the time I'll go on and investigate the exact position the error occurs. Maybe it is possible to build it with debugging symbols, in code I have to find a way throug the macro djungle ... one reason to use Object Pascal.

Thanks for waking me up. ;)

Marc

Show quoted text 49 lines
On 12/02/15 04:54, Marc Santhoff wrote: > On Do, 2015-02-12 at 00:42 +0100, Marc Santhoff wrote: >> Hi, >> >> when I try to run a program that dynamically links an external .so >> library it throws a SIGSEGV seems to happen while >> loading the library before any code of mine is exeuted. >> >> The library is pulled into it by a header translation from C to Pascal. >> The functions are declared "...; stdcall; external libname name xxx'. >> This way it worked on i386. >> >> When the error occurs the ASM window pops up, which is not very helpful >> for me. >> >> There may be a minor change in the lib source itself, but compiling the >> header and linking works OK. I have no idea how to check besides getting >> the older versions source and try again. Will try if nothing else is >> fishy. > I did test the old library and the error is the same. > > GDB tells me it happens when starting at BEGIN and running the init function > of the library. That function does not have any arguments, I'm assuming > "something else" then ... > >> My questions: >> >> Is there any difference in declaring external functions between 32 and >> 64 bit mode? >> >> How can I debug what is happening? Will compiling the library having an >> external debug file work when debugging from Lazarus? >> >> Any hints for diagnosing what's going wrong are very welcome. >> >> >> >> Some data points: >> fpc 2.6.2 x86_64 >> library is compiled to 64 bit >> FreeBSD 9.1-STABLE amd64 >> >> Best regards, >> Marc >> _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Marc Santhoff

Reply by email

Tony Whyman
to Marc Santhoff: Err ... you're right and it is cdecl. I have mixed it up because it is an $if_n_def ...:…

A pity, because that would have been an easy fix. This type of error comes from either mis-matched calling conventions or incompatible argument lists.

From having done this sort of thing before all I can advise is to go through each function argument one by one and make sure that each argument type is exactly the same in terms of data representation. When going between 32 and 64 bit you can often find that there are subtle differences in how the different compilers treat types resulting in a type that might be compatible in the 32 bit world, ceasing to be compatible in the 64bit world.

Also: you are using a 'C' library and not a C++ library aren't you?

Marc Santhoff wrote 67 lines
On Do, 2015-02-12 at 09:03 +0000, Tony Whyman wrote: > Marc, > > If your running on a *nix system then you probably want "cdecl" rather > than "stdcall" Err ... you're right and it is cdecl. I have mixed it up because it is an $if_n_def ...: ...;{$ifndef Win32}cdecl{$else}stdcall{$endif};external libname name 'xxx'; If I find the time I'll go on and investigate the exact position the error occurs. Maybe it is possible to build it with debugging symbols, in code I have to find a way throug the macro djungle ... one reason to use Object Pascal. Thanks for waking me up. ;) Marc > On 12/02/15 04:54, Marc Santhoff wrote: >> On Do, 2015-02-12 at 00:42 +0100, Marc Santhoff wrote: >>> Hi, >>> >>> when I try to run a program that dynamically links an external .so >>> library it throws a SIGSEGV seems to happen while >>> loading the library before any code of mine is exeuted. >>> >>> The library is pulled into it by a header translation from C to Pascal. >>> The functions are declared "...; stdcall; external libname name xxx'. >>> This way it worked on i386. >>> >>> When the error occurs the ASM window pops up, which is not very helpful >>> for me. >>> >>> There may be a minor change in the lib source itself, but compiling the >>> header and linking works OK. I have no idea how to check besides getting >>> the older versions source and try again. Will try if nothing else is >>> fishy. >> I did test the old library and the error is the same. >> >> GDB tells me it happens when starting at BEGIN and running the init function >> of the library. That function does not have any arguments, I'm assuming >> "something else" then ... >> >>> My questions: >>> >>> Is there any difference in declaring external functions between 32 and >>> 64 bit mode? >>> >>> How can I debug what is happening? Will compiling the library having an >>> external debug file work when debugging from Lazarus? >>> >>> Any hints for diagnosing what's going wrong are very welcome. >>> >>> >>> >>> Some data points: >>> fpc 2.6.2 x86_64 >>> library is compiled to 64 bit >>> FreeBSD 9.1-STABLE amd64 >>> >>> Best regards, >>> Marc >>> > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Marc Santhoff
to Tony Whyman: A pity, because that would have been an easy fix. This type of error comes from either…
Tony Whyman wrote
A pity, because that would have been an easy fix. This type of error comes from either mis-matched calling conventions or incompatible argument lists.

No easy solution in sight...

Show quoted text 7 lines
>From having done this sort of thing before all I can advise is to go through each function argument one by one and make sure that each argument type is exactly the same in terms of data representation. When going between 32 and 64 bit you can often find that there are subtle differences in how the different compilers treat types resulting in a type that might be compatible in the 32 bit world, ceasing to be compatible in the 64bit world.

I already started checking type sizes, but looking at the code is very tough because of heavy use of macros.

Hopefully I can tweak those autoxxx-stuff into building a debug version of the library. That should help in getting to know what exactly happens where.

If that fails it'll be the time to polish some preprocessor tricks.

Also: you are using a 'C' library and not a C++ library aren't you?

Yes.

Thanks so far.

Marc Santhoff

Reply by email

Marc Santhoff
to Jonas Maebe: Always use the latest FPC release, in this case 2.6.4. I think even some x86-64 calling…
Jonas Maebe wrote 6 lines
On 12/02/15 00:42, Marc Santhoff wrote: > Some data points: > fpc 2.6.2 x86_64 Always use the latest FPC release, in this case 2.6.4. I think even some x86-64 calling convention issues were fixed between 2.6.2 and 2.6.4.

Wow, completely building a program (clean, compile, link) is a lot faster than on 2.6.2 running on FreeBSD 9. :)

Marc Santhoff

Reply by email