authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-16 13:49:13+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-18 10:00:04+02:00
log372acb83500e9f910f48b78eaca4bf35a6e4f9d8
tree856173cf6df55fca9097bc114bcb9072ea8d5166
parent275abf7c5712e572c96db825cc4a0e46a4890250

macho: ensure we extend section size when updating last atom


2 files changed, 14 insertions(+), 11 deletions(-)

src/link/MachO.zig+13-10
...@@ -3121,6 +3121,11 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8)...@@ -3121,6 +3121,11 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8)
3121 }3121 }
3122 } else if (code_len < atom.size) {3122 } else if (code_len < atom.size) {
3123 self.shrinkAtom(atom, code_len);3123 self.shrinkAtom(atom, code_len);
3124 } else if (atom.next == null) {
3125 const header = &self.sections.items(.header)[sect_id];
3126 const segment = self.getSegment(sect_id);
3127 const needed_size = (sym.n_value + code_len) - segment.vmaddr;
3128 header.size = needed_size;
3124 }3129 }
3125 atom.size = code_len;3130 atom.size = code_len;
3126 } else {3131 } else {
...@@ -4688,12 +4693,11 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {...@@ -4688,12 +4693,11 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
46884693
4689 const end = start + padToIdeal(size);4694 const end = start + padToIdeal(size);
46904695
4691 for (self.sections.items(.segment_index)) |segment_index| {4696 for (self.sections.items(.header)) |header| {
4692 const segment = self.segments.items[segment_index];4697 const tight_size = header.size;
4693 const tight_size = segment.filesize;
4694 const increased_size = padToIdeal(tight_size);4698 const increased_size = padToIdeal(tight_size);
4695 const test_end = segment.fileoff + increased_size;4699 const test_end = header.offset + increased_size;
4696 if (end > segment.fileoff and start < test_end) {4700 if (end > header.offset and start < test_end) {
4697 return test_end;4701 return test_end;
4698 }4702 }
4699 }4703 }
...@@ -4705,10 +4709,9 @@ fn allocatedSize(self: *MachO, start: u64) u64 {...@@ -4705,10 +4709,9 @@ fn allocatedSize(self: *MachO, start: u64) u64 {
4705 if (start == 0)4709 if (start == 0)
4706 return 0;4710 return 0;
4707 var min_pos: u64 = std.math.maxInt(u64);4711 var min_pos: u64 = std.math.maxInt(u64);
4708 for (self.sections.items(.segment_index)) |segment_index| {4712 for (self.sections.items(.header)) |header| {
4709 const segment = self.segments.items[segment_index];4713 if (header.offset <= start) continue;
4710 if (segment.fileoff <= start) continue;4714 if (header.offset < min_pos) min_pos = header.offset;
4711 if (segment.fileoff < min_pos) min_pos = segment.fileoff;
4712 }4715 }
4713 return min_pos - start;4716 return min_pos - start;
4714}4717}
...@@ -4721,7 +4724,7 @@ fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u32) u64 {...@@ -4721,7 +4724,7 @@ fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u32) u64 {
4721 return start;4724 return start;
4722}4725}
47234726
4724fn allocatedVirtualSize(self: *MachO, start: u64) u64 {4727pub fn allocatedVirtualSize(self: *MachO, start: u64) u64 {
4725 if (start == 0)4728 if (start == 0)
4726 return 0;4729 return 0;
4727 var min_pos: u64 = std.math.maxInt(u64);4730 var min_pos: u64 = std.math.maxInt(u64);
src/link/MachO/Atom.zig+1-1
...@@ -195,7 +195,7 @@ pub fn capacity(self: Atom, macho_file: *MachO) u64 {...@@ -195,7 +195,7 @@ pub fn capacity(self: Atom, macho_file: *MachO) u64 {
195 } else {195 } else {
196 // We are the last atom.196 // We are the last atom.
197 // The capacity is limited only by virtual address space.197 // The capacity is limited only by virtual address space.
198 return std.math.maxInt(u64) - self_sym.n_value;198 return macho_file.allocatedVirtualSize(self_sym.n_value);
199 }199 }
200}200}
201201