The stack & function calls¶
2.2 The stack & function calls (x86-64 SysV)¶
When a function foo() is called, roughly this happens:
- Caller pushes arguments (in registers like RDI, RSI, RDX… first, but also stack for extra).
call foopushes the return address (address of instruction aftercall) onto the stack and jumps tofoo.- Inside
foo, the prologue usually: - Local variables are stored below
rbpon the stack. - When
fooreturns, epilogue:
On x86–64, there are a few key registers:
RIP: the Register Instruction Pointer – it tells the CPU which instruction to execute next.RSP: the Register Stack Pointer – it points to the top of the stack.RBP: the Register Base Pointer – it’s a fixed reference point used to access local variables.
The crucial idea is this:
[!important] If you can overwrite the saved return address on the stack, you can control where the
retinstruction sends the program next. That means you controlRIP, the instruction pointer.
This is the heart of many classic exploits.