authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-22 10:14:58+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-22 12:06:02+02:00
log7e5a2673ac8b76dbd8cbcfadb829083238182666
tree18b752fffd3cadbb3ca9948ddc4724b7c226dfc5
parent06a0da3e8a34d12de7adece6223d4235c4673aaf

macho: calc section sizes in parallel in -r mode


1 files changed, 49 insertions(+), 20 deletions(-)

src/link/MachO/relocatable.zig+49-20
...@@ -288,37 +288,54 @@ fn calcSectionSizes(macho_file: *MachO) !void {...@@ -288,37 +288,54 @@ fn calcSectionSizes(macho_file: *MachO) !void {
288 const tracy = trace(@src());288 const tracy = trace(@src());
289 defer tracy.end();289 defer tracy.end();
290290
291 for (macho_file.sections.items(.atoms), 0..) |atoms, i| {
292 if (atoms.items.len == 0) continue;
293 calcSectionSize(macho_file, @intCast(i));
294 }
295
296 if (macho_file.getZigObject()) |zo| {291 if (macho_file.getZigObject()) |zo| {
297 // TODO this will create a race292 // TODO this will create a race as we need to track merging of debug sections which we currently don't
298 zo.calcNumRelocs(macho_file);293 zo.calcNumRelocs(macho_file);
299 zo.calcSymtabSize(macho_file);
300 }294 }
301295
302 if (macho_file.eh_frame_sect_index) |_| {296 const tp = macho_file.base.comp.thread_pool;
303 try calcEhFrameSize(macho_file);297 var wg: WaitGroup = .{};
304 }298 {
299 wg.reset();
300 defer wg.wait();
301
302 for (macho_file.sections.items(.atoms), 0..) |atoms, i| {
303 if (atoms.items.len == 0) continue;
304 tp.spawnWg(&wg, calcSectionSizeWorker, .{ macho_file, @as(u8, @intCast(i)) });
305 }
306
307 if (macho_file.eh_frame_sect_index) |_| {
308 tp.spawnWg(&wg, calcEhFrameSizeWorker, .{macho_file});
309 }
305310
306 for (macho_file.objects.items) |index| {
307 if (macho_file.unwind_info_sect_index) |_| {311 if (macho_file.unwind_info_sect_index) |_| {
308 macho_file.getFile(index).?.object.calcCompactUnwindSizeRelocatable(macho_file);312 for (macho_file.objects.items) |index| {
313 tp.spawnWg(&wg, Object.calcCompactUnwindSizeRelocatable, .{
314 macho_file.getFile(index).?.object,
315 macho_file,
316 });
317 }
318 }
319
320 for (macho_file.objects.items) |index| {
321 tp.spawnWg(&wg, File.calcSymtabSize, .{ macho_file.getFile(index).?, macho_file });
322 }
323 if (macho_file.getZigObject()) |zo| {
324 tp.spawnWg(&wg, File.calcSymtabSize, .{ zo.asFile(), macho_file });
309 }325 }
310 macho_file.getFile(index).?.calcSymtabSize(macho_file);
311 }
312326
313 try macho_file.data_in_code.updateSize(macho_file);327 tp.spawnWg(&wg, MachO.updateLinkeditSizeWorker, .{ macho_file, .data_in_code });
328 }
314329
315 if (macho_file.unwind_info_sect_index) |_| {330 if (macho_file.unwind_info_sect_index) |_| {
316 calcCompactUnwindSize(macho_file);331 calcCompactUnwindSize(macho_file);
317 }332 }
318 try calcSymtabSize(macho_file);333 try calcSymtabSize(macho_file);
334
335 if (macho_file.has_errors.swap(false, .seq_cst)) return error.FlushFailure;
319}336}
320337
321fn calcSectionSize(macho_file: *MachO, sect_id: u8) void {338fn calcSectionSizeWorker(macho_file: *MachO, sect_id: u8) void {
322 const tracy = trace(@src());339 const tracy = trace(@src());
323 defer tracy.end();340 defer tracy.end();
324341
...@@ -339,14 +356,25 @@ fn calcSectionSize(macho_file: *MachO, sect_id: u8) void {...@@ -339,14 +356,25 @@ fn calcSectionSize(macho_file: *MachO, sect_id: u8) void {
339 }356 }
340}357}
341358
342fn calcEhFrameSize(macho_file: *MachO) !void {359fn calcEhFrameSizeWorker(macho_file: *MachO) void {
343 const tracy = trace(@src());360 const tracy = trace(@src());
344 defer tracy.end();361 defer tracy.end();
345362
363 const doWork = struct {
364 fn doWork(mfile: *MachO, header: *macho.section_64) !void {
365 header.size = try eh_frame.calcSize(mfile);
366 header.@"align" = 3;
367 header.nreloc = eh_frame.calcNumRelocs(mfile);
368 }
369 }.doWork;
370
346 const header = &macho_file.sections.items(.header)[macho_file.eh_frame_sect_index.?];371 const header = &macho_file.sections.items(.header)[macho_file.eh_frame_sect_index.?];
347 header.size = try eh_frame.calcSize(macho_file);372 doWork(macho_file, header) catch |err| {
348 header.@"align" = 3;373 macho_file.reportUnexpectedError("failed to calculate size of section '__TEXT,__eh_frame': {s}", .{
349 header.nreloc = eh_frame.calcNumRelocs(macho_file);374 @errorName(err),
375 }) catch {};
376 _ = macho_file.has_errors.swap(true, .seq_cst);
377 };
350}378}
351379
352fn calcCompactUnwindSize(macho_file: *MachO) void {380fn calcCompactUnwindSize(macho_file: *MachO) void {
...@@ -716,3 +744,4 @@ const File = @import("file.zig").File;...@@ -716,3 +744,4 @@ const File = @import("file.zig").File;
716const MachO = @import("../MachO.zig");744const MachO = @import("../MachO.zig");
717const Object = @import("Object.zig");745const Object = @import("Object.zig");
718const Symbol = @import("Symbol.zig");746const Symbol = @import("Symbol.zig");
747const WaitGroup = std.Thread.WaitGroup;