| ... | ... | @@ -16,8 +16,6 @@ const Module = @import("../Module.zig"); |
| 16 | 16 | const link = @import("../link.zig"); |
| 17 | 17 | const File = link.File; |
| 18 | 18 | |
| 19 | | const is_darwin = std.Target.current.os.tag.isDarwin(); |
| 20 | | |
| 21 | 19 | pub const base_tag: File.Tag = File.Tag.macho; |
| 22 | 20 | |
| 23 | 21 | base: File, |
| ... | ... | @@ -227,63 +225,61 @@ pub fn flush(self: *MachO, module: *Module) !void { |
| 227 | 225 | |
| 228 | 226 | switch (self.base.options.output_mode) { |
| 229 | 227 | .Exe => { |
| 230 | | if (is_darwin) { |
| 231 | | { |
| 232 | | // Specify path to dynamic linker dyld |
| 233 | | const cmdsize = commandSize(@sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH)); |
| 234 | | const load_dylinker = [1]macho.dylinker_command{ |
| 235 | | .{ |
| 236 | | .cmd = macho.LC_LOAD_DYLINKER, |
| 237 | | .cmdsize = cmdsize, |
| 238 | | .name = @sizeOf(macho.dylinker_command), |
| 239 | | }, |
| 240 | | }; |
| 241 | | try self.commands.append(self.base.allocator, .{ |
| 228 | { |
| 229 | // Specify path to dynamic linker dyld |
| 230 | const cmdsize = commandSize(@sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH)); |
| 231 | const load_dylinker = [1]macho.dylinker_command{ |
| 232 | .{ |
| 242 | 233 | .cmd = macho.LC_LOAD_DYLINKER, |
| 243 | 234 | .cmdsize = cmdsize, |
| 244 | | }); |
| 245 | | |
| 246 | | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), self.command_file_offset.?); |
| 247 | | |
| 248 | | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylinker_command); |
| 249 | | try self.addPadding(cmdsize - @sizeOf(macho.dylinker_command), file_offset); |
| 250 | | |
| 251 | | try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), file_offset); |
| 252 | | self.command_file_offset.? += cmdsize; |
| 253 | | } |
| 254 | | |
| 255 | | { |
| 256 | | // Link against libSystem |
| 257 | | const cmdsize = commandSize(@sizeOf(macho.dylib_command) + mem.lenZ(LIB_SYSTEM_PATH)); |
| 258 | | // According to Apple's manual, we should obtain current libSystem version using libc call |
| 259 | | // NSVersionOfRunTimeLibrary. |
| 260 | | const version = std.c.NSVersionOfRunTimeLibrary(LIB_SYSTEM_NAME); |
| 261 | | const dylib = .{ |
| 262 | | .name = @sizeOf(macho.dylib_command), |
| 263 | | .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files |
| 264 | | .current_version = version, |
| 265 | | .compatibility_version = 0x10000, // not sure why this either; value from reverse engineering |
| 266 | | }; |
| 267 | | const load_dylib = [1]macho.dylib_command{ |
| 268 | | .{ |
| 269 | | .cmd = macho.LC_LOAD_DYLIB, |
| 270 | | .cmdsize = cmdsize, |
| 271 | | .dylib = dylib, |
| 272 | | }, |
| 273 | | }; |
| 274 | | try self.commands.append(self.base.allocator, .{ |
| 235 | .name = @sizeOf(macho.dylinker_command), |
| 236 | }, |
| 237 | }; |
| 238 | try self.commands.append(self.base.allocator, .{ |
| 239 | .cmd = macho.LC_LOAD_DYLINKER, |
| 240 | .cmdsize = cmdsize, |
| 241 | }); |
| 242 | |
| 243 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylinker[0..1]), self.command_file_offset.?); |
| 244 | |
| 245 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylinker_command); |
| 246 | try self.addPadding(cmdsize - @sizeOf(macho.dylinker_command), file_offset); |
| 247 | |
| 248 | try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), file_offset); |
| 249 | self.command_file_offset.? += cmdsize; |
| 250 | } |
| 251 | |
| 252 | { |
| 253 | // Link against libSystem |
| 254 | const cmdsize = commandSize(@sizeOf(macho.dylib_command) + mem.lenZ(LIB_SYSTEM_PATH)); |
| 255 | // TODO Find a way to work out runtime version from the OS version triple stored in std.Target. |
| 256 | // In the meantime, we're gonna hardcode to the minimum compatibility version of 1.0.0. |
| 257 | const min_version = 0x10000; |
| 258 | const dylib = .{ |
| 259 | .name = @sizeOf(macho.dylib_command), |
| 260 | .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files |
| 261 | .current_version = min_version, |
| 262 | .compatibility_version = min_version, |
| 263 | }; |
| 264 | const load_dylib = [1]macho.dylib_command{ |
| 265 | .{ |
| 275 | 266 | .cmd = macho.LC_LOAD_DYLIB, |
| 276 | 267 | .cmdsize = cmdsize, |
| 277 | | }); |
| 268 | .dylib = dylib, |
| 269 | }, |
| 270 | }; |
| 271 | try self.commands.append(self.base.allocator, .{ |
| 272 | .cmd = macho.LC_LOAD_DYLIB, |
| 273 | .cmdsize = cmdsize, |
| 274 | }); |
| 278 | 275 | |
| 279 | | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), self.command_file_offset.?); |
| 276 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(load_dylib[0..1]), self.command_file_offset.?); |
| 280 | 277 | |
| 281 | | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylib_command); |
| 282 | | try self.addPadding(cmdsize - @sizeOf(macho.dylib_command), file_offset); |
| 278 | const file_offset = self.command_file_offset.? + @sizeOf(macho.dylib_command); |
| 279 | try self.addPadding(cmdsize - @sizeOf(macho.dylib_command), file_offset); |
| 283 | 280 | |
| 284 | | try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), file_offset); |
| 285 | | self.command_file_offset.? += cmdsize; |
| 286 | | } |
| 281 | try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), file_offset); |
| 282 | self.command_file_offset.? += cmdsize; |
| 287 | 283 | } |
| 288 | 284 | }, |
| 289 | 285 | .Obj => return error.TODOImplementWritingObjFiles, |