authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-30 10:22:49+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-06-30 10:22:49+02:00
loga95ba0d10d582020dbd3e6efded53f17287ed211
tree7f17ba0b98e18e6ff4c833d08a45779a5ce52d01
parent908b431abeb1df5d3c3ad34d9299e720c3e06a9d
parent186577225f2a026fea01b48c7bcca47f36efc95c
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9254 from moosichu/feature/macos-link-universal-static-libs

Add fat/universal archive support to zig ld

3 files changed, 68 insertions(+), 53 deletions(-)

src/link/MachO/Archive.zig+10-1
......@@ -6,6 +6,7 @@ const fs = std.fs;
66const log = std.log.scoped(.archive);
77const macho = std.macho;
88const mem = std.mem;
9const fat = @import("fat.zig");
910
1011const Allocator = mem.Allocator;
1112const Arch = std.Target.Cpu.Arch;
......@@ -19,6 +20,10 @@ file: ?fs.File = null,
1920header: ?ar_hdr = null,
2021name: ?[]const u8 = null,
2122
23// The actual contents we care about linking with will be embedded at
24// an offset within a file if we are linking against a fat lib
25library_offset: u64 = 0,
26
2227/// Parsed table of contents.
2328/// Each symbol name points to a list of all definition
2429/// sites within the current static archive.
......@@ -139,6 +144,10 @@ pub fn closeFile(self: Archive) void {
139144}
140145
141146pub fn parse(self: *Archive) !void {
147 self.library_offset = try fat.getLibraryOffset(self.file.?.reader(), self.arch.?);
148
149 try self.file.?.seekTo(self.library_offset);
150
142151 var reader = self.file.?.reader();
143152 const magic = try reader.readBytesNoEof(SARMAG);
144153
......@@ -226,7 +235,7 @@ fn parseTableOfContents(self: *Archive, reader: anytype) !void {
226235/// Caller owns the Object instance.
227236pub fn parseObject(self: Archive, offset: u32) !*Object {
228237 var reader = self.file.?.reader();
229 try reader.context.seekTo(offset);
238 try reader.context.seekTo(offset + self.library_offset);
230239
231240 const object_header = try reader.readStruct(ar_hdr);
232241
src/link/MachO/Dylib.zig+3-52
......@@ -1,7 +1,6 @@
11const Dylib = @This();
22
33const std = @import("std");
4const builtin = std.builtin;
54const assert = std.debug.assert;
65const fs = std.fs;
76const fmt = std.fmt;
......@@ -9,7 +8,7 @@ const log = std.log.scoped(.dylib);
98const macho = std.macho;
109const math = std.math;
1110const mem = std.mem;
12const native_endian = builtin.target.cpu.arch.endian();
11const fat = @import("fat.zig");
1312
1413const Allocator = mem.Allocator;
1514const Arch = std.Target.Cpu.Arch;
......@@ -238,42 +237,10 @@ pub fn closeFile(self: Dylib) void {
238237 }
239238}
240239
241fn decodeArch(cputype: macho.cpu_type_t) !std.Target.Cpu.Arch {
242 const arch: Arch = switch (cputype) {
243 macho.CPU_TYPE_ARM64 => .aarch64,
244 macho.CPU_TYPE_X86_64 => .x86_64,
245 else => {
246 return error.UnsupportedCpuArchitecture;
247 },
248 };
249 return arch;
250}
251
252240pub fn parse(self: *Dylib) !void {
253241 log.debug("parsing shared library '{s}'", .{self.name.?});
254242
255 self.library_offset = offset: {
256 const fat_header = try readFatStruct(self.file.?.reader(), macho.fat_header);
257 if (fat_header.magic != macho.FAT_MAGIC) break :offset 0;
258
259 var fat_arch_index: u32 = 0;
260 while (fat_arch_index < fat_header.nfat_arch) : (fat_arch_index += 1) {
261 const fat_arch = try readFatStruct(self.file.?.reader(), macho.fat_arch);
262 // If we come across an architecture that we do not know how to handle, that's
263 // fine because we can keep looking for one that might match.
264 const lib_arch = decodeArch(fat_arch.cputype) catch |err| switch (err) {
265 error.UnsupportedCpuArchitecture => continue,
266 else => |e| return e,
267 };
268 if (lib_arch == self.arch.?) {
269 // We have found a matching architecture!
270 break :offset fat_arch.offset;
271 }
272 } else {
273 log.err("Could not find matching cpu architecture in fat library: expected {s}", .{self.arch.?});
274 return error.MismatchedCpuArchitecture;
275 }
276 };
243 self.library_offset = try fat.getLibraryOffset(self.file.?.reader(), self.arch.?);
277244
278245 try self.file.?.seekTo(self.library_offset);
279246
......@@ -285,13 +252,7 @@ pub fn parse(self: *Dylib) !void {
285252 return error.NotDylib;
286253 }
287254
288 const this_arch: Arch = decodeArch(self.header.?.cputype) catch |err| switch (err) {
289 error.UnsupportedCpuArchitecture => |e| {
290 log.err("unsupported cpu architecture 0x{x}", .{self.header.?.cputype});
291 return e;
292 },
293 else => |e| return e,
294 };
255 const this_arch: Arch = try fat.decodeArch(self.header.?.cputype, true);
295256
296257 if (this_arch != self.arch.?) {
297258 log.err("mismatched cpu architecture: expected {s}, found {s}", .{ self.arch.?, this_arch });
......@@ -303,16 +264,6 @@ pub fn parse(self: *Dylib) !void {
303264 try self.parseSymbols();
304265}
305266
306fn readFatStruct(reader: anytype, comptime T: type) !T {
307 // Fat structures (fat_header & fat_arch) are always written and read to/from
308 // disk in big endian order.
309 var res: T = try reader.readStruct(T);
310 if (native_endian != builtin.Endian.Big) {
311 mem.bswapAllFields(T, &res);
312 }
313 return res;
314}
315
316267fn readLoadCommands(self: *Dylib, reader: anytype) !void {
317268 const should_lookup_reexports = self.header.?.flags & macho.MH_NO_REEXPORTED_DYLIBS == 0;
318269
src/link/MachO/fat.zig created+55
......@@ -0,0 +1,55 @@
1const std = @import("std");
2const builtin = std.builtin;
3const log = std.log.scoped(.archive);
4const macho = std.macho;
5const mem = std.mem;
6const native_endian = builtin.target.cpu.arch.endian();
7
8const Arch = std.Target.Cpu.Arch;
9
10pub fn decodeArch(cputype: macho.cpu_type_t, comptime logError: bool) !std.Target.Cpu.Arch {
11 const arch: Arch = switch (cputype) {
12 macho.CPU_TYPE_ARM64 => .aarch64,
13 macho.CPU_TYPE_X86_64 => .x86_64,
14 else => {
15 if (logError) {
16 log.err("unsupported cpu architecture 0x{x}", .{cputype});
17 }
18 return error.UnsupportedCpuArchitecture;
19 },
20 };
21 return arch;
22}
23
24fn readFatStruct(reader: anytype, comptime T: type) !T {
25 // Fat structures (fat_header & fat_arch) are always written and read to/from
26 // disk in big endian order.
27 var res = try reader.readStruct(T);
28 if (native_endian != builtin.Endian.Big) {
29 mem.bswapAllFields(T, &res);
30 }
31 return res;
32}
33
34pub fn getLibraryOffset(reader: anytype, arch: Arch) !u64 {
35 const fat_header = try readFatStruct(reader, macho.fat_header);
36 if (fat_header.magic != macho.FAT_MAGIC) return 0;
37
38 var fat_arch_index: u32 = 0;
39 while (fat_arch_index < fat_header.nfat_arch) : (fat_arch_index += 1) {
40 const fat_arch = try readFatStruct(reader, macho.fat_arch);
41 // If we come across an architecture that we do not know how to handle, that's
42 // fine because we can keep looking for one that might match.
43 const lib_arch = decodeArch(fat_arch.cputype, false) catch |err| switch (err) {
44 error.UnsupportedCpuArchitecture => continue,
45 else => |e| return e,
46 };
47 if (lib_arch == arch) {
48 // We have found a matching architecture!
49 return fat_arch.offset;
50 }
51 } else {
52 log.err("Could not find matching cpu architecture in fat library: expected {s}", .{arch});
53 return error.MismatchedCpuArchitecture;
54 }
55}