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
16721672 break :blk new_start_vaddr;
16731673 } 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
16771677 const expand_section = atom_placement == null or atom_placement.?.next == null;
16781678 if (expand_section) {
......@@ -3803,13 +3803,13 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
38033803
38043804 if (last_sect_off + offset_amt > seg_off) {
38053805 // Need to grow segment first.
3806 log.warn(" (need to grow segment first)", .{});
3806 log.debug(" (need to grow segment first)", .{});
38073807 const spill_size = (last_sect_off + offset_amt) - seg_off;
38083808 const seg_offset_amt = mem.alignForwardGeneric(u64, spill_size, self.page_size);
38093809 seg.inner.filesize += seg_offset_amt;
38103810 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}))", .{
38133813 seg.inner.segname,
38143814 seg.inner.fileoff,
38153815 seg.inner.fileoff + seg.inner.filesize,
......@@ -3836,7 +3836,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
38363836 next_seg.inner.fileoff += seg_offset_amt;
38373837 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}))", .{
38403840 next_seg.inner.segname,
38413841 next_seg.inner.fileoff,
38423842 next_seg.inner.fileoff + next_seg.inner.filesize,
......@@ -3848,7 +3848,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
38483848 moved_sect.offset += @intCast(u32, seg_offset_amt);
38493849 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}))", .{
38523852 commands.segmentName(moved_sect.*),
38533853 commands.sectionName(moved_sect.*),
38543854 moved_sect.offset,
......@@ -3884,7 +3884,7 @@ fn growSection(self: *MachO, match: MatchingSection, new_size: u32) !void {
38843884 moved_sect.offset += @intCast(u32, offset_amt);
38853885 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}))", .{
38883888 commands.segmentName(moved_sect.*),
38893889 commands.sectionName(moved_sect.*),
38903890 moved_sect.offset,
src/link/MachO/Object.zig+22-3
......@@ -408,7 +408,14 @@ const TextBlockParser = struct {
408408
409409 const block = try context.macho_file.createEmptyAtom(senior_nlist.index, size, actual_align);
410410 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
413420 try block.aliases.ensureTotalCapacity(context.allocator, aliases.items.len);
414421 for (aliases.items) |alias| {
......@@ -567,7 +574,13 @@ pub fn parseTextBlocks(
567574 const block_size = block_code.len;
568575 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
572585 try block.parseRelocs(relocs, .{
573586 .base_addr = 0,
......@@ -667,7 +680,13 @@ pub fn parseTextBlocks(
667680 };
668681 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
672691 try block.parseRelocs(relocs, .{
673692 .base_addr = 0,