| ... | @@ -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(); |
| 290 | | 290 | |
| 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 race | 292 | // 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 | } |
| 301 | | 295 | |
| 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 | } |
| 305 | | 310 | |
| 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 | } | | |
| 312 | | 326 | |
| 313 | try macho_file.data_in_code.updateSize(macho_file); | 327 | tp.spawnWg(&wg, MachO.updateLinkeditSizeWorker, .{ macho_file, .data_in_code }); |
| | 328 | } |
| 314 | | 329 | |
| 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 | } |
| 320 | | 337 | |
| 321 | fn calcSectionSize(macho_file: *MachO, sect_id: u8) void { | 338 | fn 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(); |
| 324 | | 341 | |
| ... | @@ -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 | } |
| 341 | | 358 | |
| 342 | fn calcEhFrameSize(macho_file: *MachO) !void { | 359 | fn calcEhFrameSizeWorker(macho_file: *MachO) void { |
| 343 | const tracy = trace(@src()); | 360 | const tracy = trace(@src()); |
| 344 | defer tracy.end(); | 361 | defer tracy.end(); |
| 345 | | 362 | |
| | 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 | } |
| 351 | | 379 | |
| 352 | fn calcCompactUnwindSize(macho_file: *MachO) void { | 380 | fn 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; |
| 716 | const MachO = @import("../MachO.zig"); | 744 | const MachO = @import("../MachO.zig"); |
| 717 | const Object = @import("Object.zig"); | 745 | const Object = @import("Object.zig"); |
| 718 | const Symbol = @import("Symbol.zig"); | 746 | const Symbol = @import("Symbol.zig"); |
| | 747 | const WaitGroup = std.Thread.WaitGroup; |