Today, when I debug my code on linux, I suddenly find that the function address in gdb is different with what nm shows. (The executable is statically linked.)
I thought that the addresses that they show should have been the same, as they were all logical addresses. But today's finding makes me confused.
Does someone know the reason why they are different?
I write the following simple program to illustrate what I encountered.
int add(int a, int b)
return a + b;
int a = 1;
int b = 2;
the output of nm is:
0000000000400584 T _Z2fov
0000000000400595 T _Z3addii
000000000040058a T _Z3foov
00000000004005b3 T main
but when I use gdb to debug the compiled executable, putting the breakpoint at line 3.
#0 fo () at test.cpp:4
#1 0x0000000000400593 in foo () at test.cpp:9
#2 0x00000000004005a8 in add (a=1, b=2) at test.cpp:14
#3 0x00000000004005d8 in main () at test.cpp:22
you can see that the output of gdb is different from that of nm.
nm shows the address of the start of the function, while
gdb stacktrace shows where exactly the execution has progressed inside the function. Precisely it should be the return address in the stack frame, that is pointer to next instruction to be executed in the function when the function above it in stack returns.
Note, that if you just ask
gdb for the function pointer by evaluating function pointer expression, it should give the same address as