链接器选项忽略未使用的依赖关系 [英] linker option to ignore unused dependencies

查看:129
本文介绍了链接器选项忽略未使用的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我编译的C ++二进制文件中删除所有未使用的符号。我看到了这个,它给出了使用gcc的概述,这是我正在使用的工具链:如何用GCC和ld去除未使用的C / C ++符号?然而,在我的系统中,链接选项( -Wl , - gc-sections )被拒绝:

  $ gcc -fdata-sections -ffunction-部分ac -o ao -Wl, -  gc-sections 
ld:fatal:无法识别的选项' - '
ld:fatal:使用-z帮助选项获取使用信息
collect2:错误:ld返回1退出状态

我在Illumos上运行,这是一个Solaris的叉,与GCC 4.7。任何人都知道在这里使用正确的链接器选项是什么?






编辑:搜索更接近出现的手册页 zignore:

  -z忽略|记录

忽略或记录非
引用的动态依赖项作为链接编辑的一部分。忽略作为链接编辑一部分的可重定位
对象中未引用或
记录的未引用ELF节。按
的默认值,-z记录有效。

如果ELF部分被忽略,则从正在生成的输出文件中删除
部分。当三个条件为真时,部分被忽略
。被清除的
部分必须为可分配部分做出贡献。
消除部分不能提供全局符号。任何对
链接编辑有贡献的对象都没有
的其他部分,必须引用一个被删除的部分。

然而,以下序列仍然会将 FUNCTION_SHOULD_BE_REMOVED 放入ELF部分 .text.FUNCTION

  $ cat ac 
int main(){
return 0;
}
$ cat b.c
int FUNCTION_SHOULD_BE_REMOVED(){
return 0;
}
$ gcc -fdata-sections -ffunction-sections -c ac -Wl,-zignore
$ gcc -fdata-sections -ffunction-sections -c bc -Wl,-zignore
$ gcc -fdata-sections -ffunction-sections ao bo -Wl,-zignore
$ elfdump -s a.out#为了简洁,我删除了很多输出
符号表部分:.dynsym
[2] 0x08050e72 0x0000000a FUNC GLOB D 1.text.FUNCTION FUNCTION_SHOULD_BE_REMOVED
符号表部分:.symtab
[71] 0x08050e72 0x0000000a FUNC GLOB D 0 .text.FUNCTION FUNCTION_SHOULD_BE_REMOVED

因为手册页中显示没有全局符号,所以我试着让函数static结果。

解决方案

ld'-z ignore'选项是位置的,它适用于那些在命令行。你给的例子:

  gcc ao bo -Wl,-zignore 

将选项应用于无对象 - 因此没有任何操作。

  gcc -Wl,-zignore ao bo 

可以工作


I would like to remove all unused symbols from my compiled C++ binary. I saw this, which gives an overview using gcc, which is the toolchain I'm using: How to remove unused C/C++ symbols with GCC and ld?

However, on my system, the linking option (-Wl,--gc-sections) is rejected:

$ gcc -fdata-sections -ffunction-sections a.c -o a.o -Wl,--gc-sections
ld: fatal: unrecognized option '--'
ld: fatal: use the -z help option for usage information
collect2: error: ld returned 1 exit status

I'm running on illumos, which is a (relatively) recent fork of Solaris, with GCC 4.7. Anybody know what the correct linker option to use here is?


Edit: searching the man pages more closely turned up "-zignore":

 -z ignore | record

     Ignores, or records, dynamic dependencies that  are  not
     referenced   as  part  of  the  link-edit.  Ignores,  or
     records, unreferenced ELF sections from the  relocatable
     objects  that  are  read  as  part  of the link-edit. By
     default, -z record is in effect.

     If an ELF section is ignored, the section is  eliminated
     from  the  output  file  being  generated.  A section is
     ignored when three conditions are true.  The  eliminated
     section  must  contribute to an allocatable segment. The
     eliminated section must provide no  global  symbols.  No
     other  section  from  any object that contributes to the
     link-edit, must reference an eliminated section.

However the following sequence still puts FUNCTION_SHOULD_BE_REMOVED in the ELF section .text.FUNCTION:

$ cat a.c
int main() {
    return 0;
}
$ cat b.c
int FUNCTION_SHOULD_BE_REMOVED() {
    return 0;
}
$ gcc -fdata-sections -ffunction-sections -c a.c -Wl,-zignore
$ gcc -fdata-sections -ffunction-sections -c b.c -Wl,-zignore
$ gcc -fdata-sections -ffunction-sections a.o b.o -Wl,-zignore
$ elfdump -s a.out                     # I removed a lot of output for brevity
Symbol Table Section:  .dynsym
[2]  0x08050e72 0x0000000a  FUNC GLOB  D    1 .text.FUNCTION FUNCTION_SHOULD_BE_REMOVED
Symbol Table Section:  .symtab
[71]  0x08050e72 0x0000000a  FUNC GLOB  D    0 .text.FUNCTION FUNCTION_SHOULD_BE_REMOVED

Because the man pages say "no global symbols", I tried making the function "static" and that had the same end result.

解决方案

The ld '-z ignore' option is positional, it applies to those input objects which occur after it on the command line. The example you gave:

gcc a.o b.o -Wl,-zignore

Applies the option to no objects -- so nothing is done.

gcc -Wl,-zignore a.o b.o

Should work

这篇关于链接器选项忽略未使用的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆