简体   繁体   English

如何验证已从二进制文件中删除了无效代码?

[英]how can I verify that dead code was stripped from the binary?

My c/obj-c code (an iOS app built with clang) has some functions excluded by #ifdefs . 我的c / obj-c代码(使用clang构建的iOS应用)具有#ifdefs排除的某些功能。 I want to make sure that code that gets called from those functions, but not from others (dead code) gets stripped out (eliminated) at link time. 我想确保从那些函数而不是从其他函数调用的代码(死代码)在链接时被剥离(消除)。 I tried: 我试过了:

  1. Adding a local literal char[] in a function that should be eliminated; 在应该消除的函数中添加局部文字char []; the string is still visible when running strings on the executable. 在可执行文件上运行strings时,该字符串仍然可见。
  2. Adding a function that should be eliminated; 增加了应消除的功能; the function name is still visible when running strings. 运行字符串时,函数名称仍然可见。

Before you ask, I'm building for release, and all strip settings (including dead-code stripping, obviously) are enabled. 在您问之前,我正在为发布做准备,并且所有剥离设置(显然包括死代码剥离)都已启用。

The question is not really xcode/apple/iOS specific; 这个问题并不是真的针对xcode / apple / iOS。 I assume the answer should be pretty much the same on any POSIX development platform. 我认为答案在任何POSIX开发平台上都应该几乎相同。

(EDIT) (编辑)

In binutils, ld has the --gc-sections option which does what you want for sections on object level. 在binutils中, ld具有--gc-sections选项,该选项可以执行对象级别的节所需的操作。 You have several options: 您有几种选择:

  • use gcc 's flags -ffunction-sections and -fdata-sections to isolate each symbol into its own section, then use --gc-sections ; 使用gcc的标志-ffunction-sections-fdata-sections将每个符号分隔成自己的部分,然后使用--gc-sections

  • put all candidates for removal into a separate file and the linker will be able to strip the whole section; 所有要删除的候选文件放到一个单独的文件中,链接器将可以删除整个节;

  • disassemble the resulting binary, remove dead code, assemble again; 分解生成的二进制文件,删除无效代码,然后再次组装;

  • use strip with appropriate -N options to discard the offending symbols from the symbol table - this will leave the code and data there, but it won't show up in the symbol table. 使用带有适当-N选项的strip从符号表中丢弃有问题的符号-这会将代码和数据保留在此处,但不会显示在符号表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM