Counting the number of selectors in an objective-c binary

439 Views Asked by At

Is there a tool/script which can help me iterate over (or just count) all the selectors in a objective-c binary. I want to statically analyze objective c binaries and get that metric. I tried otool -tV but all it dumps is hex-data which I'm unable to parse. Some people suggest using lldb but I'm unsure how to do that.

2

There are 2 best solutions below

0
On BEST ANSWER

You can print all of the selectors like this:

$ objdump -section=__objc_selrefs -macho /Applications/Xcode.app/Contents/Frameworks/IBFoundation.framework/IBFoundation
/Applications/Xcode.app/Contents/Frameworks/IBFoundation.framework/IBFoundation:
Contents of (__DATA,__objc_selrefs) section
000000000016a808  __TEXT:__objc_methname:init
000000000016a810  __TEXT:__objc_methname:copy
000000000016a818  __TEXT:__objc_methname:array
000000000016a820  __TEXT:__objc_methname:dictionary
000000000016a828  __TEXT:__objc_methname:addObject:
000000000016a830  __TEXT:__objc_methname:setObject:forKey:
000000000016a838  __TEXT:__objc_methname:appendString:
000000000016a840  __TEXT:__objc_methname:allKeys
...

If you want to count them, grep out the two header lines and pipe the rest into wc:

$ objdump -section=__objc_selrefs -macho /Applications/Xcode.app/Contents/Frameworks/IBFoundation.framework/IBFoundation  | grep ^'[0-9a-f]' | wc -l
    1668
0
On

Another tool to explore is objdump, which is otool's successor. You may need to use xcrun objdump to run it, and it takes a -help argument. For example try:

xcrun objdump -macho -objc-meta-data <binary> | grep "name 0x"

which may get you close to what you are after.