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,
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.
......@@ -391,6 +396,13 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
391396
392397pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
393398 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
395407 self.* = .{
396408 .base = .{
......@@ -399,7 +411,8 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
399411 .allocator = gpa,
400412 .file = null,
401413 },
402 .page_size = if (options.target.cpu.arch == .aarch64) 0x4000 else 0x1000,
414 .page_size = page_size,
415 .requires_adhoc_codesig = requires_adhoc_codesig,
403416 };
404417
405418 return self;
......@@ -811,7 +824,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
811824 defer tracy.end();
812825
813826 const output_mode = self.base.options.output_mode;
814 const target = self.base.options.target;
815827
816828 switch (output_mode) {
817829 .Exe => {
......@@ -837,7 +849,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
837849 try ds.flushModule(self.base.allocator, self.base.options);
838850 }
839851
840 if (target.cpu.arch == .aarch64) {
852 if (self.requires_adhoc_codesig) {
841853 // Preallocate space for the code signature.
842854 // We need to do this at this stage so that we have the load commands with proper values
843855 // written out to the file.
......@@ -870,11 +882,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
870882 assert(!self.strtab_dirty);
871883 assert(!self.strtab_needs_relocation);
872884
873 if (target.cpu.arch == .aarch64) {
874 switch (output_mode) {
875 .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
876 else => {},
877 }
885 if (self.requires_adhoc_codesig) {
886 try self.writeCodeSignature(); // code signing always comes last
878887 }
879888}
880889
......@@ -2831,7 +2840,7 @@ fn addDataInCodeLC(self: *MachO) !void {
28312840}
28322841
28332842fn 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) {
28352844 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
28362845 try self.load_commands.append(self.base.allocator, .{
28372846 .LinkeditData = .{
......@@ -2925,14 +2934,14 @@ fn flushZld(self: *MachO) !void {
29252934 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);
29262935 }
29272936
2928 if (self.base.options.target.cpu.arch == .aarch64) {
2937 if (self.requires_adhoc_codesig) {
29292938 try self.writeCodeSignaturePadding();
29302939 }
29312940
29322941 try self.writeLoadCommands();
29332942 try self.writeHeader();
29342943
2935 if (self.base.options.target.cpu.arch == .aarch64) {
2944 if (self.requires_adhoc_codesig) {
29362945 try self.writeCodeSignature();
29372946 }
29382947}
......@@ -4444,7 +4453,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
44444453 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });
44454454 self.load_commands_dirty = true;
44464455 }
4447 if (self.code_signature_cmd_index == null) {
4456 if (self.code_signature_cmd_index == null and self.requires_adhoc_codesig) {
44484457 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
44494458 try self.load_commands.append(self.base.allocator, .{
44504459 .LinkeditData = .{
......@@ -5709,8 +5718,8 @@ fn writeStringTableZld(self: *MachO) !void {
57095718
57105719 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) {
5713 // This is the last section, so we need to pad it out.
5721 if (symtab.strsize > self.strtab.items.len) {
5722 // This is potentially the last section, so we need to pad it out.
57145723 try self.base.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1);
57155724 }
57165725}