Skip to content

A Swift library for dynamically loading shared objects

License

Notifications You must be signed in to change notification settings

llvm-swift/Symbolic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Symbolic

Symbolic makes it easy to get information about an executable, shared object, or static library.

Usage

Note

⚠️ This library is incredibly unsafe. Use it with caution! ⚠️

To dynamically load a shared object file, create a SharedObject, passing in the file URL where the object resides.

You can also use SharedObject.current() to get ahold of the object in which your code will reside once compiled.

let libFoo = SharedObject(object: URL(fileURLWithPath: "/usr/lib/libfoo.dylib"))
let myExe = SharedObject.current()

From there, you can ask the object for the addresses of symbols in the object and (if you're adventurous) cast function addresses to @convention(c) function pointers.

let libcURL = URL(fileURLWithPath: "/usr/lib/libc.dylib")
let libc = SharedObject(object: libcURL)

let addr = libc.address(forSymbol: "sin") // will not be `nil`

typealias SinFn = @convention(c) (Double) -> Double

// Will perform an unsafeBitCast on your behalf!
let sinFn = libc.function(forSymbol: "sin", ofType: SinFn.self)

sinFn?(0.5) // 0.4794255386

Additionally, if you have an address in mind that you've already linked, you can ask for it directly in your current address space:

let addrInfo = SymbolInfo(address: addr)
addrInfo.symbolName // "sin"

Author

Harlan Haskins (@harlanhaskins)

License

This project is released under the MIT license, a copy of which is avaialable in this repository.

About

A Swift library for dynamically loading shared objects

Resources

License

Stars

Watchers

Forks

Packages

No packages published