authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-19 23:37:21+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-19 23:37:21+01:00
log6712575e044ff56e17d0cf8329d8edcd462453f9
treea978c91beaef51c1b8219b0b6e6e3d37269c35c6
parentca1d03fe77b8f173870804580b868568c1d61fbd

macho: preallocate space for linkedit hidden sections;


1 files changed, 122 insertions(+), 5 deletions(-)

src/link/MachO.zig+122-5
......@@ -1273,6 +1273,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12731273 }
12741274 header.reserved = 0;
12751275 self.header = header;
1276 self.cmd_table_dirty = true;
12761277 }
12771278 if (self.pagezero_segment_cmd_index == null) {
12781279 self.pagezero_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
......@@ -1410,6 +1411,14 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14101411 }
14111412 if (self.dyld_info_cmd_index == null) {
14121413 self.dyld_info_cmd_index = @intCast(u16, self.load_commands.items.len);
1414 const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1415
1416 // TODO Preallocate rebase, binding, and lazy binding info.
1417 const export_size = 2;
1418 const export_off = self.findFreeSpace(&linkedit_segment, export_size, 1);
1419
1420 log.debug("found export info free space 0x{x} to 0x{x}\n", .{ export_off, export_off + export_size });
1421
14131422 try self.load_commands.append(self.base.allocator, .{
14141423 .DyldInfoOnly = .{
14151424 .cmd = macho.LC_DYLD_INFO_ONLY,
......@@ -1430,6 +1439,18 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14301439 }
14311440 if (self.symtab_cmd_index == null) {
14321441 self.symtab_cmd_index = @intCast(u16, self.load_commands.items.len);
1442 const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1443
1444 const symtab_size = self.base.options.symbol_count_hint * @sizeOf(macho.nlist_64);
1445 const symtab_off = self.findFreeSpace(&linkedit_segment, symtab_size, @sizeOf(macho.nlist_64));
1446
1447 log.debug("found symbol table free space 0x{x} to 0x{x}\n", .{ symtab_off, symtab_off + symtab_size });
1448
1449 const strtab_size = 1;
1450 const strtab_off = self.findFreeSpace(&linkedit_segment, strtab_size, 1);
1451
1452 log.debug("found string table free space 0x{x} to 0x{x}\n", .{ strtab_off, strtab_off + strtab_size });
1453
14331454 try self.load_commands.append(self.base.allocator, .{
14341455 .Symtab = .{
14351456 .cmd = macho.LC_SYMTAB,
......@@ -1444,6 +1465,9 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14441465 }
14451466 if (self.dysymtab_cmd_index == null) {
14461467 self.dysymtab_cmd_index = @intCast(u16, self.load_commands.items.len);
1468
1469 // TODO Preallocate space for indirect symbol table.
1470
14471471 try self.load_commands.append(self.base.allocator, .{
14481472 .Dysymtab = .{
14491473 .cmd = macho.LC_DYSYMTAB,
......@@ -1552,6 +1576,13 @@ pub fn populateMissingMetadata(self: *MachO) !void {
15521576 }
15531577 if (self.code_signature_cmd_index == null) {
15541578 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
1579 const linkedit_segment = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1580
1581 const codesig_size = 1;
1582 const codesig_off = self.findFreeSpace(&linkedit_segment, codesig_size, @sizeOf(u64));
1583
1584 log.debug("found code signature free space 0x{x} to 0x{x}\n", .{ codesig_off, codesig_off + codesig_size });
1585
15551586 try self.load_commands.append(self.base.allocator, .{
15561587 .LinkeditData = .{
15571588 .cmd = macho.LC_CODE_SIGNATURE,
......@@ -1726,15 +1757,101 @@ fn nextSegmentAddressAndOffset(self: *MachO) NextSegmentAddressAndOffset {
17261757 };
17271758}
17281759
1760inline fn checkForCollision(start: u64, end: u64, off: u64, size: u64) ?u64 {
1761 const increased_size = satMul(size, alloc_num) / alloc_den;
1762 const test_end = off + increased_size;
1763 if (end > off and start < test_end) {
1764 return test_end;
1765 }
1766 return null;
1767}
1768
17291769fn detectAllocCollision(self: *MachO, segment: *const SegmentCommand, start: u64, size: u64) ?u64 {
17301770 const end = start + satMul(size, alloc_num) / alloc_den;
1731 for (segment.sections.items) |section| {
1732 const increased_size = satMul(section.size, alloc_num) / alloc_den;
1733 const test_end = section.offset + increased_size;
1734 if (end > section.offset and start < test_end) {
1735 return test_end;
1771
1772 if (parseAndCmpName(&segment.inner.segname, "__LINKEDIT")) {
1773 // __LINKEDIT is a weird segment where sections get their own load commands so we
1774 // special-case it.
1775 if (self.dyld_info_cmd_index) |idx| outer: {
1776 if (self.load_commands.items.len == idx) break :outer;
1777 const dyld_info = self.load_commands.items[idx].DyldInfoOnly;
1778 if (checkForCollision(start, end, dyld_info.rebase_off, dyld_info.rebase_size)) |pos| {
1779 return pos;
1780 }
1781 // Binding info
1782 if (checkForCollision(start, end, dyld_info.bind_off, dyld_info.bind_size)) |pos| {
1783 return pos;
1784 }
1785 // Weak binding info
1786 if (checkForCollision(start, end, dyld_info.weak_bind_off, dyld_info.weak_bind_size)) |pos| {
1787 return pos;
1788 }
1789 // Lazy binding info
1790 if (checkForCollision(start, end, dyld_info.lazy_bind_off, dyld_info.lazy_bind_size)) |pos| {
1791 return pos;
1792 }
1793 // Export info
1794 if (checkForCollision(start, end, dyld_info.export_off, dyld_info.export_size)) |pos| {
1795 return pos;
1796 }
1797 }
1798
1799 if (self.function_starts_cmd_index) |idx| outer: {
1800 if (self.load_commands.items.len == idx) break :outer;
1801 const fstart = self.load_commands.items[idx].LinkeditData;
1802 if (checkForCollision(start, end, fstart.dataoff, fstart.datasize)) |pos| {
1803 return pos;
1804 }
1805 }
1806
1807 if (self.data_in_code_cmd_index) |idx| outer: {
1808 if (self.load_commands.items.len == idx) break :outer;
1809 const dic = self.load_commands.items[idx].LinkeditData;
1810 if (checkForCollision(start, end, dic.dataoff, dic.datasize)) |pos| {
1811 return pos;
1812 }
1813 }
1814
1815 if (self.dysymtab_cmd_index) |idx| outer: {
1816 if (self.load_commands.items.len == idx) break :outer;
1817 const dysymtab = self.load_commands.items[idx].Dysymtab;
1818 // Indirect symbol table
1819 const nindirectsize = dysymtab.nindirectsyms * @sizeOf(u32);
1820 if (checkForCollision(start, end, dysymtab.indirectsymoff, nindirectsize)) |pos| {
1821 return pos;
1822 }
1823 // TODO Handle more dynamic symbol table sections.
1824 }
1825
1826 if (self.symtab_cmd_index) |idx| outer: {
1827 if (self.load_commands.items.len == idx) break :outer;
1828 const symtab = self.load_commands.items[idx].Symtab;
1829 // Symbol table
1830 const symsize = symtab.nsyms * @sizeOf(macho.nlist_64);
1831 if (checkForCollision(start, end, symtab.symoff, symsize)) |pos| {
1832 return pos;
1833 }
1834 // String table
1835 if (checkForCollision(start, end, symtab.stroff, symtab.strsize)) |pos| {
1836 return pos;
1837 }
1838 }
1839
1840 if (self.code_signature_cmd_index) |idx| outer: {
1841 if (self.load_commands.items.len == idx) break :outer;
1842 const codesig = self.load_commands.items[idx].LinkeditData;
1843 if (checkForCollision(start, end, codesig.dataoff, codesig.datasize)) |pos| {
1844 return pos;
1845 }
1846 }
1847 } else {
1848 for (segment.sections.items) |section| {
1849 if (checkForCollision(start, end, section.offset, section.size)) |pos| {
1850 return pos;
1851 }
17361852 }
17371853 }
1854
17381855 return null;
17391856}
17401857