authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-05 23:30:06+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-05 23:30:06+02:00
log61dca19107a30011ba67754ff867c858024eb5c5
tree17ca3a623ce63d4ce5a891ff8845e43b5b1e9771
parentea8808f87b319c92dd74d75f1bb4fae1180e00ed

macho: encaps logic for extract sect offset into fn


1 files changed, 40 insertions(+), 60 deletions(-)

src/link/MachO.zig+40-60
...@@ -1667,18 +1667,8 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64...@@ -1667,18 +1667,8 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64
1667 const expand_section = atom_placement == null or atom_placement.?.next == null;1667 const expand_section = atom_placement == null or atom_placement.?.next == null;
1668 if (expand_section) {1668 if (expand_section) {
1669 const needed_size = (vaddr + atom.size) - sect.addr;1669 const needed_size = (vaddr + atom.size) - sect.addr;
1670 const sect_offset: u64 = blk: {1670 const sect_offset = self.getPtrToSectionOffset(match);
1671 if (self.data_segment_cmd_index.? == match.seg) {1671 const file_offset = sect_offset.* + vaddr - sect.addr;
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;
1682 const max_size = seg.allocatedSize(file_offset);1672 const max_size = seg.allocatedSize(file_offset);
1683 log.debug(" (section {s},{s} needed size 0x{x}, max available size 0x{x})", .{1673 log.debug(" (section {s},{s} needed size 0x{x}, max available size 0x{x})", .{
1684 commands.segmentName(sect.*),1674 commands.segmentName(sect.*),
...@@ -1741,38 +1731,28 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64...@@ -1741,38 +1731,28 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64
1741 });1731 });
17421732
1743 for (next_seg.sections.items) |*moved_sect, moved_sect_id| {1733 for (next_seg.sections.items) |*moved_sect, moved_sect_id| {
1744 // TODO put below snippet in a function.1734 const moved_match = MatchingSection{
1745 const moved_sect_offset = blk: {1735 .seg = @intCast(u16, next),
1746 if (self.data_segment_cmd_index.? == next) {1736 .sect = @intCast(u16, moved_sect_id),
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;
1755 };1737 };
1756 moved_sect_offset.* += offset_amt;1738 const ptr_sect_offset = self.getPtrToSectionOffset(moved_match);
1739 ptr_sect_offset.* += offset_amt;
1757 moved_sect.addr += offset_amt;1740 moved_sect.addr += offset_amt;
1758 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{1741 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
1759 commands.segmentName(moved_sect.*),1742 commands.segmentName(moved_sect.*),
1760 commands.sectionName(moved_sect.*),1743 commands.sectionName(moved_sect.*),
1761 moved_sect_offset.*,1744 ptr_sect_offset.*,
1762 moved_sect_offset.* + moved_sect.size,1745 ptr_sect_offset.* + moved_sect.size,
1763 moved_sect.addr,1746 moved_sect.addr,
1764 moved_sect.addr + moved_sect.size,1747 moved_sect.addr + moved_sect.size,
1765 });1748 });
17661749
1767 try self.allocateLocalSymbols(.{1750 try self.allocateLocalSymbols(moved_match, offset_amt);
1768 .seg = @intCast(u16, next),
1769 .sect = @intCast(u16, moved_sect_id),
1770 }, offset_amt);
1771 }1751 }
1772 }1752 }
1773 }1753 }
1774 sect.offset = new_offset;1754 sect_offset.* = new_offset;
1775 sect.addr = seg.inner.vmaddr + sect.offset - seg.inner.fileoff;1755 sect.addr = seg.inner.vmaddr + sect_offset.* - seg.inner.fileoff;
1776 log.debug(" (found new {s},{s} free space from 0x{x} to 0x{x})", .{1756 log.debug(" (found new {s},{s} free space from 0x{x} to 0x{x})", .{
1777 commands.segmentName(sect.*),1757 commands.segmentName(sect.*),
1778 commands.sectionName(sect.*),1758 commands.sectionName(sect.*),
...@@ -1820,17 +1800,7 @@ pub fn writeAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {...@@ -1820,17 +1800,7 @@ pub fn writeAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {
1820 const seg = self.load_commands.items[match.seg].Segment;1800 const seg = self.load_commands.items[match.seg].Segment;
1821 const sect = seg.sections.items[match.sect];1801 const sect = seg.sections.items[match.sect];
1822 const sym = self.locals.items[atom.local_sym_index];1802 const sym = self.locals.items[atom.local_sym_index];
1823 const sect_offset: u64 = blk: {1803 const sect_offset = self.getPtrToSectionOffset(match).*;
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 };
1834 const file_offset = sect_offset + sym.n_value - sect.addr;1804 const file_offset = sect_offset + sym.n_value - sect.addr;
1835 try atom.resolveRelocs(self);1805 try atom.resolveRelocs(self);
1836 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ self.getString(sym.n_strx), file_offset });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,6 +3866,25 @@ fn allocateSection(
3896 return index;3866 return index;
3897}3867}
38983868
3869fn 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
3899fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {3888fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
3900 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;3889 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
3901 const text_section = &text_segment.sections.items[self.text_section_index.?];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,32 +4017,23 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
40284017
4029 for (next_seg.sections.items) |*moved_sect, moved_sect_id| {4018 for (next_seg.sections.items) |*moved_sect, moved_sect_id| {
4030 // TODO put below snippet in a function.4019 // TODO put below snippet in a function.
4031 const moved_sect_offset = blk: {4020 const moved_match = MatchingSection{
4032 if (self.data_segment_cmd_index.? == next) {4021 .seg = @intCast(u16, next),
4033 if (self.bss_section_index) |idx| {4022 .sect = @intCast(u16, moved_sect_id),
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;
4041 };4023 };
4042 moved_sect_offset.* += offset_amt;4024 const ptr_sect_offset = self.getPtrToSectionOffset(moved_match);
4025 ptr_sect_offset.* += offset_amt;
4043 moved_sect.addr += offset_amt;4026 moved_sect.addr += offset_amt;
4044 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{4027 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
4045 commands.segmentName(moved_sect.*),4028 commands.segmentName(moved_sect.*),
4046 commands.sectionName(moved_sect.*),4029 commands.sectionName(moved_sect.*),
4047 moved_sect_offset.*,4030 ptr_sect_offset.*,
4048 moved_sect_offset.* + moved_sect.size,4031 ptr_sect_offset.* + moved_sect.size,
4049 moved_sect.addr,4032 moved_sect.addr,
4050 moved_sect.addr + moved_sect.size,4033 moved_sect.addr + moved_sect.size,
4051 });4034 });
40524035
4053 try self.allocateLocalSymbols(.{4036 try self.allocateLocalSymbols(moved_match, offset_amt);
4054 .seg = @intCast(u16, next),
4055 .sect = @intCast(u16, moved_sect_id),
4056 }, offset_amt);
4057 }4037 }
4058 }4038 }
4059 }4039 }