| ... | @@ -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. |
| 55 | page_size: u16, | 55 | page_size: u16, |
| 56 | | 56 | |
| | 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 |
| | 60 | requires_adhoc_codesig: bool, |
| | 61 | |
| 57 | /// We commit 0x1000 = 4096 bytes of space to the header and | 62 | /// We commit 0x1000 = 4096 bytes of space to the header and |
| 58 | /// the table of load commands. This should be plenty for any | 63 | /// the table of load commands. This should be plenty for any |
| 59 | /// potential future extensions. | 64 | /// potential future extensions. |
| ... | @@ -400,6 +405,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO { | ... | @@ -400,6 +405,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO { |
| 400 | .file = null, | 405 | .file = null, |
| 401 | }, | 406 | }, |
| 402 | .page_size = if (options.target.cpu.arch == .aarch64) 0x4000 else 0x1000, | 407 | .page_size = if (options.target.cpu.arch == .aarch64) 0x4000 else 0x1000, |
| | 408 | .requires_adhoc_codesig = options.target.cpu.arch == .aarch64 and options.target.os.tag == .macos, |
| 403 | }; | 409 | }; |
| 404 | | 410 | |
| 405 | return self; | 411 | return self; |
| ... | @@ -459,7 +465,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { | ... | @@ -459,7 +465,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { |
| 459 | try ds.flushModule(self.base.allocator, self.base.options); | 465 | try ds.flushModule(self.base.allocator, self.base.options); |
| 460 | } | 466 | } |
| 461 | | 467 | |
| 462 | if (target.cpu.arch == .aarch64) { | 468 | if (self.requires_adhoc_codesig) { |
| 463 | // Preallocate space for the code signature. | 469 | // Preallocate space for the code signature. |
| 464 | // We need to do this at this stage so that we have the load commands with proper values | 470 | // We need to do this at this stage so that we have the load commands with proper values |
| 465 | // written out to the file. | 471 | // written out to the file. |
| ... | @@ -492,11 +498,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { | ... | @@ -492,11 +498,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { |
| 492 | assert(!self.strtab_dirty); | 498 | assert(!self.strtab_dirty); |
| 493 | assert(!self.strtab_needs_relocation); | 499 | assert(!self.strtab_needs_relocation); |
| 494 | | 500 | |
| 495 | if (target.cpu.arch == .aarch64) { | 501 | if (self.requires_adhoc_codesig) { |
| 496 | switch (output_mode) { | 502 | try self.writeCodeSignature(); // code signing always comes last |
| 497 | .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last | | |
| 498 | else => {}, | | |
| 499 | } | | |
| 500 | } | 503 | } |
| 501 | } | 504 | } |
| 502 | | 505 | |
| ... | @@ -2841,7 +2844,7 @@ fn addDataInCodeLC(self: *MachO) !void { | ... | @@ -2841,7 +2844,7 @@ fn addDataInCodeLC(self: *MachO) !void { |
| 2841 | } | 2844 | } |
| 2842 | | 2845 | |
| 2843 | fn addCodeSignatureLC(self: *MachO) !void { | 2846 | fn addCodeSignatureLC(self: *MachO) !void { |
| 2844 | if (self.code_signature_cmd_index == null and self.base.options.target.cpu.arch == .aarch64) { | 2847 | if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) { |
| 2845 | self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len); | 2848 | self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len); |
| 2846 | try self.load_commands.append(self.base.allocator, .{ | 2849 | try self.load_commands.append(self.base.allocator, .{ |
| 2847 | .LinkeditData = .{ | 2850 | .LinkeditData = .{ |
| ... | @@ -2935,14 +2938,14 @@ fn flushZld(self: *MachO) !void { | ... | @@ -2935,14 +2938,14 @@ fn flushZld(self: *MachO) !void { |
| 2935 | seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size); | 2938 | seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size); |
| 2936 | } | 2939 | } |
| 2937 | | 2940 | |
| 2938 | if (self.base.options.target.cpu.arch == .aarch64) { | 2941 | if (self.requires_adhoc_codesig) { |
| 2939 | try self.writeCodeSignaturePadding(); | 2942 | try self.writeCodeSignaturePadding(); |
| 2940 | } | 2943 | } |
| 2941 | | 2944 | |
| 2942 | try self.writeLoadCommands(); | 2945 | try self.writeLoadCommands(); |
| 2943 | try self.writeHeader(); | 2946 | try self.writeHeader(); |
| 2944 | | 2947 | |
| 2945 | if (self.base.options.target.cpu.arch == .aarch64) { | 2948 | if (self.requires_adhoc_codesig) { |
| 2946 | try self.writeCodeSignature(); | 2949 | try self.writeCodeSignature(); |
| 2947 | } | 2950 | } |
| 2948 | } | 2951 | } |
| ... | @@ -4454,7 +4457,7 @@ pub fn populateMissingMetadata(self: *MachO) !void { | ... | @@ -4454,7 +4457,7 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 4454 | try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd }); | 4457 | try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd }); |
| 4455 | self.load_commands_dirty = true; | 4458 | self.load_commands_dirty = true; |
| 4456 | } | 4459 | } |
| 4457 | if (self.code_signature_cmd_index == null) { | 4460 | if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) { |
| 4458 | self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len); | 4461 | self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len); |
| 4459 | try self.load_commands.append(self.base.allocator, .{ | 4462 | try self.load_commands.append(self.base.allocator, .{ |
| 4460 | .LinkeditData = .{ | 4463 | .LinkeditData = .{ |
| ... | @@ -5719,8 +5722,8 @@ fn writeStringTableZld(self: *MachO) !void { | ... | @@ -5719,8 +5722,8 @@ fn writeStringTableZld(self: *MachO) !void { |
| 5719 | | 5722 | |
| 5720 | try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff); | 5723 | try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff); |
| 5721 | | 5724 | |
| 5722 | if (symtab.strsize > self.strtab.items.len and self.base.options.target.cpu.arch == .x86_64) { | 5725 | if (symtab.strsize > self.strtab.items.len) { |
| 5723 | // This is the last section, so we need to pad it out. | 5726 | // This is potentially the last section, so we need to pad it out. |
| 5724 | try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1); | 5727 | try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1); |
| 5725 | } | 5728 | } |
| 5726 | } | 5729 | } |