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:
- Adding a local literal char[] in a function that should be eliminated; the string is still visible when running
stringson the executable. - 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.
(EDIT)
In binutils,
ldhas the--gc-sectionsoption which does what you want for sections on object level. You have several options:use
gcc‘s flags-ffunction-sectionsand-fdata-sectionsto 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
stripwith appropriate-Noptions to discard the offending symbols from thesymbol table – this will leave the code and data there, but it won’t show up in the symbol table.