1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
const CodeSignature = @This();
const std = @import("std");
const assert = std.debug.assert;
const log = std.log.scoped(.link);
const macho = std.macho;
const mem = std.mem;
const testing = std.testing;
const Allocator = mem.Allocator;
const MachO = @import("../MachO.zig");
const Blob = struct {
inner: macho.CodeDirectory,
data: std.ArrayListUnmanaged(u8) = .{},
fn size(self: Blob) u32 {
return self.inner.length;
}
fn write(self: Blob, buffer: []u8) void {
assert(buffer.len >= self.inner.length);
mem.writeIntBig(u32, buffer[0..4], self.inner.magic);
mem.writeIntBig(u32, buffer[4..8], self.inner.length);
mem.writeIntBig(u32, buffer[8..12], self.inner.version);
mem.writeIntBig(u32, buffer[12..16], self.inner.flags);
mem.writeIntBig(u32, buffer[16..20], self.inner.hashOffset);
mem.writeIntBig(u32, buffer[20..24], self.inner.identOffset);
mem.writeIntBig(u32, buffer[24..28], self.inner.nSpecialSlots);
mem.writeIntBig(u32, buffer[28..32], self.inner.nCodeSlots);
mem.writeIntBig(u32, buffer[32..36], self.inner.codeLimit);
mem.writeIntBig(u8, buffer[36..37], self.inner.hashSize);
mem.writeIntBig(u8, buffer[37..38], self.inner.hashType);
mem.writeIntBig(u8, buffer[38..39], self.inner.platform);
mem.writeIntBig(u8, buffer[39..40], self.inner.pageSize);
mem.writeIntBig(u32, buffer[40..44], self.inner.spare2);
mem.writeIntBig(u32, buffer[44..48], self.inner.scatterOffset);
mem.writeIntBig(u32, buffer[48..52], self.inner.teamOffset);
mem.writeIntBig(u32, buffer[52..56], self.inner.spare3);
mem.writeIntBig(u64, buffer[56..64], self.inner.codeLimit64);
mem.writeIntBig(u64, buffer[64..72], self.inner.execSegBase);
mem.writeIntBig(u64, buffer[72..80], self.inner.execSegLimit);
mem.writeIntBig(u64, buffer[80..88], self.inner.execSegFlags);
}
};
alloc: *Allocator,
inner: macho.SuperBlob = .{
.magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,
.length = @sizeOf(macho.SuperBlob),
.count = 0,
},
blob: ?Blob = null,
pub fn init(alloc: *Allocator) CodeSignature {
return .{
.alloc = alloc,
};
}
pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
const text_segment = bin_file.load_commands.items[bin_file.text_segment_cmd_index.?].Segment;
const execSegBase: u64 = text_segment.fileoff;
const execSegLimit: u64 = text_segment.filesize;
const execSegFlags: u64 = text_segment.flags;
var blob = Blob{
.inner = .{
.magic = macho.CSMAGIC_CODEDIRECTORY,
.length = @sizeOf(macho.CodeDirectory),
.version = macho.CS_SUPPORTSEXECSEG,
.flags = macho.CS_ADHOC,
.hashOffset = 0,
.identOffset = 0,
.nSpecialSlots = 0,
.nCodeSlots = 0,
.codeLimit = 0,
.hashSize = 0,
.hashType = 0,
.platform = 0,
.pageSize = 0,
.spare2 = 0,
.scatterOffset = 0,
.teamOffset = 0,
.spare3 = 0,
.codeLimit64 = 0,
.execSegBase = execSegBase,
.execSegLimit = execSegLimit,
.execSegFlags = execSegFlags,
},
};
self.inner.length += @sizeOf(macho.BlobIndex) + blob.size();
self.inner.count = 1;
self.blob = blob;
}
pub fn size(self: CodeSignature) u32 {
return self.inner.length;
}
pub fn write(self: CodeSignature, buffer: []u8) void {
assert(buffer.len >= self.inner.length);
self.writeHeader(buffer);
const offset: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex);
writeBlobIndex(macho.CSSLOT_CODEDIRECTORY, offset, buffer[@sizeOf(macho.SuperBlob)..]);
self.blob.?.write(buffer[offset..]);
}
pub fn deinit(self: *CodeSignature) void {
if (self.blob) |*b| {
b.data.deinit(self.alloc);
}
}
fn writeHeader(self: CodeSignature, buffer: []u8) void {
assert(buffer.len >= @sizeOf(macho.SuperBlob));
mem.writeIntBig(u32, buffer[0..4], self.inner.magic);
mem.writeIntBig(u32, buffer[4..8], self.inner.length);
mem.writeIntBig(u32, buffer[8..12], self.inner.count);
}
fn writeBlobIndex(tt: u32, offset: u32, buffer: []u8) void {
assert(buffer.len >= @sizeOf(macho.BlobIndex));
mem.writeIntBig(u32, buffer[0..4], tt);
mem.writeIntBig(u32, buffer[4..8], offset);
}
test "CodeSignature header" {
var code_sig = CodeSignature.init(testing.allocator);
defer code_sig.deinit();
var buffer: [@sizeOf(macho.SuperBlob)]u8 = undefined;
code_sig.writeHeader(buffer[0..]);
const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 };
testing.expect(mem.eql(u8, expected[0..], buffer[0..]));
}
|