列出未使用的符号 [英] Listing Unused Symbols

查看:14
本文介绍了列出未使用的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个大型项目中删除死代码,并希望从未使用的符号开始.有没有办法让链接器列出它已优化的未使用符号?我将 GNU 链接器 (LD) 与 GCC 一起使用.

I want to remove dead code from a largish project and would like to start with unused symbols. Is there anyway to get the linker to list unused symbols that it has optimized out? I am using the GNU linker (LD) together with GCC.

如果做不到这一点,任何 Binutils(readelf 或 objdump)都可以执行相同的功能吗?

Failing that, can any of the Binutils (readelf or objdump) perform the same function?

推荐答案

大多数编译器/链接器会优化未使用的符号.如果您在 *nix 系统上运行,您可以尝试对所有目标文件使用命令nm",对其进行过滤和排序,以生成由这些目标文件定义的所有导出函数的列表.

Most compilers/linkers optimise out unused symbols. If you're running on a *nix system, you can try using the command "nm" on all the object files, filter it and sort it, to produce a list of all exported functions defined by those object files.

nm *.o | grep "^[0-9a-f]* T " | sed 's/^[0-9a-f]* T //' | sort -u > symbols_in.txt

我相信您可以对最终的二进制文件执行相同的操作.

I believe you can do the same on the final binaries.

然后,如果您对两组结果进行比较,您应该会得到所有未使用的导出函数的列表.

If you then diff the two sets of results you should get a list of all unused exported functions.

请注意,由于条件编译而被排除的代码可能会使用某些函数.例如.#ifdef 开关表示在平台 A 上使用某某内置功能,而在另一个平台上使用您自己的函数版本,因为没有内置或标准库等效,或者它无法正常工作.

Beware though that some functions may be used by code that is excluded as a result of conditional compilation. E.g. a #ifdef switch to say that on platform A, use such and such built in functionality and on another platform use your own version of the function because there is no built in or standard library equivalent, or it doesn't work properly.

这篇关于列出未使用的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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