linux调试符号表,strip去除目标文件中符号表、调试符号表信息

strip经常用来去除目标文件中的一些符号表、调试符号表信息,以减小程序的大小,在rpmbuild包的最后就用到。

其支持的选项如下:

>strip -h

用法:strip 输入文件

从文件中删除符号和节

选项为:

-I --input-target=      Assume input file is in format

-O --output-target=     Create an output file in format

-F --target=            Set both input and output format to

-p --preserve-dates              Copy modified/access timestamps to the output

-R --remove-section=       Remove section from the output

-s --strip-all                   Remove all symbol and relocation information

-g -S -d --strip-debug           Remove all debugging symbols & sections

--strip-unneeded              Remove all symbols not needed by relocations

--only-keep-debug             Strip everything but the debug information

-N --strip-symbol=         Do not copy symbol

-K --keep-symbol=          Do not strip symbol

--keep-file-symbols           Do not strip file symbol(s)

-w --wildcard                    Permit wildcard in symbol comparison

-x --discard-all                 Remove all non-global symbols

-X --discard-locals              Remove any compiler-generated symbols

-v --verbose                     List all object files modified

-V --version                     Display this program's version number

-h --help                        Display this output

--info                        List object formats & architectures supported

-o                         Place stripped output into

strip:支持的目标: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-little elf32-big elf64-alpha ecoff-littlealpha elf64-little elf64-big elf32-littlearm elf32-bigarm elf32-hppa-linux elf32-hppa elf64-ia64-little elf64-ia64-big efi-app-ia64 elf32-m68k a.out-m68k-linux elf32-powerpc aixcoff-rs6000 elf32-powerpcle ppcboot elf64-powerpc elf64-powerpcle aixcoff64-rs6000 elf32-s390 elf64-s390 elf32-sparc a.out-sparc-linux elf64-sparc a.out-sunos-big elf64-x86-64 pe-i386 pei-i386 srec symbolsrec tekhex binary ihex trad-core

目标文件分为:可重定位文件、可执行文件、共享文件

strip的默认选项会去除.symbol节的内容以及.debug节的内容,因此尽量只对可执行文件执行strip而不要对静态库或动态库等目标文件strip。

测试代码如下:

/* max.c */int max(int val1, int val2){  int iVal = (val1 > val2) ? val1 : val2;  return iVal;}

/* min.c */int min(int val1, int val2){  int iVal = (val1 < val2) ? val1 : val2;  return iVal;}

/* main.c */#include extern int max(int val1, int val2);extern int min(int val1, int val2);int main(){   int val1, val2;   scanf("%d  %d", &val1, &val2);   printf("%d\n", max(val1, val2));   printf("%d\n", min(val1, val2));}

>gcc -c max.c min.c

>ar rcs libcmp.a max.o min.o

>gcc -o test main.c libcmp.a

>gcc -share -fPIC -o libcmp.so max.c min.c

>cp libcmp.a libcmp.a.bak

>cp libcmp.so libcmp.so.bak

>cp test test.orig

>strip libcmp.a libcmp.so

>strip test

>ll -h

总计 92K

-rwxr-xr-x 1 6.9K a.out

-rw-r--r-- 1 1.1K libcmp.a

-rw-r--r-- 1 1.6K libcmp.a.bak

-rwxr-xr-x 1 2.9K libcmp.so

-rwxr-xr-x 1 5.3K libcmp.so.bak

-rw-r--r-- 1  237 main.c

-rw-r--r-- 1   89 max.c

-rw-r--r-- 1  695 max.o

-rw-r--r-- 1   89 min.c

-rw-r--r-- 1  695 min.o

-rwxr-xr-x 1 3.2K test

-rwxr-xr-x 1 6.8K test.orig

选项简释:

The -fPIC flag directs the compiler to generate position independent code section).

The -shared flag directs the linker to create a shared object file.

可见无论是静态库(libcmp.a)还是动态库(libcmp.so)还是可执行文件(test),去掉一些符号信息后都减小了很多,

但如果这时再链接这两个库的话是编不过的,因此,如果不是指定特殊的strip选项的话,还是尽量不要对库文件strip,

只对链接后的可执行文件strip就可以了(如果也不调试)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值