Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- vim: ft=cpp et ts=4 sw=4: * * Copyright (c) 2023 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ enum MachOError: Error { case truncatedRead case badMagic(UInt32) } // TODO: Convert everything in this file from UnsafeRawBufferPointer to Span // Only implement what we need until then // enum MachO { case notMachO case thinMachO(MachO.File) case universalMachO(MachO.UniversalFile) init(_ buffer: UnsafeRawBufferPointer) throws(MachOError) { var slice = buffer[0..<buffer.count] guard let magic: UInt32 = try? slice.read(as: UInt32.self) else { throw .truncatedRead } switch magic { case 0xFEED_FACE, 0xFEED_FACF, 0xCEFA_EDFE, 0xCFFA_EDFE: self = .thinMachO(try MachO.File(buffer)) case 0xCAFE_BABE, 0xCAFE_BABF, 0xBEBA_FECA, 0xBFBA_FECA: self = .universalMachO(try MachO.UniversalFile(buffer)) default: self = .notMachO } } } extension MachO { struct File { fileprivate let buffer: UnsafeRawBufferPointer fileprivate let endian: Endian fileprivate let is64bit: Bool init(_ buffer: UnsafeRawBufferPointer) throws(MachOError) { var slice = buffer[0..<buffer.count] guard let magic: UInt32 = try? slice.read(as: UInt32.self) else { throw .truncatedRead } switch magic { case 0xFEED_FACE: endian = .native is64bit = false case 0xFEED_FACF: endian = .native is64bit = true case 0xCEFA_EDFE: endian = .reverse is64bit = false case 0xCFFA_EDFE: endian = .reverse is64bit = true default: throw .badMagic(magic) } self.buffer = buffer } var uuid: UUID? { for loadCommand in loadCommands { switch loadCommand { case .uuid(let uuid): return uuid default: continue } } return nil } var loadCommands: LoadCommandSequence { return LoadCommandSequence(self) } fileprivate struct Header { let buffer: UnsafeRawBufferPointer let endian: Endian init(_ macho: MachO.File) { self.buffer = macho.buffer self.endian = macho.endian } var magic: UInt32 { return buffer.load(fromByteOffset:0, as:UInt32.self).from(endian:endian) } var cputype: cpu_type_t { return buffer.load(fromByteOffset:4, as:Int32.self).from(endian:endian) } var cpusubtype: cpu_subtype_t { return buffer.load(fromByteOffset:8, as:Int32.self).from(endian:endian) } var filetype: UInt32 { return buffer.load(fromByteOffset:12, as:UInt32.self).from(endian:endian) } var ncmds: UInt32 { return buffer.load(fromByteOffset:16, as:UInt32.self).from(endian:endian) } var sizeofcmds: UInt32 { return buffer.load(fromByteOffset:20, as:UInt32.self).from(endian:endian) } var flags: UInt32 { return buffer.load(fromByteOffset:24, as:UInt32.self).from(endian:endian) } } } } fileprivate let LC_UUID: UInt32 = 0x0000001b extension MachO { enum LoadCommand { case uuid(UUID) case unknown(UInt32) } struct LoadCommandSequence: Sequence { fileprivate var macho: MachO.File fileprivate init(_ macho: MachO.File) { self.macho = macho } func makeIterator() -> LoadCommandIterator { return LoadCommandIterator(macho) } } struct LoadCommandIterator: IteratorProtocol { fileprivate var slice: Slice<UnsafeRawBufferPointer> fileprivate let endian: Endian init(_ macho: MachO.File) { let header = MachO.File.Header(macho) let lowerBound = macho.buffer.indices.lowerBound self.slice = macho.buffer[lowerBound..<lowerBound+Int(header.sizeofcmds)] if macho.is64bit { self.slice = self.slice.dropFirst(32) } else { self.slice = self.slice.dropFirst(28) } self.endian = macho.endian } mutating func next() -> LoadCommand? { guard let commandType = try? slice.read(endian:endian, as: UInt32.self), let commandSize = try? slice.read(endian:endian, as: UInt32.self) else { return nil } switch commandType { case LC_UUID: guard let uuid = try? slice.readUuid() else { return nil } let remainingBytes = Int(commandSize) - 24 // We already read the type, size, and uuid data slice = slice.dropFirst(remainingBytes) // Skip to the next comand return .uuid(uuid) default: let remainingBytes = Int(commandSize) - 8 // We already read the type and size slice = slice.dropFirst(remainingBytes) // Skip to the next comand return .unknown(commandType) } } } } extension MachO { struct UniversalFile { private let buffer: UnsafeRawBufferPointer private let is64bit: Bool init(_ buffer: UnsafeRawBufferPointer) throws(MachOError) { var slice = buffer[0..<buffer.count] guard let magic: UInt32 = try? slice.read(endian:.big, as: UInt32.self) else { throw .truncatedRead } switch magic { case 0xCAFE_BABE: is64bit = false case 0xCAFE_BABF: is64bit = true default: throw .badMagic(magic) } self.buffer = buffer } var slices: SliceSequence { return SliceSequence(self) } var sliceInfos: SliceInfoSequence { return SliceInfoSequence(self) } struct SliceInfo { private let slice: Slice<UnsafeRawBufferPointer> private let is64Bit: Bool init(slice: Slice<UnsafeRawBufferPointer>, is64Bit: Bool) { self.slice = slice self.is64Bit = is64Bit } // FIXME: Move these to enums later var cpuType: cpu_type_t { return slice.load(fromByteOffset:0, as:Int32.self).from(endian:.big) } var cpuSubType: cpu_subtype_t { return slice.load(fromByteOffset:4, as:Int32.self).from(endian:.big) } var offset: Int { if is64Bit { return Int(slice.load(fromByteOffset:8, as:UInt64.self).from(endian:.big)) } else { return Int(slice.load(fromByteOffset:8, as:UInt32.self).from(endian:.big)) } } var size: Int { if is64Bit { return Int(slice.load(fromByteOffset:16, as:UInt64.self).from(endian:.big)) } else { return Int(slice.load(fromByteOffset:12, as:UInt32.self).from(endian:.big)) } } } struct SliceInfoSequence: Sequence { fileprivate let universalFile: UniversalFile fileprivate init(_ universalFile: UniversalFile) { self.universalFile = universalFile } func makeIterator() -> SliceInfoIterator { return SliceInfoIterator(universalFile) } } struct SliceInfoIterator: IteratorProtocol { private var slice: Slice<UnsafeRawBufferPointer> private var index: Int = 0 private let header: MachO.UniversalFile.Header private let is64Bit: Bool init(_ universalFile: UniversalFile) { self.header = MachO.UniversalFile.Header(universalFile) self.slice = universalFile.buffer[universalFile.buffer.indices] self.is64Bit = universalFile.is64bit } mutating func next() -> SliceInfo? { guard index < header.nfat_arch else { return nil } var result: SliceInfo if is64Bit { let lowerBound = slice.indices.lowerBound + 8 + (32 * index) // Header + index * size of 63 bit fat record result = SliceInfo(slice:slice[lowerBound..<slice.indices.upperBound], is64Bit:true) } else { let lowerBound = slice.indices.lowerBound + 8 + (20 * index) // Header + index * size of 63 bit fat record result = SliceInfo(slice:slice[lowerBound..<slice.indices.upperBound], is64Bit:false) } index += 1 return result } } // This is a bit confusing because we use `slice` as a term of art within MachO, and swift uses // slice to refer to range of collection. Once we have Span most of the uses of swift slices will // go away and this will be cleaerer struct SliceSequence: Sequence { fileprivate let universalFile: UniversalFile fileprivate init(_ universalFile: UniversalFile) { self.universalFile = universalFile } func makeIterator() -> SliceIterator { return SliceIterator(universalFile) } } struct SliceIterator: IteratorProtocol { private let slice: Slice<UnsafeRawBufferPointer> private var sliceInfoIterator: SliceInfoIterator init(_ universalFile: UniversalFile) { self.slice = universalFile.buffer[universalFile.buffer.indices] self.sliceInfoIterator = SliceInfoIterator(universalFile) } mutating func next() -> MachO.File? { guard let sliceInfo = sliceInfoIterator.next() else { return nil } let lowerBound = slice.indices.lowerBound+sliceInfo.offset let sliceSlice = slice[lowerBound..<lowerBound+sliceInfo.size] return try? MachO.File(UnsafeRawBufferPointer(rebasing:sliceSlice)) } } fileprivate struct Header { let buffer: UnsafeRawBufferPointer init(_ universalFile: UniversalFile) { self.buffer = universalFile.buffer } var magic: UInt32 { return buffer.load(as:UInt32.self).from(endian:.big) } var nfat_arch: UInt32 { return buffer.load(fromByteOffset:4, as:UInt32.self).from(endian:.big) } } } } |