Method Internals in Swift 5.0

Method Internals in Swift 5.0

One of the nice things about Swift 5 is the final stabilization of the ABI. This is actually a big deal. The application binary interface defines exactly how data is stored in programs, shared from libraries, things like that. It includes name decoration, class and object definitions, and so on. Now that we have a stable ABI, building tools that analyzed and manipulate these binary representations will become much, well, not easier, but not as much a waste of time. Until now, you were just about guaranteed to have any tools you created broken by new Swift versions. With a stable ABI? This shouldn't happen.

In this post, we continue to get a better understanding of the internal workings of Swift by examining how methods work in this language.

One of the nice things about Swift 5 is the final stabilization of the ABI. This is actually a big deal. The application binary interface defines exactly how data is stored in programs, shared from libraries, things like that. It includes name decoration, class and object definitions, and so on. Now that we have a stable ABI, building tools that analyzed and manipulate these binary representations will become much, well, not easier, but not as much a waste of time. Until now, you were just about guaranteed to have any tools you created broken by new Swift versions. With a stable ABI? This shouldn't happen.

We just covered how classes are defined in Swift 5, and we discovered that they reflect the basic design in Objective-C. There are some key differences though, and one of those is member method definitions.

In Objective-C, you might remember that methods defined in a data pointer are stored in the class definition. This data pointer contained another pointer than references a list of method structures. The method structures contained a name, a pointer to an implementation, and a few other things. Let's see what Swift does.

First, we know swift does use the objc_class structure, and in this case it looks like this:

_$s9swift_cmd7PrinterCN:
  struct __objc_class {
  _$s9swift_cmd7PrinterCMm, // metaclass
  _OBJC_CLASS_$__TtCs12_SwiftObject, // superclass
  __objc_empty_cache, // cache
  0x0, // vtable
  __objc_class__TtC9swift_cmd7Printer_data+1 // data
  }

There's a slight difference here, it seems that the final pointer, the object data pointer, actually points to an offset from the beginning of the objcclassTtC9swift_cmd7Prointer_data structure. If we take a look at that address, we find this:

__objc_class__TtC9swift_cmd7Printer_data:
  struct __objc_data {
    0x80, // flags
    16, // instance start
    32, // instance size
    0x0,
    0x0, // ivar layout
    aTtc9swiftcmd7p, // name
    0x0, // base methods
    0x0, // base protocols
    __objc_class__TtC9swift_cmd7Printer_ivars, // ivars
    0x0, // weak ivar layout
    0x0 // base properties
  }

Okay, so far so good right? Very similar to what we've seen in Objective-C, even with the offset. But look a little closer — there's no corresponding list of methods. Uh oh.

Well, we know that methods are associated with class instantiations somehow. But how? Well, let's take a look at the procedures defined in the executable.

Hopper gives us a list of 29 procedures. The two we're interested in are:

  • _$s9swift_cmd7PrinterC8printMsgyyF  
  • _$s9swift_cmd7PrinterC11printString7messageySS_tF 

How do I know? Well, even though the swift-demangle utility has yet to catch up with the name mangling in Swift 5, if you look closely you can see the names of the methods we're interested in embedded in this procedure names. Let's take a look at $s9swiftcmd7PrinterC11printString7messageySS_tF — it's the method we call, and if you take a look at the Swift implementation you can see it calls printMsg():

func printString(message: String) {
  str_to_print = message
  printMsg()
}

Perhaps it'll give us a clue as to how methods are defined on classes.

Here's the disassembly:

_$s9swift_cmd7PrinterC11printString7messageySS_tF:
  push rbp
  mov rbp, rsp
  push r13
  sub rsp, 0x48
  xorps xmm0, xmm0
  movaps xmmword [rbp+var_20], xmm0
  mov qword [rbp+var_28], 0x0
  mov qword [rbp+var_20], rdi
  mov qword [rbp+var_18], rsi
  mov qword [rbp+var_28], r13
  mov qword [rbp+var_30], rdi
  mov rdi, rsi
  mov qword [rbp+var_38], rsi
  mov qword [rbp+var_40], r13
  call imp___stubs__swift_bridgeObjectRetain ; swift_bridgeObjectRetain
  mov rsi, qword [rbp+var_40]
  mov rdi, qword [rsi]
  mov rdi, qword [rdi+0x60]
  mov r13, qword [rbp+var_30]
  mov qword [rbp+var_48], rdi
  mov rdi, r13
  mov rsi, qword [rbp+var_38]
  mov r13, qword [rbp+var_40]
  mov rcx, qword [rbp+var_48]
  mov qword [rbp+var_50], rax
  call rcx
  mov rax, qword [rbp+var_40]
  mov rcx, qword [rax]
  mov rcx, qword [rcx+0x78]
  mov r13, rax
  call rcx
  add rsp, 0x48
  pop r13
  pop rbp
  ret

We have two CALL opcodes. The first calls out to an external function, bridgeObjectRetain(). That's not really what we're interested in, so let's take a look at the second:

mov rcx, qword [rbp+var_48]
mov qword [rbp+var_50], rax
call rcx

The second CALL references the RCX register, which is loaded with an address from the stack. Yes, from the stack. This is a MAJOR change from Objective-C, and we'll need to start up LLDB to look at this a bit more closely.

Originally published by Christopher Lamb at dzone.com

------------------------------------------------------------------------------------------------------------------------------------------

Follow great articles on Twitter


Learn More

☞ iOS 12 & Swift - The Complete iOS App Development Bootcamp

☞ Learn Swift 4: From Beginner to Advanced

☞ Creating an iOS app with user presence using Node.js and Swift

☞ Learn Swift 4 for Beginners

☞ What’s new in Swift 5.0?

swift

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

10 Free Online Resources To Learn Swift Language

Swift is a fast and efficient general-purpose programming language that provides real-time feedback and can be seamlessly incorporated.

Hire Dedicated Swift Developers

Want to create a native iOS application for your Startup? **[Hire Dedicated Swift Developers](https://hourlydeveloper.io/hire-dedicated-swift-developer/ "Hire Dedicated Swift Developers")** for end-to-end services like development, migration...

Useful Swift Extension

After working with Swift for some time I found there are lot of things that aren’t there out of the box as with other languages. Following are some extensions that I think are useful for day to day working

Resources are Coming to Swift Packages

You can now add resources to your swift packages in Xcode 12. What does it mean?In one of my previous articles, I mentioned how SPM (Swift Package Manager) changed the way I build and maintain my Code. I praised the deep integration Xcode 11 has with Swift Packages and how easy it is to create a tremendous modular Code.You cannot add images, data files, or even a storyboard.

Getting Started with Swift on Ubuntu

Did you know that Swift files can be compiled on Ubuntu? Heres a simple guide to set up a Swift compiler on Ubuntu machines.