M
Michael Speer
The problem with casting a pointer-to-label to a pointer-to-function is that
a label is not a function. The beginning of a function contains a prologue
which builds the appropriate stack frame, retrieves arguments from wherever
they are, etc.
I my understanding of the cdecl convention it states the code calling
the function sets up the arguments for the stack frame then calls the
function. Then the call command pushes the current base and stack
registers onto the stack. Then only local variables would require the
base and stack pointers to be moved again. The ret command undoes the
stack frame register push on the stack. Finally the calling code
undoes the argument push and the frame is back where it started.
So the code in the function itself need only create local variables.
So using the my hack above should break on functions that have local
variables, but not ones that do not.
So it should work to jump in halfway, as long as there are no local
variables that would have missed being setup.
The compiler doesn't know it needs to do that at a label
used as a function entry point -- in fact it probably can't do it even if it
does know it needs to, since you can (usually) arrive at the label when it's
_not_ being used as a function entry point.
To the compiler it should not matter. I have told the compiler
through casting that my label points to a function entry taking an int
and char** argument set, so it should do a normal cdecl argument push
and call the address at the label.
My guess is that this extension was created so that labels could be stored
in variables and one could later do "goto variable;",
... which is so far out there it's
not even just "wrong".
Correct on both accusations. In defense of the hack, a pointer to a
memory location is a pointer to a memory location. There aren't
actually pointers to objects, functions, labels, etc. Just pointers
that we inform the compiler will be used for these purposes so it
knows how to setup any pointer arithmetic as it needs to do. Outside
of that, one could goto 0x08374322 and it should work, if the code
they want to jump to is really located at that point.
There's probably a portable way to implement whatever you're trying to do;
give us the problem first, not the solution, and we can try to help.
S
Unfortunately this is a solution without a problem. Or a problem
starting with the phrase "I wonder if I could...", depending on how
you look at it.