| ... | @@ -30,24 +30,26 @@ command_file_offset: ?u64 = null, | ... | @@ -30,24 +30,26 @@ command_file_offset: ?u64 = null, |
| 30 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. | 30 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| 31 | /// Same order as in the file. | 31 | /// Same order as in the file. |
| 32 | segments: std.ArrayListUnmanaged(macho.segment_command_64) = std.ArrayListUnmanaged(macho.segment_command_64){}, | 32 | segments: std.ArrayListUnmanaged(macho.segment_command_64) = std.ArrayListUnmanaged(macho.segment_command_64){}, |
| | 33 | /// Section (headers) *always* follow segment (load commands) directly! |
| 33 | sections: std.ArrayListUnmanaged(macho.section_64) = std.ArrayListUnmanaged(macho.section_64){}, | 34 | sections: std.ArrayListUnmanaged(macho.section_64) = std.ArrayListUnmanaged(macho.section_64){}, |
| 34 | segment_table_offset: ?u64 = null, | 35 | |
| | 36 | /// Offset (index) into __TEXT segment load command. |
| | 37 | text_segment_offset: ?u64 = null, |
| | 38 | /// Offset (index) into __LINKEDIT segment load command. |
| | 39 | linkedit_segment_offset: ?u664 = null, |
| 35 | | 40 | |
| 36 | /// Entry point load command | 41 | /// Entry point load command |
| 37 | entry_point_cmd: ?macho.entry_point_command = null, | 42 | entry_point_cmd: ?macho.entry_point_command = null, |
| 38 | entry_addr: ?u64 = null, | 43 | entry_addr: ?u64 = null, |
| 39 | | 44 | |
| 40 | /// Default VM start address set at 4GB | 45 | /// The first 4GB of process' memory is reserved for the null (__PAGEZERO) segment. |
| | 46 | /// This is also the start address for our binary. |
| 41 | vm_start_address: u64 = 0x100000000, | 47 | vm_start_address: u64 = 0x100000000, |
| 42 | | 48 | |
| 43 | seg_table_dirty: bool = false, | 49 | seg_table_dirty: bool = false, |
| 44 | | 50 | |
| 45 | error_flags: File.ErrorFlags = File.ErrorFlags{}, | 51 | error_flags: File.ErrorFlags = File.ErrorFlags{}, |
| 46 | | 52 | |
| 47 | /// TODO ultimately this will be propagated down from main() and set (in this form or another) | | |
| 48 | /// when user links against system lib. | | |
| 49 | link_against_system: bool = false, | | |
| 50 | | | |
| 51 | /// `alloc_num / alloc_den` is the factor of padding when allocating. | 53 | /// `alloc_num / alloc_den` is the factor of padding when allocating. |
| 52 | const alloc_num = 4; | 54 | const alloc_num = 4; |
| 53 | const alloc_den = 3; | 55 | const alloc_den = 3; |
| ... | @@ -138,8 +140,8 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Mach | ... | @@ -138,8 +140,8 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Mach |
| 138 | .vmsize = self.vm_start_address, | 140 | .vmsize = self.vm_start_address, |
| 139 | .fileoff = 0, | 141 | .fileoff = 0, |
| 140 | .filesize = 0, | 142 | .filesize = 0, |
| 141 | .maxprot = 0, | 143 | .maxprot = macho.VM_PROT_NONE, |
| 142 | .initprot = 0, | 144 | .initprot = macho.VM_PROT_NONE, |
| 143 | .nsects = 0, | 145 | .nsects = 0, |
| 144 | .flags = 0, | 146 | .flags = 0, |
| 145 | }; | 147 | }; |
| ... | @@ -225,66 +227,62 @@ pub fn flush(self: *MachO, module: *Module) !void { | ... | @@ -225,66 +227,62 @@ pub fn flush(self: *MachO, module: *Module) !void { |
| 225 | | 227 | |
| 226 | switch (self.base.options.output_mode) { | 228 | switch (self.base.options.output_mode) { |
| 227 | .Exe => { | 229 | .Exe => { |
| 228 | if (self.link_against_system) { | 230 | if (is_darwin) { |
| 229 | if (is_darwin) { | 231 | { |
| 230 | { | 232 | // Specify path to dynamic linker dyld |
| 231 | // Specify path to dynamic linker dyld | 233 | const cmdsize = commandSize(@sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH)); |
| 232 | const cmdsize = commandSize(@intCast(u32, @sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH))); | 234 | const load_dylinker = [1]macho.dylinker_command{ |
| 233 | const load_dylinker = [1]macho.dylinker_command{ | 235 | .{ |
| 234 | .{ | | |
| 235 | .cmd = macho.LC_LOAD_DYLINKER, | | |
| 236 | .cmdsize = cmdsize, | | |
| 237 | .name = @sizeOf(macho.dylinker_command), | | |
| 238 | }, | | |
| 239 | }; | | |
| 240 | try self.commands.append(self.base.allocator, .{ | | |
| 241 | .cmd = macho.LC_LOAD_DYLINKER, | 236 | .cmd = macho.LC_LOAD_DYLINKER, |
| 242 | .cmdsize = cmdsize, | 237 | .cmdsize = cmdsize, |
| 243 | }); | 238 | .name = @sizeOf(macho.dylinker_command), |
| 244 | | 239 | }, |
| 245 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), self.command_file_offset.?); | 240 | }; |
| 246 | | 241 | try self.commands.append(self.base.allocator, .{ |
| 247 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylinker_command); | 242 | .cmd = macho.LC_LOAD_DYLINKER, |
| 248 | try self.addPadding(cmdsize - @sizeOf(macho.dylinker_command), file_offset); | 243 | .cmdsize = cmdsize, |
| 249 | | 244 | }); |
| 250 | try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), file_offset); | 245 | |
| 251 | self.command_file_offset.? += cmdsize; | 246 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), self.command_file_offset.?); |
| 252 | } | 247 | |
| 253 | | 248 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylinker_command); |
| 254 | { | 249 | try self.addPadding(cmdsize - @sizeOf(macho.dylinker_command), file_offset); |
| 255 | // Link against libSystem | 250 | |
| 256 | const cmdsize = commandSize(@intCast(u32, @sizeOf(macho.dylib_command) + mem.lenZ(LIB_SYSTEM_PATH))); | 251 | try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), file_offset); |
| 257 | // According to Apple's manual, we should obtain current libSystem version using libc call | 252 | self.command_file_offset.? += cmdsize; |
| 258 | // NSVersionOfRunTimeLibrary. | 253 | } |
| 259 | const version = std.c.NSVersionOfRunTimeLibrary(LIB_SYSTEM_NAME); | 254 | |
| 260 | const dylib = .{ | 255 | { |
| 261 | .name = @sizeOf(macho.dylib_command), | 256 | // Link against libSystem |
| 262 | .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files | 257 | const cmdsize = commandSize(@sizeOf(macho.dylib_command) + mem.lenZ(LIB_SYSTEM_PATH)); |
| 263 | .current_version = version, | 258 | // According to Apple's manual, we should obtain current libSystem version using libc call |
| 264 | .compatibility_version = 0x10000, // not sure why this either; value from reverse engineering | 259 | // NSVersionOfRunTimeLibrary. |
| 265 | }; | 260 | const version = std.c.NSVersionOfRunTimeLibrary(LIB_SYSTEM_NAME); |
| 266 | const load_dylib = [1]macho.dylib_command{ | 261 | const dylib = .{ |
| 267 | .{ | 262 | .name = @sizeOf(macho.dylib_command), |
| 268 | .cmd = macho.LC_LOAD_DYLIB, | 263 | .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files |
| 269 | .cmdsize = cmdsize, | 264 | .current_version = version, |
| 270 | .dylib = dylib, | 265 | .compatibility_version = 0x10000, // not sure why this either; value from reverse engineering |
| 271 | }, | 266 | }; |
| 272 | }; | 267 | const load_dylib = [1]macho.dylib_command{ |
| 273 | try self.commands.append(self.base.allocator, .{ | 268 | .{ |
| 274 | .cmd = macho.LC_LOAD_DYLIB, | 269 | .cmd = macho.LC_LOAD_DYLIB, |
| 275 | .cmdsize = cmdsize, | 270 | .cmdsize = cmdsize, |
| 276 | }); | 271 | .dylib = dylib, |
| | 272 | }, |
| | 273 | }; |
| | 274 | try self.commands.append(self.base.allocator, .{ |
| | 275 | .cmd = macho.LC_LOAD_DYLIB, |
| | 276 | .cmdsize = cmdsize, |
| | 277 | }); |
| 277 | | 278 | |
| 278 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), self.command_file_offset.?); | 279 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), self.command_file_offset.?); |
| 279 | | 280 | |
| 280 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylib_command); | 281 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylib_command); |
| 281 | try self.addPadding(cmdsize - @sizeOf(macho.dylib_command), file_offset); | 282 | try self.addPadding(cmdsize - @sizeOf(macho.dylib_command), file_offset); |
| 282 | | 283 | |
| 283 | try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), file_offset); | 284 | try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), file_offset); |
| 284 | self.command_file_offset.? += cmdsize; | 285 | self.command_file_offset.? += cmdsize; |
| 285 | } | | |
| 286 | } else { | | |
| 287 | @panic("linking against libSystem on non-native target is unsupported"); | | |
| 288 | } | 286 | } |
| 289 | } | 287 | } |
| 290 | }, | 288 | }, |
| ... | @@ -327,20 +325,53 @@ pub fn getDeclVAddr(self: *MachO, decl: *const Module.Decl) u64 { | ... | @@ -327,20 +325,53 @@ pub fn getDeclVAddr(self: *MachO, decl: *const Module.Decl) u64 { |
| 327 | @panic("TODO implement getDeclVAddr for MachO"); | 325 | @panic("TODO implement getDeclVAddr for MachO"); |
| 328 | } | 326 | } |
| 329 | | 327 | |
| 330 | pub fn populateMissingMetadata(self: *MachO) !void {} | 328 | pub fn populateMissingMetadata(self: *MachO) !void { |
| | 329 | if (self.text_segment_offset == null) { |
| | 330 | self.text_segment_offset = @intCast(u64, self.segments.items.len); |
| | 331 | const file_size = alignSize(u64, self.base.options.program_code_size_hint, 0x1000); |
| | 332 | log.debug("vmsize/filesize = {}", .{file_size}); |
| | 333 | const file_offset = 0; |
| | 334 | const vm_address = self.vm_start_address; // the end of __PAGEZERO segment in VM |
| | 335 | const protection = macho.VM_PROT_READ | macho.VM_PROT_EXECUTE; |
| | 336 | const cmdsize = commandSize(@sizeOf(macho.segment_command_64)); |
| | 337 | const text_segment = .{ |
| | 338 | .cmd = macho.LC_SEGMENT_64, |
| | 339 | .cmdsize = cmdsize, |
| | 340 | .segname = makeString("__TEXT"), |
| | 341 | .vmaddr = vm_address, |
| | 342 | .vmsize = file_size, |
| | 343 | .fileoff = 0, // __TEXT segment *always* starts at 0 file offset |
| | 344 | .filesize = 0, //file_size, |
| | 345 | .maxprot = protection, |
| | 346 | .initprot = protection, |
| | 347 | .nsects = 0, |
| | 348 | .flags = 0, |
| | 349 | }; |
| | 350 | try self.commands.append(self.base.allocator, .{ |
| | 351 | .cmd = macho.LC_SEGMENT_64, |
| | 352 | .cmdsize = cmdsize, |
| | 353 | }); |
| | 354 | try self.segments.append(self.base.allocator, text_segment); |
| | 355 | } |
| | 356 | } |
| 331 | | 357 | |
| 332 | fn makeString(comptime bytes: []const u8) [16]u8 { | 358 | fn makeString(comptime bytes: []const u8) [16]u8 { |
| 333 | var buf: [16]u8 = undefined; | 359 | var buf = [_]u8{0} ** 16; |
| 334 | if (bytes.len > buf.len) @compileError("MachO segment/section name too long"); | 360 | if (bytes.len > buf.len) @compileError("MachO segment/section name too long"); |
| 335 | mem.copy(u8, buf[0..], bytes); | 361 | mem.copy(u8, buf[0..], bytes); |
| 336 | return buf; | 362 | return buf; |
| 337 | } | 363 | } |
| 338 | | 364 | |
| 339 | fn commandSize(min_size: u32) u32 { | 365 | fn alignSize(comptime Int: type, min_size: anytype, alignment: Int) Int { |
| 340 | if (min_size % @sizeOf(u64) == 0) return min_size; | 366 | const size = @intCast(Int, min_size); |
| | 367 | if (size % alignment == 0) return size; |
| | 368 | |
| | 369 | const div = size / alignment; |
| | 370 | return (div + 1) * alignment; |
| | 371 | } |
| 341 | | 372 | |
| 342 | const div = min_size / @sizeOf(u64); | 373 | fn commandSize(min_size: anytype) u32 { |
| 343 | return (div + 1) * @sizeOf(u64); | 374 | return alignSize(u32, min_size, @sizeOf(u64)); |
| 344 | } | 375 | } |
| 345 | | 376 | |
| 346 | fn addPadding(self: *MachO, size: u32, file_offset: u64) !void { | 377 | fn addPadding(self: *MachO, size: u32, file_offset: u64) !void { |