authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-03 13:41:31-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-03 13:41:31-08:00
logecf7dfcd3a3e575ee525487687a0b7607a6a7481
tree56cf2bfa237e4fdb0f13230983184e86b2f9b501
parenta2cd9dc3bd1e6a5f4e6a3c593b05b0f7bd2943ce
parentd3be4992708e7e6631df52fb5f4ccb0f521a77de
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7273 from kubkon/lld-codesig-poc

lld+macho: patch lld output on Apple Silicon by calculating and embedding adhoc code signature

6 files changed, 678 insertions(+), 286 deletions(-)

src/codegen.zig+4-2
......@@ -1795,7 +1795,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
17951795 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
17961796 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
17971797 const func = func_val.func;
1798 const got = &macho_file.sections.items[macho_file.got_section_index.?];
1798 const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
1799 const got = &text_segment.sections.items[macho_file.got_section_index.?];
17991800 const got_addr = got.addr + func.owner_decl.link.macho.offset_table_index * @sizeOf(u64);
18001801 switch (arch) {
18011802 .x86_64 => {
......@@ -3196,7 +3197,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
31963197 return MCValue{ .memory = got_addr };
31973198 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
31983199 const decl = payload.decl;
3199 const got = &macho_file.sections.items[macho_file.got_section_index.?];
3200 const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
3201 const got = &text_segment.sections.items[macho_file.got_section_index.?];
32003202 const got_addr = got.addr + decl.link.macho.offset_table_index * ptr_bytes;
32013203 return MCValue{ .memory = got_addr };
32023204 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
src/link.zig+8
......@@ -238,6 +238,14 @@ pub const File = struct {
238238 }
239239
240240 pub fn makeExecutable(base: *File) !void {
241 switch (base.options.output_mode) {
242 .Obj => return,
243 .Lib => switch (base.options.link_mode) {
244 .Static => return,
245 .Dynamic => {},
246 },
247 .Exe => {},
248 }
241249 switch (base.tag) {
242250 .macho => if (base.file) |f| {
243251 if (base.intermediary_basename != null) {
src/link/MachO.zig+268-259
......@@ -24,55 +24,9 @@ const target_util = @import("../target.zig");
2424const Trie = @import("MachO/Trie.zig");
2525const CodeSignature = @import("MachO/CodeSignature.zig");
2626
27pub const base_tag: File.Tag = File.Tag.macho;
28
29const LoadCommand = union(enum) {
30 Segment: macho.segment_command_64,
31 LinkeditData: macho.linkedit_data_command,
32 Symtab: macho.symtab_command,
33 Dysymtab: macho.dysymtab_command,
34 DyldInfo: macho.dyld_info_command,
35 Dylinker: macho.dylinker_command,
36 Dylib: macho.dylib_command,
37 EntryPoint: macho.entry_point_command,
38 MinVersion: macho.version_min_command,
39 SourceVersion: macho.source_version_command,
40
41 pub fn cmdsize(self: LoadCommand) u32 {
42 return switch (self) {
43 .Segment => |x| x.cmdsize,
44 .LinkeditData => |x| x.cmdsize,
45 .Symtab => |x| x.cmdsize,
46 .Dysymtab => |x| x.cmdsize,
47 .DyldInfo => |x| x.cmdsize,
48 .Dylinker => |x| x.cmdsize,
49 .Dylib => |x| x.cmdsize,
50 .EntryPoint => |x| x.cmdsize,
51 .MinVersion => |x| x.cmdsize,
52 .SourceVersion => |x| x.cmdsize,
53 };
54 }
55
56 pub fn write(self: LoadCommand, file: *fs.File, offset: u64) !void {
57 return switch (self) {
58 .Segment => |cmd| writeGeneric(cmd, file, offset),
59 .LinkeditData => |cmd| writeGeneric(cmd, file, offset),
60 .Symtab => |cmd| writeGeneric(cmd, file, offset),
61 .Dysymtab => |cmd| writeGeneric(cmd, file, offset),
62 .DyldInfo => |cmd| writeGeneric(cmd, file, offset),
63 .Dylinker => |cmd| writeGeneric(cmd, file, offset),
64 .Dylib => |cmd| writeGeneric(cmd, file, offset),
65 .EntryPoint => |cmd| writeGeneric(cmd, file, offset),
66 .MinVersion => |cmd| writeGeneric(cmd, file, offset),
67 .SourceVersion => |cmd| writeGeneric(cmd, file, offset),
68 };
69 }
27usingnamespace @import("MachO/commands.zig");
7028
71 fn writeGeneric(cmd: anytype, file: *fs.File, offset: u64) !void {
72 const slice = [1]@TypeOf(cmd){cmd};
73 return file.pwriteAll(mem.sliceAsBytes(slice[0..1]), offset);
74 }
75};
29pub const base_tag: File.Tag = File.Tag.macho;
7630
7731base: File,
7832
......@@ -80,6 +34,9 @@ base: File,
8034/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
8135page_size: u16,
8236
37/// Mach-O header
38header: ?macho.mach_header_64 = null,
39
8340/// Table of all load commands
8441load_commands: std.ArrayListUnmanaged(LoadCommand) = .{},
8542/// __PAGEZERO segment
......@@ -115,18 +72,14 @@ source_version_cmd_index: ?u16 = null,
11572/// Code signature
11673code_signature_cmd_index: ?u16 = null,
11774
118/// Table of all sections
119sections: std.ArrayListUnmanaged(macho.section_64) = .{},
120
121/// __TEXT,__text section
75/// Index into __TEXT,__text section.
12276text_section_index: ?u16 = null,
123
124/// __DATA,__got section
77/// Index into __TEXT,__got section.
12578got_section_index: ?u16 = null,
126
79/// The absolute address of the entry point.
12780entry_addr: ?u64 = null,
12881
129// TODO move this into each Segment aggregator
82/// TODO move this into each Segment aggregator
13083linkedit_segment_next_offset: ?u32 = null,
13184
13285/// Table of all local symbols
......@@ -154,8 +107,6 @@ offset_table: std.ArrayListUnmanaged(u64) = .{},
154107error_flags: File.ErrorFlags = File.ErrorFlags{},
155108
156109cmd_table_dirty: bool = false,
157dylinker_cmd_dirty: bool = false,
158libsystem_cmd_dirty: bool = false,
159110
160111/// A list of text blocks that have surplus capacity. This list can have false
161112/// positives, as functions grow and shrink over time, only sometimes being added
......@@ -355,40 +306,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
355306 if (self.entry_addr) |addr| {
356307 // Update LC_MAIN with entry offset.
357308 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
358 const main_cmd = &self.load_commands.items[self.main_cmd_index.?].EntryPoint;
359 main_cmd.entryoff = addr - text_segment.vmaddr;
360 }
361 if (self.dylinker_cmd_dirty) {
362 // Write path to dyld loader.
363 var off: usize = @sizeOf(macho.mach_header_64);
364 for (self.load_commands.items) |cmd| {
365 if (cmd == .Dylinker) break;
366 off += cmd.cmdsize();
367 }
368 const cmd = &self.load_commands.items[self.dylinker_cmd_index.?].Dylinker;
369 off += cmd.name;
370 log.debug("writing LC_LOAD_DYLINKER path to dyld at 0x{x}\n", .{off});
371 try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), off);
372 self.dylinker_cmd_dirty = false;
373 }
374 if (self.libsystem_cmd_dirty) {
375 // Write path to libSystem.
376 var off: usize = @sizeOf(macho.mach_header_64);
377 for (self.load_commands.items) |cmd| {
378 if (cmd == .Dylib) break;
379 off += cmd.cmdsize();
380 }
381 const cmd = &self.load_commands.items[self.libsystem_cmd_index.?].Dylib;
382 off += cmd.dylib.name;
383 log.debug("writing LC_LOAD_DYLIB path to libSystem at 0x{x}\n", .{off});
384 try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), off);
385 self.libsystem_cmd_dirty = false;
309 const main_cmd = &self.load_commands.items[self.main_cmd_index.?].Main;
310 main_cmd.entryoff = addr - text_segment.inner.vmaddr;
386311 }
387
388312 try self.writeExportTrie();
389313 try self.writeSymbolTable();
390314 try self.writeStringTable();
391
392315 // Preallocate space for the code signature.
393316 // We need to do this at this stage so that we have the load commands with proper values
394317 // written out to the file.
......@@ -401,8 +324,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
401324 }
402325
403326 if (self.cmd_table_dirty) {
404 try self.writeCmdHeaders();
405 try self.writeMachOHeader();
327 try self.writeLoadCommands();
328 try self.writeHeader();
406329 self.cmd_table_dirty = false;
407330 }
408331
......@@ -415,8 +338,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
415338 }
416339
417340 assert(!self.cmd_table_dirty);
418 assert(!self.dylinker_cmd_dirty);
419 assert(!self.libsystem_cmd_dirty);
420341
421342 switch (self.base.options.output_mode) {
422343 .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
......@@ -760,7 +681,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
760681 if (result.term != .Exited or result.term.Exited != 0) {
761682 // TODO parse this output and surface with the Compilation API rather than
762683 // directly outputting to stderr here.
763 std.debug.print("{}", .{result.stderr});
684 std.log.err("{}", .{result.stderr});
764685 return error.LDReportedFailure;
765686 }
766687 } else {
......@@ -795,12 +716,53 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
795716 if (!ok) {
796717 // TODO parse this output and surface with the Compilation API rather than
797718 // directly outputting to stderr here.
798 std.debug.print("{}", .{stderr_context.data.items});
719 std.log.err("{}", .{stderr_context.data.items});
799720 return error.LLDReportedFailure;
800721 }
801722 if (stderr_context.data.items.len != 0) {
802723 std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items});
803724 }
725
726 // At this stage, LLD has done its job. It is time to patch the resultant
727 // binaries up!
728 const out_file = try directory.handle.openFile(self.base.options.emit.?.sub_path, .{ .write = true });
729 try self.parseFromFile(out_file);
730 if (self.code_signature_cmd_index == null) {
731 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
732 const text_section = text_segment.sections.items[self.text_section_index.?];
733 const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64);
734 const needed_size = @sizeOf(macho.linkedit_data_command);
735 if (needed_size + after_last_cmd_offset > text_section.offset) {
736 // TODO We are in the position to be able to increase the padding by moving all sections
737 // by the required offset, but this requires a little bit more thinking and bookkeeping.
738 // For now, return an error informing the user of the problem.
739 std.log.err("Not enough padding between load commands and start of __text section:\n", .{});
740 std.log.err("Offset after last load command: 0x{x}\n", .{after_last_cmd_offset});
741 std.log.err("Beginning of __text section: 0x{x}\n", .{text_section.offset});
742 std.log.err("Needed size: 0x{x}\n", .{needed_size});
743 return error.NotEnoughPadding;
744 }
745 const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
746 // TODO This is clunky.
747 self.linkedit_segment_next_offset = @intCast(u32, mem.alignForwardGeneric(u64, linkedit_segment.inner.fileoff + linkedit_segment.inner.filesize, @sizeOf(u64)));
748 // Add code signature load command
749 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
750 try self.load_commands.append(self.base.allocator, .{
751 .LinkeditData = .{
752 .cmd = macho.LC_CODE_SIGNATURE,
753 .cmdsize = @sizeOf(macho.linkedit_data_command),
754 .dataoff = 0,
755 .datasize = 0,
756 },
757 });
758 // Pad out space for code signature
759 try self.writeCodeSignaturePadding();
760 // Write updated load commands and the header
761 try self.writeLoadCommands();
762 try self.writeHeader();
763 // Generate adhoc code signature
764 try self.writeCodeSignature();
765 }
804766 }
805767 }
806768
......@@ -857,7 +819,9 @@ pub fn deinit(self: *MachO) void {
857819 self.global_symbol_free_list.deinit(self.base.allocator);
858820 self.local_symbols.deinit(self.base.allocator);
859821 self.local_symbol_free_list.deinit(self.base.allocator);
860 self.sections.deinit(self.base.allocator);
822 for (self.load_commands.items) |*lc| {
823 lc.deinit(self.base.allocator);
824 }
861825 self.load_commands.deinit(self.base.allocator);
862826}
863827
......@@ -1011,7 +975,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
1011975 }
1012976
1013977 // Perform PIE fixups (if any)
1014 const got_section = self.sections.items[self.got_section_index.?];
978 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
979 const got_section = text_segment.sections.items[self.got_section_index.?];
1015980 while (self.pie_fixups.popOrNull()) |fixup| {
1016981 const target_addr = fixup.address;
1017982 const this_addr = symbol.n_value + fixup.start;
......@@ -1030,7 +995,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
1030995 }
1031996 }
1032997
1033 const text_section = self.sections.items[self.text_section_index.?];
998 const text_section = text_segment.sections.items[self.text_section_index.?];
1034999 const section_offset = symbol.n_value - text_section.addr;
10351000 const file_offset = text_section.offset + section_offset;
10361001 try self.base.file.?.pwriteAll(code, file_offset);
......@@ -1145,10 +1110,57 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11451110 .Lib => return error.TODOImplementWritingLibFiles,
11461111 }
11471112
1113 if (self.header == null) {
1114 var header: macho.mach_header_64 = undefined;
1115 header.magic = macho.MH_MAGIC_64;
1116
1117 const CpuInfo = struct {
1118 cpu_type: macho.cpu_type_t,
1119 cpu_subtype: macho.cpu_subtype_t,
1120 };
1121
1122 const cpu_info: CpuInfo = switch (self.base.options.target.cpu.arch) {
1123 .aarch64 => .{
1124 .cpu_type = macho.CPU_TYPE_ARM64,
1125 .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL,
1126 },
1127 .x86_64 => .{
1128 .cpu_type = macho.CPU_TYPE_X86_64,
1129 .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL,
1130 },
1131 else => return error.UnsupportedMachOArchitecture,
1132 };
1133 header.cputype = cpu_info.cpu_type;
1134 header.cpusubtype = cpu_info.cpu_subtype;
1135
1136 const filetype: u32 = switch (self.base.options.output_mode) {
1137 .Exe => macho.MH_EXECUTE,
1138 .Obj => macho.MH_OBJECT,
1139 .Lib => switch (self.base.options.link_mode) {
1140 .Static => return error.TODOStaticLibMachOType,
1141 .Dynamic => macho.MH_DYLIB,
1142 },
1143 };
1144 header.filetype = filetype;
1145 // These will get populated at the end of flushing the results to file.
1146 header.ncmds = 0;
1147 header.sizeofcmds = 0;
1148
1149 switch (self.base.options.output_mode) {
1150 .Exe => {
1151 header.flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE;
1152 },
1153 else => {
1154 header.flags = 0;
1155 },
1156 }
1157 header.reserved = 0;
1158 self.header = header;
1159 }
11481160 if (self.pagezero_segment_cmd_index == null) {
11491161 self.pagezero_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
11501162 try self.load_commands.append(self.base.allocator, .{
1151 .Segment = .{
1163 .Segment = SegmentCommand.empty(.{
11521164 .cmd = macho.LC_SEGMENT_64,
11531165 .cmdsize = @sizeOf(macho.segment_command_64),
11541166 .segname = makeStaticString("__PAGEZERO"),
......@@ -1160,7 +1172,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11601172 .initprot = 0,
11611173 .nsects = 0,
11621174 .flags = 0,
1163 },
1175 }),
11641176 });
11651177 self.cmd_table_dirty = true;
11661178 }
......@@ -1169,7 +1181,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11691181 const maxprot = macho.VM_PROT_READ | macho.VM_PROT_WRITE | macho.VM_PROT_EXECUTE;
11701182 const initprot = macho.VM_PROT_READ | macho.VM_PROT_EXECUTE;
11711183 try self.load_commands.append(self.base.allocator, .{
1172 .Segment = .{
1184 .Segment = SegmentCommand.empty(.{
11731185 .cmd = macho.LC_SEGMENT_64,
11741186 .cmdsize = @sizeOf(macho.segment_command_64),
11751187 .segname = makeStaticString("__TEXT"),
......@@ -1181,45 +1193,45 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11811193 .initprot = initprot,
11821194 .nsects = 0,
11831195 .flags = 0,
1184 },
1196 }),
11851197 });
11861198 self.cmd_table_dirty = true;
11871199 }
11881200 if (self.text_section_index == null) {
1189 self.text_section_index = @intCast(u16, self.sections.items.len);
11901201 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1202 self.text_section_index = @intCast(u16, text_segment.sections.items.len);
11911203
11921204 const program_code_size_hint = self.base.options.program_code_size_hint;
11931205 const file_size = mem.alignForwardGeneric(u64, program_code_size_hint, self.page_size);
11941206 const off = @intCast(u32, self.findFreeSpace(file_size, self.page_size)); // TODO maybe findFreeSpace should return u32 directly?
1195 const flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS;
11961207
11971208 log.debug("found __text section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
11981209
1199 try self.sections.append(self.base.allocator, .{
1210 try text_segment.sections.append(self.base.allocator, .{
12001211 .sectname = makeStaticString("__text"),
12011212 .segname = makeStaticString("__TEXT"),
1202 .addr = text_segment.vmaddr + off,
1213 .addr = text_segment.inner.vmaddr + off,
12031214 .size = file_size,
12041215 .offset = off,
12051216 .@"align" = if (self.base.options.target.cpu.arch == .aarch64) 2 else 0, // 2^2 for aarch64, 2^0 for x86_64
12061217 .reloff = 0,
12071218 .nreloc = 0,
1208 .flags = flags,
1219 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
12091220 .reserved1 = 0,
12101221 .reserved2 = 0,
12111222 .reserved3 = 0,
12121223 });
12131224
1214 text_segment.vmsize = file_size + off; // We add off here since __TEXT segment includes everything prior to __text section.
1215 text_segment.filesize = file_size + off;
1216 text_segment.cmdsize += @sizeOf(macho.section_64);
1217 text_segment.nsects += 1;
1225 text_segment.inner.vmsize = file_size + off; // We add off here since __TEXT segment includes everything prior to __text section.
1226 text_segment.inner.filesize = file_size + off;
1227 text_segment.inner.cmdsize += @sizeOf(macho.section_64);
1228 text_segment.inner.nsects += 1;
12181229 self.cmd_table_dirty = true;
12191230 }
12201231 if (self.got_section_index == null) {
1221 self.got_section_index = @intCast(u16, self.sections.items.len);
1222 const text_section = &self.sections.items[self.text_section_index.?];
1232 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1233 const text_section = &text_segment.sections.items[self.text_section_index.?];
1234 self.got_section_index = @intCast(u16, text_segment.sections.items.len);
12231235
12241236 const file_size = @sizeOf(u64) * self.base.options.symbol_count_hint;
12251237 // TODO looking for free space should be done *within* a segment it belongs to
......@@ -1227,7 +1239,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12271239
12281240 log.debug("found __got section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
12291241
1230 try self.sections.append(self.base.allocator, .{
1242 try text_segment.sections.append(self.base.allocator, .{
12311243 .sectname = makeStaticString("__got"),
12321244 .segname = makeStaticString("__TEXT"),
12331245 .addr = text_section.addr + text_section.size,
......@@ -1236,18 +1248,17 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12361248 .@"align" = if (self.base.options.target.cpu.arch == .aarch64) 2 else 0,
12371249 .reloff = 0,
12381250 .nreloc = 0,
1239 .flags = macho.S_REGULAR,
1251 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
12401252 .reserved1 = 0,
12411253 .reserved2 = 0,
12421254 .reserved3 = 0,
12431255 });
12441256
1245 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
12461257 const added_size = mem.alignForwardGeneric(u64, file_size, self.page_size);
1247 text_segment.vmsize += added_size;
1248 text_segment.filesize += added_size;
1249 text_segment.cmdsize += @sizeOf(macho.section_64);
1250 text_segment.nsects += 1;
1258 text_segment.inner.vmsize += added_size;
1259 text_segment.inner.filesize += added_size;
1260 text_segment.inner.cmdsize += @sizeOf(macho.section_64);
1261 text_segment.inner.nsects += 1;
12511262 self.cmd_table_dirty = true;
12521263 }
12531264 if (self.linkedit_segment_cmd_index == null) {
......@@ -1255,13 +1266,13 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12551266 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
12561267 const maxprot = macho.VM_PROT_READ | macho.VM_PROT_WRITE | macho.VM_PROT_EXECUTE;
12571268 const initprot = macho.VM_PROT_READ;
1258 const off = text_segment.fileoff + text_segment.filesize;
1269 const off = text_segment.inner.fileoff + text_segment.inner.filesize;
12591270 try self.load_commands.append(self.base.allocator, .{
1260 .Segment = .{
1271 .Segment = SegmentCommand.empty(.{
12611272 .cmd = macho.LC_SEGMENT_64,
12621273 .cmdsize = @sizeOf(macho.segment_command_64),
12631274 .segname = makeStaticString("__LINKEDIT"),
1264 .vmaddr = text_segment.vmaddr + text_segment.vmsize,
1275 .vmaddr = text_segment.inner.vmaddr + text_segment.inner.vmsize,
12651276 .vmsize = 0,
12661277 .fileoff = off,
12671278 .filesize = 0,
......@@ -1269,7 +1280,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12691280 .initprot = initprot,
12701281 .nsects = 0,
12711282 .flags = 0,
1272 },
1283 }),
12731284 });
12741285 self.linkedit_segment_next_offset = @intCast(u32, off);
12751286 self.cmd_table_dirty = true;
......@@ -1277,7 +1288,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12771288 if (self.dyld_info_cmd_index == null) {
12781289 self.dyld_info_cmd_index = @intCast(u16, self.load_commands.items.len);
12791290 try self.load_commands.append(self.base.allocator, .{
1280 .DyldInfo = .{
1291 .DyldInfoOnly = .{
12811292 .cmd = macho.LC_DYLD_INFO_ONLY,
12821293 .cmdsize = @sizeOf(macho.dyld_info_command),
12831294 .rebase_off = 0,
......@@ -1339,15 +1350,16 @@ pub fn populateMissingMetadata(self: *MachO) !void {
13391350 if (self.dylinker_cmd_index == null) {
13401351 self.dylinker_cmd_index = @intCast(u16, self.load_commands.items.len);
13411352 const cmdsize = mem.alignForwardGeneric(u64, @sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH), @sizeOf(u64));
1342 try self.load_commands.append(self.base.allocator, .{
1343 .Dylinker = .{
1344 .cmd = macho.LC_LOAD_DYLINKER,
1345 .cmdsize = @intCast(u32, cmdsize),
1346 .name = @sizeOf(macho.dylinker_command),
1347 },
1353 var dylinker_cmd = emptyGenericCommandWithData(macho.dylinker_command{
1354 .cmd = macho.LC_LOAD_DYLINKER,
1355 .cmdsize = @intCast(u32, cmdsize),
1356 .name = @sizeOf(macho.dylinker_command),
13481357 });
1358 dylinker_cmd.data = try self.base.allocator.alloc(u8, cmdsize - dylinker_cmd.inner.name);
1359 mem.set(u8, dylinker_cmd.data, 0);
1360 mem.copy(u8, dylinker_cmd.data, mem.spanZ(DEFAULT_DYLD_PATH));
1361 try self.load_commands.append(self.base.allocator, .{ .Dylinker = dylinker_cmd });
13491362 self.cmd_table_dirty = true;
1350 self.dylinker_cmd_dirty = true;
13511363 }
13521364 if (self.libsystem_cmd_index == null) {
13531365 self.libsystem_cmd_index = @intCast(u16, self.load_commands.items.len);
......@@ -1355,26 +1367,26 @@ pub fn populateMissingMetadata(self: *MachO) !void {
13551367 // TODO Find a way to work out runtime version from the OS version triple stored in std.Target.
13561368 // In the meantime, we're gonna hardcode to the minimum compatibility version of 0.0.0.
13571369 const min_version = 0x0;
1358 const dylib = .{
1359 .name = @sizeOf(macho.dylib_command),
1360 .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files
1361 .current_version = min_version,
1362 .compatibility_version = min_version,
1363 };
1364 try self.load_commands.append(self.base.allocator, .{
1365 .Dylib = .{
1366 .cmd = macho.LC_LOAD_DYLIB,
1367 .cmdsize = @intCast(u32, cmdsize),
1368 .dylib = dylib,
1370 var dylib_cmd = emptyGenericCommandWithData(macho.dylib_command{
1371 .cmd = macho.LC_LOAD_DYLIB,
1372 .cmdsize = @intCast(u32, cmdsize),
1373 .dylib = .{
1374 .name = @sizeOf(macho.dylib_command),
1375 .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files
1376 .current_version = min_version,
1377 .compatibility_version = min_version,
13691378 },
13701379 });
1380 dylib_cmd.data = try self.base.allocator.alloc(u8, cmdsize - dylib_cmd.inner.dylib.name);
1381 mem.set(u8, dylib_cmd.data, 0);
1382 mem.copy(u8, dylib_cmd.data, mem.spanZ(LIB_SYSTEM_PATH));
1383 try self.load_commands.append(self.base.allocator, .{ .Dylib = dylib_cmd });
13711384 self.cmd_table_dirty = true;
1372 self.libsystem_cmd_dirty = true;
13731385 }
13741386 if (self.main_cmd_index == null) {
13751387 self.main_cmd_index = @intCast(u16, self.load_commands.items.len);
13761388 try self.load_commands.append(self.base.allocator, .{
1377 .EntryPoint = .{
1389 .Main = .{
13781390 .cmd = macho.LC_MAIN,
13791391 .cmdsize = @sizeOf(macho.entry_point_command),
13801392 .entryoff = 0x0,
......@@ -1395,7 +1407,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
13951407 const ver = self.base.options.target.os.version_range.semver.min;
13961408 const version = ver.major << 16 | ver.minor << 8 | ver.patch;
13971409 try self.load_commands.append(self.base.allocator, .{
1398 .MinVersion = .{
1410 .VersionMin = .{
13991411 .cmd = cmd,
14001412 .cmdsize = @sizeOf(macho.version_min_command),
14011413 .version = version,
......@@ -1438,7 +1450,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14381450}
14391451
14401452fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
1441 const text_section = &self.sections.items[self.text_section_index.?];
1453 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1454 const text_section = &text_segment.sections.items[self.text_section_index.?];
14421455 const new_block_ideal_capacity = new_block_size * alloc_num / alloc_den;
14431456
14441457 // We use these to indicate our intention to update metadata, placing the new block,
......@@ -1536,7 +1549,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
15361549 return vaddr;
15371550}
15381551
1539fn makeStaticString(comptime bytes: []const u8) [16]u8 {
1552pub fn makeStaticString(comptime bytes: []const u8) [16]u8 {
15401553 var buf = [_]u8{0} ** 16;
15411554 if (bytes.len > buf.len) @compileError("string too long; max 16 bytes");
15421555 mem.copy(u8, buf[0..], bytes);
......@@ -1580,15 +1593,18 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
15801593 return test_end;
15811594 }
15821595 }
1583 for (self.sections.items) |section| {
1584 const increased_size = satMul(section.size, alloc_num) / alloc_den;
1585 const test_end = section.offset + increased_size;
1586 if (end > section.offset and start < test_end) {
1587 return test_end;
1596 if (self.text_segment_cmd_index) |text_index| {
1597 const text_segment = self.load_commands.items[text_index].Segment;
1598 for (text_segment.sections.items) |section| {
1599 const increased_size = satMul(section.size, alloc_num) / alloc_den;
1600 const test_end = section.offset + increased_size;
1601 if (end > section.offset and start < test_end) {
1602 return test_end;
1603 }
15881604 }
15891605 }
15901606 if (self.dyld_info_cmd_index) |dyld_info_index| {
1591 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfo;
1607 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfoOnly;
15921608 const tight_size = dyld_info.export_size;
15931609 const increased_size = satMul(tight_size, alloc_num) / alloc_den;
15941610 const test_end = dyld_info.export_off + increased_size;
......@@ -1625,12 +1641,15 @@ fn allocatedSize(self: *MachO, start: u64) u64 {
16251641 const off = @sizeOf(macho.mach_header_64);
16261642 if (off > start and off < min_pos) min_pos = off;
16271643 }
1628 for (self.sections.items) |section| {
1629 if (section.offset <= start) continue;
1630 if (section.offset < min_pos) min_pos = section.offset;
1644 if (self.text_segment_cmd_index) |text_index| {
1645 const text_segment = self.load_commands.items[text_index].Segment;
1646 for (text_segment.sections.items) |section| {
1647 if (section.offset <= start) continue;
1648 if (section.offset < min_pos) min_pos = section.offset;
1649 }
16311650 }
16321651 if (self.dyld_info_cmd_index) |dyld_info_index| {
1633 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfo;
1652 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfoOnly;
16341653 if (dyld_info.export_off > start and dyld_info.export_off < min_pos) min_pos = dyld_info.export_off;
16351654 }
16361655 if (self.symtab_cmd_index) |symtab_index| {
......@@ -1650,7 +1669,8 @@ fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u16) u64 {
16501669}
16511670
16521671fn writeOffsetTableEntry(self: *MachO, index: usize) !void {
1653 const sect = &self.sections.items[self.got_section_index.?];
1672 const text_semgent = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1673 const sect = &text_semgent.sections.items[self.got_section_index.?];
16541674 const off = sect.offset + @sizeOf(u64) * index;
16551675 const vmaddr = sect.addr + @sizeOf(u64) * index;
16561676
......@@ -1718,9 +1738,9 @@ fn writeSymbolTable(self: *MachO) !void {
17181738
17191739 // Advance size of __LINKEDIT segment
17201740 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1721 linkedit.filesize += symtab.nsyms * @sizeOf(macho.nlist_64);
1722 if (linkedit.vmsize < linkedit.filesize) {
1723 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1741 linkedit.inner.filesize += symtab.nsyms * @sizeOf(macho.nlist_64);
1742 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1743 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
17241744 }
17251745 self.cmd_table_dirty = true;
17261746}
......@@ -1728,16 +1748,16 @@ fn writeSymbolTable(self: *MachO) !void {
17281748fn writeCodeSignaturePadding(self: *MachO) !void {
17291749 const code_sig_cmd = &self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
17301750 const fileoff = self.linkedit_segment_next_offset.?;
1731 const datasize: u32 = 0x1000; // TODO Calculate the expected size of the signature.
1751 const datasize = CodeSignature.calcCodeSignaturePadding(self.base.options.emit.?.sub_path, fileoff);
17321752 code_sig_cmd.dataoff = fileoff;
17331753 code_sig_cmd.datasize = datasize;
17341754
17351755 self.linkedit_segment_next_offset = fileoff + datasize;
17361756 // Advance size of __LINKEDIT segment
17371757 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1738 linkedit.filesize += datasize;
1739 if (linkedit.vmsize < linkedit.filesize) {
1740 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1758 linkedit.inner.filesize += datasize;
1759 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1760 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
17411761 }
17421762 log.debug("writing code signature padding from 0x{x} to 0x{x}\n", .{ fileoff, fileoff + datasize });
17431763 // Pad out the space. We need to do this to calculate valid hashes for everything in the file
......@@ -1746,15 +1766,21 @@ fn writeCodeSignaturePadding(self: *MachO) !void {
17461766}
17471767
17481768fn writeCodeSignature(self: *MachO) !void {
1749 const code_sig_cmd = &self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
1769 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1770 const code_sig_cmd = self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
1771
17501772 var code_sig = CodeSignature.init(self.base.allocator);
17511773 defer code_sig.deinit();
1752
1753 try code_sig.calcAdhocSignature(self);
1774 try code_sig.calcAdhocSignature(
1775 self.base.file.?,
1776 self.base.options.emit.?.sub_path,
1777 text_segment.inner,
1778 code_sig_cmd,
1779 self.base.options.output_mode,
1780 );
17541781
17551782 var buffer = try self.base.allocator.alloc(u8, code_sig.size());
17561783 defer self.base.allocator.free(buffer);
1757
17581784 code_sig.write(buffer);
17591785
17601786 log.debug("writing code signature from 0x{x} to 0x{x}\n", .{ code_sig_cmd.dataoff, code_sig_cmd.dataoff + buffer.len });
......@@ -1772,10 +1798,10 @@ fn writeExportTrie(self: *MachO) !void {
17721798 for (self.global_symbols.items) |symbol| {
17731799 // TODO figure out if we should put all global symbols into the export trie
17741800 const name = self.getString(symbol.n_strx);
1775 assert(symbol.n_value >= text_segment.vmaddr);
1801 assert(symbol.n_value >= text_segment.inner.vmaddr);
17761802 try trie.put(self.base.allocator, .{
17771803 .name = name,
1778 .vmaddr_offset = symbol.n_value - text_segment.vmaddr,
1804 .vmaddr_offset = symbol.n_value - text_segment.inner.vmaddr,
17791805 .export_flags = 0, // TODO workout creation of export flags
17801806 });
17811807 }
......@@ -1785,7 +1811,7 @@ fn writeExportTrie(self: *MachO) !void {
17851811
17861812 try trie.writeULEB128Mem(self.base.allocator, &buffer);
17871813
1788 const dyld_info = &self.load_commands.items[self.dyld_info_cmd_index.?].DyldInfo;
1814 const dyld_info = &self.load_commands.items[self.dyld_info_cmd_index.?].DyldInfoOnly;
17891815 const export_size = @intCast(u32, mem.alignForward(buffer.items.len, @sizeOf(u64)));
17901816 dyld_info.export_off = self.linkedit_segment_next_offset.?;
17911817 dyld_info.export_size = export_size;
......@@ -1801,9 +1827,9 @@ fn writeExportTrie(self: *MachO) !void {
18011827 self.linkedit_segment_next_offset = dyld_info.export_off + dyld_info.export_size;
18021828 // Advance size of __LINKEDIT segment
18031829 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1804 linkedit.filesize += dyld_info.export_size;
1805 if (linkedit.vmsize < linkedit.filesize) {
1806 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1830 linkedit.inner.filesize += dyld_info.export_size;
1831 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1832 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
18071833 }
18081834 self.cmd_table_dirty = true;
18091835}
......@@ -1826,103 +1852,41 @@ fn writeStringTable(self: *MachO) !void {
18261852 self.linkedit_segment_next_offset = symtab.stroff + symtab.strsize;
18271853 // Advance size of __LINKEDIT segment
18281854 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1829 linkedit.filesize += symtab.strsize;
1830 if (linkedit.vmsize < linkedit.filesize) {
1831 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1855 linkedit.inner.filesize += symtab.strsize;
1856 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1857 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
18321858 }
18331859 self.cmd_table_dirty = true;
18341860}
18351861
1836fn writeCmdHeaders(self: *MachO) !void {
1837 assert(self.cmd_table_dirty);
1838
1839 // Write all load command headers first.
1840 // Since command sizes are up-to-date and accurate, we will correctly
1841 // leave space for any section headers that any of the segment load
1842 // commands might consist of.
1843 var last_cmd_offset: usize = @sizeOf(macho.mach_header_64);
1844 for (self.load_commands.items) |cmd| {
1845 try cmd.write(&self.base.file.?, last_cmd_offset);
1846 last_cmd_offset += cmd.cmdsize();
1862/// Writes all load commands and section headers.
1863fn writeLoadCommands(self: *MachO) !void {
1864 var sizeofcmds: usize = 0;
1865 for (self.load_commands.items) |lc| {
1866 sizeofcmds += lc.cmdsize();
18471867 }
1848 {
1849 const off = if (self.text_segment_cmd_index) |text_segment_index| blk: {
1850 var i: usize = 0;
1851 var cmdsize: usize = @sizeOf(macho.mach_header_64) + @sizeOf(macho.segment_command_64);
1852 while (i < text_segment_index) : (i += 1) {
1853 cmdsize += self.load_commands.items[i].cmdsize();
1854 }
1855 break :blk cmdsize;
1856 } else {
1857 // If we've landed in here, we are building a MachO object file, so we have
1858 // only one, noname segment to append this section header to.
1859 return error.TODOImplementWritingObjFiles;
1860 };
1861 // write sections belonging to __TEXT segment
1862 // TODO section indices should belong to each Segment, and we should iterate dynamically.
1863 const id = self.text_section_index.?;
1864 log.debug("writing __TEXT section headers at 0x{x}\n", .{off});
1865 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.sections.items[id .. id + 2]), off);
1868
1869 var buffer = try self.base.allocator.alloc(u8, sizeofcmds);
1870 defer self.base.allocator.free(buffer);
1871 var writer = std.io.fixedBufferStream(buffer).writer();
1872 for (self.load_commands.items) |lc| {
1873 try lc.write(writer);
18661874 }
1875
1876 try self.base.file.?.pwriteAll(buffer, @sizeOf(macho.mach_header_64));
18671877}
18681878
18691879/// Writes Mach-O file header.
1870/// Should be invoked last as it needs up-to-date values of ncmds and sizeof_cmds bookkeeping
1871/// variables.
1872fn writeMachOHeader(self: *MachO) !void {
1873 var hdr: macho.mach_header_64 = undefined;
1874 hdr.magic = macho.MH_MAGIC_64;
1875
1876 const CpuInfo = struct {
1877 cpu_type: macho.cpu_type_t,
1878 cpu_subtype: macho.cpu_subtype_t,
1879 };
1880
1881 const cpu_info: CpuInfo = switch (self.base.options.target.cpu.arch) {
1882 .aarch64 => .{
1883 .cpu_type = macho.CPU_TYPE_ARM64,
1884 .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL,
1885 },
1886 .x86_64 => .{
1887 .cpu_type = macho.CPU_TYPE_X86_64,
1888 .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL,
1889 },
1890 else => return error.UnsupportedMachOArchitecture,
1891 };
1892 hdr.cputype = cpu_info.cpu_type;
1893 hdr.cpusubtype = cpu_info.cpu_subtype;
1894
1895 const filetype: u32 = switch (self.base.options.output_mode) {
1896 .Exe => macho.MH_EXECUTE,
1897 .Obj => macho.MH_OBJECT,
1898 .Lib => switch (self.base.options.link_mode) {
1899 .Static => return error.TODOStaticLibMachOType,
1900 .Dynamic => macho.MH_DYLIB,
1901 },
1902 };
1903 hdr.filetype = filetype;
1904 hdr.ncmds = @intCast(u32, self.load_commands.items.len);
1905
1880fn writeHeader(self: *MachO) !void {
1881 self.header.?.ncmds = @intCast(u32, self.load_commands.items.len);
19061882 var sizeofcmds: u32 = 0;
19071883 for (self.load_commands.items) |cmd| {
19081884 sizeofcmds += cmd.cmdsize();
19091885 }
1910
1911 hdr.sizeofcmds = sizeofcmds;
1912
1913 switch (self.base.options.output_mode) {
1914 .Exe => {
1915 hdr.flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE;
1916 },
1917 else => {
1918 hdr.flags = 0;
1919 },
1920 }
1921 hdr.reserved = 0;
1922
1923 log.debug("writing Mach-O header {}\n", .{hdr});
1924
1925 try self.base.file.?.pwriteAll(@ptrCast([*]const u8, &hdr)[0..@sizeOf(macho.mach_header_64)], 0);
1886 self.header.?.sizeofcmds = sizeofcmds;
1887 log.debug("writing Mach-O header {}\n", .{self.header.?});
1888 const slice = [1]macho.mach_header_64{self.header.?};
1889 try self.base.file.?.pwriteAll(mem.sliceAsBytes(slice[0..1]), 0);
19261890}
19271891
19281892/// Saturating multiplication
......@@ -1930,3 +1894,48 @@ fn satMul(a: anytype, b: anytype) @TypeOf(a, b) {
19301894 const T = @TypeOf(a, b);
19311895 return std.math.mul(T, a, b) catch std.math.maxInt(T);
19321896}
1897
1898/// Parse MachO contents from existing binary file.
1899/// TODO This method is incomplete and currently parses only the header
1900/// plus the load commands.
1901fn parseFromFile(self: *MachO, file: fs.File) !void {
1902 self.base.file = file;
1903 var reader = file.reader();
1904 const header = try reader.readStruct(macho.mach_header_64);
1905 try self.load_commands.ensureCapacity(self.base.allocator, header.ncmds);
1906 var i: u16 = 0;
1907 while (i < header.ncmds) : (i += 1) {
1908 const cmd = try LoadCommand.read(self.base.allocator, reader);
1909 switch (cmd.cmd()) {
1910 macho.LC_SEGMENT_64 => {
1911 const x = cmd.Segment;
1912 if (isSegmentOrSection(&x.inner.segname, "__LINKEDIT")) {
1913 self.linkedit_segment_cmd_index = i;
1914 } else if (isSegmentOrSection(&x.inner.segname, "__TEXT")) {
1915 self.text_segment_cmd_index = i;
1916 for (x.sections.items) |sect, j| {
1917 if (isSegmentOrSection(&sect.sectname, "__text")) {
1918 self.text_section_index = @intCast(u16, j);
1919 }
1920 }
1921 }
1922 },
1923 macho.LC_SYMTAB => {
1924 self.symtab_cmd_index = i;
1925 },
1926 macho.LC_CODE_SIGNATURE => {
1927 self.code_signature_cmd_index = i;
1928 },
1929 // TODO populate more MachO fields
1930 else => {},
1931 }
1932 self.load_commands.appendAssumeCapacity(cmd);
1933 }
1934 self.header = header;
1935
1936 // TODO parse memory mapped segments
1937}
1938
1939fn isSegmentOrSection(name: *const [16]u8, needle: []const u8) bool {
1940 return mem.eql(u8, mem.trimRight(u8, name.*[0..], &[_]u8{0}), needle);
1941}
src/link/MachO/CodeSignature.zig+26-19
......@@ -2,6 +2,7 @@ const CodeSignature = @This();
22
33const std = @import("std");
44const assert = std.debug.assert;
5const fs = std.fs;
56const log = std.log.scoped(.link);
67const macho = std.macho;
78const mem = std.mem;
......@@ -9,8 +10,6 @@ const testing = std.testing;
910const Allocator = mem.Allocator;
1011const Sha256 = std.crypto.hash.sha2.Sha256;
1112
12const MachO = @import("../MachO.zig");
13
1413const hash_size: u8 = 32;
1514const page_size: u16 = 0x1000;
1615
......@@ -51,7 +50,7 @@ const CodeDirectory = struct {
5150 }
5251};
5352
54alloc: *Allocator,
53allocator: *Allocator,
5554inner: macho.SuperBlob = .{
5655 .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,
5756 .length = @sizeOf(macho.SuperBlob),
......@@ -59,19 +58,21 @@ inner: macho.SuperBlob = .{
5958},
6059cdir: ?CodeDirectory = null,
6160
62pub fn init(alloc: *Allocator) CodeSignature {
63 return .{
64 .alloc = alloc,
65 };
61pub fn init(allocator: *Allocator) CodeSignature {
62 return .{ .allocator = allocator };
6663}
6764
68pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
69 const text_segment = bin_file.load_commands.items[bin_file.text_segment_cmd_index.?].Segment;
70 const code_sig_cmd = bin_file.load_commands.items[bin_file.code_signature_cmd_index.?].LinkeditData;
71
65pub fn calcAdhocSignature(
66 self: *CodeSignature,
67 file: fs.File,
68 id: []const u8,
69 text_segment: macho.segment_command_64,
70 code_sig_cmd: macho.linkedit_data_command,
71 output_mode: std.builtin.OutputMode,
72) !void {
7273 const execSegBase: u64 = text_segment.fileoff;
7374 const execSegLimit: u64 = text_segment.filesize;
74 const execSegFlags: u64 = if (bin_file.base.options.output_mode == .Exe) macho.CS_EXECSEG_MAIN_BINARY else 0;
75 const execSegFlags: u64 = if (output_mode == .Exe) macho.CS_EXECSEG_MAIN_BINARY else 0;
7576 const file_size = code_sig_cmd.dataoff;
7677 var cdir = CodeDirectory{
7778 .inner = .{
......@@ -102,12 +103,10 @@ pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
102103 const total_pages = mem.alignForward(file_size, page_size) / page_size;
103104
104105 var hash: [hash_size]u8 = undefined;
105 var buffer = try bin_file.base.allocator.alloc(u8, page_size);
106 defer bin_file.base.allocator.free(buffer);
107 const macho_file = bin_file.base.file.?;
106 var buffer = try self.allocator.alloc(u8, page_size);
107 defer self.allocator.free(buffer);
108108
109 const id = bin_file.base.options.emit.?.sub_path;
110 try cdir.data.ensureCapacity(self.alloc, total_pages * hash_size + id.len + 1);
109 try cdir.data.ensureCapacity(self.allocator, total_pages * hash_size + id.len + 1);
111110
112111 // 1. Save the identifier and update offsets
113112 cdir.inner.identOffset = cdir.inner.length;
......@@ -122,7 +121,7 @@ pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
122121 while (i < total_pages) : (i += 1) {
123122 const fstart = i * page_size;
124123 const fsize = if (fstart + page_size > file_size) file_size - fstart else page_size;
125 const len = try macho_file.preadAll(buffer, fstart);
124 const len = try file.preadAll(buffer, fstart);
126125 assert(fsize <= len);
127126
128127 Sha256.hash(buffer[0..fsize], &hash, .{});
......@@ -153,7 +152,7 @@ pub fn write(self: CodeSignature, buffer: []u8) void {
153152
154153pub fn deinit(self: *CodeSignature) void {
155154 if (self.cdir) |*cdir| {
156 cdir.data.deinit(self.alloc);
155 cdir.data.deinit(self.allocator);
157156 }
158157}
159158
......@@ -180,3 +179,11 @@ test "CodeSignature header" {
180179 const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 };
181180 testing.expect(mem.eql(u8, expected[0..], buffer[0..]));
182181}
182
183pub fn calcCodeSignaturePadding(id: []const u8, file_size: u64) u32 {
184 const ident_size = id.len + 1;
185 const total_pages = mem.alignForwardGeneric(u64, file_size, page_size) / page_size;
186 const hashed_size = total_pages * hash_size;
187 const codesig_header = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex) + @sizeOf(macho.CodeDirectory);
188 return @intCast(u32, mem.alignForwardGeneric(u64, codesig_header + ident_size + hashed_size, @sizeOf(u64)));
189}
src/link/MachO/commands.zig created+369
......@@ -0,0 +1,369 @@
1const std = @import("std");
2const fs = std.fs;
3const io = std.io;
4const mem = std.mem;
5const meta = std.meta;
6const macho = std.macho;
7const testing = std.testing;
8
9const Allocator = std.mem.Allocator;
10const makeName = @import("../MachO.zig").makeStaticString;
11
12pub const LoadCommand = union(enum) {
13 Segment: SegmentCommand,
14 DyldInfoOnly: macho.dyld_info_command,
15 Symtab: macho.symtab_command,
16 Dysymtab: macho.dysymtab_command,
17 Dylinker: GenericCommandWithData(macho.dylinker_command),
18 Dylib: GenericCommandWithData(macho.dylib_command),
19 Main: macho.entry_point_command,
20 VersionMin: macho.version_min_command,
21 SourceVersion: macho.source_version_command,
22 LinkeditData: macho.linkedit_data_command,
23 Unknown: GenericCommandWithData(macho.load_command),
24
25 pub fn read(allocator: *Allocator, reader: anytype) !LoadCommand {
26 const header = try reader.readStruct(macho.load_command);
27 var buffer = try allocator.alloc(u8, header.cmdsize);
28 defer allocator.free(buffer);
29 mem.copy(u8, buffer[0..], mem.asBytes(&header));
30 try reader.readNoEof(buffer[@sizeOf(macho.load_command)..]);
31 var stream = io.fixedBufferStream(buffer[0..]);
32
33 return switch (header.cmd) {
34 macho.LC_SEGMENT_64 => LoadCommand{
35 .Segment = try SegmentCommand.read(allocator, stream.reader()),
36 },
37 macho.LC_DYLD_INFO, macho.LC_DYLD_INFO_ONLY => LoadCommand{
38 .DyldInfoOnly = try stream.reader().readStruct(macho.dyld_info_command),
39 },
40 macho.LC_SYMTAB => LoadCommand{
41 .Symtab = try stream.reader().readStruct(macho.symtab_command),
42 },
43 macho.LC_DYSYMTAB => LoadCommand{
44 .Dysymtab = try stream.reader().readStruct(macho.dysymtab_command),
45 },
46 macho.LC_ID_DYLINKER, macho.LC_LOAD_DYLINKER, macho.LC_DYLD_ENVIRONMENT => LoadCommand{
47 .Dylinker = try GenericCommandWithData(macho.dylinker_command).read(allocator, stream.reader()),
48 },
49 macho.LC_ID_DYLIB, macho.LC_LOAD_WEAK_DYLIB, macho.LC_LOAD_DYLIB, macho.LC_REEXPORT_DYLIB => LoadCommand{
50 .Dylib = try GenericCommandWithData(macho.dylib_command).read(allocator, stream.reader()),
51 },
52 macho.LC_MAIN => LoadCommand{
53 .Main = try stream.reader().readStruct(macho.entry_point_command),
54 },
55 macho.LC_VERSION_MIN_MACOSX, macho.LC_VERSION_MIN_IPHONEOS, macho.LC_VERSION_MIN_WATCHOS, macho.LC_VERSION_MIN_TVOS => LoadCommand{
56 .VersionMin = try stream.reader().readStruct(macho.version_min_command),
57 },
58 macho.LC_SOURCE_VERSION => LoadCommand{
59 .SourceVersion = try stream.reader().readStruct(macho.source_version_command),
60 },
61 macho.LC_FUNCTION_STARTS, macho.LC_DATA_IN_CODE, macho.LC_CODE_SIGNATURE => LoadCommand{
62 .LinkeditData = try stream.reader().readStruct(macho.linkedit_data_command),
63 },
64 else => LoadCommand{
65 .Unknown = try GenericCommandWithData(macho.load_command).read(allocator, stream.reader()),
66 },
67 };
68 }
69
70 pub fn write(self: LoadCommand, writer: anytype) !void {
71 return switch (self) {
72 .DyldInfoOnly => |x| writeStruct(x, writer),
73 .Symtab => |x| writeStruct(x, writer),
74 .Dysymtab => |x| writeStruct(x, writer),
75 .Main => |x| writeStruct(x, writer),
76 .VersionMin => |x| writeStruct(x, writer),
77 .SourceVersion => |x| writeStruct(x, writer),
78 .LinkeditData => |x| writeStruct(x, writer),
79 .Segment => |x| x.write(writer),
80 .Dylinker => |x| x.write(writer),
81 .Dylib => |x| x.write(writer),
82 .Unknown => |x| x.write(writer),
83 };
84 }
85
86 pub fn cmd(self: LoadCommand) u32 {
87 return switch (self) {
88 .DyldInfoOnly => |x| x.cmd,
89 .Symtab => |x| x.cmd,
90 .Dysymtab => |x| x.cmd,
91 .Main => |x| x.cmd,
92 .VersionMin => |x| x.cmd,
93 .SourceVersion => |x| x.cmd,
94 .LinkeditData => |x| x.cmd,
95 .Segment => |x| x.inner.cmd,
96 .Dylinker => |x| x.inner.cmd,
97 .Dylib => |x| x.inner.cmd,
98 .Unknown => |x| x.inner.cmd,
99 };
100 }
101
102 pub fn cmdsize(self: LoadCommand) u32 {
103 return switch (self) {
104 .DyldInfoOnly => |x| x.cmdsize,
105 .Symtab => |x| x.cmdsize,
106 .Dysymtab => |x| x.cmdsize,
107 .Main => |x| x.cmdsize,
108 .VersionMin => |x| x.cmdsize,
109 .SourceVersion => |x| x.cmdsize,
110 .LinkeditData => |x| x.cmdsize,
111 .Segment => |x| x.inner.cmdsize,
112 .Dylinker => |x| x.inner.cmdsize,
113 .Dylib => |x| x.inner.cmdsize,
114 .Unknown => |x| x.inner.cmdsize,
115 };
116 }
117
118 pub fn deinit(self: *LoadCommand, allocator: *Allocator) void {
119 return switch (self.*) {
120 .Segment => |*x| x.deinit(allocator),
121 .Dylinker => |*x| x.deinit(allocator),
122 .Dylib => |*x| x.deinit(allocator),
123 .Unknown => |*x| x.deinit(allocator),
124 else => {},
125 };
126 }
127
128 fn writeStruct(command: anytype, writer: anytype) !void {
129 return writer.writeAll(mem.asBytes(&command));
130 }
131
132 fn eql(self: LoadCommand, other: LoadCommand) bool {
133 if (@as(@TagType(LoadCommand), self) != @as(@TagType(LoadCommand), other)) return false;
134 return switch (self) {
135 .DyldInfoOnly => |x| meta.eql(x, other.DyldInfoOnly),
136 .Symtab => |x| meta.eql(x, other.Symtab),
137 .Dysymtab => |x| meta.eql(x, other.Dysymtab),
138 .Main => |x| meta.eql(x, other.Main),
139 .VersionMin => |x| meta.eql(x, other.VersionMin),
140 .SourceVersion => |x| meta.eql(x, other.SourceVersion),
141 .LinkeditData => |x| meta.eql(x, other.LinkeditData),
142 .Segment => |x| x.eql(other.Segment),
143 .Dylinker => |x| x.eql(other.Dylinker),
144 .Dylib => |x| x.eql(other.Dylib),
145 .Unknown => |x| x.eql(other.Unknown),
146 };
147 }
148};
149
150pub const SegmentCommand = struct {
151 inner: macho.segment_command_64,
152 sections: std.ArrayListUnmanaged(macho.section_64) = .{},
153
154 pub fn empty(inner: macho.segment_command_64) SegmentCommand {
155 return .{ .inner = inner };
156 }
157
158 pub fn read(alloc: *Allocator, reader: anytype) !SegmentCommand {
159 const inner = try reader.readStruct(macho.segment_command_64);
160 var segment = SegmentCommand{
161 .inner = inner,
162 };
163 try segment.sections.ensureCapacity(alloc, inner.nsects);
164
165 var i: usize = 0;
166 while (i < inner.nsects) : (i += 1) {
167 const section = try reader.readStruct(macho.section_64);
168 segment.sections.appendAssumeCapacity(section);
169 }
170
171 return segment;
172 }
173
174 pub fn write(self: SegmentCommand, writer: anytype) !void {
175 try writer.writeAll(mem.asBytes(&self.inner));
176 for (self.sections.items) |sect| {
177 try writer.writeAll(mem.asBytes(&sect));
178 }
179 }
180
181 pub fn deinit(self: *SegmentCommand, alloc: *Allocator) void {
182 self.sections.deinit(alloc);
183 }
184
185 fn eql(self: SegmentCommand, other: SegmentCommand) bool {
186 if (!meta.eql(self.inner, other.inner)) return false;
187 const lhs = self.sections.items;
188 const rhs = other.sections.items;
189 var i: usize = 0;
190 while (i < self.inner.nsects) : (i += 1) {
191 if (!meta.eql(lhs[i], rhs[i])) return false;
192 }
193 return true;
194 }
195};
196
197pub fn emptyGenericCommandWithData(cmd: anytype) GenericCommandWithData(@TypeOf(cmd)) {
198 return .{ .inner = cmd };
199}
200
201pub fn GenericCommandWithData(comptime Cmd: type) type {
202 return struct {
203 inner: Cmd,
204 /// This field remains undefined until `read` is called.
205 data: []u8 = undefined,
206
207 const Self = @This();
208
209 pub fn read(allocator: *Allocator, reader: anytype) !Self {
210 const inner = try reader.readStruct(Cmd);
211 var data = try allocator.alloc(u8, inner.cmdsize - @sizeOf(Cmd));
212 errdefer allocator.free(data);
213 try reader.readNoEof(data[0..]);
214 return Self{
215 .inner = inner,
216 .data = data,
217 };
218 }
219
220 pub fn write(self: Self, writer: anytype) !void {
221 try writer.writeAll(mem.asBytes(&self.inner));
222 try writer.writeAll(self.data);
223 }
224
225 pub fn deinit(self: *Self, allocator: *Allocator) void {
226 allocator.free(self.data);
227 }
228
229 fn eql(self: Self, other: Self) bool {
230 if (!meta.eql(self.inner, other.inner)) return false;
231 return mem.eql(u8, self.data, other.data);
232 }
233 };
234}
235
236fn testRead(allocator: *Allocator, buffer: []const u8, expected: anytype) !void {
237 var stream = io.fixedBufferStream(buffer);
238 var given = try LoadCommand.read(allocator, stream.reader());
239 defer given.deinit(allocator);
240 testing.expect(expected.eql(given));
241}
242
243fn testWrite(buffer: []u8, cmd: LoadCommand, expected: []const u8) !void {
244 var stream = io.fixedBufferStream(buffer);
245 try cmd.write(stream.writer());
246 testing.expect(mem.eql(u8, expected, buffer[0..expected.len]));
247}
248
249test "read-write segment command" {
250 var gpa = testing.allocator;
251 const in_buffer = &[_]u8{
252 0x19, 0x00, 0x00, 0x00, // cmd
253 0x98, 0x00, 0x00, 0x00, // cmdsize
254 0x5f, 0x5f, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // segname
255 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // vmaddr
256 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // vmsize
257 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // fileoff
258 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // filesize
259 0x07, 0x00, 0x00, 0x00, // maxprot
260 0x05, 0x00, 0x00, 0x00, // initprot
261 0x01, 0x00, 0x00, 0x00, // nsects
262 0x00, 0x00, 0x00, 0x00, // flags
263 0x5f, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sectname
264 0x5f, 0x5f, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // segname
265 0x00, 0x40, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // address
266 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // size
267 0x00, 0x40, 0x00, 0x00, // offset
268 0x02, 0x00, 0x00, 0x00, // alignment
269 0x00, 0x00, 0x00, 0x00, // reloff
270 0x00, 0x00, 0x00, 0x00, // nreloc
271 0x00, 0x04, 0x00, 0x80, // flags
272 0x00, 0x00, 0x00, 0x00, // reserved1
273 0x00, 0x00, 0x00, 0x00, // reserved2
274 0x00, 0x00, 0x00, 0x00, // reserved3
275 };
276 var cmd = SegmentCommand{
277 .inner = .{
278 .cmd = macho.LC_SEGMENT_64,
279 .cmdsize = 152,
280 .segname = makeName("__TEXT"),
281 .vmaddr = 4294967296,
282 .vmsize = 294912,
283 .fileoff = 0,
284 .filesize = 294912,
285 .maxprot = macho.VM_PROT_READ | macho.VM_PROT_WRITE | macho.VM_PROT_EXECUTE,
286 .initprot = macho.VM_PROT_EXECUTE | macho.VM_PROT_READ,
287 .nsects = 1,
288 .flags = 0,
289 },
290 };
291 try cmd.sections.append(gpa, .{
292 .sectname = makeName("__text"),
293 .segname = makeName("__TEXT"),
294 .addr = 4294983680,
295 .size = 448,
296 .offset = 16384,
297 .@"align" = 2,
298 .reloff = 0,
299 .nreloc = 0,
300 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
301 .reserved1 = 0,
302 .reserved2 = 0,
303 .reserved3 = 0,
304 });
305 defer cmd.deinit(gpa);
306 try testRead(gpa, in_buffer[0..], LoadCommand{ .Segment = cmd });
307
308 var out_buffer: [in_buffer.len]u8 = undefined;
309 try testWrite(out_buffer[0..], LoadCommand{ .Segment = cmd }, in_buffer[0..]);
310}
311
312test "read-write generic command with data" {
313 var gpa = testing.allocator;
314 const in_buffer = &[_]u8{
315 0x0c, 0x00, 0x00, 0x00, // cmd
316 0x20, 0x00, 0x00, 0x00, // cmdsize
317 0x18, 0x00, 0x00, 0x00, // name
318 0x02, 0x00, 0x00, 0x00, // timestamp
319 0x00, 0x00, 0x00, 0x00, // current_version
320 0x00, 0x00, 0x00, 0x00, // compatibility_version
321 0x2f, 0x75, 0x73, 0x72, 0x00, 0x00, 0x00, 0x00, // data
322 };
323 var cmd = GenericCommandWithData(macho.dylib_command){
324 .inner = .{
325 .cmd = macho.LC_LOAD_DYLIB,
326 .cmdsize = 32,
327 .dylib = .{
328 .name = 24,
329 .timestamp = 2,
330 .current_version = 0,
331 .compatibility_version = 0,
332 },
333 },
334 };
335 cmd.data = try gpa.alloc(u8, 8);
336 defer gpa.free(cmd.data);
337 cmd.data[0] = 0x2f;
338 cmd.data[1] = 0x75;
339 cmd.data[2] = 0x73;
340 cmd.data[3] = 0x72;
341 cmd.data[4] = 0x0;
342 cmd.data[5] = 0x0;
343 cmd.data[6] = 0x0;
344 cmd.data[7] = 0x0;
345 try testRead(gpa, in_buffer[0..], LoadCommand{ .Dylib = cmd });
346
347 var out_buffer: [in_buffer.len]u8 = undefined;
348 try testWrite(out_buffer[0..], LoadCommand{ .Dylib = cmd }, in_buffer[0..]);
349}
350
351test "read-write C struct command" {
352 var gpa = testing.allocator;
353 const in_buffer = &[_]u8{
354 0x28, 0x00, 0x00, 0x80, // cmd
355 0x18, 0x00, 0x00, 0x00, // cmdsize
356 0x04, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // entryoff
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // stacksize
358 };
359 const cmd = .{
360 .cmd = macho.LC_MAIN,
361 .cmdsize = 24,
362 .entryoff = 16644,
363 .stacksize = 0,
364 };
365 try testRead(gpa, in_buffer[0..], LoadCommand{ .Main = cmd });
366
367 var out_buffer: [in_buffer.len]u8 = undefined;
368 try testWrite(out_buffer[0..], LoadCommand{ .Main = cmd }, in_buffer[0..]);
369}
src/main.zig+3-6
......@@ -1773,9 +1773,7 @@ fn buildOutputType(
17731773 error.SemanticAnalyzeFail => process.exit(1),
17741774 else => |e| return e,
17751775 };
1776 if (output_mode == .Exe) {
1777 try comp.makeBinFileExecutable();
1778 }
1776 try comp.makeBinFileExecutable();
17791777
17801778 if (build_options.is_stage1 and comp.stage1_lock != null and watch) {
17811779 warn("--watch is not recommended with the stage1 backend; it leaks memory and is not capable of incremental compilation", .{});
......@@ -1874,9 +1872,7 @@ fn buildOutputType(
18741872
18751873 while (watch) {
18761874 try stderr.print("(zig) ", .{});
1877 if (output_mode == .Exe) {
1878 try comp.makeBinFileExecutable();
1879 }
1875 try comp.makeBinFileExecutable();
18801876 if (stdin.readUntilDelimiterOrEof(&repl_buf, '\n') catch |err| {
18811877 try stderr.print("\nUnable to parse command: {}\n", .{@errorName(err)});
18821878 continue;
......@@ -2413,6 +2409,7 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
24132409 defer comp.destroy();
24142410
24152411 try updateModule(gpa, comp, null, .none);
2412 try comp.makeBinFileExecutable();
24162413
24172414 child_argv.items[argv_index_exe] = try comp.bin_file.options.emit.?.directory.join(
24182415 arena,