12

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...

Community
  • 1
  • 1
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

4 Answers4

29

I believe in 64-bit code you can simply do lea rax, [rip].

The 32-bit idiom is:

      call next
next: pop eax
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • And is it possible to do something like that in 32 bit? – MetallicPriest Oct 28 '11 at 17:01
  • I often require the address repeatedly during a block of code, so I do `call next` and access the value using `[esp]` rather than by popping it into a register. It gives you an extra free register to use and you can just do `add esp, 4` (on a downward growing stack) to clean up the stack when you're done. – Polynomial Oct 30 '11 at 14:07
  • 2
    Another option in assembly `mov (r|e)ax, $` or `mylabel: mov (r|e)ax, offset mylabel`. – Alexey Frunze Sep 13 '12 at 00:47
  • this idiom is actually not recommended http://blogs.msdn.com/b/oldnewthing/archive/2004/12/16/317157.aspx – phuclv Nov 10 '14 at 16:49
  • 1
    `lea rax, [rip]` did not work in NASM 2.10. It seems that RIP can only be used indirectly with `rel` as in `lea rax, [rel _start]`? – Ciro Santilli OurBigBook.com May 10 '15 at 21:31
11

If using GCC, you could also use __builtin_return_address

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 2
    Keep in mind you'll need to wrap that in a function in order to have the intended effect, otherwise you'll end up with the return address for the current stack frame rather than the address of the current instruction. – Jason Oct 28 '11 at 17:33
  • 3
    if using GCC it's easier to use [`somelabel: return &&somelabel;`](http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html) – phuclv Dec 28 '13 at 01:06
  • 1
    @Jason is right and also make sure the definition of the wrapping function containing the __builtin_return_address is not in a header file and will never be inlined. – Michal Fapso Jun 09 '17 at 08:54
  • @phuclv yes, see also: https://stackoverflow.com/questions/1777990/is-it-possible-to-store-the-address-of-a-label-in-a-variable-and-use-goto-to-jum – Ciro Santilli OurBigBook.com Jul 16 '19 at 15:19
9

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:

GetAddress:
    mov eax, [esp]
    ret
...
    call GetAddress     ; address of this line stored in eax

Or, even simpler:

    call NextLine
NextLine:
    pop eax             ; address of previous line stored in EAX

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.

Community
  • 1
  • 1
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
  • the OP asks about x86_64, which has relative addressing, so there are instructions "accessible" to RIP – phuclv Dec 28 '13 at 01:10
1

This site gives a simple version of setjmp and longjmp, which is as follows.

#include "setjmp.h"

#define OFS_EBP   0
#define OFS_EBX   4
#define OFS_EDI   8
#define OFS_ESI   12
#define OFS_ESP   16
#define OFS_EIP   20

__declspec(naked) int setjmp(jmp_buf env)
{
  __asm
  {
    mov edx, 4[esp]          // Get jmp_buf pointer
    mov eax, [esp]           // Save EIP
    mov OFS_EIP[edx], eax
    mov OFS_EBP[edx], ebp    // Save EBP, EBX, EDI, ESI, and ESP
    mov OFS_EBX[edx], ebx
    mov OFS_EDI[edx], edi
    mov OFS_ESI[edx], esi
    mov OFS_ESP[edx], esp
    xor eax, eax             // Return 0
    ret
  }
}

__declspec(naked) void longjmp(jmp_buf env, int value)
{
  __asm
  {
    mov edx, 4[esp]          // Get jmp_buf pointer
    mov eax, 8[esp]          // Get return value (eax)

    mov esp, OFS_ESP[edx]    // Switch to new stack position
    mov ebx, OFS_EIP[edx]    // Get new EIP value and set as return address
    mov [esp], ebx

    mov ebp, OFS_EBP[edx]    // Restore EBP, EBX, EDI, and ESI
    mov ebx, OFS_EBX[edx]
    mov edi, OFS_EDI[edx]
    mov esi, OFS_ESI[edx]

    ret
  }
}
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356