tags:

views:

50

answers:

2
+1  Q: 

Address problem !

consider this :

[mdstest:~/onkar/test]$cat test.c
#include<stdio.h>

int main(int argc,char **argv)
{
        printf("%p\n",main);
        return 0;
}
[mdstest:~/onkar/test]$make
gcc -g -Wall -o test test.c
[mdstest:~/onkar/test]$./test
0x8048368 ------------------------------------- (1) 
[mdstest:~/onkar/test]$gdb test
:::::::::::
:::::::::::
(gdb) b main
Breakpoint 1 at 0x8048384: file test.c, line 5.
(gdb) r
Starting program: /home/mdstest/onkar/test/test
[Thread debugging using libthread_db enabled]

Breakpoint 1, main (argc=1, argv=0xbffff2d4) at test.c:5
5               printf("%p\n",main);
(gdb) disassemble
Dump of assembler code for function main:
   0x08048368 <+0>:     push   %ebp
   0x08048369 <+1>:     mov    %esp,%ebp
   0x0804836b <+3>:     sub    $0x8,%esp
   0x0804836e <+6>:     and    $0xfffffff0,%esp
   0x08048371 <+9>:     mov    $0x0,%eax
   0x08048376 <+14>:    add    $0xf,%eax
   0x08048379 <+17>:    add    $0xf,%eax
   0x0804837c <+20>:    shr    $0x4,%eax
   0x0804837f <+23>:    shl    $0x4,%eax
   0x08048382 <+26>:    sub    %eax,%esp
=> 0x08048384 <+28>:    sub    $0x8,%esp -----------------------------(2) 
   0x08048387 <+31>:    push   $0x8048368
   0x0804838c <+36>:    push   $0x8048480
   0x08048391 <+41>:    call   0x80482b0 <printf@plt>
   0x08048396 <+46>:    add    $0x10,%esp
   0x08048399 <+49>:    mov    $0x0,%eax
   0x0804839e <+54>:    leave
   0x0804839f <+55>:    ret
End of assembler dump.

Why are (1) and (2) addresses different ? That is , why some other address is getting printed in (1) whereas the debugger stops at some other location ?

A: 

"The address of main" is indeed 0x08048368 -- the address of source line 5, where the breakpoint was set, is just after the standard start-of-function boilerplate, just before the code prepping printf's argument and calling it (so that a n will execute that printf-call statement, for example).

Alex Martelli
+2  A: 

When a function is called, the calling function does a bit of stuff, and then issues a call instruction pointing to the function being called.

The callee then does a lot of boilerplate of their own - saving registers, shifting the stack pointer to allocate space for stack variables, etc.

When you ask gdb to break at the start of a function, it breaks after that boilerplate, at the start of your actual code - so the address of the function is going to be earlier than the point at which gdb breaks.

Anon.