View Full Version : Function Return Address
tooie
May 5th, 2003, 08:01
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
May 5th, 2003, 08:42
Originally posted by tooie
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 ?
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
May 5th, 2003, 10:19
Originally posted by tooie
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
May 5th, 2003, 15:25
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
tooie
May 5th, 2003, 19:39
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
May 6th, 2003, 12:54
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)
Powered by vBulletin® Version 4.1.11 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.