Search notes:

cl: _ReturnAddress()

The cl intrinsic function _ReturnAddress() reports the address of the machine code instruction that a function returns to when it is finished. Technically, it takes the address from the stack.

prog.c

#include <stdio.h>

//
//  Defined in get_eip.asm
//
uintptr_t get_eip(void);


void h(void);
void g(void);

void f() {

     uintptr_t *basePointer;
     uintptr_t *retAddr;

   __asm mov basePointer, ebp;
     retAddr = _ReturnAddress();

     printf("  f: _ReturnAddress = %x, ebp+1 = %x\n", retAddr, *(basePointer+1));

}


void g() {
     uintptr_t eip;
     printf("In f_3\n");

     f();

     eip = get_eip();

 // 
 //  get_eip() returned the address of the instruction
 //  that followed the get_eip() call.
 //  The x86 call instruction consists of 5 bytes. Thus,
 //  we have to subtract 5 bytes to get the address of the
 //  instruction that follows the call f() instruction.
 // 
     eip = eip - 5;

     printf("  eip following call of f = %x\n", eip);
}


int main() {
    g();
}
Github repository about-cl, path: /intrinsic/_ReturnAddress/prog.c

get_eip.asm

;
;       https://stackoverflow.com/a/599982/180275
;
.model  flat

_TEXT  SEGMENT

_get_eip  PROC

    mov   eax, DWORD PTR 0[esp]
    ret

_get_eip ENDP

_TEXT  ENDS

END          ; END directive required at end of file
Github repository about-cl, path: /intrinsic/_ReturnAddress/get_eip.asm

Makefile

prog.exe: prog.obj get_eip.obj
	cl /nologo /Feprog.exe prog.obj get_eip.obj

prog.obj: prog.c
	cl /nologo /W4 /c prog.c

get_eip.obj: get_eip.asm
	ml /nologo /c get_eip.asm
Github repository about-cl, path: /intrinsic/_ReturnAddress/Makefile

Index