0

I'm working on a program that uses inline assembly to perform a long jump. To my understanding, all I need to do is replace the FP and PC to a saved FP and PC. Using assembly, I'm able to change the frame pointer (%ebp) however I'm unable to do it to the PC.

int jump(int x)
{
  int oldFP = getebp();  //the FP of the calling function
  int oldPC = getebp()+4;  //the PC of the calling function

  ljump();  //uses assembly to change FP (works) but can't figure out PC

  return x;
}

and my ljump() is

ljump: # return stack frame pointer FP 
       movl  savedFP, %ebp
       ret

my previous attempt to change PC have been using a jump, however I usually get a segmentation error.

Any input would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kosz
  • 124
  • 1
  • 9
  • Not sure what the answer is, but you could look into how `setjmp` and `longjmp` are implemented for your platform and attempt something similar. – Theodoros Chatzigiannakis Feb 07 '14 at 18:35
  • 1
    Instead of trying to reimplement `longjmp`, just use `longjmp`. Nonlocal transfer is harder than it looks. Better to let the runtime library do it for you. It understands the various ABI requirements for nonlocal transfer. (On x86, you also need to restore nonvolatile registers.) – Raymond Chen Feb 07 '14 at 18:38
  • thanks for the suggestions. I've looked at the source code for longjmp but am having a hard time reading it. changing the frame pointer was easy enough and works, but changing the PC is proving to be way more difficult – Kosz Feb 07 '14 at 19:11

1 Answers1

1

If you want your code to continue on some predefined address you could do it like this in your asm code (pseudocode):

push myNewAddress
ret

or if you prefer it differently, by using a register:

mov  eax, myNewAddress
jmp eax

You can not modify the PC directly with an instruction, because it is always where the current instruction is. However, you should be aware that this may cause memory leaks or other sideeffects because the stack may not be properly handled.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • thanks! unfortunately i keep getting a "segmentation error" when trying to jump, but I think you're on the right track – Kosz Feb 07 '14 at 19:13
  • Which one did you try? if you use the `ret`, then check if you have the correct instruction size for the push and the ret. – Devolus Feb 07 '14 at 22:08