authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-01 10:34:46+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-15 18:49:46+02:00
log9c3ebe0216306b5e346ec52959de41d1b4d504d9
tree23dd11a4fdb7a8fadbd2ea3b7c6837ec2baef928
parente08f7ba8896ad64a696424cff9b9c0963a7a4a0b

zld: clean up logic for creating mach header


4 files changed, 117 insertions(+), 159 deletions(-)

src/link/MachO.zig+45-81
......@@ -41,8 +41,6 @@ d_sym: ?DebugSymbols = null,
4141/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
4242page_size: u16,
4343
44/// Mach-O header
45header: ?macho.mach_header_64 = null,
4644/// We commit 0x1000 = 4096 bytes of space to the header and
4745/// the table of load commands. This should be plenty for any
4846/// potential future extensions.
......@@ -128,7 +126,6 @@ offset_table: std.ArrayListUnmanaged(GOTEntry) = .{},
128126error_flags: File.ErrorFlags = File.ErrorFlags{},
129127
130128offset_table_count_dirty: bool = false,
131header_dirty: bool = false,
132129load_commands_dirty: bool = false,
133130rebase_info_dirty: bool = false,
134131binding_info_dirty: bool = false,
......@@ -497,7 +494,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
497494 }
498495
499496 assert(!self.offset_table_count_dirty);
500 assert(!self.header_dirty);
501497 assert(!self.load_commands_dirty);
502498 assert(!self.rebase_info_dirty);
503499 assert(!self.binding_info_dirty);
......@@ -1488,54 +1484,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14881484 .Lib => return error.TODOImplementWritingLibFiles,
14891485 }
14901486
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 }
15391487 if (self.pagezero_segment_cmd_index == null) {
15401488 self.pagezero_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
15411489 try self.load_commands.append(self.base.allocator, .{
......@@ -1543,7 +1491,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
15431491 .vmsize = 0x100000000, // size always set to 4GB
15441492 }),
15451493 });
1546 self.header_dirty = true;
15471494 self.load_commands_dirty = true;
15481495 }
15491496 if (self.text_segment_cmd_index == null) {
......@@ -1567,7 +1514,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
15671514 .initprot = initprot,
15681515 }),
15691516 });
1570 self.header_dirty = true;
15711517 self.load_commands_dirty = true;
15721518 }
15731519 if (self.text_section_index == null) {
......@@ -1592,7 +1538,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
15921538 .@"align" = alignment,
15931539 .flags = flags,
15941540 });
1595 self.header_dirty = true;
15961541 self.load_commands_dirty = true;
15971542 }
15981543 if (self.stubs_section_index == null) {
......@@ -1624,7 +1569,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
16241569 .flags = flags,
16251570 .reserved2 = stub_size,
16261571 });
1627 self.header_dirty = true;
16281572 self.load_commands_dirty = true;
16291573 }
16301574 if (self.stub_helper_section_index == null) {
......@@ -1650,7 +1594,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
16501594 .@"align" = alignment,
16511595 .flags = flags,
16521596 });
1653 self.header_dirty = true;
16541597 self.load_commands_dirty = true;
16551598 }
16561599 if (self.data_const_segment_cmd_index == null) {
......@@ -1674,7 +1617,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
16741617 .initprot = initprot,
16751618 }),
16761619 });
1677 self.header_dirty = true;
16781620 self.load_commands_dirty = true;
16791621 }
16801622 if (self.got_section_index == null) {
......@@ -1695,7 +1637,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
16951637 .@"align" = 3, // 2^3 = @sizeOf(u64)
16961638 .flags = flags,
16971639 });
1698 self.header_dirty = true;
16991640 self.load_commands_dirty = true;
17001641 }
17011642 if (self.data_segment_cmd_index == null) {
......@@ -1719,7 +1660,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
17191660 .initprot = initprot,
17201661 }),
17211662 });
1722 self.header_dirty = true;
17231663 self.load_commands_dirty = true;
17241664 }
17251665 if (self.la_symbol_ptr_section_index == null) {
......@@ -1740,7 +1680,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
17401680 .@"align" = 3, // 2^3 = @sizeOf(u64)
17411681 .flags = flags,
17421682 });
1743 self.header_dirty = true;
17441683 self.load_commands_dirty = true;
17451684 }
17461685 if (self.data_section_index == null) {
......@@ -1759,7 +1698,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
17591698 .offset = @intCast(u32, off),
17601699 .@"align" = 3, // 2^3 = @sizeOf(u64)
17611700 });
1762 self.header_dirty = true;
17631701 self.load_commands_dirty = true;
17641702 }
17651703 if (self.linkedit_segment_cmd_index == null) {
......@@ -1779,7 +1717,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
17791717 .initprot = initprot,
17801718 }),
17811719 });
1782 self.header_dirty = true;
17831720 self.load_commands_dirty = true;
17841721 }
17851722 if (self.dyld_info_cmd_index == null) {
......@@ -1826,7 +1763,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
18261763 dyld.export_off = @intCast(u32, export_off);
18271764 dyld.export_size = expected_size;
18281765
1829 self.header_dirty = true;
18301766 self.load_commands_dirty = true;
18311767 }
18321768 if (self.symtab_cmd_index == null) {
......@@ -1858,7 +1794,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
18581794 symtab.stroff = @intCast(u32, strtab_off);
18591795 symtab.strsize = @intCast(u32, strtab_size);
18601796
1861 self.header_dirty = true;
18621797 self.load_commands_dirty = true;
18631798 self.string_table_dirty = true;
18641799 }
......@@ -1895,7 +1830,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
18951830 .nlocrel = 0,
18961831 },
18971832 });
1898 self.header_dirty = true;
18991833 self.load_commands_dirty = true;
19001834 }
19011835 if (self.dylinker_cmd_index == null) {
......@@ -1914,7 +1848,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19141848 mem.set(u8, dylinker_cmd.data, 0);
19151849 mem.copy(u8, dylinker_cmd.data, mem.spanZ(DEFAULT_DYLD_PATH));
19161850 try self.load_commands.append(self.base.allocator, .{ .Dylinker = dylinker_cmd });
1917 self.header_dirty = true;
19181851 self.load_commands_dirty = true;
19191852 }
19201853 if (self.libsystem_cmd_index == null) {
......@@ -1925,7 +1858,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19251858
19261859 try self.load_commands.append(self.base.allocator, .{ .Dylib = dylib_cmd });
19271860
1928 self.header_dirty = true;
19291861 self.load_commands_dirty = true;
19301862 }
19311863 if (self.main_cmd_index == null) {
......@@ -1938,7 +1870,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19381870 .stacksize = 0,
19391871 },
19401872 });
1941 self.header_dirty = true;
19421873 self.load_commands_dirty = true;
19431874 }
19441875 if (self.version_min_cmd_index == null) {
......@@ -1960,7 +1891,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19601891 .sdk = version,
19611892 },
19621893 });
1963 self.header_dirty = true;
19641894 self.load_commands_dirty = true;
19651895 }
19661896 if (self.source_version_cmd_index == null) {
......@@ -1972,7 +1902,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19721902 .version = 0x0,
19731903 },
19741904 });
1975 self.header_dirty = true;
19761905 self.load_commands_dirty = true;
19771906 }
19781907 if (self.uuid_cmd_index == null) {
......@@ -1984,7 +1913,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19841913 };
19851914 std.crypto.random.bytes(&uuid_cmd.uuid);
19861915 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });
1987 self.header_dirty = true;
19881916 self.load_commands_dirty = true;
19891917 }
19901918 if (self.code_signature_cmd_index == null) {
......@@ -1997,7 +1925,6 @@ pub fn populateMissingMetadata(self: *MachO) !void {
19971925 .datasize = 0,
19981926 },
19991927 });
2000 self.header_dirty = true;
20011928 self.load_commands_dirty = true;
20021929 }
20031930 if (!self.nonlazy_imports.contains("dyld_stub_binder")) {
......@@ -3224,24 +3151,57 @@ fn writeLoadCommands(self: *MachO) !void {
32243151 }
32253152
32263153 const off = @sizeOf(macho.mach_header_64);
3154
32273155 log.debug("writing {} load commands from 0x{x} to 0x{x}", .{ self.load_commands.items.len, off, off + sizeofcmds });
3156
32283157 try self.base.file.?.pwriteAll(buffer, off);
32293158 self.load_commands_dirty = false;
32303159}
32313160
32323161/// Writes Mach-O file header.
32333162fn 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;
32353197
3236 self.header.?.ncmds = @intCast(u32, self.load_commands.items.len);
3237 var sizeofcmds: u32 = 0;
32383198 for (self.load_commands.items) |cmd| {
3239 sizeofcmds += cmd.cmdsize();
3199 header.sizeofcmds += cmd.cmdsize();
32403200 }
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);
32453205}
32463206
32473207pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
......@@ -3249,3 +3209,7 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
32493209 return std.math.add(@TypeOf(actual_size), actual_size, actual_size / ideal_factor) catch
32503210 std.math.maxInt(@TypeOf(actual_size));
32513211}
3212
3213fn hasTlvDescriptors(_: *MachO) bool {
3214 return false;
3215}
src/link/MachO/DebugSymbols.zig+35-53
......@@ -3,7 +3,7 @@ const DebugSymbols = @This();
33const std = @import("std");
44const assert = std.debug.assert;
55const fs = std.fs;
6const log = std.log.scoped(.link);
6const log = std.log.scoped(.dsym);
77const macho = std.macho;
88const mem = std.mem;
99const DW = std.dwarf;
......@@ -27,9 +27,6 @@ const page_size: u16 = 0x1000;
2727base: *MachO,
2828file: fs.File,
2929
30/// Mach header
31header: ?macho.mach_header_64 = null,
32
3330/// Table of all load commands
3431load_commands: std.ArrayListUnmanaged(LoadCommand) = .{},
3532/// __PAGEZERO segment
......@@ -78,7 +75,6 @@ dbg_info_decl_last: ?*TextBlock = null,
7875/// Table of debug symbol names aka the debug string table.
7976debug_string_table: std.ArrayListUnmanaged(u8) = .{},
8077
81header_dirty: bool = false,
8278load_commands_dirty: bool = false,
8379string_table_dirty: bool = false,
8480debug_string_table_dirty: bool = false,
......@@ -106,26 +102,10 @@ const min_nop_size = 2;
106102/// You must call this function *after* `MachO.populateMissingMetadata()`
107103/// has been called to get a viable debug symbols output.
108104pub 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 }
124105 if (self.uuid_cmd_index == null) {
125106 const base_cmd = self.base.load_commands.items[self.base.uuid_cmd_index.?];
126107 self.uuid_cmd_index = @intCast(u16, self.load_commands.items.len);
127108 try self.load_commands.append(allocator, base_cmd);
128 self.header_dirty = true;
129109 self.load_commands_dirty = true;
130110 }
131111 if (self.symtab_cmd_index == null) {
......@@ -134,11 +114,11 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
134114 const symtab_size = base_cmd.nsyms * @sizeOf(macho.nlist_64);
135115 const symtab_off = self.findFreeSpaceLinkedit(symtab_size, @sizeOf(macho.nlist_64));
136116
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 });
138118
139119 const strtab_off = self.findFreeSpaceLinkedit(base_cmd.strsize, 1);
140120
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 });
142122
143123 try self.load_commands.append(allocator, .{
144124 .Symtab = .{
......@@ -150,7 +130,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
150130 .strsize = base_cmd.strsize,
151131 },
152132 });
153 self.header_dirty = true;
154133 self.load_commands_dirty = true;
155134 self.string_table_dirty = true;
156135 }
......@@ -159,7 +138,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
159138 const base_cmd = self.base.load_commands.items[self.base.pagezero_segment_cmd_index.?].Segment;
160139 const cmd = try self.copySegmentCommand(allocator, base_cmd);
161140 try self.load_commands.append(allocator, .{ .Segment = cmd });
162 self.header_dirty = true;
163141 self.load_commands_dirty = true;
164142 }
165143 if (self.text_segment_cmd_index == null) {
......@@ -167,7 +145,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
167145 const base_cmd = self.base.load_commands.items[self.base.text_segment_cmd_index.?].Segment;
168146 const cmd = try self.copySegmentCommand(allocator, base_cmd);
169147 try self.load_commands.append(allocator, .{ .Segment = cmd });
170 self.header_dirty = true;
171148 self.load_commands_dirty = true;
172149 }
173150 if (self.data_const_segment_cmd_index == null) outer: {
......@@ -176,7 +153,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
176153 const base_cmd = self.base.load_commands.items[self.base.data_const_segment_cmd_index.?].Segment;
177154 const cmd = try self.copySegmentCommand(allocator, base_cmd);
178155 try self.load_commands.append(allocator, .{ .Segment = cmd });
179 self.header_dirty = true;
180156 self.load_commands_dirty = true;
181157 }
182158 if (self.data_segment_cmd_index == null) outer: {
......@@ -185,7 +161,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
185161 const base_cmd = self.base.load_commands.items[self.base.data_segment_cmd_index.?].Segment;
186162 const cmd = try self.copySegmentCommand(allocator, base_cmd);
187163 try self.load_commands.append(allocator, .{ .Segment = cmd });
188 self.header_dirty = true;
189164 self.load_commands_dirty = true;
190165 }
191166 if (self.linkedit_segment_cmd_index == null) {
......@@ -196,7 +171,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
196171 cmd.inner.fileoff = self.linkedit_off;
197172 cmd.inner.filesize = self.linkedit_size;
198173 try self.load_commands.append(allocator, .{ .Segment = cmd });
199 self.header_dirty = true;
200174 self.load_commands_dirty = true;
201175 }
202176 if (self.dwarf_segment_cmd_index == null) {
......@@ -208,7 +182,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
208182 const off = linkedit.inner.fileoff + linkedit.inner.filesize;
209183 const vmaddr = linkedit.inner.vmaddr + linkedit.inner.vmsize;
210184
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 });
212186
213187 try self.load_commands.append(allocator, .{
214188 .Segment = SegmentCommand.empty("__DWARF", .{
......@@ -218,7 +192,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
218192 .filesize = needed_size,
219193 }),
220194 });
221 self.header_dirty = true;
222195 self.load_commands_dirty = true;
223196 }
224197 if (self.debug_str_section_index == null) {
......@@ -232,7 +205,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
232205 .offset = @intCast(u32, dwarf_segment.inner.fileoff),
233206 .@"align" = 1,
234207 });
235 self.header_dirty = true;
236208 self.load_commands_dirty = true;
237209 self.debug_string_table_dirty = true;
238210 }
......@@ -244,7 +216,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
244216 const p_align = 1;
245217 const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null);
246218
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 });
248220
249221 try dwarf_segment.addSection(allocator, "__debug_info", .{
250222 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
......@@ -252,7 +224,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
252224 .offset = @intCast(u32, off),
253225 .@"align" = p_align,
254226 });
255 self.header_dirty = true;
256227 self.load_commands_dirty = true;
257228 self.debug_info_header_dirty = true;
258229 }
......@@ -264,7 +235,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
264235 const p_align = 1;
265236 const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null);
266237
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 });
268239
269240 try dwarf_segment.addSection(allocator, "__debug_abbrev", .{
270241 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
......@@ -272,7 +243,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
272243 .offset = @intCast(u32, off),
273244 .@"align" = p_align,
274245 });
275 self.header_dirty = true;
276246 self.load_commands_dirty = true;
277247 self.debug_abbrev_section_dirty = true;
278248 }
......@@ -284,7 +254,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
284254 const p_align = 16;
285255 const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null);
286256
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 });
288258
289259 try dwarf_segment.addSection(allocator, "__debug_aranges", .{
290260 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
......@@ -292,7 +262,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
292262 .offset = @intCast(u32, off),
293263 .@"align" = p_align,
294264 });
295 self.header_dirty = true;
296265 self.load_commands_dirty = true;
297266 self.debug_aranges_section_dirty = true;
298267 }
......@@ -304,7 +273,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
304273 const p_align = 1;
305274 const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null);
306275
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 });
308277
309278 try dwarf_segment.addSection(allocator, "__debug_line", .{
310279 .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff,
......@@ -312,7 +281,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
312281 .offset = @intCast(u32, off),
313282 .@"align" = p_align,
314283 });
315 self.header_dirty = true;
316284 self.load_commands_dirty = true;
317285 self.debug_line_header_dirty = true;
318286 }
......@@ -624,7 +592,6 @@ pub fn flushModule(self: *DebugSymbols, allocator: *Allocator, options: link.Opt
624592 try self.writeLoadCommands(allocator);
625593 try self.writeHeader();
626594
627 assert(!self.header_dirty);
628595 assert(!self.load_commands_dirty);
629596 assert(!self.string_table_dirty);
630597 assert(!self.debug_abbrev_section_dirty);
......@@ -716,23 +683,38 @@ fn writeLoadCommands(self: *DebugSymbols, allocator: *Allocator) !void {
716683 }
717684
718685 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 });
720687 try self.file.pwriteAll(buffer, off);
721688 self.load_commands_dirty = false;
722689}
723690
724691fn 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;
726710
727 self.header.?.ncmds = @intCast(u32, self.load_commands.items.len);
728 var sizeofcmds: u32 = 0;
729711 for (self.load_commands.items) |cmd| {
730 sizeofcmds += cmd.cmdsize();
712 header.sizeofcmds += cmd.cmdsize();
731713 }
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);
736718}
737719
738720fn allocatedSizeLinkedit(self: *DebugSymbols, start: u64) u64 {
......@@ -798,7 +780,7 @@ fn relocateSymbolTable(self: *DebugSymbols) !void {
798780 const existing_size = symtab.nsyms * @sizeOf(macho.nlist_64);
799781
800782 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}", .{
802784 symtab.symoff,
803785 symtab.symoff + existing_size,
804786 new_symoff,
......@@ -820,7 +802,7 @@ pub fn writeLocalSymbol(self: *DebugSymbols, index: usize) !void {
820802 try self.relocateSymbolTable();
821803 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab;
822804 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 });
824806 try self.file.pwriteAll(mem.asBytes(&self.base.locals.items[index]), off);
825807}
826808
......@@ -839,7 +821,7 @@ fn writeStringTable(self: *DebugSymbols) !void {
839821 symtab.stroff = @intCast(u32, self.findFreeSpaceLinkedit(needed_size, 1));
840822 }
841823 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 });
843825
844826 try self.file.pwriteAll(self.base.string_table.items, symtab.stroff);
845827 self.load_commands_dirty = true;
src/link/MachO/Zld.zig+15-25
......@@ -3132,54 +3132,44 @@ fn writeLoadCommands(self: *Zld) !void {
31323132}
31333133
31343134fn 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 });
31423138
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;
31473143 },
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;
31513147 },
31523148 else => return error.UnsupportedCpuArchitecture,
3153 };
3154 header.cputype = cpu_info.cpu_type;
3155 header.cpusubtype = cpu_info.cpu_subtype;
3149 }
31563150
31573151 switch (self.output.?.tag) {
31583152 .exe => {
31593153 header.filetype = macho.MH_EXECUTE;
3160 header.flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE | macho.MH_TWOLEVEL;
31613154 },
31623155 .dylib => {
31633156 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;
31693158 },
31703159 }
31713160
3172 header.reserved = 0;
3173
31743161 if (self.tlv_section_index) |_|
31753162 header.flags |= macho.MH_HAS_TLV_DESCRIPTORS;
31763163
31773164 header.ncmds = @intCast(u32, self.load_commands.items.len);
31783165 header.sizeofcmds = 0;
3166
31793167 for (self.load_commands.items) |cmd| {
31803168 header.sizeofcmds += cmd.cmdsize();
31813169 }
3170
31823171 log.debug("writing Mach-O header {}", .{header});
3172
31833173 try self.file.?.pwriteAll(mem.asBytes(&header), 0);
31843174}
31853175
src/link/MachO/commands.zig+22
......@@ -11,6 +11,28 @@ const Allocator = std.mem.Allocator;
1111const MachO = @import("../MachO.zig");
1212const padToIdeal = MachO.padToIdeal;
1313
14pub 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
23pub 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
1436pub const LoadCommand = union(enum) {
1537 Segment: SegmentCommand,
1638 DyldInfoOnly: macho.dyld_info_command,