authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-16 10:17:54+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-08-16 10:17:54+02:00
log4c9d41730e66b253d12472d616e3a519c79a41cb
treec6dc8365698dbdabe0bd8c22b67b3c1739410512
parente9bf8014bd29360353a9bfdff4aa9d5a45bc59f6
parente2303840de713e34fe6b2b16c5e9866e6dad9080
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9568 from ziglang/issue-9565

macho: don't embed codesig unless targeting aarch64-macos

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;
...@@ -433,7 +446,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -433,7 +446,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
433 defer tracy.end();446 defer tracy.end();
434447
435 const output_mode = self.base.options.output_mode;448 const output_mode = self.base.options.output_mode;
436 const target = self.base.options.target;
437449
438 switch (output_mode) {450 switch (output_mode) {
439 .Exe => {451 .Exe => {
...@@ -459,7 +471,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -459,7 +471,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
459 try ds.flushModule(self.base.allocator, self.base.options);471 try ds.flushModule(self.base.allocator, self.base.options);
460 }472 }
461473
462 if (target.cpu.arch == .aarch64) {474 if (self.requires_adhoc_codesig) {
463 // Preallocate space for the code signature.475 // Preallocate space for the code signature.
464 // We need to do this at this stage so that we have the load commands with proper values476 // We need to do this at this stage so that we have the load commands with proper values
465 // written out to the file.477 // written out to the file.
...@@ -492,11 +504,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -492,11 +504,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
492 assert(!self.strtab_dirty);504 assert(!self.strtab_dirty);
493 assert(!self.strtab_needs_relocation);505 assert(!self.strtab_needs_relocation);
494506
495 if (target.cpu.arch == .aarch64) {507 if (self.requires_adhoc_codesig) {
496 switch (output_mode) {508 try self.writeCodeSignature(); // code signing always comes last
497 .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
498 else => {},
499 }
500 }509 }
501}510}
502511
...@@ -2841,7 +2850,7 @@ fn addDataInCodeLC(self: *MachO) !void {...@@ -2841,7 +2850,7 @@ fn addDataInCodeLC(self: *MachO) !void {
2841}2850}
28422851
2843fn addCodeSignatureLC(self: *MachO) !void {2852fn addCodeSignatureLC(self: *MachO) !void {
2844 if (self.code_signature_cmd_index == null and self.base.options.target.cpu.arch == .aarch64) {2853 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);2854 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
2846 try self.load_commands.append(self.base.allocator, .{2855 try self.load_commands.append(self.base.allocator, .{
2847 .LinkeditData = .{2856 .LinkeditData = .{
...@@ -2935,14 +2944,14 @@ fn flushZld(self: *MachO) !void {...@@ -2935,14 +2944,14 @@ fn flushZld(self: *MachO) !void {
2935 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);2944 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);
2936 }2945 }
29372946
2938 if (self.base.options.target.cpu.arch == .aarch64) {2947 if (self.requires_adhoc_codesig) {
2939 try self.writeCodeSignaturePadding();2948 try self.writeCodeSignaturePadding();
2940 }2949 }
29412950
2942 try self.writeLoadCommands();2951 try self.writeLoadCommands();
2943 try self.writeHeader();2952 try self.writeHeader();
29442953
2945 if (self.base.options.target.cpu.arch == .aarch64) {2954 if (self.requires_adhoc_codesig) {
2946 try self.writeCodeSignature();2955 try self.writeCodeSignature();
2947 }2956 }
2948}2957}
...@@ -4454,7 +4463,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -4454,7 +4463,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
4454 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });4463 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });
4455 self.load_commands_dirty = true;4464 self.load_commands_dirty = true;
4456 }4465 }
4457 if (self.code_signature_cmd_index == null) {4466 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);4467 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
4459 try self.load_commands.append(self.base.allocator, .{4468 try self.load_commands.append(self.base.allocator, .{
4460 .LinkeditData = .{4469 .LinkeditData = .{
...@@ -5719,8 +5728,8 @@ fn writeStringTableZld(self: *MachO) !void {...@@ -5719,8 +5728,8 @@ fn writeStringTableZld(self: *MachO) !void {
57195728
5720 try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff);5729 try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff);
57215730
5722 if (symtab.strsize > self.strtab.items.len and self.base.options.target.cpu.arch == .x86_64) {5731 if (symtab.strsize > self.strtab.items.len) {
5723 // This is the last section, so we need to pad it out.5732 // 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);5733 try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1);
5725 }5734 }
5726}5735}