什么是“符号值” from nm命令是什么意思? [英] What does "symbol value" from nm command mean?

查看:243
本文介绍了什么是“符号值” from nm命令是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当列出静态库的符号表,如 nm mylib.a 时,每个符号旁边显示的8位十六进制是什么意思?这是每个符号在代码中的相对位置吗?

When you list the symbol table of a static library, like nm mylib.a, what does the 8 digit hex that show up next to each symbol mean? Is that the relative location of each symbol in the code?

此外,多个符号可以有相同的符号值吗?

Also, can multiple symbols have the same symbol value? Is there something wrong with a bunchof different symbols all having the symbol value of 00000000?

推荐答案

这里是我写的代码片段C:

Here's a snippet of code I wrote in C:


#include 
#include 

void foo();

int main(int argc, char* argv[]) {
    foo();
}

void foo() {
   printf("Foo bar baz!");
}



在该代码上运行`gcc -c foo.c'。这里是`nm foo.o`显示的:

I ran `gcc -c foo.c` on that code. Here is what `nm foo.o` showed:


000000000000001b T foo
0000000000000000 T main
                 U printf

对于这个例子,我运行Ubuntu Linux 64位;这就是为什么你看到的8位十六进制在这里是16位数。 : - )

For this example I am running Ubuntu Linux 64-bit; that is why the 8 digit hex you see is 16 digit here. :-)

您看到的十六进制数字是对象文件中相对于 .text开头的代码的地址。 部分。 (假设我们寻址目标文件中从0x0开始的部分)。如果您运行 objdump -td foo.o ,您将在输出中看到以下内容:

The hex digit you see is the address of the code in question within the object file relative to the beginning of the .text. section. (assuming we address sections of the object file beginning at 0x0). If you run objdump -td foo.o, you'll see the following in the output:


Disassembly of section .text:

0000000000000000 :
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 10             sub    $0x10,%rsp
   8:   89 7d fc                mov    %edi,-0x4(%rbp)
   b:   48 89 75 f0             mov    %rsi,-0x10(%rbp)
   f:   b8 00 00 00 00          mov    $0x0,%eax
  14:   e8 00 00 00 00          callq  19 
  19:   c9                      leaveq
  1a:   c3                      retq

000000000000001b :
  1b:   55                      push   %rbp
  1c:   48 89 e5                mov    %rsp,%rbp
  1f:   b8 00 00 00 00          mov    $0x0,%eax
  24:   48 89 c7                mov    %rax,%rdi
  27:   b8 00 00 00 00          mov    $0x0,%eax
  2c:   e8 00 00 00 00          callq  31 
  31:   c9                      leaveq
  32:   c3                      retq

请注意,这两个符号直接与符号表中从 nm 。请记住,如果将此对象文件链接到其他目标文件,这些地址可能会更改。另外,请记住,当您将此文件链接到系统提供的任何libc时,在$ 0x2c的 callq 将更改,因为这是一个不完整的调用printf(它不

Notice that these two symbols line right up with the entries we saw in the symbol table from nm. Bare in mind, these addresses may change if you link this object file to other object files. Also, bare in mind that callq at 0x2c will change when you link this file to whatever libc your system provides, since that is currently an incomplete call to printf (it doesn't know where it is right now).

至于你的 mylib.a ,还有更多的事情。您拥有的文件是存档;它包含多个对象文件,每个文件都有自己的文本段。例如,这里是我的盒子上的/usr/lib/libm.a的一部分。

As for your mylib.a, there is more going on here. The file you have is an archive; it contains multiple object files, each one of which with it's own text segment. As an example, here is part of an nm against /usr/lib/libm.a on my box here


e_sinh.o:
0000000000000000 r .LC0
0000000000000008 r .LC1
0000000000000010 r .LC2
0000000000000018 r .LC3
0000000000000000 r .LC4
                 U __expm1
                 U __ieee754_exp
0000000000000000 T __ieee754_sinh

e_sqrt.o:
0000000000000000 T __ieee754_sqrt

e_gamma_r.o:
0000000000000000 r .LC0
                 U __ieee754_exp
0000000000000000 T __ieee754_gamma_r
                 U __ieee754_lgamma_r
                 U __rint

文本段 - 由第二列中的T表示,位于地址0x0,但每个单独的文件只有一个0x0处的文本段符号。

You'll see that multiple text segment entires -- indicated by the T in the second column rest at address 0x0, but each individual file has only one text segment symbol at 0x0.

对于具有多个符号位于相同地址的单个文件,似乎可能会 。毕竟,它只是一个表中的条目,用于确定数据块的位置和大小。但我不知道肯定。我从来没有见过多个符号引用前一节的同一部分。任何人对此知识比我更能了解: - )

As for individual files having multiple symbols resting at the same address, it seems like it would be possible perhaps. After all, it is just an entry in a table used to determine the location and size of a chunk of data. But I don't know for certain. I have never seen multiple symbols referencing the same part of a section before. Anyone with more knowledge on this than me can chime in. :-)

希望这有助于一些。

这篇关于什么是“符号值” from nm命令是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆