| author | |
| committer | |
| log | 9c3ebe0216306b5e346ec52959de41d1b4d504d9 |
| tree | 23dd11a4fdb7a8fadbd2ea3b7c6837ec2baef928 |
| parent | e08f7ba8896ad64a696424cff9b9c0963a7a4a0b |
4 files changed, 117 insertions(+), 159 deletions(-)
src/link/MachO.zig+45-81| ... | ... | @@ -41,8 +41,6 @@ d_sym: ?DebugSymbols = null, |
| 41 | 41 | /// For x86_64 that's 4KB, whereas for aarch64, that's 16KB. |
| 42 | 42 | page_size: u16, |
| 43 | 43 | |
| 44 | /// Mach-O header | |
| 45 | header: ?macho.mach_header_64 = null, | |
| 46 | 44 | /// We commit 0x1000 = 4096 bytes of space to the header and |
| 47 | 45 | /// the table of load commands. This should be plenty for any |
| 48 | 46 | /// potential future extensions. |
| ... | ... | @@ -128,7 +126,6 @@ offset_table: std.ArrayListUnmanaged(GOTEntry) = .{}, |
| 128 | 126 | error_flags: File.ErrorFlags = File.ErrorFlags{}, |
| 129 | 127 | |
| 130 | 128 | offset_table_count_dirty: bool = false, |
| 131 | header_dirty: bool = false, | |
| 132 | 129 | load_commands_dirty: bool = false, |
| 133 | 130 | rebase_info_dirty: bool = false, |
| 134 | 131 | binding_info_dirty: bool = false, |
| ... | ... | @@ -497,7 +494,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { |
| 497 | 494 | } |
| 498 | 495 | |
| 499 | 496 | assert(!self.offset_table_count_dirty); |
| 500 | assert(!self.header_dirty); | |
| 501 | 497 | assert(!self.load_commands_dirty); |
| 502 | 498 | assert(!self.rebase_info_dirty); |
| 503 | 499 | assert(!self.binding_info_dirty); |
| ... | ... | @@ -1488,54 +1484,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1488 | 1484 | .Lib => return error.TODOImplementWritingLibFiles, |
| 1489 | 1485 | } |
| 1490 | 1486 | |
| 1491 | if (self.header == null) { | |
| 1492 | var header: macho.mach_header_64 = undefined; | |
| 1493 | header.magic = macho.MH_MAGIC_64; | |
| 1494 | ||
| 1495 | const CpuInfo = struct { | |
| 1496 | cpu_type: macho.cpu_type_t, | |
| 1497 | cpu_subtype: macho.cpu_subtype_t, | |
| 1498 | }; | |
| 1499 | ||
| 1500 | const cpu_info: CpuInfo = switch (self.base.options.target.cpu.arch) { | |
| 1501 | .aarch64 => .{ | |
| 1502 | .cpu_type = macho.CPU_TYPE_ARM64, | |
| 1503 | .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL, | |
| 1504 | }, | |
| 1505 | .x86_64 => .{ | |
| 1506 | .cpu_type = macho.CPU_TYPE_X86_64, | |
| 1507 | .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL, | |
| 1508 | }, | |
| 1509 | else => return error.UnsupportedMachOArchitecture, | |
| 1510 | }; | |
| 1511 | header.cputype = cpu_info.cpu_type; | |
| 1512 | header.cpusubtype = cpu_info.cpu_subtype; | |
| 1513 | ||
| 1514 | const filetype: u32 = switch (self.base.options.output_mode) { | |
| 1515 | .Exe => macho.MH_EXECUTE, | |
| 1516 | .Obj => macho.MH_OBJECT, | |
| 1517 | .Lib => switch (self.base.options.link_mode) { | |
| 1518 | .Static => return error.TODOStaticLibMachOType, | |
| 1519 | .Dynamic => macho.MH_DYLIB, | |
| 1520 | }, | |
| 1521 | }; | |
| 1522 | header.filetype = filetype; | |
| 1523 | // These will get populated at the end of flushing the results to file. | |
| 1524 | header.ncmds = 0; | |
| 1525 | header.sizeofcmds = 0; | |
| 1526 | ||
| 1527 | switch (self.base.options.output_mode) { | |
| 1528 | .Exe => { | |
| 1529 | header.flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE; | |
| 1530 | }, | |
| 1531 | else => { | |
| 1532 | header.flags = 0; | |
| 1533 | }, | |
| 1534 | } | |
| 1535 | header.reserved = 0; | |
| 1536 | self.header = header; | |
| 1537 | self.header_dirty = true; | |
| 1538 | } | |
| 1539 | 1487 | if (self.pagezero_segment_cmd_index == null) { |
| 1540 | 1488 | self.pagezero_segment_cmd_index = @intCast(u16, self.load_commands.items.len); |
| 1541 | 1489 | try self.load_commands.append(self.base.allocator, .{ |
| ... | ... | @@ -1543,7 +1491,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1543 | 1491 | .vmsize = 0x100000000, // size always set to 4GB |
| 1544 | 1492 | }), |
| 1545 | 1493 | }); |
| 1546 | self.header_dirty = true; | |
| 1547 | 1494 | self.load_commands_dirty = true; |
| 1548 | 1495 | } |
| 1549 | 1496 | if (self.text_segment_cmd_index == null) { |
| ... | ... | @@ -1567,7 +1514,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1567 | 1514 | .initprot = initprot, |
| 1568 | 1515 | }), |
| 1569 | 1516 | }); |
| 1570 | self.header_dirty = true; | |
| 1571 | 1517 | self.load_commands_dirty = true; |
| 1572 | 1518 | } |
| 1573 | 1519 | if (self.text_section_index == null) { |
| ... | ... | @@ -1592,7 +1538,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1592 | 1538 | .@"align" = alignment, |
| 1593 | 1539 | .flags = flags, |
| 1594 | 1540 | }); |
| 1595 | self.header_dirty = true; | |
| 1596 | 1541 | self.load_commands_dirty = true; |
| 1597 | 1542 | } |
| 1598 | 1543 | if (self.stubs_section_index == null) { |
| ... | ... | @@ -1624,7 +1569,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1624 | 1569 | .flags = flags, |
| 1625 | 1570 | .reserved2 = stub_size, |
| 1626 | 1571 | }); |
| 1627 | self.header_dirty = true; | |
| 1628 | 1572 | self.load_commands_dirty = true; |
| 1629 | 1573 | } |
| 1630 | 1574 | if (self.stub_helper_section_index == null) { |
| ... | ... | @@ -1650,7 +1594,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1650 | 1594 | .@"align" = alignment, |
| 1651 | 1595 | .flags = flags, |
| 1652 | 1596 | }); |
| 1653 | self.header_dirty = true; | |
| 1654 | 1597 | self.load_commands_dirty = true; |
| 1655 | 1598 | } |
| 1656 | 1599 | if (self.data_const_segment_cmd_index == null) { |
| ... | ... | @@ -1674,7 +1617,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1674 | 1617 | .initprot = initprot, |
| 1675 | 1618 | }), |
| 1676 | 1619 | }); |
| 1677 | self.header_dirty = true; | |
| 1678 | 1620 | self.load_commands_dirty = true; |
| 1679 | 1621 | } |
| 1680 | 1622 | if (self.got_section_index == null) { |
| ... | ... | @@ -1695,7 +1637,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1695 | 1637 | .@"align" = 3, // 2^3 = @sizeOf(u64) |
| 1696 | 1638 | .flags = flags, |
| 1697 | 1639 | }); |
| 1698 | self.header_dirty = true; | |
| 1699 | 1640 | self.load_commands_dirty = true; |
| 1700 | 1641 | } |
| 1701 | 1642 | if (self.data_segment_cmd_index == null) { |
| ... | ... | @@ -1719,7 +1660,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1719 | 1660 | .initprot = initprot, |
| 1720 | 1661 | }), |
| 1721 | 1662 | }); |
| 1722 | self.header_dirty = true; | |
| 1723 | 1663 | self.load_commands_dirty = true; |
| 1724 | 1664 | } |
| 1725 | 1665 | if (self.la_symbol_ptr_section_index == null) { |
| ... | ... | @@ -1740,7 +1680,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1740 | 1680 | .@"align" = 3, // 2^3 = @sizeOf(u64) |
| 1741 | 1681 | .flags = flags, |
| 1742 | 1682 | }); |
| 1743 | self.header_dirty = true; | |
| 1744 | 1683 | self.load_commands_dirty = true; |
| 1745 | 1684 | } |
| 1746 | 1685 | if (self.data_section_index == null) { |
| ... | ... | @@ -1759,7 +1698,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1759 | 1698 | .offset = @intCast(u32, off), |
| 1760 | 1699 | .@"align" = 3, // 2^3 = @sizeOf(u64) |
| 1761 | 1700 | }); |
| 1762 | self.header_dirty = true; | |
| 1763 | 1701 | self.load_commands_dirty = true; |
| 1764 | 1702 | } |
| 1765 | 1703 | if (self.linkedit_segment_cmd_index == null) { |
| ... | ... | @@ -1779,7 +1717,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1779 | 1717 | .initprot = initprot, |
| 1780 | 1718 | }), |
| 1781 | 1719 | }); |
| 1782 | self.header_dirty = true; | |
| 1783 | 1720 | self.load_commands_dirty = true; |
| 1784 | 1721 | } |
| 1785 | 1722 | if (self.dyld_info_cmd_index == null) { |
| ... | ... | @@ -1826,7 +1763,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1826 | 1763 | dyld.export_off = @intCast(u32, export_off); |
| 1827 | 1764 | dyld.export_size = expected_size; |
| 1828 | 1765 | |
| 1829 | self.header_dirty = true; | |
| 1830 | 1766 | self.load_commands_dirty = true; |
| 1831 | 1767 | } |
| 1832 | 1768 | if (self.symtab_cmd_index == null) { |
| ... | ... | @@ -1858,7 +1794,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1858 | 1794 | symtab.stroff = @intCast(u32, strtab_off); |
| 1859 | 1795 | symtab.strsize = @intCast(u32, strtab_size); |
| 1860 | 1796 | |
| 1861 | self.header_dirty = true; | |
| 1862 | 1797 | self.load_commands_dirty = true; |
| 1863 | 1798 | self.string_table_dirty = true; |
| 1864 | 1799 | } |
| ... | ... | @@ -1895,7 +1830,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1895 | 1830 | .nlocrel = 0, |
| 1896 | 1831 | }, |
| 1897 | 1832 | }); |
| 1898 | self.header_dirty = true; | |
| 1899 | 1833 | self.load_commands_dirty = true; |
| 1900 | 1834 | } |
| 1901 | 1835 | if (self.dylinker_cmd_index == null) { |
| ... | ... | @@ -1914,7 +1848,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1914 | 1848 | mem.set(u8, dylinker_cmd.data, 0); |
| 1915 | 1849 | mem.copy(u8, dylinker_cmd.data, mem.spanZ(DEFAULT_DYLD_PATH)); |
| 1916 | 1850 | try self.load_commands.append(self.base.allocator, .{ .Dylinker = dylinker_cmd }); |
| 1917 | self.header_dirty = true; | |
| 1918 | 1851 | self.load_commands_dirty = true; |
| 1919 | 1852 | } |
| 1920 | 1853 | if (self.libsystem_cmd_index == null) { |
| ... | ... | @@ -1925,7 +1858,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1925 | 1858 | |
| 1926 | 1859 | try self.load_commands.append(self.base.allocator, .{ .Dylib = dylib_cmd }); |
| 1927 | 1860 | |
| 1928 | self.header_dirty = true; | |
| 1929 | 1861 | self.load_commands_dirty = true; |
| 1930 | 1862 | } |
| 1931 | 1863 | if (self.main_cmd_index == null) { |
| ... | ... | @@ -1938,7 +1870,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1938 | 1870 | .stacksize = 0, |
| 1939 | 1871 | }, |
| 1940 | 1872 | }); |
| 1941 | self.header_dirty = true; | |
| 1942 | 1873 | self.load_commands_dirty = true; |
| 1943 | 1874 | } |
| 1944 | 1875 | if (self.version_min_cmd_index == null) { |
| ... | ... | @@ -1960,7 +1891,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1960 | 1891 | .sdk = version, |
| 1961 | 1892 | }, |
| 1962 | 1893 | }); |
| 1963 | self.header_dirty = true; | |
| 1964 | 1894 | self.load_commands_dirty = true; |
| 1965 | 1895 | } |
| 1966 | 1896 | if (self.source_version_cmd_index == null) { |
| ... | ... | @@ -1972,7 +1902,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1972 | 1902 | .version = 0x0, |
| 1973 | 1903 | }, |
| 1974 | 1904 | }); |
| 1975 | self.header_dirty = true; | |
| 1976 | 1905 | self.load_commands_dirty = true; |
| 1977 | 1906 | } |
| 1978 | 1907 | if (self.uuid_cmd_index == null) { |
| ... | ... | @@ -1984,7 +1913,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1984 | 1913 | }; |
| 1985 | 1914 | std.crypto.random.bytes(&uuid_cmd.uuid); |
| 1986 | 1915 | try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd }); |
| 1987 | self.header_dirty = true; | |
| 1988 | 1916 | self.load_commands_dirty = true; |
| 1989 | 1917 | } |
| 1990 | 1918 | if (self.code_signature_cmd_index == null) { |
| ... | ... | @@ -1997,7 +1925,6 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 1997 | 1925 | .datasize = 0, |
| 1998 | 1926 | }, |
| 1999 | 1927 | }); |
| 2000 | self.header_dirty = true; | |
| 2001 | 1928 | self.load_commands_dirty = true; |
| 2002 | 1929 | } |
| 2003 | 1930 | if (!self.nonlazy_imports.contains("dyld_stub_binder")) { |
| ... | ... | @@ -3224,24 +3151,57 @@ fn writeLoadCommands(self: *MachO) !void { |
| 3224 | 3151 | } |
| 3225 | 3152 | |
| 3226 | 3153 | const off = @sizeOf(macho.mach_header_64); |
| 3154 | ||
| 3227 | 3155 | log.debug("writing {} load commands from 0x{x} to 0x{x}", .{ self.load_commands.items.len, off, off + sizeofcmds }); |
| 3156 | ||
| 3228 | 3157 | try self.base.file.?.pwriteAll(buffer, off); |
| 3229 | 3158 | self.load_commands_dirty = false; |
| 3230 | 3159 | } |
| 3231 | 3160 | |
| 3232 | 3161 | /// Writes Mach-O file header. |
| 3233 | 3162 | fn writeHeader(self: *MachO) !void { |
| 3234 | if (!self.header_dirty) return; | |
| 3163 | var header = emptyHeader(.{ | |
| 3164 | .flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE | macho.MH_TWOLEVEL, | |
| 3165 | }); | |
| 3166 | ||
| 3167 | switch (self.base.options.target.cpu.arch) { | |
| 3168 | .aarch64 => { | |
| 3169 | header.cputype = macho.CPU_TYPE_ARM64; | |
| 3170 | header.cpusubtype = macho.CPU_SUBTYPE_ARM_ALL; | |
| 3171 | }, | |
| 3172 | .x86_64 => { | |
| 3173 | header.cputype = macho.CPU_TYPE_X86_64; | |
| 3174 | header.cpusubtype = macho.CPU_SUBTYPE_X86_64_ALL; | |
| 3175 | }, | |
| 3176 | else => return error.UnsupportedCpuArchitecture, | |
| 3177 | } | |
| 3178 | ||
| 3179 | switch (self.base.options.output_mode) { | |
| 3180 | .Exe => { | |
| 3181 | header.filetype = macho.MH_EXECUTE; | |
| 3182 | }, | |
| 3183 | .Lib => { | |
| 3184 | // By this point, it can only be a dylib. | |
| 3185 | header.filetype = macho.MH_DYLIB; | |
| 3186 | header.flags |= macho.MH_NO_REEXPORTED_DYLIBS; | |
| 3187 | }, | |
| 3188 | else => unreachable, | |
| 3189 | } | |
| 3190 | ||
| 3191 | if (self.hasTlvDescriptors()) { | |
| 3192 | header.flags |= macho.MH_HAS_TLV_DESCRIPTORS; | |
| 3193 | } | |
| 3194 | ||
| 3195 | header.ncmds = @intCast(u32, self.load_commands.items.len); | |
| 3196 | header.sizeofcmds = 0; | |
| 3235 | 3197 | |
| 3236 | self.header.?.ncmds = @intCast(u32, self.load_commands.items.len); | |
| 3237 | var sizeofcmds: u32 = 0; | |
| 3238 | 3198 | for (self.load_commands.items) |cmd| { |
| 3239 | sizeofcmds += cmd.cmdsize(); | |
| 3199 | header.sizeofcmds += cmd.cmdsize(); | |
| 3240 | 3200 | } |
| 3241 | self.header.?.sizeofcmds = sizeofcmds; | |
| 3242 | log.debug("writing Mach-O header {}", .{self.header.?}); | |
| 3243 | try self.base.file.?.pwriteAll(mem.asBytes(&self.header.?), 0); | |
| 3244 | self.header_dirty = false; | |
| 3201 | ||
| 3202 | log.debug("writing Mach-O header {}", .{header}); | |
| 3203 | ||
| 3204 | try self.base.file.?.pwriteAll(mem.asBytes(&header), 0); | |
| 3245 | 3205 | } |
| 3246 | 3206 | |
| 3247 | 3207 | pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| ... | ... | @@ -3249,3 +3209,7 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 3249 | 3209 | return std.math.add(@TypeOf(actual_size), actual_size, actual_size / ideal_factor) catch |
| 3250 | 3210 | std.math.maxInt(@TypeOf(actual_size)); |
| 3251 | 3211 | } |
| 3212 | ||
| 3213 | fn hasTlvDescriptors(_: *MachO) bool { | |
| 3214 | return false; | |
| 3215 | } |
src/link/MachO/DebugSymbols.zig+35-53| ... | ... | @@ -3,7 +3,7 @@ const DebugSymbols = @This(); |
| 3 | 3 | const std = @import("std"); |
| 4 | 4 | const assert = std.debug.assert; |
| 5 | 5 | const fs = std.fs; |
| 6 | const log = std.log.scoped(.link); | |
| 6 | const log = std.log.scoped(.dsym); | |
| 7 | 7 | const macho = std.macho; |
| 8 | 8 | const mem = std.mem; |
| 9 | 9 | const DW = std.dwarf; |
| ... | ... | @@ -27,9 +27,6 @@ const page_size: u16 = 0x1000; |
| 27 | 27 | base: *MachO, |
| 28 | 28 | file: fs.File, |
| 29 | 29 | |
| 30 | /// Mach header | |
| 31 | header: ?macho.mach_header_64 = null, | |
| 32 | ||
| 33 | 30 | /// Table of all load commands |
| 34 | 31 | load_commands: std.ArrayListUnmanaged(LoadCommand) = .{}, |
| 35 | 32 | /// __PAGEZERO segment |
| ... | ... | @@ -78,7 +75,6 @@ dbg_info_decl_last: ?*TextBlock = null, |
| 78 | 75 | /// Table of debug symbol names aka the debug string table. |
| 79 | 76 | debug_string_table: std.ArrayListUnmanaged(u8) = .{}, |
| 80 | 77 | |
| 81 | header_dirty: bool = false, | |
| 82 | 78 | load_commands_dirty: bool = false, |
| 83 | 79 | string_table_dirty: bool = false, |
| 84 | 80 | debug_string_table_dirty: bool = false, |
| ... | ... | @@ -106,26 +102,10 @@ const min_nop_size = 2; |
| 106 | 102 | /// You must call this function *after* `MachO.populateMissingMetadata()` |
| 107 | 103 | /// has been called to get a viable debug symbols output. |
| 108 | 104 | pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void { |
| 109 | if (self.header == null) { | |
| 110 | const base_header = self.base.header.?; | |
| 111 | var header: macho.mach_header_64 = undefined; | |
| 112 | header.magic = macho.MH_MAGIC_64; | |
| 113 | header.cputype = base_header.cputype; | |
| 114 | header.cpusubtype = base_header.cpusubtype; | |
| 115 | header.filetype = macho.MH_DSYM; | |
| 116 | // These will get populated at the end of flushing the results to file. | |
| 117 | header.ncmds = 0; | |
| 118 | header.sizeofcmds = 0; | |
| 119 | header.flags = 0; | |
| 120 | header.reserved = 0; | |
| 121 | self.header = header; | |
| 122 | self.header_dirty = true; | |
| 123 | } | |
| 124 | 105 | if (self.uuid_cmd_index == null) { |
| 125 | 106 | const base_cmd = self.base.load_commands.items[self.base.uuid_cmd_index.?]; |
| 126 | 107 | self.uuid_cmd_index = @intCast(u16, self.load_commands.items.len); |
| 127 | 108 | try self.load_commands.append(allocator, base_cmd); |
| 128 | self.header_dirty = true; | |
| 129 | 109 | self.load_commands_dirty = true; |
| 130 | 110 | } |
| 131 | 111 | if (self.symtab_cmd_index == null) { |
| ... | ... | @@ -134,11 +114,11 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 134 | 114 | const symtab_size = base_cmd.nsyms * @sizeOf(macho.nlist_64); |
| 135 | 115 | const symtab_off = self.findFreeSpaceLinkedit(symtab_size, @sizeOf(macho.nlist_64)); |
| 136 | 116 | |
| 137 | log.debug("found dSym symbol table free space 0x{x} to 0x{x}", .{ symtab_off, symtab_off + symtab_size }); | |
| 117 | log.debug("found symbol table free space 0x{x} to 0x{x}", .{ symtab_off, symtab_off + symtab_size }); | |
| 138 | 118 | |
| 139 | 119 | const strtab_off = self.findFreeSpaceLinkedit(base_cmd.strsize, 1); |
| 140 | 120 | |
| 141 | log.debug("found dSym string table free space 0x{x} to 0x{x}", .{ strtab_off, strtab_off + base_cmd.strsize }); | |
| 121 | log.debug("found string table free space 0x{x} to 0x{x}", .{ strtab_off, strtab_off + base_cmd.strsize }); | |
| 142 | 122 | |
| 143 | 123 | try self.load_commands.append(allocator, .{ |
| 144 | 124 | .Symtab = .{ |
| ... | ... | @@ -150,7 +130,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 150 | 130 | .strsize = base_cmd.strsize, |
| 151 | 131 | }, |
| 152 | 132 | }); |
| 153 | self.header_dirty = true; | |
| 154 | 133 | self.load_commands_dirty = true; |
| 155 | 134 | self.string_table_dirty = true; |
| 156 | 135 | } |
| ... | ... | @@ -159,7 +138,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 159 | 138 | const base_cmd = self.base.load_commands.items[self.base.pagezero_segment_cmd_index.?].Segment; |
| 160 | 139 | const cmd = try self.copySegmentCommand(allocator, base_cmd); |
| 161 | 140 | try self.load_commands.append(allocator, .{ .Segment = cmd }); |
| 162 | self.header_dirty = true; | |
| 163 | 141 | self.load_commands_dirty = true; |
| 164 | 142 | } |
| 165 | 143 | if (self.text_segment_cmd_index == null) { |
| ... | ... | @@ -167,7 +145,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 167 | 145 | const base_cmd = self.base.load_commands.items[self.base.text_segment_cmd_index.?].Segment; |
| 168 | 146 | const cmd = try self.copySegmentCommand(allocator, base_cmd); |
| 169 | 147 | try self.load_commands.append(allocator, .{ .Segment = cmd }); |
| 170 | self.header_dirty = true; | |
| 171 | 148 | self.load_commands_dirty = true; |
| 172 | 149 | } |
| 173 | 150 | if (self.data_const_segment_cmd_index == null) outer: { |
| ... | ... | @@ -176,7 +153,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 176 | 153 | const base_cmd = self.base.load_commands.items[self.base.data_const_segment_cmd_index.?].Segment; |
| 177 | 154 | const cmd = try self.copySegmentCommand(allocator, base_cmd); |
| 178 | 155 | try self.load_commands.append(allocator, .{ .Segment = cmd }); |
| 179 | self.header_dirty = true; | |
| 180 | 156 | self.load_commands_dirty = true; |
| 181 | 157 | } |
| 182 | 158 | if (self.data_segment_cmd_index == null) outer: { |
| ... | ... | @@ -185,7 +161,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 185 | 161 | const base_cmd = self.base.load_commands.items[self.base.data_segment_cmd_index.?].Segment; |
| 186 | 162 | const cmd = try self.copySegmentCommand(allocator, base_cmd); |
| 187 | 163 | try self.load_commands.append(allocator, .{ .Segment = cmd }); |
| 188 | self.header_dirty = true; | |
| 189 | 164 | self.load_commands_dirty = true; |
| 190 | 165 | } |
| 191 | 166 | if (self.linkedit_segment_cmd_index == null) { |
| ... | ... | @@ -196,7 +171,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 196 | 171 | cmd.inner.fileoff = self.linkedit_off; |
| 197 | 172 | cmd.inner.filesize = self.linkedit_size; |
| 198 | 173 | try self.load_commands.append(allocator, .{ .Segment = cmd }); |
| 199 | self.header_dirty = true; | |
| 200 | 174 | self.load_commands_dirty = true; |
| 201 | 175 | } |
| 202 | 176 | if (self.dwarf_segment_cmd_index == null) { |
| ... | ... | @@ -208,7 +182,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 208 | 182 | const off = linkedit.inner.fileoff + linkedit.inner.filesize; |
| 209 | 183 | const vmaddr = linkedit.inner.vmaddr + linkedit.inner.vmsize; |
| 210 | 184 | |
| 211 | log.debug("found dSym __DWARF segment free space 0x{x} to 0x{x}", .{ off, off + needed_size }); | |
| 185 | log.debug("found __DWARF segment free space 0x{x} to 0x{x}", .{ off, off + needed_size }); | |
| 212 | 186 | |
| 213 | 187 | try self.load_commands.append(allocator, .{ |
| 214 | 188 | .Segment = SegmentCommand.empty("__DWARF", .{ |
| ... | ... | @@ -218,7 +192,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 218 | 192 | .filesize = needed_size, |
| 219 | 193 | }), |
| 220 | 194 | }); |
| 221 | self.header_dirty = true; | |
| 222 | 195 | self.load_commands_dirty = true; |
| 223 | 196 | } |
| 224 | 197 | if (self.debug_str_section_index == null) { |
| ... | ... | @@ -232,7 +205,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 232 | 205 | .offset = @intCast(u32, dwarf_segment.inner.fileoff), |
| 233 | 206 | .@"align" = 1, |
| 234 | 207 | }); |
| 235 | self.header_dirty = true; | |
| 236 | 208 | self.load_commands_dirty = true; |
| 237 | 209 | self.debug_string_table_dirty = true; |
| 238 | 210 | } |
| ... | ... | @@ -244,7 +216,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 244 | 216 | const p_align = 1; |
| 245 | 217 | const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null); |
| 246 | 218 | |
| 247 | log.debug("found dSym __debug_info free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 219 | log.debug("found __debug_info free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 248 | 220 | |
| 249 | 221 | try dwarf_segment.addSection(allocator, "__debug_info", .{ |
| 250 | 222 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| ... | ... | @@ -252,7 +224,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 252 | 224 | .offset = @intCast(u32, off), |
| 253 | 225 | .@"align" = p_align, |
| 254 | 226 | }); |
| 255 | self.header_dirty = true; | |
| 256 | 227 | self.load_commands_dirty = true; |
| 257 | 228 | self.debug_info_header_dirty = true; |
| 258 | 229 | } |
| ... | ... | @@ -264,7 +235,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 264 | 235 | const p_align = 1; |
| 265 | 236 | const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null); |
| 266 | 237 | |
| 267 | log.debug("found dSym __debug_abbrev free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 238 | log.debug("found __debug_abbrev free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 268 | 239 | |
| 269 | 240 | try dwarf_segment.addSection(allocator, "__debug_abbrev", .{ |
| 270 | 241 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| ... | ... | @@ -272,7 +243,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 272 | 243 | .offset = @intCast(u32, off), |
| 273 | 244 | .@"align" = p_align, |
| 274 | 245 | }); |
| 275 | self.header_dirty = true; | |
| 276 | 246 | self.load_commands_dirty = true; |
| 277 | 247 | self.debug_abbrev_section_dirty = true; |
| 278 | 248 | } |
| ... | ... | @@ -284,7 +254,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 284 | 254 | const p_align = 16; |
| 285 | 255 | const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null); |
| 286 | 256 | |
| 287 | log.debug("found dSym __debug_aranges free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 257 | log.debug("found __debug_aranges free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 288 | 258 | |
| 289 | 259 | try dwarf_segment.addSection(allocator, "__debug_aranges", .{ |
| 290 | 260 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| ... | ... | @@ -292,7 +262,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 292 | 262 | .offset = @intCast(u32, off), |
| 293 | 263 | .@"align" = p_align, |
| 294 | 264 | }); |
| 295 | self.header_dirty = true; | |
| 296 | 265 | self.load_commands_dirty = true; |
| 297 | 266 | self.debug_aranges_section_dirty = true; |
| 298 | 267 | } |
| ... | ... | @@ -304,7 +273,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 304 | 273 | const p_align = 1; |
| 305 | 274 | const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null); |
| 306 | 275 | |
| 307 | log.debug("found dSym __debug_line free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 276 | log.debug("found __debug_line free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); | |
| 308 | 277 | |
| 309 | 278 | try dwarf_segment.addSection(allocator, "__debug_line", .{ |
| 310 | 279 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| ... | ... | @@ -312,7 +281,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 312 | 281 | .offset = @intCast(u32, off), |
| 313 | 282 | .@"align" = p_align, |
| 314 | 283 | }); |
| 315 | self.header_dirty = true; | |
| 316 | 284 | self.load_commands_dirty = true; |
| 317 | 285 | self.debug_line_header_dirty = true; |
| 318 | 286 | } |
| ... | ... | @@ -624,7 +592,6 @@ pub fn flushModule(self: *DebugSymbols, allocator: *Allocator, options: link.Opt |
| 624 | 592 | try self.writeLoadCommands(allocator); |
| 625 | 593 | try self.writeHeader(); |
| 626 | 594 | |
| 627 | assert(!self.header_dirty); | |
| 628 | 595 | assert(!self.load_commands_dirty); |
| 629 | 596 | assert(!self.string_table_dirty); |
| 630 | 597 | assert(!self.debug_abbrev_section_dirty); |
| ... | ... | @@ -716,23 +683,38 @@ fn writeLoadCommands(self: *DebugSymbols, allocator: *Allocator) !void { |
| 716 | 683 | } |
| 717 | 684 | |
| 718 | 685 | const off = @sizeOf(macho.mach_header_64); |
| 719 | log.debug("writing {} dSym load commands from 0x{x} to 0x{x}", .{ self.load_commands.items.len, off, off + sizeofcmds }); | |
| 686 | log.debug("writing {} load commands from 0x{x} to 0x{x}", .{ self.load_commands.items.len, off, off + sizeofcmds }); | |
| 720 | 687 | try self.file.pwriteAll(buffer, off); |
| 721 | 688 | self.load_commands_dirty = false; |
| 722 | 689 | } |
| 723 | 690 | |
| 724 | 691 | fn writeHeader(self: *DebugSymbols) !void { |
| 725 | if (!self.header_dirty) return; | |
| 692 | var header = emptyHeader(.{ | |
| 693 | .filetype = macho.MH_DSYM, | |
| 694 | }); | |
| 695 | ||
| 696 | switch (self.base.base.options.target.cpu.arch) { | |
| 697 | .aarch64 => { | |
| 698 | header.cputype = macho.CPU_TYPE_ARM64; | |
| 699 | header.cpusubtype = macho.CPU_SUBTYPE_ARM_ALL; | |
| 700 | }, | |
| 701 | .x86_64 => { | |
| 702 | header.cputype = macho.CPU_TYPE_X86_64; | |
| 703 | header.cpusubtype = macho.CPU_SUBTYPE_X86_64_ALL; | |
| 704 | }, | |
| 705 | else => return error.UnsupportedCpuArchitecture, | |
| 706 | } | |
| 707 | ||
| 708 | header.ncmds = @intCast(u32, self.load_commands.items.len); | |
| 709 | header.sizeofcmds = 0; | |
| 726 | 710 | |
| 727 | self.header.?.ncmds = @intCast(u32, self.load_commands.items.len); | |
| 728 | var sizeofcmds: u32 = 0; | |
| 729 | 711 | for (self.load_commands.items) |cmd| { |
| 730 | sizeofcmds += cmd.cmdsize(); | |
| 712 | header.sizeofcmds += cmd.cmdsize(); | |
| 731 | 713 | } |
| 732 | self.header.?.sizeofcmds = sizeofcmds; | |
| 733 | log.debug("writing Mach-O dSym header {}", .{self.header.?}); | |
| 734 | try self.file.pwriteAll(mem.asBytes(&self.header.?), 0); | |
| 735 | self.header_dirty = false; | |
| 714 | ||
| 715 | log.debug("writing Mach-O header {}", .{header}); | |
| 716 | ||
| 717 | try self.file.pwriteAll(mem.asBytes(&header), 0); | |
| 736 | 718 | } |
| 737 | 719 | |
| 738 | 720 | fn allocatedSizeLinkedit(self: *DebugSymbols, start: u64) u64 { |
| ... | ... | @@ -798,7 +780,7 @@ fn relocateSymbolTable(self: *DebugSymbols) !void { |
| 798 | 780 | const existing_size = symtab.nsyms * @sizeOf(macho.nlist_64); |
| 799 | 781 | |
| 800 | 782 | assert(new_symoff + existing_size <= self.linkedit_off + self.linkedit_size); // TODO expand LINKEDIT segment. |
| 801 | log.debug("relocating dSym symbol table from 0x{x}-0x{x} to 0x{x}-0x{x}", .{ | |
| 783 | log.debug("relocating symbol table from 0x{x}-0x{x} to 0x{x}-0x{x}", .{ | |
| 802 | 784 | symtab.symoff, |
| 803 | 785 | symtab.symoff + existing_size, |
| 804 | 786 | new_symoff, |
| ... | ... | @@ -820,7 +802,7 @@ pub fn writeLocalSymbol(self: *DebugSymbols, index: usize) !void { |
| 820 | 802 | try self.relocateSymbolTable(); |
| 821 | 803 | const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| 822 | 804 | const off = symtab.symoff + @sizeOf(macho.nlist_64) * index; |
| 823 | log.debug("writing dSym local symbol {} at 0x{x}", .{ index, off }); | |
| 805 | log.debug("writing local symbol {} at 0x{x}", .{ index, off }); | |
| 824 | 806 | try self.file.pwriteAll(mem.asBytes(&self.base.locals.items[index]), off); |
| 825 | 807 | } |
| 826 | 808 | |
| ... | ... | @@ -839,7 +821,7 @@ fn writeStringTable(self: *DebugSymbols) !void { |
| 839 | 821 | symtab.stroff = @intCast(u32, self.findFreeSpaceLinkedit(needed_size, 1)); |
| 840 | 822 | } |
| 841 | 823 | symtab.strsize = @intCast(u32, needed_size); |
| 842 | log.debug("writing dSym string table from 0x{x} to 0x{x}", .{ symtab.stroff, symtab.stroff + symtab.strsize }); | |
| 824 | log.debug("writing string table from 0x{x} to 0x{x}", .{ symtab.stroff, symtab.stroff + symtab.strsize }); | |
| 843 | 825 | |
| 844 | 826 | try self.file.pwriteAll(self.base.string_table.items, symtab.stroff); |
| 845 | 827 | self.load_commands_dirty = true; |
src/link/MachO/Zld.zig+15-25| ... | ... | @@ -3132,54 +3132,44 @@ fn writeLoadCommands(self: *Zld) !void { |
| 3132 | 3132 | } |
| 3133 | 3133 | |
| 3134 | 3134 | fn writeHeader(self: *Zld) !void { |
| 3135 | var header: macho.mach_header_64 = undefined; | |
| 3136 | header.magic = macho.MH_MAGIC_64; | |
| 3137 | ||
| 3138 | const CpuInfo = struct { | |
| 3139 | cpu_type: macho.cpu_type_t, | |
| 3140 | cpu_subtype: macho.cpu_subtype_t, | |
| 3141 | }; | |
| 3135 | var header = emptyHeader(.{ | |
| 3136 | .flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE | macho.MH_TWOLEVEL, | |
| 3137 | }); | |
| 3142 | 3138 | |
| 3143 | const cpu_info: CpuInfo = switch (self.target.?.cpu.arch) { | |
| 3144 | .aarch64 => .{ | |
| 3145 | .cpu_type = macho.CPU_TYPE_ARM64, | |
| 3146 | .cpu_subtype = macho.CPU_SUBTYPE_ARM_ALL, | |
| 3139 | switch (self.target.?.cpu.arch) { | |
| 3140 | .aarch64 => { | |
| 3141 | header.cputype = macho.CPU_TYPE_ARM64; | |
| 3142 | header.cpusubtype = macho.CPU_SUBTYPE_ARM_ALL; | |
| 3147 | 3143 | }, |
| 3148 | .x86_64 => .{ | |
| 3149 | .cpu_type = macho.CPU_TYPE_X86_64, | |
| 3150 | .cpu_subtype = macho.CPU_SUBTYPE_X86_64_ALL, | |
| 3144 | .x86_64 => { | |
| 3145 | header.cputype = macho.CPU_TYPE_X86_64; | |
| 3146 | header.cpusubtype = macho.CPU_SUBTYPE_X86_64_ALL; | |
| 3151 | 3147 | }, |
| 3152 | 3148 | else => return error.UnsupportedCpuArchitecture, |
| 3153 | }; | |
| 3154 | header.cputype = cpu_info.cpu_type; | |
| 3155 | header.cpusubtype = cpu_info.cpu_subtype; | |
| 3149 | } | |
| 3156 | 3150 | |
| 3157 | 3151 | switch (self.output.?.tag) { |
| 3158 | 3152 | .exe => { |
| 3159 | 3153 | header.filetype = macho.MH_EXECUTE; |
| 3160 | header.flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE | macho.MH_TWOLEVEL; | |
| 3161 | 3154 | }, |
| 3162 | 3155 | .dylib => { |
| 3163 | 3156 | header.filetype = macho.MH_DYLIB; |
| 3164 | header.flags = macho.MH_NOUNDEFS | | |
| 3165 | macho.MH_DYLDLINK | | |
| 3166 | macho.MH_PIE | | |
| 3167 | macho.MH_TWOLEVEL | | |
| 3168 | macho.MH_NO_REEXPORTED_DYLIBS; | |
| 3157 | header.flags |= macho.MH_NO_REEXPORTED_DYLIBS; | |
| 3169 | 3158 | }, |
| 3170 | 3159 | } |
| 3171 | 3160 | |
| 3172 | header.reserved = 0; | |
| 3173 | ||
| 3174 | 3161 | if (self.tlv_section_index) |_| |
| 3175 | 3162 | header.flags |= macho.MH_HAS_TLV_DESCRIPTORS; |
| 3176 | 3163 | |
| 3177 | 3164 | header.ncmds = @intCast(u32, self.load_commands.items.len); |
| 3178 | 3165 | header.sizeofcmds = 0; |
| 3166 | ||
| 3179 | 3167 | for (self.load_commands.items) |cmd| { |
| 3180 | 3168 | header.sizeofcmds += cmd.cmdsize(); |
| 3181 | 3169 | } |
| 3170 | ||
| 3182 | 3171 | log.debug("writing Mach-O header {}", .{header}); |
| 3172 | ||
| 3183 | 3173 | try self.file.?.pwriteAll(mem.asBytes(&header), 0); |
| 3184 | 3174 | } |
| 3185 | 3175 |
src/link/MachO/commands.zig+22| ... | ... | @@ -11,6 +11,28 @@ const Allocator = std.mem.Allocator; |
| 11 | 11 | const MachO = @import("../MachO.zig"); |
| 12 | 12 | const padToIdeal = MachO.padToIdeal; |
| 13 | 13 | |
| 14 | pub const HeaderArgs = struct { | |
| 15 | magic: u32 = macho.MH_MAGIC_64, | |
| 16 | cputype: macho.cpu_type_t = 0, | |
| 17 | cpusubtype: macho.cpu_subtype_t = 0, | |
| 18 | filetype: u32 = 0, | |
| 19 | flags: u32 = 0, | |
| 20 | reserved: u32 = 0, | |
| 21 | }; | |
| 22 | ||
| 23 | pub fn emptyHeader(args: HeaderArgs) macho.mach_header_64 { | |
| 24 | return .{ | |
| 25 | .magic = args.magic, | |
| 26 | .cputype = args.cputype, | |
| 27 | .cpusubtype = args.cpusubtype, | |
| 28 | .filetype = args.filetype, | |
| 29 | .ncmds = 0, | |
| 30 | .sizeofcmds = 0, | |
| 31 | .flags = args.flags, | |
| 32 | .reserved = args.reserved, | |
| 33 | }; | |
| 34 | } | |
| 35 | ||
| 14 | 36 | pub const LoadCommand = union(enum) { |
| 15 | 37 | Segment: SegmentCommand, |
| 16 | 38 | DyldInfoOnly: macho.dyld_info_command, |