转自:https://blog.csdn.net/swedenfeng/article/details/53417085

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/swedenfeng/article/details/53417085

1) 如何生成不含符号表的目标文件 ?

#一个方式是编译的时候使用 选项 s

root@root2768:~/test_c>gcc -s test1.c

root@root2768:~/test_c> objdump -t a.out

a.out:     file format elf64-x86-64

SYMBOL TABLE:
no symbols

# 另外一个方式是,生成目标文件以后,使用strip命令从目标文件中去掉符号表

root@root2768:~/test_c> gcc test1.c
root@root2768:~/test_c> objdump -t a.out

a.out:     file format elf64-x86-64

SYMBOL TABLE:
0000000000400238 l    d  .interp 0000000000000000              .interp
0000000000400254 l    d  .note.ABI-tag 0000000000000000              .note.ABI-tag
0000000000400274 l    d  .note.SuSE 0000000000000000              .note.SuSE
000000000040028c l    d  .note.gnu.build-id
0000000000000000              .note.gnu.build-id
00000000004002b0 l    d  .hash 0000000000000000              .hash
00000000004002c8 l    d  .gnu.hash 0000000000000000              .gnu.hash
00000000004002e8 l    d  .dynsym 0000000000000000              .dynsym
0000000000400330 l    d  .dynstr 0000000000000000              .dynstr
0000000000400368 l    d  .gnu.version 0000000000000000              .gnu.version
0000000000400370 l    d  .gnu.version_r 0000000000000000              .gnu.version_r
0000000000400390 l    d  .rela.dyn 0000000000000000              .rela.dyn
00000000004003a8 l    d  .rela.plt 0000000000000000              .rela.plt
00000000004003c0 l    d  .init 0000000000000000              .init
00000000004003e0 l    d  .plt 0000000000000000              .plt
0000000000400400 l    d  .text 0000000000000000              .text
00000000004005d8 l    d  .fini 0000000000000000              .fini
00000000004005f0 l    d  .rodata 0000000000000000              .rodata
00000000004005f4 l    d  .eh_frame_hdr 0000000000000000              .eh_frame_hdr
0000000000400628 l    d  .eh_frame 0000000000000000              .eh_frame
0000000000600e30 l    d  .ctors 0000000000000000              .ctors
0000000000600e40 l    d  .dtors 0000000000000000              .dtors
0000000000600e50 l    d  .jcr 0000000000000000              .jcr
0000000000600e58 l    d  .dynamic 0000000000000000              .dynamic
0000000000600ff8 l    d  .got 0000000000000000              .got
0000000000601000 l    d  .got.plt 0000000000000000              .got.plt
0000000000601020 l    d  .data 0000000000000000              .data
0000000000601038 l    d  .bss 0000000000000000              .bss
0000000000000000 l    d  .comment 0000000000000000              .comment
0000000000000000 l    d  .comment.SUSE.OPTs
0000000000000000              .comment.SUSE.OPTs
0000000000000000 l    d  .debug_aranges 0000000000000000              .debug_aranges
0000000000000000 l    d  .debug_pubnames 0000000000000000              .debug_pubnames
0000000000000000 l    d  .debug_info 0000000000000000              .debug_info
0000000000000000 l    d  .debug_abbrev 0000000000000000              .debug_abbrev
0000000000000000 l    d  .debug_line 0000000000000000              .debug_line
0000000000000000 l    d  .debug_frame 0000000000000000              .debug_frame
0000000000000000 l    d  .debug_str 0000000000000000              .debug_str
0000000000000000 l    d  .debug_loc 0000000000000000              .debug_loc
0000000000000000 l    d  .debug_ranges 0000000000000000              .debug_ranges
0000000000000000 l    df *ABS* 0000000000000000              init.c
0000000000000000 l    df *ABS* 0000000000000000              
0000000000000000 l    df *ABS* 0000000000000000              initfini.c
000000000040042c l     F .text 0000000000000000              call_gmon_start
00000000004005e8 l       .fini 0000000000000000              _real_fini
0000000000000000 l    df *ABS* 0000000000000000              crtstuff.c
0000000000600e30 l     O .ctors 0000000000000000              __CTOR_LIST__
0000000000600e40 l     O .dtors 0000000000000000              __DTOR_LIST__
0000000000600e50 l     O .jcr 0000000000000000              __JCR_LIST__
0000000000400450 l     F .text 0000000000000000              __do_global_dtors_aux
0000000000601038 l     O .bss 0000000000000001              completed.6159
0000000000601040 l     O .bss 0000000000000008              dtor_idx.6161
00000000004004c0 l     F .text 0000000000000000              frame_dummy
0000000000000000 l    df *ABS* 0000000000000000              crtstuff.c
0000000000600e38 l     O .ctors 0000000000000000              __CTOR_END__
0000000000400700 l     O .eh_frame 0000000000000000              __FRAME_END__
0000000000600e50 l     O .jcr 0000000000000000              __JCR_END__
00000000004005a0 l     F .text 0000000000000000              __do_global_ctors_aux
0000000000000000 l    df *ABS* 0000000000000000              initfini.c
0000000000000000 l    df *ABS* 0000000000000000              test1.c
0000000000000000 l    df *ABS* 0000000000000000              elf-init.c
0000000000000000 l    df *ABS* 0000000000000000              
0000000000600e2c l       .ctors 0000000000000000              __init_array_end
0000000000600e58 l     O .dynamic 0000000000000000              _DYNAMIC
0000000000600e2c l       .ctors 0000000000000000              __init_array_start
0000000000601000 l     O .got.plt 0000000000000000              _GLOBAL_OFFSET_TABLE_
0000000000400500 g     F .text 0000000000000002              __libc_csu_fini
0000000000601048 g     O .bss 0000000000000004              global_x
0000000000601020  w      .data 0000000000000000              data_start
0000000000601034 g       .data 0000000000000000              _edata
00000000004005d8 g     F .fini 0000000000000010              _fini
0000000000600e48 g     O .dtors 0000000000000000              .hidden __DTOR_END__
0000000000000000       F *UND* 0000000000000000              __libc_start_main@@GLIBC_2.2.5
0000000000601020 g       .data 0000000000000000              __data_start
0000000000000000  w      *UND* 0000000000000000              __gmon_start__
0000000000601028 g     O .data 0000000000000000              .hidden __dso_handle
00000000004005f0 g     O .rodata 0000000000000004              _IO_stdin_used
0000000000601030 g     O .data 0000000000000004              global_y
0000000000400510 g     F .text 0000000000000089              __libc_csu_init
0000000000601050 g       .bss 0000000000000000              _end
0000000000400400 g     F .text 0000000000000000              _start
0000000000601034 g       .bss 0000000000000000              __bss_start
00000000004004ec g     F .text 0000000000000012              main
0000000000000000  w      *UND* 0000000000000000              _Jv_RegisterClasses
00000000004003c0 g     F .init 0000000000000000              _init

root@root2768:~/test_c> strip a.out
root@root2768:~/test_c> objdump -t a.out

a.out:     file format elf64-x86-64

SYMBOL TABLE:
no symbols

# 两种方式的区别,拷贝自stackoverflow

gcc being a compiler/linker, its -s option is something done while compiling and linking. It's also not configurable - it has a set of information which it removes, no more no less.

strip is something which can be run on an object file which is already compiled. It also has a variety of command-line options which you can use to configure which information will be removed. For example, -g strips only the debug information which gcc -g adds.

Note that strip is not a bash command, though you may be running it from a bash shell. It is a command totally separate from bash, part of the GNU binary utilities suite.

2) 如何查看目标文件的符号表 

#一个方式是使用 objdump

root@root2768:~/test_c> objdump -t test1.o

test1.o:     file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l    df *ABS* 0000000000000000 test1.c
0000000000000000 l    d  .text 0000000000000000 .text
0000000000000000 l    d  .data 0000000000000000 .data
0000000000000000 l    d  .bss 0000000000000000 .bss
0000000000000000 l     O .data 0000000000000004 global_s   // local, can not be referenced when linking
0000000000000000 l    d  .eh_frame 0000000000000000 .eh_frame
0000000000000000 l    d  .comment.SUSE.OPTs
0000000000000000 .comment.SUSE.OPTs
0000000000000000 l    d  .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l    d  .comment 0000000000000000 .comment
0000000000000004 g     O .data 0000000000000004 global_y    // global, can be referenced when linking
0000000000000000 g     F .text 000000000000001a main
0000000000000000         *UND* 0000000000000000 global_x    //undefined symbol, will search from other obj file when linking
0000000000000000         *UND* 0000000000000000 foo           //undefined symbol, will search from other obj file when linking

#另外一个方式是使用 nm

ejinfeg@esekilxxen2768:~/test_c> nm -S test1.o

U foo                                                            //undefined symbol, will search from other obj file when linking 
0000000000000000 0000000000000004 d global_s             //initialized local variable, can not be referenced by other obj file when linking
                 U global_x                                                     //undefined symbol, will search from other obj file when linking
0000000000000004 0000000000000004 D global_y            //initialized global variable, can be referenced by other obj file when linking
0000000000000000 000000000000001a T main

C代码 test1.c

extern int foo ( int x );
extern int global_x;

static global_s = 2;
int global_y = 1;

int main ()
{
   foo(global_x);
   int local_x;

return 0;
}

# 附上 nm 命令链接

http://man7.org/linux/man-pages/man1/nm.1.html

##博客仅作个人记录##

---------------------

本文来自 swedenfeng 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/swedenfeng/article/details/53417085?utm_source=copy

GCC 符号表小结【转】的更多相关文章

  1. GCC制作动态库导出符号表【转】

    转自:https://blog.csdn.net/whb_fei/article/details/76974543 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  2. C++编译器符号表有哪些内容?

    http://blog.csdn.net/wangbingcsu/article/details/48340479 C++编译器符号表有哪些内容? 很早就想写一篇关于符号表的学习小结,可是迟迟不能下笔 ...

  3. 使用objdump objcopy查看与修改符号表

    使用objdump objcopy查看与修改符号表动态库Linuxgccfunction    我们在 Linux 下运行一个程序,有时会无法启动,报缺少某某库.这时需要查看可执行程序或者动态库中的符 ...

  4. 程序减肥,strip,eu-strip 及其符号表

    程序减肥,strip,eu-strip 及其符号表 我们要给我们生成的可执行文件和DSO瘦身,因为这样可以节省更多的磁盘空间,所以我们移除了debug信息,移除了符号表信息,同时我们还希望万一出事了, ...

  5. ELF格式文件符号表全解析及readelf命令使用方法

    http://blog.csdn.net/edonlii/article/details/8779075 1. 读取ELF文件头: $ readelf -h signELF Header:  Magi ...

  6. 程序运行之ELF 符号表

    当一个工程中有多个文件的时候,链接的本质就是要把多个不同的目标文件相互粘到一起.就想玩具积木一样整合成一个整体.为了使不同的目标文件之间能够相互粘合,这些目标文件之间必须要有固定的规则才行.比如目标文 ...

  7. C 符号表导出

    编译符号表导出示例: 使用GCC编译链接参数--version-script 控制动态符号表,如想 使用链接参数 --retain-symbols-file 控制静态符号表,--version-scr ...

  8. GDB如何调试没有符号表(未加-g选项的编译)的程序

    /********************************************************************* * Author  : Samson * Date    ...

  9. C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)

    详解link  有 些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错 ...

随机推荐

  1. android 让真机显示 DeBug Log调试信息

    真机默认是不开启Log 开关的,这么来说我们如果使用真机来搞程序测试的话,需要做以下几个步骤: 下面以华为手机为例开启手机的log功能:  1.在拨号界面输入:*#*#2846579#*#*  进入测 ...

  2. 【BZOJ2001】[HNOI2010]城市建设(CDQ分治,线段树分治)

    [BZOJ2001][HNOI2010]城市建设(CDQ分治,线段树分治) 题面 BZOJ 洛谷 题解 好神仙啊这题.原来想做一直不会做(然而YCB神仙早就切了),今天来怒写一发. 很明显这个玩意换种 ...

  3. BZOJ4332 JSOI2012 分零食 【倍增 + NTT】

    题目链接 权限题BZOJ4332 题解 容易想到\(dp\) 设\(g[i][j]\)表示前\(i\)人分到\(j\)颗糖的所有方案的乘积之和 设\(f(x) = Ox^2 + Sx + U\) \[ ...

  4. wildfly jobss 同时连接多个数据源 datasource xa-datasource

    由于需要从一个远程机器取数据.处理后保存到本地数据库处理.用 wildfly datasource 会报: [com.arjuna.ats.arjuna] (default task-6) ARJUN ...

  5. Asp: Server.mapPath() 注意事项

    今天下午,在删除一个用户后,竟然发现该用户上传的图片还能正常显示. 郁闷了,怎么会这样? 为了再次验证这个结果,我新注册一个用户名,然后上传3张图片,接着删除这个用户,再查看刚刚上传的3张图片,竟然没 ...

  6. 网络流 KM dinic

    study from: https://blog.csdn.net/A_Comme_Amour/article/details/79356220 1. Edmonds-Karp 无优化 最坏时间复杂度 ...

  7. maven项目检出后报错(包括编译报错和运行报错)的常见检查处理方式

    maven项目检出后报错(包括编译报错和运行报错)的常见检查处理方式: 1.更改项目的jdk为我们安装的jdk2.更改build配置里的 output folder 目录为 xxx项目名/target ...

  8. 对entry-common.S和call.S的部分理解1

    内核版本: linux-2.6.30.4 文件: linux-2.6.30.4/arch/arm/kernel/entry-common.S linux-2.6.30.4/arch/arm/kerne ...

  9. java基础-Idea开发工具介绍

    java基础-Idea开发工具介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前给大家介绍过一款Java的IDE叫eclipse,有些功能用起来不是很得心应手,尤其是在导报的 ...

  10. linux配置无秘钥登陆

    linux配置无秘钥登陆 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近有点心烦,很少写博客了,后期的3个月里,估计更新的频率也不会太高,请见谅,今天给大家共享一下linux不 ...