可以指示GNU ld打印链接期间需要哪些.o文件吗? [英] Can GNU ld be instructed to print which .o files are needed during a link?

查看:148
本文介绍了可以指示GNU ld打印链接期间需要哪些.o文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景知识:我正在尝试为嵌入式传感器系统构建AVR二进制文件,并且我的运行接近极限.我使用一些外部库来帮助我,但是当每个库编译成一个对象时,它们很大.我想将它们放入较小的对象中,以便仅将我需要的功能链接到程序中.我已经设法通过拆分一个大的库将二进制文件大小减少了2k.

A little background: I'm trying to build an AVR binary for an embedded sensor system, and I'm running close to my size limit. I use a few external libraries to help me, but they are rather large when compiled into one object per library. I want to pull these into smaller objects so only the functionality I need is linked into my program. I've already managed to drop the binary size by 2k by splitting up a large library.

知道在游戏的每个阶段都在使用哪些对象会很有帮助,因此我可以更有效地拆分它们.有没有办法让ld打印它链接的对象?

It would help a lot to know which objects are being used at each stage of the game so I can split them more efficiently. Is there a way to make ld print which objects it's linking?

推荐答案

我不确定 object 级别,但是我相信您也许可以在符号上解决此问题级别使用CFLAGS="-fdata-sections -ffunction-sections"LDFLAGS="-Wl,--gc-sections -Wl,--print-gc-sections".这应该摆脱所有未引用符号的代码,并向您显示已删除的符号,如果出于某种原因您决定返回到目标文件级别并希望标识仅包含已删除符号的目标文件,这可能会很有用.

I'm not sure about the object level, but I believe you might be able to tackle this on the symbol level using CFLAGS="-fdata-sections -ffunction-sections" and LDFLAGS="-Wl,--gc-sections -Wl,--print-gc-sections". This should get rid of the code for all unreferenced symbols, and display the removed symbols to you as well which might be useful if for some reason you decide to go back to the object file level and want to identify object files only containing removed symbols.

更准确地说,我引用的编译器标志将要求编译器将每个函数或全局变量本身放置在一个节中,然后--gc-sections链接器标志将删除所有未使用的节.即使每个目标文件中的所有功能共享一个节,也可能每个目标文件都包含其自己的节.在这种情况下,仅链接标志应满足您的要求:消除不使用的整个对象. gcc手册指出,编译器标志会增加对象的大小,尽管我希望最终可执行文件不受此影响,但我不确定,因此您应单独尝试LDFLAGS="-Wl,--gc-sections任何情况下.

To be more precise, the compiler flags I quoted will ask the compiler to place each function or global variable in a section for itself, and the --gc-sections linker flag will then remove all the sections which have not been used. It might be that each object file contains its own sections, even if the functions therein all share a single section. In that case the linker flag alone should do what you ask for: eliminate whole objects which are not used. The gcc manual states that the compiler flags will increase the object size, and although I hope that the final executable should not be affected by this, I don't know for sure, so you should give the LDFLAGS="-Wl,--gc-sections by itself a try in any case.

列出的选项名称可能是有用的关键字,可在stackoverflow上搜索有关如何减小二进制文件大小的其他建议. gc-sections例如目前产生62场比赛.

The listed option names might be useful keywords to search on stackoverflow for other suggestions on how to reduce the size of the binary. gc-sections e.g. yields 62 matches at the moment.

这篇关于可以指示GNU ld打印链接期间需要哪些.o文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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