Linux 如何彻底剥离可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4179010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:57:29  来源:igfitidea点击:

How to strip executables thoroughly

linuxgccexecutableelfstrip

提问by peoro

I'd like to strip as much as I can - on Linux: an ELF. I only want in there the stuff I need to run it.

我想尽可能多地剥离 - 在 Linux 上:一个 ELF。我只想要运行它所需的东西。

I tried using strip:

我尝试使用strip

strip --strip-all elf

But it doesn't seem to do a good job: nmstill displays lots of stuff, and the binary is still big.

但它似乎做得并不好:nm仍然显示了很多东西,而且二进制文件仍然很大。

What should I do?

我该怎么办?

采纳答案by Dirk Eddelbuettel

If everything else fails, you could read the documentation, starting with man strip.

如果其他一切都失败了,您可以阅读文档,从man strip.

Seriously, maybe your application has a lot of symbols and code. At one extreme, the biggest size reduction would be rm elfbut then your program won't run anymore. It all depends on your programand what you coded into it.

说真的,也许您的应用程序有很多符号和代码。在一种极端情况下,最大的尺寸减少将是rm elf但您的程序将不再运行。这一切都取决于您的程序以及您在其中编写的代码。

As a concrete example, I recently worked with a large C++ library where stripwithout further arguments reduced the size from 400+mb to about 28 mb. But then you could not link anymore against it (in the context of other shared libraries), rendering it somewhat useless.

作为一个具体的例子,我最近使用了一个大型 C++ 库,其中strip没有进一步的参数将大小从 400+mb 减少到大约 28 mb。但是随后您无法再链接到它(在其他共享库的上下文中),使其变得有些无用。

But when using strip --strip-unneeded, it changed the size from 400+ mb to 55 mb which is still considerable, yet allowed the library to be accessed from other shared libraries.

但是在使用时strip --strip-unneeded,它将大小从 400+ mb 更改为 55 mb,这仍然相当可观,但允许从其他共享库访问该库。

In short, I'd trust strip. Maybe your application cannot be reduced any further without code changes.

简而言之,我相信strip. 如果不更改代码,您的应用程序可能无法进一步缩减。

回答by yorick

Using the -R option to strip, you can strip away all sections you don't need. Also look at thisregarding minimal ELF executables.

使用 -R 选项剥离,您可以剥离所有不需要的部分。另外,也要看看这个关于最小的ELF可执行文件。

回答by krzysz00

strip -R .comment
gcc -Os elf.c

Those two might help.

这两个可能会有所帮助。

回答by ossys

I would check out this great article that goes into depth on making an ELF executable as small as possible. Maybe it has info that could help!

我会查看这篇很棒的文章,它深入探讨了如何使 ELF 可执行文件尽可能小。也许它有可以帮助的信息!

http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html

http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html