gcc链接器获取未使用对象的列表 [英] gcc linker get list of unused objects

查看:123
本文介绍了gcc链接器获取未使用对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在具有多个库的大型C应用程序中识别未使用的对象文件。随着时间的推移,这个项目不断增长,现在我想搜索不再使用的库,这样我就可以将它们从依赖文件中移除。例如,如果我使用gcc编译应用程序,并且让我们说没有任何符号/函数,那么可以使用gcc链接器来识别任何未使用的对象吗?

库2的使用。有没有办法获得有关哪些对象没有链接的信息?

  gcc library1.o library2.o main.o -o main.elf 

我知道gcc有编译器和链接器标志去除未使用的符号: / p>

  -fdata-sections -ffunction-sections -Wl, -  gc-sections 

然而这种方式我不知道gcc删除了哪些对象。如果gcc有一个选项可以获得未链接到应用程序的对象列表,那将是完美的。



只要提到:我需要它在目标文件基础上在功能/符号的基础!



有人知道这样的gcc选项吗?例如,如果我使用gcc编译应用程序,并且让我们说没有任何符号/文件夹,使用库2的函数。有没有办法获得有关哪些对象没有链接的信息?



gcc library1.o library2.o main.o -o main 。


使用以上命令, library2.o 链接在一起。要理解原因,请阅读 this



它是如果 -Wl,-gc-sections 链接 ,然后从 library2.o获取所有代码和数据



  gcc main.o -o main.elf -lrary1 -lrary2 
library2
中的任何代码,链接器将会不要将它拉入链接。



没有办法要求链接器列出 没有使用的东西,但是(if您正在使用 GNU-ld)有一种方法可以向它询问它 使用的对象列表: -M -Map 选项。一旦您知道使用了哪些对象,只需从链接中使用的所有对象中减去使用的对象以获取未使用的列表即可。



更新:

Gold链接器支持 - print-symbol-counts FILENAME 选项,它也可以这里有帮助。它打印已定义的和使用的符号计数。对于 library2.a ,它应该打印 $ num_defined 0 0 ,表示实际没有使用 library2.a 中的对象。


I want to identify unused object files in a large C application with many libraries. The project has grown a lot over time and now I want to search for libraries that are not used anymore, so that I can remove them from the dependency file. Is it possible with the gcc linker to identify any object that is not used?

For example, if I compile an application with gcc and let's say none of the symbols/functions of library2 are used. Is there any way to get the info about which objects are not linked in?

gcc library1.o library2.o main.o -o main.elf

I know that gcc has the compiler and linker flags to remove unused symbols:

-fdata-sections -ffunction-sections -Wl,--gc-sections

However this way I don't know which of the objects were removed by gcc. It would be perfect if gcc has an option to get a list of objects which were not linked into the application.

Just to mention: I need it on object file basis not on function/symbol basis!

Does anyone know such an option for gcc?

解决方案

For example, if I compile an application with gcc and let's say none of the symbols/functions of library2 are used. Is there any way to get the info about which objects are not linked in?

gcc library1.o library2.o main.o -o main.elf

With above command, library2.o will be linked in even if none of the code from it is ever used. To understand why, read this or this.

It is true that if you compile code in library2.o with -ffunction-sections -fdata-sections and link with -Wl,-gc-sections, then all of the code and data from library2.o will be GC'd out, but that is not the command you gave.

Presumably, you are more interested in what happens if you use libraries as libraries:

gcc main.o -o main.elf -lrary1 -lrary2

In that case, if none of the code from library2 is referenced, the linker will not pull it into the link.

There is no way to ask the linker for list of things it didn't use, but (if you are using GNU-ld) there is a way to ask it for a list of objects it did use: the -M or -Map option. Once you know what objects are used, it's a simple matter of subtracting used objects from all objects used while linking to get the list that is not used.

Update:

Gold linker supports --print-symbol-counts FILENAME option, which can also be helpful here. It prints defined and used symbol counts. For library2.a, it should print $num_defined 0, the 0 indicating that none of the objects from library2.a were actually used.

这篇关于gcc链接器获取未使用对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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