| ... | ... | @@ -16,6 +16,7 @@ const link = @import("../../link.zig"); |
| 16 | 16 | const load_commands = @import("load_commands.zig"); |
| 17 | 17 | const thunks = @import("thunks.zig"); |
| 18 | 18 | const trace = @import("../../tracy.zig").trace; |
| 19 | const uuid = @import("uuid.zig"); |
| 19 | 20 | |
| 20 | 21 | const Allocator = mem.Allocator; |
| 21 | 22 | const Archive = @import("Archive.zig"); |
| ... | ... | @@ -3986,6 +3987,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr |
| 3986 | 3987 | |
| 3987 | 3988 | var lc_buffer = std.ArrayList(u8).init(arena); |
| 3988 | 3989 | const lc_writer = lc_buffer.writer(); |
| 3990 | |
| 3989 | 3991 | var ncmds: u32 = 0; |
| 3990 | 3992 | |
| 3991 | 3993 | try zld.writeLinkeditSegmentData(&ncmds, lc_writer, reverse_lookups); |
| ... | ... | @@ -4030,15 +4032,14 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr |
| 4030 | 4032 | try load_commands.writeSourceVersionLC(&ncmds, lc_writer); |
| 4031 | 4033 | try load_commands.writeBuildVersionLC(zld.options, &ncmds, lc_writer); |
| 4032 | 4034 | |
| 4033 | | { |
| 4034 | | var uuid_lc = macho.uuid_command{ |
| 4035 | | .cmdsize = @sizeOf(macho.uuid_command), |
| 4036 | | .uuid = undefined, |
| 4037 | | }; |
| 4038 | | std.crypto.random.bytes(&uuid_lc.uuid); |
| 4039 | | try lc_writer.writeStruct(uuid_lc); |
| 4040 | | ncmds += 1; |
| 4041 | | } |
| 4035 | // Looking forward into the future, we will want to offer `-no_uuid` support in which case |
| 4036 | // there will be nothing to backpatch. |
| 4037 | const uuid_offset_backpatch: ?usize = blk: { |
| 4038 | const index = lc_buffer.items.len; |
| 4039 | var uuid_buf: [16]u8 = [_]u8{0} ** 16; |
| 4040 | try load_commands.writeUuidLC(&uuid_buf, &ncmds, lc_writer); |
| 4041 | break :blk index; |
| 4042 | }; |
| 4042 | 4043 | |
| 4043 | 4044 | try load_commands.writeLoadDylibLCs(zld.dylibs.items, zld.referenced_dylibs.keys(), &ncmds, lc_writer); |
| 4044 | 4045 | |
| ... | ... | @@ -4071,6 +4072,15 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr |
| 4071 | 4072 | try zld.file.pwriteAll(lc_buffer.items, @sizeOf(macho.mach_header_64) + headers_buf.items.len); |
| 4072 | 4073 | try zld.writeHeader(ncmds, @intCast(u32, lc_buffer.items.len + headers_buf.items.len)); |
| 4073 | 4074 | |
| 4075 | if (uuid_offset_backpatch) |backpatch| { |
| 4076 | const seg = zld.getLinkeditSegmentPtr(); |
| 4077 | const file_size = seg.fileoff + seg.filesize; |
| 4078 | var uuid_buf: [16]u8 = undefined; |
| 4079 | try uuid.calcMd5Hash(zld.gpa, zld.file, file_size, &uuid_buf); |
| 4080 | const offset = @sizeOf(macho.mach_header_64) + headers_buf.items.len + backpatch + @sizeOf(macho.load_command); |
| 4081 | try zld.file.pwriteAll(&uuid_buf, offset); |
| 4082 | } |
| 4083 | |
| 4074 | 4084 | if (codesig) |*csig| { |
| 4075 | 4085 | try zld.writeCodeSignature(comp, csig, codesig_offset.?); // code signing always comes last |
| 4076 | 4086 | } |