| ... | @@ -372,6 +372,7 @@ fn calcSectionSizes(macho_file: *MachO) !void { | ... | @@ -372,6 +372,7 @@ fn calcSectionSizes(macho_file: *MachO) !void { |
| 372 | if (macho_file.getZigObject()) |zo| { | 372 | if (macho_file.getZigObject()) |zo| { |
| 373 | // TODO this will create a race | 373 | // TODO this will create a race |
| 374 | zo.calcNumRelocs(macho_file); | 374 | zo.calcNumRelocs(macho_file); |
| | 375 | zo.calcSymtabSize(macho_file); |
| 375 | } | 376 | } |
| 376 | | 377 | |
| 377 | if (macho_file.eh_frame_sect_index) |_| { | 378 | if (macho_file.eh_frame_sect_index) |_| { |
| ... | @@ -390,7 +391,7 @@ fn calcSectionSizes(macho_file: *MachO) !void { | ... | @@ -390,7 +391,7 @@ fn calcSectionSizes(macho_file: *MachO) !void { |
| 390 | if (macho_file.unwind_info_sect_index) |_| { | 391 | if (macho_file.unwind_info_sect_index) |_| { |
| 391 | calcCompactUnwindSize(macho_file); | 392 | calcCompactUnwindSize(macho_file); |
| 392 | } | 393 | } |
| 393 | calcSymtabSize(macho_file); | 394 | try calcSymtabSize(macho_file); |
| 394 | } | 395 | } |
| 395 | | 396 | |
| 396 | fn calcSectionSize(macho_file: *MachO, sect_id: u8) void { | 397 | fn calcSectionSize(macho_file: *MachO, sect_id: u8) void { |
| ... | @@ -445,19 +446,27 @@ fn calcCompactUnwindSize(macho_file: *MachO) void { | ... | @@ -445,19 +446,27 @@ fn calcCompactUnwindSize(macho_file: *MachO) void { |
| 445 | sect.@"align" = 3; | 446 | sect.@"align" = 3; |
| 446 | } | 447 | } |
| 447 | | 448 | |
| 448 | fn calcSymtabSize(macho_file: *MachO) void { | 449 | fn calcSymtabSize(macho_file: *MachO) error{OutOfMemory}!void { |
| 449 | const tracy = trace(@src()); | 450 | const tracy = trace(@src()); |
| 450 | defer tracy.end(); | 451 | defer tracy.end(); |
| 451 | | 452 | |
| | 453 | const gpa = macho_file.base.comp.gpa; |
| | 454 | |
| 452 | var nlocals: u32 = 0; | 455 | var nlocals: u32 = 0; |
| 453 | var nstabs: u32 = 0; | 456 | var nstabs: u32 = 0; |
| 454 | var nexports: u32 = 0; | 457 | var nexports: u32 = 0; |
| 455 | var nimports: u32 = 0; | 458 | var nimports: u32 = 0; |
| 456 | var strsize: u32 = 1; | 459 | var strsize: u32 = 1; |
| 457 | | 460 | |
| 458 | for (macho_file.objects.items) |index| { | 461 | var objects = try std.ArrayList(File.Index).initCapacity(gpa, macho_file.objects.items.len + 1); |
| 459 | const object = macho_file.getFile(index).?.object; | 462 | defer objects.deinit(); |
| 460 | const ctx = &object.output_symtab_ctx; | 463 | if (macho_file.getZigObject()) |zo| objects.appendAssumeCapacity(zo.index); |
| | 464 | objects.appendSliceAssumeCapacity(macho_file.objects.items); |
| | 465 | |
| | 466 | for (objects.items) |index| { |
| | 467 | const ctx = switch (macho_file.getFile(index).?) { |
| | 468 | inline else => |x| &x.output_symtab_ctx, |
| | 469 | }; |
| 461 | ctx.ilocal = nlocals; | 470 | ctx.ilocal = nlocals; |
| 462 | ctx.istab = nstabs; | 471 | ctx.istab = nstabs; |
| 463 | ctx.iexport = nexports; | 472 | ctx.iexport = nexports; |
| ... | @@ -470,9 +479,10 @@ fn calcSymtabSize(macho_file: *MachO) void { | ... | @@ -470,9 +479,10 @@ fn calcSymtabSize(macho_file: *MachO) void { |
| 470 | strsize += ctx.strsize; | 479 | strsize += ctx.strsize; |
| 471 | } | 480 | } |
| 472 | | 481 | |
| 473 | for (macho_file.objects.items) |index| { | 482 | for (objects.items) |index| { |
| 474 | const object = macho_file.getFile(index).?.object; | 483 | const ctx = switch (macho_file.getFile(index).?) { |
| 475 | const ctx = &object.output_symtab_ctx; | 484 | inline else => |x| &x.output_symtab_ctx, |
| | 485 | }; |
| 476 | ctx.istab += nlocals; | 486 | ctx.istab += nlocals; |
| 477 | ctx.iexport += nlocals + nstabs; | 487 | ctx.iexport += nlocals + nstabs; |
| 478 | ctx.iimport += nlocals + nstabs + nexports; | 488 | ctx.iimport += nlocals + nstabs + nexports; |
| ... | @@ -645,6 +655,11 @@ fn writeSections(macho_file: *MachO) !void { | ... | @@ -645,6 +655,11 @@ fn writeSections(macho_file: *MachO) !void { |
| 645 | macho_file.getFile(index).?.writeSymtab(macho_file, macho_file); | 655 | macho_file.getFile(index).?.writeSymtab(macho_file, macho_file); |
| 646 | } | 656 | } |
| 647 | | 657 | |
| | 658 | if (macho_file.getZigObject()) |zo| { |
| | 659 | try zo.writeRelocs(macho_file); |
| | 660 | zo.writeSymtab(macho_file, macho_file); |
| | 661 | } |
| | 662 | |
| 648 | if (macho_file.eh_frame_sect_index) |_| { | 663 | if (macho_file.eh_frame_sect_index) |_| { |
| 649 | try writeEhFrame(macho_file); | 664 | try writeEhFrame(macho_file); |
| 650 | } | 665 | } |