authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-24 22:40:19+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-24 22:40:19+02:00
log8fc5b5a0874f16a16dd16206e35157cefba5edc5
tree677e702cb87fb64be978e663249ca737cf8f8520
parentad0be7857745613f53d3902c86d54401bb72696c

zld: put DICE and CodeSig load commands last

after `LC_LOAD_DYLIB` commands to match ld64 and make the binaries compatible with Apple tools.

1 files changed, 14 insertions(+), 10 deletions(-)

src/link/MachO/Zld.zig+14-10
......@@ -238,7 +238,6 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8, args: L
238238 });
239239
240240 try self.populateMetadata();
241 try self.addRpaths(args.rpaths);
242241 try self.parseInputFiles(files);
243242 try self.parseLibs(args.libs);
244243 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
246245 try self.resolveStubsAndGotEntries();
247246 try self.updateMetadata();
248247 try self.sortSections();
248 try self.addRpaths(args.rpaths);
249 try self.addDataInCodeLC();
250 try self.addCodeSignatureLC();
249251 try self.allocateTextSegment();
250252 try self.allocateDataConstSegment();
251253 try self.allocateDataSegment();
......@@ -2231,24 +2233,28 @@ fn populateMetadata(self: *Zld) !void {
22312233 std.crypto.random.bytes(&uuid_cmd.uuid);
22322234 try self.load_commands.append(self.allocator, .{ .Uuid = uuid_cmd });
22332235 }
2236}
22342237
2235 if (self.code_signature_cmd_index == null and self.arch.? == .aarch64) {
2236 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
2238fn addDataInCodeLC(self: *Zld) !void {
2239 if (self.data_in_code_cmd_index == null) {
2240 self.data_in_code_cmd_index = @intCast(u16, self.load_commands.items.len);
22372241 try self.load_commands.append(self.allocator, .{
22382242 .LinkeditData = .{
2239 .cmd = macho.LC_CODE_SIGNATURE,
2243 .cmd = macho.LC_DATA_IN_CODE,
22402244 .cmdsize = @sizeOf(macho.linkedit_data_command),
22412245 .dataoff = 0,
22422246 .datasize = 0,
22432247 },
22442248 });
22452249 }
2250}
22462251
2247 if (self.data_in_code_cmd_index == null and self.arch.? == .x86_64) {
2248 self.data_in_code_cmd_index = @intCast(u16, self.load_commands.items.len);
2252fn addCodeSignatureLC(self: *Zld) !void {
2253 if (self.code_signature_cmd_index == null and self.arch.? == .aarch64) {
2254 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
22492255 try self.load_commands.append(self.allocator, .{
22502256 .LinkeditData = .{
2251 .cmd = macho.LC_DATA_IN_CODE,
2257 .cmd = macho.LC_CODE_SIGNATURE,
22522258 .cmdsize = @sizeOf(macho.linkedit_data_command),
22532259 .dataoff = 0,
22542260 .datasize = 0,
......@@ -2344,9 +2350,7 @@ fn flush(self: *Zld) !void {
23442350 try self.writeBindInfoTable();
23452351 try self.writeLazyBindInfoTable();
23462352 try self.writeExportInfo();
2347 if (self.arch.? == .x86_64) {
2348 try self.writeDataInCode();
2349 }
2353 try self.writeDataInCode();
23502354
23512355 {
23522356 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;