Commit 270fbbcd86

Alex Rønne Petersen <alex@alexrp.com>
2024-10-16 04:36:40
std.Target: Add muslabin32 and muslabi64 tags to Abi.
Once we upgrade to LLVM 20, these should be lowered verbatim rather than to simply musl. Similarly, the special case in llvmMachineAbi() should go away.
1 parent 8045268
Changed files (10)
lib/compiler/aro/aro/target.zig
@@ -437,9 +437,18 @@ pub fn ldEmulationOption(target: std.Target, arm_endianness: ?std.builtin.Endian
         .loongarch64 => "elf64loongarch",
         .mips => "elf32btsmip",
         .mipsel => "elf32ltsmip",
-        .mips64 => if (target.abi == .gnuabin32) "elf32btsmipn32" else "elf64btsmip",
-        .mips64el => if (target.abi == .gnuabin32) "elf32ltsmipn32" else "elf64ltsmip",
-        .x86_64 => if (target.abi == .gnux32 or target.abi == .muslx32) "elf32_x86_64" else "elf_x86_64",
+        .mips64 => switch (target.abi) {
+            .gnuabin32, .muslabin32 => "elf32btsmipn32",
+            else => "elf64btsmip",
+        },
+        .mips64el => switch (target.abi) {
+            .gnuabin32, .muslabin32 => "elf32ltsmipn32",
+            else => "elf64ltsmip",
+        },
+        .x86_64 => switch (target.abi) {
+            .gnux32, .muslx32 => "elf32_x86_64",
+            else => "elf_x86_64",
+        },
         .ve => "elf64ve",
         .csky => "cskyelf_linux",
         else => null,
@@ -691,6 +700,8 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
         .android => "android",
         .androideabi => "androideabi",
         .musl => "musl",
+        .muslabin32 => "muslabin32",
+        .muslabi64 => "muslabi64",
         .musleabi => "musleabi",
         .musleabihf => "musleabihf",
         .muslx32 => "muslx32",
lib/std/os/linux.zig
@@ -136,10 +136,10 @@ pub const SYS = switch (@import("builtin").cpu.arch) {
     .loongarch64 => syscalls.LoongArch64,
     .m68k => syscalls.M68k,
     .mips, .mipsel => syscalls.MipsO32,
-    .mips64, .mips64el => if (builtin.abi == .gnuabin32)
-        syscalls.MipsN32
-    else
-        syscalls.MipsN64,
+    .mips64, .mips64el => switch (builtin.abi) {
+        .gnuabin32, .muslabin32 => syscalls.MipsN32,
+        else => syscalls.MipsN64,
+    },
     .powerpc, .powerpcle => syscalls.PowerPC,
     .powerpc64, .powerpc64le => syscalls.PowerPC64,
     .s390x => syscalls.S390x,
@@ -8657,11 +8657,11 @@ pub const AUDIT = struct {
             .mips => .MIPS,
             .mipsel => .MIPSEL,
             .mips64 => switch (native_abi) {
-                .gnuabin32 => .MIPS64N32,
+                .gnuabin32, .muslabin32 => .MIPS64N32,
                 else => .MIPS64,
             },
             .mips64el => switch (native_abi) {
-                .gnuabin32 => .MIPSEL64N32,
+                .gnuabin32, .muslabin32 => .MIPSEL64N32,
                 else => .MIPSEL64,
             },
             .powerpc => .PPC,
lib/std/zig/LibCDirs.zig
@@ -232,6 +232,8 @@ fn libCGenericName(target: std.Target) [:0]const u8 {
         .gnuilp32,
         => return "glibc",
         .musl,
+        .muslabin32,
+        .muslabi64,
         .musleabi,
         .musleabihf,
         .muslx32,
lib/std/zig/system.zig
@@ -91,16 +91,16 @@ pub fn getExternalExecutor(
             .mips => Executor{ .qemu = "qemu-mips" },
             .mipsel => Executor{ .qemu = "qemu-mipsel" },
             .mips64 => Executor{
-                .qemu = if (candidate.abi == .gnuabin32)
-                    "qemu-mipsn32"
-                else
-                    "qemu-mips64",
+                .qemu = switch (candidate.abi) {
+                    .gnuabin32, .muslabin32 => "qemu-mipsn32",
+                    else => "qemu-mips64",
+                },
             },
             .mips64el => Executor{
-                .qemu = if (candidate.abi == .gnuabin32)
-                    "qemu-mipsn32el"
-                else
-                    "qemu-mips64el",
+                .qemu = switch (candidate.abi) {
+                    .gnuabin32, .muslabin32 => "qemu-mipsn32el",
+                    else => "qemu-mips64el",
+                },
             },
             .powerpc => Executor{ .qemu = "qemu-ppc" },
             .powerpc64 => Executor{ .qemu = "qemu-ppc64" },
lib/std/zig/target.zig
@@ -99,6 +99,7 @@ pub fn canBuildLibC(target: std.Target) bool {
 
 pub fn muslArchName(arch: std.Target.Cpu.Arch, abi: std.Target.Abi) [:0]const u8 {
     return switch (abi) {
+        .muslabin32 => "mipsn32",
         .muslx32 => "x32",
         else => switch (arch) {
             .arm, .armeb, .thumb, .thumbeb => "arm",
@@ -129,6 +130,7 @@ pub fn muslArchNameHeaders(arch: std.Target.Cpu.Arch) [:0]const u8 {
 
 pub fn muslAbiNameHeaders(abi: std.Target.Abi) [:0]const u8 {
     return switch (abi) {
+        .muslabin32 => "mipsn32",
         .muslx32 => "muslx32",
         else => "musl",
     };
lib/std/Target.zig
@@ -761,6 +761,8 @@ pub const Abi = enum {
     android,
     androideabi,
     musl,
+    muslabin32,
+    muslabi64,
     musleabi,
     musleabihf,
     muslx32,
@@ -931,6 +933,8 @@ pub const Abi = enum {
     pub inline fn isMusl(abi: Abi) bool {
         return switch (abi) {
             .musl,
+            .muslabin32,
+            .muslabi64,
             .musleabi,
             .musleabihf,
             .muslx32,
@@ -2287,9 +2291,9 @@ pub const DynamicLinker = struct {
                     .mips64,
                     .mips64el,
                     => |arch| initFmt("/lib/ld-musl-mips{s}{s}{s}.so.1", .{
-                        // TODO: `n32` ABI support in LLVM 20.
                         switch (abi) {
-                            .musl => "64",
+                            .muslabi64 => "64",
+                            .muslabin32 => "n32",
                             else => return none,
                         },
                         if (mips.featureSetHas(cpu.features, .mips64r6)) "r6" else "",
@@ -2584,8 +2588,8 @@ pub fn standardDynamicLinkerPath(target: Target) DynamicLinker {
 
 pub fn ptrBitWidth_cpu_abi(cpu: Cpu, abi: Abi) u16 {
     switch (abi) {
-        .gnux32, .muslx32, .gnuabin32, .gnuilp32, .ilp32 => return 32,
-        .gnuabi64 => return 64,
+        .gnux32, .muslx32, .gnuabin32, .muslabin32, .gnuilp32, .ilp32 => return 32,
+        .gnuabi64, .muslabi64 => return 64,
         else => {},
     }
     return switch (cpu.arch) {
@@ -2788,7 +2792,10 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
                 .char => return 8,
                 .short, .ushort => return 16,
                 .int, .uint, .float => return 32,
-                .long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
+                .long, .ulong => switch (target.abi) {
+                    .gnuabin32, .muslabin32 => return 32,
+                    else => return 64,
+                },
                 .longlong, .ulonglong, .double => return 64,
                 .longdouble => return 128,
             },
@@ -2821,6 +2828,8 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
                     .powerpc64le,
                     => switch (target.abi) {
                         .musl,
+                        .muslabin32,
+                        .muslabi64,
                         .musleabi,
                         .musleabihf,
                         .muslx32,
@@ -2876,7 +2885,10 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
                 .char => return 8,
                 .short, .ushort => return 16,
                 .int, .uint, .float => return 32,
-                .long, .ulong => return if (target.abi != .gnuabin32) 64 else 32,
+                .long, .ulong => switch (target.abi) {
+                    .gnuabin32, .muslabin32 => return 32,
+                    else => return 64,
+                },
                 .longlong, .ulonglong, .double => return 64,
                 .longdouble => if (target.os.tag == .freebsd) return 64 else return 128,
             },
@@ -2907,6 +2919,8 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
                     .powerpcle,
                     => switch (target.abi) {
                         .musl,
+                        .muslabin32,
+                        .muslabi64,
                         .musleabi,
                         .musleabihf,
                         .muslx32,
@@ -2921,6 +2935,8 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
                     .powerpc64le,
                     => switch (target.abi) {
                         .musl,
+                        .muslabin32,
+                        .muslabi64,
                         .musleabi,
                         .musleabihf,
                         .muslx32,
src/codegen/llvm.zig
@@ -270,6 +270,8 @@ pub fn targetTriple(allocator: Allocator, target: std.Target) ![]const u8 {
         .android => "android",
         .androideabi => "androideabi",
         .musl => "musl",
+        .muslabin32 => "musl", // Should be muslabin32 in LLVM 20.
+        .muslabi64 => "musl", // Should be muslabi64 in LLVM 20.
         .musleabi => "musleabi",
         .musleabihf => "musleabihf",
         .muslx32 => "muslx32",
src/link/Elf.zig
@@ -4100,21 +4100,21 @@ fn getLDMOption(target: std.Target) ?[]const u8 {
         },
         .mips64 => switch (target.os.tag) {
             .freebsd => switch (target.abi) {
-                .gnuabin32 => "elf32btsmipn32_fbsd",
+                .gnuabin32, .muslabin32 => "elf32btsmipn32_fbsd",
                 else => "elf64btsmip_fbsd",
             },
             else => switch (target.abi) {
-                .gnuabin32 => "elf32btsmipn32",
+                .gnuabin32, .muslabin32 => "elf32btsmipn32",
                 else => "elf64btsmip",
             },
         },
         .mips64el => switch (target.os.tag) {
             .freebsd => switch (target.abi) {
-                .gnuabin32 => "elf32ltsmipn32_fbsd",
+                .gnuabin32, .muslabin32 => "elf32ltsmipn32_fbsd",
                 else => "elf64ltsmip_fbsd",
             },
             else => switch (target.abi) {
-                .gnuabin32 => "elf32ltsmipn32",
+                .gnuabin32, .muslabin32 => "elf32ltsmipn32",
                 else => "elf64ltsmip",
             },
         },
src/target.zig
@@ -438,6 +438,16 @@ pub fn arePointersLogical(target: std.Target, as: AddressSpace) bool {
 }
 
 pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
+    // This special-casing should be removed with LLVM 20.
+    switch (target.cpu.arch) {
+        .mips, .mipsel => return "o32",
+        .mips64, .mips64el => return switch (target.abi) {
+            .gnuabin32, .muslabin32 => "n32",
+            else => "n64",
+        },
+        else => {},
+    }
+
     // LLD does not support ELFv1. Rather than having LLVM produce ELFv1 code and then linking it
     // into a broken ELFv2 binary, just force LLVM to use ELFv2 as well. This will break when glibc
     // is linked as glibc only supports ELFv2 for little endian, but there's nothing we can do about
test/llvm_targets.zig
@@ -161,6 +161,8 @@ const targets = [_]std.Target.Query{
     .{ .cpu_arch = .mips64, .os_tag = .linux, .abi = .gnuabi64 },
     .{ .cpu_arch = .mips64, .os_tag = .linux, .abi = .gnuabin32 },
     .{ .cpu_arch = .mips64, .os_tag = .linux, .abi = .musl },
+    .{ .cpu_arch = .mips64, .os_tag = .linux, .abi = .muslabi64 },
+    .{ .cpu_arch = .mips64, .os_tag = .linux, .abi = .muslabin32 },
     .{ .cpu_arch = .mips64, .os_tag = .linux, .abi = .none },
     .{ .cpu_arch = .mips64, .os_tag = .netbsd, .abi = .none },
     .{ .cpu_arch = .mips64, .os_tag = .openbsd, .abi = .none },
@@ -170,6 +172,8 @@ const targets = [_]std.Target.Query{
     .{ .cpu_arch = .mips64el, .os_tag = .linux, .abi = .gnuabi64 },
     .{ .cpu_arch = .mips64el, .os_tag = .linux, .abi = .gnuabin32 },
     .{ .cpu_arch = .mips64el, .os_tag = .linux, .abi = .musl },
+    .{ .cpu_arch = .mips64el, .os_tag = .linux, .abi = .muslabi64 },
+    .{ .cpu_arch = .mips64el, .os_tag = .linux, .abi = .muslabin32 },
     .{ .cpu_arch = .mips64el, .os_tag = .linux, .abi = .none },
     .{ .cpu_arch = .mips64el, .os_tag = .netbsd, .abi = .none },
     .{ .cpu_arch = .mips64el, .os_tag = .openbsd, .abi = .none },