linux中的nm命令简介

转:http://blog.csdn.net/stpeace/article/details/47089585

一般来说, 搞linux开发的人, 才会用到nm命令, 非开发的人, 应该用不到。 虽然nm很简单, 但是还是有必要写几句, 聊表心意。

nm不是ni ma的缩写, 当然, 也不是ni mei的缩写, 而是names的缩写, nm命令主要是用来列出某些文件中的符号(说白了就是一些函数和全局变量等)。  下面, 我们一起来看看。

test.h为:

[cpp] view plain copy print?

  1. void print();

void print();

test.c为:

[cpp] view plain copy print?

  1. #include <stdio.h>
  2. #include "test.h"
  3. void print()
  4. {
  5. printf("rainy days\n");
  6. }

#include <stdio.h>
#include "test.h"

void print()
{
	printf("rainy days\n");
}

main.c为:

[cpp] view plain copy print?

  1. #include "test.h"
  2. int main()
  3. {
  4. print();
  5. return 0;
  6. }

#include "test.h"

int main()
{
	print();
	return 0;
}

好, 我们看看nm命令的作用效果, 如下:

[plain] view plain copy print?

  1. [[email protected] learn_nm]$ nm *
  2. nm: main.c: File format not recognized
  3. nm: test.c: File format not recognized
  4. nm: test.h: File format not recognized
  5. [[email protected] learn_nm]$

[[email protected] learn_nm]$ nm *
nm: main.c: File format not recognized
nm: test.c: File format not recognized
nm: test.h: File format not recognized
[[email protected] learn_nm]$

ni ma, 啥都没有, 这说明nm对这类文件无用。

继续看nm能否读取目标文件和可执行文件:

[plain] view plain copy print?

  1. [[email protected] learn_nm]$ ls
  2. main.c  test.c  test.h
  3. [[email protected] learn_nm]$ gcc -c test.c main.c
  4. [[email protected] learn_nm]$ gcc test.o main.o
  5. [[email protected] learn_nm]$ ./a.out
  6. rainy days
  7. [[email protected] learn_nm]$ nm *
  8. a.out:
  9. 08049564 d _DYNAMIC
  10. 08049630 d _GLOBAL_OFFSET_TABLE_
  11. 0804849c R _IO_stdin_used
  12. w _Jv_RegisterClasses
  13. 08049554 d __CTOR_END__
  14. 08049550 d __CTOR_LIST__
  15. 0804955c D __DTOR_END__
  16. 08049558 d __DTOR_LIST__
  17. 0804854c r __FRAME_END__
  18. 08049560 d __JCR_END__
  19. 08049560 d __JCR_LIST__
  20. 0804964c A __bss_start
  21. 08049648 D __data_start
  22. 08048450 t __do_global_ctors_aux
  23. 08048330 t __do_global_dtors_aux
  24. 080484a0 R __dso_handle
  25. w __gmon_start__
  26. 0804844a T __i686.get_pc_thunk.bx
  27. 08049550 d __init_array_end
  28. 08049550 d __init_array_start
  29. 080483e0 T __libc_csu_fini
  30. 080483f0 T __libc_csu_init
  31. U [email protected]@GLIBC_2.0
  32. 0804964c A _edata
  33. 08049654 A _end
  34. 0804847c T _fini
  35. 08048498 R _fp_hw
  36. 08048290 T _init
  37. 08048300 T _start
  38. 0804964c b completed.5963
  39. 08049648 W data_start
  40. 08049650 b dtor_idx.5965
  41. 08048390 t frame_dummy
  42. 080483c8 T main
  43. 080483b4 T print
  44. U [email protected]@GLIBC_2.0
  45. nm: main.c: File format not recognized
  46. main.o:
  47. 00000000 T main
  48. U print
  49. nm: test.c: File format not recognized
  50. nm: test.h: File format not recognized
  51. test.o:
  52. 00000000 T print
  53. U puts
  54. [[email protected] learn_nm]$

[[email protected] learn_nm]$ ls
main.c  test.c  test.h
[[email protected] learn_nm]$ gcc -c test.c main.c
[[email protected] learn_nm]$ gcc test.o main.o
[[email protected] learn_nm]$ ./a.out
rainy days
[[email protected] learn_nm]$ nm *

a.out:
08049564 d _DYNAMIC
08049630 d _GLOBAL_OFFSET_TABLE_
0804849c R _IO_stdin_used
         w _Jv_RegisterClasses
08049554 d __CTOR_END__
08049550 d __CTOR_LIST__
0804955c D __DTOR_END__
08049558 d __DTOR_LIST__
0804854c r __FRAME_END__
08049560 d __JCR_END__
08049560 d __JCR_LIST__
0804964c A __bss_start
08049648 D __data_start
08048450 t __do_global_ctors_aux
08048330 t __do_global_dtors_aux
080484a0 R __dso_handle
         w __gmon_start__
0804844a T __i686.get_pc_thunk.bx
08049550 d __init_array_end
08049550 d __init_array_start
080483e0 T __libc_csu_fini
080483f0 T __libc_csu_init
         U [email protected]@GLIBC_2.0
0804964c A _edata
08049654 A _end
0804847c T _fini
08048498 R _fp_hw
08048290 T _init
08048300 T _start
0804964c b completed.5963
08049648 W data_start
08049650 b dtor_idx.5965
08048390 t frame_dummy
080483c8 T main
080483b4 T print
         U [email protected]@GLIBC_2.0
nm: main.c: File format not recognized

main.o:
00000000 T main
         U print
nm: test.c: File format not recognized
nm: test.h: File format not recognized

test.o:
00000000 T print
         U puts
[[email protected] learn_nm]$

可以看到, 对于目标文件和可执行文件而言, 均可以获得其中的函数, 如print函数。

我们继续看静态库和动态库, 如下:

[plain] view plain copy print?

  1. [[email protected] learn_nm]$ ls
  2. main.c  test.c  test.h
  3. [[email protected] learn_nm]$ gcc -c test.c
  4. [[email protected] learn_nm]$ ar rcs libtest.a test.o
  5. [[email protected] learn_nm]$ gcc -shared -fPIC -o libtest.so test.o
  6. [[email protected] learn_nm]$ ls
  7. libtest.a  libtest.so  main.c  test.c  test.h  test.o
  8. [[email protected] learn_nm]$ nm lib*
  9. libtest.a:
  10. test.o:
  11. 00000000 T print
  12. U puts
  13. libtest.so:
  14. 000014bc a _DYNAMIC
  15. 00001590 a _GLOBAL_OFFSET_TABLE_
  16. w _Jv_RegisterClasses
  17. 000014a8 d __CTOR_END__
  18. 000014a4 d __CTOR_LIST__
  19. 000014b0 d __DTOR_END__
  20. 000014ac d __DTOR_LIST__
  21. 000004a0 r __FRAME_END__
  22. 000014b4 d __JCR_END__
  23. 000014b4 d __JCR_LIST__
  24. 000015a4 A __bss_start
  25. w [email protected]@GLIBC_2.1.3
  26. 00000440 t __do_global_ctors_aux
  27. 00000350 t __do_global_dtors_aux
  28. 000014b8 d __dso_handle
  29. w __gmon_start__
  30. 00000419 t __i686.get_pc_thunk.bx
  31. 000015a4 A _edata
  32. 000015ac A _end
  33. 00000478 T _fini
  34. 000002ec T _init
  35. 000015a4 b completed.5963
  36. 000015a8 b dtor_idx.5965
  37. 000003e0 t frame_dummy
  38. 00000420 T print
  39. U [email protected]@GLIBC_2.0
  40. [[email protected] learn_nm]$

[[email protected] learn_nm]$ ls
main.c  test.c  test.h
[[email protected] learn_nm]$ gcc -c test.c
[[email protected] learn_nm]$ ar rcs libtest.a test.o
[[email protected] learn_nm]$ gcc -shared -fPIC -o libtest.so test.o
[[email protected] learn_nm]$ ls
libtest.a  libtest.so  main.c  test.c  test.h  test.o
[[email protected] learn_nm]$ nm lib*

libtest.a:

test.o:
00000000 T print
         U puts

libtest.so:
000014bc a _DYNAMIC
00001590 a _GLOBAL_OFFSET_TABLE_
         w _Jv_RegisterClasses
000014a8 d __CTOR_END__
000014a4 d __CTOR_LIST__
000014b0 d __DTOR_END__
000014ac d __DTOR_LIST__
000004a0 r __FRAME_END__
000014b4 d __JCR_END__
000014b4 d __JCR_LIST__
000015a4 A __bss_start
         w [email protected]@GLIBC_2.1.3
00000440 t __do_global_ctors_aux
00000350 t __do_global_dtors_aux
000014b8 d __dso_handle
         w __gmon_start__
00000419 t __i686.get_pc_thunk.bx
000015a4 A _edata
000015ac A _end
00000478 T _fini
000002ec T _init
000015a4 b completed.5963
000015a8 b dtor_idx.5965
000003e0 t frame_dummy
00000420 T print
         U [email protected]@GLIBC_2.0
[[email protected] learn_nm]$

可以看到, 我们可以从静态库和动态库中获取到函数名称, 如print函数。

好, 我们再来看看全局变量的情形, 我们把main.c改为:

[cpp] view plain copy print?

  1. #include <stdio.h>
  2. int add(int x, int y)
  3. {
  4. return x + y;
  5. }
  6. int aaa;
  7. int bbb = 1;
  8. char szTest[] = "good";
  9. int main()
  10. {
  11. int ccc = 2;
  12. return 0;
  13. }

#include <stdio.h>

int add(int x, int y)
{
	return x + y;
}

int aaa;
int bbb = 1;
char szTest[] = "good";

int main()
{
	int ccc = 2;
	return 0;
}

然后用nm分析a.out(注意, 如果只有nm命令, 则默认a.out为其要处理的文件):

[plain] view plain copy print?

  1. [[email protected] learn_nm]$ ls
  2. main.c
  3. [[email protected] learn_nm]$ gcc main.c
  4. [[email protected] learn_nm]$ ./a.out
  5. [[email protected] learn_nm]$ nm a.out
  6. 08049538 d _DYNAMIC
  7. 08049604 d _GLOBAL_OFFSET_TABLE_
  8. 0804847c R _IO_stdin_used
  9. w _Jv_RegisterClasses
  10. 08049528 d __CTOR_END__
  11. 08049524 d __CTOR_LIST__
  12. 08049530 D __DTOR_END__
  13. 0804952c d __DTOR_LIST__
  14. 08048520 r __FRAME_END__
  15. 08049534 d __JCR_END__
  16. 08049534 d __JCR_LIST__
  17. 08049628 A __bss_start
  18. 08049618 D __data_start
  19. 08048430 t __do_global_ctors_aux
  20. 08048310 t __do_global_dtors_aux
  21. 08048480 R __dso_handle
  22. w __gmon_start__
  23. 0804842a T __i686.get_pc_thunk.bx
  24. 08049524 d __init_array_end
  25. 08049524 d __init_array_start
  26. 080483c0 T __libc_csu_fini
  27. 080483d0 T __libc_csu_init
  28. U [email protected]@GLIBC_2.0
  29. 08049628 A _edata
  30. 08049634 A _end
  31. 0804845c T _fini
  32. 08048478 R _fp_hw
  33. 08048274 T _init
  34. 080482e0 T _start
  35. 08049630 B aaa
  36. 08048394 T add
  37. 0804961c D bbb
  38. 08049628 b completed.5963
  39. 08049618 W data_start
  40. 0804962c b dtor_idx.5965
  41. 08048370 t frame_dummy
  42. 080483a2 T main
  43. 08049620 D szTest
  44. [[email protected] learn_nm]$

[[email protected] learn_nm]$ ls
main.c
[[email protected] learn_nm]$ gcc main.c
[[email protected] learn_nm]$ ./a.out
[[email protected] learn_nm]$ nm a.out
08049538 d _DYNAMIC
08049604 d _GLOBAL_OFFSET_TABLE_
0804847c R _IO_stdin_used
         w _Jv_RegisterClasses
08049528 d __CTOR_END__
08049524 d __CTOR_LIST__
08049530 D __DTOR_END__
0804952c d __DTOR_LIST__
08048520 r __FRAME_END__
08049534 d __JCR_END__
08049534 d __JCR_LIST__
08049628 A __bss_start
08049618 D __data_start
08048430 t __do_global_ctors_aux
08048310 t __do_global_dtors_aux
08048480 R __dso_handle
         w __gmon_start__
0804842a T __i686.get_pc_thunk.bx
08049524 d __init_array_end
08049524 d __init_array_start
080483c0 T __libc_csu_fini
080483d0 T __libc_csu_init
         U [email protected]@GLIBC_2.0
08049628 A _edata
08049634 A _end
0804845c T _fini
08048478 R _fp_hw
08048274 T _init
080482e0 T _start
08049630 B aaa
08048394 T add
0804961c D bbb
08049628 b completed.5963
08049618 W data_start
0804962c b dtor_idx.5965
08048370 t frame_dummy
080483a2 T main
08049620 D szTest
[[email protected] learn_nm]$

可以看到, 不仅有add函数, 还有全局变量aaa, bbb和szTest, 要注意, aaa是未初始化的, 所以在Bss段, 而bbb、szTest是初始化了的, 所以在Data段。 值得注意的是, 并没有ccc, 因为ccc是局部变量, nm看不到的。

我们还应该注意到, 在上面看不到"good", 为啥呢? 因为nm是用来看szTest而非"good"的。 别忘了, 我们之前介绍过的strings命令可干这事, 如下:

[plain] view plain copy print?

  1. [[email protected] learn_nm]$ ls
  2. a.out  main.c
  3. [[email protected] learn_nm]$ strings a.out
  4. /lib/ld-linux.so.2
  5. __gmon_start__
  6. libc.so.6
  7. _IO_stdin_used
  8. __libc_start_main
  9. GLIBC_2.0
  10. PTRh
  11. [^_]
  12. good
  13. [[email protected] learn_nm]$

[[email protected] learn_nm]$ ls
a.out  main.c
[[email protected] learn_nm]$ strings a.out
/lib/ld-linux.so.2
__gmon_start__
libc.so.6
_IO_stdin_used
__libc_start_main
GLIBC_2.0
PTRh
[^_]
good
[[email protected] learn_nm]$

nm命令主要列出特性文件中的符号信息, 具体更加详细的用法, 请问man, 我就不再过多介绍了。

时间: 2024-08-10 02:08:41

linux中的nm命令简介的相关文章

linux中的strings命令简介

摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道strings命令的.我们先用man strings来看看: strings - print the strings of printable characters in files. 意思是, 打印文件中可打印的字符.  我来补充一下吧, 这个文件可以是文本文件(test.c), 可执行文件(te

linux中的strings命令简介2

摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 之前我们聊过linux strings的用法和用途, 但据我了解, 还有部分朋友并不常用strings, 这是个不好的习惯. 所以, 本文继续啰嗦一下strings命令. 在软件开发中, 我们经常需要修改代码, 并生成静态库.动态库或者可执行文件, 有时候, 工程太大, 那怎样确定自己改动的代码正确编译到库中去了呢? 用strings命令吧!  

linux中的strip命令简介------给文件脱衣服

转载于:http://blog.csdn.net/stpeace/article/details/47090255 作为一名linux开发人员, 如果没有听说过strip命令, 那是很不应该的. strip这个单词, 大家应该早就学过了, 你就记住是脱衣服就行了, 别的不要多想. 在linux中, strip也有脱衣服的含义, 具体就是从特定文件中剥掉一些符号信息和调试信息. 我们来看main.c文件: [cpp] view plain copy print? #include <stdio.h

linux中的压缩命令详细解析(二)

我们在<Linux中的压缩命令详细解析(一)>中已经讲解了常见的三种压缩命令,下面我们开始讲解工作中最常用到的tar命令. 为了使压缩和解压缩变得简单,tar命令就应运而生了.那么究竟该如何使用呢? tar.gz格式: 压缩命令: tar -zcvf 压缩文件名 源文件名 举例: 把abc文件压缩成后缀为tar.gz格式的文件 tar -zcvf abc.tar.gz abc 解压缩命令: 举例:解压缩abc.tar.gz文件 tar -zxvf abc.tar.gz tar.bz2格式: 压

教你在Linux中如何用命令或手动修改文件来添加一个用户

教你在Linux中如何使用命令或手动修改文件添加一个用户 首先我们从一个例子进行引入:添加一个happy用户,基本组为happy(5200),附加组为luzhi. 一.用命令的方法实现: groupadd -g 5200 happy useradd -u 5200 -g happy -G luzhi  happy passwd happy su - happy 这样就这个用户就创建成功了. 下面来演示一下: 验证系统中是否存在happy用户,从输出看是没有存在happy用户的. 2.我们先建一个

Linux中的In命令

ln是linux中一个非常重要命令.它的功能是为某一个文件在另外一个位置建立一个同步的链接,这个命令最常用的参数是-s,具体用法是: ln -s  源文件 目标文件    -s 是 symbolic的意思. 例:ln  -s  /lib/lsb   /usr/lj即:在usr目录下建立指向/lib/lsb目录的lj文件. 当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目录下用ln命令链接(link

辛星浅析linux中的ac命令

linux中的ac命令根据当前/var/log/wtmp文件中的登录的进入和退出来报告用户连接的时间,默认是以小时为单位,如果不使用标识,则报告的是总时间. 它的主要参数有两个: (1)-d将显示每天的连接时间. (2)-p将显示每个用户的连接时间.

Linux中history历史命令使用方法详解

在/etc/profile里添加如下: #History export HISTTIMEFORMAT="[%F %T]" HISTDIR=/home/common/.hist if [ ! -d $HISTDIR ]; then         mkdir -p $HISTDIR         chmod 777 $HISTDIR fi export HISTSIZE=100000 export HISTFILE="$HISTDIR/${LOGNAME}.hist"

一张图详解Linux中的top命令

一张图详解Linux中的top命令及每个参数的含义: