雑なメモ書き

気楽にいきます

nmコマンドで遊んでみた

nm - オブジェクトファイルのシンボルをリストする

--debug-syms

#include <stdio.h>

int main()
{
    printf("Hello,World\n");
    return 0;
}
  • このcファイルでgccを実行した場合
  • オプションがある場合と無い場合でこのような差が出る
  • 出ている差分はdebug用のシンボルだ
@@ -1,3 +1,29 @@
+0000000000000000 a
+0000000000201010 b .bss
+0000000000000000 n .comment
+0000000000201000 d .data
+0000000000200dc8 d .dynamic
+0000000000000360 r .dynstr
+00000000000002b8 r .dynsym
+0000000000000730 r .eh_frame
+00000000000006f0 r .eh_frame_hdr
+00000000000006d4 t .fini
+0000000000200dc0 t .fini_array
+0000000000000298 r .gnu.hash
+00000000000003e2 r .gnu.version
+00000000000003f0 r .gnu.version_r
+0000000000200fb8 d .got
+00000000000004e8 t .init
+0000000000200db8 t .init_array
+0000000000000238 r .interp
+0000000000000254 r .note.ABI-tag
+0000000000000274 r .note.gnu.build-id
+0000000000000500 t .plt
+0000000000000520 t .plt.got
+0000000000000410 r .rela.dyn
+00000000000004d0 r .rela.plt
+00000000000006e0 r .rodata
+0000000000000530 t .text
 0000000000200dc8 d _DYNAMIC
 0000000000200fb8 d _GLOBAL_OFFSET_TABLE_
 00000000000006e0 R _IO_stdin_used
@@ -25,9 +51,12 @@
 00000000000004e8 T _init
 0000000000000530 T _start
 0000000000201010 b completed.7696
+0000000000000000 a crtstuff.c
+0000000000000000 a crtstuff.c
 0000000000201000 W data_start
 0000000000000560 t deregister_tm_clones
 0000000000000630 t frame_dummy
 000000000000063a T main
+0000000000000000 a main.c
                  U puts@@GLIBC_2.2.5
 00000000000005a0 t register_tm_clones

-A

  • ファイル名が付与される
./a.out:0000000000200dc8 d _DYNAMIC
./a.out:0000000000200fb8 d _GLOBAL_OFFSET_TABLE_
./a.out:00000000000006e0 R _IO_stdin_used
./a.out:                 w _ITM_deregisterTMCloneTable
./a.out:                 w _ITM_registerTMCloneTable
./a.out:0000000000000834 r __FRAME_END__
./a.out:00000000000006f0 r __GNU_EH_FRAME_HDR
./a.out:0000000000201010 D __TMC_END__
./a.out:0000000000201010 B __bss_start
./a.out:                 w __cxa_finalize@@GLIBC_2.2.5
./a.out:0000000000201000 D __data_start
./a.out:00000000000005f0 t __do_global_dtors_aux
./a.out:0000000000200dc0 t __do_global_dtors_aux_fini_array_entry
./a.out:0000000000201008 D __dso_handle
./a.out:0000000000200db8 t __frame_dummy_init_array_entry
./a.out:                 w __gmon_start__
./a.out:0000000000200dc0 t __init_array_end
./a.out:0000000000200db8 t __init_array_start
./a.out:00000000000006d0 T __libc_csu_fini
./a.out:0000000000000660 T __libc_csu_init
./a.out:                 U __libc_start_main@@GLIBC_2.2.5
./a.out:0000000000201010 D _edata
./a.out:0000000000201018 B _end
./a.out:00000000000006d4 T _fini
./a.out:00000000000004e8 T _init
./a.out:0000000000000530 T _start
./a.out:0000000000201010 b completed.7696
./a.out:0000000000201000 W data_start
./a.out:0000000000000560 t deregister_tm_clones
./a.out:0000000000000630 t frame_dummy
./a.out:000000000000063a T main
./a.out:                 U puts@@GLIBC_2.2.5
./a.out:00000000000005a0 t register_tm_clones

-D

  • 共有ライブラリの表示
 nm -D ./a.out
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w __cxa_finalize
                 w __gmon_start__
                 U __libc_start_main
                 U puts

-g

  • 外部シンボルを表示
00000000000006e0 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000201010 D __TMC_END__
0000000000201010 B __bss_start
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000201000 D __data_start
0000000000201008 D __dso_handle
                 w __gmon_start__
00000000000006d0 T __libc_csu_fini
0000000000000660 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
0000000000201010 D _edata
0000000000201018 B _end
00000000000006d4 T _fini
00000000000004e8 T _init
0000000000000530 T _start
0000000000201000 W data_start
000000000000063a T main
                 U puts@@GLIBC_2.2.5

-u

  • そのファイルで未定義なシンボルのみを表示
 nm -u ./a.out
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w __cxa_finalize@@GLIBC_2.2.5
                 w __gmon_start__
                 U __libc_start_main@@GLIBC_2.2.5
                 U puts@@GLIBC_2.2.5

-l

  • debug情報がある場合に行数を出してくれる
000000000000063a T main  /data/nm/main.c:3
                 U puts@@GLIBC_2.2.5
00000000000005a0 t register_tm_clones

その他

nm -o /usr/lib/*.a | grep finding_function