-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from p-x9/feature/support-old-prebuild-loader
[dyld cache] Support latest and older version of prebuilt loader
- Loading branch information
Showing
7 changed files
with
336 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Sources/MachOKit/Model/DyldCache/Loader/PrebuiltLoaderProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// PrebuiltLoaderProtocol.swift | ||
// MachOKit | ||
// | ||
// Created by p-x9 on 2024/11/09 | ||
// | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
public protocol PrebuiltLoaderProtocol { | ||
/// Address where this loader is located. | ||
/// | ||
/// Slides after loading are not included. | ||
var address: Int { get } | ||
|
||
/// magic of loader starts | ||
var magic: String? { get } | ||
/// PrebuiltLoader vs JustInTimeLoader | ||
var isPrebuilt: Bool { get } | ||
var ref: LoaderRef { get } | ||
|
||
/// path for target mach-o image | ||
/// - Parameter cache: DyldCache to which `self` belongs | ||
/// - Returns: path name | ||
func path(in cache: DyldCache) -> String? | ||
/// loader reference list of target 's dependencies | ||
/// - Parameter cache: DyldCache to which `self` belongs | ||
/// - Returns: sequence of loader reference | ||
func dependentLoaderRefs(in cache: DyldCache) -> DataSequence<LoaderRef>? | ||
|
||
/// path for target mach-o image | ||
/// - Parameter cache: DyldCacheLoaded to which `self` belongs | ||
/// - Returns: path name | ||
func path(in cache: DyldCacheLoaded) -> String? | ||
/// loader reference list of target 's dependencies | ||
/// - Parameter cache: DyldCacheLoaded to which `self` belongs | ||
/// - Returns: sequence of loader reference | ||
func dependentLoaderRefs(in cache: DyldCacheLoaded) -> MemorySequence<LoaderRef>? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Sources/MachOKit/Model/DyldCache/Loader/PrebuiltLoader_Pre1165_3.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// PrebuiltLoader_Pre1165_3.swift | ||
// MachOKit | ||
// | ||
// Created by p-x9 on 2024/11/09 | ||
// | ||
// | ||
|
||
public struct PrebuiltLoader_Pre1165_3: LayoutWrapper, PrebuiltLoaderProtocol { | ||
public typealias Layout = prebuilt_loader_pre1165_3 | ||
|
||
public var layout: Layout | ||
public var address: Int | ||
} | ||
|
||
extension PrebuiltLoader_Pre1165_3 { | ||
// always true | ||
public var isPrebuilt: Bool { | ||
layout.loader.isPrebuilt != 0 | ||
} | ||
|
||
public var ref: LoaderRef { | ||
.init(layout: layout.loader.ref) | ||
} | ||
} | ||
|
||
extension PrebuiltLoader_Pre1165_3 { | ||
public func path(in cache: DyldCache) -> String? { | ||
guard let offset = cache.fileOffset( | ||
of: numericCast(address) + numericCast(layout.pathOffset) | ||
) else { return nil } | ||
return cache.fileHandle.readString(offset: offset) | ||
} | ||
|
||
public func dependentLoaderRefs(in cache: DyldCache) -> DataSequence<LoaderRef>? { | ||
guard layout.dependentLoaderRefsArrayOffset != 0, | ||
let offset = cache.fileOffset( | ||
of: numericCast(address) + numericCast(layout.dependentLoaderRefsArrayOffset) | ||
) else { | ||
return nil | ||
} | ||
return cache.fileHandle.readDataSequence( | ||
offset: offset, | ||
numberOfElements: numericCast(layout.depCount) | ||
) | ||
} | ||
} | ||
|
||
extension PrebuiltLoader_Pre1165_3 { | ||
public func path(in cache: DyldCacheLoaded) -> String? { | ||
// swiftlint:disable:previous unused_parameter | ||
guard let baseAddress = UnsafeRawPointer(bitPattern: address) else { | ||
return nil | ||
} | ||
return String( | ||
cString: baseAddress | ||
.advanced(by: numericCast(layout.pathOffset)) | ||
.assumingMemoryBound(to: CChar.self) | ||
) | ||
} | ||
|
||
public func dependentLoaderRefs(in cache: DyldCacheLoaded) -> MemorySequence<LoaderRef>? { | ||
// swiftlint:disable:previous unused_parameter | ||
guard layout.dependentLoaderRefsArrayOffset != 0, | ||
let baseAddress = UnsafeRawPointer(bitPattern: address) else { | ||
return nil | ||
} | ||
return .init( | ||
basePointer: baseAddress | ||
.advanced(by: numericCast(layout.dependentLoaderRefsArrayOffset)) | ||
.assumingMemoryBound(to: LoaderRef.self), | ||
numberOfElements: numericCast(layout.depCount) | ||
) | ||
} | ||
} | ||
|
||
extension PrebuiltLoader_Pre1165_3 { | ||
// [dyld implementation](https://github.com/apple-oss-distributions/dyld/blob/65bbeed63cec73f313b1d636e63f243964725a9d/dyld/Loader.h#L317) | ||
public var magic: String? { | ||
withUnsafeBytes(of: layout.loader.magic.bigEndian, { | ||
let cString = $0.map({ CChar($0)} ) + [0] | ||
return String( | ||
cString: cString, | ||
encoding: .utf8 | ||
) | ||
}) | ||
} | ||
} |
Oops, something went wrong.