Commit 9dcf7ee9c9

Jakub Konka <kubkon@jakubkonka.com>
2020-11-20 11:50:44
stage2 macho: add info about __TEXT segment
1 parent 79381dc
Changed files (3)
lib/std/macho.zig
@@ -1402,6 +1402,10 @@ pub const CS_SIGNER_TYPE_UNKNOWN: u32 = 0;
 pub const CS_SIGNER_TYPE_LEGACYVPN: u32 = 5;
 pub const CS_SIGNER_TYPE_MAC_APP_STORE: u32 = 6;
 
+pub const CS_ADHOC: u32 = 0x2;
+
+pub const CS_EXECSEG_MAIN_BINARY: u32 = 0x1;
+
 /// This CodeDirectory is tailored specfically at version 0x20400.
 pub const CodeDirectory = extern struct {
     /// Magic number (CSMAGIC_CODEDIRECTORY)
@@ -1488,8 +1492,6 @@ pub const SuperBlob = extern struct {
 
     /// Number of index BlobIndex entries following this struct
     count: u32,
-
-    // index: []const BlobIndex,
 };
 
 pub const GenericBlob = extern struct {
@@ -1498,6 +1500,4 @@ pub const GenericBlob = extern struct {
 
     /// Total length of blob
     length: u32,
-
-    // data: []const u8,
 };
src/link/MachO/CodeSignature.zig
@@ -8,6 +8,8 @@ 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) = .{},
@@ -56,13 +58,17 @@ pub fn init(alloc: *Allocator) CodeSignature {
     };
 }
 
-pub fn calcAdhocSignature(self: *CodeSignature) !void {
+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 = 0x20400,
-            .flags = 0,
+            .version = macho.CS_SUPPORTSEXECSEG,
+            .flags = macho.CS_ADHOC,
             .hashOffset = 0,
             .identOffset = 0,
             .nSpecialSlots = 0,
@@ -77,9 +83,9 @@ pub fn calcAdhocSignature(self: *CodeSignature) !void {
             .teamOffset = 0,
             .spare3 = 0,
             .codeLimit64 = 0,
-            .execSegBase = 0,
-            .execSegLimit = 0,
-            .execSegFlags = 0,
+            .execSegBase = execSegBase,
+            .execSegLimit = execSegLimit,
+            .execSegFlags = execSegFlags,
         },
     };
     self.inner.length += @sizeOf(macho.BlobIndex) + blob.size();
src/link/MachO.zig
@@ -1758,7 +1758,7 @@ fn writecodeSignature(self: *MachO) !void {
     var code_sig = CodeSignature.init(self.base.allocator);
     defer code_sig.deinit();
 
-    try code_sig.calcAdhocSignature();
+    try code_sig.calcAdhocSignature(self);
 
     var buffer = try self.base.allocator.alloc(u8, code_sig.size());
     defer self.base.allocator.free(buffer);