Swift reverse engineering:swift function name rule?

前端 未结 2 1435
后悔当初
后悔当初 2021-02-02 02:49

I have a question about swift function name rule. As I tried in IDA Pro to analyze a iOS app (Maybe OS X is the same case) written in swift, such as swift-2048, I got function n

相关标签:
2条回答
  • 2021-02-02 03:21

    Swift is using Name Mangling for the naming of methods,classes..... I came across this article which describes about swift name mangling. Section about mangling is shown below.


    Name Mangling

    Swift keeps metadata about functions (and more) in their respective symbols, which is called name mangling. This metadata includes the function’s name (obviously), attributes, module name, argument types, return type, and more. Take this for example:

    class Shape{
        func numberOfSides() -> Int {
            return 5
        }
    }
    

    The mangled name for the simpleDescription method is _TFC9swifttest5Shape17simpleDescriptionfS0_FT_Si.

    Here’s the breakdown:

    • _T – The prefix for all Swift symbols. Everything will start with this.

    • F – Function.

    • C – Function of a class. (method)

    • 9swifttest – The module name, with a prefixed length.

    • 5Shape – The class name the function belongs to, again, with a prefixed length.

    • 17simpleDescription – The function name.

    • f – The function attribute. In this case it’s ‘f’, which is just a normal function. We’ll get to that in a minute.

    • S0_FT – I’m not exactly sure what this means, but it appears to mark the start of the arguments and return type.

    • ‘_’ – This underscore separates the argument types from the return type. Since the function takes no arguments, it comes directly after S0_FT.

    • S – This is the beginning of the return type. The ‘S’ stands for Swift; the return type is a Swift builtin type. The next character determines the type.

    • i – This is the Swift builtin type. A lowercase ‘I’, which stands for Int.


    Excerpt from: Inside Swift

    looks like actual link is broken, find mirror here

    0 讨论(0)
  • 2021-02-02 03:23

    Using the swift-demangle command line tool you can see the difference between the two functions.

    _TToFC10swift_204811AppDelegate27applicationWillResignActivefS0_FCSo13UIApplicationT_ ---> @objc swift_2048.AppDelegate.applicationWillResignActive (swift_2048.AppDelegate)(ObjectiveC.UIApplication) -> ()
    
    _TFC10swift_204811AppDelegate27applicationWillResignActivefS0_FCSo13UIApplicationT_ ---> swift_2048.AppDelegate.applicationWillResignActive (swift_2048.AppDelegate)(ObjectiveC.UIApplication) -> ()
    

    _T prefixes all swift functions and it looks like To corresponds to the function having the @objc attribute.

    Unfortunately, I don't have enough knowledge of the internals of swift and the objective-c runtime to tell you what each of these functions does. I think it's safe to assume it's part of the objective-c to swift bridging process though.

    0 讨论(0)
提交回复
热议问题