|
| 1 | +// |
| 2 | +// MachOImage+ExportTrie.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by p-x9 on 2023/12/04. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +extension MachOImage { |
| 12 | + public struct ExportTrie: Sequence { |
| 13 | + public typealias Wrapped = MemoryTrieTree<ExportTrieNodeContent> |
| 14 | + |
| 15 | + private let wrapped: MemoryTrieTree<ExportTrieNodeContent> |
| 16 | + let ldVersion: Version? |
| 17 | + |
| 18 | + var isPreDyld_1008: Bool { |
| 19 | + if let ldVersion { |
| 20 | + // Initial version of ld-prime |
| 21 | + // Xcode 15.0 beta 1 (15A5160n) |
| 22 | + return ldVersion < .init(major: 1008, minor: 7, patch: 0) |
| 23 | + } |
| 24 | + return false // fallback |
| 25 | + } |
| 26 | + |
| 27 | + public var basePointer: UnsafeRawPointer { |
| 28 | + wrapped.basePointer |
| 29 | + } |
| 30 | + public var exportSize: Int { |
| 31 | + wrapped.size |
| 32 | + } |
| 33 | + |
| 34 | + init( |
| 35 | + wrapped: MemoryTrieTree<ExportTrieNodeContent>, |
| 36 | + ldVersion: Version? |
| 37 | + ) { |
| 38 | + self.wrapped = wrapped |
| 39 | + self.ldVersion = ldVersion |
| 40 | + } |
| 41 | + |
| 42 | + public func makeIterator() -> Iterator { |
| 43 | + .init( |
| 44 | + wrapped: wrapped.makeIterator(), |
| 45 | + isPreDyld_1008: isPreDyld_1008 |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +extension MachOImage.ExportTrie { |
| 52 | + /// All exported symbols from the trie tree |
| 53 | + public var exportedSymbols: [ExportedSymbol] { |
| 54 | + wrapped.exportedSymbols |
| 55 | + } |
| 56 | + |
| 57 | + /// Elements of each of the nodes that make up the trie tree |
| 58 | + /// |
| 59 | + /// It is obtained by traversing the nodes of the trie tree.It is obtained by traversing a trie tree. |
| 60 | + /// Slower than using `ExportTrie` iterator, but compatible with all Linker(ld) versions |
| 61 | + public var entries: [ExportTrieEntry] { |
| 62 | + wrapped.entries |
| 63 | + } |
| 64 | + |
| 65 | + /// Search the trie tree by symbol name to get the expoted symbol |
| 66 | + /// - Parameter key: symbol name |
| 67 | + /// - Returns: If found, retruns exported symbol |
| 68 | + public func search(for key: String) -> ExportedSymbol? { |
| 69 | + wrapped.search(for: key) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +extension MachOImage.ExportTrie { |
| 74 | + public struct Iterator: IteratorProtocol { |
| 75 | + public typealias Element = Wrapped.Element |
| 76 | + |
| 77 | + private var wrapped: Wrapped.Iterator |
| 78 | + let isPreDyld_1008: Bool |
| 79 | + |
| 80 | + @_spi(Support) |
| 81 | + public init(wrapped: Wrapped.Iterator, isPreDyld_1008: Bool) { |
| 82 | + self.wrapped = wrapped |
| 83 | + self.isPreDyld_1008 = isPreDyld_1008 |
| 84 | + } |
| 85 | + |
| 86 | + public mutating func next() -> Element? { |
| 87 | + let isRoot = wrapped.nextOffset == 0 |
| 88 | + |
| 89 | + guard let next = wrapped.next() else { |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + // HACK: for after dyld-1008.7 |
| 94 | + if isRoot && !isPreDyld_1008 { |
| 95 | + // ref: https://github.com/apple-oss-distributions/dyld/blob/main/mach_o/ExportsTrie.cpp#L669-L674 |
| 96 | + // root is allocated the size that `UINT_MAX` can represent |
| 97 | + // 32 / 7 |
| 98 | + wrapped.nextOffset -= next.children |
| 99 | + .map(\.offset.uleb128Size) |
| 100 | + .reduce(0, +) |
| 101 | + wrapped.nextOffset += 5 * next.children.count |
| 102 | + } |
| 103 | + |
| 104 | + return next |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +extension MachOImage.ExportTrie { |
| 110 | + init( |
| 111 | + ptr: UnsafeRawPointer, |
| 112 | + text: SegmentCommand64, |
| 113 | + linkedit: SegmentCommand64, |
| 114 | + info: dyld_info_command, |
| 115 | + ldVersion: Version? |
| 116 | + ) { |
| 117 | + let fileSlide = Int(linkedit.vmaddr) - Int(text.vmaddr) - Int(linkedit.fileoff) |
| 118 | + let ptr = ptr |
| 119 | + .advanced(by: Int(info.export_off)) |
| 120 | + .advanced(by: Int(fileSlide)) |
| 121 | + .assumingMemoryBound(to: UInt8.self) |
| 122 | + |
| 123 | + self.init( |
| 124 | + wrapped: .init(basePointer: ptr, size: Int(info.export_size)), |
| 125 | + ldVersion: ldVersion |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + init( |
| 130 | + ptr: UnsafeRawPointer, |
| 131 | + text: SegmentCommand, |
| 132 | + linkedit: SegmentCommand, |
| 133 | + info: dyld_info_command, |
| 134 | + ldVersion: Version? |
| 135 | + ) { |
| 136 | + let fileSlide = Int(linkedit.vmaddr) - Int(text.vmaddr) - Int(linkedit.fileoff) |
| 137 | + let ptr = ptr |
| 138 | + .advanced(by: Int(info.export_off)) |
| 139 | + .advanced(by: Int(fileSlide)) |
| 140 | + .assumingMemoryBound(to: UInt8.self) |
| 141 | + |
| 142 | + self.init( |
| 143 | + wrapped: .init(basePointer: ptr, size: Int(info.export_size)), |
| 144 | + ldVersion: ldVersion |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + init( |
| 149 | + linkedit: SegmentCommand64, |
| 150 | + export: linkedit_data_command, |
| 151 | + vmaddrSlide: Int, |
| 152 | + ldVersion: Version? |
| 153 | + ) { |
| 154 | + |
| 155 | + let linkeditStart = vmaddrSlide + Int(linkedit.layout.vmaddr - linkedit.layout.fileoff) |
| 156 | + let ptr = UnsafeRawPointer(bitPattern: linkeditStart)! // swiftlint:disable:this force_unwrapping |
| 157 | + .advanced(by: Int(export.dataoff)) |
| 158 | + .assumingMemoryBound(to: UInt8.self) |
| 159 | + |
| 160 | + self.init( |
| 161 | + wrapped: .init(basePointer: ptr, size: Int(export.datasize)), |
| 162 | + ldVersion: ldVersion |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + init( |
| 167 | + linkedit: SegmentCommand, |
| 168 | + export: linkedit_data_command, |
| 169 | + vmaddrSlide: Int, |
| 170 | + ldVersion: Version? |
| 171 | + ) { |
| 172 | + let linkeditStart = vmaddrSlide + Int(linkedit.layout.vmaddr - linkedit.layout.fileoff) |
| 173 | + let ptr = UnsafeRawPointer(bitPattern: linkeditStart)! // swiftlint:disable:this force_unwrapping |
| 174 | + .advanced(by: Int(export.dataoff)) |
| 175 | + .assumingMemoryBound(to: UInt8.self) |
| 176 | + |
| 177 | + self.init( |
| 178 | + wrapped: .init(basePointer: ptr, size: Int(export.datasize)), |
| 179 | + ldVersion: ldVersion |
| 180 | + ) |
| 181 | + } |
| 182 | +} |
0 commit comments