aboutsummaryrefslogtreecommitdiff
path: root/src/link
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2021-08-05 11:56:32 +0200
committerJakub Konka <kubkon@jakubkonka.com>2021-08-10 13:41:07 +0200
commitace9b3de642ab89dd356d877ee6b46ce88640e1d (patch)
tree7548aa57cb64984780b9f86a569817d75c51afe0 /src/link
parent049ff45430f7d556d1c6947c0bc44f3df67a8b00 (diff)
downloadzig-ace9b3de642ab89dd356d877ee6b46ce88640e1d.tar.gz
zig-ace9b3de642ab89dd356d877ee6b46ce88640e1d.zip
macho: fix parsing target string when linking against tbds
Diffstat (limited to 'src/link')
-rw-r--r--src/link/MachO.zig23
-rw-r--r--src/link/MachO/Archive.zig13
-rw-r--r--src/link/MachO/Dylib.zig42
-rw-r--r--src/link/MachO/Object.zig13
-rw-r--r--src/link/MachO/fat.zig10
5 files changed, 46 insertions, 55 deletions
diff --git a/src/link/MachO.zig b/src/link/MachO.zig
index 6b0710129f..81f0808dc7 100644
--- a/src/link/MachO.zig
+++ b/src/link/MachO.zig
@@ -995,7 +995,6 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
}
fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const u8) !void {
- const arch = self.base.options.target.cpu.arch;
for (files) |file_name| {
const full_path = full_path: {
var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
@@ -1004,17 +1003,17 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
};
defer self.base.allocator.free(full_path);
- if (try Object.createAndParseFromPath(self.base.allocator, arch, full_path)) |object| {
+ if (try Object.createAndParseFromPath(self.base.allocator, self.base.options.target, full_path)) |object| {
try self.objects.append(self.base.allocator, object);
continue;
}
- if (try Archive.createAndParseFromPath(self.base.allocator, arch, full_path)) |archive| {
+ if (try Archive.createAndParseFromPath(self.base.allocator, self.base.options.target, full_path)) |archive| {
try self.archives.append(self.base.allocator, archive);
continue;
}
- if (try Dylib.createAndParseFromPath(self.base.allocator, arch, full_path, .{
+ if (try Dylib.createAndParseFromPath(self.base.allocator, self.base.options.target, full_path, .{
.syslibroot = syslibroot,
})) |dylibs| {
defer self.base.allocator.free(dylibs);
@@ -1032,9 +1031,8 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
}
fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !void {
- const arch = self.base.options.target.cpu.arch;
for (libs) |lib| {
- if (try Dylib.createAndParseFromPath(self.base.allocator, arch, lib, .{
+ if (try Dylib.createAndParseFromPath(self.base.allocator, self.base.options.target, lib, .{
.syslibroot = syslibroot,
})) |dylibs| {
defer self.base.allocator.free(dylibs);
@@ -1047,7 +1045,7 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8) !v
continue;
}
- if (try Archive.createAndParseFromPath(self.base.allocator, arch, lib)) |archive| {
+ if (try Archive.createAndParseFromPath(self.base.allocator, self.base.options.target, lib)) |archive| {
try self.archives.append(self.base.allocator, archive);
continue;
}
@@ -2236,11 +2234,7 @@ fn resolveSymbols(self: *MachO) !void {
const object_id = @intCast(u16, self.objects.items.len);
const object = try self.objects.addOne(self.base.allocator);
- object.* = try archive.parseObject(
- self.base.allocator,
- self.base.options.target.cpu.arch,
- offsets.items[0],
- );
+ object.* = try archive.parseObject(self.base.allocator, self.base.options.target, offsets.items[0]);
try self.resolveSymbolsInObject(object_id);
continue :loop;
@@ -2885,11 +2879,6 @@ fn flushZld(self: *MachO) !void {
if (self.base.options.target.cpu.arch == .aarch64) {
try self.writeCodeSignature();
}
-
- // if (comptime std.Target.current.isDarwin() and std.Target.current.cpu.arch == .aarch64) {
- // const out_path = self.output.?.path;
- // try fs.cwd().copyFile(out_path, fs.cwd(), out_path, .{});
- // }
}
fn writeGotEntries(self: *MachO) !void {
diff --git a/src/link/MachO/Archive.zig b/src/link/MachO/Archive.zig
index 6959dbac89..3b01233e1f 100644
--- a/src/link/MachO/Archive.zig
+++ b/src/link/MachO/Archive.zig
@@ -9,7 +9,6 @@ const mem = std.mem;
const fat = @import("fat.zig");
const Allocator = mem.Allocator;
-const Arch = std.Target.Cpu.Arch;
const Object = @import("Object.zig");
file: fs.File,
@@ -104,7 +103,7 @@ pub fn deinit(self: *Archive, allocator: *Allocator) void {
allocator.free(self.name);
}
-pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u8) !?Archive {
+pub fn createAndParseFromPath(allocator: *Allocator, target: std.Target, path: []const u8) !?Archive {
const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) {
error.FileNotFound => return null,
else => |e| return e,
@@ -119,7 +118,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
.file = file,
};
- archive.parse(allocator, arch) catch |err| switch (err) {
+ archive.parse(allocator, target) catch |err| switch (err) {
error.EndOfStream, error.NotArchive => {
archive.deinit(allocator);
return null;
@@ -130,9 +129,9 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
return archive;
}
-pub fn parse(self: *Archive, allocator: *Allocator, arch: Arch) !void {
+pub fn parse(self: *Archive, allocator: *Allocator, target: std.Target) !void {
const reader = self.file.reader();
- self.library_offset = try fat.getLibraryOffset(reader, arch);
+ self.library_offset = try fat.getLibraryOffset(reader, target);
try self.file.seekTo(self.library_offset);
const magic = try reader.readBytesNoEof(SARMAG);
@@ -215,7 +214,7 @@ fn parseTableOfContents(self: *Archive, allocator: *Allocator, reader: anytype)
}
}
-pub fn parseObject(self: Archive, allocator: *Allocator, arch: Arch, offset: u32) !Object {
+pub fn parseObject(self: Archive, allocator: *Allocator, target: std.Target, offset: u32) !Object {
const reader = self.file.reader();
try reader.context.seekTo(offset + self.library_offset);
@@ -244,7 +243,7 @@ pub fn parseObject(self: Archive, allocator: *Allocator, arch: Arch, offset: u32
.mtime = try self.header.?.date(),
};
- try object.parse(allocator, arch);
+ try object.parse(allocator, target);
try reader.context.seekTo(0);
return object;
diff --git a/src/link/MachO/Dylib.zig b/src/link/MachO/Dylib.zig
index 71301ccbbf..580159a13c 100644
--- a/src/link/MachO/Dylib.zig
+++ b/src/link/MachO/Dylib.zig
@@ -12,7 +12,6 @@ const fat = @import("fat.zig");
const commands = @import("commands.zig");
const Allocator = mem.Allocator;
-const Arch = std.Target.Cpu.Arch;
const LibStub = @import("../tapi.zig").LibStub;
const LoadCommand = commands.LoadCommand;
const MachO = @import("../MachO.zig");
@@ -139,11 +138,12 @@ pub const Error = error{
pub const CreateOpts = struct {
syslibroot: ?[]const u8 = null,
id: ?Id = null,
+ target: ?std.Target = null,
};
pub fn createAndParseFromPath(
allocator: *Allocator,
- arch: Arch,
+ target: std.Target,
path: []const u8,
opts: CreateOpts,
) Error!?[]Dylib {
@@ -161,7 +161,7 @@ pub fn createAndParseFromPath(
.file = file,
};
- dylib.parse(allocator, arch) catch |err| switch (err) {
+ dylib.parse(allocator, target) catch |err| switch (err) {
error.EndOfStream, error.NotDylib => {
try file.seekTo(0);
@@ -171,7 +171,7 @@ pub fn createAndParseFromPath(
};
defer lib_stub.deinit();
- try dylib.parseFromStub(allocator, arch, lib_stub);
+ try dylib.parseFromStub(allocator, target, lib_stub);
},
else => |e| return e,
};
@@ -195,7 +195,7 @@ pub fn createAndParseFromPath(
try dylibs.append(dylib);
// TODO this should not be performed if the user specifies `-flat_namespace` flag.
// See ld64 manpages.
- try dylib.parseDependentLibs(allocator, arch, &dylibs, opts.syslibroot);
+ try dylib.parseDependentLibs(allocator, target, &dylibs, opts.syslibroot);
return dylibs.toOwnedSlice();
}
@@ -222,10 +222,10 @@ pub fn deinit(self: *Dylib, allocator: *Allocator) void {
}
}
-pub fn parse(self: *Dylib, allocator: *Allocator, arch: Arch) !void {
+pub fn parse(self: *Dylib, allocator: *Allocator, target: std.Target) !void {
log.debug("parsing shared library '{s}'", .{self.name});
- self.library_offset = try fat.getLibraryOffset(self.file.reader(), arch);
+ self.library_offset = try fat.getLibraryOffset(self.file.reader(), target);
try self.file.seekTo(self.library_offset);
@@ -237,10 +237,10 @@ pub fn parse(self: *Dylib, allocator: *Allocator, arch: Arch) !void {
return error.NotDylib;
}
- const this_arch: Arch = try fat.decodeArch(self.header.?.cputype, true);
+ const this_arch: std.Target.Cpu.Arch = try fat.decodeArch(self.header.?.cputype, true);
- if (this_arch != arch) {
- log.err("mismatched cpu architecture: expected {s}, found {s}", .{ arch, this_arch });
+ if (this_arch != target.cpu.arch) {
+ log.err("mismatched cpu architecture: expected {s}, found {s}", .{ target.cpu.arch, this_arch });
return error.MismatchedCpuArchitecture;
}
@@ -334,7 +334,16 @@ fn addObjCClassSymbols(self: *Dylib, allocator: *Allocator, sym_name: []const u8
}
}
-pub fn parseFromStub(self: *Dylib, allocator: *Allocator, arch: Arch, lib_stub: LibStub) !void {
+fn targetToAppleString(allocator: *Allocator, target: std.Target) ![]const u8 {
+ const arch = switch (target.cpu.arch) {
+ .aarch64 => "arm64",
+ .x86_64 => "x86_64",
+ else => unreachable,
+ };
+ return std.fmt.allocPrint(allocator, "{s}-{s}", .{ arch, @tagName(target.os.tag) });
+}
+
+pub fn parseFromStub(self: *Dylib, allocator: *Allocator, target: std.Target, lib_stub: LibStub) !void {
if (lib_stub.inner.len == 0) return error.EmptyStubFile;
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:
}
self.id = id;
- const target_string: []const u8 = switch (arch) {
- .aarch64 => "arm64-macos",
- .x86_64 => "x86_64-macos",
- else => unreachable,
- };
+ const target_string = try targetToAppleString(allocator, target);
+ defer allocator.free(target_string);
var umbrella_libs = std.StringHashMap(void).init(allocator);
defer umbrella_libs.deinit();
@@ -443,7 +449,7 @@ pub fn parseFromStub(self: *Dylib, allocator: *Allocator, arch: Arch, lib_stub:
pub fn parseDependentLibs(
self: *Dylib,
allocator: *Allocator,
- arch: Arch,
+ target: std.Target,
out: *std.ArrayList(Dylib),
syslibroot: ?[]const u8,
) !void {
@@ -475,7 +481,7 @@ pub fn parseDependentLibs(
const dylibs = (try createAndParseFromPath(
allocator,
- arch,
+ target,
full_path,
.{
.id = id,
diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig
index 2e6a20ad4b..c6aa2fb631 100644
--- a/src/link/MachO/Object.zig
+++ b/src/link/MachO/Object.zig
@@ -15,7 +15,6 @@ const segmentName = commands.segmentName;
const sectionName = commands.sectionName;
const Allocator = mem.Allocator;
-const Arch = std.Target.Cpu.Arch;
const LoadCommand = commands.LoadCommand;
const MachO = @import("../MachO.zig");
const TextBlock = @import("TextBlock.zig");
@@ -154,7 +153,7 @@ pub fn deinit(self: *Object, allocator: *Allocator) void {
}
}
-pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u8) !?Object {
+pub fn createAndParseFromPath(allocator: *Allocator, target: std.Target, path: []const u8) !?Object {
const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) {
error.FileNotFound => return null,
else => |e| return e,
@@ -169,7 +168,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
.file = file,
};
- object.parse(allocator, arch) catch |err| switch (err) {
+ object.parse(allocator, target) catch |err| switch (err) {
error.EndOfStream, error.NotObject => {
object.deinit(allocator);
return null;
@@ -180,7 +179,7 @@ pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u
return object;
}
-pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void {
+pub fn parse(self: *Object, allocator: *Allocator, target: std.Target) !void {
const reader = self.file.reader();
if (self.file_offset) |offset| {
try reader.context.seekTo(offset);
@@ -195,7 +194,7 @@ pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void {
return error.NotObject;
}
- const this_arch: Arch = switch (header.cputype) {
+ const this_arch: std.Target.Cpu.Arch = switch (header.cputype) {
macho.CPU_TYPE_ARM64 => .aarch64,
macho.CPU_TYPE_X86_64 => .x86_64,
else => |value| {
@@ -203,8 +202,8 @@ pub fn parse(self: *Object, allocator: *Allocator, arch: Arch) !void {
return error.UnsupportedCpuArchitecture;
},
};
- if (this_arch != arch) {
- log.err("mismatched cpu architecture: expected {s}, found {s}", .{ arch, this_arch });
+ if (this_arch != target.cpu.arch) {
+ log.err("mismatched cpu architecture: expected {s}, found {s}", .{ target.cpu.arch, this_arch });
return error.MismatchedCpuArchitecture;
}
diff --git a/src/link/MachO/fat.zig b/src/link/MachO/fat.zig
index 61592c88a5..c911d89535 100644
--- a/src/link/MachO/fat.zig
+++ b/src/link/MachO/fat.zig
@@ -5,10 +5,8 @@ const macho = std.macho;
const mem = std.mem;
const native_endian = builtin.target.cpu.arch.endian();
-const Arch = std.Target.Cpu.Arch;
-
pub fn decodeArch(cputype: macho.cpu_type_t, comptime logError: bool) !std.Target.Cpu.Arch {
- const arch: Arch = switch (cputype) {
+ const arch: std.Target.Cpu.Arch = switch (cputype) {
macho.CPU_TYPE_ARM64 => .aarch64,
macho.CPU_TYPE_X86_64 => .x86_64,
else => {
@@ -31,7 +29,7 @@ fn readFatStruct(reader: anytype, comptime T: type) !T {
return res;
}
-pub fn getLibraryOffset(reader: anytype, arch: Arch) !u64 {
+pub fn getLibraryOffset(reader: anytype, target: std.Target) !u64 {
const fat_header = try readFatStruct(reader, macho.fat_header);
if (fat_header.magic != macho.FAT_MAGIC) return 0;
@@ -44,12 +42,12 @@ pub fn getLibraryOffset(reader: anytype, arch: Arch) !u64 {
error.UnsupportedCpuArchitecture => continue,
else => |e| return e,
};
- if (lib_arch == arch) {
+ if (lib_arch == target.cpu.arch) {
// We have found a matching architecture!
return fat_arch.offset;
}
} else {
- log.err("Could not find matching cpu architecture in fat library: expected {s}", .{arch});
+ log.err("Could not find matching cpu architecture in fat library: expected {s}", .{target.cpu.arch});
return error.MismatchedCpuArchitecture;
}
}