[comp.sys.mac.programmer] Think C non-tail patching

merlyn@digibd (Brian Westley (Merlyn LeRoy)) (02/08/90)

How do I clean up the stack & jmp to a patched routine, instead
of tail-patching it, in Think C 4.0?  Something like..

<init main>
{
jmpto = NGetTrapAddress(trapnum, ToolTrap);
NSetTrapAddress(trapnum, myroutine, ToolTrap);
}
....
myroutine(arg1, arg2)
{
	<stuff>
	CallPascal(arg1, arg2, jmpto);		/* A nasty tail patch */
}

The CallPascal() call should be replaced by asm {} stuff that cleans up
the stack and does a jmp to "jmpto".  However, I haven't been able to
figure it out and I'm tired of crashing my machine.  Someone must have
invented this wheel already.

On a related note, how do I patch PACK3, which has different arguments
depending on how it's called?  Similar to the above problem.

Thanks for any help.  May as well post it, it's of general interest.
-----
Merlyn LeRoy

rcfische@polyslo.CalPoly.EDU (Raymond C. Fischer) (02/12/90)

In article <1990Feb8.150728.9760@digibd> merlyn@digibd (Brian Westley (Merlyn LeRoy)) writes:
>How do I clean up the stack & jmp to a patched routine, instead
>of tail-patching it, in Think C 4.0?  Something like..
>
><init main>
>{
>jmpto = NGetTrapAddress(trapnum, ToolTrap);
>NSetTrapAddress(trapnum, myroutine, ToolTrap);
>}
>....
>myroutine(arg1, arg2)
>{
>	<stuff>
>	CallPascal(arg1, arg2, jmpto);		/* A nasty tail patch */
>}
>
>The CallPascal() call should be replaced by asm {} stuff that cleans up
>the stack and does a jmp to "jmpto".  However, I haven't been able to
>figure it out and I'm tired of crashing my machine.  Someone must have
>invented this wheel already.

How's about something like this ...

asm {
	move	jmpto,a0	; get the trap address
	unlk	a6		; restore a6 and toss stack frame
	jmp	(a0)		; go to trap address
}

Your trap patch MUST be declared as a pascal routine exactly the
same way the trap is defined.  C calling conventions and Pascal calling
conventions are different and incompatible.  In the example above
you would use ...

pascal void myroutine(arg1, arg2)

>On a related note, how do I patch PACK3, which has different arguments
>depending on how it's called?  Similar to the above problem.

Actually, since the above assembly code leaves the parameters on the 
stack along with the return address, it will work regardless of the
number of parameters.


Ray Fischer
rcfische@polyslo.calpoly.edu

chaffee@reed.UUCP (Alex Chaffee) (02/12/90)

In article <1990Feb8.150728.9760@digibd> merlyn@digibd (Brian Westley (Merlyn LeRoy)) writes:
>How do I clean up the stack & jmp to a patched routine, instead
>of tail-patching it, in Think C 4.0?  Something like..

The other respondent gave a basic answer; for source that deals with saving
registers and the like, look for BeepShuffle INIT on sumex.  Or write me if
you can't find it.

-- 
Alex Chaffee
chaffee@reed.UUCP
Reed College, Portland OR 97202
____________________