linux strip作用,linux gcc strip命令简介

阅读目录

strip简介

strip经常用来去除目标文件中的一些符号表、调试符号表信息,以减小静态库、动态库和程序的大小。

strip支持的选项可通过如下命令查看:

strip --help

strip示例

有如下test.c文件

//test.c

#include

int add(int x, int y)

{

return x + y;

}

int aaa;

int bbb = 1;

char szTest[] = "good";

int main()

{

int ccc = 2;

return 0;

}

下面操作编译、并对比strip前后程序的差异

mayue:~/mayueadd/learn/gcc/strip$ gcc test.c

mayue:~/mayueadd/learn/gcc/strip$ ll a.out

-rwxrwxr-x 1 mayue mayue 8457 1月 16 11:06 a.out*

mayue:~/mayueadd/learn/gcc/strip$ file a.out

a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7413e8ec7c63fa4f78aca5d0c7584206d47709f6, not stripped

mayue:~/mayueadd/learn/gcc/strip$ nm a.out

0000000000600e50 d _DYNAMIC

0000000000600fe8 d _GLOBAL_OFFSET_TABLE_

00000000004005c8 R _IO_stdin_used

w _Jv_RegisterClasses

0000000000600e30 d __CTOR_END__

0000000000600e28 d __CTOR_LIST__

0000000000600e40 D __DTOR_END__

0000000000600e38 d __DTOR_LIST__

00000000004006c0 r __FRAME_END__

0000000000600e48 d __JCR_END__

0000000000600e48 d __JCR_LIST__

0000000000601024 A __bss_start

0000000000601008 D __data_start

0000000000400580 t __do_global_ctors_aux

0000000000400420 t __do_global_dtors_aux

0000000000601010 D __dso_handle

w __gmon_start__

0000000000600e24 d __init_array_end

0000000000600e24 d __init_array_start

0000000000400570 T __libc_csu_fini

00000000004004e0 T __libc_csu_init

U __libc_start_main@@GLIBC_2.2.5

0000000000601024 A _edata

0000000000601040 A _end

00000000004005b8 T _fini

0000000000400390 T _init

00000000004003d0 T _start

0000000000601038 B aaa

00000000004004b4 T add

0000000000601018 D bbb

00000000004003fc t call_gmon_start

0000000000601028 b completed.6531

0000000000601008 W data_start

0000000000601030 b dtor_idx.6533

0000000000400490 t frame_dummy

00000000004004c8 T main

000000000060101c D szTest

通过ls -l 命令可知, a.out的大小是8457个字节;

通过file命令可知, a.out是可执行文件, 且是not stripped, 也就是带符号表和调试信息的;

通过nm命令, 可以读出a.out中的符号信息;

现在将a.out程序经过strip处理后得到如下结果

mayue:~/mayueadd/learn/gcc/strip$

mayue:~/mayueadd/learn/gcc/strip$ strip a.out

mayue:~/mayueadd/learn/gcc/strip$ ls -l a.out

-rwxrwxr-x 1 mayue mayue 6208 1月 16 11:08 a.out

mayue:~/mayueadd/learn/gcc/strip$ file a.out

a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7413e8ec7c63fa4f78aca5d0c7584206d47709f6, stripped

mayue:~/mayueadd/learn/gcc/strip$ nm a.out

nm: a.out: no symbols

通过ls -l 命令可知, a.out的大小是6208个字节, 程序大小减小;

通过file命令可知, a.out是可执行文件, 且是stripped, 也就是strip处理过的;

通过nm命令, 发现a.out中的符号没有了;

strip命令用法

程序开发是否要strip

strip可以压缩目标文件、静态库、动态库、可执行程序的大小,但是会失去符号表、调试符号表信息。为了方便定位问题(比如定位 core dump问题), 建议, 尽量不要strip, 除非存储紧张。

在实际的开发中, 若需要对动态库.so进行strip操作, 减少占地空间。 通常的做法是: strip前的库用来调试, strip后的库用来实际发布, 他们两者有对应关系。 一旦发布的strip后的库出了问题, 就可以找对应的未strip的库来定位。

参考资料

518ad76ec554faf3df33a30ae42c355d.png

068be7e19a8fd7ea1cc73db7ddbb42c1.png

mayue_csdn

发布了60 篇原创文章 · 获赞 43 · 访问量 7万+

私信

关注

标签:__,gcc,strip,mayueadd,linux,mayue,out

来源: https://blog.csdn.net/mayue_web/article/details/104001392

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值