authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-21 23:15:55+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-21 23:16:00+01:00
logf106a46fd293605fab7cb776d36bb22cc482e692
treee41216eaa58050b1f6f8e76bba6156b2c2adcd6e
parent3f21f9155fe8e10e3990ee5738b190accbdffd3a

macho: enable binding and lazy binding info writes


1 files changed, 47 insertions(+), 38 deletions(-)

src/link/MachO.zig+47-38
......@@ -114,6 +114,8 @@ error_flags: File.ErrorFlags = File.ErrorFlags{},
114114offset_table_count_dirty: bool = false,
115115header_dirty: bool = false,
116116load_commands_dirty: bool = false,
117binding_info_dirty: bool = false,
118lazy_binding_info_dirty: bool = false,
117119export_info_dirty: bool = false,
118120string_table_dirty: bool = false,
119121
......@@ -322,6 +324,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
322324 main_cmd.entryoff = addr - text_segment.inner.vmaddr;
323325 self.load_commands_dirty = true;
324326 }
327 try self.writeBindingInfoTable();
328 try self.writeLazyBindingInfoTable();
325329 try self.writeExportTrie();
326330 try self.writeAllGlobalAndUndefSymbols();
327331 try self.writeStringTable();
......@@ -354,6 +358,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
354358 assert(!self.offset_table_count_dirty);
355359 assert(!self.header_dirty);
356360 assert(!self.load_commands_dirty);
361 assert(!self.binding_info_dirty);
362 assert(!self.lazy_binding_info_dirty);
357363 assert(!self.export_info_dirty);
358364 assert(!self.string_table_dirty);
359365
......@@ -831,7 +837,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
831837 symbol.dylib_ordinal = next_ordinal;
832838 }
833839
834 // Write update dyld info
840 // Write updated dyld info.
835841 const dyld_info = self.load_commands.items[self.dyld_info_cmd_index.?].DyldInfoOnly;
836842 {
837843 const size = try self.binding_info_table.calcSize();
......@@ -861,6 +867,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
861867 // Write updated load commands and the header
862868 try self.writeLoadCommands();
863869 try self.writeHeader();
870
871 assert(!self.header_dirty);
872 assert(!self.load_commands_dirty);
864873 }
865874 if (self.code_signature_cmd_index == null) outer: {
866875 if (target.cpu.arch != .aarch64) break :outer; // This is currently needed only for aarch64 targets.
......@@ -896,6 +905,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
896905 try self.writeHeader();
897906 // Generate adhoc code signature
898907 try self.writeCodeSignature();
908
909 assert(!self.header_dirty);
910 assert(!self.load_commands_dirty);
899911 }
900912 }
901913 }
......@@ -2136,7 +2148,7 @@ fn writeExportTrie(self: *MachO) !void {
21362148 dyld_info.export_off = @intCast(u32, self.findFreeSpace(&linkedit_segment, needed_size, 1));
21372149 }
21382150 dyld_info.export_size = @intCast(u32, needed_size);
2139 log.debug("writing export trie from 0x{x} to 0x{x}", .{ dyld_info.export_off, dyld_info.export_off + dyld_info.export_size });
2151 log.debug("writing export info from 0x{x} to 0x{x}", .{ dyld_info.export_off, dyld_info.export_off + dyld_info.export_size });
21402152
21412153 try self.base.file.?.pwriteAll(buffer, dyld_info.export_off);
21422154 self.load_commands_dirty = true;
......@@ -2144,65 +2156,62 @@ fn writeExportTrie(self: *MachO) !void {
21442156}
21452157
21462158fn writeBindingInfoTable(self: *MachO) !void {
2147 const size = self.binding_info_table.calcSize();
2159 if (!self.binding_info_dirty) return;
2160
2161 const tracy = trace(@src());
2162 defer tracy.end();
2163
2164 const size = try self.binding_info_table.calcSize();
21482165 var buffer = try self.base.allocator.alloc(u8, size);
21492166 defer self.base.allocator.free(buffer);
21502167
21512168 var stream = std.io.fixedBufferStream(buffer);
21522169 try self.binding_info_table.write(stream.writer());
21532170
2171 const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
21542172 const dyld_info = &self.load_commands.items[self.dyld_info_cmd_index.?].DyldInfoOnly;
2155 const bind_size = @intCast(u32, mem.alignForward(buffer.len, @sizeOf(u64)));
2156 dyld_info.bind_off = self.linkedit_segment_next_offset.?;
2157 dyld_info.bind_size = bind_size;
2158
2159 log.debug("writing binding info table from 0x{x} to 0x{x}", .{ dyld_info.bind_off, dyld_info.bind_off + bind_size });
2173 const allocated_size = self.allocatedSize(&linkedit_segment, dyld_info.bind_off);
2174 const needed_size = mem.alignForwardGeneric(u64, buffer.len, @alignOf(u64));
21602175
2161 if (bind_size > buffer.len) {
2162 // Pad out to align(8).
2163 try self.base.file.?.pwriteAll(&[_]u8{0}, dyld_info.bind_off + bind_size);
2176 if (needed_size > allocated_size) {
2177 dyld_info.bind_off = 0;
2178 dyld_info.bind_off = @intCast(u32, self.findFreeSpace(&linkedit_segment, needed_size, 1));
21642179 }
2165 try self.base.file.?.pwriteAll(buffer, dyld_info.bind_off);
21662180
2167 self.linkedit_segment_next_offset = dyld_info.bind_off + dyld_info.bind_size;
2168 // Advance size of __LINKEDIT segment
2169 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
2170 linkedit.inner.filesize += dyld_info.bind_size;
2171 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
2172 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
2173 }
2174 self.cmd_table_dirty = true;
2181 dyld_info.bind_size = @intCast(u32, needed_size);
2182 log.debug("writing binding info from 0x{x} to 0x{x}", .{ dyld_info.bind_off, dyld_info.bind_off + dyld_info.bind_size });
2183
2184 try self.base.file.?.pwriteAll(buffer, dyld_info.bind_off);
2185 self.load_commands_dirty = true;
2186 self.binding_info_dirty = false;
21752187}
21762188
21772189fn writeLazyBindingInfoTable(self: *MachO) !void {
2178 const size = self.lazy_binding_info_table.calcSize();
2190 if (!self.lazy_binding_info_dirty) return;
2191
2192 const size = try self.lazy_binding_info_table.calcSize();
21792193 var buffer = try self.base.allocator.alloc(u8, size);
21802194 defer self.base.allocator.free(buffer);
21812195
21822196 var stream = std.io.fixedBufferStream(buffer);
21832197 try self.lazy_binding_info_table.write(stream.writer());
21842198
2199 const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
21852200 const dyld_info = &self.load_commands.items[self.dyld_info_cmd_index.?].DyldInfoOnly;
2186 const bind_size = @intCast(u32, mem.alignForward(buffer.len, @sizeOf(u64)));
2187 dyld_info.lazy_bind_off = self.linkedit_segment_next_offset.?;
2188 dyld_info.lazy_bind_size = bind_size;
2189
2190 log.debug("writing lazy binding info table from 0x{x} to 0x{x}", .{ dyld_info.lazy_bind_off, dyld_info.lazy_bind_off + bind_size });
2201 const allocated_size = self.allocatedSize(&linkedit_segment, dyld_info.lazy_bind_off);
2202 const needed_size = mem.alignForwardGeneric(u64, buffer.len, @alignOf(u64));
21912203
2192 if (bind_size > buffer.len) {
2193 // Pad out to align(8).
2194 try self.base.file.?.pwriteAll(&[_]u8{0}, dyld_info.lazy_bind_off + bind_size);
2204 if (needed_size > allocated_size) {
2205 dyld_info.lazy_bind_off = 0;
2206 dyld_info.lazy_bind_off = @intCast(u32, self.findFreeSpace(&linkedit_segment, needed_size, 1));
21952207 }
2196 try self.base.file.?.pwriteAll(buffer, dyld_info.lazy_bind_off);
21972208
2198 self.linkedit_segment_next_offset = dyld_info.lazy_bind_off + dyld_info.lazy_bind_size;
2199 // Advance size of __LINKEDIT segment
2200 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
2201 linkedit.inner.filesize += dyld_info.lazy_bind_size;
2202 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
2203 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
2204 }
2205 self.cmd_table_dirty = true;
2209 dyld_info.lazy_bind_size = @intCast(u32, needed_size);
2210 log.debug("writing lazy binding info from 0x{x} to 0x{x}", .{ dyld_info.lazy_bind_off, dyld_info.lazy_bind_off + dyld_info.lazy_bind_size });
2211
2212 try self.base.file.?.pwriteAll(buffer, dyld_info.lazy_bind_off);
2213 self.load_commands_dirty = true;
2214 self.lazy_binding_info_dirty = false;
22062215}
22072216
22082217fn writeStringTable(self: *MachO) !void {