authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-30 23:57:36+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-01 10:49:31+01:00
log1f7fb560ab5a3a82af24ab7abe11dce8f877c7fe
tree876dfc3329b12c8619a8e4412cbab9fdabb7abef
parentde66b65677f8207b2bc10997031b90c06b738dc8

lld: use commands.LoadCommand in self-hosted linker


4 files changed, 133 insertions(+), 218 deletions(-)

src/codegen.zig+4-2
......@@ -1790,7 +1790,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
17901790 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
17911791 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
17921792 const func = func_val.func;
1793 const got = &macho_file.sections.items[macho_file.got_section_index.?];
1793 const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
1794 const got = &text_segment.sections.items[macho_file.got_section_index.?];
17941795 const got_addr = got.addr + func.owner_decl.link.macho.offset_table_index * @sizeOf(u64);
17951796 switch (arch) {
17961797 .x86_64 => {
......@@ -3191,7 +3192,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
31913192 return MCValue{ .memory = got_addr };
31923193 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
31933194 const decl = payload.decl;
3194 const got = &macho_file.sections.items[macho_file.got_section_index.?];
3195 const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
3196 const got = &text_segment.sections.items[macho_file.got_section_index.?];
31953197 const got_addr = got.addr + decl.link.macho.offset_table_index * ptr_bytes;
31963198 return MCValue{ .memory = got_addr };
31973199 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
src/link/MachO.zig+111-205
......@@ -25,55 +25,9 @@ const Trie = @import("MachO/Trie.zig");
2525const CodeSignature = @import("MachO/CodeSignature.zig");
2626const Parser = @import("MachO/Parser.zig");
2727
28pub const base_tag: File.Tag = File.Tag.macho;
29
30const LoadCommand = union(enum) {
31 Segment: macho.segment_command_64,
32 LinkeditData: macho.linkedit_data_command,
33 Symtab: macho.symtab_command,
34 Dysymtab: macho.dysymtab_command,
35 DyldInfo: macho.dyld_info_command,
36 Dylinker: macho.dylinker_command,
37 Dylib: macho.dylib_command,
38 EntryPoint: macho.entry_point_command,
39 MinVersion: macho.version_min_command,
40 SourceVersion: macho.source_version_command,
41
42 pub fn cmdsize(self: LoadCommand) u32 {
43 return switch (self) {
44 .Segment => |x| x.cmdsize,
45 .LinkeditData => |x| x.cmdsize,
46 .Symtab => |x| x.cmdsize,
47 .Dysymtab => |x| x.cmdsize,
48 .DyldInfo => |x| x.cmdsize,
49 .Dylinker => |x| x.cmdsize,
50 .Dylib => |x| x.cmdsize,
51 .EntryPoint => |x| x.cmdsize,
52 .MinVersion => |x| x.cmdsize,
53 .SourceVersion => |x| x.cmdsize,
54 };
55 }
56
57 pub fn write(self: LoadCommand, file: *fs.File, offset: u64) !void {
58 return switch (self) {
59 .Segment => |cmd| writeGeneric(cmd, file, offset),
60 .LinkeditData => |cmd| writeGeneric(cmd, file, offset),
61 .Symtab => |cmd| writeGeneric(cmd, file, offset),
62 .Dysymtab => |cmd| writeGeneric(cmd, file, offset),
63 .DyldInfo => |cmd| writeGeneric(cmd, file, offset),
64 .Dylinker => |cmd| writeGeneric(cmd, file, offset),
65 .Dylib => |cmd| writeGeneric(cmd, file, offset),
66 .EntryPoint => |cmd| writeGeneric(cmd, file, offset),
67 .MinVersion => |cmd| writeGeneric(cmd, file, offset),
68 .SourceVersion => |cmd| writeGeneric(cmd, file, offset),
69 };
70 }
28usingnamespace @import("MachO/commands.zig");
7129
72 fn writeGeneric(cmd: anytype, file: *fs.File, offset: u64) !void {
73 const slice = [1]@TypeOf(cmd){cmd};
74 return file.pwriteAll(mem.sliceAsBytes(slice[0..1]), offset);
75 }
76};
30pub const base_tag: File.Tag = File.Tag.macho;
7731
7832base: File,
7933
......@@ -116,18 +70,14 @@ source_version_cmd_index: ?u16 = null,
11670/// Code signature
11771code_signature_cmd_index: ?u16 = null,
11872
119/// Table of all sections
120sections: std.ArrayListUnmanaged(macho.section_64) = .{},
121
122/// __TEXT,__text section
73/// Index into __TEXT,__text section.
12374text_section_index: ?u16 = null,
124
125/// __DATA,__got section
75/// Index into __TEXT,__got section.
12676got_section_index: ?u16 = null,
127
77/// The absolute address of the entry point.
12878entry_addr: ?u64 = null,
12979
130// TODO move this into each Segment aggregator
80/// TODO move this into each Segment aggregator
13181linkedit_segment_next_offset: ?u32 = null,
13282
13383/// Table of all local symbols
......@@ -356,40 +306,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
356306 if (self.entry_addr) |addr| {
357307 // Update LC_MAIN with entry offset.
358308 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
359 const main_cmd = &self.load_commands.items[self.main_cmd_index.?].EntryPoint;
360 main_cmd.entryoff = addr - text_segment.vmaddr;
361 }
362 if (self.dylinker_cmd_dirty) {
363 // Write path to dyld loader.
364 var off: usize = @sizeOf(macho.mach_header_64);
365 for (self.load_commands.items) |cmd| {
366 if (cmd == .Dylinker) break;
367 off += cmd.cmdsize();
368 }
369 const cmd = &self.load_commands.items[self.dylinker_cmd_index.?].Dylinker;
370 off += cmd.name;
371 log.debug("writing LC_LOAD_DYLINKER path to dyld at 0x{x}\n", .{off});
372 try self.base.file.?.pwriteAll(mem.spanZ(DEFAULT_DYLD_PATH), off);
373 self.dylinker_cmd_dirty = false;
309 const main_cmd = &self.load_commands.items[self.main_cmd_index.?].Main;
310 main_cmd.entryoff = addr - text_segment.inner.vmaddr;
374311 }
375 if (self.libsystem_cmd_dirty) {
376 // Write path to libSystem.
377 var off: usize = @sizeOf(macho.mach_header_64);
378 for (self.load_commands.items) |cmd| {
379 if (cmd == .Dylib) break;
380 off += cmd.cmdsize();
381 }
382 const cmd = &self.load_commands.items[self.libsystem_cmd_index.?].Dylib;
383 off += cmd.dylib.name;
384 log.debug("writing LC_LOAD_DYLIB path to libSystem at 0x{x}\n", .{off});
385 try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), off);
386 self.libsystem_cmd_dirty = false;
387 }
388
389312 try self.writeExportTrie();
390313 try self.writeSymbolTable();
391314 try self.writeStringTable();
392
393315 // Preallocate space for the code signature.
394316 // We need to do this at this stage so that we have the load commands with proper values
395317 // written out to the file.
......@@ -402,7 +324,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
402324 }
403325
404326 if (self.cmd_table_dirty) {
405 try self.writeCmdHeaders();
327 try self.writeLoadCommands();
406328 try self.writeMachOHeader();
407329 self.cmd_table_dirty = false;
408330 }
......@@ -416,8 +338,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
416338 }
417339
418340 assert(!self.cmd_table_dirty);
419 assert(!self.dylinker_cmd_dirty);
420 assert(!self.libsystem_cmd_dirty);
421341
422342 switch (self.base.options.output_mode) {
423343 .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
......@@ -921,7 +841,9 @@ pub fn deinit(self: *MachO) void {
921841 self.global_symbol_free_list.deinit(self.base.allocator);
922842 self.local_symbols.deinit(self.base.allocator);
923843 self.local_symbol_free_list.deinit(self.base.allocator);
924 self.sections.deinit(self.base.allocator);
844 for (self.load_commands.items) |*lc| {
845 lc.deinit(self.base.allocator);
846 }
925847 self.load_commands.deinit(self.base.allocator);
926848}
927849
......@@ -1075,7 +997,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
1075997 }
1076998
1077999 // Perform PIE fixups (if any)
1078 const got_section = self.sections.items[self.got_section_index.?];
1000 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1001 const got_section = text_segment.sections.items[self.got_section_index.?];
10791002 while (self.pie_fixups.popOrNull()) |fixup| {
10801003 const target_addr = fixup.address;
10811004 const this_addr = symbol.n_value + fixup.start;
......@@ -1094,7 +1017,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
10941017 }
10951018 }
10961019
1097 const text_section = self.sections.items[self.text_section_index.?];
1020 const text_section = text_segment.sections.items[self.text_section_index.?];
10981021 const section_offset = symbol.n_value - text_section.addr;
10991022 const file_offset = text_section.offset + section_offset;
11001023 try self.base.file.?.pwriteAll(code, file_offset);
......@@ -1212,7 +1135,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12121135 if (self.pagezero_segment_cmd_index == null) {
12131136 self.pagezero_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
12141137 try self.load_commands.append(self.base.allocator, .{
1215 .Segment = .{
1138 .Segment = SegmentCommand.empty(.{
12161139 .cmd = macho.LC_SEGMENT_64,
12171140 .cmdsize = @sizeOf(macho.segment_command_64),
12181141 .segname = makeStaticString("__PAGEZERO"),
......@@ -1224,7 +1147,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12241147 .initprot = 0,
12251148 .nsects = 0,
12261149 .flags = 0,
1227 },
1150 }),
12281151 });
12291152 self.cmd_table_dirty = true;
12301153 }
......@@ -1233,7 +1156,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12331156 const maxprot = macho.VM_PROT_READ | macho.VM_PROT_WRITE | macho.VM_PROT_EXECUTE;
12341157 const initprot = macho.VM_PROT_READ | macho.VM_PROT_EXECUTE;
12351158 try self.load_commands.append(self.base.allocator, .{
1236 .Segment = .{
1159 .Segment = SegmentCommand.empty(.{
12371160 .cmd = macho.LC_SEGMENT_64,
12381161 .cmdsize = @sizeOf(macho.segment_command_64),
12391162 .segname = makeStaticString("__TEXT"),
......@@ -1245,45 +1168,45 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12451168 .initprot = initprot,
12461169 .nsects = 0,
12471170 .flags = 0,
1248 },
1171 }),
12491172 });
12501173 self.cmd_table_dirty = true;
12511174 }
12521175 if (self.text_section_index == null) {
1253 self.text_section_index = @intCast(u16, self.sections.items.len);
12541176 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1177 self.text_section_index = @intCast(u16, text_segment.sections.items.len);
12551178
12561179 const program_code_size_hint = self.base.options.program_code_size_hint;
12571180 const file_size = mem.alignForwardGeneric(u64, program_code_size_hint, self.page_size);
12581181 const off = @intCast(u32, self.findFreeSpace(file_size, self.page_size)); // TODO maybe findFreeSpace should return u32 directly?
1259 const flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS;
12601182
12611183 log.debug("found __text section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
12621184
1263 try self.sections.append(self.base.allocator, .{
1185 try text_segment.sections.append(self.base.allocator, .{
12641186 .sectname = makeStaticString("__text"),
12651187 .segname = makeStaticString("__TEXT"),
1266 .addr = text_segment.vmaddr + off,
1188 .addr = text_segment.inner.vmaddr + off,
12671189 .size = file_size,
12681190 .offset = off,
12691191 .@"align" = if (self.base.options.target.cpu.arch == .aarch64) 2 else 0, // 2^2 for aarch64, 2^0 for x86_64
12701192 .reloff = 0,
12711193 .nreloc = 0,
1272 .flags = flags,
1194 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
12731195 .reserved1 = 0,
12741196 .reserved2 = 0,
12751197 .reserved3 = 0,
12761198 });
12771199
1278 text_segment.vmsize = file_size + off; // We add off here since __TEXT segment includes everything prior to __text section.
1279 text_segment.filesize = file_size + off;
1280 text_segment.cmdsize += @sizeOf(macho.section_64);
1281 text_segment.nsects += 1;
1200 text_segment.inner.vmsize = file_size + off; // We add off here since __TEXT segment includes everything prior to __text section.
1201 text_segment.inner.filesize = file_size + off;
1202 text_segment.inner.cmdsize += @sizeOf(macho.section_64);
1203 text_segment.inner.nsects += 1;
12821204 self.cmd_table_dirty = true;
12831205 }
12841206 if (self.got_section_index == null) {
1285 self.got_section_index = @intCast(u16, self.sections.items.len);
1286 const text_section = &self.sections.items[self.text_section_index.?];
1207 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1208 const text_section = &text_segment.sections.items[self.text_section_index.?];
1209 self.got_section_index = @intCast(u16, text_segment.sections.items.len);
12871210
12881211 const file_size = @sizeOf(u64) * self.base.options.symbol_count_hint;
12891212 // TODO looking for free space should be done *within* a segment it belongs to
......@@ -1291,7 +1214,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12911214
12921215 log.debug("found __got section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
12931216
1294 try self.sections.append(self.base.allocator, .{
1217 try text_segment.sections.append(self.base.allocator, .{
12951218 .sectname = makeStaticString("__got"),
12961219 .segname = makeStaticString("__TEXT"),
12971220 .addr = text_section.addr + text_section.size,
......@@ -1300,18 +1223,17 @@ pub fn populateMissingMetadata(self: *MachO) !void {
13001223 .@"align" = if (self.base.options.target.cpu.arch == .aarch64) 2 else 0,
13011224 .reloff = 0,
13021225 .nreloc = 0,
1303 .flags = macho.S_REGULAR,
1226 .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
13041227 .reserved1 = 0,
13051228 .reserved2 = 0,
13061229 .reserved3 = 0,
13071230 });
13081231
1309 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
13101232 const added_size = mem.alignForwardGeneric(u64, file_size, self.page_size);
1311 text_segment.vmsize += added_size;
1312 text_segment.filesize += added_size;
1313 text_segment.cmdsize += @sizeOf(macho.section_64);
1314 text_segment.nsects += 1;
1233 text_segment.inner.vmsize += added_size;
1234 text_segment.inner.filesize += added_size;
1235 text_segment.inner.cmdsize += @sizeOf(macho.section_64);
1236 text_segment.inner.nsects += 1;
13151237 self.cmd_table_dirty = true;
13161238 }
13171239 if (self.linkedit_segment_cmd_index == null) {
......@@ -1319,13 +1241,13 @@ pub fn populateMissingMetadata(self: *MachO) !void {
13191241 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
13201242 const maxprot = macho.VM_PROT_READ | macho.VM_PROT_WRITE | macho.VM_PROT_EXECUTE;
13211243 const initprot = macho.VM_PROT_READ;
1322 const off = text_segment.fileoff + text_segment.filesize;
1244 const off = text_segment.inner.fileoff + text_segment.inner.filesize;
13231245 try self.load_commands.append(self.base.allocator, .{
1324 .Segment = .{
1246 .Segment = SegmentCommand.empty(.{
13251247 .cmd = macho.LC_SEGMENT_64,
13261248 .cmdsize = @sizeOf(macho.segment_command_64),
13271249 .segname = makeStaticString("__LINKEDIT"),
1328 .vmaddr = text_segment.vmaddr + text_segment.vmsize,
1250 .vmaddr = text_segment.inner.vmaddr + text_segment.inner.vmsize,
13291251 .vmsize = 0,
13301252 .fileoff = off,
13311253 .filesize = 0,
......@@ -1333,7 +1255,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
13331255 .initprot = initprot,
13341256 .nsects = 0,
13351257 .flags = 0,
1336 },
1258 }),
13371259 });
13381260 self.linkedit_segment_next_offset = @intCast(u32, off);
13391261 self.cmd_table_dirty = true;
......@@ -1341,7 +1263,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
13411263 if (self.dyld_info_cmd_index == null) {
13421264 self.dyld_info_cmd_index = @intCast(u16, self.load_commands.items.len);
13431265 try self.load_commands.append(self.base.allocator, .{
1344 .DyldInfo = .{
1266 .DyldInfoOnly = .{
13451267 .cmd = macho.LC_DYLD_INFO_ONLY,
13461268 .cmdsize = @sizeOf(macho.dyld_info_command),
13471269 .rebase_off = 0,
......@@ -1403,15 +1325,16 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14031325 if (self.dylinker_cmd_index == null) {
14041326 self.dylinker_cmd_index = @intCast(u16, self.load_commands.items.len);
14051327 const cmdsize = mem.alignForwardGeneric(u64, @sizeOf(macho.dylinker_command) + mem.lenZ(DEFAULT_DYLD_PATH), @sizeOf(u64));
1406 try self.load_commands.append(self.base.allocator, .{
1407 .Dylinker = .{
1408 .cmd = macho.LC_LOAD_DYLINKER,
1409 .cmdsize = @intCast(u32, cmdsize),
1410 .name = @sizeOf(macho.dylinker_command),
1411 },
1328 var dylinker_cmd = emptyGenericCommandWithData(macho.dylinker_command{
1329 .cmd = macho.LC_LOAD_DYLINKER,
1330 .cmdsize = @intCast(u32, cmdsize),
1331 .name = @sizeOf(macho.dylinker_command),
14121332 });
1333 dylinker_cmd.data = try self.base.allocator.alloc(u8, cmdsize - dylinker_cmd.inner.name);
1334 mem.set(u8, dylinker_cmd.data, 0);
1335 mem.copy(u8, dylinker_cmd.data, mem.spanZ(DEFAULT_DYLD_PATH));
1336 try self.load_commands.append(self.base.allocator, .{ .Dylinker = dylinker_cmd });
14131337 self.cmd_table_dirty = true;
1414 self.dylinker_cmd_dirty = true;
14151338 }
14161339 if (self.libsystem_cmd_index == null) {
14171340 self.libsystem_cmd_index = @intCast(u16, self.load_commands.items.len);
......@@ -1419,26 +1342,26 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14191342 // TODO Find a way to work out runtime version from the OS version triple stored in std.Target.
14201343 // In the meantime, we're gonna hardcode to the minimum compatibility version of 0.0.0.
14211344 const min_version = 0x0;
1422 const dylib = .{
1423 .name = @sizeOf(macho.dylib_command),
1424 .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files
1425 .current_version = min_version,
1426 .compatibility_version = min_version,
1427 };
1428 try self.load_commands.append(self.base.allocator, .{
1429 .Dylib = .{
1430 .cmd = macho.LC_LOAD_DYLIB,
1431 .cmdsize = @intCast(u32, cmdsize),
1432 .dylib = dylib,
1345 var dylib_cmd = emptyGenericCommandWithData(macho.dylib_command{
1346 .cmd = macho.LC_LOAD_DYLIB,
1347 .cmdsize = @intCast(u32, cmdsize),
1348 .dylib = .{
1349 .name = @sizeOf(macho.dylib_command),
1350 .timestamp = 2, // not sure why not simply 0; this is reverse engineered from Mach-O files
1351 .current_version = min_version,
1352 .compatibility_version = min_version,
14331353 },
14341354 });
1355 dylib_cmd.data = try self.base.allocator.alloc(u8, cmdsize - dylib_cmd.inner.dylib.name);
1356 mem.set(u8, dylib_cmd.data, 0);
1357 mem.copy(u8, dylib_cmd.data, mem.spanZ(LIB_SYSTEM_PATH));
1358 try self.load_commands.append(self.base.allocator, .{ .Dylib = dylib_cmd });
14351359 self.cmd_table_dirty = true;
1436 self.libsystem_cmd_dirty = true;
14371360 }
14381361 if (self.main_cmd_index == null) {
14391362 self.main_cmd_index = @intCast(u16, self.load_commands.items.len);
14401363 try self.load_commands.append(self.base.allocator, .{
1441 .EntryPoint = .{
1364 .Main = .{
14421365 .cmd = macho.LC_MAIN,
14431366 .cmdsize = @sizeOf(macho.entry_point_command),
14441367 .entryoff = 0x0,
......@@ -1459,7 +1382,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14591382 const ver = self.base.options.target.os.version_range.semver.min;
14601383 const version = ver.major << 16 | ver.minor << 8 | ver.patch;
14611384 try self.load_commands.append(self.base.allocator, .{
1462 .MinVersion = .{
1385 .VersionMin = .{
14631386 .cmd = cmd,
14641387 .cmdsize = @sizeOf(macho.version_min_command),
14651388 .version = version,
......@@ -1502,7 +1425,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {
15021425}
15031426
15041427fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
1505 const text_section = &self.sections.items[self.text_section_index.?];
1428 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1429 const text_section = &text_segment.sections.items[self.text_section_index.?];
15061430 const new_block_ideal_capacity = new_block_size * alloc_num / alloc_den;
15071431
15081432 // We use these to indicate our intention to update metadata, placing the new block,
......@@ -1644,15 +1568,18 @@ fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 {
16441568 return test_end;
16451569 }
16461570 }
1647 for (self.sections.items) |section| {
1648 const increased_size = satMul(section.size, alloc_num) / alloc_den;
1649 const test_end = section.offset + increased_size;
1650 if (end > section.offset and start < test_end) {
1651 return test_end;
1571 if (self.text_segment_cmd_index) |text_index| {
1572 const text_segment = self.load_commands.items[text_index].Segment;
1573 for (text_segment.sections.items) |section| {
1574 const increased_size = satMul(section.size, alloc_num) / alloc_den;
1575 const test_end = section.offset + increased_size;
1576 if (end > section.offset and start < test_end) {
1577 return test_end;
1578 }
16521579 }
16531580 }
16541581 if (self.dyld_info_cmd_index) |dyld_info_index| {
1655 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfo;
1582 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfoOnly;
16561583 const tight_size = dyld_info.export_size;
16571584 const increased_size = satMul(tight_size, alloc_num) / alloc_den;
16581585 const test_end = dyld_info.export_off + increased_size;
......@@ -1689,12 +1616,15 @@ fn allocatedSize(self: *MachO, start: u64) u64 {
16891616 const off = @sizeOf(macho.mach_header_64);
16901617 if (off > start and off < min_pos) min_pos = off;
16911618 }
1692 for (self.sections.items) |section| {
1693 if (section.offset <= start) continue;
1694 if (section.offset < min_pos) min_pos = section.offset;
1619 if (self.text_segment_cmd_index) |text_index| {
1620 const text_segment = self.load_commands.items[text_index].Segment;
1621 for (text_segment.sections.items) |section| {
1622 if (section.offset <= start) continue;
1623 if (section.offset < min_pos) min_pos = section.offset;
1624 }
16951625 }
16961626 if (self.dyld_info_cmd_index) |dyld_info_index| {
1697 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfo;
1627 const dyld_info = self.load_commands.items[dyld_info_index].DyldInfoOnly;
16981628 if (dyld_info.export_off > start and dyld_info.export_off < min_pos) min_pos = dyld_info.export_off;
16991629 }
17001630 if (self.symtab_cmd_index) |symtab_index| {
......@@ -1714,7 +1644,8 @@ fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u16) u64 {
17141644}
17151645
17161646fn writeOffsetTableEntry(self: *MachO, index: usize) !void {
1717 const sect = &self.sections.items[self.got_section_index.?];
1647 const text_semgent = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1648 const sect = &text_semgent.sections.items[self.got_section_index.?];
17181649 const off = sect.offset + @sizeOf(u64) * index;
17191650 const vmaddr = sect.addr + @sizeOf(u64) * index;
17201651
......@@ -1782,9 +1713,9 @@ fn writeSymbolTable(self: *MachO) !void {
17821713
17831714 // Advance size of __LINKEDIT segment
17841715 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1785 linkedit.filesize += symtab.nsyms * @sizeOf(macho.nlist_64);
1786 if (linkedit.vmsize < linkedit.filesize) {
1787 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1716 linkedit.inner.filesize += symtab.nsyms * @sizeOf(macho.nlist_64);
1717 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1718 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
17881719 }
17891720 self.cmd_table_dirty = true;
17901721}
......@@ -1799,9 +1730,9 @@ fn writeCodeSignaturePadding(self: *MachO) !void {
17991730 self.linkedit_segment_next_offset = fileoff + datasize;
18001731 // Advance size of __LINKEDIT segment
18011732 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1802 linkedit.filesize += datasize;
1803 if (linkedit.vmsize < linkedit.filesize) {
1804 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1733 linkedit.inner.filesize += datasize;
1734 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1735 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
18051736 }
18061737 log.debug("writing code signature padding from 0x{x} to 0x{x}\n", .{ fileoff, fileoff + datasize });
18071738 // Pad out the space. We need to do this to calculate valid hashes for everything in the file
......@@ -1836,10 +1767,10 @@ fn writeExportTrie(self: *MachO) !void {
18361767 for (self.global_symbols.items) |symbol| {
18371768 // TODO figure out if we should put all global symbols into the export trie
18381769 const name = self.getString(symbol.n_strx);
1839 assert(symbol.n_value >= text_segment.vmaddr);
1770 assert(symbol.n_value >= text_segment.inner.vmaddr);
18401771 try trie.put(self.base.allocator, .{
18411772 .name = name,
1842 .vmaddr_offset = symbol.n_value - text_segment.vmaddr,
1773 .vmaddr_offset = symbol.n_value - text_segment.inner.vmaddr,
18431774 .export_flags = 0, // TODO workout creation of export flags
18441775 });
18451776 }
......@@ -1849,7 +1780,7 @@ fn writeExportTrie(self: *MachO) !void {
18491780
18501781 try trie.writeULEB128Mem(self.base.allocator, &buffer);
18511782
1852 const dyld_info = &self.load_commands.items[self.dyld_info_cmd_index.?].DyldInfo;
1783 const dyld_info = &self.load_commands.items[self.dyld_info_cmd_index.?].DyldInfoOnly;
18531784 const export_size = @intCast(u32, mem.alignForward(buffer.items.len, @sizeOf(u64)));
18541785 dyld_info.export_off = self.linkedit_segment_next_offset.?;
18551786 dyld_info.export_size = export_size;
......@@ -1865,9 +1796,9 @@ fn writeExportTrie(self: *MachO) !void {
18651796 self.linkedit_segment_next_offset = dyld_info.export_off + dyld_info.export_size;
18661797 // Advance size of __LINKEDIT segment
18671798 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1868 linkedit.filesize += dyld_info.export_size;
1869 if (linkedit.vmsize < linkedit.filesize) {
1870 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1799 linkedit.inner.filesize += dyld_info.export_size;
1800 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1801 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
18711802 }
18721803 self.cmd_table_dirty = true;
18731804}
......@@ -1890,49 +1821,31 @@ fn writeStringTable(self: *MachO) !void {
18901821 self.linkedit_segment_next_offset = symtab.stroff + symtab.strsize;
18911822 // Advance size of __LINKEDIT segment
18921823 const linkedit = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
1893 linkedit.filesize += symtab.strsize;
1894 if (linkedit.vmsize < linkedit.filesize) {
1895 linkedit.vmsize = mem.alignForwardGeneric(u64, linkedit.filesize, self.page_size);
1824 linkedit.inner.filesize += symtab.strsize;
1825 if (linkedit.inner.vmsize < linkedit.inner.filesize) {
1826 linkedit.inner.vmsize = mem.alignForwardGeneric(u64, linkedit.inner.filesize, self.page_size);
18961827 }
18971828 self.cmd_table_dirty = true;
18981829}
18991830
1900fn writeCmdHeaders(self: *MachO) !void {
1901 assert(self.cmd_table_dirty);
1902
1903 // Write all load command headers first.
1904 // Since command sizes are up-to-date and accurate, we will correctly
1905 // leave space for any section headers that any of the segment load
1906 // commands might consist of.
1907 var last_cmd_offset: usize = @sizeOf(macho.mach_header_64);
1908 for (self.load_commands.items) |cmd| {
1909 try cmd.write(&self.base.file.?, last_cmd_offset);
1910 last_cmd_offset += cmd.cmdsize();
1831/// Writes all load commands and section headers.
1832fn writeLoadCommands(self: *MachO) !void {
1833 var sizeofcmds: usize = 0;
1834 for (self.load_commands.items) |lc| {
1835 sizeofcmds += lc.cmdsize();
19111836 }
1912 {
1913 const off = if (self.text_segment_cmd_index) |text_segment_index| blk: {
1914 var i: usize = 0;
1915 var cmdsize: usize = @sizeOf(macho.mach_header_64) + @sizeOf(macho.segment_command_64);
1916 while (i < text_segment_index) : (i += 1) {
1917 cmdsize += self.load_commands.items[i].cmdsize();
1918 }
1919 break :blk cmdsize;
1920 } else {
1921 // If we've landed in here, we are building a MachO object file, so we have
1922 // only one, noname segment to append this section header to.
1923 return error.TODOImplementWritingObjFiles;
1924 };
1925 // write sections belonging to __TEXT segment
1926 // TODO section indices should belong to each Segment, and we should iterate dynamically.
1927 const id = self.text_section_index.?;
1928 log.debug("writing __TEXT section headers at 0x{x}\n", .{off});
1929 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.sections.items[id .. id + 2]), off);
1837
1838 var buffer = try self.base.allocator.alloc(u8, sizeofcmds);
1839 defer self.base.allocator.free(buffer);
1840 var writer = std.io.fixedBufferStream(buffer).writer();
1841 for (self.load_commands.items) |lc| {
1842 try lc.write(writer);
19301843 }
1844
1845 try self.base.file.?.pwriteAll(buffer, @sizeOf(macho.mach_header_64));
19311846}
19321847
19331848/// Writes Mach-O file header.
1934/// Should be invoked last as it needs up-to-date values of ncmds and sizeof_cmds bookkeeping
1935/// variables.
19361849fn writeMachOHeader(self: *MachO) !void {
19371850 var hdr: macho.mach_header_64 = undefined;
19381851 hdr.magic = macho.MH_MAGIC_64;
......@@ -1994,10 +1907,3 @@ fn satMul(a: anytype, b: anytype) @TypeOf(a, b) {
19941907 const T = @TypeOf(a, b);
19951908 return std.math.mul(T, a, b) catch std.math.maxInt(T);
19961909}
1997
1998test "" {
1999 // TODO surprisingly this causes a linking error:
2000 // _linkWithLLD symbol missing for arch
2001 // _ = std.testing.refAllDecls(@This());
2002 _ = std.testing.refAllDecls(@import("MachO/commands.zig"));
2003}
src/link/MachO/CodeSignature.zig+2-2
......@@ -146,8 +146,8 @@ pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
146146 const text_segment = bin_file.load_commands.items[bin_file.text_segment_cmd_index.?].Segment;
147147 const code_sig_cmd = bin_file.load_commands.items[bin_file.code_signature_cmd_index.?].LinkeditData;
148148
149 const execSegBase: u64 = text_segment.fileoff;
150 const execSegLimit: u64 = text_segment.filesize;
149 const execSegBase: u64 = text_segment.inner.fileoff;
150 const execSegLimit: u64 = text_segment.inner.filesize;
151151 const execSegFlags: u64 = if (bin_file.base.options.output_mode == .Exe) macho.CS_EXECSEG_MAIN_BINARY else 0;
152152 const file_size = code_sig_cmd.dataoff;
153153 var cdir = CodeDirectory{
src/link/MachO/commands.zig+16-9
......@@ -154,7 +154,11 @@ pub const LoadCommand = union(enum) {
154154
155155pub const SegmentCommand = struct {
156156 inner: macho.segment_command_64,
157 sections: std.StringArrayHashMapUnmanaged(macho.section_64) = .{},
157 sections: std.ArrayListUnmanaged(macho.section_64) = .{},
158
159 pub fn empty(inner: macho.segment_command_64) SegmentCommand {
160 return .{ .inner = inner };
161 }
158162
159163 pub fn read(alloc: *Allocator, reader: anytype) !SegmentCommand {
160164 const inner = try reader.readStruct(macho.segment_command_64);
......@@ -166,7 +170,7 @@ pub const SegmentCommand = struct {
166170 var i: usize = 0;
167171 while (i < inner.nsects) : (i += 1) {
168172 const section = try reader.readStruct(macho.section_64);
169 segment.sections.putAssumeCapacityNoClobber(mem.trimRight(u8, section.sectname[0..], &[_]u8{0}), section);
173 segment.sections.appendAssumeCapacity(section);
170174 }
171175
172176 return segment;
......@@ -176,8 +180,8 @@ pub const SegmentCommand = struct {
176180 const cmd = [1]macho.segment_command_64{self.inner};
177181 try writer.writeAll(mem.sliceAsBytes(cmd[0..1]));
178182
179 for (self.sections.items()) |entry| {
180 const section = [1]macho.section_64{entry.value};
183 for (self.sections.items) |sect| {
184 const section = [1]macho.section_64{sect};
181185 try writer.writeAll(mem.sliceAsBytes(section[0..1]));
182186 }
183187 }
......@@ -188,17 +192,20 @@ pub const SegmentCommand = struct {
188192
189193 fn eql(self: SegmentCommand, other: SegmentCommand) bool {
190194 if (!mem.eql(u8, mem.asBytes(&self.inner), mem.asBytes(&other.inner))) return false;
191 const lhs = self.sections.items();
192 const rhs = other.sections.items();
195 const lhs = self.sections.items;
196 const rhs = other.sections.items;
193197 var i: usize = 0;
194198 while (i < self.inner.nsects) : (i += 1) {
195 if (!mem.eql(u8, lhs[i].key, rhs[i].key)) return false;
196 if (!mem.eql(u8, mem.asBytes(&lhs[i].value), mem.asBytes(&rhs[i].value))) return false;
199 if (!mem.eql(u8, mem.asBytes(&lhs[i]), mem.asBytes(&rhs[i]))) return false;
197200 }
198201 return true;
199202 }
200203};
201204
205pub fn emptyGenericCommandWithData(cmd: anytype) GenericCommandWithData(@TypeOf(cmd)) {
206 return .{ .inner = cmd };
207}
208
202209pub fn GenericCommandWithData(comptime Cmd: type) type {
203210 return struct {
204211 inner: Cmd,
......@@ -290,7 +297,7 @@ test "read-write segment command" {
290297 .flags = 0,
291298 },
292299 };
293 try cmd.sections.putNoClobber(gpa, "__text", .{
300 try cmd.sections.append(gpa, .{
294301 .sectname = makeName("__text"),
295302 .segname = makeName("__TEXT"),
296303 .addr = 4294983680,