栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

linux下编译生成库函数的符号查看

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux下编译生成库函数的符号查看

个人随笔 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
参考:https://www.csdn.net/tags/MtjaUgwsODMzNS1ibG9n.html
参考:https://zhuanlan.zhihu.com/p/112754720

符号查看

不同于window下,在Linux下:
编译.o文件、
静态库.a文件、
动态库.so文件、
生成的可执行程序
都可以方便的查看函数符号。

通常使用readelf 和 nm两个命令进行符号查看,另外对于c++程序可以使用c++filt来把c++函数符号解析出来查看。

样例-编译文件.o的查看

例如下面的程序,编译生成.o文件

$ cat func.cpp 
#include "stdio.h"
int test_func_1(int id)
{
   printf("call test %dn", id);
   return 0;
}
$ g++ -c func.cpp -o func.o

使用nm对生成的编译.o文件进行查看:

$ nm -o func.o
func.o:                 U printf
func.o:0000000000000000 T _Z11test_func_1i

$ nm -o func.o | c++filt
func.o:                 U printf
func.o:0000000000000000 T test_func_1(int)

使用readlf对生成的编译.o文件进行查看

$ readelf -s func.o 

Symbol table '.symtab' contains 11 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS func.cpp
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT    7 
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT    8 
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
     9: 0000000000000000    38 FUNC    GLOBAL DEFAULT    1 _Z11test_func_1i
    10: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND printf
    
$ readelf -s func.o |c++filt

Symbol table '.symtab' contains 11 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS func.cpp
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT    7 
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT    8 
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
     9: 0000000000000000    38 FUNC    GLOBAL DEFAULT    1 test_func_1(int)
    10: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND printf
样例-编译静态库.a的查看

还是上面的test.cpp的程序,编译生成.a文件

$ g++ -c func.cpp -o func.o

$ ar -r func.a func.o
ar: creating func.a

使用nm/readelf/c++filt查看.a文件

$ nm -o func.a|c++filt
func.a:func.o:                 U printf
func.a:func.o:0000000000000000 T test_func_1(int)

$ readelf -c func.a|c++filt
Index of archive func.a: (1 entries, 0x12 bytes in the symbol table)
Contents of binary func.a(func.o) at offset 0x5e
        test_func_1(int)
样例-编译静态库.so的查看

还是上面的程序,编译生成.so文件

$ g++ -c func.cpp -o func.o -fPIC
$ g++ -shared -o func.so func.o

使用nm/readelf/c++filt查看.so文件

$ nm -o func.so|c++filt
func.so:0000000000201028 B __bss_start
func.so:0000000000201028 b completed.7295
func.so:                 w __cxa_finalize@@GLIBC_2.2.5
func.so:0000000000000540 t deregister_tm_clones
func.so:00000000000005b0 t __do_global_dtors_aux
func.so:0000000000200de0 t __do_global_dtors_aux_fini_array_entry
func.so:0000000000200de8 d __dso_handle
func.so:0000000000200df0 d _DYNAMIC
func.so:0000000000201028 D _edata
func.so:0000000000201030 B _end
func.so:0000000000000624 t _fini
func.so:00000000000005f0 t frame_dummy
func.so:0000000000200dd8 t __frame_dummy_init_array_entry
func.so:00000000000006c0 r __FRAME_END__
func.so:0000000000201000 d _GLOBAL_OFFSET_TABLE_
func.so:                 w __gmon_start__
func.so:0000000000000640 r __GNU_EH_FRAME_HDR
func.so:00000000000004e8 t _init
func.so:                 w _ITM_deregisterTMCloneTable
func.so:                 w _ITM_registerTMCloneTable
func.so:                 U printf@@GLIBC_2.2.5
func.so:0000000000000570 t register_tm_clones
func.so:0000000000201028 d __TMC_END__
func.so:00000000000005f9 T test_func_1(int)

$ readelf -s func.so|c++filt

Symbol table '.dynsym' contains 10 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
     2: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
     6: 0000000000201028     0 NOTYPE  GLOBAL DEFAULT   21 _edata
     7: 0000000000201030     0 NOTYPE  GLOBAL DEFAULT   22 _end
     8: 00000000000005f9    40 FUNC    GLOBAL DEFAULT   11 test_func_1(int)
     9: 0000000000201028     0 NOTYPE  GLOBAL DEFAULT   22 __bss_start

Symbol table '.symtab' contains 53 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
   ...   
样例-编译可执行程序的查看

使用上面生成的.a静态库,加上一个main.cpp来编译生成可执行程序

$ cat main.cpp 
int test_func_1(int);
int main(int argc, char** argv)
{
  return test_func_1(10);
}
$ g++ main.cpp func.a -o a.out

使用nm/c++filt查看a.out可执行程序文件

$ nm -o a.out|c++filt
a.out:0000000000400505 t .annobin__dl_relocate_static_pie.end
a.out:0000000000400500 t .annobin__dl_relocate_static_pie.start
a.out:0000000000400600 t .annobin_elf_init.c
a.out:0000000000400675 t .annobin_elf_init.c_end
a.out:00000000004004d0 t .annobin_elf_init.c_end.exit
a.out:00000000004004d0 t .annobin_elf_init.c_end.hot
a.out:00000000004004d0 t .annobin_elf_init.c_end.startup
a.out:00000000004004d0 t .annobin_elf_init.c_end.unlikely
a.out:00000000004004d0 t .annobin_elf_init.c.exit
a.out:00000000004004d0 t .annobin_elf_init.c.hot
a.out:00000000004004d0 t .annobin_elf_init.c.startup
a.out:00000000004004d0 t .annobin_elf_init.c.unlikely
a.out:00000000004004ff t .annobin_init.c
a.out:00000000004004ff t .annobin_init.c_end
a.out:00000000004004d0 t .annobin_init.c_end.exit
a.out:00000000004004d0 t .annobin_init.c_end.hot
a.out:00000000004004d0 t .annobin_init.c_end.startup
a.out:00000000004004d0 t .annobin_init.c_end.unlikely
a.out:00000000004004d0 t .annobin_init.c.exit
a.out:00000000004004d0 t .annobin_init.c.hot
a.out:00000000004004d0 t .annobin_init.c.startup
a.out:00000000004004d0 t .annobin_init.c.unlikely
a.out:0000000000400675 t .annobin___libc_csu_fini.end
a.out:0000000000400665 t .annobin___libc_csu_fini.start
a.out:0000000000400665 t .annobin___libc_csu_init.end
a.out:0000000000400600 t .annobin___libc_csu_init.start
a.out:0000000000400500 t .annobin_static_reloc.c
a.out:0000000000400505 t .annobin_static_reloc.c_end
a.out:00000000004004d0 t .annobin_static_reloc.c_end.exit
a.out:00000000004004d0 t .annobin_static_reloc.c_end.hot
a.out:00000000004004d0 t .annobin_static_reloc.c_end.startup
a.out:00000000004004d0 t .annobin_static_reloc.c_end.unlikely
a.out:00000000004004d0 t .annobin_static_reloc.c.exit
a.out:00000000004004d0 t .annobin_static_reloc.c.hot
a.out:00000000004004d0 t .annobin_static_reloc.c.startup
a.out:00000000004004d0 t .annobin_static_reloc.c.unlikely
a.out:0000000000601024 B __bss_start
a.out:0000000000601024 b completed.7295
a.out:0000000000601020 D __data_start
a.out:0000000000601020 W data_start
a.out:0000000000400510 t deregister_tm_clones
a.out:0000000000400500 T _dl_relocate_static_pie
a.out:0000000000400580 t __do_global_dtors_aux
a.out:0000000000600dd8 t __do_global_dtors_aux_fini_array_entry
a.out:0000000000400690 R __dso_handle
a.out:0000000000600de0 d _DYNAMIC
a.out:0000000000601024 D _edata
a.out:0000000000601028 B _end
a.out:0000000000400678 T _fini
a.out:00000000004005b0 t frame_dummy
a.out:0000000000600dd0 t __frame_dummy_init_array_entry
a.out:00000000004007f4 r __FRAME_END__
a.out:0000000000601000 d _GLOBAL_OFFSET_TABLE_
a.out:                 w __gmon_start__
a.out:00000000004006a8 r __GNU_EH_FRAME_HDR
a.out:0000000000400488 T _init
a.out:0000000000600dd8 t __init_array_end
a.out:0000000000600dd0 t __init_array_start
a.out:0000000000400688 R _IO_stdin_used
a.out:                 w _ITM_deregisterTMCloneTable
a.out:                 w _ITM_registerTMCloneTable
a.out:0000000000400670 T __libc_csu_fini
a.out:0000000000400600 T __libc_csu_init
a.out:                 U __libc_start_main@@GLIBC_2.2.5
a.out:00000000004005b6 T main
a.out:                 U printf@@GLIBC_2.2.5
a.out:0000000000400540 t register_tm_clones
a.out:00000000004004d0 T _start
a.out:0000000000601028 D __TMC_END__
a.out:00000000004005d2 T test_func_1(int)

个人随笔 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号