authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-07-26 18:22:59+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-26 18:22:59+02:00
logf821543e4b5e44b2ca3217c189272105fc3effb9
tree791536aa3ff2205cdaaa7052ea3dd596db3782ea
parentcb475aa161238ef0d77fc49b13c23f2c4b48bfc5
parent960e5c329a5436d1e5748948c5744c1a11ddecbb
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #16553 from ziglang/issue-11896

macho: fix parsing of TBDv3 dylib stubs

6 files changed, 114 insertions(+), 18 deletions(-)

src/link/MachO/Dylib.zig+40-15
...@@ -9,12 +9,14 @@ const macho = std.macho;...@@ -9,12 +9,14 @@ const macho = std.macho;
9const math = std.math;9const math = std.math;
10const mem = std.mem;10const mem = std.mem;
11const fat = @import("fat.zig");11const fat = @import("fat.zig");
12const tapi = @import("../tapi.zig");
1213
13const Allocator = mem.Allocator;14const Allocator = mem.Allocator;
14const CrossTarget = std.zig.CrossTarget;15const CrossTarget = std.zig.CrossTarget;
15const LibStub = @import("../tapi.zig").LibStub;16const LibStub = tapi.LibStub;
16const LoadCommandIterator = macho.LoadCommandIterator;17const LoadCommandIterator = macho.LoadCommandIterator;
17const MachO = @import("../MachO.zig");18const MachO = @import("../MachO.zig");
19const Tbd = tapi.Tbd;
1820
19id: ?Id = null,21id: ?Id = null,
20weak: bool = false,22weak: bool = false,
...@@ -247,7 +249,8 @@ const TargetMatcher = struct {...@@ -247,7 +249,8 @@ const TargetMatcher = struct {
247 .allocator = allocator,249 .allocator = allocator,
248 .target = target,250 .target = target,
249 };251 };
250 try self.target_strings.append(allocator, try targetToAppleString(allocator, target));252 const apple_string = try targetToAppleString(allocator, target);
253 try self.target_strings.append(allocator, apple_string);
251254
252 const abi = target.abi orelse .none;255 const abi = target.abi orelse .none;
253 if (abi == .simulator) {256 if (abi == .simulator) {
...@@ -270,22 +273,29 @@ const TargetMatcher = struct {...@@ -270,22 +273,29 @@ const TargetMatcher = struct {
270 self.target_strings.deinit(self.allocator);273 self.target_strings.deinit(self.allocator);
271 }274 }
272275
273 fn targetToAppleString(allocator: Allocator, target: CrossTarget) ![]const u8 {276 inline fn cpuArchToAppleString(cpu_arch: std.Target.Cpu.Arch) []const u8 {
274 const cpu_arch = switch (target.cpu_arch.?) {277 return switch (cpu_arch) {
275 .aarch64 => "arm64",278 .aarch64 => "arm64",
276 .x86_64 => "x86_64",279 .x86_64 => "x86_64",
277 else => unreachable,280 else => unreachable,
278 };281 };
279 const os_tag = @tagName(target.os_tag.?);282 }
280 const target_abi = target.abi orelse .none;283
281 const abi: ?[]const u8 = switch (target_abi) {284 inline fn abiToAppleString(abi: std.Target.Abi) ?[]const u8 {
285 return switch (abi) {
282 .none => null,286 .none => null,
283 .simulator => "simulator",287 .simulator => "simulator",
284 .macabi => "maccatalyst",288 .macabi => "maccatalyst",
285 else => unreachable,289 else => unreachable,
286 };290 };
287 if (abi) |x| {291 }
288 return std.fmt.allocPrint(allocator, "{s}-{s}-{s}", .{ cpu_arch, os_tag, x });292
293 fn targetToAppleString(allocator: Allocator, target: CrossTarget) ![]const u8 {
294 const cpu_arch = cpuArchToAppleString(target.cpu_arch.?);
295 const os_tag = @tagName(target.os_tag.?);
296 const target_abi = abiToAppleString(target.abi orelse .none);
297 if (target_abi) |abi| {
298 return std.fmt.allocPrint(allocator, "{s}-{s}-{s}", .{ cpu_arch, os_tag, abi });
289 }299 }
290 return std.fmt.allocPrint(allocator, "{s}-{s}", .{ cpu_arch, os_tag });300 return std.fmt.allocPrint(allocator, "{s}-{s}", .{ cpu_arch, os_tag });
291 }301 }
...@@ -305,7 +315,26 @@ const TargetMatcher = struct {...@@ -305,7 +315,26 @@ const TargetMatcher = struct {
305 }315 }
306316
307 fn matchesArch(self: TargetMatcher, archs: []const []const u8) bool {317 fn matchesArch(self: TargetMatcher, archs: []const []const u8) bool {
308 return hasValue(archs, @tagName(self.target.cpu_arch.?));318 return hasValue(archs, cpuArchToAppleString(self.target.cpu_arch.?));
319 }
320
321 fn matchesTargetTbd(self: TargetMatcher, tbd: Tbd) !bool {
322 var arena = std.heap.ArenaAllocator.init(self.allocator);
323 defer arena.deinit();
324
325 const targets = switch (tbd) {
326 .v3 => |v3| blk: {
327 var targets = std.ArrayList([]const u8).init(arena.allocator());
328 for (v3.archs) |arch| {
329 const target = try std.fmt.allocPrint(arena.allocator(), "{s}-{s}", .{ arch, v3.platform });
330 try targets.append(target);
331 }
332 break :blk targets.items;
333 },
334 .v4 => |v4| v4.targets,
335 };
336
337 return self.matchesTarget(targets);
309 }338 }
310};339};
311340
...@@ -348,11 +377,7 @@ pub fn parseFromStub(...@@ -348,11 +377,7 @@ pub fn parseFromStub(
348 defer matcher.deinit();377 defer matcher.deinit();
349378
350 for (lib_stub.inner, 0..) |elem, stub_index| {379 for (lib_stub.inner, 0..) |elem, stub_index| {
351 const is_match = switch (elem) {380 if (!(try matcher.matchesTargetTbd(elem))) continue;
352 .v3 => |stub| matcher.matchesArch(stub.archs),
353 .v4 => |stub| matcher.matchesTarget(stub.targets),
354 };
355 if (!is_match) continue;
356381
357 if (stub_index > 0) {382 if (stub_index > 0) {
358 // TODO I thought that we could switch on presence of `parent-umbrella` map;383 // TODO I thought that we could switch on presence of `parent-umbrella` map;
src/link/tapi/yaml.zig+3-3
...@@ -197,7 +197,7 @@ pub const Value = union(enum) {...@@ -197,7 +197,7 @@ pub const Value = union(enum) {
197197
198 return Value{ .string = try arena.dupe(u8, value.string_value.items) };198 return Value{ .string = try arena.dupe(u8, value.string_value.items) };
199 } else {199 } else {
200 log.err("Unexpected node type: {}", .{node.tag});200 log.debug("Unexpected node type: {}", .{node.tag});
201 return error.UnexpectedNodeType;201 return error.UnexpectedNodeType;
202 }202 }
203 }203 }
...@@ -270,7 +270,7 @@ pub const Value = union(enum) {...@@ -270,7 +270,7 @@ pub const Value = union(enum) {
270 if (try encode(arena, elem)) |value| {270 if (try encode(arena, elem)) |value| {
271 list.appendAssumeCapacity(value);271 list.appendAssumeCapacity(value);
272 } else {272 } else {
273 log.err("Could not encode value in a list: {any}", .{elem});273 log.debug("Could not encode value in a list: {any}", .{elem});
274 return error.CannotEncodeValue;274 return error.CannotEncodeValue;
275 }275 }
276 }276 }
...@@ -432,7 +432,7 @@ pub const Yaml = struct {...@@ -432,7 +432,7 @@ pub const Yaml = struct {
432 }432 }
433433
434 const unwrapped = value orelse {434 const unwrapped = value orelse {
435 log.err("missing struct field: {s}: {s}", .{ field.name, @typeName(field.type) });435 log.debug("missing struct field: {s}: {s}", .{ field.name, @typeName(field.type) });
436 return error.StructFieldMissing;436 return error.StructFieldMissing;
437 };437 };
438 @field(parsed, field.name) = try self.parseValue(field.type, unwrapped);438 @field(parsed, field.name) = try self.parseValue(field.type, unwrapped);
test/link.zig+4
...@@ -164,6 +164,10 @@ pub const cases = [_]Case{...@@ -164,6 +164,10 @@ pub const cases = [_]Case{
164 .build_root = "test/link/macho/strict_validation",164 .build_root = "test/link/macho/strict_validation",
165 .import = @import("link/macho/strict_validation/build.zig"),165 .import = @import("link/macho/strict_validation/build.zig"),
166 },166 },
167 .{
168 .build_root = "test/link/macho/tbdv3",
169 .import = @import("link/macho/tbdv3/build.zig"),
170 },
167 .{171 .{
168 .build_root = "test/link/macho/tls",172 .build_root = "test/link/macho/tls",
169 .import = @import("link/macho/tls/build.zig"),173 .import = @import("link/macho/tls/build.zig"),
test/link/macho/tbdv3/a.c created+3
...@@ -0,0 +1,3 @@
1int getFoo() {
2 return 42;
3}
test/link/macho/tbdv3/build.zig created+57
...@@ -0,0 +1,57 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4pub const requires_symlinks = true;
5pub const requires_macos_sdk = false;
6
7pub fn build(b: *std.Build) void {
8 const test_step = b.step("test", "Test it");
9 b.default_step = test_step;
10
11 add(b, test_step, .Debug);
12 add(b, test_step, .ReleaseFast);
13 add(b, test_step, .ReleaseSmall);
14 add(b, test_step, .ReleaseSafe);
15}
16
17fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
18 const target: std.zig.CrossTarget = .{ .os_tag = .macos };
19
20 const lib = b.addSharedLibrary(.{
21 .name = "a",
22 .version = .{ .major = 1, .minor = 0, .patch = 0 },
23 .optimize = optimize,
24 .target = target,
25 });
26 lib.addCSourceFile("a.c", &.{});
27 lib.linkLibC();
28
29 const tbd_file = b.addWriteFile("liba.tbd",
30 \\--- !tapi-tbd-v3
31 \\archs: [ arm64, x86_64 ]
32 \\uuids: [ 'arm64: DEADBEEF', 'x86_64: BEEFDEAD' ]
33 \\platform: macos
34 \\install-name: @rpath/liba.dylib
35 \\current-version: 0
36 \\exports:
37 \\ - archs: [ arm64, x86_64 ]
38 \\ symbols: [ _getFoo ]
39 );
40
41 const exe = b.addExecutable(.{
42 .name = "test",
43 .optimize = optimize,
44 .target = target,
45 });
46 exe.addCSourceFile("main.c", &[0][]const u8{});
47 exe.linkSystemLibrary("a");
48 exe.addLibraryPathDirectorySource(tbd_file.getDirectorySource());
49 exe.addRPathDirectorySource(lib.getOutputDirectorySource());
50 exe.linkLibC();
51
52 const run = b.addRunArtifact(exe);
53 run.skip_foreign_checks = true;
54 run.expectExitCode(0);
55
56 test_step.dependOn(&run.step);
57}
test/link/macho/tbdv3/main.c created+7
...@@ -0,0 +1,7 @@
1#include <stdio.h>
2
3int getFoo();
4
5int main() {
6 return getFoo() - 42;
7}