| author | |
| committer | |
| log | ace9b3de642ab89dd356d877ee6b46ce88640e1d |
| tree | 7548aa57cb64984780b9f86a569817d75c51afe0 |
| parent | 049ff45430f7d556d1c6947c0bc44f3df67a8b00 |
5 files changed, 46 insertions(+), 55 deletions(-)
src/link/MachO.zig+6-17| ... | ... | @@ -995,7 +995,6 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void { |
| 995 | 995 | } |
| 996 | 996 | |
| 997 | 997 | fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const u8) !void { |
| 998 | const arch = self.base.options.target.cpu.arch; | |
| 999 | 998 | for (files) |file_name| { |
| 1000 | 999 | const full_path = full_path: { |
| 1001 | 1000 | var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | ... | @@ -1004,17 +1003,17 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const |
| 1004 | 1003 | }; |
| 1005 | 1004 | defer self.base.allocator.free(full_path); |
| 1006 | 1005 | |
| 1007 | if (try Object.createAndParseFromPath(self.base.allocator, arch, full_path)) |object| { | |
| 1006 | if (try Object.createAndParseFromPath(self.base.allocator, self.base.options.target, full_path)) |object| { | |
| 1008 | 1007 | try self.objects.append(self.base.allocator, object); |
| 1009 | 1008 | continue; |
| 1010 | 1009 | } |
| 1011 | 1010 | |
| 1012 | if (try Archive.createAndParseFromPath(self.base.allocator, arch, full_path)) |archive| { | |
| 1011 | if (try Archive.createAndParseFromPath(self.base.allocator, self.base.options.target, full_path)) |archive| { | |
| 1013 | 1012 | try self.archives.append(self.base.allocator, archive); |
| 1014 | 1013 | continue; |
| 1015 | 1014 | } |
| 1016 | 1015 | |
| 1017 | if (try Dylib.createAndParseFromPath(self.base.allocator, arch, full_path, .{ | |
| 1016 | if (try Dylib.createAndParseFromPath(self.base.allocator, self.base.options.target, full_path, .{ | |
| 1018 | 1017 | .syslibroot = syslibroot, |
| 1019 | 1018 | })) |dylibs| { |
| 1020 | 1019 | defer self.base.allocator.free(dylibs); |
| ... | ... | @@ -1032,9 +1031,8 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const |
| 1032 | 1031 | } |
| 1033 | 1032 | |
| 1034 | 1033 | fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !void { |
| 1035 | const arch = self.base.options.target.cpu.arch; | |
| 1036 | 1034 | for (libs) |lib| { |
| 1037 | if (try Dylib.createAndParseFromPath(self.base.allocator, arch, lib, .{ | |
| 1035 | if (try Dylib.createAndParseFromPath(self.base.allocator, self.base.options.target, lib, .{ | |
| 1038 | 1036 | .syslibroot = syslibroot, |
| 1039 | 1037 | })) |dylibs| { |
| 1040 | 1038 | defer self.base.allocator.free(dylibs); |
| ... | ... | @@ -1047,7 +1045,7 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !v |
| 1047 | 1045 | continue; |
| 1048 | 1046 | } |
| 1049 | 1047 | |
| 1050 | if (try Archive.createAndParseFromPath(self.base.allocator, arch, lib)) |archive| { | |
| 1048 | if (try Archive.createAndParseFromPath(self.base.allocator, self.base.options.target, lib)) |archive| { | |
| 1051 | 1049 | try self.archives.append(self.base.allocator, archive); |
| 1052 | 1050 | continue; |
| 1053 | 1051 | } |
| ... | ... | @@ -2236,11 +2234,7 @@ fn resolveSymbols(self: *MachO) !void { |
| 2236 | 2234 | |
| 2237 | 2235 | const object_id = @intCast(u16, self.objects.items.len); |
| 2238 | 2236 | const object = try self.objects.addOne(self.base.allocator); |
| 2239 | object.* = try archive.parseObject( | |
| 2240 | self.base.allocator, | |
| 2241 | self.base.options.target.cpu.arch, | |
| 2242 | offsets.items[0], | |
| 2243 | ); | |
| 2237 | object.* = try archive.parseObject(self.base.allocator, self.base.options.target, offsets.items[0]); | |
| 2244 | 2238 | try self.resolveSymbolsInObject(object_id); |
| 2245 | 2239 | |
| 2246 | 2240 | continue :loop; |
| ... | ... | @@ -2885,11 +2879,6 @@ fn flushZld(self: *MachO) !void { |
| 2885 | 2879 | if (self.base.options.target.cpu.arch == .aarch64) { |
| 2886 | 2880 | try self.writeCodeSignature(); |
| 2887 | 2881 | } |
| 2888 | ||
| 2889 | // if (comptime std.Target.current.isDarwin() and std.Target.current.cpu.arch == .aarch64) { | |
| 2890 | // const out_path = self.output.?.path; | |
| 2891 | // try fs.cwd().copyFile(out_path, fs.cwd(), out_path, .{}); | |
| 2892 | // } | |
| 2893 | 2882 | } |
| 2894 | 2883 | |
| 2895 | 2884 | fn writeGotEntries(self: *MachO) !void { |
src/link/MachO/Archive.zig+6-7| ... | ... | @@ -9,7 +9,6 @@ const mem = std.mem; |
| 9 | 9 | const fat = @import("fat.zig"); |
| 10 | 10 | |
| 11 | 11 | const Allocator = mem.Allocator; |
| 12 | const Arch = std.Target.Cpu.Arch; | |
| 13 | 12 | const Object = @import("Object.zig"); |
| 14 | 13 | |
| 15 | 14 | file: fs.File, |
| ... | ... | @@ -104,7 +103,7 @@ pub fn deinit(self: *Archive, allocator: *Allocator) void { |
| 104 | 103 | allocator.free(self.name); |
| 105 | 104 | } |
| 106 | 105 | |
| 107 | pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u8) !?Archive { | |
| 106 | pub fn createAndParseFromPath(allocator: *Allocator, target: std.Target, path: []const u8) !?Archive { | |
| 108 | 107 | const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) { |
| 109 | 108 | error.FileNotFound => return null, |
| 110 | 109 | else => |e| return e, |
| ... | ... | @@ -119,7 +118,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u |
| 119 | 118 | .file = file, |
| 120 | 119 | }; |
| 121 | 120 | |
| 122 | archive.parse(allocator, arch) catch |err| switch (err) { | |
| 121 | archive.parse(allocator, target) catch |err| switch (err) { | |
| 123 | 122 | error.EndOfStream, error.NotArchive => { |
| 124 | 123 | archive.deinit(allocator); |
| 125 | 124 | return null; |
| ... | ... | @@ -130,9 +129,9 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u |
| 130 | 129 | return archive; |
| 131 | 130 | } |
| 132 | 131 | |
| 133 | pub fn parse(self: *Archive, allocator: *Allocator, arch: Arch) !void { | |
| 132 | pub fn parse(self: *Archive, allocator: *Allocator, target: std.Target) !void { | |
| 134 | 133 | const reader = self.file.reader(); |
| 135 | self.library_offset = try fat.getLibraryOffset(reader, arch); | |
| 134 | self.library_offset = try fat.getLibraryOffset(reader, target); | |
| 136 | 135 | try self.file.seekTo(self.library_offset); |
| 137 | 136 | |
| 138 | 137 | const magic = try reader.readBytesNoEof(SARMAG); |
| ... | ... | @@ -215,7 +214,7 @@ fn parseTableOfContents(self: *Archive, allocator: *Allocator, reader: anytype) |
| 215 | 214 | } |
| 216 | 215 | } |
| 217 | 216 | |
| 218 | pub fn parseObject(self: Archive, allocator: *Allocator, arch: Arch, offset: u32) !Object { | |
| 217 | pub fn parseObject(self: Archive, allocator: *Allocator, target: std.Target, offset: u32) !Object { | |
| 219 | 218 | const reader = self.file.reader(); |
| 220 | 219 | try reader.context.seekTo(offset + self.library_offset); |
| 221 | 220 | |
| ... | ... | @@ -244,7 +243,7 @@ pub fn parseObject(self: Archive, allocator: *Allocator, arch: Arch, offset: u32 |
| 244 | 243 | .mtime = try self.header.?.date(), |
| 245 | 244 | }; |
| 246 | 245 | |
| 247 | try object.parse(allocator, arch); | |
| 246 | try object.parse(allocator, target); | |
| 248 | 247 | try reader.context.seekTo(0); |
| 249 | 248 | |
| 250 | 249 | return object; |
src/link/MachO/Dylib.zig+24-18| ... | ... | @@ -12,7 +12,6 @@ const fat = @import("fat.zig"); |
| 12 | 12 | const commands = @import("commands.zig"); |
| 13 | 13 | |
| 14 | 14 | const Allocator = mem.Allocator; |
| 15 | const Arch = std.Target.Cpu.Arch; | |
| 16 | 15 | const LibStub = @import("../tapi.zig").LibStub; |
| 17 | 16 | const LoadCommand = commands.LoadCommand; |
| 18 | 17 | const MachO = @import("../MachO.zig"); |
| ... | ... | @@ -139,11 +138,12 @@ pub const Error = error{ |
| 139 | 138 | pub const CreateOpts = struct { |
| 140 | 139 | syslibroot: ?[]const u8 = null, |
| 141 | 140 | id: ?Id = null, |
| 141 | target: ?std.Target = null, | |
| 142 | 142 | }; |
| 143 | 143 | |
| 144 | 144 | pub fn createAndParseFromPath( |
| 145 | 145 | allocator: *Allocator, |
| 146 | arch: Arch, | |
| 146 | target: std.Target, | |
| 147 | 147 | path: []const u8, |
| 148 | 148 | opts: CreateOpts, |
| 149 | 149 | ) Error!?[]Dylib { |
| ... | ... | @@ -161,7 +161,7 @@ pub fn createAndParseFromPath( |
| 161 | 161 | .file = file, |
| 162 | 162 | }; |
| 163 | 163 | |
| 164 | dylib.parse(allocator, arch) catch |err| switch (err) { | |
| 164 | dylib.parse(allocator, target) catch |err| switch (err) { | |
| 165 | 165 | error.EndOfStream, error.NotDylib => { |
| 166 | 166 | try file.seekTo(0); |
| 167 | 167 | |
| ... | ... | @@ -171,7 +171,7 @@ pub fn createAndParseFromPath( |
| 171 | 171 | }; |
| 172 | 172 | defer lib_stub.deinit(); |
| 173 | 173 | |
| 174 | try dylib.parseFromStub(allocator, arch, lib_stub); | |
| 174 | try dylib.parseFromStub(allocator, target, lib_stub); | |
| 175 | 175 | }, |
| 176 | 176 | else => |e| return e, |
| 177 | 177 | }; |
| ... | ... | @@ -195,7 +195,7 @@ pub fn createAndParseFromPath( |
| 195 | 195 | try dylibs.append(dylib); |
| 196 | 196 | // TODO this should not be performed if the user specifies `-flat_namespace` flag. |
| 197 | 197 | // See ld64 manpages. |
| 198 | try dylib.parseDependentLibs(allocator, arch, &dylibs, opts.syslibroot); | |
| 198 | try dylib.parseDependentLibs(allocator, target, &dylibs, opts.syslibroot); | |
| 199 | 199 | |
| 200 | 200 | return dylibs.toOwnedSlice(); |
| 201 | 201 | } |
| ... | ... | @@ -222,10 +222,10 @@ pub fn deinit(self: *Dylib, allocator: *Allocator) void { |
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | pub fn parse(self: *Dylib, allocator: *Allocator, arch: Arch) !void { | |
| 225 | pub fn parse(self: *Dylib, allocator: *Allocator, target: std.Target) !void { | |
| 226 | 226 | log.debug("parsing shared library '{s}'", .{self.name}); |
| 227 | 227 | |
| 228 | self.library_offset = try fat.getLibraryOffset(self.file.reader(), arch); | |
| 228 | self.library_offset = try fat.getLibraryOffset(self.file.reader(), target); | |
| 229 | 229 | |
| 230 | 230 | try self.file.seekTo(self.library_offset); |
| 231 | 231 | |
| ... | ... | @@ -237,10 +237,10 @@ pub fn parse(self: *Dylib, allocator: *Allocator, arch: Arch) !void { |
| 237 | 237 | return error.NotDylib; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | const this_arch: Arch = try fat.decodeArch(self.header.?.cputype, true); | |
| 240 | const this_arch: std.Target.Cpu.Arch = try fat.decodeArch(self.header.?.cputype, true); | |
| 241 | 241 | |
| 242 | if (this_arch != arch) { | |
| 243 | log.err("mismatched cpu architecture: expected {s}, found {s}", .{ arch, this_arch }); | |
| 242 | if (this_arch != target.cpu.arch) { | |
| 243 | log.err("mismatched cpu architecture: expected {s}, found {s}", .{ target.cpu.arch, this_arch }); | |
| 244 | 244 | return error.MismatchedCpuArchitecture; |
| 245 | 245 | } |
| 246 | 246 | |
| ... | ... | @@ -334,7 +334,16 @@ fn addObjCClassSymbols(self: *Dylib, allocator: *Allocator, sym_name: []const u8 |
| 334 | 334 | } |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | pub fn parseFromStub(self: *Dylib, allocator: *Allocator, arch: Arch, lib_stub: LibStub) !void { | |
| 337 | fn targetToAppleString(allocator: *Allocator, target: std.Target) ![]const u8 { | |
| 338 | const arch = switch (target.cpu.arch) { | |
| 339 | .aarch64 => "arm64", | |
| 340 | .x86_64 => "x86_64", | |
| 341 | else => unreachable, | |
| 342 | }; | |
| 343 | return std.fmt.allocPrint(allocator, "{s}-{s}", .{ arch, @tagName(target.os.tag) }); | |
| 344 | } | |
| 345 | ||
| 346 | pub fn parseFromStub(self: *Dylib, allocator: *Allocator, target: std.Target, lib_stub: LibStub) !void { | |
| 338 | 347 | if (lib_stub.inner.len == 0) return error.EmptyStubFile; |
| 339 | 348 | |
| 340 | 349 | log.debug("parsing shared library from stub '{s}'", .{self.name}); |
| ... | ... | @@ -350,11 +359,8 @@ pub fn parseFromStub(self: *Dylib, allocator: *Allocator, arch: Arch, lib_stub: |
| 350 | 359 | } |
| 351 | 360 | self.id = id; |
| 352 | 361 | |
| 353 | const target_string: []const u8 = switch (arch) { | |
| 354 | .aarch64 => "arm64-macos", | |
| 355 | .x86_64 => "x86_64-macos", | |
| 356 | else => unreachable, | |
| 357 | }; | |
| 362 | const target_string = try targetToAppleString(allocator, target); | |
| 363 | defer allocator.free(target_string); | |
| 358 | 364 | |
| 359 | 365 | var umbrella_libs = std.StringHashMap(void).init(allocator); |
| 360 | 366 | defer umbrella_libs.deinit(); |
| ... | ... | @@ -443,7 +449,7 @@ pub fn parseFromStub(self: *Dylib, allocator: *Allocator, arch: Arch, lib_stub: |
| 443 | 449 | pub fn parseDependentLibs( |
| 444 | 450 | self: *Dylib, |
| 445 | 451 | allocator: *Allocator, |
| 446 | arch: Arch, | |
| 452 | target: std.Target, | |
| 447 | 453 | out: *std.ArrayList(Dylib), |
| 448 | 454 | syslibroot: ?[]const u8, |
| 449 | 455 | ) !void { |
| ... | ... | @@ -475,7 +481,7 @@ pub fn parseDependentLibs( |
| 475 | 481 | |
| 476 | 482 | const dylibs = (try createAndParseFromPath( |
| 477 | 483 | allocator, |
| 478 | arch, | |
| 484 | target, | |
| 479 | 485 | full_path, |
| 480 | 486 | .{ |
| 481 | 487 | .id = id, |
src/link/MachO/Object.zig+6-7| ... | ... | @@ -15,7 +15,6 @@ const segmentName = commands.segmentName; |
| 15 | 15 | const sectionName = commands.sectionName; |
| 16 | 16 | |
| 17 | 17 | const Allocator = mem.Allocator; |
| 18 | const Arch = std.Target.Cpu.Arch; | |
| 19 | 18 | const LoadCommand = commands.LoadCommand; |
| 20 | 19 | const MachO = @import("../MachO.zig"); |
| 21 | 20 | const TextBlock = @import("TextBlock.zig"); |
| ... | ... | @@ -154,7 +153,7 @@ pub fn deinit(self: *Object, allocator: *Allocator) void { |
| 154 | 153 | } |
| 155 | 154 | } |
| 156 | 155 | |
| 157 | pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u8) !?Object { | |
| 156 | pub fn createAndParseFromPath(allocator: *Allocator, target: std.Target, path: []const u8) !?Object { | |
| 158 | 157 | const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) { |
| 159 | 158 | error.FileNotFound => return null, |
| 160 | 159 | else => |e| return e, |
| ... | ... | @@ -169,7 +168,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u |
| 169 | 168 | .file = file, |
| 170 | 169 | }; |
| 171 | 170 | |
| 172 | object.parse(allocator, arch) catch |err| switch (err) { | |
| 171 | object.parse(allocator, target) catch |err| switch (err) { | |
| 173 | 172 | error.EndOfStream, error.NotObject => { |
| 174 | 173 | object.deinit(allocator); |
| 175 | 174 | return null; |
| ... | ... | @@ -180,7 +179,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u |
| 180 | 179 | return object; |
| 181 | 180 | } |
| 182 | 181 | |
| 183 | pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void { | |
| 182 | pub fn parse(self: *Object, allocator: *Allocator, target: std.Target) !void { | |
| 184 | 183 | const reader = self.file.reader(); |
| 185 | 184 | if (self.file_offset) |offset| { |
| 186 | 185 | try reader.context.seekTo(offset); |
| ... | ... | @@ -195,7 +194,7 @@ pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void { |
| 195 | 194 | return error.NotObject; |
| 196 | 195 | } |
| 197 | 196 | |
| 198 | const this_arch: Arch = switch (header.cputype) { | |
| 197 | const this_arch: std.Target.Cpu.Arch = switch (header.cputype) { | |
| 199 | 198 | macho.CPU_TYPE_ARM64 => .aarch64, |
| 200 | 199 | macho.CPU_TYPE_X86_64 => .x86_64, |
| 201 | 200 | else => |value| { |
| ... | ... | @@ -203,8 +202,8 @@ pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void { |
| 203 | 202 | return error.UnsupportedCpuArchitecture; |
| 204 | 203 | }, |
| 205 | 204 | }; |
| 206 | if (this_arch != arch) { | |
| 207 | log.err("mismatched cpu architecture: expected {s}, found {s}", .{ arch, this_arch }); | |
| 205 | if (this_arch != target.cpu.arch) { | |
| 206 | log.err("mismatched cpu architecture: expected {s}, found {s}", .{ target.cpu.arch, this_arch }); | |
| 208 | 207 | return error.MismatchedCpuArchitecture; |
| 209 | 208 | } |
| 210 | 209 |
src/link/MachO/fat.zig+4-6| ... | ... | @@ -5,10 +5,8 @@ const macho = std.macho; |
| 5 | 5 | const mem = std.mem; |
| 6 | 6 | const native_endian = builtin.target.cpu.arch.endian(); |
| 7 | 7 | |
| 8 | const Arch = std.Target.Cpu.Arch; | |
| 9 | ||
| 10 | 8 | pub fn decodeArch(cputype: macho.cpu_type_t, comptime logError: bool) !std.Target.Cpu.Arch { |
| 11 | const arch: Arch = switch (cputype) { | |
| 9 | const arch: std.Target.Cpu.Arch = switch (cputype) { | |
| 12 | 10 | macho.CPU_TYPE_ARM64 => .aarch64, |
| 13 | 11 | macho.CPU_TYPE_X86_64 => .x86_64, |
| 14 | 12 | else => { |
| ... | ... | @@ -31,7 +29,7 @@ fn readFatStruct(reader: anytype, comptime T: type) !T { |
| 31 | 29 | return res; |
| 32 | 30 | } |
| 33 | 31 | |
| 34 | pub fn getLibraryOffset(reader: anytype, arch: Arch) !u64 { | |
| 32 | pub fn getLibraryOffset(reader: anytype, target: std.Target) !u64 { | |
| 35 | 33 | const fat_header = try readFatStruct(reader, macho.fat_header); |
| 36 | 34 | if (fat_header.magic != macho.FAT_MAGIC) return 0; |
| 37 | 35 | |
| ... | ... | @@ -44,12 +42,12 @@ pub fn getLibraryOffset(reader: anytype, arch: Arch) !u64 { |
| 44 | 42 | error.UnsupportedCpuArchitecture => continue, |
| 45 | 43 | else => |e| return e, |
| 46 | 44 | }; |
| 47 | if (lib_arch == arch) { | |
| 45 | if (lib_arch == target.cpu.arch) { | |
| 48 | 46 | // We have found a matching architecture! |
| 49 | 47 | return fat_arch.offset; |
| 50 | 48 | } |
| 51 | 49 | } else { |
| 52 | log.err("Could not find matching cpu architecture in fat library: expected {s}", .{arch}); | |
| 50 | log.err("Could not find matching cpu architecture in fat library: expected {s}", .{target.cpu.arch}); | |
| 53 | 51 | return error.MismatchedCpuArchitecture; |
| 54 | 52 | } |
| 55 | 53 | } |