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 {
288288 const tracy = trace(@src());
289289 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
296291 if (macho_file.getZigObject()) |zo| {
297 // TODO this will create a race
292 // TODO this will create a race as we need to track merging of debug sections which we currently don't
298293 zo.calcNumRelocs(macho_file);
299 zo.calcSymtabSize(macho_file);
300294 }
301295
302 if (macho_file.eh_frame_sect_index) |_| {
303 try calcEhFrameSize(macho_file);
304 }
296 const tp = macho_file.base.comp.thread_pool;
297 var wg: WaitGroup = .{};
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| {
307311 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 });
309325 }
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
315330 if (macho_file.unwind_info_sect_index) |_| {
316331 calcCompactUnwindSize(macho_file);
317332 }
318333 try calcSymtabSize(macho_file);
334
335 if (macho_file.has_errors.swap(false, .seq_cst)) return error.FlushFailure;
319336}
320337
321fn calcSectionSize(macho_file: *MachO, sect_id: u8) void {
338fn calcSectionSizeWorker(macho_file: *MachO, sect_id: u8) void {
322339 const tracy = trace(@src());
323340 defer tracy.end();
324341
......@@ -339,14 +356,25 @@ fn calcSectionSize(macho_file: *MachO, sect_id: u8) void {
339356 }
340357}
341358
342fn calcEhFrameSize(macho_file: *MachO) !void {
359fn calcEhFrameSizeWorker(macho_file: *MachO) void {
343360 const tracy = trace(@src());
344361 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
346371 const header = &macho_file.sections.items(.header)[macho_file.eh_frame_sect_index.?];
347 header.size = try eh_frame.calcSize(macho_file);
348 header.@"align" = 3;
349 header.nreloc = eh_frame.calcNumRelocs(macho_file);
372 doWork(macho_file, header) catch |err| {
373 macho_file.reportUnexpectedError("failed to calculate size of section '__TEXT,__eh_frame': {s}", .{
374 @errorName(err),
375 }) catch {};
376 _ = macho_file.has_errors.swap(true, .seq_cst);
377 };
350378}
351379
352380fn calcCompactUnwindSize(macho_file: *MachO) void {
......@@ -716,3 +744,4 @@ const File = @import("file.zig").File;
716744const MachO = @import("../MachO.zig");
717745const Object = @import("Object.zig");
718746const Symbol = @import("Symbol.zig");
747const WaitGroup = std.Thread.WaitGroup;