简而言之,二进制剥离

发布于 2025-01-05 02:35:03 字数 51 浏览 1 评论 0原文

有人可以向我解释二进制剥离吗?它有多有效(它可以减少你的二进制文件多少)?你如何应用它?

Can someone explain binary stripping to me? How effective is it (How much does it reduce your binary)? How do you apply it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳 2025-01-12 02:35:03

实际上有两种剥离。

剥离二进制文件会从二进制文件的符号表中删除不需要的符号。符号表包含程序中每个对象的名称以及调试信息。这对于程序的运行来说不是必需的,因为程序通常不通过符号表访问它们自己的函数。

当程序崩溃时,符号表提供有用的调试信息,因此您可以获得回溯,显示程序崩溃时正在调用哪些函数。如果剥离符号,回溯将只包含内存地址,但不包含函数名称。您永远不应该剥离正在调试的应用程序。

剥离二进制文件也会使对可执行文件进行逆向工程变得稍微困难​​,但我希望您没有那么偏执。

剥离二进制文件并不会使程序加载速度更快。除非程序崩溃时需要进行回溯,否则符号表不会加载到内存中。

您可以使用 strip 命令从命令行中剥离二进制文件。我不太确定如何从 Xcode 触发此操作。

strip MyExecutable

死剥离是一个不同的过程,它从代码中删除未使用的函数和数据。这发生在创建二进制文件时的链接阶段。这可能会减少代码的大小。差异取决于程序中有多少未使用的数据;可能很多,也可能什么都没有。死剥离可以使您的应用程序稍微快一些,因为它增加了热代码的局部性。 (如果它产生很大的差异,则说明您的应用程序存在严重错误。)

死剥离没有一般缺点,因此我总是将其打开。您可以通过Xcode启用dead stripping,它对应于链接器标志-dead_strip

gcc -o MyExecutable -Wl,-dead_strip ....

注意: 死剥离的工作原理是将每个函数放在单独的小节中。如果您编写汇编文件,则可以使用 .subsections_via_symbols 指令将每个符号放入单独的小节中,从而允许链接器完全剥离各个符号。据我所知,GCC 总是在其汇编输出中生成该指令。

参考文献:

https ://developer.apple.com/library/archive/documentation/General/Conceptual/MOSXAppProgrammingGuide/Performance/Performance.html

There are actually two kinds of stripping.

Stripping a binary removes the symbols which are not needed from your binary's symbol table. The symbol table contains the name of every object in your program as well as debugging information. This is not necessary for your program to run, since programs do not ordinarily access their own functions through the symbol table.

The symbol table provides useful debugging information when your program crashes, so you can get a backtrace showing which functions were being called when the program crashed. If you strip the symbols, the backtrace will only contain memory addresses but no function names. You should never strip an application that you are debugging.

Stripping a binary also makes it slightly harder to reverse engineer an executable, but I am hoping you are not that paranoid.

Stripping a binary does not make your program load faster. The symbol table will not be loaded into memory unless it is needed for a backtrace if your program crashes.

You can strip a binary from the command line using the strip command. I am not quite sure how to trigger this from Xcode.

strip MyExecutable

Dead stripping is a different process which removes unused functions and data from your code. This happens at the link stage, when the binary is created. This may reduce the size of your code. The difference will depend on how much unused data is in your program; it may be a lot and it may be nothing at all. Dead stripping can make your application very slightly faster since it increases the locality of the hot code. (If it makes a big difference, there is something seriously wrong with your application.)

There are no general drawbacks to dead stripping, so I would always turn it on. You can enable dead stripping through Xcode, it corresponds to the linker flag -dead_strip.

gcc -o MyExecutable -Wl,-dead_strip ....

Note: Dead stripping works by putting each function in a separate subsection. If you write assembly files, you can use the .subsections_via_symbols directive to put each symbol in a separate subsection, allowing individual symbols to be dead stripped by the linker. GCC always produces that directive in its assembly output, as far as I can tell.

References:

https://developer.apple.com/library/archive/documentation/General/Conceptual/MOSXAppProgrammingGuide/Performance/Performance.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文