| ... | ... | @@ -1667,18 +1667,8 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64 |
| 1667 | 1667 | const expand_section = atom_placement == null or atom_placement.?.next == null; |
| 1668 | 1668 | if (expand_section) { |
| 1669 | 1669 | const needed_size = (vaddr + atom.size) - sect.addr; |
| 1670 | | const sect_offset: u64 = blk: { |
| 1671 | | if (self.data_segment_cmd_index.? == match.seg) { |
| 1672 | | if (self.bss_section_index) |idx| { |
| 1673 | | if (idx == match.sect) break :blk self.bss_file_offset.?; |
| 1674 | | } |
| 1675 | | if (self.tlv_bss_section_index) |idx| { |
| 1676 | | if (idx == match.sect) break :blk self.tlv_bss_file_offset.?; |
| 1677 | | } |
| 1678 | | } |
| 1679 | | break :blk sect.offset; |
| 1680 | | }; |
| 1681 | | const file_offset = sect_offset + vaddr - sect.addr; |
| 1670 | const sect_offset = self.getPtrToSectionOffset(match); |
| 1671 | const file_offset = sect_offset.* + vaddr - sect.addr; |
| 1682 | 1672 | const max_size = seg.allocatedSize(file_offset); |
| 1683 | 1673 | log.debug(" (section {s},{s} needed size 0x{x}, max available size 0x{x})", .{ |
| 1684 | 1674 | commands.segmentName(sect.*), |
| ... | ... | @@ -1741,38 +1731,28 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64 |
| 1741 | 1731 | }); |
| 1742 | 1732 | |
| 1743 | 1733 | for (next_seg.sections.items) |*moved_sect, moved_sect_id| { |
| 1744 | | // TODO put below snippet in a function. |
| 1745 | | const moved_sect_offset = blk: { |
| 1746 | | if (self.data_segment_cmd_index.? == next) { |
| 1747 | | if (self.bss_section_index) |idx| { |
| 1748 | | if (idx == moved_sect_id) break :blk &self.bss_file_offset.?; |
| 1749 | | } |
| 1750 | | if (self.tlv_bss_section_index) |idx| { |
| 1751 | | if (idx == moved_sect_id) break :blk &self.tlv_bss_file_offset.?; |
| 1752 | | } |
| 1753 | | } |
| 1754 | | break :blk &moved_sect.offset; |
| 1734 | const moved_match = MatchingSection{ |
| 1735 | .seg = @intCast(u16, next), |
| 1736 | .sect = @intCast(u16, moved_sect_id), |
| 1755 | 1737 | }; |
| 1756 | | moved_sect_offset.* += offset_amt; |
| 1738 | const ptr_sect_offset = self.getPtrToSectionOffset(moved_match); |
| 1739 | ptr_sect_offset.* += offset_amt; |
| 1757 | 1740 | moved_sect.addr += offset_amt; |
| 1758 | 1741 | log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{ |
| 1759 | 1742 | commands.segmentName(moved_sect.*), |
| 1760 | 1743 | commands.sectionName(moved_sect.*), |
| 1761 | | moved_sect_offset.*, |
| 1762 | | moved_sect_offset.* + moved_sect.size, |
| 1744 | ptr_sect_offset.*, |
| 1745 | ptr_sect_offset.* + moved_sect.size, |
| 1763 | 1746 | moved_sect.addr, |
| 1764 | 1747 | moved_sect.addr + moved_sect.size, |
| 1765 | 1748 | }); |
| 1766 | 1749 | |
| 1767 | | try self.allocateLocalSymbols(.{ |
| 1768 | | .seg = @intCast(u16, next), |
| 1769 | | .sect = @intCast(u16, moved_sect_id), |
| 1770 | | }, offset_amt); |
| 1750 | try self.allocateLocalSymbols(moved_match, offset_amt); |
| 1771 | 1751 | } |
| 1772 | 1752 | } |
| 1773 | 1753 | } |
| 1774 | | sect.offset = new_offset; |
| 1775 | | sect.addr = seg.inner.vmaddr + sect.offset - seg.inner.fileoff; |
| 1754 | sect_offset.* = new_offset; |
| 1755 | sect.addr = seg.inner.vmaddr + sect_offset.* - seg.inner.fileoff; |
| 1776 | 1756 | log.debug(" (found new {s},{s} free space from 0x{x} to 0x{x})", .{ |
| 1777 | 1757 | commands.segmentName(sect.*), |
| 1778 | 1758 | commands.sectionName(sect.*), |
| ... | ... | @@ -1820,17 +1800,7 @@ pub fn writeAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !void { |
| 1820 | 1800 | const seg = self.load_commands.items[match.seg].Segment; |
| 1821 | 1801 | const sect = seg.sections.items[match.sect]; |
| 1822 | 1802 | const sym = self.locals.items[atom.local_sym_index]; |
| 1823 | | const sect_offset: u64 = blk: { |
| 1824 | | if (self.data_segment_cmd_index.? == match.seg) { |
| 1825 | | if (self.bss_section_index) |idx| { |
| 1826 | | if (idx == match.sect) break :blk self.bss_file_offset.?; |
| 1827 | | } |
| 1828 | | if (self.tlv_bss_section_index) |idx| { |
| 1829 | | if (idx == match.sect) break :blk self.tlv_bss_file_offset.?; |
| 1830 | | } |
| 1831 | | } |
| 1832 | | break :blk sect.offset; |
| 1833 | | }; |
| 1803 | const sect_offset = self.getPtrToSectionOffset(match).*; |
| 1834 | 1804 | const file_offset = sect_offset + sym.n_value - sect.addr; |
| 1835 | 1805 | try atom.resolveRelocs(self); |
| 1836 | 1806 | log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ self.getString(sym.n_strx), file_offset }); |
| ... | ... | @@ -3896,6 +3866,25 @@ fn allocateSection( |
| 3896 | 3866 | return index; |
| 3897 | 3867 | } |
| 3898 | 3868 | |
| 3869 | fn getPtrToSectionOffset(self: *MachO, match: MatchingSection) *u32 { |
| 3870 | if (self.data_segment_cmd_index.? == match.seg) { |
| 3871 | if (self.bss_section_index) |idx| { |
| 3872 | if (idx == match.sect) { |
| 3873 | return &self.bss_file_offset.?; |
| 3874 | } |
| 3875 | } |
| 3876 | if (self.tlv_bss_section_index) |idx| { |
| 3877 | if (idx == match.sect) { |
| 3878 | return &self.tlv_bss_file_offset.?; |
| 3879 | } |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | const seg = &self.load_commands.items[match.seg].Segment; |
| 3884 | const sect = &seg.sections.items[match.sect]; |
| 3885 | return &sect.offset; |
| 3886 | } |
| 3887 | |
| 3899 | 3888 | fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 { |
| 3900 | 3889 | const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 3901 | 3890 | const text_section = &text_segment.sections.items[self.text_section_index.?]; |
| ... | ... | @@ -4028,32 +4017,23 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, |
| 4028 | 4017 | |
| 4029 | 4018 | for (next_seg.sections.items) |*moved_sect, moved_sect_id| { |
| 4030 | 4019 | // TODO put below snippet in a function. |
| 4031 | | const moved_sect_offset = blk: { |
| 4032 | | if (self.data_segment_cmd_index.? == next) { |
| 4033 | | if (self.bss_section_index) |idx| { |
| 4034 | | if (idx == moved_sect_id) break :blk &self.bss_file_offset.?; |
| 4035 | | } |
| 4036 | | if (self.tlv_bss_section_index) |idx| { |
| 4037 | | if (idx == moved_sect_id) break :blk &self.tlv_bss_file_offset.?; |
| 4038 | | } |
| 4039 | | } |
| 4040 | | break :blk &moved_sect.offset; |
| 4020 | const moved_match = MatchingSection{ |
| 4021 | .seg = @intCast(u16, next), |
| 4022 | .sect = @intCast(u16, moved_sect_id), |
| 4041 | 4023 | }; |
| 4042 | | moved_sect_offset.* += offset_amt; |
| 4024 | const ptr_sect_offset = self.getPtrToSectionOffset(moved_match); |
| 4025 | ptr_sect_offset.* += offset_amt; |
| 4043 | 4026 | moved_sect.addr += offset_amt; |
| 4044 | 4027 | log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{ |
| 4045 | 4028 | commands.segmentName(moved_sect.*), |
| 4046 | 4029 | commands.sectionName(moved_sect.*), |
| 4047 | | moved_sect_offset.*, |
| 4048 | | moved_sect_offset.* + moved_sect.size, |
| 4030 | ptr_sect_offset.*, |
| 4031 | ptr_sect_offset.* + moved_sect.size, |
| 4049 | 4032 | moved_sect.addr, |
| 4050 | 4033 | moved_sect.addr + moved_sect.size, |
| 4051 | 4034 | }); |
| 4052 | 4035 | |
| 4053 | | try self.allocateLocalSymbols(.{ |
| 4054 | | .seg = @intCast(u16, next), |
| 4055 | | .sect = @intCast(u16, moved_sect_id), |
| 4056 | | }, offset_amt); |
| 4036 | try self.allocateLocalSymbols(moved_match, offset_amt); |
| 4057 | 4037 | } |
| 4058 | 4038 | } |
| 4059 | 4039 | } |