Skip to content
/ zig Public
forked from ziglang/zig

Commit

Permalink
std.Target: Add muslabin32 and muslabi64 tags to Abi.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexrp committed Nov 2, 2024
1 parent 8045268 commit 270fbbc
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 27 deletions.
17 changes: 14 additions & 3 deletions lib/compiler/aro/aro/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 22 additions & 6 deletions lib/std/Target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ pub const Abi = enum {
android,
androideabi,
musl,
muslabin32,
muslabi64,
musleabi,
musleabihf,
muslx32,
Expand Down Expand Up @@ -931,6 +933,8 @@ pub const Abi = enum {
pub inline fn isMusl(abi: Abi) bool {
return switch (abi) {
.musl,
.muslabin32,
.muslabi64,
.musleabi,
.musleabihf,
.muslx32,
Expand Down Expand Up @@ -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 "",
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -2821,6 +2828,8 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
.powerpc64le,
=> switch (target.abi) {
.musl,
.muslabin32,
.muslabi64,
.musleabi,
.musleabihf,
.muslx32,
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -2907,6 +2919,8 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
.powerpcle,
=> switch (target.abi) {
.musl,
.muslabin32,
.muslabi64,
.musleabi,
.musleabihf,
.muslx32,
Expand All @@ -2921,6 +2935,8 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
.powerpc64le,
=> switch (target.abi) {
.musl,
.muslabin32,
.muslabi64,
.musleabi,
.musleabihf,
.muslx32,
Expand Down
12 changes: 6 additions & 6 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions lib/std/zig/LibCDirs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ fn libCGenericName(target: std.Target) [:0]const u8 {
.gnuilp32,
=> return "glibc",
.musl,
.muslabin32,
.muslabi64,
.musleabi,
.musleabihf,
.muslx32,
Expand Down
16 changes: 8 additions & 8 deletions lib/std/zig/system.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
2 changes: 2 additions & 0 deletions lib/std/zig/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
};
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/link/Elf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand Down
10 changes: 10 additions & 0 deletions src/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/llvm_targets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
Expand Down

0 comments on commit 270fbbc

Please sign in to comment.