authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-20 11:50:44+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-26 11:50:09+01:00
log9dcf7ee9c904b25fbffd264cabeb6638e9d9e2d6
treeb15b3c0bcc0708e44e8009b15a4c0dc72f723a6a
parent79381dc4fb1f7eb92b5e01d21f866f550ca3ff20

stage2 macho: add info about __TEXT segment


3 files changed, 17 insertions(+), 11 deletions(-)

lib/std/macho.zig+4-4
...@@ -1402,6 +1402,10 @@ pub const CS_SIGNER_TYPE_UNKNOWN: u32 = 0;...@@ -1402,6 +1402,10 @@ pub const CS_SIGNER_TYPE_UNKNOWN: u32 = 0;
1402pub const CS_SIGNER_TYPE_LEGACYVPN: u32 = 5;1402pub const CS_SIGNER_TYPE_LEGACYVPN: u32 = 5;
1403pub const CS_SIGNER_TYPE_MAC_APP_STORE: u32 = 6;1403pub const CS_SIGNER_TYPE_MAC_APP_STORE: u32 = 6;
14041404
1405pub const CS_ADHOC: u32 = 0x2;
1406
1407pub const CS_EXECSEG_MAIN_BINARY: u32 = 0x1;
1408
1405/// This CodeDirectory is tailored specfically at version 0x20400.1409/// This CodeDirectory is tailored specfically at version 0x20400.
1406pub const CodeDirectory = extern struct {1410pub const CodeDirectory = extern struct {
1407 /// Magic number (CSMAGIC_CODEDIRECTORY)1411 /// Magic number (CSMAGIC_CODEDIRECTORY)
...@@ -1488,8 +1492,6 @@ pub const SuperBlob = extern struct {...@@ -1488,8 +1492,6 @@ pub const SuperBlob = extern struct {
14881492
1489 /// Number of index BlobIndex entries following this struct1493 /// Number of index BlobIndex entries following this struct
1490 count: u32,1494 count: u32,
1491
1492 // index: []const BlobIndex,
1493};1495};
14941496
1495pub const GenericBlob = extern struct {1497pub const GenericBlob = extern struct {
...@@ -1498,6 +1500,4 @@ pub const GenericBlob = extern struct {...@@ -1498,6 +1500,4 @@ pub const GenericBlob = extern struct {
14981500
1499 /// Total length of blob1501 /// Total length of blob
1500 length: u32,1502 length: u32,
1501
1502 // data: []const u8,
1503};1503};
src/link/MachO.zig+1-1
...@@ -1758,7 +1758,7 @@ fn writecodeSignature(self: *MachO) !void {...@@ -1758,7 +1758,7 @@ fn writecodeSignature(self: *MachO) !void {
1758 var code_sig = CodeSignature.init(self.base.allocator);1758 var code_sig = CodeSignature.init(self.base.allocator);
1759 defer code_sig.deinit();1759 defer code_sig.deinit();
17601760
1761 try code_sig.calcAdhocSignature();1761 try code_sig.calcAdhocSignature(self);
17621762
1763 var buffer = try self.base.allocator.alloc(u8, code_sig.size());1763 var buffer = try self.base.allocator.alloc(u8, code_sig.size());
1764 defer self.base.allocator.free(buffer);1764 defer self.base.allocator.free(buffer);
src/link/MachO/CodeSignature.zig+12-6
...@@ -8,6 +8,8 @@ const mem = std.mem;...@@ -8,6 +8,8 @@ const mem = std.mem;
8const testing = std.testing;8const testing = std.testing;
9const Allocator = mem.Allocator;9const Allocator = mem.Allocator;
1010
11const MachO = @import("../MachO.zig");
12
11const Blob = struct {13const Blob = struct {
12 inner: macho.CodeDirectory,14 inner: macho.CodeDirectory,
13 data: std.ArrayListUnmanaged(u8) = .{},15 data: std.ArrayListUnmanaged(u8) = .{},
...@@ -56,13 +58,17 @@ pub fn init(alloc: *Allocator) CodeSignature {...@@ -56,13 +58,17 @@ pub fn init(alloc: *Allocator) CodeSignature {
56 };58 };
57}59}
5860
59pub fn calcAdhocSignature(self: *CodeSignature) !void {61pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
62 const text_segment = bin_file.load_commands.items[bin_file.text_segment_cmd_index.?].Segment;
63 const execSegBase: u64 = text_segment.fileoff;
64 const execSegLimit: u64 = text_segment.filesize;
65 const execSegFlags: u64 = text_segment.flags;
60 var blob = Blob{66 var blob = Blob{
61 .inner = .{67 .inner = .{
62 .magic = macho.CSMAGIC_CODEDIRECTORY,68 .magic = macho.CSMAGIC_CODEDIRECTORY,
63 .length = @sizeOf(macho.CodeDirectory),69 .length = @sizeOf(macho.CodeDirectory),
64 .version = 0x20400,70 .version = macho.CS_SUPPORTSEXECSEG,
65 .flags = 0,71 .flags = macho.CS_ADHOC,
66 .hashOffset = 0,72 .hashOffset = 0,
67 .identOffset = 0,73 .identOffset = 0,
68 .nSpecialSlots = 0,74 .nSpecialSlots = 0,
...@@ -77,9 +83,9 @@ pub fn calcAdhocSignature(self: *CodeSignature) !void {...@@ -77,9 +83,9 @@ pub fn calcAdhocSignature(self: *CodeSignature) !void {
77 .teamOffset = 0,83 .teamOffset = 0,
78 .spare3 = 0,84 .spare3 = 0,
79 .codeLimit64 = 0,85 .codeLimit64 = 0,
80 .execSegBase = 0,86 .execSegBase = execSegBase,
81 .execSegLimit = 0,87 .execSegLimit = execSegLimit,
82 .execSegFlags = 0,88 .execSegFlags = execSegFlags,
83 },89 },
84 };90 };
85 self.inner.length += @sizeOf(macho.BlobIndex) + blob.size();91 self.inner.length += @sizeOf(macho.BlobIndex) + blob.size();