authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-06 18:30:40+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-06 18:30:40+02:00
log6836cc473c3e75a71c0e6c0123c8afb23a79596d
tree805129caa39f1e6513b170728fbf837eced998a6
parent81e5320973e8cffd585f240b20321e6afc15e8f9

macho: make sure that parsed bss atoms are zero-filled


2 files changed, 28 insertions(+), 9 deletions(-)

src/link/MachO.zig+6-6
...@@ -1672,7 +1672,7 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64...@@ -1672,7 +1672,7 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64
1672 break :blk new_start_vaddr;1672 break :blk new_start_vaddr;
1673 } else sect.addr;1673 } else sect.addr;
16741674
1675 log.warn("allocating atom for symbol {s} at address 0x{x}", .{ self.getString(sym.n_strx), vaddr });1675 log.debug("allocating atom for symbol {s} at address 0x{x}", .{ self.getString(sym.n_strx), vaddr });
16761676
1677 const expand_section = atom_placement == null or atom_placement.?.next == null;1677 const expand_section = atom_placement == null or atom_placement.?.next == null;
1678 if (expand_section) {1678 if (expand_section) {
...@@ -3803,13 +3803,13 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {...@@ -3803,13 +3803,13 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
38033803
3804 if (last_sect_off + offset_amt > seg_off) {3804 if (last_sect_off + offset_amt > seg_off) {
3805 // Need to grow segment first.3805 // Need to grow segment first.
3806 log.warn(" (need to grow segment first)", .{});3806 log.debug(" (need to grow segment first)", .{});
3807 const spill_size = (last_sect_off + offset_amt) - seg_off;3807 const spill_size = (last_sect_off + offset_amt) - seg_off;
3808 const seg_offset_amt = mem.alignForwardGeneric(u64, spill_size, self.page_size);3808 const seg_offset_amt = mem.alignForwardGeneric(u64, spill_size, self.page_size);
3809 seg.inner.filesize += seg_offset_amt;3809 seg.inner.filesize += seg_offset_amt;
3810 seg.inner.vmsize += seg_offset_amt;3810 seg.inner.vmsize += seg_offset_amt;
38113811
3812 log.warn(" (new {s} segment file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{3812 log.debug(" (new {s} segment file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
3813 seg.inner.segname,3813 seg.inner.segname,
3814 seg.inner.fileoff,3814 seg.inner.fileoff,
3815 seg.inner.fileoff + seg.inner.filesize,3815 seg.inner.fileoff + seg.inner.filesize,
...@@ -3836,7 +3836,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {...@@ -3836,7 +3836,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
3836 next_seg.inner.fileoff += seg_offset_amt;3836 next_seg.inner.fileoff += seg_offset_amt;
3837 next_seg.inner.vmaddr += seg_offset_amt;3837 next_seg.inner.vmaddr += seg_offset_amt;
38383838
3839 log.warn(" (new {s} segment file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{3839 log.debug(" (new {s} segment file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
3840 next_seg.inner.segname,3840 next_seg.inner.segname,
3841 next_seg.inner.fileoff,3841 next_seg.inner.fileoff,
3842 next_seg.inner.fileoff + next_seg.inner.filesize,3842 next_seg.inner.fileoff + next_seg.inner.filesize,
...@@ -3848,7 +3848,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {...@@ -3848,7 +3848,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
3848 moved_sect.offset += @intCast(u32, seg_offset_amt);3848 moved_sect.offset += @intCast(u32, seg_offset_amt);
3849 moved_sect.addr += seg_offset_amt;3849 moved_sect.addr += seg_offset_amt;
38503850
3851 log.warn(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{3851 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
3852 commands.segmentName(moved_sect.*),3852 commands.segmentName(moved_sect.*),
3853 commands.sectionName(moved_sect.*),3853 commands.sectionName(moved_sect.*),
3854 moved_sect.offset,3854 moved_sect.offset,
...@@ -3884,7 +3884,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {...@@ -3884,7 +3884,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
3884 moved_sect.offset += @intCast(u32, offset_amt);3884 moved_sect.offset += @intCast(u32, offset_amt);
3885 moved_sect.addr += offset_amt;3885 moved_sect.addr += offset_amt;
38863886
3887 log.warn(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{3887 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
3888 commands.segmentName(moved_sect.*),3888 commands.segmentName(moved_sect.*),
3889 commands.sectionName(moved_sect.*),3889 commands.sectionName(moved_sect.*),
3890 moved_sect.offset,3890 moved_sect.offset,
src/link/MachO/Object.zig+22-3
...@@ -408,7 +408,14 @@ const TextBlockParser = struct {...@@ -408,7 +408,14 @@ const TextBlockParser = struct {
408408
409 const block = try context.macho_file.createEmptyAtom(senior_nlist.index, size, actual_align);409 const block = try context.macho_file.createEmptyAtom(senior_nlist.index, size, actual_align);
410 block.stab = stab;410 block.stab = stab;
411 mem.copy(u8, block.code.items, code);411
412 const is_zerofill = blk: {
413 const section_type = commands.sectionType(self.section);
414 break :blk section_type == macho.S_ZEROFILL or section_type == macho.S_THREAD_LOCAL_ZEROFILL;
415 };
416 if (!is_zerofill) {
417 mem.copy(u8, block.code.items, code);
418 }
412419
413 try block.aliases.ensureTotalCapacity(context.allocator, aliases.items.len);420 try block.aliases.ensureTotalCapacity(context.allocator, aliases.items.len);
414 for (aliases.items) |alias| {421 for (aliases.items) |alias| {
...@@ -567,7 +574,13 @@ pub fn parseTextBlocks(...@@ -567,7 +574,13 @@ pub fn parseTextBlocks(
567 const block_size = block_code.len;574 const block_size = block_code.len;
568 const block = try macho_file.createEmptyAtom(block_local_sym_index, block_size, sect.@"align");575 const block = try macho_file.createEmptyAtom(block_local_sym_index, block_size, sect.@"align");
569576
570 mem.copy(u8, block.code.items, block_code);577 const is_zerofill = blk: {
578 const section_type = commands.sectionType(sect);
579 break :blk section_type == macho.S_ZEROFILL or section_type == macho.S_THREAD_LOCAL_ZEROFILL;
580 };
581 if (!is_zerofill) {
582 mem.copy(u8, block.code.items, block_code);
583 }
571584
572 try block.parseRelocs(relocs, .{585 try block.parseRelocs(relocs, .{
573 .base_addr = 0,586 .base_addr = 0,
...@@ -667,7 +680,13 @@ pub fn parseTextBlocks(...@@ -667,7 +680,13 @@ pub fn parseTextBlocks(
667 };680 };
668 const block = try macho_file.createEmptyAtom(block_local_sym_index, sect.size, sect.@"align");681 const block = try macho_file.createEmptyAtom(block_local_sym_index, sect.size, sect.@"align");
669682
670 mem.copy(u8, block.code.items, code);683 const is_zerofill = blk: {
684 const section_type = commands.sectionType(sect);
685 break :blk section_type == macho.S_ZEROFILL or section_type == macho.S_THREAD_LOCAL_ZEROFILL;
686 };
687 if (!is_zerofill) {
688 mem.copy(u8, block.code.items, code);
689 }
671690
672 try block.parseRelocs(relocs, .{691 try block.parseRelocs(relocs, .{
673 .base_addr = 0,692 .base_addr = 0,