noamtm
noamtm

Reputation: 12983

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. 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; the string is still visible when running strings on the executable.
  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; I assume the answer should be pretty much the same on any POSIX development platform.

Upvotes: 5

Views: 2377

Answers (1)

peterph
peterph

Reputation: 980

(EDIT)

In binutils, ld has the --gc-sections option which does what you want for sections on object level. 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;

  • 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.

Upvotes: 2

Related Questions