├── Overrides.xcconfig ├── README.md ├── .gitignore ├── Sources └── Machismo │ ├── Machismo.swift │ ├── Parser.swift │ ├── Graphing │ ├── Edge.swift │ ├── Vertex.swift │ └── AdjacencyList.swift │ ├── Load Commands │ ├── SymTab.swift │ ├── RPath.swift │ ├── MinVersion.swift │ ├── LoadDylib.swift │ └── Segment.swift │ ├── Machismo.h │ ├── Extensions │ ├── Data+Extensions.swift │ └── String+Extensions.swift │ ├── SemanticVersion.swift │ ├── Header.swift │ ├── LoadCommand.swift │ ├── MachOGraph.swift │ ├── FatHeader.swift │ └── MachOFile.swift ├── Tests ├── LinuxMain.swift └── MachismoTests │ ├── XCTestManifests.swift │ └── MachismoTests.swift ├── Makefile ├── Package.swift └── LICENSE /Overrides.xcconfig: -------------------------------------------------------------------------------- 1 | MACOSX_DEPLOYMENT_TARGET = 10.13 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Machismo 2 | 3 | Parsing of Mach-O binaries. 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | -------------------------------------------------------------------------------- /Sources/Machismo/Machismo.swift: -------------------------------------------------------------------------------- 1 | struct Machismo { 2 | var text = "Hello, World!" 3 | } 4 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import MachismoTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += MachismoTests.allTests() 7 | XCTMain(tests) -------------------------------------------------------------------------------- /Tests/MachismoTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !os(macOS) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(MachismoTests.allTests), 7 | ] 8 | } 9 | #endif -------------------------------------------------------------------------------- /Sources/Machismo/Parser.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Parser.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-04. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO 11 | import MachO.fat 12 | 13 | let byteSwappedOrder = NXByteOrder(rawValue: 0) 14 | -------------------------------------------------------------------------------- /Sources/Machismo/Graphing/Edge.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Edge.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public struct Edge: Equatable where T: Hashable { 12 | public var from: Vertex 13 | public var to: Vertex 14 | public let weight: Double? 15 | } 16 | -------------------------------------------------------------------------------- /Sources/Machismo/Graphing/Vertex.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Vertex.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public struct Vertex: Hashable { 12 | var data: T 13 | } 14 | 15 | extension Vertex: CustomStringConvertible { 16 | public var description: String { 17 | return "\(data)" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SWIFTC_FLAGS = -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.13" 2 | CONFIGURATION = debug 3 | 4 | debug: build 5 | 6 | build: 7 | swift build --configuration $(CONFIGURATION) $(SWIFTC_FLAGS) 8 | 9 | test: 10 | swift test $(SWIFTC_FLAGS) 11 | 12 | xcode: 13 | swift package generate-xcodeproj --xcconfig-overrides=Overrides.xcconfig 14 | xed . 15 | 16 | clean: 17 | swift package clean 18 | 19 | .PHONY: debug build test xcode clean 20 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:4.2 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "Machismo", 6 | products: [ 7 | .library(name: "Machismo", targets: ["Machismo"]), 8 | ], 9 | dependencies: [ 10 | // Dependencies declare other packages that this package depends on. 11 | // .package(url: /* package url */, from: "1.0.0"), 12 | ], 13 | targets: [ 14 | .target(name: "Machismo", dependencies: []), 15 | .testTarget(name: "MachismoTests", dependencies: ["Machismo"]), 16 | ] 17 | ) 18 | -------------------------------------------------------------------------------- /Sources/Machismo/Load Commands/SymTab.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SymTab.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-16. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO 11 | 12 | extension LoadCommand { 13 | public struct SymTab: LoadCommandType { 14 | 15 | init(loadCommand: LoadCommand) { 16 | var command = loadCommand.data.extract(symtab_command.self, offset: loadCommand.offset) 17 | if loadCommand.byteSwapped { 18 | swap_symtab_command(&command, byteSwappedOrder) 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Sources/Machismo/Machismo.h: -------------------------------------------------------------------------------- 1 | // 2 | // Machismo.h 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-04. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Machismo. 12 | FOUNDATION_EXPORT double MachismoVersionNumber; 13 | 14 | //! Project version string for Machismo. 15 | FOUNDATION_EXPORT const unsigned char MachismoVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /Sources/Machismo/Load Commands/RPath.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RPath.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-12. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO 11 | 12 | extension LoadCommand { 13 | public struct RPath: LoadCommandType { 14 | let path: String 15 | 16 | init(loadCommand: LoadCommand) { 17 | var command = loadCommand.data.extract(rpath_command.self, offset: loadCommand.offset) 18 | if loadCommand.byteSwapped { 19 | swap_rpath_command(&command, byteSwappedOrder) 20 | } 21 | self.path = String(data: loadCommand.data, offset: loadCommand.offset, commandSize: loadCommand.size, loadCommandString: command.path) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Sources/Machismo/Extensions/Data+Extensions.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Data+Extensions.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | extension Data { 12 | subscript(_ arch: FatHeader.Architecture) -> Data { 13 | return Data(self[arch.offset..<(arch.offset + arch.size)]) 14 | } 15 | 16 | func extract(_ type: T.Type, offset: Int = 0) -> T { 17 | // let ptr = UnsafeMutablePointer.allocate(capacity: 1) 18 | let data = self[offset...size] 19 | return data.withUnsafeBytes { (pointer: UnsafePointer) -> T in 20 | pointer.withMemoryRebound(to: T.self, capacity: 1, { (p) -> T in 21 | return p.pointee 22 | }) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/Machismo/SemanticVersion.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SemanticVersion.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public struct SemanticVersion { 12 | public var majorVersion: Int 13 | public var minorVersion: Int 14 | public var patchVersion: Int 15 | public init() { 16 | self.init(majorVersion: 0, minorVersion: 0, patchVersion: 0) 17 | } 18 | 19 | public init(majorVersion: Int, minorVersion: Int, patchVersion: Int) { 20 | self.majorVersion = majorVersion 21 | self.minorVersion = minorVersion 22 | self.patchVersion = patchVersion 23 | } 24 | 25 | init(_ value: UInt32) { 26 | self.init( 27 | majorVersion: Int(value >> 16), 28 | minorVersion: Int(value >> 8) & 0xFF, 29 | patchVersion: Int(value) & 0xFF 30 | ) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Tests/MachismoTests/MachismoTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MachismoTests.swift 3 | // MachismoTests 4 | // 5 | // Created by Geoffrey Foster on 2018-05-04. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import Machismo 11 | 12 | class MachismoTests: XCTestCase { 13 | let url = URL(fileURLWithPath: "/Applications/Xcode.app/Contents/MacOS/Xcode") 14 | // func testRead() throws { 15 | // let parser = try Parser(url: url) 16 | // XCTAssertTrue(parser.is64Bit) 17 | // XCTAssertFalse(parser.byteSwapped) 18 | // parser.parseHeader() 19 | // //parser.parseSegmentCommands() 20 | // } 21 | 22 | func testFat() throws { 23 | //let machFile = try MachOFile(url: URL(fileURLWithPath: "/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa")) 24 | let machFile = try MachOFile(url: URL(fileURLWithPath: "/Applications/Xcode.app/Contents/MacOS/Xcode")) 25 | print("hello") 26 | } 27 | 28 | func testGraph() throws { 29 | var graph = try MachOGraph(executableURL: url) 30 | print("hello") 31 | let dot = graph.graph.dotRepresentation(name: "\"\(url.path)\"") 32 | print(dot) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Sources/Machismo/Load Commands/MinVersion.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MinVersion.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO 11 | 12 | extension LoadCommand { 13 | public struct MinVersion: LoadCommandType { 14 | public enum Platform { 15 | case iOS 16 | case macOS 17 | case tvOS 18 | case watchOS 19 | } 20 | let platform: Platform 21 | let version: SemanticVersion 22 | let sdk: SemanticVersion 23 | init(loadCommand: LoadCommand) { 24 | var command = loadCommand.data.extract(version_min_command.self, offset: loadCommand.offset) 25 | if loadCommand.byteSwapped { 26 | swap_version_min_command(&command, byteSwappedOrder) 27 | } 28 | self.platform = { 29 | switch Int32(command.cmd) { 30 | case LC_VERSION_MIN_IPHONEOS: 31 | return .iOS 32 | case LC_VERSION_MIN_MACOSX: 33 | return .macOS 34 | case LC_VERSION_MIN_TVOS: 35 | return .tvOS 36 | case LC_VERSION_MIN_WATCHOS: 37 | return .watchOS 38 | default: 39 | fatalError("Unknown Platform") 40 | } 41 | }() 42 | self.version = SemanticVersion(command.version) 43 | self.sdk = SemanticVersion(command.sdk) 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Sources/Machismo/Header.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Header.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-05. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO 11 | 12 | public struct Header { 13 | public let magic: UInt32 /* mach magic number identifier */ 14 | public let cputype: cpu_type_t /* cpu specifier */ 15 | //public let cpusubtype: cpu_subtype_t /* machine specifier */ 16 | public let filetype: UInt32 /* type of file */ 17 | //public let ncmds: UInt32 /* number of load commands */ 18 | //public var sizeofcmds: UInt32 /* the size of all the load commands */ 19 | //public var flags: UInt32 /* flags */ 20 | 21 | public let loadCommandCount: UInt32 22 | public let loadCommandSize: UInt32 23 | 24 | public let size: Int 25 | 26 | public init(header: mach_header_64) { 27 | self.magic = header.magic 28 | self.cputype = header.cputype 29 | self.filetype = header.filetype 30 | self.loadCommandCount = header.ncmds 31 | self.loadCommandSize = header.sizeofcmds 32 | self.size = MemoryLayout.size(ofValue: header) 33 | } 34 | 35 | public init(header: mach_header) { 36 | self.magic = header.magic 37 | self.cputype = header.cputype 38 | self.filetype = header.filetype 39 | self.loadCommandCount = header.ncmds 40 | self.loadCommandSize = header.sizeofcmds 41 | self.size = MemoryLayout.size(ofValue: header) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Sources/Machismo/LoadCommand.swift: -------------------------------------------------------------------------------- 1 | // 2 | // LoadCommand.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-06. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol LoadCommandType { 12 | 13 | } 14 | 15 | struct LoadCommand { 16 | let command: UInt32 17 | let size: Int 18 | 19 | let data: Data 20 | let offset: Int 21 | let byteSwapped: Bool 22 | 23 | init(data: Data, offset: Int, byteSwapped: Bool) { 24 | var loadCommand = data.extract(load_command.self, offset: offset) 25 | if byteSwapped { 26 | swap_load_command(&loadCommand, byteSwappedOrder) 27 | } 28 | self.command = loadCommand.cmd 29 | self.size = Int(loadCommand.cmdsize) 30 | self.data = data 31 | self.offset = offset 32 | self.byteSwapped = byteSwapped 33 | } 34 | 35 | func command(from data: Data, offset: Int, byteSwapped: Bool) -> LoadCommandType? { 36 | switch Int(command) { 37 | case Int(LC_SEGMENT), Int(LC_SEGMENT_64): 38 | return Segment(loadCommand: self) 39 | case Int(LC_RPATH): 40 | return RPath(loadCommand: self) 41 | case Int(LC_LOAD_DYLIB): 42 | return LoadDylib(loadCommand: self) 43 | case Int(LC_VERSION_MIN_IPHONEOS), Int(LC_VERSION_MIN_MACOSX), Int(LC_VERSION_MIN_TVOS), Int(LC_VERSION_MIN_WATCHOS): 44 | return MinVersion(loadCommand: self) 45 | case Int(LC_SYMTAB): 46 | return SymTab(loadCommand: self) 47 | default: 48 | return nil 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Sources/Machismo/Load Commands/LoadDylib.swift: -------------------------------------------------------------------------------- 1 | // 2 | // LoadDylib.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-12. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO 11 | 12 | struct Dylib { 13 | // public var name: lc_str /* library's path name */ 14 | // public var timestamp: UInt32 /* library's build time stamp */ 15 | // public var current_version: UInt32 /* library's current version number */ 16 | // public var compatibility_version: UInt32 /* library's compatibility vers number*/ 17 | 18 | public let name: String 19 | public let timestamp: Date 20 | public let currentVersion: SemanticVersion 21 | public let compatibilityVersion: SemanticVersion 22 | 23 | init(loadCommand: LoadCommand, dylib: dylib) { 24 | self.name = String(loadCommand: loadCommand, string: dylib.name) 25 | self.timestamp = Date(timeIntervalSince1970: Double(dylib.timestamp)) 26 | self.currentVersion = SemanticVersion(dylib.current_version) 27 | self.compatibilityVersion = SemanticVersion(dylib.compatibility_version) 28 | } 29 | } 30 | 31 | extension LoadCommand { 32 | public struct LoadDylib: LoadCommandType { 33 | public let dylib: Dylib 34 | 35 | init(loadCommand: LoadCommand) { 36 | var command = loadCommand.data.extract(dylib_command.self, offset: loadCommand.offset) 37 | if loadCommand.byteSwapped { 38 | swap_dylib_command(&command, byteSwappedOrder) 39 | } 40 | self.dylib = Dylib(loadCommand: loadCommand, dylib: command.dylib) 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Sources/Machismo/Extensions/String+Extensions.swift: -------------------------------------------------------------------------------- 1 | // 2 | // String+Extensions.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | extension String { 12 | init(_ rawCString: (Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8)) { 13 | var rawCString = rawCString 14 | let rawCStringSize = MemoryLayout.size(ofValue: rawCString) 15 | let string = withUnsafePointer(to: &rawCString) { (pointer) -> String in 16 | return pointer.withMemoryRebound(to: UInt8.self, capacity: rawCStringSize, { 17 | return String(cString: $0) 18 | }) 19 | } 20 | self.init(string) 21 | } 22 | 23 | /* 24 | * A variable length string in a load command is represented by an lc_str 25 | * union. The strings are stored just after the load command structure and 26 | * the offset is from the start of the load command structure. The size 27 | * of the string is reflected in the cmdsize field of the load command. 28 | * Once again any padded bytes to bring the cmdsize field to a multiple 29 | * of 4 bytes must be zero. 30 | */ 31 | init(data: Data, offset: Int, commandSize: Int, loadCommandString: lc_str) { 32 | let loadCommandStringOffset = Int(loadCommandString.offset) 33 | let stringOffset = offset + loadCommandStringOffset 34 | let length = commandSize - loadCommandStringOffset 35 | self = String(data: data[stringOffset..<(stringOffset + length)], encoding: .utf8)!.trimmingCharacters(in: .controlCharacters) 36 | } 37 | 38 | init(loadCommand: LoadCommand, string: lc_str) { 39 | let stringOffset = loadCommand.offset + Int(string.offset) 40 | self = String(data: loadCommand.data[stringOffset..<(loadCommand.offset + loadCommand.size)], encoding: .utf8)!.trimmingCharacters(in: .controlCharacters) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Sources/Machismo/Graphing/AdjacencyList.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AdjacencyList.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | final class AdjacencyListGraph { 12 | private var adjacencyList: [Vertex: [Edge]] = [:] 13 | 14 | public init() { 15 | 16 | } 17 | 18 | func addVertex(_ data: T) -> Vertex { 19 | if let vertex = adjacencyList.first(where: { $0.key.data == data })?.key { 20 | return vertex 21 | } 22 | let vertex = Vertex(data: data) 23 | adjacencyList[vertex] = [] 24 | return vertex 25 | } 26 | 27 | func addEdge(from: Vertex, to: Vertex, weight: Double? = nil) { 28 | let edge = Edge(from: from, to: to, weight: weight) 29 | adjacencyList[from, default: []].append(edge) 30 | } 31 | 32 | func dotRepresentation(name: String) -> String { 33 | var lines: [String] = [] 34 | lines.append("digraph \(name) {") 35 | for (vertex, edges) in adjacencyList { 36 | lines.append("\t\"\(vertex)\";") 37 | for edge in edges { 38 | lines.append("\t\"\(edge.from)\" -> \"\(edge.to)\";") 39 | } 40 | } 41 | lines.append("}") 42 | return lines.joined(separator: "\n") 43 | } 44 | } 45 | 46 | extension AdjacencyListGraph { 47 | // var description: String { 48 | // var rows: [String] = [] 49 | // for edgeList in adjacencyList { 50 | // guard let edges = edgeList.edges else { 51 | // continue 52 | // } 53 | // 54 | // var row = [String]() 55 | // for edge in edges { 56 | // var value = "\(edge.to.data)" 57 | // if edge.weight != nil { 58 | // value = "(\(value): \(edge.weight!))" 59 | // } 60 | // row.append(value) 61 | // } 62 | // rows.append("\(edgeList.vertex.data) -> [\(row.joined(separator: ", "))]") 63 | // } 64 | // 65 | // return rows.joined(separator: "\n") 66 | // } 67 | } 68 | -------------------------------------------------------------------------------- /Sources/Machismo/MachOGraph.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MachOGraph.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class MachOGraph { 12 | var binaries: [URL: MachOFile] = [:] 13 | let executableURL: URL 14 | 15 | let graph = AdjacencyListGraph() 16 | 17 | init(executableURL: URL) throws { 18 | self.executableURL = executableURL 19 | 20 | try load(url: executableURL) 21 | } 22 | 23 | func load(url: URL) throws { 24 | guard binaries[url] == nil else { return } 25 | let binary = try MachOFile(url: url) 26 | binaries[url] = binary 27 | 28 | let fromVertex = graph.addVertex(url) 29 | 30 | let loaderExecutablePath = "@executable_path/" 31 | let rpaths: [URL] = binary.rpaths.compactMap { 32 | if $0.hasPrefix(loaderExecutablePath) { 33 | let path = $0.dropFirst(loaderExecutablePath.count) 34 | return URL(fileURLWithPath: String(path), relativeTo: executableURL) 35 | } 36 | return nil 37 | } 38 | 39 | 40 | let dylibURLs: [URL] = binary.dylibs.compactMap { 41 | return path(for: $0, rpaths: rpaths) 42 | } 43 | 44 | try dylibURLs.forEach { 45 | let toVertex = graph.addVertex($0) 46 | graph.addEdge(from: fromVertex, to: toVertex) 47 | try load(url: $0) 48 | } 49 | } 50 | 51 | private func path(for dylib: Dylib, rpaths: [URL]) -> URL? { 52 | let loaderRPath = "@rpath/" 53 | if dylib.name.hasPrefix(loaderRPath) { 54 | for rpath in rpaths { 55 | let path = dylib.name.dropFirst(loaderRPath.count) 56 | let potentialURL = URL(fileURLWithPath: String(path), relativeTo: rpath) 57 | if let reachable = try? potentialURL.checkResourceIsReachable(), reachable { 58 | return potentialURL 59 | } 60 | } 61 | return nil 62 | } else { 63 | return URL(fileURLWithPath: dylib.name) 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Sources/Machismo/Load Commands/Segment.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Segment.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-06. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO 11 | 12 | extension LoadCommand { 13 | public struct Segment: LoadCommandType { 14 | 15 | // public var cmd: UInt32 /* for 64-bit architectures */ /* LC_SEGMENT_64 */ 16 | // public var cmdsize: UInt32 /* includes sizeof section_64 structs */ 17 | // public var segname: (Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8) /* segment name */ 18 | // public var vmaddr: UInt64 /* memory address of this segment */ 19 | // public var vmsize: UInt64 /* memory size of this segment */ 20 | // public var fileoff: UInt64 /* file offset of this segment */ 21 | // public var filesize: UInt64 /* amount to map from the file */ 22 | // public var maxprot: vm_prot_t /* maximum VM protection */ 23 | // public var initprot: vm_prot_t /* initial VM protection */ 24 | // public var nsects: UInt32 /* number of sections in segment */ 25 | // public var flags: UInt32 /* flags */ 26 | 27 | public let name: String 28 | 29 | init(command: segment_command_64) { 30 | self.name = String(command.segname) 31 | } 32 | 33 | init(command: segment_command) { 34 | self.name = String(command.segname) 35 | } 36 | 37 | init(loadCommand: LoadCommand) { 38 | if loadCommand.command == LC_SEGMENT_64 { 39 | var segmentCommand = loadCommand.data.extract(segment_command_64.self, offset: loadCommand.offset) 40 | if loadCommand.byteSwapped { 41 | swap_segment_command_64(&segmentCommand, byteSwappedOrder) 42 | } 43 | self.init(command: segmentCommand) 44 | } else { 45 | var segmentCommand = loadCommand.data.extract(segment_command.self, offset: loadCommand.offset) 46 | if loadCommand.byteSwapped { 47 | swap_segment_command(&segmentCommand, byteSwappedOrder) 48 | } 49 | self.init(command: segmentCommand) 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/Machismo/FatHeader.swift: -------------------------------------------------------------------------------- 1 | // 2 | // FatHeader.swift 3 | // Machismo 4 | // 5 | // Created by Geoffrey Foster on 2018-05-13. 6 | // Copyright © 2018 g-Off.net. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import MachO.fat 11 | 12 | public struct FatHeader { 13 | public struct Architecture { 14 | public var cputype: cpu_type_t /* cpu specifier (int) */ 15 | public var cpusubtype: cpu_subtype_t /* machine specifier (int) */ 16 | public var offset: UInt64 /* file offset to this object file */ 17 | public var size: UInt64 /* size of this object file */ 18 | public var align: UInt32 /* alignment as a power of 2 */ 19 | 20 | init(arch: fat_arch_64) { 21 | self.cputype = arch.cputype 22 | self.cpusubtype = arch.cpusubtype 23 | self.offset = arch.offset 24 | self.size = arch.size 25 | self.align = arch.align 26 | } 27 | 28 | init(arch: fat_arch) { 29 | self.cputype = arch.cputype 30 | self.cpusubtype = arch.cpusubtype 31 | self.offset = UInt64(arch.offset) 32 | self.size = UInt64(arch.size) 33 | self.align = arch.align 34 | } 35 | } 36 | 37 | public let architectures: [Architecture] 38 | init?(data: Data) { 39 | let magic = data.extract(UInt32.self) 40 | guard [FAT_MAGIC, FAT_MAGIC_64, FAT_CIGAM, FAT_CIGAM_64].contains(magic) else { return nil } 41 | 42 | var header = data.extract(fat_header.self) 43 | let is64Bit = [FAT_MAGIC_64, FAT_CIGAM_64].contains(magic) 44 | let byteSwapped = [FAT_CIGAM, FAT_CIGAM_64].contains(magic) 45 | if [FAT_CIGAM, FAT_CIGAM_64].contains(magic) { 46 | swap_fat_header(&header, byteSwappedOrder) 47 | } 48 | var offset = MemoryLayout.size(ofValue: header) 49 | var architectures: [Architecture] = [] 50 | if is64Bit { 51 | for _ in 0.. MachAttributes { 49 | let magic = data.extract(UInt32.self) 50 | let is64Bit = magic == MH_MAGIC_64 || magic == MH_CIGAM_64 51 | let isByteSwapped = magic == MH_CIGAM || magic == MH_CIGAM_64 52 | return MachAttributes(is64Bit: is64Bit, isByteSwapped: isByteSwapped) 53 | } 54 | 55 | private static func header(from data: Data, attributes: MachAttributes) -> Header { 56 | if attributes.is64Bit { 57 | let header = data.extract(mach_header_64.self) 58 | return Header(header: header) 59 | } else { 60 | let header = data.extract(mach_header.self) 61 | return Header(header: header) 62 | } 63 | } 64 | 65 | private static func segmentCommands(from data: Data, header: Header, attributes: MachAttributes) -> [LoadCommandType] { 66 | var segmentCommands: [LoadCommandType] = [] 67 | var offset = header.size 68 | for _ in 0..