Skip to content

Commit a1e8e8c

Browse files
authored
Merge pull request #119 from p-x9/feature/djustments-for-linux-support
Some adjustments for linux support
2 parents c3ef0ac + ec0ed0d commit a1e8e8c

File tree

8 files changed

+60
-54
lines changed

8 files changed

+60
-54
lines changed

Sources/MachOKit/DyldCache.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ public class DyldCache {
5050
)
5151

5252
// check magic of header
53-
// FIXME: error instead of unwrap
54-
let cpuType = header._cpuType!
55-
let cpuSubType = header._cpuSubType!
53+
guard header.magic.starts(with: "dyld_") else {
54+
throw MachOKitError.invalidMagic
55+
}
56+
57+
guard let cpuType = header._cpuType,
58+
let cpuSubType = header._cpuSubType else {
59+
throw MachOKitError.invalidCpuType
60+
}
5661
self.cpu = .init(
5762
typeRawValue: cpuType.rawValue,
5863
subtypeRawValue: cpuSubType.rawValue

Sources/MachOKit/Header/CPU.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extension CPU {
4444
}
4545
}
4646

47+
#if canImport(Darwin)
4748
extension CPU {
4849
/// CPU type and subtype of host pc
4950
static var current: CPU? {
@@ -57,3 +58,4 @@ extension CPU {
5758
)
5859
}
5960
}
61+
#endif

Sources/MachOKit/Header/CPUSubType.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,10 @@ extension CPUARM64_32SubType: CustomStringConvertible {
10511051
}
10521052
}
10531053

1054+
#if canImport(Darwin)
10541055
extension CPUSubType {
10551056
/// CPU subtype of host pc
10561057
static var current: CPUSubType? {
1057-
#if canImport(Darwin)
10581058
guard let cpuType: CPUType = .current else {
10591059
return nil
10601060
}
@@ -1063,11 +1063,9 @@ extension CPUSubType {
10631063
let ret = sysctlbyname("hw.cpusubtype", &subtype, &size, nil, 0)
10641064
guard ret != -1 else { return nil }
10651065
return .init(rawValue: subtype, of: cpuType)
1066-
#else
1067-
return nil
1068-
#endif
10691066
}
10701067
}
1068+
#endif
10711069

10721070
/*
10731071
I386 series declarations cannot be used from swift

Sources/MachOKit/Header/CPUType.swift

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ extension CPUType: RawRepresentable {
4949
public init?(rawValue: cpu_type_t) {
5050
switch rawValue {
5151
case RawValue(CPU_TYPE_ANY): self = .any
52-
case CPU_TYPE_VAX: self = .vax
53-
case CPU_TYPE_MC680x0: self = .mc680x0
54-
case CPU_TYPE_X86: self = .x86
55-
case CPU_TYPE_I386: self = .i386
56-
case CPU_TYPE_X86_64: self = .x86_64
57-
case CPU_TYPE_MC98000: self = .mc98000
58-
case CPU_TYPE_HPPA: self = .hppa
59-
case CPU_TYPE_ARM: self = .arm
60-
case CPU_TYPE_ARM64: self = .arm64
61-
case CPU_TYPE_ARM64_32: self = .arm64_32
62-
case CPU_TYPE_MC88000: self = .mc88000
63-
case CPU_TYPE_SPARC: self = .sparc
64-
case CPU_TYPE_I860: self = .i860
65-
case CPU_TYPE_POWERPC: self = .powerpc
66-
case CPU_TYPE_POWERPC64: self = .powerpc64
52+
case RawValue(CPU_TYPE_VAX): self = .vax
53+
case RawValue(CPU_TYPE_MC680x0): self = .mc680x0
54+
case RawValue(CPU_TYPE_X86): self = .x86
55+
case RawValue(CPU_TYPE_I386): self = .i386
56+
case RawValue(CPU_TYPE_X86_64): self = .x86_64
57+
case RawValue(CPU_TYPE_MC98000): self = .mc98000
58+
case RawValue(CPU_TYPE_HPPA): self = .hppa
59+
case RawValue(CPU_TYPE_ARM): self = .arm
60+
case RawValue(CPU_TYPE_ARM64): self = .arm64
61+
case RawValue(CPU_TYPE_ARM64_32): self = .arm64_32
62+
case RawValue(CPU_TYPE_MC88000): self = .mc88000
63+
case RawValue(CPU_TYPE_SPARC): self = .sparc
64+
case RawValue(CPU_TYPE_I860): self = .i860
65+
case RawValue(CPU_TYPE_POWERPC): self = .powerpc
66+
case RawValue(CPU_TYPE_POWERPC64): self = .powerpc64
6767
default:
6868
return nil
6969
}
@@ -72,21 +72,21 @@ extension CPUType: RawRepresentable {
7272
public var rawValue: cpu_type_t {
7373
switch self {
7474
case .any: RawValue(CPU_TYPE_ANY)
75-
case .vax: CPU_TYPE_VAX
76-
case .mc680x0: CPU_TYPE_MC680x0
77-
case .x86: CPU_TYPE_X86
78-
case .i386: CPU_TYPE_I386
79-
case .x86_64: CPU_TYPE_X86_64
80-
case .mc98000: CPU_TYPE_MC98000
81-
case .hppa: CPU_TYPE_HPPA
82-
case .arm: CPU_TYPE_ARM
83-
case .arm64: CPU_TYPE_ARM64
84-
case .arm64_32: CPU_TYPE_ARM64_32
85-
case .mc88000: CPU_TYPE_MC88000
86-
case .sparc: CPU_TYPE_SPARC
87-
case .i860: CPU_TYPE_I860
88-
case .powerpc: CPU_TYPE_POWERPC
89-
case .powerpc64: CPU_TYPE_POWERPC64
75+
case .vax: RawValue(CPU_TYPE_VAX)
76+
case .mc680x0: RawValue(CPU_TYPE_MC680x0)
77+
case .x86: RawValue(CPU_TYPE_X86)
78+
case .i386: RawValue(CPU_TYPE_I386)
79+
case .x86_64: RawValue(CPU_TYPE_X86_64)
80+
case .mc98000: RawValue(CPU_TYPE_MC98000)
81+
case .hppa: RawValue(CPU_TYPE_HPPA)
82+
case .arm: RawValue(CPU_TYPE_ARM)
83+
case .arm64: RawValue(CPU_TYPE_ARM64)
84+
case .arm64_32: RawValue(CPU_TYPE_ARM64_32)
85+
case .mc88000: RawValue(CPU_TYPE_MC88000)
86+
case .sparc: RawValue(CPU_TYPE_SPARC)
87+
case .i860: RawValue(CPU_TYPE_I860)
88+
case .powerpc: RawValue(CPU_TYPE_POWERPC)
89+
case .powerpc64: RawValue(CPU_TYPE_POWERPC64)
9090
}
9191
}
9292
}
@@ -124,10 +124,10 @@ extension CPUType {
124124
}
125125
}
126126

127+
#if canImport(Darwin)
127128
extension CPUType {
128129
/// CPU type of host pc
129130
static var current: CPUType? {
130-
#if canImport(Darwin)
131131
var type: cpu_type_t = 0
132132
var size = MemoryLayout<cpu_type_t>.size
133133
let ret = sysctlbyname("hw.cputype", &type, &size, nil, 0)
@@ -141,8 +141,6 @@ extension CPUType {
141141
}
142142

143143
return .init(rawValue: type)
144-
#else
145-
return nil
146-
#endif
147144
}
148145
}
146+
#endif

Sources/MachOKit/MachOImage+static.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
import Foundation
1010

11-
11+
#if canImport(Darwin)
1212
extension MachOImage {
1313
/// Sequence of loaded machO images.
1414
public static var images: AnySequence<MachOImage> {
15-
#if canImport(Darwin)
1615
AnySequence(
1716
(0..<_dyld_image_count())
1817
.lazy
@@ -21,9 +20,6 @@ extension MachOImage {
2120
MachOImage(ptr: $0)
2221
}
2322
)
24-
#else
25-
AnySequence([])
26-
#endif
2723
}
2824
}
2925

@@ -148,3 +144,4 @@ fileprivate extension MachOImage {
148144
return addressRange.contains(address)
149145
}
150146
}
147+
#endif

Sources/MachOKit/MachOKit.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ public enum File {
55
case fat(FatFile)
66
}
77

8+
public enum MachOKitError: LocalizedError {
9+
case invalidMagic
10+
case invalidCpuType
11+
}
12+
813
public func loadFromFile(url: URL) throws -> File {
914
let fileHandle = try FileHandle(forReadingFrom: url)
1015
let magicRaw: UInt32 = fileHandle.read(offset: 0)
11-
12-
// FIXME: error instead of unwrap
13-
let magic = Magic(rawValue: magicRaw)!
16+
17+
guard let magic = Magic(rawValue: magicRaw) else {
18+
throw MachOKitError.invalidMagic
19+
}
1420

1521
if magic.isFat {
1622
return .fat(try FatFile(url: url))

Sources/MachOKit/Model/Codesign/CodeDirectory/CodeSignCodeDirectory.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
import Foundation
1010
import MachOKitC
11-
import Crypto
11+
//#if compiler(>=5.10)
12+
//private import Crypto
13+
//#else
14+
@_implementationOnly import Crypto
15+
//#endif
1216

1317
public struct CodeSignCodeDirectory: LayoutWrapper {
1418
public typealias Layout = CS_CodeDirectory

Tests/MachOKitTests/MachOPrintTests.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ extension MachOPrintTests {
406406
}
407407
}
408408

409-
#if canImport(Darwin)
410409
extension MachOPrintTests {
411410
func testClosestSymbol() {
412411
for symbol in machO.symbols.shuffled().prefix(100) {
@@ -445,7 +444,6 @@ extension MachOPrintTests {
445444
}
446445
}
447446
}
448-
#endif
449447

450448
extension MachOPrintTests {
451449
func testFindSymbolByName() {
@@ -473,7 +471,6 @@ extension MachOPrintTests {
473471
}
474472
}
475473

476-
#if canImport(Darwin)
477474
extension MachOPrintTests {
478475
func testFunctionStarts() {
479476
guard let functionStarts = machO.functionStarts else { return }
@@ -500,7 +497,6 @@ extension MachOPrintTests {
500497
}
501498
}
502499
}
503-
#endif
504500

505501
extension MachOPrintTests {
506502
func testDataInCode() {

0 commit comments

Comments
 (0)