当前位置: 动力学知识库 > 问答 > 编程问答 >

linux - why the function address that gdb shows is different with that of nm?

问题描述:

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.

void fo()

{

return ;

}

void foo()

{

fo();

}

int add(int a, int b)

{

foo();

return a + b;

}

int main()

{

int a = 1;

int b = 2;

add(a, b);

return 0;

}

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.

(gdb) bt

#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.

网友答案:

Because 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 nm.

分享给朋友:
您可能感兴趣的文章:
随机阅读: