Hi,<br><br>I was going through this issue along with the standards. What the standard states is(reference: <a href="http://www.sco.com/developers/devspecs/abi386-4.pdf">http://www.sco.com/developers/devspecs/abi386-4.pdf</a>):<br>
<br>If a function returns a structure or union, then the caller provides space for the<br>return value and places its address on the stack as argument word zero. In effect,<br>this address becomes a ‘‘hidden’’ first argument. Having the caller supply the<br>
return object’s space allows re-entrancy.<br><br>A function that returns a structure or union also sets% e a x to the value of the original<br>address of the caller’s area before it returns. Thus when the caller receives<br>
control again, the address of the returned object resides in register% e a x and can<br>be used to access the object. Both the calling and the called functions must<br>cooperate to pass the return value successfully.<br><br>
Also i verified that through the assembly code, that when the caller calls the callee function, the hidden pointer is stored in eax(other arguments being passed in ecx and edx). But the callee function modifies eax and in the end it does not store the hidden pointer in eax, but it is stored in edx.<br>
<br>So this seems to be the cause of the problem.<br><br><b>@Renato Golin</b>:- Does the code execute correctly? Does a printf on the sum outputs the<br>
correct value? : yes the code executes correctly and some gives the correct value. Its only in gdb that we face this problem as gdb expects the hidden pointer in eax.<br><br>Note: GDB "struct return convention" : The caller passes an additional hidden first parameter to the caller. That parameter contains the address at which the value being returned should be stored. <br>
<br><br><div class="gmail_quote">On Mon, Dec 3, 2012 at 2:37 PM, Renato Golin <span dir="ltr"><<a href="mailto:rengolin@systemcall.org" target="_blank">rengolin@systemcall.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 2 December 2012 08:12, Mayur Pandey <<a href="mailto:mayurthebond@gmail.com">mayurthebond@gmail.com</a>> wrote:<br>
> So is this just a coincidence for g++ that eax points to this address and<br>
> gdb prints the right value on the command print sum(one,two)??<br>
<br>
</div>The code is small, and the structure is probably set at the end of the<br>
block, so I think it's not that much of a coincidence.<br>
<br>
Regardless of the specific value on that specific compilation unit,<br>
you should never rely on behaviour of clobbered registers. That<br>
function returns void, you should look up in the caller, where is the<br>
address of the structure.<br>
<br>
Does the code execute correctly? Does a printf on the sum outputs the<br>
correct value? StrucRet is stable on Intel for years, I'd be surprised<br>
id that didn't work.<br>
<br>
However, it's possible that clang is messing up the position of the<br>
structure in Dwarf, so I'd investigate the Dwarf emission first, since<br>
your problem seems to bee with clang+gdb.<br>
<br>
<br>
<br>
--<br>
cheers,<br>
--renato<br>
<br>
<a href="http://systemcall.org/" target="_blank">http://systemcall.org/</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div>Thanx & Regards <br></div>
<div><b>Mayur Pandey </b><br></div><br><div><font color="#3333ff"> <br></font></div>
<div> </div>
<div> </div><br>