authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-16 15:19:11+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-16 15:24:20+02:00
log4c90c1ff63883c2bb8d2baa40e952674daba6814
treecfb64a43da8ee99a41d7ff953def54432e0c2b88
parentb20b6d7da9b63783a4f481fd0a4b48a47843abc8
parent4c9d41730e66b253d12472d616e3a519c79a41cb

Merge remote-tracking branch 'origin/master' into zld-incr


1 files changed, 23 insertions(+), 14 deletions(-)

src/link/MachO.zig+23-14
...@@ -54,6 +54,11 @@ d_sym: ?DebugSymbols = null,...@@ -54,6 +54,11 @@ d_sym: ?DebugSymbols = null,
54/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.54/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
55page_size: u16,55page_size: u16,
5656
57/// TODO Should we figure out embedding code signatures for other Apple platforms as part of the linker?
58/// Or should this be a separate tool?
59/// https://github.com/ziglang/zig/issues/9567
60requires_adhoc_codesig: bool,
61
57/// We commit 0x1000 = 4096 bytes of space to the header and62/// We commit 0x1000 = 4096 bytes of space to the header and
58/// the table of load commands. This should be plenty for any63/// the table of load commands. This should be plenty for any
59/// potential future extensions.64/// potential future extensions.
...@@ -391,6 +396,13 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio...@@ -391,6 +396,13 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
391396
392pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {397pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
393 const self = try gpa.create(MachO);398 const self = try gpa.create(MachO);
399 const cpu_arch = options.target.cpu.arch;
400 const os_tag = options.target.os.tag;
401 const abi = options.target.abi;
402 const page_size: u16 = if (cpu_arch == .aarch64) 0x4000 else 0x1000;
403 // Adhoc code signature is required when targeting aarch64-macos either directly or indirectly via the simulator
404 // ABI such as aarch64-ios-simulator, etc.
405 const requires_adhoc_codesig = cpu_arch == .aarch64 and (os_tag == .macos or abi == .simulator);
394406
395 self.* = .{407 self.* = .{
396 .base = .{408 .base = .{
...@@ -399,7 +411,8 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {...@@ -399,7 +411,8 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
399 .allocator = gpa,411 .allocator = gpa,
400 .file = null,412 .file = null,
401 },413 },
402 .page_size = if (options.target.cpu.arch == .aarch64) 0x4000 else 0x1000,414 .page_size = page_size,
415 .requires_adhoc_codesig = requires_adhoc_codesig,
403 };416 };
404417
405 return self;418 return self;
...@@ -811,7 +824,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -811,7 +824,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
811 defer tracy.end();824 defer tracy.end();
812825
813 const output_mode = self.base.options.output_mode;826 const output_mode = self.base.options.output_mode;
814 const target = self.base.options.target;
815827
816 switch (output_mode) {828 switch (output_mode) {
817 .Exe => {829 .Exe => {
...@@ -837,7 +849,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -837,7 +849,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
837 try ds.flushModule(self.base.allocator, self.base.options);849 try ds.flushModule(self.base.allocator, self.base.options);
838 }850 }
839851
840 if (target.cpu.arch == .aarch64) {852 if (self.requires_adhoc_codesig) {
841 // Preallocate space for the code signature.853 // Preallocate space for the code signature.
842 // We need to do this at this stage so that we have the load commands with proper values854 // We need to do this at this stage so that we have the load commands with proper values
843 // written out to the file.855 // written out to the file.
...@@ -870,11 +882,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -870,11 +882,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
870 assert(!self.strtab_dirty);882 assert(!self.strtab_dirty);
871 assert(!self.strtab_needs_relocation);883 assert(!self.strtab_needs_relocation);
872884
873 if (target.cpu.arch == .aarch64) {885 if (self.requires_adhoc_codesig) {
874 switch (output_mode) {886 try self.writeCodeSignature(); // code signing always comes last
875 .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
876 else => {},
877 }
878 }887 }
879}888}
880889
...@@ -2831,7 +2840,7 @@ fn addDataInCodeLC(self: *MachO) !void {...@@ -2831,7 +2840,7 @@ fn addDataInCodeLC(self: *MachO) !void {
2831}2840}
28322841
2833fn addCodeSignatureLC(self: *MachO) !void {2842fn addCodeSignatureLC(self: *MachO) !void {
2834 if (self.code_signature_cmd_index == null and self.base.options.target.cpu.arch == .aarch64) {2843 if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) {
2835 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);2844 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
2836 try self.load_commands.append(self.base.allocator, .{2845 try self.load_commands.append(self.base.allocator, .{
2837 .LinkeditData = .{2846 .LinkeditData = .{
...@@ -2925,14 +2934,14 @@ fn flushZld(self: *MachO) !void {...@@ -2925,14 +2934,14 @@ fn flushZld(self: *MachO) !void {
2925 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);2934 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);
2926 }2935 }
29272936
2928 if (self.base.options.target.cpu.arch == .aarch64) {2937 if (self.requires_adhoc_codesig) {
2929 try self.writeCodeSignaturePadding();2938 try self.writeCodeSignaturePadding();
2930 }2939 }
29312940
2932 try self.writeLoadCommands();2941 try self.writeLoadCommands();
2933 try self.writeHeader();2942 try self.writeHeader();
29342943
2935 if (self.base.options.target.cpu.arch == .aarch64) {2944 if (self.requires_adhoc_codesig) {
2936 try self.writeCodeSignature();2945 try self.writeCodeSignature();
2937 }2946 }
2938}2947}
...@@ -4444,7 +4453,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -4444,7 +4453,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
4444 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });4453 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });
4445 self.load_commands_dirty = true;4454 self.load_commands_dirty = true;
4446 }4455 }
4447 if (self.code_signature_cmd_index == null) {4456 if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) {
4448 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);4457 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
4449 try self.load_commands.append(self.base.allocator, .{4458 try self.load_commands.append(self.base.allocator, .{
4450 .LinkeditData = .{4459 .LinkeditData = .{
...@@ -5709,8 +5718,8 @@ fn writeStringTableZld(self: *MachO) !void {...@@ -5709,8 +5718,8 @@ fn writeStringTableZld(self: *MachO) !void {
57095718
5710 try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff);5719 try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff);
57115720
5712 if (symtab.strsize > self.strtab.items.len and self.base.options.target.cpu.arch == .x86_64) {5721 if (symtab.strsize > self.strtab.items.len) {
5713 // This is the last section, so we need to pad it out.5722 // This is potentially the last section, so we need to pad it out.
5714 try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1);5723 try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1);
5715 }5724 }
5716}5725}