authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-09 01:13:07+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-09 01:13:07+02:00
log23be9cae346614d7d42d9c1c6426bfe2d6721c68
tree71a419fa455403843fa76d3b41e8283b6edb3111
parent9fb44e8e1fa4f07e44e2d354e09db9d953861e71

macho: padToIdeal each parsed section size before storing

This way, we should not need to grow at all when allocating atoms representing objects' sections as atoms.

1 files changed, 55 insertions(+), 5 deletions(-)

src/link/MachO.zig+55-5
...@@ -1769,11 +1769,16 @@ fn writeAtoms(self: *MachO) !void {...@@ -1769,11 +1769,16 @@ fn writeAtoms(self: *MachO) !void {
1769 const match = entry.key_ptr.*;1769 const match = entry.key_ptr.*;
1770 var atom: *TextBlock = entry.value_ptr.*;1770 var atom: *TextBlock = entry.value_ptr.*;
17711771
1772 while (atom.prev) |prev| {1772 while (true) {
1773 try self.writeAtom(atom, match);1773 if (atom.dirty) {
1774 atom = prev;1774 try self.writeAtom(atom, match);
1775 atom.dirty = false;
1776 }
1777
1778 if (atom.prev) |prev| {
1779 atom = prev;
1780 } else break;
1775 }1781 }
1776 try self.writeAtom(atom, match);
1777 }1782 }
1778}1783}
17791784
...@@ -2609,8 +2614,9 @@ fn parseTextBlocks(self: *MachO) !void {...@@ -2609,8 +2614,9 @@ fn parseTextBlocks(self: *MachO) !void {
2609 .alignment = 0,2614 .alignment = 0,
2610 };2615 };
2611 }2616 }
2617 const size = padToIdeal(sect.size);
2612 const alignment = try math.powi(u32, 2, sect.@"align");2618 const alignment = try math.powi(u32, 2, sect.@"align");
2613 res.value_ptr.size += mem.alignForwardGeneric(u64, sect.size, alignment);2619 res.value_ptr.size += mem.alignForwardGeneric(u64, size, alignment);
2614 res.value_ptr.alignment = math.max(res.value_ptr.alignment, sect.@"align");2620 res.value_ptr.alignment = math.max(res.value_ptr.alignment, sect.@"align");
2615 }2621 }
2616 }2622 }
...@@ -2633,6 +2639,49 @@ fn parseTextBlocks(self: *MachO) !void {...@@ -2633,6 +2639,49 @@ fn parseTextBlocks(self: *MachO) !void {
2633 for (self.objects.items) |*object, object_id| {2639 for (self.objects.items) |*object, object_id| {
2634 try object.parseTextBlocks(self.base.allocator, @intCast(u16, object_id), self);2640 try object.parseTextBlocks(self.base.allocator, @intCast(u16, object_id), self);
2635 }2641 }
2642
2643 // it = section_metadata.iterator();
2644 // while (it.next()) |entry| {
2645 // const match = entry.key_ptr.*;
2646 // const metadata = entry.value_ptr.*;
2647 // const seg = self.load_commands.items[match.seg].Segment;
2648 // const sect = seg.sections.items[match.sect];
2649
2650 // var buffer = try self.base.allocator.alloc(u8, metadata.size);
2651 // defer self.base.allocator.free(buffer);
2652 // log.warn("{s},{s} buffer size 0x{x}", .{
2653 // commands.segmentName(sect),
2654 // commands.sectionName(sect),
2655 // metadata.size,
2656 // });
2657
2658 // var atom = self.blocks.get(match).?;
2659
2660 // while (atom.prev) |prev| {
2661 // atom = prev;
2662 // }
2663
2664 // const base = blk: {
2665 // const sym = self.locals.items[atom.local_sym_index];
2666 // break :blk sym.n_value;
2667 // };
2668
2669 // while (true) {
2670 // const sym = self.locals.items[atom.local_sym_index];
2671 // const offset = sym.n_value - base;
2672 // try atom.resolveRelocs(self);
2673 // log.warn("writing atom for symbol {s} at buffer offset 0x{x}", .{
2674 // self.getString(sym.n_strx),
2675 // offset,
2676 // });
2677 // mem.copy(u8, buffer[offset..][0..atom.code.items.len], atom.code.items);
2678 // atom.dirty = false;
2679
2680 // if (atom.next) |next| {
2681 // atom = next;
2682 // } else break;
2683 // }
2684 // }
2636}2685}
26372686
2638fn addDataInCodeLC(self: *MachO) !void {2687fn addDataInCodeLC(self: *MachO) !void {
...@@ -3836,6 +3885,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {...@@ -3836,6 +3885,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
3836 const needed_size = mem.alignForwardGeneric(u32, ideal_size, alignment);3885 const needed_size = mem.alignForwardGeneric(u32, ideal_size, alignment);
38373886
3838 if (needed_size > max_size) blk: {3887 if (needed_size > max_size) blk: {
3888 log.debug(" (need to grow!)", .{});
3839 // Need to move all sections below in file and address spaces.3889 // Need to move all sections below in file and address spaces.
3840 const offset_amt = offset: {3890 const offset_amt = offset: {
3841 const max_alignment = try self.getSectionMaxAlignment(match.seg, match.sect + 1);3891 const max_alignment = try self.getSectionMaxAlignment(match.seg, match.sect + 1);