Bus error interfacing with C function returning large struct

Jacob Carlborg doob at me.com
Tue Apr 16 23:37:40 PDT 2013


On 2013-04-17 01:03, John Colvin wrote:

> Some observations:
>
> Assuming main is doing everything properly, it's passing a pointer to 12
> bytes of stack space to bar in eax (as per the D ABI). bar then puts
> that pointer on the stack for foo (as per the IA32 OS X ABI). However,
> it looks to me like it's in the wrong place, because of this line:
> 0x00002673 <D4test3barFZS4test3Foo+11>: sub    $0x8,%esp
>
> This is just from a quick glance, I may have added my hexes wrongly.

I don't know, that's why I'm asking here :) This is what Martin Nowak 
said in the bug report:

"Seems like OSX deviates from the SysV IA-32 ABI for memory struct 
returns. The callee does NOT return the hidden pointer in EAX.
Instead the caller has to use the value passed as argument."

http://d.puremagic.com/issues/show_bug.cgi?id=9931#c7

The ABI documentation says:

"When a function returns a structure or union larger than 8 bytes, the 
caller passes a pointer to appropriate storage as the first argument to 
the function."

And:

"The called function returns structures according to their aligned size.

* Structures 1 or 2 bytes in size are placed in EAX.
* Structures 4 or 8 bytes in size are placed in: EAX and EDX.
* Structures of other sizes are placed at the address supplied by the 
caller. For example, the C++ language occasionally forces the compiler 
to return a value in memory when it would normally be returned in 
registers. See “Passing Arguments” for more information."

http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/LowLevelABI/130-IA-32_Function_Calling_Conventions/IA32.html#//apple_ref/doc/uid/TP40002492-SW4

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list