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
16671667 const expand_section = atom_placement == null or atom_placement.?.next == null;
16681668 if (expand_section) {
16691669 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;
16821672 const max_size = seg.allocatedSize(file_offset);
16831673 log.debug(" (section {s},{s} needed size 0x{x}, max available size 0x{x})", .{
16841674 commands.segmentName(sect.*),
......@@ -1741,38 +1731,28 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64
17411731 });
17421732
17431733 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),
17551737 };
1756 moved_sect_offset.* += offset_amt;
1738 const ptr_sect_offset = self.getPtrToSectionOffset(moved_match);
1739 ptr_sect_offset.* += offset_amt;
17571740 moved_sect.addr += offset_amt;
17581741 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
17591742 commands.segmentName(moved_sect.*),
17601743 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,
17631746 moved_sect.addr,
17641747 moved_sect.addr + moved_sect.size,
17651748 });
17661749
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);
17711751 }
17721752 }
17731753 }
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;
17761756 log.debug(" (found new {s},{s} free space from 0x{x} to 0x{x})", .{
17771757 commands.segmentName(sect.*),
17781758 commands.sectionName(sect.*),
......@@ -1820,17 +1800,7 @@ pub fn writeAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {
18201800 const seg = self.load_commands.items[match.seg].Segment;
18211801 const sect = seg.sections.items[match.sect];
18221802 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).*;
18341804 const file_offset = sect_offset + sym.n_value - sect.addr;
18351805 try atom.resolveRelocs(self);
18361806 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(
38963866 return index;
38973867}
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
38993888fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
39003889 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
39013890 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,
40284017
40294018 for (next_seg.sections.items) |*moved_sect, moved_sect_id| {
40304019 // 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),
40414023 };
4042 moved_sect_offset.* += offset_amt;
4024 const ptr_sect_offset = self.getPtrToSectionOffset(moved_match);
4025 ptr_sect_offset.* += offset_amt;
40434026 moved_sect.addr += offset_amt;
40444027 log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{
40454028 commands.segmentName(moved_sect.*),
40464029 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,
40494032 moved_sect.addr,
40504033 moved_sect.addr + moved_sect.size,
40514034 });
40524035
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);
40574037 }
40584038 }
40594039 }