| ... | ... | @@ -34,6 +34,8 @@ pub fn isDylib(path: []const u8, fat_arch: ?fat.Arch) !bool { |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | pub fn deinit(self: *Dylib, allocator: Allocator) void { |
| 37 | allocator.free(self.data); |
| 38 | allocator.free(self.path); |
| 37 | 39 | self.exports.deinit(allocator); |
| 38 | 40 | self.strtab.deinit(allocator); |
| 39 | 41 | if (self.id) |*id| id.deinit(allocator); |
| ... | ... | @@ -49,7 +51,7 @@ pub fn parse(self: *Dylib, macho_file: *MachO) !void { |
| 49 | 51 | const tracy = trace(@src()); |
| 50 | 52 | defer tracy.end(); |
| 51 | 53 | |
| 52 | | const gpa = macho_file.base.allocator; |
| 54 | const gpa = macho_file.base.comp.gpa; |
| 53 | 55 | var stream = std.io.fixedBufferStream(self.data); |
| 54 | 56 | const reader = stream.reader(); |
| 55 | 57 | |
| ... | ... | @@ -57,9 +59,22 @@ pub fn parse(self: *Dylib, macho_file: *MachO) !void { |
| 57 | 59 | |
| 58 | 60 | self.header = try reader.readStruct(macho.mach_header_64); |
| 59 | 61 | |
| 62 | const this_cpu_arch: std.Target.Cpu.Arch = switch (self.header.?.cputype) { |
| 63 | macho.CPU_TYPE_ARM64 => .aarch64, |
| 64 | macho.CPU_TYPE_X86_64 => .x86_64, |
| 65 | else => |x| { |
| 66 | try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x}); |
| 67 | return error.InvalidCpuArch; |
| 68 | }, |
| 69 | }; |
| 70 | if (macho_file.getTarget().cpu.arch != this_cpu_arch) { |
| 71 | try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)}); |
| 72 | return error.InvalidCpuArch; |
| 73 | } |
| 74 | |
| 60 | 75 | const lc_id = self.getLoadCommand(.ID_DYLIB) orelse { |
| 61 | | macho_file.base.fatal("{s}: missing LC_ID_DYLIB load command", .{self.path}); |
| 62 | | return error.ParseFailed; |
| 76 | try macho_file.reportParseError2(self.index, "missing LC_ID_DYLIB load command", .{}); |
| 77 | return error.MalformedDylib; |
| 63 | 78 | }; |
| 64 | 79 | self.id = try Id.fromLoadCommand(gpa, lc_id.cast(macho.dylib_command).?, lc_id.getDylibPathName()); |
| 65 | 80 | |
| ... | ... | @@ -90,6 +105,23 @@ pub fn parse(self: *Dylib, macho_file: *MachO) !void { |
| 90 | 105 | }; |
| 91 | 106 | |
| 92 | 107 | self.initPlatform(); |
| 108 | |
| 109 | if (self.platform) |platform| { |
| 110 | if (!macho_file.platform.eqlTarget(platform)) { |
| 111 | try macho_file.reportParseError2(self.index, "invalid platform: {}", .{ |
| 112 | platform.fmtTarget(macho_file.getTarget().cpu.arch), |
| 113 | }); |
| 114 | return error.InvalidTarget; |
| 115 | } |
| 116 | if (macho_file.platform.version.order(platform.version) == .lt) { |
| 117 | try macho_file.reportParseError2(self.index, "object file built for newer platform: {}: {} < {}", .{ |
| 118 | macho_file.platform.fmtTarget(macho_file.getTarget().cpu.arch), |
| 119 | macho_file.platform.version, |
| 120 | platform.version, |
| 121 | }); |
| 122 | return error.InvalidTarget; |
| 123 | } |
| 124 | } |
| 93 | 125 | } |
| 94 | 126 | |
| 95 | 127 | const TrieIterator = struct { |
| ... | ... | @@ -187,7 +219,7 @@ fn parseTrieNode( |
| 187 | 219 | fn parseTrie(self: *Dylib, data: []const u8, macho_file: *MachO) !void { |
| 188 | 220 | const tracy = trace(@src()); |
| 189 | 221 | defer tracy.end(); |
| 190 | | const gpa = macho_file.base.allocator; |
| 222 | const gpa = macho_file.base.comp.gpa; |
| 191 | 223 | var arena = std.heap.ArenaAllocator.init(gpa); |
| 192 | 224 | defer arena.deinit(); |
| 193 | 225 | |
| ... | ... | @@ -204,7 +236,7 @@ pub fn parseTbd( |
| 204 | 236 | ) !void { |
| 205 | 237 | const tracy = trace(@src()); |
| 206 | 238 | defer tracy.end(); |
| 207 | | const gpa = macho_file.base.allocator; |
| 239 | const gpa = macho_file.base.comp.gpa; |
| 208 | 240 | |
| 209 | 241 | log.debug("parsing dylib from stub", .{}); |
| 210 | 242 | |
| ... | ... | @@ -435,7 +467,7 @@ fn addObjCExport( |
| 435 | 467 | } |
| 436 | 468 | |
| 437 | 469 | pub fn initSymbols(self: *Dylib, macho_file: *MachO) !void { |
| 438 | | const gpa = macho_file.base.allocator; |
| 470 | const gpa = macho_file.base.comp.gpa; |
| 439 | 471 | |
| 440 | 472 | try self.symbols.ensureTotalCapacityPrecise(gpa, self.exports.items(.name).len); |
| 441 | 473 | |
| ... | ... | @@ -459,7 +491,7 @@ fn initPlatform(self: *Dylib) void { |
| 459 | 491 | .VERSION_MIN_IPHONEOS, |
| 460 | 492 | .VERSION_MIN_TVOS, |
| 461 | 493 | .VERSION_MIN_WATCHOS, |
| 462 | | => break MachO.Options.Platform.fromLoadCommand(cmd), |
| 494 | => break MachO.Platform.fromLoadCommand(cmd), |
| 463 | 495 | else => {}, |
| 464 | 496 | } |
| 465 | 497 | } else null; |