Commit 4e3520a2f7

Jakub Konka <kubkon@jakubkonka.com>
2020-11-17 09:57:35
stage2 macho: make page size target cpu arch dependent
1 parent 8450e6f
Changed files (1)
src
src/link/MachO.zig
@@ -73,6 +73,10 @@ const LoadCommand = union(enum) {
 
 base: File,
 
+/// Page size is dependent on the target cpu architecture.
+/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
+page_size: u16,
+
 /// Table of all load commands
 load_commands: std.ArrayListUnmanaged(LoadCommand) = .{},
 /// __PAGEZERO segment
@@ -301,6 +305,7 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
             .allocator = gpa,
             .file = null,
         },
+        .page_size = if (options.target.cpu.arch == .aarch64) 0x4000 else 0x1000,
     };
     return self;
 }
@@ -387,7 +392,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
         const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab;
         const file_size = symtab.stroff + symtab.strsize - linkedit.fileoff;
         linkedit.filesize = file_size;
-        linkedit.vmsize = mem.alignForwardGeneric(u64, file_size, 0x4000);
+        linkedit.vmsize = mem.alignForwardGeneric(u64, file_size, self.page_size);
     }
 
     if (self.cmd_table_dirty) {
@@ -1163,8 +1168,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {
 
         // const program_code_size_hint = self.base.options.program_code_size_hint;
         const program_code_size_hint = 128;
-        const file_size = mem.alignForwardGeneric(u64, program_code_size_hint, 0x4000);
-        const off = @intCast(u32, self.findFreeSpace(file_size, 0x4000)); // TODO maybe findFreeSpace should return u32 directly?
+        const file_size = mem.alignForwardGeneric(u64, program_code_size_hint, self.page_size);
+        const off = @intCast(u32, self.findFreeSpace(file_size, self.page_size)); // TODO maybe findFreeSpace should return u32 directly?
         const flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS;
 
         log.debug("found __text section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
@@ -1237,7 +1242,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
             .reserved3 = 0,
         });
 
-        const segment_size = mem.alignForwardGeneric(u64, file_size, 0x4000);
+        const segment_size = mem.alignForwardGeneric(u64, file_size, self.page_size);
         data_segment.vmsize = segment_size;
         data_segment.filesize = segment_size;
         data_segment.fileoff = off;