diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index 8fc40decd76b098d727ebd1da8677334894016fe..f391899159ea2940f3bddbed3c666ae2e69e2ace 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -238,7 +238,6 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8, args: L }); try self.populateMetadata(); - try self.addRpaths(args.rpaths); try self.parseInputFiles(files); try self.parseLibs(args.libs); try self.parseLibSystem(args.libc_stub_path); @@ -246,6 +245,9 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8, args: L try self.resolveStubsAndGotEntries(); try self.updateMetadata(); try self.sortSections(); + try self.addRpaths(args.rpaths); + try self.addDataInCodeLC(); + try self.addCodeSignatureLC(); try self.allocateTextSegment(); try self.allocateDataConstSegment(); try self.allocateDataSegment(); @@ -2231,7 +2233,23 @@ fn populateMetadata(self: *Zld) !void { std.crypto.random.bytes(&uuid_cmd.uuid); try self.load_commands.append(self.allocator, .{ .Uuid = uuid_cmd }); } +} +fn addDataInCodeLC(self: *Zld) !void { + if (self.data_in_code_cmd_index == null) { + self.data_in_code_cmd_index = @intCast(u16, self.load_commands.items.len); + try self.load_commands.append(self.allocator, .{ + .LinkeditData = .{ + .cmd = macho.LC_DATA_IN_CODE, + .cmdsize = @sizeOf(macho.linkedit_data_command), + .dataoff = 0, + .datasize = 0, + }, + }); + } +} + +fn addCodeSignatureLC(self: *Zld) !void { if (self.code_signature_cmd_index == null and self.arch.? == .aarch64) { self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len); try self.load_commands.append(self.allocator, .{ @@ -2243,18 +2261,6 @@ fn populateMetadata(self: *Zld) !void { }, }); } - - if (self.data_in_code_cmd_index == null and self.arch.? == .x86_64) { - self.data_in_code_cmd_index = @intCast(u16, self.load_commands.items.len); - try self.load_commands.append(self.allocator, .{ - .LinkeditData = .{ - .cmd = macho.LC_DATA_IN_CODE, - .cmdsize = @sizeOf(macho.linkedit_data_command), - .dataoff = 0, - .datasize = 0, - }, - }); - } } fn addRpaths(self: *Zld, rpaths: []const []const u8) !void { @@ -2344,9 +2350,7 @@ fn flush(self: *Zld) !void { try self.writeBindInfoTable(); try self.writeLazyBindInfoTable(); try self.writeExportInfo(); - if (self.arch.? == .x86_64) { - try self.writeDataInCode(); - } + try self.writeDataInCode(); { const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;