Commit bff54536ff

Jakub Konka <kubkon@jakubkonka.com>
2023-09-03 08:18:29
macho: check if we should emit LC_VERSION_MIN_ if target low enough
1 parent 5cc1831
Changed files (2)
src/link/MachO/load_commands.zig
@@ -76,13 +76,16 @@ fn calcLCsSize(gpa: Allocator, options: *const link.Options, ctx: CalcLCsSizeCtx
     }
     // LC_SOURCE_VERSION
     sizeofcmds += @sizeOf(macho.source_version_command);
-    // LC_BUILD_VERSION or LC_VERSION_MIN_
-    if (Platform.fromTarget(options.target).isBuildVersionCompatible()) {
-        // LC_BUILD_VERSION
-        sizeofcmds += @sizeOf(macho.build_version_command) + @sizeOf(macho.build_tool_version);
-    } else {
-        // LC_VERSION_MIN_
-        sizeofcmds += @sizeOf(macho.version_min_command);
+    // LC_BUILD_VERSION or LC_VERSION_MIN_ or nothing
+    {
+        const platform = Platform.fromTarget(options.target);
+        if (platform.isBuildVersionCompatible()) {
+            // LC_BUILD_VERSION
+            sizeofcmds += @sizeOf(macho.build_version_command) + @sizeOf(macho.build_tool_version);
+        } else if (platform.isVersionMinCompatible()) {
+            // LC_VERSION_MIN_
+            sizeofcmds += @sizeOf(macho.version_min_command);
+        }
     }
     // LC_UUID
     sizeofcmds += @sizeOf(macho.uuid_command);
@@ -384,6 +387,15 @@ pub const Platform = struct {
         return false;
     }
 
+    pub fn isVersionMinCompatible(plat: Platform) bool {
+        inline for (supported_platforms) |sup_plat| {
+            if (sup_plat[0] == plat.os_tag and sup_plat[1] == plat.abi) {
+                return sup_plat[3] <= plat.toAppleVersion();
+            }
+        }
+        return false;
+    }
+
     pub fn fmtTarget(plat: Platform, cpu_arch: std.Target.Cpu.Arch) std.fmt.Formatter(formatTarget) {
         return .{ .data = .{ .platform = plat, .cpu_arch = cpu_arch } };
     }
src/link/MachO.zig
@@ -564,7 +564,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
             null;
         if (platform.isBuildVersionCompatible()) {
             try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer);
-        } else {
+        } else if (platform.isVersionMinCompatible()) {
             try load_commands.writeVersionMinLC(platform, sdk_version, lc_writer);
         }
     }