c++ 查看库文件符号表

来源:互联网 发布:黑客网络hacknet 攻略 编辑:程序博客网 时间:2024/05/16 17:27

做个笔记:


linux 下查看符号表工具:nm


操作如下:

  linux的nm命令可以一个文件中的符号列表,列出以上代码Gcc -c编译出的a.o(a.a a.so)可以通过nm命令来查看其中的符号信息:

源码打印?
  1. 0000000000000000 t   
  2. 0000000000000000 d   
  3. 0000000000000000 b   
  4. 0000000000000000 r   
  5. 0000000000000000 r   
  6. 0000000000000000 n   
  7. 0000000000000000 n   
  8. 0000000000000000 B g1  
  9. 0000000000000004 B g2  
  10. 0000000000000008 b g3  
  11. 000000000000000c b g4  
  12. 000000000000001c r g5  
  13. 0000000000000020 r g6  
  14.                  U __gxx_personality_v0  
  15. 0000000000000000 T main  
  16. 0000000000000000 a nm.cpp  
  17.                  U printf  
  18. 000000000000003e T _Z4foo1v  
  19. 0000000000000044 t _Z4foo2v  
  20. 0000000000000054 T _Z8overloadf  
  21. 000000000000004a T _Z8overloadi  
  22. 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 .idata$2
          14 .idata$3
           4 .idata$4
           4 .idata$5
           C .idata$6






0 0
原创粉丝点击