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 {
52395239 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
52405240 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);
5243 defer code_sig.deinit();
5242 var code_sig: CodeSignature = .{};
5243 defer code_sig.deinit(self.base.allocator);
5244
52445245 try code_sig.calcAdhocSignature(
5246 self.base.allocator,
52455247 self.base.file.?,
52465248 self.base.options.emit.?.sub_path,
52475249 text_segment.inner,
52485250 code_sig_cmd,
52495251 self.base.options.output_mode,
5252 self.page_size,
52505253 );
52515254
52525255 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 {
4646 }
4747};
4848
49allocator: *Allocator,
50
5149/// Code signature blob header.
5250inner: macho.SuperBlob = .{
5351 .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,
......@@ -58,24 +56,15 @@ inner: macho.SuperBlob = .{
5856/// CodeDirectory header which holds the hash of the binary.
5957cdir: ?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
7259pub fn calcAdhocSignature(
7360 self: *CodeSignature,
61 allocator: *Allocator,
7462 file: fs.File,
7563 id: []const u8,
7664 text_segment: macho.segment_command_64,
7765 code_sig_cmd: macho.linkedit_data_command,
7866 output_mode: std.builtin.OutputMode,
67 page_size: u16,
7968) !void {
8069 const execSegBase: u64 = text_segment.fileoff;
8170 const execSegLimit: u64 = text_segment.filesize;
......@@ -95,7 +84,7 @@ pub fn calcAdhocSignature(
9584 .hashSize = hash_size,
9685 .hashType = macho.CS_HASHTYPE_SHA256,
9786 .platform = 0,
98 .pageSize = @truncate(u8, std.math.log2(self.page_size)),
87 .pageSize = @truncate(u8, std.math.log2(page_size)),
9988 .spare2 = 0,
10089 .scatterOffset = 0,
10190 .teamOffset = 0,
......@@ -107,13 +96,13 @@ pub fn calcAdhocSignature(
10796 },
10897 };
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
112101 var hash: [hash_size]u8 = undefined;
113 var buffer = try self.allocator.alloc(u8, self.page_size);
114 defer self.allocator.free(buffer);
102 var buffer = try allocator.alloc(u8, page_size);
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
118107 // 1. Save the identifier and update offsets
119108 cdir.inner.identOffset = cdir.inner.length;
......@@ -126,8 +115,8 @@ pub fn calcAdhocSignature(
126115 cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1;
127116 var i: usize = 0;
128117 while (i < total_pages) : (i += 1) {
129 const fstart = i * self.page_size;
130 const fsize = if (fstart + self.page_size > file_size) file_size - fstart else self.page_size;
118 const fstart = i * page_size;
119 const fsize = if (fstart + page_size > file_size) file_size - fstart else page_size;
131120 const len = try file.preadAll(buffer, fstart);
132121 assert(fsize <= len);
133122
......@@ -156,9 +145,9 @@ pub fn write(self: CodeSignature, writer: anytype) !void {
156145 try self.cdir.?.write(writer);
157146}
158147
159pub fn deinit(self: *CodeSignature) void {
148pub fn deinit(self: *CodeSignature, allocator: *Allocator) void {
160149 if (self.cdir) |*cdir| {
161 cdir.data.deinit(self.allocator);
150 cdir.data.deinit(allocator);
162151 }
163152}
164153