authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-19 23:51:39+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-19 23:51:39+01:00
log3f7dbde92af9dbcc657b0f3dffb68adf90d68989
tree029b214feca8980103ab62f7c3a5fbf49f643f46
parent6712575e044ff56e17d0cf8329d8edcd462453f9

macho: bring back allocatedSize function


1 files changed, 53 insertions(+), 0 deletions(-)

src/link/MachO.zig+53
...@@ -1757,6 +1757,58 @@ fn nextSegmentAddressAndOffset(self: *MachO) NextSegmentAddressAndOffset {...@@ -1757,6 +1757,58 @@ fn nextSegmentAddressAndOffset(self: *MachO) NextSegmentAddressAndOffset {
1757 };1757 };
1758}1758}
17591759
1760fn allocatedSize(self: *MachO, segment: *const SegmentCommand, start: u64) u64 {
1761 assert(start > 0);
1762 var min_pos: u64 = std.math.maxInt(u64);
1763
1764 if (parseAndCmpName(&segment.inner.segname, "__LINKEDIT")) {
1765 assert(segment.sections.items.len == 0);
1766 // __LINKEDIT is a weird segment where sections get their own load commands so we
1767 // special-case it.
1768 if (self.dyld_info_cmd_index) |idx| {
1769 const dyld_info = self.load_commands.items[idx].DyldInfoOnly;
1770 if (dyld_info.rebase_off > start and dyld_info.rebase_off < min_pos) min_pos = off;
1771 if (dyld_info.bind_off > start and dyld_info.bind_off < min_pos) min_pos = off;
1772 if (dyld_info.weak_bind_off > start and dyld_info.weak_bind_off < min_pos) min_pos = off;
1773 if (dyld_info.lazy_bind_off > start and dyld_info.lazy_bind_off < min_pos) min_pos = off;
1774 if (dyld_info.export_off > start and dyld_info.export_off < min_pos) min_pos = off;
1775 }
1776
1777 if (self.function_starts_cmd_index) |idx| {
1778 const fstart = self.load_commands.items[idx].LinkeditData;
1779 if (fstart.dataoff > start and fstart.dataoff < min_pos) min_pos = off;
1780 }
1781
1782 if (self.data_in_code_cmd_index) |idx| {
1783 const dic = self.load_commands.items[idx].LinkeditData;
1784 if (dic.dataoff > start and dic.dataoff < min_pos) min_pos = off;
1785 }
1786
1787 if (self.dysymtab_cmd_index) |idx| {
1788 const dysymtab = self.load_commands.items[idx].Dysymtab;
1789 if (dysymtab.indirectsymoff > start and dysymtab.indirectsymoff < min_pos) min_pos = off;
1790 // TODO Handle more dynamic symbol table sections.
1791 }
1792
1793 if (self.symtab_cmd_index) |idx| {
1794 const symtab = self.load_commands.items[idx].Symtab;
1795 if (symtab.symoff > start and symtab.symoff < min_pos) min_pos = off;
1796 if (symtab.stroff > start and symtab.stroff < min_pos) min_pos = off;
1797 }
1798
1799 if (self.code_signature_cmd_index) |idx| {
1800 const codesig = self.load_commands.items[idx].LinkeditData;
1801 if (codesig.dataoff > start and codesig.dataoff < min_pos) min_pos = off;
1802 }
1803 } else {
1804 for (segment.sections.items) |section| {
1805 if (section.offset > start and sections.offset < min_pos) min_pos = off;
1806 }
1807 }
1808
1809 return min_pos - start;
1810}
1811
1760inline fn checkForCollision(start: u64, end: u64, off: u64, size: u64) ?u64 {1812inline fn checkForCollision(start: u64, end: u64, off: u64, size: u64) ?u64 {
1761 const increased_size = satMul(size, alloc_num) / alloc_den;1813 const increased_size = satMul(size, alloc_num) / alloc_den;
1762 const test_end = off + increased_size;1814 const test_end = off + increased_size;
...@@ -1770,6 +1822,7 @@ fn detectAllocCollision(self: *MachO, segment: *const SegmentCommand, start: u64...@@ -1770,6 +1822,7 @@ fn detectAllocCollision(self: *MachO, segment: *const SegmentCommand, start: u64
1770 const end = start + satMul(size, alloc_num) / alloc_den;1822 const end = start + satMul(size, alloc_num) / alloc_den;
17711823
1772 if (parseAndCmpName(&segment.inner.segname, "__LINKEDIT")) {1824 if (parseAndCmpName(&segment.inner.segname, "__LINKEDIT")) {
1825 assert(segment.sections.items.len == 0);
1773 // __LINKEDIT is a weird segment where sections get their own load commands so we1826 // __LINKEDIT is a weird segment where sections get their own load commands so we
1774 // special-case it.1827 // special-case it.
1775 if (self.dyld_info_cmd_index) |idx| outer: {1828 if (self.dyld_info_cmd_index) |idx| outer: {