This question already has an answer here:
- Reading program counter directly 6 answers
I am using Linux with x86 (64 bit to be precise). Is there a way I can get the address of the current instruction. Actually I want to write my own simplified versions of setjmp/longjmp. Here, R.. posted a simplified version of longjmp. Any idea how setjmp is implemented. A simplified version that is, without taking into account of exceptions and signals etc...
If using GCC, you could also use
__builtin_return_address
The offset-into-the-current-segment register (
EIP
) is not normally accessible. However, there is a hackish-way to read it indirectly - you trick the program into pushing the value of EIP onto the stack, then just read it off. You could create a subroutine that looks like this:Or, even simpler:
If you use a
CALL FAR
instruction, the segment value (CS
) will be pushed on the stack as well.If you're using C, there are various compiler-specific C-extensions you could use on this page. See also this interesting article.
I believe in 64-bit code you can simply do
lea rax, [rip]
.The 32-bit idiom is:
This site gives a simple version of setjmp and longjmp, which is as follows.