linux的nm命令可以获取可执行文件里的符号表。
如可执行文件helloworld,
执行 nm helloworld
root@ubuntu:/share/learn#
root@ubuntu:/share/learn# nm helloworld
0804a024 B __bss_start
0804a024 b completed.7181
0804a018 D __data_start
0804a018 W data_start
08048360 t deregister_tm_clones
080483d0 t __do_global_dtors_aux
08049f0c t __do_global_dtors_aux_fini_array_entry
0804a01c D __dso_handle
08049f14 d _DYNAMIC
0804a024 D _edata
0804a028 B _end
08048504 T _fini
08048518 R _fp_hw
080483f0 t frame_dummy
08049f08 t __frame_dummy_init_array_entry
080486bc r __FRAME_END__
0804a000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0804841b T hello1_print
08048473 T hello2_print
080482b0 T _init
08049f0c t __init_array_end
08049f08 t __init_array_start
0804851c R _IO_stdin_used
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
08049f10 d __JCR_END__
08049f10 d __JCR_LIST__
w _Jv_RegisterClasses
08048500 T __libc_csu_fini
080484a0 T __libc_csu_init
U __libc_start_main@@GLIBC_2.0
0804844a T main
U puts@@GLIBC_2.0
08048390 t register_tm_clones
0804a020 D s
08048320 T _start
0804a024 D __TMC_END__
08048446 T __x86.get_pc_thunk.ax
08048350 T __x86.get_pc_thunk.bx
root@ubuntu:/share/learn#
如0804841b T hello1_print
第一列是当前符号的地址,第二列是当前符号的类型,第三列是当前符号的名称。
nm加上-C指定列出符号是demangle,可以使得结果更加易读,但本人测试C的代码,加上-C跟没加是一样的结果。
nm命令的用途:
1 主要查看可执行文件里有没有指定的符号
格式 nm -C 可执行文件 | grep 符号
如nm -C helloworld | grep hello1
2 解决程序编译时undefined reference的错误及mutiple definition
3 查看某个符号的地址,以及进程空间的大概位置bss、data、text区,具体可以通过第二列的类型来判断。