Skip to content

Commit

Permalink
Merge pull request #119 from p-x9/feature/djustments-for-linux-support
Browse files Browse the repository at this point in the history
Some adjustments for linux support
  • Loading branch information
p-x9 authored Oct 1, 2024
2 parents c3ef0ac + ec0ed0d commit a1e8e8c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 54 deletions.
11 changes: 8 additions & 3 deletions Sources/MachOKit/DyldCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ public class DyldCache {
)

// check magic of header
// FIXME: error instead of unwrap
let cpuType = header._cpuType!
let cpuSubType = header._cpuSubType!
guard header.magic.starts(with: "dyld_") else {
throw MachOKitError.invalidMagic
}

guard let cpuType = header._cpuType,
let cpuSubType = header._cpuSubType else {
throw MachOKitError.invalidCpuType
}
self.cpu = .init(
typeRawValue: cpuType.rawValue,
subtypeRawValue: cpuSubType.rawValue
Expand Down
2 changes: 2 additions & 0 deletions Sources/MachOKit/Header/CPU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extension CPU {
}
}

#if canImport(Darwin)
extension CPU {
/// CPU type and subtype of host pc
static var current: CPU? {
Expand All @@ -57,3 +58,4 @@ extension CPU {
)
}
}
#endif
6 changes: 2 additions & 4 deletions Sources/MachOKit/Header/CPUSubType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,10 @@ extension CPUARM64_32SubType: CustomStringConvertible {
}
}

#if canImport(Darwin)
extension CPUSubType {
/// CPU subtype of host pc
static var current: CPUSubType? {
#if canImport(Darwin)
guard let cpuType: CPUType = .current else {
return nil
}
Expand All @@ -1063,11 +1063,9 @@ extension CPUSubType {
let ret = sysctlbyname("hw.cpusubtype", &subtype, &size, nil, 0)
guard ret != -1 else { return nil }
return .init(rawValue: subtype, of: cpuType)
#else
return nil
#endif
}
}
#endif

/*
I386 series declarations cannot be used from swift
Expand Down
66 changes: 32 additions & 34 deletions Sources/MachOKit/Header/CPUType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ extension CPUType: RawRepresentable {
public init?(rawValue: cpu_type_t) {
switch rawValue {
case RawValue(CPU_TYPE_ANY): self = .any
case CPU_TYPE_VAX: self = .vax
case CPU_TYPE_MC680x0: self = .mc680x0
case CPU_TYPE_X86: self = .x86
case CPU_TYPE_I386: self = .i386
case CPU_TYPE_X86_64: self = .x86_64
case CPU_TYPE_MC98000: self = .mc98000
case CPU_TYPE_HPPA: self = .hppa
case CPU_TYPE_ARM: self = .arm
case CPU_TYPE_ARM64: self = .arm64
case CPU_TYPE_ARM64_32: self = .arm64_32
case CPU_TYPE_MC88000: self = .mc88000
case CPU_TYPE_SPARC: self = .sparc
case CPU_TYPE_I860: self = .i860
case CPU_TYPE_POWERPC: self = .powerpc
case CPU_TYPE_POWERPC64: self = .powerpc64
case RawValue(CPU_TYPE_VAX): self = .vax
case RawValue(CPU_TYPE_MC680x0): self = .mc680x0
case RawValue(CPU_TYPE_X86): self = .x86
case RawValue(CPU_TYPE_I386): self = .i386
case RawValue(CPU_TYPE_X86_64): self = .x86_64
case RawValue(CPU_TYPE_MC98000): self = .mc98000
case RawValue(CPU_TYPE_HPPA): self = .hppa
case RawValue(CPU_TYPE_ARM): self = .arm
case RawValue(CPU_TYPE_ARM64): self = .arm64
case RawValue(CPU_TYPE_ARM64_32): self = .arm64_32
case RawValue(CPU_TYPE_MC88000): self = .mc88000
case RawValue(CPU_TYPE_SPARC): self = .sparc
case RawValue(CPU_TYPE_I860): self = .i860
case RawValue(CPU_TYPE_POWERPC): self = .powerpc
case RawValue(CPU_TYPE_POWERPC64): self = .powerpc64
default:
return nil
}
Expand All @@ -72,21 +72,21 @@ extension CPUType: RawRepresentable {
public var rawValue: cpu_type_t {
switch self {
case .any: RawValue(CPU_TYPE_ANY)
case .vax: CPU_TYPE_VAX
case .mc680x0: CPU_TYPE_MC680x0
case .x86: CPU_TYPE_X86
case .i386: CPU_TYPE_I386
case .x86_64: CPU_TYPE_X86_64
case .mc98000: CPU_TYPE_MC98000
case .hppa: CPU_TYPE_HPPA
case .arm: CPU_TYPE_ARM
case .arm64: CPU_TYPE_ARM64
case .arm64_32: CPU_TYPE_ARM64_32
case .mc88000: CPU_TYPE_MC88000
case .sparc: CPU_TYPE_SPARC
case .i860: CPU_TYPE_I860
case .powerpc: CPU_TYPE_POWERPC
case .powerpc64: CPU_TYPE_POWERPC64
case .vax: RawValue(CPU_TYPE_VAX)
case .mc680x0: RawValue(CPU_TYPE_MC680x0)
case .x86: RawValue(CPU_TYPE_X86)
case .i386: RawValue(CPU_TYPE_I386)
case .x86_64: RawValue(CPU_TYPE_X86_64)
case .mc98000: RawValue(CPU_TYPE_MC98000)
case .hppa: RawValue(CPU_TYPE_HPPA)
case .arm: RawValue(CPU_TYPE_ARM)
case .arm64: RawValue(CPU_TYPE_ARM64)
case .arm64_32: RawValue(CPU_TYPE_ARM64_32)
case .mc88000: RawValue(CPU_TYPE_MC88000)
case .sparc: RawValue(CPU_TYPE_SPARC)
case .i860: RawValue(CPU_TYPE_I860)
case .powerpc: RawValue(CPU_TYPE_POWERPC)
case .powerpc64: RawValue(CPU_TYPE_POWERPC64)
}
}
}
Expand Down Expand Up @@ -124,10 +124,10 @@ extension CPUType {
}
}

#if canImport(Darwin)
extension CPUType {
/// CPU type of host pc
static var current: CPUType? {
#if canImport(Darwin)
var type: cpu_type_t = 0
var size = MemoryLayout<cpu_type_t>.size
let ret = sysctlbyname("hw.cputype", &type, &size, nil, 0)
Expand All @@ -141,8 +141,6 @@ extension CPUType {
}

return .init(rawValue: type)
#else
return nil
#endif
}
}
#endif
7 changes: 2 additions & 5 deletions Sources/MachOKit/MachOImage+static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

import Foundation


#if canImport(Darwin)
extension MachOImage {
/// Sequence of loaded machO images.
public static var images: AnySequence<MachOImage> {
#if canImport(Darwin)
AnySequence(
(0..<_dyld_image_count())
.lazy
Expand All @@ -21,9 +20,6 @@ extension MachOImage {
MachOImage(ptr: $0)
}
)
#else
AnySequence([])
#endif
}
}

Expand Down Expand Up @@ -148,3 +144,4 @@ fileprivate extension MachOImage {
return addressRange.contains(address)
}
}
#endif
12 changes: 9 additions & 3 deletions Sources/MachOKit/MachOKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ public enum File {
case fat(FatFile)
}

public enum MachOKitError: LocalizedError {
case invalidMagic
case invalidCpuType
}

public func loadFromFile(url: URL) throws -> File {
let fileHandle = try FileHandle(forReadingFrom: url)
let magicRaw: UInt32 = fileHandle.read(offset: 0)

// FIXME: error instead of unwrap
let magic = Magic(rawValue: magicRaw)!

guard let magic = Magic(rawValue: magicRaw) else {
throw MachOKitError.invalidMagic
}

if magic.isFat {
return .fat(try FatFile(url: url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import Foundation
import MachOKitC
import Crypto
//#if compiler(>=5.10)
//private import Crypto
//#else
@_implementationOnly import Crypto
//#endif

public struct CodeSignCodeDirectory: LayoutWrapper {
public typealias Layout = CS_CodeDirectory
Expand Down
4 changes: 0 additions & 4 deletions Tests/MachOKitTests/MachOPrintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ extension MachOPrintTests {
}
}

#if canImport(Darwin)
extension MachOPrintTests {
func testClosestSymbol() {
for symbol in machO.symbols.shuffled().prefix(100) {
Expand Down Expand Up @@ -445,7 +444,6 @@ extension MachOPrintTests {
}
}
}
#endif

extension MachOPrintTests {
func testFindSymbolByName() {
Expand Down Expand Up @@ -473,7 +471,6 @@ extension MachOPrintTests {
}
}

#if canImport(Darwin)
extension MachOPrintTests {
func testFunctionStarts() {
guard let functionStarts = machO.functionStarts else { return }
Expand All @@ -500,7 +497,6 @@ extension MachOPrintTests {
}
}
}
#endif

extension MachOPrintTests {
func testDataInCode() {
Expand Down

0 comments on commit a1e8e8c

Please sign in to comment.