Search code examples
objective-cselectorlldbobjective-c++otool

Counting the number of selectors in an objective-c binary


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.


Solution

  • 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