[Mono-list] passing and returning structs in native calls

Robert Jordan robertj at gmx.net
Mon Jan 10 11:02:43 EST 2011


On 10.01.2011 14:07, Edd Barrett wrote:
> Hi,
>
> On Fri, Jan 07, 2011 at 04:55:22PM +0100, Robert Jordan wrote:
>> On 07.01.2011 16:19, Edd Barrett wrote:
>>> Why is this? Myself and a colleague have spent a number of hours trying
>>> to work this out.
>>
>> Because returning structs by value from functions is problematic
>> in general. Try to stay away from such signatures if you want
>> portability.
>
> I have my bindings working now, but this is interesting...
>
> After some consideration, I see that C compilers have to do magic in order to
> make it appear as though functions are returning structs bigger than the return
> register size.
>
> What confuses me however, is that whatever transformation tha occurs
> here *must* be standardised (atleast on a single compiler), as the very
> same transformation needs to be understood by the runtime linker when
> setting up the execution image of binaries using shared objects.

The System V AMD64 ABI does not specify any symbol mangling regarding
parameters and return values for C and extern "C" functions, so the
linker is out of the equation because it doesn't have enough information
to enforce a correct binding.

> Is this "understanding" of the ABI just not implemented in mono, or is there a
> further complication. I notice the same issue in python.

Odds are that the issue is *BSD related. Under Linux/amd64 your small
struct is returned correctly in RAX. This is the correct behaviour,
if I understand the specs[1] correctly.

Mono even has a test case for small structs in tests/pinvoke11.cs.

Robert

[1] http://www.x86-64.org/documentation/abi.pdf



More information about the Mono-list mailing list