authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-26 21:16:53+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-31 10:19:04+01:00
log0ff56e8bb14273fa8abe4503855e9f53d699c8cd
treefe601218cccefa19aaf4b2f4ff427cd8dd35c35d
parente1451f92f8e49f844c528386c4463c9b6fc9a0f3

macho: add and populate UUID load command


2 files changed, 25 insertions(+), 0 deletions(-)

src/link/MachO.zig+17
......@@ -74,6 +74,8 @@ main_cmd_index: ?u16 = null,
7474version_min_cmd_index: ?u16 = null,
7575/// Source version
7676source_version_cmd_index: ?u16 = null,
77/// UUID load command
78uuid_cmd_index: ?u16 = null,
7779/// Code signature
7880code_signature_cmd_index: ?u16 = null,
7981
......@@ -1609,6 +1611,18 @@ pub fn populateMissingMetadata(self: *MachO) !void {
16091611 self.header_dirty = true;
16101612 self.load_commands_dirty = true;
16111613 }
1614 if (self.uuid_cmd_index == null) {
1615 self.uuid_cmd_index = @intCast(u16, self.load_commands.items.len);
1616 var uuid_cmd: macho.uuid_command = .{
1617 .cmd = macho.LC_UUID,
1618 .cmdsize = @sizeOf(macho.uuid_command),
1619 .uuid = undefined,
1620 };
1621 std.crypto.random.bytes(&uuid_cmd.uuid);
1622 try self.load_commands.append(self.base.allocator, .{ .Uuid = uuid_cmd });
1623 self.header_dirty = true;
1624 self.load_commands_dirty = true;
1625 }
16121626 if (self.code_signature_cmd_index == null) {
16131627 self.code_signature_cmd_index = @intCast(u16, self.load_commands.items.len);
16141628 try self.load_commands.append(self.base.allocator, .{
......@@ -2347,6 +2361,9 @@ fn parseFromFile(self: *MachO, file: fs.File) !void {
23472361 macho.LC_SOURCE_VERSION => {
23482362 self.source_version_cmd_index = i;
23492363 },
2364 macho.LC_UUID => {
2365 self.uuid_cmd_index = i;
2366 },
23502367 macho.LC_MAIN => {
23512368 self.main_cmd_index = i;
23522369 },
src/link/MachO/commands.zig+8
......@@ -23,6 +23,7 @@ pub const LoadCommand = union(enum) {
2323 Main: macho.entry_point_command,
2424 VersionMin: macho.version_min_command,
2525 SourceVersion: macho.source_version_command,
26 Uuid: macho.uuid_command,
2627 LinkeditData: macho.linkedit_data_command,
2728 Unknown: GenericCommandWithData(macho.load_command),
2829
......@@ -62,6 +63,9 @@ pub const LoadCommand = union(enum) {
6263 macho.LC_SOURCE_VERSION => LoadCommand{
6364 .SourceVersion = try stream.reader().readStruct(macho.source_version_command),
6465 },
66 macho.LC_UUID => LoadCommand{
67 .Uuid = try stream.reader().readStruct(macho.uuid_command),
68 },
6569 macho.LC_FUNCTION_STARTS, macho.LC_DATA_IN_CODE, macho.LC_CODE_SIGNATURE => LoadCommand{
6670 .LinkeditData = try stream.reader().readStruct(macho.linkedit_data_command),
6771 },
......@@ -79,6 +83,7 @@ pub const LoadCommand = union(enum) {
7983 .Main => |x| writeStruct(x, writer),
8084 .VersionMin => |x| writeStruct(x, writer),
8185 .SourceVersion => |x| writeStruct(x, writer),
86 .Uuid => |x| writeStruct(x, writer),
8287 .LinkeditData => |x| writeStruct(x, writer),
8388 .Segment => |x| x.write(writer),
8489 .Dylinker => |x| x.write(writer),
......@@ -95,6 +100,7 @@ pub const LoadCommand = union(enum) {
95100 .Main => |x| x.cmd,
96101 .VersionMin => |x| x.cmd,
97102 .SourceVersion => |x| x.cmd,
103 .Uuid => |x| x.cmd,
98104 .LinkeditData => |x| x.cmd,
99105 .Segment => |x| x.inner.cmd,
100106 .Dylinker => |x| x.inner.cmd,
......@@ -112,6 +118,7 @@ pub const LoadCommand = union(enum) {
112118 .VersionMin => |x| x.cmdsize,
113119 .SourceVersion => |x| x.cmdsize,
114120 .LinkeditData => |x| x.cmdsize,
121 .Uuid => |x| x.cmdsize,
115122 .Segment => |x| x.inner.cmdsize,
116123 .Dylinker => |x| x.inner.cmdsize,
117124 .Dylib => |x| x.inner.cmdsize,
......@@ -142,6 +149,7 @@ pub const LoadCommand = union(enum) {
142149 .Main => |x| meta.eql(x, other.Main),
143150 .VersionMin => |x| meta.eql(x, other.VersionMin),
144151 .SourceVersion => |x| meta.eql(x, other.SourceVersion),
152 .Uuid => |x| meta.eql(x, other.Uuid),
145153 .LinkeditData => |x| meta.eql(x, other.LinkeditData),
146154 .Segment => |x| x.eql(other.Segment),
147155 .Dylinker => |x| x.eql(other.Dylinker),