Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
This is compiled with -O2 and gcc seems to use the fact that the argument is not used. In fact, it stores the argument to print (the string) at the place where arg is, and so everything is broken upon f return.
This doesn't happen when compiling with -O0, so I'd like to know if there's a way to tell gcc not to optimize anything related to arg (including assuming its stack space is free) ?
Last edited by pini on Tue Jan 22, 2008 7:43 am, edited 1 time in total.
The problem is not with optimizations, but that with standard C calling convention the caller cleans up the params it pushed onto the stack. One thing you should do is do what linux does for it's "asmlinkage" macro which is an attribute which guarantees C style calling convention.
pini wrote:This is compiled with -O2 and gcc seems to use the fact that the argument is not used. In fact, it stores the argument to print (the string) at the place where arg is, and so everything is broken upon f return.
I really don't see how 'everything' could be 'broken upon return'. Upon return, the stack location used to store arg should be released anyway. It is temporary. GCC using the location for something else is just the same as you adding a line saying 'arg = (int)(int *)"some message"'. Are you sure there's not something else that's screwed up?
pini wrote:As you can see, the argument is not thrown away, as it would normally be the case but reused and that's why I want gcc not to touch it.
You're skating on really thin ice here, as gcc may change any registers in a C function as it sees fit, and may change anything on the stack frame passed to it as it sees fit. You are calling a C-function, but do not adhere to the calling convention for that function.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
is your function supposed to alter the value of ds? If not, then this would do as well (what you currently have will have the net effect of pass-by-reference since you pop the result back into ds).