Skip to content

Commit

Permalink
macho: don't embed codesig unless targeting aarch64-macos
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kubkon committed Aug 15, 2021
1 parent e9bf801 commit f82c26e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/link/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ d_sym: ?DebugSymbols = null,
/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
page_size: u16,

/// TODO Should we figure out embedding code signatures for other Apple platforms as part of the linker?
/// Or should this be a separate tool?
/// https://github.com/ziglang/zig/issues/9567
requires_adhoc_codesig: bool,

/// We commit 0x1000 = 4096 bytes of space to the header and
/// the table of load commands. This should be plenty for any
/// potential future extensions.
Expand Down Expand Up @@ -400,6 +405,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
.file = null,
},
.page_size = if (options.target.cpu.arch == .aarch64) 0x4000 else 0x1000,
.requires_adhoc_codesig = options.target.cpu.arch == .aarch64 and options.target.os.tag == .macos,
};

return self;
Expand Down Expand Up @@ -459,7 +465,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
try ds.flushModule(self.base.allocator, self.base.options);
}

if (target.cpu.arch == .aarch64) {
if (self.requires_adhoc_codesig) {
// Preallocate space for the code signature.
// We need to do this at this stage so that we have the load commands with proper values
// written out to the file.
Expand Down Expand Up @@ -492,11 +498,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
assert(!self.strtab_dirty);
assert(!self.strtab_needs_relocation);

if (target.cpu.arch == .aarch64) {
switch (output_mode) {
.Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
else => {},
}
if (self.requires_adhoc_codesig) {
try self.writeCodeSignature(); // code signing always comes last
}
}

Expand Down Expand Up @@ -2841,7 +2844,7 @@ fn addDataInCodeLC(self: *MachO) !void {
}

fn addCodeSignatureLC(self: *MachO) !void {
if (self.code_signature_cmd_index == null and self.base.options.target.cpu.arch == .aarch64) {
if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) {
self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
try self.load_commands.append(self.base.allocator, .{
.LinkeditData = .{
Expand Down Expand Up @@ -2935,14 +2938,14 @@ fn flushZld(self: *MachO) !void {
seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);
}

if (self.base.options.target.cpu.arch == .aarch64) {
if (self.requires_adhoc_codesig) {
try self.writeCodeSignaturePadding();
}

try self.writeLoadCommands();
try self.writeHeader();

if (self.base.options.target.cpu.arch == .aarch64) {
if (self.requires_adhoc_codesig) {
try self.writeCodeSignature();
}
}
Expand Down Expand Up @@ -4454,7 +4457,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });
self.load_commands_dirty = true;
}
if (self.code_signature_cmd_index == null) {
if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) {
self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
try self.load_commands.append(self.base.allocator, .{
.LinkeditData = .{
Expand Down Expand Up @@ -5719,8 +5722,8 @@ fn writeStringTableZld(self: *MachO) !void {

try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff);

if (symtab.strsize > self.strtab.items.len and self.base.options.target.cpu.arch == .x86_64) {
// This is the last section, so we need to pad it out.
if (symtab.strsize > self.strtab.items.len) {
// This is potentially the last section, so we need to pad it out.
try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1);
}
}
Expand Down

0 comments on commit f82c26e

Please sign in to comment.