Skip to content

Commit

Permalink
Merge pull request #8 from p-x9/feature/rename-machO-machOImage
Browse files Browse the repository at this point in the history
Rename `MachO` to `MachOImage`
  • Loading branch information
p-x9 authored Dec 25, 2023
2 parents bba0138 + b597e67 commit 9857036
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 79 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ It can be initialized by using the Mach-O Header pointer obtained by `_dyld_get_

```swift
guard let mh = _dyld_get_image_header(0) else { return }
let machO = MachO(ptr: mh)
let machO = MachOImage(ptr: mh)
```

Alternatively, it can be initialized using the name.

```swift
// /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
guard let machO = MachO(name: "Foundation") else { return }
guard let machO = MachOImage(name: "Foundation") else { return }
```

### Load from file
Expand Down
2 changes: 1 addition & 1 deletion Sources/MachOKit/LoadCommand/LinkerOptionCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension LinkerOptionCommand {
.advanced(by: offset)
.advanced(by: layoutSize)
.assumingMemoryBound(to: CChar.self)
let strings = MachO.Strings(
let strings = MachOImage.Strings(
basePointer: ptr,
tableSize: Int(layout.cmdsize) - layoutSize
).map(\.string)
Expand Down
10 changes: 5 additions & 5 deletions Sources/MachOKit/LoadCommand/Model/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public protocol SectionProtocol: LayoutWrapper {
var flags: SectionFlags { get }

/// returns nil except when type is `cstring_literals
func strings(ptr: UnsafeRawPointer) -> MachO.Strings?
func strings(ptr: UnsafeRawPointer) -> MachOImage.Strings?

/// returns nil except when type is `cstring_literals
func strings(in machO: MachOFile) -> MachOFile.Strings?
Expand Down Expand Up @@ -65,13 +65,13 @@ extension SectionProtocol {
ptr: UnsafeRawPointer,
sectionOffset: UInt32,
sectionSize: UInt64
) -> MachO.Strings? {
) -> MachOImage.Strings? {
guard flags.type == .cstring_literals else { return nil }
let basePointer = ptr
.advanced(by: numericCast(sectionOffset))
.assumingMemoryBound(to: CChar.self)
let tableSize = Int(sectionSize)
return MachO.Strings(
return MachOImage.Strings(
basePointer: basePointer,
tableSize: tableSize
)
Expand All @@ -97,7 +97,7 @@ extension SectionProtocol {
}

extension Section {
public func strings(ptr: UnsafeRawPointer) -> MachO.Strings? {
public func strings(ptr: UnsafeRawPointer) -> MachOImage.Strings? {
_strings(ptr: ptr, sectionOffset: layout.offset, sectionSize: UInt64(layout.size))
}

Expand All @@ -107,7 +107,7 @@ extension Section {
}

extension Section64 {
public func strings(ptr: UnsafeRawPointer) -> MachO.Strings? {
public func strings(ptr: UnsafeRawPointer) -> MachOImage.Strings? {
_strings(ptr: ptr, sectionOffset: layout.offset, sectionSize: layout.size)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

extension MachO {
extension MachOImage {
public struct BindOperations: Sequence {
public let basePointer: UnsafePointer<UInt8>
public let bindSize: Int
Expand All @@ -19,7 +19,7 @@ extension MachO {
}
}

extension MachO.BindOperations {
extension MachOImage.BindOperations {
init(
ptr: UnsafeRawPointer,
text: SegmentCommand64,
Expand Down Expand Up @@ -53,7 +53,7 @@ extension MachO.BindOperations {
}
}

extension MachO.BindOperations {
extension MachOImage.BindOperations {
public struct Iterator: IteratorProtocol {
public typealias Element = BindOperation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MachO+ExportTrieEntries.swift
// MachOImage+ExportTrieEntries.swift
//
//
// Created by p-x9 on 2023/12/04.
Expand All @@ -8,7 +8,7 @@

import Foundation

extension MachO {
extension MachOImage {
public struct ExportTrieEntries: Sequence {
public let basePointer: UnsafePointer<UInt8>
public let exportSize: Int
Expand All @@ -19,7 +19,7 @@ extension MachO {
}
}

extension MachO.ExportTrieEntries {
extension MachOImage.ExportTrieEntries {
init(
ptr: UnsafeRawPointer,
text: SegmentCommand64,
Expand Down Expand Up @@ -78,7 +78,7 @@ extension MachO.ExportTrieEntries {
}
}

extension MachO.ExportTrieEntries {
extension MachOImage.ExportTrieEntries {
public struct Iterator: IteratorProtocol {
public typealias Element = ExportTrieEntry

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MachO+LoadCommands.swift
// MachOImage+LoadCommands.swift
//
//
// Created by p-x9 on 2023/11/28.
Expand All @@ -8,7 +8,7 @@

import Foundation

extension MachO {
extension MachOImage {
public struct LoadCommands: Sequence {
public let start: UnsafeRawPointer
public let numberOfCommands: Int
Expand All @@ -22,7 +22,7 @@ extension MachO {
}
}

extension MachO.LoadCommands {
extension MachOImage.LoadCommands {
public struct Iterator: IteratorProtocol {
public typealias Element = LoadCommand

Expand Down Expand Up @@ -57,4 +57,4 @@ extension MachO.LoadCommands {
}
}

extension MachO.LoadCommands: LoadCommandsProtocol {}
extension MachOImage.LoadCommands: LoadCommandsProtocol {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

extension MachO {
extension MachOImage {
public struct RebaseOperations: Sequence {
public let basePointer: UnsafePointer<UInt8>
public let rebaseSize: Int
Expand All @@ -19,7 +19,7 @@ extension MachO {
}
}

extension MachO.RebaseOperations {
extension MachOImage.RebaseOperations {
init(
ptr: UnsafeRawPointer,
text: SegmentCommand64,
Expand Down Expand Up @@ -51,7 +51,7 @@ extension MachO.RebaseOperations {
}
}

extension MachO.RebaseOperations {
extension MachOImage.RebaseOperations {
public struct Iterator: IteratorProtocol {
public typealias Element = RebaseOperation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

extension MachO {
extension MachOImage {
public struct Strings: Sequence {
public let basePointer: UnsafePointer<CChar>
public let tableSize: Int
Expand All @@ -22,7 +22,7 @@ extension MachO {
}
}

extension MachO.Strings {
extension MachOImage.Strings {
init(
ptr: UnsafeRawPointer,
text: SegmentCommand64,
Expand Down Expand Up @@ -52,7 +52,7 @@ extension MachO.Strings {
}
}

extension MachO.Strings {
extension MachOImage.Strings {
public struct Iterator: IteratorProtocol {
public typealias Element = StringTableEntry

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MachO+Symbols..swift
// MachOImage+Symbols..swift
//
//
// Created by p-x9 on 2023/11/28.
Expand All @@ -8,7 +8,7 @@

import Foundation

extension MachO {
extension MachOImage {
public struct Symbol: SymbolProtocol {
public let nameC: UnsafePointer<CChar>

Expand All @@ -21,14 +21,14 @@ extension MachO {
}
}

extension MachO.Symbol {
extension MachOImage.Symbol {
public var name: String {
.init(cString: nameC)
}
}

// https://stackoverflow.com/questions/20481058/find-pathname-from-dlopen-handle-on-osx
extension MachO {
extension MachOImage {
public struct Symbols64: Sequence {
public let ptr: UnsafeRawPointer
public let text: SegmentCommand64
Expand Down Expand Up @@ -68,7 +68,7 @@ extension MachO {
}
}

extension MachO {
extension MachOImage {
public struct Symbols: Sequence {
public let ptr: UnsafeRawPointer
public let text: SegmentCommand
Expand Down Expand Up @@ -108,9 +108,9 @@ extension MachO {
}
}

extension MachO.Symbols64 {
extension MachOImage.Symbols64 {
public struct Iterator: IteratorProtocol {
public typealias Element = MachO.Symbol
public typealias Element = MachOImage.Symbol

public let stringBase: UnsafePointer<CChar>
public let addressStart: Int
Expand Down Expand Up @@ -145,7 +145,7 @@ extension MachO.Symbols64 {
.advanced(by: numericCast(symbol.n_un.n_strx))
let address = addressStart + numericCast(symbol.n_value)

return MachO.Symbol(
return MachOImage.Symbol(
nameC: str,
offset: address,
nlist: Nlist64(layout: symbol)
Expand All @@ -154,9 +154,9 @@ extension MachO.Symbols64 {
}
}

extension MachO.Symbols {
extension MachOImage.Symbols {
public struct Iterator: IteratorProtocol {
public typealias Element = MachO.Symbol
public typealias Element = MachOImage.Symbol

public let stringBase: UnsafePointer<CChar>
public let addressStart: Int
Expand Down Expand Up @@ -191,7 +191,7 @@ extension MachO.Symbols {
.advanced(by: Int(symbol.n_un.n_strx))
let address = addressStart + numericCast(symbol.n_value)

return MachO.Symbol(
return MachOImage.Symbol(
nameC: str,
offset: address,
nlist: Nlist(layout: symbol)
Expand Down
Loading

0 comments on commit 9857036

Please sign in to comment.