What's new

Function Return Address

tooie

New member
I know when I am writing a program in Visual C++ I can get the return address of the function by:

DWORD * EBPreg, ReturnAddress;
_asm mov EBPreg, ebp
ReturnAddress = *(EBPreg + 1);

is this the same with GCC under linux .. or is there a better way of doing this ?
 

euphoria

Emutalk Member
tooie said:
I know when I am writing a program in Visual C++ I can get the return address of the function by:
Code:
DWORD * EBPreg, ReturnAddress; 
_asm mov EBPreg, ebp
ReturnAddress  = *(EBPreg + 1);
is this the same with GCC under linux .. or is there a better way of doing this ?

Some thing that come to mind:
-GCC uses AT&T style assembler which could cause problems, dunno since i've never done inline assembler. i do all my asm functions in nasm and then link them.
-DWORD isn't defined in gcc or ANSI-/POSIX-C for that matter. You have to replace/define it with unsigned int or unsigned long or whatever suits you.
 

Hacktarux

Emulator Developer
Moderator
tooie said:
I know when I am writing a program in Visual C++ I can get the return address of the function by:

DWORD * EBPreg, ReturnAddress;
_asm mov EBPreg, ebp
ReturnAddress = *(EBPreg + 1);

is this the same with GCC under linux .. or is there a better way of doing this ?

On Linux, the return address is at the same place but you have to write your asm line differently (AT&T inline assembly syntax). It should be something like:

asm ("mov %%ebp, %1 \n"
: "m" (EBPreg)
:
: "memory");
 

Cyberman

Moderator
Moderator
This is also highly processor dependant. Some processors store the return address in a register. ARM and MIPS do this the called function if it's recursive preserves the register when necessary. Much of the time the X86 spends is bouncing between subroutines and manipulating the stack I've noticed.

Anyhow as to the question, yep that's what you can do under GCC for X86. ARM, MIPS or PowerPC are a different matter.

Cyb
 
OP
tooie

tooie

New member
thanks all .. Yer I know it is for x86 only .. I will have to do different inline asm .. but I was more wondering on the theory, which hack has confirmed.
 

blight

New member
i think where the return address is stored depends on the CPU and not on the compiler since there is a ret instruction which gets the return address from the same place where call put it (the stack)
 

Top