做個筆記:
linux 下查看符號表工具:nm
操作如下:
linux的nm命令可以一個檔案中的符號列表,列出以上代碼Gcc -c編譯出的a.o(a.a a.so)可以通過nm命令來查看其中的符號資訊: 源碼列印。 0000000000000000 t 0000000000000000 d 0000000000000000 b 0000000000000000 r 0000000000000000 r 0000000000000000 n 0000000000000000 n 0000000000000000 B g1 0000000000000004 B g2 0000000000000008 b g3 000000000000000c b g4 000000000000001c r g5 0000000000000020 r g6 U __gxx_personality_v0 0000000000000000 T main 0000000000000000 a nm.cpp U printf 000000000000003e T _Z4foo1v 0000000000000044 t _Z4foo2v 0000000000000054 T _Z8overloadf 000000000000004a T _Z8overloadi 0000000000000010 b _ZZ4mainE2st
其中左邊第一列是符號的地址值,對應源碼可以看出遞增的規律。第二列是該符號的類型,第三列是符號的名稱(比如函數名,變數名):
符號類型:介紹幾個最常用的,其他的如果遇到了直接Google:
B --- 全域非初始化資料區段(BBS段)的符號,其值表示該符號在bss段中的位移,如g1
b --- 全域static的符號,如g3
r --- const型唯讀變數(readonly)
N --- debug用的符號
T --- 位於代碼區的符號,比如本檔案裡的函數main foo
t --- 位於代碼區的符號,一般是static函數
U --- 位於本檔案外的調用函數或變數符號,比如系統的printf()函數
這裡要注意的是,本人使用g++編譯的,所以是按c++的支援重載的函數風格編譯的,可以看到所有函數均帶了首碼和尾碼,首碼代表屬於類的名字,尾碼代表參數列表的類型縮寫,因為重載必須是區分參數類型,這裡也可以看出,為什麼傳回值不同的函數不是重載,因為符號表裡沒有傳回值的記錄。
例如兩個overload函數的尾碼分別是f和i代表一個是float型一個是int型(上面還有v ->void型)。
windows 下查看符號表工具:dumpbin
操作如下:
查看符號表
C:\Program Files\Microsoft Visual Studio\VC98\Lib>dumpbin /symbols msimg32.lib
Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file msimg32.lib
File Type: LIBRARY
COFF SYMBOL TABLE
000 00131F62 ABS notype Static | @comp.id
001 00000000 SECT2 notype External | __IMPORT_DESCRIPTOR_MSIMG32
002 C0000040 SECT2 notype Section | .idata$2
003 00000000 SECT3 notype Static | .idata$6
004 C0000040 UNDEF notype Section | .idata$4
005 C0000040 UNDEF notype Section | .idata$5
006 00000000 UNDEF notype External | __NULL_IMPORT_DESCRIPTOR
007 00000000 UNDEF notype External | MSIMG32_NULL_THUNK_DATA
String Table Size = 0x52 bytes
COFF SYMBOL TABLE
000 00131F62 ABS notype Static | @comp.id
001 00000000 SECT2 notype External | __NULL_IMPORT_DESCRIPTOR
String Table Size = 0x1D bytes
COFF SYMBOL TABLE
000 00131F62 ABS notype Static | @comp.id
001 00000000 SECT2 notype External | MSIMG32_NULL_THUNK_DATA
String Table Size = 0x1D bytes
Summary
AE .debug$S
14 .idata$2
14 .idata$3
4 .idata$4
4 .idata$5
C .idata$6
查看匯出函數
C:\Program Files\Microsoft Visual Studio\VC98\Lib>dumpbin /exports msimg32.lib
Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file msimg32.lib
File Type: LIBRARY
Exports
ordinal name
_AlphaBlend@44
_GradientFill@24
_TransparentBlt@44
Summary
AE .debug$S
14 .