authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-05 11:56:32+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-10 13:41:07+02:00
logace9b3de642ab89dd356d877ee6b46ce88640e1d
tree7548aa57cb64984780b9f86a569817d75c51afe0
parent049ff45430f7d556d1c6947c0bc44f3df67a8b00

macho: fix parsing target string when linking against tbds


5 files changed, 46 insertions(+), 55 deletions(-)

src/link/MachO.zig+6-17
......@@ -995,7 +995,6 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
995995}
996996
997997fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const u8) !void {
998 const arch = self.base.options.target.cpu.arch;
999998 for (files) |file_name| {
1000999 const full_path = full_path: {
10011000 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
......@@ -1004,17 +1003,17 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
10041003 };
10051004 defer self.base.allocator.free(full_path);
10061005
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| {
10081007 try self.objects.append(self.base.allocator, object);
10091008 continue;
10101009 }
10111010
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| {
10131012 try self.archives.append(self.base.allocator, archive);
10141013 continue;
10151014 }
10161015
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, .{
10181017 .syslibroot = syslibroot,
10191018 })) |dylibs| {
10201019 defer self.base.allocator.free(dylibs);
......@@ -1032,9 +1031,8 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
10321031}
10331032
10341033fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !void {
1035 const arch = self.base.options.target.cpu.arch;
10361034 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, .{
10381036 .syslibroot = syslibroot,
10391037 })) |dylibs| {
10401038 defer self.base.allocator.free(dylibs);
......@@ -1047,7 +1045,7 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !v
10471045 continue;
10481046 }
10491047
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| {
10511049 try self.archives.append(self.base.allocator, archive);
10521050 continue;
10531051 }
......@@ -2236,11 +2234,7 @@ fn resolveSymbols(self: *MachO) !void {
22362234
22372235 const object_id = @intCast(u16, self.objects.items.len);
22382236 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]);
22442238 try self.resolveSymbolsInObject(object_id);
22452239
22462240 continue :loop;
......@@ -2885,11 +2879,6 @@ fn flushZld(self: *MachO) !void {
28852879 if (self.base.options.target.cpu.arch == .aarch64) {
28862880 try self.writeCodeSignature();
28872881 }
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 // }
28932882}
28942883
28952884fn writeGotEntries(self: *MachO) !void {
src/link/MachO/Archive.zig+6-7
......@@ -9,7 +9,6 @@ const mem = std.mem;
99const fat = @import("fat.zig");
1010
1111const Allocator = mem.Allocator;
12const Arch = std.Target.Cpu.Arch;
1312const Object = @import("Object.zig");
1413
1514file: fs.File,
......@@ -104,7 +103,7 @@ pub fn deinit(self: *Archive, allocator: *Allocator) void {
104103 allocator.free(self.name);
105104}
106105
107pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u8) !?Archive {
106pub fn createAndParseFromPath(allocator: *Allocator, target: std.Target, path: []const u8) !?Archive {
108107 const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) {
109108 error.FileNotFound => return null,
110109 else => |e| return e,
......@@ -119,7 +118,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
119118 .file = file,
120119 };
121120
122 archive.parse(allocator, arch) catch |err| switch (err) {
121 archive.parse(allocator, target) catch |err| switch (err) {
123122 error.EndOfStream, error.NotArchive => {
124123 archive.deinit(allocator);
125124 return null;
......@@ -130,9 +129,9 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
130129 return archive;
131130}
132131
133pub fn parse(self: *Archive, allocator: *Allocator, arch: Arch) !void {
132pub fn parse(self: *Archive, allocator: *Allocator, target: std.Target) !void {
134133 const reader = self.file.reader();
135 self.library_offset = try fat.getLibraryOffset(reader, arch);
134 self.library_offset = try fat.getLibraryOffset(reader, target);
136135 try self.file.seekTo(self.library_offset);
137136
138137 const magic = try reader.readBytesNoEof(SARMAG);
......@@ -215,7 +214,7 @@ fn parseTableOfContents(self: *Archive, allocator: *Allocator, reader: anytype)
215214 }
216215}
217216
218pub fn parseObject(self: Archive, allocator: *Allocator, arch: Arch, offset: u32) !Object {
217pub fn parseObject(self: Archive, allocator: *Allocator, target: std.Target, offset: u32) !Object {
219218 const reader = self.file.reader();
220219 try reader.context.seekTo(offset + self.library_offset);
221220
......@@ -244,7 +243,7 @@ pub fn parseObject(self: Archive, allocator: *Allocator, arch: Arch, offset: u32
244243 .mtime = try self.header.?.date(),
245244 };
246245
247 try object.parse(allocator, arch);
246 try object.parse(allocator, target);
248247 try reader.context.seekTo(0);
249248
250249 return object;
src/link/MachO/Dylib.zig+24-18
......@@ -12,7 +12,6 @@ const fat = @import("fat.zig");
1212const commands = @import("commands.zig");
1313
1414const Allocator = mem.Allocator;
15const Arch = std.Target.Cpu.Arch;
1615const LibStub = @import("../tapi.zig").LibStub;
1716const LoadCommand = commands.LoadCommand;
1817const MachO = @import("../MachO.zig");
......@@ -139,11 +138,12 @@ pub const Error = error{
139138pub const CreateOpts = struct {
140139 syslibroot: ?[]const u8 = null,
141140 id: ?Id = null,
141 target: ?std.Target = null,
142142};
143143
144144pub fn createAndParseFromPath(
145145 allocator: *Allocator,
146 arch: Arch,
146 target: std.Target,
147147 path: []const u8,
148148 opts: CreateOpts,
149149) Error!?[]Dylib {
......@@ -161,7 +161,7 @@ pub fn createAndParseFromPath(
161161 .file = file,
162162 };
163163
164 dylib.parse(allocator, arch) catch |err| switch (err) {
164 dylib.parse(allocator, target) catch |err| switch (err) {
165165 error.EndOfStream, error.NotDylib => {
166166 try file.seekTo(0);
167167
......@@ -171,7 +171,7 @@ pub fn createAndParseFromPath(
171171 };
172172 defer lib_stub.deinit();
173173
174 try dylib.parseFromStub(allocator, arch, lib_stub);
174 try dylib.parseFromStub(allocator, target, lib_stub);
175175 },
176176 else => |e| return e,
177177 };
......@@ -195,7 +195,7 @@ pub fn createAndParseFromPath(
195195 try dylibs.append(dylib);
196196 // TODO this should not be performed if the user specifies `-flat_namespace` flag.
197197 // See ld64 manpages.
198 try dylib.parseDependentLibs(allocator, arch, &dylibs, opts.syslibroot);
198 try dylib.parseDependentLibs(allocator, target, &dylibs, opts.syslibroot);
199199
200200 return dylibs.toOwnedSlice();
201201}
......@@ -222,10 +222,10 @@ pub fn deinit(self: *Dylib, allocator: *Allocator) void {
222222 }
223223}
224224
225pub fn parse(self: *Dylib, allocator: *Allocator, arch: Arch) !void {
225pub fn parse(self: *Dylib, allocator: *Allocator, target: std.Target) !void {
226226 log.debug("parsing shared library '{s}'", .{self.name});
227227
228 self.library_offset = try fat.getLibraryOffset(self.file.reader(), arch);
228 self.library_offset = try fat.getLibraryOffset(self.file.reader(), target);
229229
230230 try self.file.seekTo(self.library_offset);
231231
......@@ -237,10 +237,10 @@ pub fn parse(self: *Dylib, allocator: *Allocator, arch: Arch) !void {
237237 return error.NotDylib;
238238 }
239239
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);
241241
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 });
244244 return error.MismatchedCpuArchitecture;
245245 }
246246
......@@ -334,7 +334,16 @@ fn addObjCClassSymbols(self: *Dylib, allocator: *Allocator, sym_name: []const u8
334334 }
335335}
336336
337pub fn parseFromStub(self: *Dylib, allocator: *Allocator, arch: Arch, lib_stub: LibStub) !void {
337fn 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
346pub fn parseFromStub(self: *Dylib, allocator: *Allocator, target: std.Target, lib_stub: LibStub) !void {
338347 if (lib_stub.inner.len == 0) return error.EmptyStubFile;
339348
340349 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:
350359 }
351360 self.id = id;
352361
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);
358364
359365 var umbrella_libs = std.StringHashMap(void).init(allocator);
360366 defer umbrella_libs.deinit();
......@@ -443,7 +449,7 @@ pub fn parseFromStub(self: *Dylib, allocator: *Allocator, arch: Arch, lib_stub:
443449pub fn parseDependentLibs(
444450 self: *Dylib,
445451 allocator: *Allocator,
446 arch: Arch,
452 target: std.Target,
447453 out: *std.ArrayList(Dylib),
448454 syslibroot: ?[]const u8,
449455) !void {
......@@ -475,7 +481,7 @@ pub fn parseDependentLibs(
475481
476482 const dylibs = (try createAndParseFromPath(
477483 allocator,
478 arch,
484 target,
479485 full_path,
480486 .{
481487 .id = id,
src/link/MachO/Object.zig+6-7
......@@ -15,7 +15,6 @@ const segmentName = commands.segmentName;
1515const sectionName = commands.sectionName;
1616
1717const Allocator = mem.Allocator;
18const Arch = std.Target.Cpu.Arch;
1918const LoadCommand = commands.LoadCommand;
2019const MachO = @import("../MachO.zig");
2120const TextBlock = @import("TextBlock.zig");
......@@ -154,7 +153,7 @@ pub fn deinit(self: *Object, allocator: *Allocator) void {
154153 }
155154}
156155
157pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u8) !?Object {
156pub fn createAndParseFromPath(allocator: *Allocator, target: std.Target, path: []const u8) !?Object {
158157 const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) {
159158 error.FileNotFound => return null,
160159 else => |e| return e,
......@@ -169,7 +168,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
169168 .file = file,
170169 };
171170
172 object.parse(allocator, arch) catch |err| switch (err) {
171 object.parse(allocator, target) catch |err| switch (err) {
173172 error.EndOfStream, error.NotObject => {
174173 object.deinit(allocator);
175174 return null;
......@@ -180,7 +179,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
180179 return object;
181180}
182181
183pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void {
182pub fn parse(self: *Object, allocator: *Allocator, target: std.Target) !void {
184183 const reader = self.file.reader();
185184 if (self.file_offset) |offset| {
186185 try reader.context.seekTo(offset);
......@@ -195,7 +194,7 @@ pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void {
195194 return error.NotObject;
196195 }
197196
198 const this_arch: Arch = switch (header.cputype) {
197 const this_arch: std.Target.Cpu.Arch = switch (header.cputype) {
199198 macho.CPU_TYPE_ARM64 => .aarch64,
200199 macho.CPU_TYPE_X86_64 => .x86_64,
201200 else => |value| {
......@@ -203,8 +202,8 @@ pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void {
203202 return error.UnsupportedCpuArchitecture;
204203 },
205204 };
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 });
208207 return error.MismatchedCpuArchitecture;
209208 }
210209
src/link/MachO/fat.zig+4-6
......@@ -5,10 +5,8 @@ const macho = std.macho;
55const mem = std.mem;
66const native_endian = builtin.target.cpu.arch.endian();
77
8const Arch = std.Target.Cpu.Arch;
9
108pub 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) {
1210 macho.CPU_TYPE_ARM64 => .aarch64,
1311 macho.CPU_TYPE_X86_64 => .x86_64,
1412 else => {
......@@ -31,7 +29,7 @@ fn readFatStruct(reader: anytype, comptime T: type) !T {
3129 return res;
3230}
3331
34pub fn getLibraryOffset(reader: anytype, arch: Arch) !u64 {
32pub fn getLibraryOffset(reader: anytype, target: std.Target) !u64 {
3533 const fat_header = try readFatStruct(reader, macho.fat_header);
3634 if (fat_header.magic != macho.FAT_MAGIC) return 0;
3735
......@@ -44,12 +42,12 @@ pub fn getLibraryOffset(reader: anytype, arch: Arch) !u64 {
4442 error.UnsupportedCpuArchitecture => continue,
4543 else => |e| return e,
4644 };
47 if (lib_arch == arch) {
45 if (lib_arch == target.cpu.arch) {
4846 // We have found a matching architecture!
4947 return fat_arch.offset;
5048 }
5149 } 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});
5351 return error.MismatchedCpuArchitecture;
5452 }
5553}