authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-10-11 16:03:39+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-10-13 08:38:17+02:00
logadfd298e44be707541b13e2d8f1edc5b237fa674
tree49009639aa28ca9c2fc7f5594ebe1a49fb805521
parent951721343f0e04f39df0a049b93c774c3cde0741

Do not rewrite paths to dyld and libSystem unless changed


1 files changed, 26 insertions(+), 13 deletions(-)

src/link/MachO.zig+26-13
......@@ -133,6 +133,8 @@ offset_table: std.ArrayListUnmanaged(u64) = .{},
133133error_flags: File.ErrorFlags = File.ErrorFlags{},
134134
135135cmd_table_dirty: bool = false,
136dylinker_cmd_dirty: bool = false,
137libsystem_cmd_dirty: bool = false,
136138
137139/// A list of text blocks that have surplus capacity. This list can have false
138140/// positives, as functions grow and shrink over time, only sometimes being added
......@@ -318,14 +320,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
318320 .Exe => {
319321 // Write export trie.
320322 try self.writeExportTrie();
321
322323 if (self.entry_addr) |addr| {
323 // Update LC_MAIN with entry offset
324 // Update LC_MAIN with entry offset.
324325 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
325326 const main_cmd = &self.load_commands.items[self.main_cmd_index.?].EntryPoint;
326327 main_cmd.entryoff = addr - text_segment.vmaddr;
327328 }
328
329329 {
330330 // Update dynamic symbol table.
331331 const nlocals = @intCast(u32, self.local_symbols.items.len);
......@@ -338,7 +338,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
338338 dysymtab.iundefsym = nlocals + nglobals;
339339 dysymtab.nundefsym = nundefs;
340340 }
341 {
341 if (self.dylinker_cmd_dirty) {
342342 // Write path to dyld loader.
343343 var off: usize = @sizeOf(macho.mach_header_64);
344344 for (self.load_commands.items) |cmd| {
......@@ -349,8 +349,9 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
349349 off += cmd.name;
350350 log.debug("writing LC_LOAD_DYLINKER path to dyld at 0x{x}\n", .{off});
351351 try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), off);
352 self.dylinker_cmd_dirty = false;
352353 }
353 {
354 if (self.libsystem_cmd_dirty) {
354355 // Write path to libSystem.
355356 var off: usize = @sizeOf(macho.mach_header_64);
356357 for (self.load_commands.items) |cmd| {
......@@ -361,6 +362,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
361362 off += cmd.dylib.name;
362363 log.debug("writing LC_LOAD_DYLIB path to libSystem at 0x{x}\n", .{off});
363364 try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), off);
365 self.libsystem_cmd_dirty = false;
364366 }
365367 },
366368 .Obj => {},
......@@ -376,7 +378,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
376378 symtab.nsyms = nlocals + nglobals + nundefs;
377379 }
378380
379 if (self.cmd_table_dirty) try self.writeCmdHeaders();
381 if (self.cmd_table_dirty) {
382 try self.writeCmdHeaders();
383 try self.writeMachOHeader();
384 self.cmd_table_dirty = false;
385 }
380386
381387 if (self.entry_addr == null and self.base.options.output_mode == .Exe) {
382388 log.debug("flushing. no_entry_point_found = true\n", .{});
......@@ -384,8 +390,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
384390 } else {
385391 log.debug("flushing. no_entry_point_found = false\n", .{});
386392 self.error_flags.no_entry_point_found = false;
387 try self.writeMachOHeader();
388393 }
394
395 assert(!self.cmd_table_dirty);
396 assert(!self.dylinker_cmd_dirty);
397 assert(!self.libsystem_cmd_dirty);
389398}
390399
391400fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
......@@ -846,7 +855,7 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
846855 try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1);
847856
848857 if (self.local_symbol_free_list.popOrNull()) |i| {
849 log.debug("reusing symbol index {} for {}\n", .{i, decl.name});
858 log.debug("reusing symbol index {} for {}\n", .{ i, decl.name });
850859 decl.link.macho.local_sym_index = i;
851860 } else {
852861 log.debug("allocating symbol index {} for {}\n", .{ self.local_symbols.items.len, decl.name });
......@@ -980,6 +989,7 @@ pub fn updateDeclExports(
980989 .Strong => blk: {
981990 if (mem.eql(u8, exp.options.name, "_start")) {
982991 self.entry_addr = decl_sym.n_value;
992 self.cmd_table_dirty = true; // TODO This should be handled more granularly instead of invalidating all commands.
983993 }
984994 break :blk macho.REFERENCE_FLAG_DEFINED;
985995 },
......@@ -1121,6 +1131,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11211131
11221132 text_segment.vmsize = file_size + off; // We add off here since __TEXT segment includes everything prior to __text section.
11231133 text_segment.filesize = file_size + off;
1134 self.cmd_table_dirty = true;
11241135 }
11251136 if (self.data_segment_cmd_index == null) {
11261137 self.data_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
......@@ -1173,6 +1184,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11731184 data_segment.vmsize = segment_size;
11741185 data_segment.filesize = segment_size;
11751186 data_segment.fileoff = off;
1187 self.cmd_table_dirty = true;
11761188 }
11771189 if (self.linkedit_segment_cmd_index == null) {
11781190 self.linkedit_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
......@@ -1268,6 +1280,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12681280 },
12691281 });
12701282 self.cmd_table_dirty = true;
1283 self.dylinker_cmd_dirty = true;
12711284 }
12721285 if (self.libsystem_cmd_index == null) {
12731286 self.libsystem_cmd_index = @intCast(u16, self.load_commands.items.len);
......@@ -1289,6 +1302,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12891302 },
12901303 });
12911304 self.cmd_table_dirty = true;
1305 self.libsystem_cmd_dirty = true;
12921306 }
12931307 if (self.main_cmd_index == null) {
12941308 self.main_cmd_index = @intCast(u16, self.load_commands.items.len);
......@@ -1407,8 +1421,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
14071421 free_list_removal = i;
14081422 }
14091423 break :blk new_start_vaddr;
1410 }
1411 else if (self.last_text_block) |last| {
1424 } else if (self.last_text_block) |last| {
14121425 const last_symbol = self.local_symbols.items[last.local_sym_index];
14131426 // TODO We should pad out the excess capacity with NOPs. For executables,
14141427 // no padding seems to be OK, but it will probably not be for objects.
......@@ -1431,7 +1444,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
14311444 self.last_text_block = text_block;
14321445 text_section.size = needed_size;
14331446
1434 self.cmd_table_dirty = true;
1447 self.cmd_table_dirty = true; // TODO Make more granular.
14351448 }
14361449 text_block.size = new_block_size;
14371450
......@@ -1684,7 +1697,7 @@ fn writeCmdHeaders(self: *MachO) !void {
16841697 return error.TODOImplementWritingObjFiles;
16851698 };
16861699 const idx = self.text_section_index.?;
1687 log.debug("writing text section {} at 0x{x}\n", .{ self.sections.items[idx .. idx + 1], off });
1700 log.debug("writing text section header at 0x{x}\n", .{off});
16881701 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.sections.items[idx .. idx + 1]), off);
16891702 }
16901703 {
......@@ -1702,7 +1715,7 @@ fn writeCmdHeaders(self: *MachO) !void {
17021715 return error.TODOImplementWritingObjFiles;
17031716 };
17041717 const idx = self.got_section_index.?;
1705 log.debug("writing got section {} at 0x{x}\n", .{ self.sections.items[idx .. idx + 1], off });
1718 log.debug("writing got section header at 0x{x}\n", .{off});
17061719 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.sections.items[idx .. idx + 1]), off);
17071720 }
17081721}