| ... | ... | @@ -287,9 +287,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { |
| 287 | 287 | } |
| 288 | 288 | const cmd = &self.load_commands.items[self.dylinker_cmd_index.?].Dylinker; |
| 289 | 289 | off += cmd.name; |
| 290 | | const padding = cmd.cmdsize - @sizeOf(macho.dylinker_command); |
| 291 | | log.debug("writing LC_LOAD_DYLINKER padding of size {} at 0x{x}\n", .{ padding, off }); |
| 292 | | try self.addPadding(padding, off); |
| 293 | 290 | log.debug("writing LC_LOAD_DYLINKER path to dyld at 0x{x}\n", .{off}); |
| 294 | 291 | try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), off); |
| 295 | 292 | } |
| ... | ... | @@ -302,9 +299,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { |
| 302 | 299 | } |
| 303 | 300 | const cmd = &self.load_commands.items[self.libsystem_cmd_index.?].Dylib; |
| 304 | 301 | off += cmd.dylib.name; |
| 305 | | const padding = cmd.cmdsize - @sizeOf(macho.dylib_command); |
| 306 | | log.debug("writing LC_LOAD_DYLIB padding of size {} at 0x{x}\n", .{ padding, off }); |
| 307 | | try self.addPadding(padding, off); |
| 308 | 302 | log.debug("writing LC_LOAD_DYLIB path to libSystem at 0x{x}\n", .{off}); |
| 309 | 303 | try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), off); |
| 310 | 304 | } |
| ... | ... | @@ -1264,21 +1258,6 @@ fn updateString(self: *MachO, old_str_off: u32, new_name: []const u8) !u32 { |
| 1264 | 1258 | return self.makeString(new_name); |
| 1265 | 1259 | } |
| 1266 | 1260 | |
| 1267 | | /// TODO This should not heap allocate, instead it should utilize a fixed size, statically allocated |
| 1268 | | /// global const array. You could even use pwritev to write the same buffer multiple times with only |
| 1269 | | /// 1 syscall if you needed to, for example, write 8192 bytes using a buffer of only 4096 bytes. |
| 1270 | | /// This size parameter should probably be a usize not u64. |
| 1271 | | fn addPadding(self: *MachO, size: u64, file_offset: u64) !void { |
| 1272 | | if (size == 0) return; |
| 1273 | | |
| 1274 | | const buf = try self.base.allocator.alloc(u8, @intCast(usize, size)); |
| 1275 | | defer self.base.allocator.free(buf); |
| 1276 | | |
| 1277 | | mem.set(u8, buf[0..], 0); |
| 1278 | | |
| 1279 | | try self.base.file.?.pwriteAll(buf, file_offset); |
| 1280 | | } |
| 1281 | | |
| 1282 | 1261 | fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 { |
| 1283 | 1262 | const hdr_size: u64 = @sizeOf(macho.mach_header_64); |
| 1284 | 1263 | if (start < hdr_size) return hdr_size; |