Skip to content

Commit bca86ee

Browse files
authored
Merge pull request #13 from MxIris-Reverse-Engineering/develop
0.5.0
2 parents 36b465b + d11a383 commit bca86ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+690
-508
lines changed

Package.swift

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
// swift-tools-version: 5.10
1+
// swift-tools-version: 6.1
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

4-
import PackageDescription
4+
@preconcurrency import PackageDescription
55
import CompilerPluginSupport
66

7-
let useSPMPrebuildVersion = false
7+
func envEnable(_ key: String, default defaultValue: Bool = false) -> Bool {
8+
guard let value = Context.environment[key] else {
9+
return defaultValue
10+
}
11+
if value == "1" {
12+
return true
13+
} else if value == "0" {
14+
return false
15+
} else {
16+
return defaultValue
17+
}
18+
}
19+
20+
let isSilentTest = envEnable("MACHO_SWIFT_SECTION_SILENT_TEST", default: false)
21+
22+
let useSPMPrebuildVersion = envEnable("MACHO_SWIFT_SECTION_USE_SPM_PREBUILD_VERSION", default: false)
23+
24+
var testSettings: [SwiftSetting] = []
25+
26+
if isSilentTest {
27+
testSettings.append(.define("SILENT_TEST"))
28+
}
829

930
extension Package.Dependency {
1031
static let MachOKit: Package.Dependency = {
@@ -72,7 +93,7 @@ extension Target.Dependency {
7293

7394
let package = Package(
7495
name: "MachOSwiftSection",
75-
platforms: [.macOS(.v10_15)],
96+
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
7697
products: [
7798
.library(
7899
name: "MachOSwiftSection",
@@ -89,7 +110,7 @@ let package = Package(
89110
],
90111
dependencies: [
91112
.MachOKit,
92-
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"602.0.0"),
113+
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0" ..< "602.0.0"),
93114
.package(url: "https://github.com/p-x9/AssociatedObject", from: "0.13.0"),
94115
.package(url: "https://github.com/p-x9/swift-fileio.git", from: "0.9.0"),
95116
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.1"),
@@ -111,6 +132,7 @@ let package = Package(
111132
name: "MachOExtensions",
112133
dependencies: [
113134
.MachOKit,
135+
"MachOMacro",
114136
]
115137
),
116138

@@ -146,10 +168,10 @@ let package = Package(
146168
.target(
147169
name: "MachOSwiftSection",
148170
dependencies: [
171+
.MachOKit,
149172
"Demangle",
150173
"MachOFoundation",
151174
"MachOMacro",
152-
.MachOKit,
153175
]
154176
),
155177

@@ -158,7 +180,9 @@ let package = Package(
158180
dependencies: [
159181
.MachOKit,
160182
"MachOExtensions",
161-
]
183+
"SwiftDump",
184+
],
185+
swiftSettings: testSettings
162186
),
163187

164188
.target(
@@ -209,15 +233,18 @@ let package = Package(
209233
"MachOSwiftSection",
210234
"SwiftDump",
211235
"MachOTestingSupport",
212-
]
236+
],
237+
swiftSettings: testSettings
213238
),
214239

215240
.testTarget(
216241
name: "SwiftDumpTests",
217242
dependencies: [
218243
"SwiftDump",
219244
"MachOTestingSupport",
220-
]
245+
],
246+
swiftSettings: testSettings
221247
),
222-
]
248+
],
249+
swiftLanguageModes: [.v5]
223250
)

Sources/Demangle/Main/DemangleOptions.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public struct DemangleOptions: OptionSet, Codable, Sendable {
2424
public static let printForTypeName = DemangleOptions(rawValue: 1 << 19)
2525
public static let showClosureSignature = DemangleOptions(rawValue: 1 << 20)
2626
public static let showModuleInDependentMemberType = DemangleOptions(rawValue: 1 << 21)
27-
public static let showPrefixAndSuffix = DemangleOptions(rawValue: 1 << 22)
2827

28+
package static let removeWeakPrefix = DemangleOptions(rawValue: 1 << 22)
2929

3030
public init(rawValue: Int) {
3131
self.rawValue = rawValue
@@ -47,8 +47,7 @@ public struct DemangleOptions: OptionSet, Codable, Sendable {
4747
.displayStdlibModule,
4848
.displayObjCModule,
4949
.showClosureSignature,
50-
.showModuleInDependentMemberType,
51-
.showPrefixAndSuffix
50+
.showModuleInDependentMemberType
5251
]
5352

5453
public static let simplified: DemangleOptions = [

Sources/Demangle/Main/NodePrinter.swift

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct NodePrinter: Sendable {
55
var specializationPrefixPrinted: Bool
66
var options: DemangleOptions
77
var hidingCurrentModule: String = ""
8-
8+
99
init(options: DemangleOptions = .default) {
1010
self.target = .init()
1111
self.specializationPrefixPrinted = false
@@ -16,7 +16,7 @@ struct NodePrinter: Sendable {
1616
guard options.contains(.qualifyEntities) else {
1717
return false
1818
}
19-
if !options.contains(.showModuleInDependentMemberType), let dependentMemberType = context.parent?.parent?.parent?.parent, dependentMemberType.kind == .dependentMemberType {
19+
if !options.contains(.showModuleInDependentMemberType), let dependentMemberType = context.parent?.parent?.parent?.parent, dependentMemberType.kind == .dependentMemberType {
2020
return false
2121
}
2222
if context.kind == .module, let text = context.text, !text.isEmpty {
@@ -35,13 +35,9 @@ struct NodePrinter: Sendable {
3535

3636
mutating func printOptional(_ optional: Node?, prefix: String? = nil, suffix: String? = nil, asPrefixContext: Bool = false) -> Node? {
3737
guard let o = optional else { return nil }
38-
if options.contains(.showPrefixAndSuffix) {
39-
prefix.map { target.write($0) }
40-
}
38+
prefix.map { target.write($0) }
4139
let r = printName(o)
42-
if options.contains(.showPrefixAndSuffix) {
43-
suffix.map { target.write($0) }
44-
}
40+
suffix.map { target.write($0) }
4541
return r
4642
}
4743

@@ -105,7 +101,7 @@ struct NodePrinter: Sendable {
105101

106102
mutating func printModule(_ name: Node) {
107103
if options.contains(.displayModuleNames) {
108-
target.write(name.text ?? "")
104+
target.write(name.text ?? "", type: .other)
109105
}
110106
}
111107

@@ -848,7 +844,7 @@ struct NodePrinter: Sendable {
848844

849845
mutating func printImplDifferentiabilityKind(_ name: Node) {
850846
target.write("@differentiable")
851-
if case let .index(value) = name.contents, let differentiability = Differentiability(value) {
847+
if case .index(let value) = name.contents, let differentiability = Differentiability(value) {
852848
switch differentiability {
853849
case .normal: break
854850
case .linear: target.write("(_linear)")
@@ -859,7 +855,7 @@ struct NodePrinter: Sendable {
859855
}
860856

861857
mutating func printImplCoroutineKind(_ name: Node) {
862-
guard case let .name(value) = name.contents, !value.isEmpty else { return }
858+
guard case .name(let value) = name.contents, !value.isEmpty else { return }
863859
target.write("@\(value)")
864860
}
865861

@@ -876,7 +872,7 @@ struct NodePrinter: Sendable {
876872
}
877873

878874
mutating func printImplParameterName(_ name: Node) {
879-
guard case let .name(value) = name.contents, !value.isEmpty else { return }
875+
guard case .name(let value) = name.contents, !value.isEmpty else { return }
880876
target.write("\(value) ")
881877
}
882878

@@ -1183,7 +1179,7 @@ struct NodePrinter: Sendable {
11831179
case .globalVariableOnceFunction,
11841180
.globalVariableOnceToken: printGlobalVariableOnceFunction(name)
11851181
case .hasSymbolQuery: target.write("#_hasSymbol query for ")
1186-
case .identifier: target.write(name.text ?? "", type: (name.parent?.kind == .function || name.parent?.kind == .variable) ? .functionDeclaration : .typeName)
1182+
case .identifier: printIdentifier(name, asPrefixContext: asPrefixContext)
11871183
case .implConvention: target.write(name.text ?? "")
11881184
case .implCoroutineKind: printImplCoroutineKind(name)
11891185
case .implDifferentiabilityKind: printImplDifferentiabilityKind(name)
@@ -1396,13 +1392,36 @@ struct NodePrinter: Sendable {
13961392
case .variadicMarker: target.write(" variadic-marker ")
13971393
case .vTableAttribute: target.write("override ")
13981394
case .vTableThunk: printVTableThunk(name)
1399-
case .weak: printFirstChild(name, prefix: "weak ")
1395+
case .weak: printFirstChild(name, prefix: options.contains(.removeWeakPrefix) ? "" : "weak ")
14001396
case .willSet: return printAbstractStorage(name.children.first, asPrefixContext: asPrefixContext, extraName: "willset")
14011397
}
14021398

14031399
return nil
14041400
}
14051401

1402+
mutating func printIdentifier(_ name: Node, asPrefixContext: Bool = false) {
1403+
let semanticType: SemanticType
1404+
1405+
switch name.parent?.kind {
1406+
case .function:
1407+
semanticType = .function(.declaration)
1408+
case .variable:
1409+
semanticType = .variable
1410+
case .enum:
1411+
semanticType = .type(.enum, .name)
1412+
case .structure:
1413+
semanticType = .type(.struct, .name)
1414+
case .class:
1415+
semanticType = .type(.class, .name)
1416+
case .protocol:
1417+
semanticType = .type(.protocol, .name)
1418+
default:
1419+
semanticType = .standard
1420+
}
1421+
1422+
target.write(name.text ?? "", type: semanticType)
1423+
}
1424+
14061425
mutating func printAbstractStorage(_ name: Node?, asPrefixContext: Bool, extraName: String) -> Node? {
14071426
guard let n = name else { return nil }
14081427
switch n.kind {
@@ -1609,17 +1628,17 @@ struct NodePrinter: Sendable {
16091628
target.write("(")
16101629
for tuple in parameters.children.enumerated() {
16111630
if let label = labelList?.children.at(tuple.offset) {
1612-
target.write(label.kind == .identifier ? (label.text ?? "") : "_", type: .functionDeclaration)
1631+
target.write(label.kind == .identifier ? (label.text ?? "") : "_", type: .function(.declaration))
16131632
target.write(":")
16141633
if showTypes {
16151634
target.write(" ")
16161635
}
16171636
} else if !showTypes {
16181637
if let label = tuple.element.children.first(where: { $0.kind == .tupleElementName }) {
1619-
target.write(label.text ?? "", type: .functionDeclaration)
1638+
target.write(label.text ?? "", type: .function(.declaration))
16201639
target.write(":")
16211640
} else {
1622-
target.write("_", type: .functionDeclaration)
1641+
target.write("_", type: .function(.declaration))
16231642
target.write(":")
16241643
}
16251644
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Foundation
2+
import MachOKit
3+
import MachOMacro
4+
5+
@MachOImageAllMembersGenerator
6+
extension MachORepresentable {
7+
package func _section(
8+
for name: String,
9+
in machOFile: MachOFile
10+
) -> (any SectionProtocol)? {
11+
sections.first(
12+
where: {
13+
$0.sectionName == name
14+
}
15+
)
16+
}
17+
}

Sources/MachOExtensions/MachORepresentableWithCache.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import MachOKit
22

33
package protocol MachORepresentableWithCache: MachORepresentable {
4-
var cache: DyldCache? { get }
4+
associatedtype Cache: DyldCacheRepresentable
5+
6+
var cache: Cache? { get }
57
var startOffset: Int { get }
68
}
79

@@ -16,8 +18,22 @@ extension MachOFile: MachORepresentableWithCache {
1618
}
1719

1820
extension MachOImage: MachORepresentableWithCache {
19-
package var cache: DyldCache? { nil }
20-
package var startOffset: Int { 0 }
21+
package var cache: DyldCacheLoaded? {
22+
guard let currentCache = DyldCacheLoaded.current else { return nil }
23+
24+
if ptr.int - currentCache.mainCacheHeader.sharedRegionStart.cast() >= 0 {
25+
return currentCache
26+
}
27+
return nil
28+
}
29+
30+
package var startOffset: Int {
31+
if let cache {
32+
return cache.mainCacheHeader.sharedRegionStart.cast()
33+
} else {
34+
return 0
35+
}
36+
}
2137
}
2238

2339
package func address<MachO: MachORepresentableWithCache>(of fileOffset: Int, in machO: MachO) -> UInt64 {

Sources/MachOExtensions/SegmentCommandProtocol+.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ extension SegmentCommandProtocol {
1818
)
1919
}
2020
}
21+
22+
23+

Sources/MachOPointer/Symbol/MachOSymbolCache.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ class MachOSymbolCache {
3434
var cacheEntry: CacheEntry = [:]
3535
for symbol in symbols64 where !symbol.name.isEmpty {
3636
var offset = symbol.offset
37+
cacheEntry[offset] = .init(offset: offset, stringValue: symbol.name)
3738
if let cache = machO.cache {
3839
offset -= cache.mainCacheHeader.sharedRegionStart.cast()
40+
cacheEntry[offset] = .init(offset: offset, stringValue: symbol.name)
3941
}
40-
cacheEntry[offset] = .init(offset: offset, stringValue: symbol.name)
4142
}
4243

4344
for exportedSymbol in machO.exportedSymbols {
4445
if var offset = exportedSymbol.offset {
46+
cacheEntry[offset] = .init(offset: offset, stringValue: exportedSymbol.name)
4547
offset += machO.startOffset
4648
cacheEntry[offset] = .init(offset: offset, stringValue: exportedSymbol.name)
4749
}
@@ -78,15 +80,19 @@ class MachOSymbolCache {
7880

7981
func symbol(for offset: Int, in machOImage: MachOImage) -> MachOSymbol? {
8082
let identifier = CacheIdentifier.image(machOImage.ptr)
81-
return symbol(for: offset, with: identifier)
83+
return symbol(for: offset, with: identifier, in: machOImage)
8284
}
8385

8486
func symbol(for offset: Int, in machOFile: MachOFile) -> MachOSymbol? {
8587
let identifier = CacheIdentifier.file(machOFile.imagePath)
86-
return symbol(for: offset, with: identifier)
88+
return symbol(for: offset, with: identifier, in: machOFile)
8789
}
8890

89-
private func symbol(for offset: Int, with identifier: CacheIdentifier) -> MachOSymbol? {
90-
return entryByIdentifier[identifier]?[offset]
91+
private func symbol<MachO: MachORepresentableWithCache>(for offset: Int, with identifier: CacheIdentifier, in machO: MachO) -> MachOSymbol? {
92+
if let symbol = entryByIdentifier[identifier, default: [:]][offset] {
93+
return symbol
94+
} else {
95+
return nil
96+
}
9197
}
9298
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
3+
func + <Element>(lhs: [Element]?, rhs: [Element]?) -> [Element] {
4+
(lhs ?? []) + (rhs ?? [])
5+
}
6+
7+
func + <Element>(lhs: [Element], rhs: [Element]?) -> [Element] {
8+
lhs + (rhs ?? [])
9+
}
10+
11+
func + <Element>(lhs: [Element]?, rhs: [Element]) -> [Element] {
12+
(lhs ?? []) + rhs
13+
}

Sources/MachOSwiftSection/Extensions/Data+.swift

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)