Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!stevec From: stevec@Apple.COM (Steve Christensen) Newsgroups: comp.sys.mac.programmer Subject: Re: trap patches Message-ID: <46831@apple.Apple.COM> Date: 27 Nov 90 00:02:50 GMT References: <1990Nov26.040223.8193@ucselx.sdsu.edu> Organization: Apple Computer Inc., Cupertino, CA Lines: 63 s-zeerox@steer.calstate.edu (Stan Malyshev) writes: >Does anyone have any SIMPLE code to illustrate a trap patch via and INIT? >My creation bombs (ID = 02) at startup, and I can't figure out what's >wrong. Arrghhhh! Here's a quickie in assembly: Start BRA Setup ; here's a patch that calls the original code and then does its thing Patch1 JSR $12345678 ; call original trap code MOVEM.L savedRegs,-(SP) ; save all registers we use ... ; do whatever is needed MOVEM.L (SP)+,savedRegs ; restore registers we used RTS ; here's a patch that does its thing and then falls thru into original code Patch2 MOVEM.L savedRegs,-(SP) ; save all registers we use ... ; do whatever is needed MOVEM.L (SP)+,savedRegs ; restore registers we used ToOld JMP $12345678 ; jump to original trap code ; here's the installation code Setup MOVE.W #Trap1Num,D0 ; get the address of the trap to patch _GetTrapAddress LEA Patch1+2,A1 ; point to the JSR address MOVE.L A0,(A1) ; and save the original code's address LEA Patch1,A0 ; point the trap to our code MOVE.W #Trap1Num,D0 _SetTrapAddress MOVE.W #Trap2Num,D0 ; get the address of the trap to patch _GetTrapAddress LEA ToOld+2,A1 ; point to the JMP address MOVE.L A0,(A1) ; and save the original code's address LEA Patch2,A0 ; point the trap to our code MOVE.W #Trap2Num,D0 _SetTrapAddress LEA Start,A0 ; point to the start of this block _RecoverHandle ; and use it to get the handle back MOVE.L A0,-(SP) ; detach the block so it doesn't go _DetachResource ; away when the INIT file closes RTS Note that the code is modified. This is an easy illustration, and you could instead save the original trap addresses in your variables and jump thru that. It's also very important that the "system heap" and "locked" bits for this INIT resource are checked, otherwise it will be trashed or moved... steve -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ whoami? Steve Christensen snail: Apple Computer, 20525 Mariani Ave, MS-81CS, Cupertino, CA 95035 internet: stevec@apple.com AppleLink: stevec CompuServe: 76174,1712