nm命令

nm——列出目标文件的符号清单

nm命令的格式如下

nm[选项][目标文件]

nm 后如果没有输入目标文件名,nm假想该文件是a.out。nm的用途之一就是调试程序,特别是在编嵌入式程序。

nm的目标文件包括编译后的.o文件,可执行的.elf文件,打包的.a文件(静态库),动态链接库.so文件。nm输入格式如下:

nm main.o

                 U exit
                 U log5
00000000 T main
                 U printf

第一列是符号的值,数值进制可以通过选项控制,默认是16进制;

第二列是符号的类型,其中大写是全局或外部定义的符号,小写是局部的符号;

第三列是符号的名字。

符号的类型有以下几种:

"A" The symbol's value is absolute, and will not be changed by further linking.

 A  符号的值是绝对的,不会在链接过程中改变。

"B"
"b" The symbol is in the uninitialized data section (known as BSS).

 B/b 符号位于未初始化段(bss段),即初始值为0

其值表示在段中的偏移量

"C" The symbol is common.  Common symbols are uninitialized data. When linking, multiple common symbols may appear with the same  name.  If the symbol is defined   anywhere, the common symbols  are treated as undefined references.

C 符号位于common段中,common段是编译的.o中临时存在的段。该段中的符号是全局的未显示初始化的数据比如在文件中定义全局变量:int a;则a变量对应的符号就位于Common段中。在链接后common中的符号会放在bss段中。关于common段和bss段的区别请见我的另一篇博客。

其值表示所占的字节数

 "D"
 "d" The symbol is in the initialized data section.

 D/d 表示符号位于已初始化数据段.data中。

其值表示在段中的偏移量

 "G"
 "g" The symbol is in an initialized data section for small objects.Some object file formats permit more efficient access to small  data objects, such as a global int variable as opposed to a  large global array.

G/g 该符号也位于初始化数据段中。主要用于small object提高访问small data object效率的一种方式。

 "i" For PE format files this indicates that the symbol is in a   section specific to the implementation of DLLs.  For ELF format  files this indicates that the symbol is an indirect function.This is a GNU extension to the standard set of ELF symbol types.  It indicates a symbol which if referenced by a  relocation does not evaluate to its address, but stead must  be invoked at runtime.  The runtime execution will then return  the value to be used in the relocation.

"N" The symbol is a debugging symbol.

N 符号是用来调试的。

"p" The symbols is in a stack unwind section.

p 这些符号是在堆栈展开段。

"R"
"r" The symbol is in a read only data section.

R/r这些符号位于只读数据段。比如const char a[]= "Hello World\n";

其值表示在段中的偏移量

"S"
"s" The symbol is in an uninitialized data section for small   objects.

S/s 这些符号位于small objects的未初始化数据段。

 "T"
 "t" The symbol is in the text (code) section.

T/t 这些符号位于代码段。

其值表示在段中的偏移量

 "U" The symbol is undefined.

U 该符号在当前文件中是未定义的,即该符号的定义在别的文件中。例如,当前文件调用另一个文件中定义的函数,在这个被调用的函数在当前就是未定义的;但是在定义它的文件中类型是T。但是对于全局变量来说,在定义它的文件中,其符号类型为C,在使用它的文件中,其类型为U。

 "u" The symbol is a unique global symbol.  This is a GNU extension to the standard set of ELF symbol bindings.  For such a symbol   the dynamic linker will make sure that in the entire process there is just one symbol with this name and type in use.

u 符号是唯一的全局变量。这是对GNU elf符号的拓展。对于这种符号动态链接器会在整个进程中保证该符号名字和类型的唯一性。

"V"
"v" The symbol is a weak object.  When a weak defined symbol is  linked with a normal defined symbol, the normal defined symbol   is used with no error.  When a weak undefined symbol is linked  and the symbol is not defined, the value of the weak symbol   becomes zero with no error.  On some systems, uppercase  indicates that a default value has been specified.

V/v 表示这个符号是弱对象。当定义一个符号是弱对象时,如果该符号同时定义成正常的符号,那么正常符号被使用弱符号被忽略。如果弱对象未定义,在链接是其值为0(这个值以系统而定),这个过程中不会出错。在某些系统中V表示弱符号的值已指定。

"W"
"w" The symbol is a weak symbol that has not been specifically  tagged as a weak object symbol.  When a weak defined symbol is  linked with a normal defined symbol, the normal defined symbol is used with no error.  When a weak undefined symbol is linked and the symbol is not defined, the value of the symbol is  determined in a system-specific manner without error.  On someystems, uppercase indicates that a default value has been  specified.

同上

 "-" The symbol is a stabs symbol in an a.out object file.  In this  case, the next values printed are the stabs other field, the stabs desc field, and the stab type.  Stabs symbols are used to  hold debugging information.

- 符号是a.out的stabs符号,stabs符号保存调试信息。

"?" The symbol type is unknown, or object file format specific.

?符号类型未知
#include <stdio.h>

#include <string.h>

extern float log5(float);
float f;
char g;
double a =10.1;
int b =10;
int c =0;
char d ='\0';
const char str[]="Please input the number:\n";
int main(int argc,char * argv[]){
  puts(str);
   scanf("%f\n",&f);
   printf("log5(%f) is %f\n",f,log5(f));
   exit(0);
}
void test(){
   int a,b;
   a = 10;
   b = a;

}

nm main.o 结构如下:

         U __isoc99_scanf
00000000 D a
00000008 D b
00000000 B c
00000004 B d
         U exit
00000004 C f
00000001 C g
         U log5
00000000 T main
         U printf
         U puts
00000000 R str
00000060 T test

nm的选项

-A

-o

在符号前面加上符号所在的文件名。

-a

显示所有的符号,包括调试用的符号。

-C

将低级的符号重组为用户级的符号

--no-demangle

不重组低级的符号

-D

显示动态符号,只对动态目标意义,比如共享库。

 -f format

设置输出的个数,可以是b(bsd),s(sysv),p(posix).默认值是"bsd"。

-g

只显示外部符号。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值