Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/p-x9/MachOKit.git", from: "0.30.0"),
.package(url: "https://github.com/p-x9/MachOKit.git", from: "0.34.0"),
.package(url: "https://github.com/p-x9/swift-objc-dump.git", from: "0.7.0")
],
targets: [
Expand Down
11 changes: 11 additions & 0 deletions Sources/MachOObjCSection/Extension/DyldCache+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,14 @@ extension DyldCache {
return nil
}
}

extension DyldCache {
func machO(containing unslidAddress: UInt64) -> MachOFile? {
for machO in self.machOFiles() {
if machO.contains(unslidAddress: unslidAddress) {
return machO
}
}
return nil
}
}
20 changes: 20 additions & 0 deletions Sources/MachOObjCSection/Extension/DyldCacheLoaded+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,23 @@ extension DyldCacheLoaded {
return nil
}
}

extension DyldCacheLoaded {
func machO(containing ptr: UnsafeRawPointer) -> MachOImage? {
for machO in machOImages() {
if machO.contains(ptr: ptr) {
return machO
}
}
return nil
}

func machO(containing unslidAddress: UInt64) -> MachOImage? {
for machO in self.machOImages() {
if machO.contains(unslidAddress: unslidAddress) {
return machO
}
}
return nil
}
}
15 changes: 12 additions & 3 deletions Sources/MachOObjCSection/Model/Protocol/ObjCProtocolList32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public struct ObjCProtocolList32: ObjCProtocolListProtocol {
extension ObjCProtocolList32 {
public func protocols(
in machO: MachOImage
) -> [ObjCProtocol]? {
) -> [(MachOImage, ObjCProtocol)]? {
_readProtocols(in: machO, pointerType: UInt32.self)
}

public func protocols(
in machO: MachOFile
) -> [ObjCProtocol]? {
) -> [(MachOFile, ObjCProtocol)]? {
guard !isListOfLists else {
assertionFailure()
return nil
Expand Down Expand Up @@ -63,23 +63,32 @@ extension ObjCProtocolList32 {
let offset = $0 + numericCast(headerStartOffset)
var resolvedOffset = offset

var targetMachO = machO

var fileHandle = machO.fileHandle

if let (_cache, _offset) = machO.cacheAndFileOffset(
fromStart: offset
) {
resolvedOffset = _offset
fileHandle = _cache.fileHandle

let unslidAddress = offset + _cache.mainCacheHeader.sharedRegionStart
if !targetMachO.contains(unslidAddress: unslidAddress),
let machO = _cache.machO(containing: unslidAddress) {
targetMachO = machO
}
}

let layout: ObjCProtocol32.Layout = fileHandle.read(
offset: numericCast(resolvedOffset),
swapHandler: { _ in }
)
return .init(
let `protocol`: ObjCProtocol = .init(
layout: layout,
offset: numericCast(offset) - machO.headerStartOffset
)
return (targetMachO, `protocol`)
}
}
}
15 changes: 12 additions & 3 deletions Sources/MachOObjCSection/Model/Protocol/ObjCProtocolList64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public struct ObjCProtocolList64: ObjCProtocolListProtocol {
extension ObjCProtocolList64 {
public func protocols(
in machO: MachOImage
) -> [ObjCProtocol]? {
) -> [(MachOImage, ObjCProtocol)]? {
_readProtocols(in: machO, pointerType: UInt64.self)
}

public func protocols(
in machO: MachOFile
) -> [ObjCProtocol]? {
) -> [(MachOFile, ObjCProtocol)]? {
guard !isListOfLists else {
assertionFailure()
return nil
Expand Down Expand Up @@ -63,23 +63,32 @@ extension ObjCProtocolList64 {
let offset = machO.fileOffset(of: $0) + numericCast(headerStartOffset)
var resolvedOffset = offset

var targetMachO = machO

var fileHandle = machO.fileHandle

if let (_cache, _offset) = machO.cacheAndFileOffset(
fromStart: offset
) {
resolvedOffset = _offset
fileHandle = _cache.fileHandle

let unslidAddress = offset + _cache.mainCacheHeader.sharedRegionStart
if !targetMachO.contains(unslidAddress: unslidAddress),
let machO = _cache.machO(containing: unslidAddress) {
targetMachO = machO
}
}

let layout: ObjCProtocol64.Layout = fileHandle.read(
offset: numericCast(resolvedOffset),
swapHandler: { _ in }
)
return .init(
let `protocol`: ObjCProtocol = .init(
layout: layout,
offset: numericCast(offset) - machO.headerStartOffset
)
return (targetMachO, `protocol`)
}
}
}
Loading