authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-31 21:31:06+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-01 09:06:56+02:00
logd19fdf09ae1686ae5f7eb5f2386dd6de1f441db5
tree34e0e521816ea35a19a6ba1b564260c9ca14d1e7
parent2e30bf23aa4515682038bc69ffc0b7d06b734be9

macho: make CodeSignature accept allocator as param

instead storing it within the struct.

2 files changed, 16 insertions(+), 24 deletions(-)

src/link/MachO.zig+5-2
...@@ -5239,14 +5239,17 @@ fn writeCodeSignature(self: *MachO) !void {...@@ -5239,14 +5239,17 @@ fn writeCodeSignature(self: *MachO) !void {
5239 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;5239 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
5240 const code_sig_cmd = self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;5240 const code_sig_cmd = self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
52415241
5242 var code_sig = CodeSignature.init(self.base.allocator, self.page_size);5242 var code_sig: CodeSignature = .{};
5243 defer code_sig.deinit();5243 defer code_sig.deinit(self.base.allocator);
5244
5244 try code_sig.calcAdhocSignature(5245 try code_sig.calcAdhocSignature(
5246 self.base.allocator,
5245 self.base.file.?,5247 self.base.file.?,
5246 self.base.options.emit.?.sub_path,5248 self.base.options.emit.?.sub_path,
5247 text_segment.inner,5249 text_segment.inner,
5248 code_sig_cmd,5250 code_sig_cmd,
5249 self.base.options.output_mode,5251 self.base.options.output_mode,
5252 self.page_size,
5250 );5253 );
52515254
5252 var buffer = try self.base.allocator.alloc(u8, code_sig.size());5255 var buffer = try self.base.allocator.alloc(u8, code_sig.size());
src/link/MachO/CodeSignature.zig+11-22
...@@ -46,8 +46,6 @@ const CodeDirectory = struct {...@@ -46,8 +46,6 @@ const CodeDirectory = struct {
46 }46 }
47};47};
4848
49allocator: *Allocator,
50
51/// Code signature blob header.49/// Code signature blob header.
52inner: macho.SuperBlob = .{50inner: macho.SuperBlob = .{
53 .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,51 .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,
...@@ -58,24 +56,15 @@ inner: macho.SuperBlob = .{...@@ -58,24 +56,15 @@ inner: macho.SuperBlob = .{
58/// CodeDirectory header which holds the hash of the binary.56/// CodeDirectory header which holds the hash of the binary.
59cdir: ?CodeDirectory = null,57cdir: ?CodeDirectory = null,
6058
61/// Page size is dependent on the target cpu architecture.
62/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
63page_size: u16,
64
65pub fn init(allocator: *Allocator, page_size: u16) CodeSignature {
66 return .{
67 .allocator = allocator,
68 .page_size = page_size,
69 };
70}
71
72pub fn calcAdhocSignature(59pub fn calcAdhocSignature(
73 self: *CodeSignature,60 self: *CodeSignature,
61 allocator: *Allocator,
74 file: fs.File,62 file: fs.File,
75 id: []const u8,63 id: []const u8,
76 text_segment: macho.segment_command_64,64 text_segment: macho.segment_command_64,
77 code_sig_cmd: macho.linkedit_data_command,65 code_sig_cmd: macho.linkedit_data_command,
78 output_mode: std.builtin.OutputMode,66 output_mode: std.builtin.OutputMode,
67 page_size: u16,
79) !void {68) !void {
80 const execSegBase: u64 = text_segment.fileoff;69 const execSegBase: u64 = text_segment.fileoff;
81 const execSegLimit: u64 = text_segment.filesize;70 const execSegLimit: u64 = text_segment.filesize;
...@@ -95,7 +84,7 @@ pub fn calcAdhocSignature(...@@ -95,7 +84,7 @@ pub fn calcAdhocSignature(
95 .hashSize = hash_size,84 .hashSize = hash_size,
96 .hashType = macho.CS_HASHTYPE_SHA256,85 .hashType = macho.CS_HASHTYPE_SHA256,
97 .platform = 0,86 .platform = 0,
98 .pageSize = @truncate(u8, std.math.log2(self.page_size)),87 .pageSize = @truncate(u8, std.math.log2(page_size)),
99 .spare2 = 0,88 .spare2 = 0,
100 .scatterOffset = 0,89 .scatterOffset = 0,
101 .teamOffset = 0,90 .teamOffset = 0,
...@@ -107,13 +96,13 @@ pub fn calcAdhocSignature(...@@ -107,13 +96,13 @@ pub fn calcAdhocSignature(
107 },96 },
108 };97 };
10998
110 const total_pages = mem.alignForward(file_size, self.page_size) / self.page_size;99 const total_pages = mem.alignForward(file_size, page_size) / page_size;
111100
112 var hash: [hash_size]u8 = undefined;101 var hash: [hash_size]u8 = undefined;
113 var buffer = try self.allocator.alloc(u8, self.page_size);102 var buffer = try allocator.alloc(u8, page_size);
114 defer self.allocator.free(buffer);103 defer allocator.free(buffer);
115104
116 try cdir.data.ensureCapacity(self.allocator, total_pages * hash_size + id.len + 1);105 try cdir.data.ensureCapacity(allocator, total_pages * hash_size + id.len + 1);
117106
118 // 1. Save the identifier and update offsets107 // 1. Save the identifier and update offsets
119 cdir.inner.identOffset = cdir.inner.length;108 cdir.inner.identOffset = cdir.inner.length;
...@@ -126,8 +115,8 @@ pub fn calcAdhocSignature(...@@ -126,8 +115,8 @@ pub fn calcAdhocSignature(
126 cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1;115 cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1;
127 var i: usize = 0;116 var i: usize = 0;
128 while (i < total_pages) : (i += 1) {117 while (i < total_pages) : (i += 1) {
129 const fstart = i * self.page_size;118 const fstart = i * page_size;
130 const fsize = if (fstart + self.page_size > file_size) file_size - fstart else self.page_size;119 const fsize = if (fstart + page_size > file_size) file_size - fstart else page_size;
131 const len = try file.preadAll(buffer, fstart);120 const len = try file.preadAll(buffer, fstart);
132 assert(fsize <= len);121 assert(fsize <= len);
133122
...@@ -156,9 +145,9 @@ pub fn write(self: CodeSignature, writer: anytype) !void {...@@ -156,9 +145,9 @@ pub fn write(self: CodeSignature, writer: anytype) !void {
156 try self.cdir.?.write(writer);145 try self.cdir.?.write(writer);
157}146}
158147
159pub fn deinit(self: *CodeSignature) void {148pub fn deinit(self: *CodeSignature, allocator: *Allocator) void {
160 if (self.cdir) |*cdir| {149 if (self.cdir) |*cdir| {
161 cdir.data.deinit(self.allocator);150 cdir.data.deinit(allocator);
162 }151 }
163}152}
164153