Return address of a recursive program .
Here is a debug showing that the return address in a recursive function call will be same for same line recursive call.
* Saved RIP is the the return pointer .
* RIP is the instruction pointer pointing to the next instruction to be executed.
#include<stdio.h>
int fact(n)
{
if(n==0)
return 1;
else
return n*fact(n-1); // return is 0x4004ee
}
int main()
{
int x= fact(5);
printf(" %d ",x);
}
(gdb) where
#0 fact (n=0) at factorial.c:5
#1 0x00000000004004ee in fact (n=1) at factorial.c:8
#2 0x00000000004004ee in fact (n=2) at factorial.c:8
#3 0x00000000004004ee in fact (n=3) at factorial.c:8
#4 0x00000000004004ee in fact (n=4) at factorial.c:8
#5 0x00000000004004ee in fact (n=5) at factorial.c:8
#6 0x000000000040050b in main () at factorial.c:13
GDB frame info shows return address to be the "return statement" in fact method . This same for sys stack 1 to 5.
(gdb) info frame 0
Stack frame at 0x7fffffffe180:
rip = 0x4004cf in fact (factorial.c:5); saved rip 0x4004ee
called by frame at 0x7fffffffe1a0
source language c.
Arglist at 0x7fffffffe170, args: n=0
Locals at 0x7fffffffe170, Previous frame's sp is 0x7fffffffe180
Saved registers:
rbp at 0x7fffffffe170, rip at 0x7fffffffe178
(gdb) info frame 1
Stack frame at 0x7fffffffe1a0:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe1c0, caller of frame at 0x7fffffffe180
source language c.
Arglist at 0x7fffffffe190, args: n=1
Locals at 0x7fffffffe190, Previous frame's sp is 0x7fffffffe1a0
Saved registers:
rbp at 0x7fffffffe190, rip at 0x7fffffffe198
(gdb) info frame 2
Stack frame at 0x7fffffffe1c0:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe1e0, caller of frame at 0x7fffffffe1a0
source language c.
Arglist at 0x7fffffffe1b0, args: n=2
Locals at 0x7fffffffe1b0, Previous frame's sp is 0x7fffffffe1c0
Saved registers:
rbp at 0x7fffffffe1b0, rip at 0x7fffffffe1b8
(gdb) info frame 3
Stack frame at 0x7fffffffe1e0:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe200, caller of frame at 0x7fffffffe1c0
source language c.
Arglist at 0x7fffffffe1d0, args: n=3
Locals at 0x7fffffffe1d0, Previous frame's sp is 0x7fffffffe1e0
Saved registers:
rbp at 0x7fffffffe1d0, rip at 0x7fffffffe1d8
(gdb) info frame 4
Stack frame at 0x7fffffffe200:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe220, caller of frame at 0x7fffffffe1e0
source language c.
Arglist at 0x7fffffffe1f0, args: n=4
Locals at 0x7fffffffe1f0, Previous frame's sp is 0x7fffffffe200
Saved registers:
rbp at 0x7fffffffe1f0, rip at 0x7fffffffe1f8
(gdb) info frame 5
Stack frame at 0x7fffffffe220:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x40050b
called by frame at 0x7fffffffe240, caller of frame at 0x7fffffffe200
source language c.
Arglist at 0x7fffffffe210, args: n=5
Locals at 0x7fffffffe210, Previous frame's sp is 0x7fffffffe220
Saved registers:
rbp at 0x7fffffffe210, rip at 0x7fffffffe218
(gdb) info frame 6
Stack frame at 0x7fffffffe240:
rip = 0x40050b in main (factorial.c:13); saved rip 0x37a941ed5d
caller of frame at 0x7fffffffe220
source language c.
Arglist at 0x7fffffffe230, args:
Locals at 0x7fffffffe230, Previous frame's sp is 0x7fffffffe240
Saved registers:
rbp at 0x7fffffffe230, rip at 0x7fffffffe238
Here is a debug showing that the return address in a recursive function call will be same for same line recursive call.
* Saved RIP is the the return pointer .
* RIP is the instruction pointer pointing to the next instruction to be executed.
#include<stdio.h>
int fact(n)
{
if(n==0)
return 1;
else
return n*fact(n-1); // return is 0x4004ee
}
int main()
{
int x= fact(5);
printf(" %d ",x);
}
#0 fact (n=0) at factorial.c:5
#1 0x00000000004004ee in fact (n=1) at factorial.c:8
#2 0x00000000004004ee in fact (n=2) at factorial.c:8
#3 0x00000000004004ee in fact (n=3) at factorial.c:8
#4 0x00000000004004ee in fact (n=4) at factorial.c:8
#5 0x00000000004004ee in fact (n=5) at factorial.c:8
#6 0x000000000040050b in main () at factorial.c:13
GDB frame info shows return address to be the "return statement" in fact method . This same for sys stack 1 to 5.
(gdb) info frame 0
Stack frame at 0x7fffffffe180:
rip = 0x4004cf in fact (factorial.c:5); saved rip 0x4004ee
called by frame at 0x7fffffffe1a0
source language c.
Arglist at 0x7fffffffe170, args: n=0
Locals at 0x7fffffffe170, Previous frame's sp is 0x7fffffffe180
Saved registers:
rbp at 0x7fffffffe170, rip at 0x7fffffffe178
(gdb) info frame 1
Stack frame at 0x7fffffffe1a0:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe1c0, caller of frame at 0x7fffffffe180
source language c.
Arglist at 0x7fffffffe190, args: n=1
Locals at 0x7fffffffe190, Previous frame's sp is 0x7fffffffe1a0
Saved registers:
rbp at 0x7fffffffe190, rip at 0x7fffffffe198
(gdb) info frame 2
Stack frame at 0x7fffffffe1c0:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe1e0, caller of frame at 0x7fffffffe1a0
source language c.
Arglist at 0x7fffffffe1b0, args: n=2
Locals at 0x7fffffffe1b0, Previous frame's sp is 0x7fffffffe1c0
Saved registers:
rbp at 0x7fffffffe1b0, rip at 0x7fffffffe1b8
(gdb) info frame 3
Stack frame at 0x7fffffffe1e0:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe200, caller of frame at 0x7fffffffe1c0
source language c.
Arglist at 0x7fffffffe1d0, args: n=3
Locals at 0x7fffffffe1d0, Previous frame's sp is 0x7fffffffe1e0
Saved registers:
rbp at 0x7fffffffe1d0, rip at 0x7fffffffe1d8
(gdb) info frame 4
Stack frame at 0x7fffffffe200:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x4004ee
called by frame at 0x7fffffffe220, caller of frame at 0x7fffffffe1e0
source language c.
Arglist at 0x7fffffffe1f0, args: n=4
Locals at 0x7fffffffe1f0, Previous frame's sp is 0x7fffffffe200
Saved registers:
rbp at 0x7fffffffe1f0, rip at 0x7fffffffe1f8
(gdb) info frame 5
Stack frame at 0x7fffffffe220:
rip = 0x4004ee in fact (factorial.c:8); saved rip 0x40050b
called by frame at 0x7fffffffe240, caller of frame at 0x7fffffffe200
source language c.
Arglist at 0x7fffffffe210, args: n=5
Locals at 0x7fffffffe210, Previous frame's sp is 0x7fffffffe220
Saved registers:
rbp at 0x7fffffffe210, rip at 0x7fffffffe218
(gdb) info frame 6
Stack frame at 0x7fffffffe240:
rip = 0x40050b in main (factorial.c:13); saved rip 0x37a941ed5d
caller of frame at 0x7fffffffe220
source language c.
Arglist at 0x7fffffffe230, args:
Locals at 0x7fffffffe230, Previous frame's sp is 0x7fffffffe240
Saved registers:
rbp at 0x7fffffffe230, rip at 0x7fffffffe238