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...@@ -238,7 +238,6 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8, args: L
238 });238 });
239239
240 try self.populateMetadata();240 try self.populateMetadata();
241 try self.addRpaths(args.rpaths);
242 try self.parseInputFiles(files);241 try self.parseInputFiles(files);
243 try self.parseLibs(args.libs);242 try self.parseLibs(args.libs);
244 try self.parseLibSystem(args.libc_stub_path);243 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...@@ -246,6 +245,9 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8, args: L
246 try self.resolveStubsAndGotEntries();245 try self.resolveStubsAndGotEntries();
247 try self.updateMetadata();246 try self.updateMetadata();
248 try self.sortSections();247 try self.sortSections();
248 try self.addRpaths(args.rpaths);
249 try self.addDataInCodeLC();
250 try self.addCodeSignatureLC();
249 try self.allocateTextSegment();251 try self.allocateTextSegment();
250 try self.allocateDataConstSegment();252 try self.allocateDataConstSegment();
251 try self.allocateDataSegment();253 try self.allocateDataSegment();
...@@ -2231,24 +2233,28 @@ fn populateMetadata(self: *Zld) !void {...@@ -2231,24 +2233,28 @@ fn populateMetadata(self: *Zld) !void {
2231 std.crypto.random.bytes(&uuid_cmd.uuid);2233 std.crypto.random.bytes(&uuid_cmd.uuid);
2232 try self.load_commands.append(self.allocator, .{ .Uuid = uuid_cmd });2234 try self.load_commands.append(self.allocator, .{ .Uuid = uuid_cmd });
2233 }2235 }
2236}
22342237
2235 if (self.code_signature_cmd_index == null and self.arch.? == .aarch64) {2238fn addDataInCodeLC(self: *Zld) !void {
2236 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);2239 if (self.data_in_code_cmd_index == null) {
2240 self.data_in_code_cmd_index = @intCast(u16, self.load_commands.items.len);
2237 try self.load_commands.append(self.allocator, .{2241 try self.load_commands.append(self.allocator, .{
2238 .LinkeditData = .{2242 .LinkeditData = .{
2239 .cmd = macho.LC_CODE_SIGNATURE,2243 .cmd = macho.LC_DATA_IN_CODE,
2240 .cmdsize = @sizeOf(macho.linkedit_data_command),2244 .cmdsize = @sizeOf(macho.linkedit_data_command),
2241 .dataoff = 0,2245 .dataoff = 0,
2242 .datasize = 0,2246 .datasize = 0,
2243 },2247 },
2244 });2248 });
2245 }2249 }
2250}
22462251
2247 if (self.data_in_code_cmd_index == null and self.arch.? == .x86_64) {2252fn addCodeSignatureLC(self: *Zld) !void {
2248 self.data_in_code_cmd_index = @intCast(u16, self.load_commands.items.len);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);
2249 try self.load_commands.append(self.allocator, .{2255 try self.load_commands.append(self.allocator, .{
2250 .LinkeditData = .{2256 .LinkeditData = .{
2251 .cmd = macho.LC_DATA_IN_CODE,2257 .cmd = macho.LC_CODE_SIGNATURE,
2252 .cmdsize = @sizeOf(macho.linkedit_data_command),2258 .cmdsize = @sizeOf(macho.linkedit_data_command),
2253 .dataoff = 0,2259 .dataoff = 0,
2254 .datasize = 0,2260 .datasize = 0,
...@@ -2344,9 +2350,7 @@ fn flush(self: *Zld) !void {...@@ -2344,9 +2350,7 @@ fn flush(self: *Zld) !void {
2344 try self.writeBindInfoTable();2350 try self.writeBindInfoTable();
2345 try self.writeLazyBindInfoTable();2351 try self.writeLazyBindInfoTable();
2346 try self.writeExportInfo();2352 try self.writeExportInfo();
2347 if (self.arch.? == .x86_64) {2353 try self.writeDataInCode();
2348 try self.writeDataInCode();
2349 }
23502354
2351 {2355 {
2352 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;2356 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;