diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-12-08 12:47:48 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-12-08 12:47:48 -0700 |
| commit | a482b82e2d4fdcd0a9a8683e403ced789c7f98a3 (patch) | |
| tree | 474029faeaf703387232fb8c12d233b864a00c42 /src | |
| parent | 97c0e1cc41c24c6cbb60117751d5b82dcd9d0e43 (diff) | |
| download | zig-a482b82e2d4fdcd0a9a8683e403ced789c7f98a3.tar.gz zig-a482b82e2d4fdcd0a9a8683e403ced789c7f98a3.zip | |
stage2: only patch up LLD binaries on aarch64-macos
The code is a bit fragile so it was causing CI failures on x86_64-macos.
Also the patch up code is only needed for aarch64-macos, so we were
doing unnecessary work.
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/MachO.zig | 75 |
1 files changed, 39 insertions, 36 deletions
diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 5379b536ef..1904b2cf5e 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -725,43 +725,46 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { // At this stage, LLD has done its job. It is time to patch the resultant // binaries up! - const out_file = try directory.handle.openFile(self.base.options.emit.?.sub_path, .{ .write = true }); - try self.parseFromFile(out_file); - if (self.code_signature_cmd_index == null) { - const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; - const text_section = text_segment.sections.items[self.text_section_index.?]; - const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64); - const needed_size = @sizeOf(macho.linkedit_data_command); - if (needed_size + after_last_cmd_offset > text_section.offset) { - // TODO We are in the position to be able to increase the padding by moving all sections - // by the required offset, but this requires a little bit more thinking and bookkeeping. - // For now, return an error informing the user of the problem. - std.log.err("Not enough padding between load commands and start of __text section:\n", .{}); - std.log.err("Offset after last load command: 0x{x}\n", .{after_last_cmd_offset}); - std.log.err("Beginning of __text section: 0x{x}\n", .{text_section.offset}); - std.log.err("Needed size: 0x{x}\n", .{needed_size}); - return error.NotEnoughPadding; + // This is currently needed only for aarch64 targets. + if (target.cpu.arch == .aarch64) { + const out_file = try directory.handle.openFile(self.base.options.emit.?.sub_path, .{ .write = true }); + try self.parseFromFile(out_file); + if (self.code_signature_cmd_index == null) { + const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; + const text_section = text_segment.sections.items[self.text_section_index.?]; + const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64); + const needed_size = @sizeOf(macho.linkedit_data_command); + if (needed_size + after_last_cmd_offset > text_section.offset) { + // TODO We are in the position to be able to increase the padding by moving all sections + // by the required offset, but this requires a little bit more thinking and bookkeeping. + // For now, return an error informing the user of the problem. + std.log.err("Not enough padding between load commands and start of __text section:\n", .{}); + std.log.err("Offset after last load command: 0x{x}\n", .{after_last_cmd_offset}); + std.log.err("Beginning of __text section: 0x{x}\n", .{text_section.offset}); + std.log.err("Needed size: 0x{x}\n", .{needed_size}); + return error.NotEnoughPadding; + } + const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment; + // TODO This is clunky. + self.linkedit_segment_next_offset = @intCast(u32, mem.alignForwardGeneric(u64, linkedit_segment.inner.fileoff + linkedit_segment.inner.filesize, @sizeOf(u64))); + // Add code signature load command + self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len); + try self.load_commands.append(self.base.allocator, .{ + .LinkeditData = .{ + .cmd = macho.LC_CODE_SIGNATURE, + .cmdsize = @sizeOf(macho.linkedit_data_command), + .dataoff = 0, + .datasize = 0, + }, + }); + // Pad out space for code signature + try self.writeCodeSignaturePadding(); + // Write updated load commands and the header + try self.writeLoadCommands(); + try self.writeHeader(); + // Generate adhoc code signature + try self.writeCodeSignature(); } - const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment; - // TODO This is clunky. - self.linkedit_segment_next_offset = @intCast(u32, mem.alignForwardGeneric(u64, linkedit_segment.inner.fileoff + linkedit_segment.inner.filesize, @sizeOf(u64))); - // Add code signature load command - self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len); - try self.load_commands.append(self.base.allocator, .{ - .LinkeditData = .{ - .cmd = macho.LC_CODE_SIGNATURE, - .cmdsize = @sizeOf(macho.linkedit_data_command), - .dataoff = 0, - .datasize = 0, - }, - }); - // Pad out space for code signature - try self.writeCodeSignaturePadding(); - // Write updated load commands and the header - try self.writeLoadCommands(); - try self.writeHeader(); - // Generate adhoc code signature - try self.writeCodeSignature(); } } } |
