| ... | ... | @@ -7,19 +7,24 @@ const macho = std.macho; |
| 7 | 7 | const mem = std.mem; |
| 8 | 8 | const testing = std.testing; |
| 9 | 9 | const Allocator = mem.Allocator; |
| 10 | const Sha256 = std.crypto.hash.sha2.Sha256; |
| 10 | 11 | |
| 11 | 12 | const MachO = @import("../MachO.zig"); |
| 12 | 13 | |
| 13 | | const Blob = struct { |
| 14 | const hash_size: u8 = 32; |
| 15 | const page_size: u16 = 0x1000; |
| 16 | |
| 17 | const CodeDirectory = struct { |
| 14 | 18 | inner: macho.CodeDirectory, |
| 15 | 19 | data: std.ArrayListUnmanaged(u8) = .{}, |
| 16 | 20 | |
| 17 | | fn size(self: Blob) u32 { |
| 21 | fn size(self: CodeDirectory) u32 { |
| 18 | 22 | return self.inner.length; |
| 19 | 23 | } |
| 20 | 24 | |
| 21 | | fn write(self: Blob, buffer: []u8) void { |
| 25 | fn write(self: CodeDirectory, buffer: []u8) void { |
| 22 | 26 | assert(buffer.len >= self.inner.length); |
| 27 | |
| 23 | 28 | mem.writeIntBig(u32, buffer[0..4], self.inner.magic); |
| 24 | 29 | mem.writeIntBig(u32, buffer[4..8], self.inner.length); |
| 25 | 30 | mem.writeIntBig(u32, buffer[8..12], self.inner.version); |
| ... | ... | @@ -29,10 +34,10 @@ const Blob = struct { |
| 29 | 34 | mem.writeIntBig(u32, buffer[24..28], self.inner.nSpecialSlots); |
| 30 | 35 | mem.writeIntBig(u32, buffer[28..32], self.inner.nCodeSlots); |
| 31 | 36 | mem.writeIntBig(u32, buffer[32..36], self.inner.codeLimit); |
| 32 | | mem.writeIntBig(u8, buffer[36..37], self.inner.hashSize); |
| 33 | | mem.writeIntBig(u8, buffer[37..38], self.inner.hashType); |
| 34 | | mem.writeIntBig(u8, buffer[38..39], self.inner.platform); |
| 35 | | mem.writeIntBig(u8, buffer[39..40], self.inner.pageSize); |
| 37 | buffer[36] = self.inner.hashSize; |
| 38 | buffer[37] = self.inner.hashType; |
| 39 | buffer[38] = self.inner.platform; |
| 40 | buffer[39] = self.inner.pageSize; |
| 36 | 41 | mem.writeIntBig(u32, buffer[40..44], self.inner.spare2); |
| 37 | 42 | mem.writeIntBig(u32, buffer[44..48], self.inner.scatterOffset); |
| 38 | 43 | mem.writeIntBig(u32, buffer[48..52], self.inner.teamOffset); |
| ... | ... | @@ -41,6 +46,8 @@ const Blob = struct { |
| 41 | 46 | mem.writeIntBig(u64, buffer[64..72], self.inner.execSegBase); |
| 42 | 47 | mem.writeIntBig(u64, buffer[72..80], self.inner.execSegLimit); |
| 43 | 48 | mem.writeIntBig(u64, buffer[80..88], self.inner.execSegFlags); |
| 49 | |
| 50 | mem.copy(u8, buffer[88..], self.data.items); |
| 44 | 51 | } |
| 45 | 52 | }; |
| 46 | 53 | |
| ... | ... | @@ -50,7 +57,7 @@ inner: macho.SuperBlob = .{ |
| 50 | 57 | .length = @sizeOf(macho.SuperBlob), |
| 51 | 58 | .count = 0, |
| 52 | 59 | }, |
| 53 | | blob: ?Blob = null, |
| 60 | cdir: ?CodeDirectory = null, |
| 54 | 61 | |
| 55 | 62 | pub fn init(alloc: *Allocator) CodeSignature { |
| 56 | 63 | return .{ |
| ... | ... | @@ -60,10 +67,14 @@ pub fn init(alloc: *Allocator) CodeSignature { |
| 60 | 67 | |
| 61 | 68 | pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void { |
| 62 | 69 | const text_segment = bin_file.load_commands.items[bin_file.text_segment_cmd_index.?].Segment; |
| 70 | const data_segment = bin_file.load_commands.items[bin_file.data_segment_cmd_index.?].Segment; |
| 71 | const linkedit_segment = bin_file.load_commands.items[bin_file.linkedit_segment_cmd_index.?].Segment; |
| 72 | const symtab = bin_file.load_commands.items[bin_file.symtab_cmd_index.?].Symtab; |
| 73 | |
| 63 | 74 | const execSegBase: u64 = text_segment.fileoff; |
| 64 | 75 | const execSegLimit: u64 = text_segment.filesize; |
| 65 | | const execSegFlags: u64 = text_segment.flags; |
| 66 | | var blob = Blob{ |
| 76 | const execSegFlags: u64 = if (bin_file.base.options.output_mode == .Exe) macho.CS_EXECSEG_MAIN_BINARY else 0; |
| 77 | var cdir = CodeDirectory{ |
| 67 | 78 | .inner = .{ |
| 68 | 79 | .magic = macho.CSMAGIC_CODEDIRECTORY, |
| 69 | 80 | .length = @sizeOf(macho.CodeDirectory), |
| ... | ... | @@ -74,10 +85,10 @@ pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void { |
| 74 | 85 | .nSpecialSlots = 0, |
| 75 | 86 | .nCodeSlots = 0, |
| 76 | 87 | .codeLimit = 0, |
| 77 | | .hashSize = 0, |
| 78 | | .hashType = 0, |
| 88 | .hashSize = hash_size, |
| 89 | .hashType = macho.CS_HASHTYPE_SHA256, |
| 79 | 90 | .platform = 0, |
| 80 | | .pageSize = 0, |
| 91 | .pageSize = @truncate(u8, std.math.log2(page_size)), |
| 81 | 92 | .spare2 = 0, |
| 82 | 93 | .scatterOffset = 0, |
| 83 | 94 | .teamOffset = 0, |
| ... | ... | @@ -88,9 +99,48 @@ pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void { |
| 88 | 99 | .execSegFlags = execSegFlags, |
| 89 | 100 | }, |
| 90 | 101 | }; |
| 91 | | self.inner.length += @sizeOf(macho.BlobIndex) + blob.size(); |
| 102 | |
| 103 | const file_size = symtab.stroff + symtab.strsize; |
| 104 | const total_pages = mem.alignForward(file_size, page_size) / page_size; |
| 105 | log.debug("Total file size: {}; total number of pages: {}\n", .{ file_size, total_pages }); |
| 106 | |
| 107 | var hash: [hash_size]u8 = undefined; |
| 108 | var buffer = try bin_file.base.allocator.alloc(u8, page_size); |
| 109 | defer bin_file.base.allocator.free(buffer); |
| 110 | const macho_file = bin_file.base.file.?; |
| 111 | |
| 112 | const id = bin_file.base.options.emit.?.sub_path; |
| 113 | try cdir.data.ensureCapacity(self.alloc, total_pages * hash_size + id.len + 1); |
| 114 | |
| 115 | // 1. Save the identifier and update offsets |
| 116 | cdir.inner.identOffset = cdir.inner.length; |
| 117 | cdir.data.appendSliceAssumeCapacity(id); |
| 118 | cdir.data.appendAssumeCapacity(0); |
| 119 | |
| 120 | // 2. Calculate hash for each page (in file) and write it to the buffer |
| 121 | // TODO figure out how we can cache several hashes since we won't update |
| 122 | // every page during incremental linking |
| 123 | cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1; |
| 124 | var i: usize = 0; |
| 125 | while (i < total_pages) : (i += 1) { |
| 126 | const fstart = i * page_size; |
| 127 | const fsize = if (fstart + page_size > file_size) file_size - fstart else page_size; |
| 128 | const len = try macho_file.preadAll(buffer, fstart); |
| 129 | assert(fsize <= len); |
| 130 | |
| 131 | Sha256.hash(buffer[0..fsize], &hash, .{}); |
| 132 | log.debug("Calculated hash for page 0x{x}-0x{x}: 0x{x}\n", .{ fstart, fstart + fsize, hash[0..] }); |
| 133 | |
| 134 | cdir.data.appendSliceAssumeCapacity(hash[0..]); |
| 135 | cdir.inner.nCodeSlots += 1; |
| 136 | } |
| 137 | |
| 138 | // 3. Update CodeDirectory length |
| 139 | cdir.inner.length += @intCast(u32, cdir.data.items.len); |
| 140 | |
| 141 | self.inner.length += @sizeOf(macho.BlobIndex) + cdir.size(); |
| 92 | 142 | self.inner.count = 1; |
| 93 | | self.blob = blob; |
| 143 | self.cdir = cdir; |
| 94 | 144 | } |
| 95 | 145 | |
| 96 | 146 | pub fn size(self: CodeSignature) u32 { |
| ... | ... | @@ -102,12 +152,12 @@ pub fn write(self: CodeSignature, buffer: []u8) void { |
| 102 | 152 | self.writeHeader(buffer); |
| 103 | 153 | const offset: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex); |
| 104 | 154 | writeBlobIndex(macho.CSSLOT_CODEDIRECTORY, offset, buffer[@sizeOf(macho.SuperBlob)..]); |
| 105 | | self.blob.?.write(buffer[offset..]); |
| 155 | self.cdir.?.write(buffer[offset..]); |
| 106 | 156 | } |
| 107 | 157 | |
| 108 | 158 | pub fn deinit(self: *CodeSignature) void { |
| 109 | | if (self.blob) |*b| { |
| 110 | | b.data.deinit(self.alloc); |
| 159 | if (self.cdir) |*cdir| { |
| 160 | cdir.data.deinit(self.alloc); |
| 111 | 161 | } |
| 112 | 162 | } |
| 113 | 163 | |