About Swift naming in final mach-O file

Questions and Answers about all things *OS (macOS, iOS, tvOS, watchOS)

About Swift naming in final mach-O file

Postby Wingzero » Mon Sep 18, 2017 2:33 am

Hi there,

Wanted to ask a question:

We all know APPL is pushing Swift so hard. I checked the mach-O file which is written in swift. The class name in mach-O file will have some pattern than the swift class name, e.g.:

Code: Select all
_TtC21DistributedDemo11AppDelegate
in macho-O file compare to its swift class name:
Code: Select all
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
.
It has a prefix _TtC21 before the project name, and an "order id" 11 after it. So another example:
Code: Select all
_TtC21DistributedDemo18HomeViewController
to its swift class:
Code: Select all
class HomeViewController: UIViewController


So in terms of static analysis of the mach-O file, the question is, is there any guides or rules or patterns we can match the swift class name correctly to the mach-O class name?
Wingzero
 
Posts: 34
Joined: Thu Jul 27, 2017 2:35 am

Re: About Swift naming in final mach-O file

Postby scknight » Mon Sep 18, 2017 7:26 pm

https://github.com/apple/swift/blob/mas ... nifesto.md

This first link has a section on mangling that links to other Apple Swift docs. Mike Ash has a good write up about Swift name mangling as well.

https://mikeash.com/pyblog/friday-qa-20 ... gling.html
scknight
 
Posts: 27
Joined: Thu Nov 10, 2016 1:01 pm

Re: About Swift naming in final mach-O file

Postby morpheus » Tue Sep 19, 2017 1:38 am

You can use Xcode's "swift-demangle" utility. - easier. Works like cppfilt , and is compatible with jtool -d .... | swift-demangle. Just find it in your path.

Volume II has a section on how the mangling works :-) So wait a couple more weeks for the full guide or rule or pattern :-)
morpheus
Site Admin
 
Posts: 532
Joined: Thu Apr 11, 2013 6:24 pm

Re: About Swift naming in final mach-O file

Postby Wingzero » Tue Sep 19, 2017 3:41 am

Thanks @scknight and @morpheus!

BTW if I read this correctly, we have to wait another long time for Vol II

Does Jtool supports Swift-4 as well? Swift 4 took 4 versions and still not ABI stablized... I just saw IDA Pro publishes v7, but the swift decompiling seems still confusing to me. BTW, is there any major difference besides mangling for swift based mach-O file and pure objC machoO file? Do we have a chapter about it?!
Wingzero
 
Posts: 34
Joined: Thu Jul 27, 2017 2:35 am

Re: About Swift naming in final mach-O file

Postby scknight » Wed Sep 20, 2017 12:53 pm

In terms of compiled Swift vs Objective-C there are some differences. I believe the class info is still laid out in a similar way but method calls are different. The Swift code doesn't have to use objc_msgSend for calling other Swift functions. In practice however most code out there today still interacts with Objective-C classes and in turn you'll still see a lot of objc_msgSend calls. This is an older page but does a pretty good job talking about swift and method dispatching.

https://allegro.tech/2014/12/swift-meth ... ching.html
scknight
 
Posts: 27
Joined: Thu Nov 10, 2016 1:01 pm


Return to Questions and Answers

Who is online

Users browsing this forum: No registered users and 6 guests