linux中的nm命令简介
转:http://blog.csdn.net/stpeace/article/details/47089585
一般来说, 搞linux开发的人, 才会用到nm命令, 非开发的人, 应该用不到。 虽然nm很简单, 但是还是有必要写几句, 聊表心意。
nm不是ni ma的缩写, 当然, 也不是ni mei的缩写, 而是names的缩写, nm命令主要是用来列出某些文件中的符号(说白了就是一些函数和全局变量等)。 下面, 我们一起来看看。
test.h为:
- void print();

void print();
test.c为:
- #include <stdio.h>
- #include "test.h"
- void print()
- {
- printf("rainy days\n");
- }

#include <stdio.h>
#include "test.h" void print()
{
printf("rainy days\n");
}
main.c为:
- #include "test.h"
- int main()
- {
- print();
- return 0;
- }

#include "test.h" int main()
{
print();
return 0;
}
好, 我们看看nm命令的作用效果, 如下:
- [taoge@localhost learn_nm]$ nm *
- nm: main.c: File format not recognized
- nm: test.c: File format not recognized
- nm: test.h: File format not recognized
- [taoge@localhost learn_nm]$

[taoge@localhost learn_nm]$ nm *
nm: main.c: File format not recognized
nm: test.c: File format not recognized
nm: test.h: File format not recognized
[taoge@localhost learn_nm]$
ni ma, 啥都没有, 这说明nm对这类文件无用。
继续看nm能否读取目标文件和可执行文件:
- [taoge@localhost learn_nm]$ ls
- main.c test.c test.h
- [taoge@localhost learn_nm]$ gcc -c test.c main.c
- [taoge@localhost learn_nm]$ gcc test.o main.o
- [taoge@localhost learn_nm]$ ./a.out
- rainy days
- [taoge@localhost 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 __libc_start_main@@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 puts@@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
- [taoge@localhost learn_nm]$

[taoge@localhost learn_nm]$ ls
main.c test.c test.h
[taoge@localhost learn_nm]$ gcc -c test.c main.c
[taoge@localhost learn_nm]$ gcc test.o main.o
[taoge@localhost learn_nm]$ ./a.out
rainy days
[taoge@localhost 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 __libc_start_main@@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 puts@@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
[taoge@localhost learn_nm]$
可以看到, 对于目标文件和可执行文件而言, 均可以获得其中的函数, 如print函数。
我们继续看静态库和动态库, 如下:
- [taoge@localhost learn_nm]$ ls
- main.c test.c test.h
- [taoge@localhost learn_nm]$ gcc -c test.c
- [taoge@localhost learn_nm]$ ar rcs libtest.a test.o
- [taoge@localhost learn_nm]$ gcc -shared -fPIC -o libtest.so test.o
- [taoge@localhost learn_nm]$ ls
- libtest.a libtest.so main.c test.c test.h test.o
- [taoge@localhost 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 __cxa_finalize@@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 puts@@GLIBC_2.0
- [taoge@localhost learn_nm]$

[taoge@localhost learn_nm]$ ls
main.c test.c test.h
[taoge@localhost learn_nm]$ gcc -c test.c
[taoge@localhost learn_nm]$ ar rcs libtest.a test.o
[taoge@localhost learn_nm]$ gcc -shared -fPIC -o libtest.so test.o
[taoge@localhost learn_nm]$ ls
libtest.a libtest.so main.c test.c test.h test.o
[taoge@localhost 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 __cxa_finalize@@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 puts@@GLIBC_2.0
[taoge@localhost learn_nm]$
可以看到, 我们可以从静态库和动态库中获取到函数名称, 如print函数。
好, 我们再来看看全局变量的情形, 我们把main.c改为:
- #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;
- }

#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为其要处理的文件):
- [taoge@localhost learn_nm]$ ls
- main.c
- [taoge@localhost learn_nm]$ gcc main.c
- [taoge@localhost learn_nm]$ ./a.out
- [taoge@localhost 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 __libc_start_main@@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
- [taoge@localhost learn_nm]$

[taoge@localhost learn_nm]$ ls
main.c
[taoge@localhost learn_nm]$ gcc main.c
[taoge@localhost learn_nm]$ ./a.out
[taoge@localhost 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 __libc_start_main@@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
[taoge@localhost learn_nm]$
可以看到, 不仅有add函数, 还有全局变量aaa, bbb和szTest, 要注意, aaa是未初始化的, 所以在Bss段, 而bbb、szTest是初始化了的, 所以在Data段。 值得注意的是, 并没有ccc, 因为ccc是局部变量, nm看不到的。
我们还应该注意到, 在上面看不到"good", 为啥呢? 因为nm是用来看szTest而非"good"的。 别忘了, 我们之前介绍过的strings命令可干这事, 如下:
- [taoge@localhost learn_nm]$ ls
- a.out main.c
- [taoge@localhost 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
- [taoge@localhost learn_nm]$

[taoge@localhost learn_nm]$ ls
a.out main.c
[taoge@localhost 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
[taoge@localhost learn_nm]$
nm命令主要列出特性文件中的符号信息, 具体更加详细的用法, 请问man, 我就不再过多介绍了。
linux中的nm命令简介的更多相关文章
- linux中的strings命令简介2
摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 之前我们聊过linux strings的用法和用 ...
- linux中的strings命令简介
摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道 ...
- linux中的ldd命令简介
转载自:http://blog.csdn.net/stpeace/article/details/47069215 在linux中, 有些命令是大家通用的, 比如ls, rm, mv, cp等等, 这 ...
- linux中的strip命令简介------给文件脱衣服
1.去掉-g,等于程序做了--strip-debug2.strip程序,等于程序做了--strip-debug和--strip-symbol 作为一名Linux开发人员, 如果没有听说过strip命令 ...
- linux中的strip命令简介------给文件脱衣服【转】
转自:http://blog.csdn.net/stpeace/article/details/47090255 版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则 ...
- linux中的strip命令简介
转载:https://blog.csdn.net/qq_37858386/article/details/78559490 strip:去除,剥去 一.下面是man strip获得到的信息,简 ...
- Python学习之旅:使用Python实现Linux中的ls命令
一.写在前面 前几天在微信上看到这样一篇文章,链接为:https://mp.weixin.qq.com/s/rl6Sgv3uk_IpoFAx6cWa8w,在这篇文章中,有这样一段话,吸引了我的注意: ...
- Linux中的历史命令
Linux中的历史命令一般保存在用户 /root/.bash_history history 选项 历史命令保存文件夹 选项 -c:清空历史命令 -w :把缓存中的历史命令写入历 ...
- 关于XShell的常见使用和设置以及Linux中的常见命令.
本文部分转自:http://sundful.iteye.com/blog/704079 和 http://www.vckai.com/p/5 有时候在XShell中操作的一些命令傻傻的分不清这个命令到 ...
随机推荐
- D3D9 优化小技巧
此篇文章主要讲一些小技巧,针对前面转载的D3D9 GPU Hacks,我们可以做的一些优化. 在做延迟渲染或者其它需要深度的地方使用INTZ格式的纹理,这样可以直接对纹理进行操作,节省了显存和带宽,这 ...
- mysql导sql脚本
在navicat或sql yog ,或dos窗口中,如下 命令 ->mysql source d:\test.sql;
- IntelliJ IDEA 项目相关的几个重要概念介绍
必备材料介绍 IntelliJ IDEA 对其他 IDE 转过来的用户有特别优待,对其专门整理了非常棒的资料,还请其他 IDE 过来的用户抽时间查看,会有很大帮助:Eclipse 用户可以看:http ...
- 一群猴子排成一圈,按1,2,...,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去...,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入m、n, 输出最后那个大王的编号
<?php/** * [猴子选大王] * @param [type] $m [猴子数] * @param [type] $n [出局次数] * @return [type] [desc ...
- linux时间的查看与修改
1.查看时间和日期 date 2.设置时间和日期 将系统日期设定成1996年6月10日的命令 date -s 06/22/96 将系统时间设定成下午1点52分0秒的命令 date -s 13:52:0 ...
- java使用json将HashMap转化成javabean小例子
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import net.sf.json.JSONOb ...
- paper 90:人脸检测研究2015最新进展
搜集整理了2004~2015性能最好的人脸检测的部分资料,欢迎交流和补充相关资料. 1:人脸检测性能 1.1 人脸检测测评 目前有两个比较大的人脸测评网站: 1:Face Detection Data ...
- 14---Net基础加强
更新中,敬请期待............ 复习-匿名类型 Xml介绍
- 夺命雷公狗---DEDECMS----12dedecms全局标签的使用以及嵌套标签的使用
在网站开发中,在很多页面可能会使用到同一个变量,比如路径网站信息等,所以我们可以用全局变量来使用. 默认的放在: 进去里面看下就会发现很多的常量都是在这里定义的: 我们在实际开发的时候可以将我们在多个 ...
- clock gating and PLL
一个gating的clock是指:clock network除了包含inverter和buffer外,还有其他logic. PrimeTime会自动的对gating input进行setup和hold ...