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)
31213121 }
31223122 } else if (code_len < atom.size) {
31233123 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;
31243129 }
31253130 atom.size = code_len;
31263131 } else {
......@@ -4688,12 +4693,11 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
46884693
46894694 const end = start + padToIdeal(size);
46904695
4691 for (self.sections.items(.segment_index)) |segment_index| {
4692 const segment = self.segments.items[segment_index];
4693 const tight_size = segment.filesize;
4696 for (self.sections.items(.header)) |header| {
4697 const tight_size = header.size;
46944698 const increased_size = padToIdeal(tight_size);
4695 const test_end = segment.fileoff + increased_size;
4696 if (end > segment.fileoff and start < test_end) {
4699 const test_end = header.offset + increased_size;
4700 if (end > header.offset and start < test_end) {
46974701 return test_end;
46984702 }
46994703 }
......@@ -4705,10 +4709,9 @@ fn allocatedSize(self: *MachO, start: u64) u64 {
47054709 if (start == 0)
47064710 return 0;
47074711 var min_pos: u64 = std.math.maxInt(u64);
4708 for (self.sections.items(.segment_index)) |segment_index| {
4709 const segment = self.segments.items[segment_index];
4710 if (segment.fileoff <= start) continue;
4711 if (segment.fileoff < min_pos) min_pos = segment.fileoff;
4712 for (self.sections.items(.header)) |header| {
4713 if (header.offset <= start) continue;
4714 if (header.offset < min_pos) min_pos = header.offset;
47124715 }
47134716 return min_pos - start;
47144717}
......@@ -4721,7 +4724,7 @@ fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u32) u64 {
47214724 return start;
47224725}
47234726
4724fn allocatedVirtualSize(self: *MachO, start: u64) u64 {
4727pub fn allocatedVirtualSize(self: *MachO, start: u64) u64 {
47254728 if (start == 0)
47264729 return 0;
47274730 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 {
195195 } else {
196196 // We are the last atom.
197197 // 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);
199199 }
200200}
201201