authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-15 18:07:30+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-15 18:07:30+02:00
logf82c26eb04590fc6083a3520b470333078cfc571
tree10007f0fe68fda9fbd6c4c8e06d17d7b07a90518
parente9bf8014bd29360353a9bfdff4aa9d5a45bc59f6

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

When developing an iOS app for example, the developer is required to use Apple's codesign utility to generate a valid signature as done by Xcode.

1 files changed, 15 insertions(+), 12 deletions(-)

src/link/MachO.zig+15-12
......@@ -54,6 +54,11 @@ d_sym: ?DebugSymbols = null,
5454/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
5555page_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
5762/// We commit 0x1000 = 4096 bytes of space to the header and
5863/// the table of load commands. This should be plenty for any
5964/// potential future extensions.
......@@ -400,6 +405,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
400405 .file = null,
401406 },
402407 .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,
403409 };
404410
405411 return self;
......@@ -459,7 +465,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
459465 try ds.flushModule(self.base.allocator, self.base.options);
460466 }
461467
462 if (target.cpu.arch == .aarch64) {
468 if (self.requires_adhoc_codesig) {
463469 // Preallocate space for the code signature.
464470 // We need to do this at this stage so that we have the load commands with proper values
465471 // written out to the file.
......@@ -492,11 +498,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
492498 assert(!self.strtab_dirty);
493499 assert(!self.strtab_needs_relocation);
494500
495 if (target.cpu.arch == .aarch64) {
496 switch (output_mode) {
497 .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
498 else => {},
499 }
501 if (self.requires_adhoc_codesig) {
502 try self.writeCodeSignature(); // code signing always comes last
500503 }
501504}
502505
......@@ -2841,7 +2844,7 @@ fn addDataInCodeLC(self: *MachO) !void {
28412844}
28422845
28432846fn 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) {
28452848 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
28462849 try self.load_commands.append(self.base.allocator, .{
28472850 .LinkeditData = .{
......@@ -2935,14 +2938,14 @@ fn flushZld(self: *MachO) !void {
29352938 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);
29362939 }
29372940
2938 if (self.base.options.target.cpu.arch == .aarch64) {
2941 if (self.requires_adhoc_codesig) {
29392942 try self.writeCodeSignaturePadding();
29402943 }
29412944
29422945 try self.writeLoadCommands();
29432946 try self.writeHeader();
29442947
2945 if (self.base.options.target.cpu.arch == .aarch64) {
2948 if (self.requires_adhoc_codesig) {
29462949 try self.writeCodeSignature();
29472950 }
29482951}
......@@ -4454,7 +4457,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
44544457 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });
44554458 self.load_commands_dirty = true;
44564459 }
4457 if (self.code_signature_cmd_index == null) {
4460 if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) {
44584461 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
44594462 try self.load_commands.append(self.base.allocator, .{
44604463 .LinkeditData = .{
......@@ -5719,8 +5722,8 @@ fn writeStringTableZld(self: *MachO) !void {
57195722
57205723 try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff);
57215724
5722 if (symtab.strsize > self.strtab.items.len and self.base.options.target.cpu.arch == .x86_64) {
5723 // This is the last section, so we need to pad it out.
5725 if (symtab.strsize > self.strtab.items.len) {
5726 // This is potentially the last section, so we need to pad it out.
57245727 try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1);
57255728 }
57265729}