authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-08-27 08:44:13+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-03 16:43:47-04:00
logdac1cd77505ef9fa493e069549c139d74e31081f
treef63d0a9182a154c069d2c531c9a652730c4b9d9c
parent2a58e30bd5f522bf3077f556f47a1e28c537627e

Write out simple Mach-O object file

This commit adds enough Mach-O linker implementation to write out simple Mach-O object file. Be warned however, the object file is largely incomplete: misses relocation info, debug symbols, etc. However, it seemed like a good starting to get the basic understanding right. Signed-off-by: Jakub Konka <kubkon@jakubkonka.com>

2 files changed, 581 insertions(+), 160 deletions(-)

src-self-hosted/codegen.zig+56-1
......@@ -1443,7 +1443,57 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
14431443 }
14441444 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
14451445 switch (arch) {
1446 .x86_64 => return self.fail(inst.base.src, "TODO implement codegen for call when linking with MachO for x86_64 arch", .{}),
1446 .x86_64 => {
1447 for (info.args) |mc_arg, arg_i| {
1448 const arg = inst.args[arg_i];
1449 const arg_mcv = try self.resolveInst(inst.args[arg_i]);
1450 // Here we do not use setRegOrMem even though the logic is similar, because
1451 // the function call will move the stack pointer, so the offsets are different.
1452 switch (mc_arg) {
1453 .none => continue,
1454 .register => |reg| {
1455 try self.genSetReg(arg.src, reg, arg_mcv);
1456 // TODO interact with the register allocator to mark the instruction as moved.
1457 },
1458 .stack_offset => {
1459 // Here we need to emit instructions like this:
1460 // mov qword ptr [rsp + stack_offset], x
1461 return self.fail(inst.base.src, "TODO implement calling with parameters in memory", .{});
1462 },
1463 .ptr_stack_offset => {
1464 return self.fail(inst.base.src, "TODO implement calling with MCValue.ptr_stack_offset arg", .{});
1465 },
1466 .ptr_embedded_in_code => {
1467 return self.fail(inst.base.src, "TODO implement calling with MCValue.ptr_embedded_in_code arg", .{});
1468 },
1469 .undef => unreachable,
1470 .immediate => unreachable,
1471 .unreach => unreachable,
1472 .dead => unreachable,
1473 .embedded_in_code => unreachable,
1474 .memory => unreachable,
1475 .compare_flags_signed => unreachable,
1476 .compare_flags_unsigned => unreachable,
1477 }
1478 }
1479
1480 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
1481 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
1482 const func = func_val.func;
1483 const got = &macho_file.sections.items[macho_file.got_section_index.?];
1484 const ptr_bytes = 8;
1485 const got_addr = @intCast(u32, got.addr + func.owner_decl.link.macho.offset_table_index.? * ptr_bytes);
1486 // ff 14 25 xx xx xx xx call [addr]
1487 try self.code.ensureCapacity(self.code.items.len + 7);
1488 self.code.appendSliceAssumeCapacity(&[3]u8{ 0xff, 0x14, 0x25 });
1489 mem.writeIntLittle(u32, self.code.addManyAsArrayAssumeCapacity(4), got_addr);
1490 } else {
1491 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
1492 }
1493 } else {
1494 return self.fail(inst.base.src, "TODO implement calling runtime known function pointer", .{});
1495 }
1496 },
14471497 .aarch64 => return self.fail(inst.base.src, "TODO implement codegen for call when linking with MachO for aarch64 arch", .{}),
14481498 else => unreachable,
14491499 }
......@@ -2486,6 +2536,11 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
24862536 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
24872537 const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes;
24882538 return MCValue{ .memory = got_addr };
2539 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
2540 const decl = payload.decl;
2541 const got = &macho_file.sections.items[macho_file.got_section_index.?];
2542 const got_addr = got.addr + decl.link.macho.offset_table_index.? * ptr_bytes;
2543 return MCValue{ .memory = got_addr };
24892544 } else {
24902545 return self.fail(src, "TODO codegen non-ELF const Decl pointer", .{});
24912546 }
src-self-hosted/link/MachO.zig+525-159
......@@ -18,36 +18,66 @@ const File = link.File;
1818
1919pub const base_tag: File.Tag = File.Tag.macho;
2020
21const LoadCommand = union(enum) {
22 Segment: macho.segment_command_64,
23 LinkeditData: macho.linkedit_data_command,
24 Symtab: macho.symtab_command,
25 Dysymtab: macho.dysymtab_command,
26
27 pub fn cmdsize(self: LoadCommand) u32 {
28 return switch (self) {
29 .Segment => |x| x.cmdsize,
30 .LinkeditData => |x| x.cmdsize,
31 .Symtab => |x| x.cmdsize,
32 .Dysymtab => |x| x.cmdsize,
33 };
34 }
35};
36
2137base: File,
2238
23/// List of all load command headers that are in the file.
24/// We use it to track number and size of all commands needed by the header.
25commands: std.ArrayListUnmanaged(macho.load_command) = std.ArrayListUnmanaged(macho.load_command){},
26command_file_offset: ?u64 = null,
39/// Table of all load commands
40load_commands: std.ArrayListUnmanaged(LoadCommand) = .{},
41segment_cmd_index: ?u16 = null,
42symtab_cmd_index: ?u16 = null,
43dysymtab_cmd_index: ?u16 = null,
44data_in_code_cmd_index: ?u16 = null,
2745
28/// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write.
29/// Same order as in the file.
30segments: std.ArrayListUnmanaged(macho.segment_command_64) = std.ArrayListUnmanaged(macho.segment_command_64){},
31/// Section (headers) *always* follow segment (load commands) directly!
32sections: std.ArrayListUnmanaged(macho.section_64) = std.ArrayListUnmanaged(macho.section_64){},
46/// Table of all sections
47sections: std.ArrayListUnmanaged(macho.section_64) = .{},
3348
34/// Offset (index) into __TEXT segment load command.
35text_segment_offset: ?u64 = null,
36/// Offset (index) into __LINKEDIT segment load command.
37linkedit_segment_offset: ?u664 = null,
49/// __TEXT segment sections
50text_section_index: ?u16 = null,
51cstring_section_index: ?u16 = null,
52const_text_section_index: ?u16 = null,
53stubs_section_index: ?u16 = null,
54stub_helper_section_index: ?u16 = null,
55
56/// __DATA segment sections
57got_section_index: ?u16 = null,
58const_data_section_index: ?u16 = null,
3859
39/// Entry point load command
40entry_point_cmd: ?macho.entry_point_command = null,
4160entry_addr: ?u64 = null,
4261
43/// The first 4GB of process' memory is reserved for the null (__PAGEZERO) segment.
44/// This is also the start address for our binary.
45vm_start_address: u64 = 0x100000000,
62/// Table of all symbols used.
63/// Internally references string table for names (which are optional).
64symbol_table: std.ArrayListUnmanaged(macho.nlist_64) = .{},
65
66/// Table of symbol names aka the string table.
67string_table: std.ArrayListUnmanaged(u8) = .{},
4668
47seg_table_dirty: bool = false,
69/// Table of symbol vaddr values. The values is the absolute vaddr value.
70/// If the vaddr of the executable __TEXT segment vaddr changes, the entire offset
71/// table needs to be rewritten.
72offset_table: std.ArrayListUnmanaged(u64) = .{},
4873
4974error_flags: File.ErrorFlags = File.ErrorFlags{},
5075
76cmd_table_dirty: bool = false,
77
78/// Pointer to the last allocated text block
79last_text_block: ?*TextBlock = null,
80
5181/// `alloc_num / alloc_den` is the factor of padding when allocating.
5282const alloc_num = 4;
5383const alloc_den = 3;
......@@ -67,7 +97,23 @@ const LIB_SYSTEM_NAME: [*:0]const u8 = "System";
6797const LIB_SYSTEM_PATH: [*:0]const u8 = DEFAULT_LIB_SEARCH_PATH ++ "/libSystem.B.dylib";
6898
6999pub const TextBlock = struct {
70 pub const empty = TextBlock{};
100 /// Index into the symbol table
101 symbol_table_index: ?u32,
102 /// Index into offset table
103 offset_table_index: ?u32,
104 /// Size of this text block
105 size: u64,
106 /// Points to the previous and next neighbours
107 prev: ?*TextBlock,
108 next: ?*TextBlock,
109
110 pub const empty = TextBlock{
111 .symbol_table_index = null,
112 .offset_table_index = null,
113 .size = 0,
114 .prev = null,
115 .next = null,
116 };
71117};
72118
73119pub const SrcFn = struct {
......@@ -117,6 +163,12 @@ fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO
117163/// Truncates the existing file contents and overwrites the contents.
118164/// Returns an error if `file` is not already open with +read +write +seek abilities.
119165fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !MachO {
166 switch (options.output_mode) {
167 .Exe => {},
168 .Obj => {},
169 .Lib => return error.TODOImplementWritingLibFiles,
170 }
171
120172 var self: MachO = .{
121173 .base = .{
122174 .file = file,
......@@ -127,104 +179,15 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Mach
127179 };
128180 errdefer self.deinit();
129181
130 switch (options.output_mode) {
131 .Exe => {
132 // The first segment command for executables is always a __PAGEZERO segment.
133 const pagezero = .{
134 .cmd = macho.LC_SEGMENT_64,
135 .cmdsize = commandSize(@sizeOf(macho.segment_command_64)),
136 .segname = makeString("__PAGEZERO"),
137 .vmaddr = 0,
138 .vmsize = self.vm_start_address,
139 .fileoff = 0,
140 .filesize = 0,
141 .maxprot = macho.VM_PROT_NONE,
142 .initprot = macho.VM_PROT_NONE,
143 .nsects = 0,
144 .flags = 0,
145 };
146 try self.commands.append(allocator, .{
147 .cmd = pagezero.cmd,
148 .cmdsize = pagezero.cmdsize,
149 });
150 try self.segments.append(allocator, pagezero);
151 },
152 .Obj => return error.TODOImplementWritingObjFiles,
153 .Lib => return error.TODOImplementWritingLibFiles,
154 }
155
156182 try self.populateMissingMetadata();
157183
158184 return self;
159185}
160186
161fn writeMachOHeader(self: *MachO) !void {
162 var hdr: macho.mach_header_64 = undefined;
163 hdr.magic = macho.MH_MAGIC_64;
164
165 const CpuInfo = struct {
166 cpu_type: macho.cpu_type_t,
167 cpu_subtype: macho.cpu_subtype_t,
168 };
169
170 const cpu_info: CpuInfo = switch (self.base.options.target.cpu.arch) {
171 .aarch64 => .{
172 .cpu_type = macho.CPU_TYPE_ARM64,
173 .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL,
174 },
175 .x86_64 => .{
176 .cpu_type = macho.CPU_TYPE_X86_64,
177 .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL,
178 },
179 else => return error.UnsupportedMachOArchitecture,
180 };
181 hdr.cputype = cpu_info.cpu_type;
182 hdr.cpusubtype = cpu_info.cpu_subtype;
183
184 const filetype: u32 = switch (self.base.options.output_mode) {
185 .Exe => macho.MH_EXECUTE,
186 .Obj => macho.MH_OBJECT,
187 .Lib => switch (self.base.options.link_mode) {
188 .Static => return error.TODOStaticLibMachOType,
189 .Dynamic => macho.MH_DYLIB,
190 },
191 };
192 hdr.filetype = filetype;
193
194 const ncmds = try math.cast(u32, self.commands.items.len);
195 hdr.ncmds = ncmds;
196
197 var sizeof_cmds: u32 = 0;
198 for (self.commands.items) |cmd| {
199 sizeof_cmds += cmd.cmdsize;
200 }
201 hdr.sizeofcmds = sizeof_cmds;
202
203 // TODO should these be set to something else?
204 hdr.flags = 0;
205 hdr.reserved = 0;
206
207 try self.base.file.?.pwriteAll(@ptrCast([*]const u8, &hdr)[0..@sizeOf(macho.mach_header_64)], 0);
208}
209
210187pub fn flush(self: *MachO, module: *Module) !void {
211 // Save segments first
212 {
213 const buf = try self.base.allocator.alloc(macho.segment_command_64, self.segments.items.len);
214 defer self.base.allocator.free(buf);
215
216 self.command_file_offset = @sizeOf(macho.mach_header_64);
217
218 for (buf) |*seg, i| {
219 seg.* = self.segments.items[i];
220 self.command_file_offset.? += self.segments.items[i].cmdsize;
221 }
222
223 try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), @sizeOf(macho.mach_header_64));
224 }
225
226188 switch (self.base.options.output_mode) {
227189 .Exe => {
190 var last_cmd_offset: usize = @sizeOf(macho.mach_header_64);
228191 {
229192 // Specify path to dynamic linker dyld
230193 const cmdsize = commandSize(@sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH));
......@@ -235,18 +198,14 @@ pub fn flush(self: *MachO, module: *Module) !void {
235198 .name = @sizeOf(macho.dylinker_command),
236199 },
237200 };
238 try self.commands.append(self.base.allocator, .{
239 .cmd = macho.LC_LOAD_DYLINKER,
240 .cmdsize = cmdsize,
241 });
242201
243 try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), self.command_file_offset.?);
202 try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), last_cmd_offset);
244203
245 const file_offset = self.command_file_offset.? + @sizeOf(macho.dylinker_command);
204 const file_offset = last_cmd_offset + @sizeOf(macho.dylinker_command);
246205 try self.addPadding(cmdsize - @sizeOf(macho.dylinker_command), file_offset);
247206
248207 try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), file_offset);
249 self.command_file_offset.? += cmdsize;
208 last_cmd_offset += cmdsize;
250209 }
251210
252211 {
......@@ -268,21 +227,44 @@ pub fn flush(self: *MachO, module: *Module) !void {
268227 .dylib = dylib,
269228 },
270229 };
271 try self.commands.append(self.base.allocator, .{
272 .cmd = macho.LC_LOAD_DYLIB,
273 .cmdsize = cmdsize,
274 });
275230
276 try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), self.command_file_offset.?);
231 try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), last_cmd_offset);
277232
278 const file_offset = self.command_file_offset.? + @sizeOf(macho.dylib_command);
233 const file_offset = last_cmd_offset + @sizeOf(macho.dylib_command);
279234 try self.addPadding(cmdsize - @sizeOf(macho.dylib_command), file_offset);
280235
281236 try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), file_offset);
282 self.command_file_offset.? += cmdsize;
237 last_cmd_offset += cmdsize;
283238 }
284239 },
285 .Obj => return error.TODOImplementWritingObjFiles,
240 .Obj => {
241 {
242 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab;
243 symtab.nsyms = @intCast(u32, self.symbol_table.items.len);
244 const allocated_size = self.allocatedSize(symtab.stroff);
245 const needed_size = self.string_table.items.len;
246 log.debug("allocated_size = 0x{x}, needed_size = 0x{x}\n", .{ allocated_size, needed_size });
247
248 if (needed_size > allocated_size) {
249 symtab.strsize = 0;
250 symtab.stroff = @intCast(u32, self.findFreeSpace(needed_size, 1));
251 }
252 symtab.strsize = @intCast(u32, needed_size);
253
254 log.debug("writing string table from 0x{x} to 0x{x}\n", .{ symtab.stroff, symtab.stroff + symtab.strsize });
255
256 try self.base.file.?.pwriteAll(self.string_table.items, symtab.stroff);
257 }
258
259 var last_cmd_offset: usize = @sizeOf(macho.mach_header_64);
260 for (self.load_commands.items) |cmd| {
261 const cmd_to_write = [1]@TypeOf(cmd){cmd};
262 try self.base.file.?.pwriteAll(mem.sliceAsBytes(cmd_to_write[0..1]), last_cmd_offset);
263 last_cmd_offset += cmd.cmdsize();
264 }
265 const off = @sizeOf(macho.mach_header_64) + @sizeOf(macho.segment_command_64);
266 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.sections.items), off);
267 },
286268 .Lib => return error.TODOImplementWritingLibFiles,
287269 }
288270
......@@ -297,14 +279,110 @@ pub fn flush(self: *MachO, module: *Module) !void {
297279}
298280
299281pub fn deinit(self: *MachO) void {
300 self.commands.deinit(self.base.allocator);
301 self.segments.deinit(self.base.allocator);
282 self.offset_table.deinit(self.base.allocator);
283 self.string_table.deinit(self.base.allocator);
284 self.symbol_table.deinit(self.base.allocator);
302285 self.sections.deinit(self.base.allocator);
286 self.load_commands.deinit(self.base.allocator);
287}
288
289pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
290 if (decl.link.macho.symbol_table_index) |_| return;
291
292 try self.symbol_table.ensureCapacity(self.base.allocator, self.symbol_table.items.len + 1);
293 try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1);
294
295 log.debug("allocating symbol index {} for {}\n", .{ self.symbol_table.items.len, decl.name });
296 decl.link.macho.symbol_table_index = @intCast(u32, self.symbol_table.items.len);
297 _ = self.symbol_table.addOneAssumeCapacity();
298
299 decl.link.macho.offset_table_index = @intCast(u32, self.offset_table.items.len);
300 _ = self.offset_table.addOneAssumeCapacity();
301
302 self.symbol_table.items[decl.link.macho.symbol_table_index.?] = .{
303 .n_strx = 0,
304 .n_type = 0,
305 .n_sect = 0,
306 .n_desc = 0,
307 .n_value = 0,
308 };
309 self.offset_table.items[decl.link.macho.offset_table_index.?] = 0;
303310}
304311
305pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {}
312pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
313 const tracy = trace(@src());
314 defer tracy.end();
315
316 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
317 defer code_buffer.deinit();
306318
307pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {}
319 var dbg_line_buffer = std.ArrayList(u8).init(self.base.allocator);
320 defer dbg_line_buffer.deinit();
321
322 var dbg_info_buffer = std.ArrayList(u8).init(self.base.allocator);
323 defer dbg_info_buffer.deinit();
324
325 var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{};
326 defer {
327 var it = dbg_info_type_relocs.iterator();
328 while (it.next()) |entry| {
329 entry.value.relocs.deinit(self.base.allocator);
330 }
331 dbg_info_type_relocs.deinit(self.base.allocator);
332 }
333
334 const typed_value = decl.typed_value.most_recent.typed_value;
335 const res = try codegen.generateSymbol(
336 &self.base,
337 decl.src(),
338 typed_value,
339 &code_buffer,
340 &dbg_line_buffer,
341 &dbg_info_buffer,
342 &dbg_info_type_relocs,
343 );
344
345 const code = switch (res) {
346 .externally_managed => |x| x,
347 .appended => code_buffer.items,
348 .fail => |em| {
349 decl.analysis = .codegen_failure;
350 try module.failed_decls.put(module.gpa, decl, em);
351 return;
352 },
353 };
354 log.debug("generated code {}\n", .{code});
355
356 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
357 const symbol = &self.symbol_table.items[decl.link.macho.symbol_table_index.?];
358
359 const decl_name = mem.spanZ(decl.name);
360 const name_str_index = try self.makeString(decl_name);
361 const addr = try self.allocateTextBlock(&decl.link.macho, code.len, required_alignment);
362 log.debug("allocated text block for {} at 0x{x}\n", .{ decl_name, addr });
363 log.debug("updated text section {}\n", .{self.sections.items[self.text_section_index.?]});
364
365 symbol.* = .{
366 .n_strx = name_str_index,
367 .n_type = macho.N_SECT,
368 .n_sect = @intCast(u8, self.text_section_index.?) + 1,
369 .n_desc = 0,
370 .n_value = addr,
371 };
372 self.offset_table.items[decl.link.macho.offset_table_index.?] = addr;
373
374 try self.writeSymbol(decl.link.macho.symbol_table_index.?);
375
376 const text_section = self.sections.items[self.text_section_index.?];
377 const section_offset = symbol.n_value - text_section.addr;
378 const file_offset = text_section.offset + section_offset;
379 log.debug("file_offset 0x{x}\n", .{file_offset});
380 try self.base.file.?.pwriteAll(code, file_offset);
381
382 // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated.
383 const decl_exports = module.decl_exports.get(decl) orelse &[0]*Module.Export{};
384 return self.updateDeclExports(module, decl, decl_exports);
385}
308386
309387pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void {}
310388
......@@ -313,51 +391,191 @@ pub fn updateDeclExports(
313391 module: *Module,
314392 decl: *const Module.Decl,
315393 exports: []const *Module.Export,
316) !void {}
394) !void {
395 const tracy = trace(@src());
396 defer tracy.end();
397
398 if (decl.link.macho.symbol_table_index == null) return;
399
400 var decl_sym = self.symbol_table.items[decl.link.macho.symbol_table_index.?];
401 // TODO implement
402 if (exports.len == 0) return;
403
404 const exp = exports[0];
405 self.entry_addr = decl_sym.n_value;
406 decl_sym.n_type |= macho.N_EXT;
407 exp.link.sym_index = 0;
408}
317409
318410pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {}
319411
320412pub fn getDeclVAddr(self: *MachO, decl: *const Module.Decl) u64 {
321 @panic("TODO implement getDeclVAddr for MachO");
413 return self.symbol_table.items[decl.link.macho.symbol_table_index.?].n_value;
322414}
323415
324416pub fn populateMissingMetadata(self: *MachO) !void {
325 if (self.text_segment_offset == null) {
326 self.text_segment_offset = @intCast(u64, self.segments.items.len);
327 const file_size = alignSize(u64, self.base.options.program_code_size_hint, 0x1000);
328 log.debug("vmsize/filesize = {}", .{file_size});
329 const file_offset = 0;
330 const vm_address = self.vm_start_address; // the end of __PAGEZERO segment in VM
331 const protection = macho.VM_PROT_READ | macho.VM_PROT_EXECUTE;
332 const cmdsize = commandSize(@sizeOf(macho.segment_command_64));
333 const text_segment = .{
334 .cmd = macho.LC_SEGMENT_64,
335 .cmdsize = cmdsize,
336 .segname = makeString("__TEXT"),
337 .vmaddr = vm_address,
338 .vmsize = file_size,
339 .fileoff = 0, // __TEXT segment *always* starts at 0 file offset
340 .filesize = 0, //file_size,
341 .maxprot = protection,
342 .initprot = protection,
343 .nsects = 0,
344 .flags = 0,
345 };
346 try self.commands.append(self.base.allocator, .{
347 .cmd = macho.LC_SEGMENT_64,
348 .cmdsize = cmdsize,
417 if (self.segment_cmd_index == null) {
418 self.segment_cmd_index = @intCast(u16, self.load_commands.items.len);
419 try self.load_commands.append(self.base.allocator, .{
420 .Segment = .{
421 .cmd = macho.LC_SEGMENT_64,
422 .cmdsize = @sizeOf(macho.segment_command_64),
423 .segname = makeStaticString(""),
424 .vmaddr = 0,
425 .vmsize = 0,
426 .fileoff = 0,
427 .filesize = 0,
428 .maxprot = 0,
429 .initprot = 0,
430 .nsects = 0,
431 .flags = 0,
432 },
433 });
434 self.cmd_table_dirty = true;
435 }
436 if (self.symtab_cmd_index == null) {
437 self.symtab_cmd_index = @intCast(u16, self.load_commands.items.len);
438 try self.load_commands.append(self.base.allocator, .{
439 .Symtab = .{
440 .cmd = macho.LC_SYMTAB,
441 .cmdsize = @sizeOf(macho.symtab_command),
442 .symoff = 0,
443 .nsyms = 0,
444 .stroff = 0,
445 .strsize = 0,
446 },
349447 });
350 try self.segments.append(self.base.allocator, text_segment);
448 self.cmd_table_dirty = true;
449 }
450 if (self.text_section_index == null) {
451 self.text_section_index = @intCast(u16, self.sections.items.len);
452 const segment = &self.load_commands.items[self.segment_cmd_index.?].Segment;
453 segment.cmdsize += @sizeOf(macho.section_64);
454 segment.nsects += 1;
455
456 const file_size = self.base.options.program_code_size_hint;
457 const off = @intCast(u32, self.findFreeSpace(file_size, 1));
458 const flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS;
459
460 log.debug("found __text section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
461
462 try self.sections.append(self.base.allocator, .{
463 .sectname = makeStaticString("__text"),
464 .segname = makeStaticString("__TEXT"),
465 .addr = 0,
466 .size = file_size,
467 .offset = off,
468 .@"align" = 0x1000,
469 .reloff = 0,
470 .nreloc = 0,
471 .flags = flags,
472 .reserved1 = 0,
473 .reserved2 = 0,
474 .reserved3 = 0,
475 });
476
477 segment.vmsize += file_size;
478 segment.filesize += file_size;
479 segment.fileoff = off;
480
481 log.debug("initial text section {}\n", .{self.sections.items[self.text_section_index.?]});
482 }
483 {
484 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab;
485 if (symtab.symoff == 0) {
486 const p_align = @sizeOf(macho.nlist_64);
487 const nsyms = self.base.options.symbol_count_hint;
488 const file_size = p_align * nsyms;
489 const off = @intCast(u32, self.findFreeSpace(file_size, p_align));
490 log.debug("found symbol table free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
491 symtab.symoff = off;
492 symtab.nsyms = @intCast(u32, nsyms);
493 }
494 if (symtab.stroff == 0) {
495 try self.string_table.append(self.base.allocator, 0);
496 const file_size = @intCast(u32, self.string_table.items.len);
497 const off = @intCast(u32, self.findFreeSpace(file_size, 1));
498 log.debug("found string table free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
499 symtab.stroff = off;
500 symtab.strsize = file_size;
501 }
351502 }
352503}
353504
354fn makeString(comptime bytes: []const u8) [16]u8 {
505fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
506 const segment = &self.load_commands.items[self.segment_cmd_index.?].Segment;
507 const text_section = &self.sections.items[self.text_section_index.?];
508 const new_block_ideal_capacity = new_block_size * alloc_num / alloc_den;
509
510 var block_placement: ?*TextBlock = null;
511 const addr = blk: {
512 if (self.last_text_block) |last| {
513 const last_symbol = self.symbol_table.items[last.symbol_table_index.?];
514 const ideal_capacity = last.size * alloc_num / alloc_den;
515 const ideal_capacity_end_addr = last_symbol.n_value + ideal_capacity;
516 const new_start_addr = mem.alignForwardGeneric(u64, ideal_capacity_end_addr, alignment);
517 block_placement = last;
518 break :blk new_start_addr;
519 } else {
520 break :blk text_section.addr;
521 }
522 };
523 log.debug("computed symbol address 0x{x}\n", .{addr});
524
525 const expand_text_section = block_placement == null or block_placement.?.next == null;
526 if (expand_text_section) {
527 const text_capacity = self.allocatedSize(text_section.offset);
528 const needed_size = (addr + new_block_size) - text_section.addr;
529 log.debug("text capacity 0x{x}, needed size 0x{x}\n", .{ text_capacity, needed_size });
530
531 if (needed_size > text_capacity) {
532 // TODO handle growth
533 }
534
535 self.last_text_block = text_block;
536 text_section.size = needed_size;
537 segment.vmsize = needed_size;
538 segment.filesize = needed_size;
539 if (alignment < text_section.@"align") {
540 text_section.@"align" = @intCast(u32, alignment);
541 }
542 }
543 text_block.size = new_block_size;
544
545 if (text_block.prev) |prev| {
546 prev.next = text_block.next;
547 }
548 if (text_block.next) |next| {
549 next.prev = text_block.prev;
550 }
551
552 if (block_placement) |big_block| {
553 text_block.prev = big_block;
554 text_block.next = big_block.next;
555 big_block.next = text_block;
556 } else {
557 text_block.prev = null;
558 text_block.next = null;
559 }
560
561 return addr;
562}
563
564fn makeStaticString(comptime bytes: []const u8) [16]u8 {
355565 var buf = [_]u8{0} ** 16;
356 if (bytes.len > buf.len) @compileError("MachO segment/section name too long");
566 if (bytes.len > buf.len) @compileError("string too long; max 16 bytes");
357567 mem.copy(u8, buf[0..], bytes);
358568 return buf;
359569}
360570
571fn makeString(self: *MachO, bytes: []const u8) !u32 {
572 try self.string_table.ensureCapacity(self.base.allocator, self.string_table.items.len + bytes.len + 1);
573 const result = self.string_table.items.len;
574 self.string_table.appendSliceAssumeCapacity(bytes);
575 self.string_table.appendAssumeCapacity(0);
576 return @intCast(u32, result);
577}
578
361579fn alignSize(comptime Int: type, min_size: anytype, alignment: Int) Int {
362580 const size = @intCast(Int, min_size);
363581 if (size % alignment == 0) return size;
......@@ -370,7 +588,7 @@ fn commandSize(min_size: anytype) u32 {
370588 return alignSize(u32, min_size, @sizeOf(u64));
371589}
372590
373fn addPadding(self: *MachO, size: u32, file_offset: u64) !void {
591fn addPadding(self: *MachO, size: u64, file_offset: u64) !void {
374592 if (size == 0) return;
375593
376594 const buf = try self.base.allocator.alloc(u8, size);
......@@ -380,3 +598,151 @@ fn addPadding(self: *MachO, size: u32, file_offset: u64) !void {
380598
381599 try self.base.file.?.pwriteAll(buf, file_offset);
382600}
601
602fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
603 const hdr_size: u64 = @sizeOf(macho.mach_header_64);
604 if (start < hdr_size)
605 return hdr_size;
606
607 const end = start + satMul(size, alloc_num) / alloc_den;
608
609 {
610 const off = @sizeOf(macho.mach_header_64);
611 var tight_size: u64 = 0;
612 for (self.load_commands.items) |cmd| {
613 tight_size += cmd.cmdsize();
614 }
615 const increased_size = satMul(tight_size, alloc_num) / alloc_den;
616 const test_end = off + increased_size;
617 if (end > off and start < test_end) {
618 return test_end;
619 }
620 }
621
622 for (self.sections.items) |section| {
623 const increased_size = satMul(section.size, alloc_num) / alloc_den;
624 const test_end = section.offset + increased_size;
625 if (end > section.offset and start < test_end) {
626 return test_end;
627 }
628 }
629
630 if (self.symtab_cmd_index) |symtab_index| {
631 const symtab = self.load_commands.items[symtab_index].Symtab;
632 {
633 const tight_size = @sizeOf(macho.nlist_64) * symtab.nsyms;
634 const increased_size = satMul(tight_size, alloc_num) / alloc_den;
635 const test_end = symtab.symoff + increased_size;
636 if (end > symtab.symoff and start < test_end) {
637 return test_end;
638 }
639 }
640 {
641 const increased_size = satMul(symtab.strsize, alloc_num) / alloc_den;
642 const test_end = symtab.stroff + increased_size;
643 if (end > symtab.stroff and start < test_end) {
644 return test_end;
645 }
646 }
647 }
648
649 return null;
650}
651
652fn allocatedSize(self: *MachO, start: u64) u64 {
653 if (start == 0)
654 return 0;
655 var min_pos: u64 = std.math.maxInt(u64);
656 {
657 const off = @sizeOf(macho.mach_header_64);
658 if (off > start and off < min_pos) min_pos = off;
659 }
660 for (self.sections.items) |section| {
661 if (section.offset <= start) continue;
662 if (section.offset < min_pos) min_pos = section.offset;
663 }
664 if (self.symtab_cmd_index) |symtab_index| {
665 const symtab = self.load_commands.items[symtab_index].Symtab;
666 if (symtab.symoff > start and symtab.symoff < min_pos) min_pos = symtab.symoff;
667 if (symtab.stroff > start and symtab.stroff < min_pos) min_pos = symtab.stroff;
668 }
669 return min_pos - start;
670}
671
672fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u16) u64 {
673 var start: u64 = 0;
674 while (self.detectAllocCollision(start, object_size)) |item_end| {
675 start = mem.alignForwardGeneric(u64, item_end, min_alignment);
676 }
677 return start;
678}
679
680fn writeSymbol(self: *MachO, index: usize) !void {
681 const tracy = trace(@src());
682 defer tracy.end();
683
684 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab;
685 var sym = [1]macho.nlist_64{self.symbol_table.items[index]};
686 const off = symtab.symoff + @sizeOf(macho.nlist_64) * index;
687 log.debug("writing symbol {} at 0x{x}\n", .{ sym[0], off });
688 try self.base.file.?.pwriteAll(mem.sliceAsBytes(sym[0..1]), off);
689}
690
691/// Writes Mach-O file header.
692/// Should be invoked last as it needs up-to-date values of ncmds and sizeof_cmds bookkeeping
693/// variables.
694fn writeMachOHeader(self: *MachO) !void {
695 var hdr: macho.mach_header_64 = undefined;
696 hdr.magic = macho.MH_MAGIC_64;
697
698 const CpuInfo = struct {
699 cpu_type: macho.cpu_type_t,
700 cpu_subtype: macho.cpu_subtype_t,
701 };
702
703 const cpu_info: CpuInfo = switch (self.base.options.target.cpu.arch) {
704 .aarch64 => .{
705 .cpu_type = macho.CPU_TYPE_ARM64,
706 .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL,
707 },
708 .x86_64 => .{
709 .cpu_type = macho.CPU_TYPE_X86_64,
710 .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL,
711 },
712 else => return error.UnsupportedMachOArchitecture,
713 };
714 hdr.cputype = cpu_info.cpu_type;
715 hdr.cpusubtype = cpu_info.cpu_subtype;
716
717 const filetype: u32 = switch (self.base.options.output_mode) {
718 .Exe => macho.MH_EXECUTE,
719 .Obj => macho.MH_OBJECT,
720 .Lib => switch (self.base.options.link_mode) {
721 .Static => return error.TODOStaticLibMachOType,
722 .Dynamic => macho.MH_DYLIB,
723 },
724 };
725 hdr.filetype = filetype;
726 hdr.ncmds = @intCast(u32, self.load_commands.items.len);
727
728 var sizeofcmds: u32 = 0;
729 for (self.load_commands.items) |cmd| {
730 sizeofcmds += cmd.cmdsize();
731 }
732
733 hdr.sizeofcmds = sizeofcmds;
734
735 // TODO should these be set to something else?
736 hdr.flags = 0;
737 hdr.reserved = 0;
738
739 log.debug("writing Mach-O header {}\n", .{hdr});
740
741 try self.base.file.?.pwriteAll(@ptrCast([*]const u8, &hdr)[0..@sizeOf(macho.mach_header_64)], 0);
742}
743
744/// Saturating multiplication
745fn satMul(a: anytype, b: anytype) @TypeOf(a, b) {
746 const T = @TypeOf(a, b);
747 return std.math.mul(T, a, b) catch std.math.maxInt(T);
748}