mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-07 14:24:43 +00:00
macho: calculate UUID as a streaming MD5 hash of the file contents
This commit is contained in:
parent
db2052bc35
commit
ec40c6b28f
3 changed files with 29 additions and 9 deletions
|
|
@ -593,6 +593,7 @@ set(ZIG_STAGE2_SOURCES
|
||||||
"${CMAKE_SOURCE_DIR}/src/link/MachO/fat.zig"
|
"${CMAKE_SOURCE_DIR}/src/link/MachO/fat.zig"
|
||||||
"${CMAKE_SOURCE_DIR}/src/link/MachO/load_commands.zig"
|
"${CMAKE_SOURCE_DIR}/src/link/MachO/load_commands.zig"
|
||||||
"${CMAKE_SOURCE_DIR}/src/link/MachO/thunks.zig"
|
"${CMAKE_SOURCE_DIR}/src/link/MachO/thunks.zig"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/link/MachO/uuid.zig"
|
||||||
"${CMAKE_SOURCE_DIR}/src/link/MachO/zld.zig"
|
"${CMAKE_SOURCE_DIR}/src/link/MachO/zld.zig"
|
||||||
"${CMAKE_SOURCE_DIR}/src/link/Plan9.zig"
|
"${CMAKE_SOURCE_DIR}/src/link/Plan9.zig"
|
||||||
"${CMAKE_SOURCE_DIR}/src/link/Plan9/aout.zig"
|
"${CMAKE_SOURCE_DIR}/src/link/Plan9/aout.zig"
|
||||||
|
|
|
||||||
|
|
@ -323,3 +323,12 @@ pub fn writeSourceVersionLC(ncmds: *u32, lc_writer: anytype) !void {
|
||||||
});
|
});
|
||||||
ncmds.* += 1;
|
ncmds.* += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn writeUuidLC(uuid: *const [16]u8, ncmds: *u32, lc_writer: anytype) !void {
|
||||||
|
var uuid_lc = macho.uuid_command{
|
||||||
|
.cmdsize = @sizeOf(macho.uuid_command),
|
||||||
|
.uuid = uuid.*,
|
||||||
|
};
|
||||||
|
try lc_writer.writeStruct(uuid_lc);
|
||||||
|
ncmds.* += 1;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ const link = @import("../../link.zig");
|
||||||
const load_commands = @import("load_commands.zig");
|
const load_commands = @import("load_commands.zig");
|
||||||
const thunks = @import("thunks.zig");
|
const thunks = @import("thunks.zig");
|
||||||
const trace = @import("../../tracy.zig").trace;
|
const trace = @import("../../tracy.zig").trace;
|
||||||
|
const uuid = @import("uuid.zig");
|
||||||
|
|
||||||
const Allocator = mem.Allocator;
|
const Allocator = mem.Allocator;
|
||||||
const Archive = @import("Archive.zig");
|
const Archive = @import("Archive.zig");
|
||||||
|
|
@ -3986,6 +3987,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
|
||||||
|
|
||||||
var lc_buffer = std.ArrayList(u8).init(arena);
|
var lc_buffer = std.ArrayList(u8).init(arena);
|
||||||
const lc_writer = lc_buffer.writer();
|
const lc_writer = lc_buffer.writer();
|
||||||
|
|
||||||
var ncmds: u32 = 0;
|
var ncmds: u32 = 0;
|
||||||
|
|
||||||
try zld.writeLinkeditSegmentData(&ncmds, lc_writer, reverse_lookups);
|
try zld.writeLinkeditSegmentData(&ncmds, lc_writer, reverse_lookups);
|
||||||
|
|
@ -4030,15 +4032,14 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
|
||||||
try load_commands.writeSourceVersionLC(&ncmds, lc_writer);
|
try load_commands.writeSourceVersionLC(&ncmds, lc_writer);
|
||||||
try load_commands.writeBuildVersionLC(zld.options, &ncmds, lc_writer);
|
try load_commands.writeBuildVersionLC(zld.options, &ncmds, lc_writer);
|
||||||
|
|
||||||
{
|
// Looking forward into the future, we will want to offer `-no_uuid` support in which case
|
||||||
var uuid_lc = macho.uuid_command{
|
// there will be nothing to backpatch.
|
||||||
.cmdsize = @sizeOf(macho.uuid_command),
|
const uuid_offset_backpatch: ?usize = blk: {
|
||||||
.uuid = undefined,
|
const index = lc_buffer.items.len;
|
||||||
|
var uuid_buf: [16]u8 = [_]u8{0} ** 16;
|
||||||
|
try load_commands.writeUuidLC(&uuid_buf, &ncmds, lc_writer);
|
||||||
|
break :blk index;
|
||||||
};
|
};
|
||||||
std.crypto.random.bytes(&uuid_lc.uuid);
|
|
||||||
try lc_writer.writeStruct(uuid_lc);
|
|
||||||
ncmds += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
try load_commands.writeLoadDylibLCs(zld.dylibs.items, zld.referenced_dylibs.keys(), &ncmds, lc_writer);
|
try load_commands.writeLoadDylibLCs(zld.dylibs.items, zld.referenced_dylibs.keys(), &ncmds, lc_writer);
|
||||||
|
|
||||||
|
|
@ -4071,6 +4072,15 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
|
||||||
try zld.file.pwriteAll(lc_buffer.items, @sizeOf(macho.mach_header_64) + headers_buf.items.len);
|
try zld.file.pwriteAll(lc_buffer.items, @sizeOf(macho.mach_header_64) + headers_buf.items.len);
|
||||||
try zld.writeHeader(ncmds, @intCast(u32, lc_buffer.items.len + headers_buf.items.len));
|
try zld.writeHeader(ncmds, @intCast(u32, lc_buffer.items.len + headers_buf.items.len));
|
||||||
|
|
||||||
|
if (uuid_offset_backpatch) |backpatch| {
|
||||||
|
const seg = zld.getLinkeditSegmentPtr();
|
||||||
|
const file_size = seg.fileoff + seg.filesize;
|
||||||
|
var uuid_buf: [16]u8 = undefined;
|
||||||
|
try uuid.calcMd5Hash(zld.gpa, zld.file, file_size, &uuid_buf);
|
||||||
|
const offset = @sizeOf(macho.mach_header_64) + headers_buf.items.len + backpatch + @sizeOf(macho.load_command);
|
||||||
|
try zld.file.pwriteAll(&uuid_buf, offset);
|
||||||
|
}
|
||||||
|
|
||||||
if (codesig) |*csig| {
|
if (codesig) |*csig| {
|
||||||
try zld.writeCodeSignature(comp, csig, codesig_offset.?); // code signing always comes last
|
try zld.writeCodeSignature(comp, csig, codesig_offset.?); // code signing always comes last
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue