I'm following
this tutorials and tried to compile one of the sources they list:
void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
int *ret;
ret = buffer1 + 12;
(*ret) += 8; //change here to insert the address of printf
}
void main() {
int x;
x = 0;
function(1,2,3);
x = 1;
printf("%d\n",x);
}
The problem is that, disassembling with gdb in "main", they get this:
0x8000490 <main>: pushl %ebp
0x8000491 <main+1>: movl %esp,%ebp
0x8000493 <main+3>: subl $0x4,%esp
0x8000496 <main+6>: movl $0x0,0xfffffffc(%ebp)
0x800049d <main+13>: pushl $0x3
0x800049f <main+15>: pushl $0x2
0x80004a1 <main+17>: pushl $0x1
0x80004a3 <main+19>: call 0x8000470 <function>
0x80004a8 <main+24>: addl $0xc,%esp
0x80004ab <main+27>: movl $0x1,0xfffffffc(%ebp)
0x80004b2 <main+34>: movl 0xfffffffc(%ebp),%eax
0x80004b5 <main+37>: pushl %eax
0x80004b6 <main+38>: pushl $0x80004f8
0x80004bb <main+43>: call 0x8000378 <printf>
0x80004c0 <main+48>: addl $0x8,%esp
0x80004c3 <main+51>: movl %ebp,%esp
0x80004c5 <main+53>: popl %ebp
0x80004c6 <main+54>: ret
0x80004c7 <main+55>: nop
and I get:
0x0804821e <main+0>: push %ebp
0x0804821f <main+1>: mov %esp,%ebp
0x08048221 <main+3>: sub $0x18,%esp
0x08048224 <main+6>: and $0xfffffff0,%esp
0x08048227 <main+9>: mov $0x0,%eax
0x0804822c <main+14>: sub %eax,%esp
0x0804822e <main+16>: movl $0x0,0xfffffffc(%ebp)
0x08048235 <main+23>: movl $0x3,0x8(%esp)
0x0804823d <main+31>: movl $0x2,0x4(%esp)
0x08048245 <main+39>: movl $0x1,(%esp)
0x0804824c <main+46>: call 0x8048204 <function>
0x08048251 <main+51>: movl $0x1,0xfffffffc(%ebp)
0x08048258 <main+58>: mov 0xfffffffc(%ebp),%eax
0x0804825b <main+61>: mov %eax,0x4(%esp)
0x0804825f <main+65>: movl $0x8095d88,(%esp)
0x08048266 <main+72>: call 0x8049650 <printf>
0x0804826b <main+77>: leave
0x0804826c <main+78>: ret
I do understant that both asm sources are equivalent (do the same thing) but the difference is obvious: one uses push and pop, the other addresses the stack directly. Is there a gcc option to get the same code? I'm using gcc 3.3.5, running "gcc -o file -ggdb -static file.c".
Also disassembling "function" I was expecting the stack pointer (the BP register) to be decremented by 28 bytes (12 for buffer2, 8 for buffer1, 4 for ret, 4 for the saved BP register) but the disassembly shows othewise:
0x08048204 <function+0>: push %ebp
0x08048205 <function+1>: mov %esp,%ebp
0x08048207 <function+3>: sub $0x38,%esp
0x0804820a <function+6>: lea 0xffffffe8(%ebp),%eax
0x0804820d <function+9>: add $0xc,%eax
0x08048210 <function+12>: mov %eax,0xffffffd4(%ebp)
0x08048213 <function+15>: mov 0xffffffd4(%ebp),%eax
0x08048216 <function+18>: movl $0x8049650,(%eax)
0x0804821c <function+24>: leave
0x0804821d <function+25>: ret
Am I not getting how the function call stack operations work?