没有 ELF 标头的目标文件或使用 GCC 减少目标文件大小的方法?
我正在使用 GCC 编译一些 C 代码。有没有办法从目标文件中剥离例如 ELF 标头并让链接器添加标头?或者,除了明显的 -Os 和 -s 标志之外,还有其他可能性来减少生成的目标文件大小吗?(-ffast-math、-fomit-frame-pointer、-fshort-doubles 确实有助于减少代码大小,但十六进制转储目标文件显示了大量“看似”无用的零)。
像 strip/sstrip 这样的工具实际上并没有多大用处,因为目标文件必须保留符号(稍后将链接)。 (-strip-unneeded 和 -R .comment -R .gnu.version 发挥了它们的魔力)。
我正在做的事情需要我将目标文件捆绑(压缩)给用户,并嵌入一个脚本以在用户端链接它。每个字节都很重要!
I'm using GCC to compile some C code. Is there a way to strip e.g. the ELF header from object file and make linker to add the header? Or, are there other possibilities to strip down the resulting object file size than the obvious -Os and -s flags?(-ffast-math, -fomit-frame-pointer, -fshort-doubles do help to reduce the code size but hexdumping the object file reveals huge amounts of zeroes which are "seemingly" useless).
Tools like strip/sstrip aren't really of much use as the object file has to preserve the symbols(it will be linked later on). (-strip-unneeded and -R .comment -R .gnu.version do their magic though).
What I'm doing is something which requires me to bundle (compressed) object file to the user and have a script embedded to link it at users-end. Every byte counts!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ELF 标头以后无法删除和恢复,因为有价值的信息存储在那里,然后永远丢失(例如某些表、体系结构等的文件偏移量。IIRC)。您已经列出了几乎所有可以减少大小的方法,除了 bzip 之外。
The ELF header can't be removed and restored later on, as valuable information is stored there and then lost forever (like file offsets for some tables, architecture, etc. IIRC). You've already listed almost everything that you can do to reduce the size, except maybe bzip'ing.
如果通过压缩算法运行目标文件,那些“大量的零”应该缩小很多倍,因为它们的信息内容很少。您可能想研究一种更好的压缩算法,如果可能的话,也许比采用修改的/非标准的“黑客”对象文件格式可以获得更多的收益。
If you run the object files through a compression algorith, those "huge amounts of zeroes" should shrink by a large factor, since they have low information content. You might want to investigate a better compression algorithnm, perhaps it's possible to gain more there than by going to a modified/non-standard "hacked" object file format, if even possible.
您可以尝试使用
-fdata-sections
、-ffunction-sections
和-Wl,--gc-sections
,但这并不安全,所以在使用它们之前一定要了解它们的工作原理。You can try playing with
-fdata-sections
,-ffunction-sections
and-Wl,--gc-sections
, but this is not safe, so be sure to understand how they work before using them.