Commit 700b1e38ce

Jakub Konka <kubkon@jakubkonka.com>
2023-08-27 23:04:27
macho: fix overalignment of stubs on aarch64
1 parent 0353bfd
Changed files (3)
src/link/MachO/stubs.zig
@@ -25,7 +25,7 @@ pub inline fn stubSize(cpu_arch: std.Target.Cpu.Arch) u8 {
 pub inline fn stubAlignment(cpu_arch: std.Target.Cpu.Arch) u8 {
     return switch (cpu_arch) {
         .x86_64 => 1,
-        .aarch64 => 2,
+        .aarch64 => 4,
         else => unreachable, // unhandled architecture type
     };
 }
src/link/MachO/zld.zig
@@ -1032,14 +1032,14 @@ fn calcSectionSizes(macho_file: *MachO) !void {
     if (macho_file.stubs_section_index) |sect_id| {
         const header = &macho_file.sections.items(.header)[sect_id];
         header.size = macho_file.stub_table.count() * stubs.stubSize(cpu_arch);
-        header.@"align" = stubs.stubAlignment(cpu_arch);
+        header.@"align" = math.log2(stubs.stubAlignment(cpu_arch));
     }
 
     if (macho_file.stub_helper_section_index) |sect_id| {
         const header = &macho_file.sections.items(.header)[sect_id];
         header.size = macho_file.stub_table.count() * stubs.stubHelperSize(cpu_arch) +
             stubs.stubHelperPreambleSize(cpu_arch);
-        header.@"align" = stubs.stubAlignment(cpu_arch);
+        header.@"align" = math.log2(stubs.stubAlignment(cpu_arch));
     }
 
     if (macho_file.la_symbol_ptr_section_index) |sect_id| {
src/link/MachO.zig
@@ -2912,11 +2912,7 @@ fn populateMissingMetadata(self: *MachO) !void {
     if (self.stub_helper_section_index == null) {
         self.stub_helper_section_index = try self.allocateSection("__TEXT3", "__stub_helper", .{
             .size = @sizeOf(u32),
-            .alignment = switch (cpu_arch) {
-                .x86_64 => 1,
-                .aarch64 => @sizeOf(u32),
-                else => unreachable, // unhandled architecture type
-            },
+            .alignment = stubs.stubAlignment(cpu_arch),
             .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
             .prot = macho.PROT.READ | macho.PROT.EXEC,
         });