Skip to content

Instantly share code, notes, and snippets.

@tarunon
Last active August 17, 2019 05:17
Show Gist options
  • Save tarunon/7c51a89993f7c2e087eee6ced22de24e to your computer and use it in GitHub Desktop.
Save tarunon/7c51a89993f7c2e087eee6ced22de24e to your computer and use it in GitHub Desktop.
private struct SwiftFuncWrapper {
var trampolinePtr: UnsafeMutablePointer<UInt64>
var functionObject: UnsafeMutablePointer<SwiftFuncObject>
var functionPtr: UnsafeMutableRawPointer {
let pointer = UnsafeMutablePointer<UInt64>(bitPattern: UInt(functionObject.pointee.address))!
// Getting actual function ptr from instruction.
// 0: 55 push rbp
// 1: 48 89 e5 mov rbp,rsp
// 4: 5d pop rbp
// 5: e9 XX XX ff ff jmp 0xffffXXXX
let relativeJmpRel = pointer.pointee >> 48 + (pointer.advanced(by: 1).pointee << 48) >> 32 + 0xffffffff0000000a
return UnsafeMutableRawPointer(bitPattern: UInt(functionObject.pointee.address &+ relativeJmpRel))!
}
}
private struct SwiftFuncObject {
var originalTypePtr: UnsafeMutablePointer<UInt64>
var unknown: UnsafeMutablePointer<UInt64>
var address: UInt64
var selfPtr: UnsafeMutablePointer<UInt64>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment