From 0def4e0db0a7000739a0bd409c51de377ce7b693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 8 Aug 2024 22:26:02 +0200 Subject: [PATCH 01/14] std.Target: Make some functions handle driverkit alongside other Apple OSs. Reference for versions: https://developer.apple.com/support/xcode --- lib/std/Target.zig | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 2ca02aa2d3..496c87b575 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -75,7 +75,13 @@ pub const Os = struct { pub inline fn isDarwin(tag: Tag) bool { return switch (tag) { - .ios, .macos, .watchos, .tvos, .visionos => true, + .driverkit, + .ios, + .macos, + .tvos, + .visionos, + .watchos, + => true, else => false, }; } @@ -116,7 +122,13 @@ pub const Os = struct { pub fn dynamicLibSuffix(tag: Tag) [:0]const u8 { return switch (tag) { .windows, .uefi => ".dll", - .ios, .macos, .watchos, .tvos, .visionos => ".dylib", + .driverkit, + .ios, + .macos, + .tvos, + .visionos, + .watchos, + => ".dylib", else => ".so", }; } @@ -163,7 +175,6 @@ pub const Os = struct { .hermit, .hurd, .emscripten, - .driverkit, .shadermodel, .uefi, .opencl, // TODO: OpenCL versions @@ -175,6 +186,7 @@ pub const Os = struct { .other, => .none, + .driverkit, .freebsd, .macos, .ios, @@ -392,7 +404,6 @@ pub const Os = struct { .hermit, .hurd, .emscripten, - .driverkit, .shadermodel, .uefi, .opencl, // TODO: OpenCL versions @@ -410,6 +421,12 @@ pub const Os = struct { .max = .{ .major = 14, .minor = 0, .patch = 0 }, }, }, + .driverkit => .{ + .semver = .{ + .min = .{ .major = 19, .minor = 0, .patch = 0 }, + .max = .{ .major = 24, .minor = 0, .patch = 0 }, + }, + }, .macos => switch (arch) { .aarch64 => VersionRange{ .semver = .{ @@ -555,6 +572,7 @@ pub const Os = struct { return switch (os.tag) { .freebsd, .netbsd, + .driverkit, .macos, .ios, .tvos, @@ -589,7 +607,6 @@ pub const Os = struct { .hurd, .wasi, .emscripten, - .driverkit, .shadermodel, .uefi, .opencl, @@ -1802,6 +1819,7 @@ pub const DynamicLinker = struct { => none, }, + .driverkit, .ios, .tvos, .watchos, @@ -1846,7 +1864,6 @@ pub const DynamicLinker = struct { .amdpal, .hermit, .hurd, - .driverkit, .shadermodel, => none, }; @@ -2261,7 +2278,13 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 { }, }, - .macos, .ios, .tvos, .watchos, .visionos => switch (c_type) { + .driverkit, + .ios, + .macos, + .tvos, + .visionos, + .watchos, + => switch (c_type) { .char => return 8, .short, .ushort => return 16, .int, .uint, .float => return 32, @@ -2334,7 +2357,6 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 { .hermit, .hurd, .opengl, - .driverkit, .shadermodel, => @panic("TODO specify the C integer and float type sizes for this OS"), } From fc93ab7182662bad6826b827e8bcf034fc90c3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 8 Aug 2024 22:26:55 +0200 Subject: [PATCH 02/14] std.Target: Fix isGnuLibC() to handle hurd too. --- lib/std/Target.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 496c87b575..458021db4a 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -144,7 +144,7 @@ pub const Os = struct { } pub inline fn isGnuLibC(tag: Os.Tag, abi: Abi) bool { - return tag == .linux and abi.isGnu(); + return (tag == .hurd or tag == .linux) and abi.isGnu(); } pub fn defaultVersionRange(tag: Tag, arch: Cpu.Arch) Os { From 1e67221f07705c124ef5d67769378d1acce95353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 8 Aug 2024 23:00:20 +0200 Subject: [PATCH 03/14] std.Target: Change requiresLibC() to return true for aix. AIX does not have a stable syscall interface; libc is required. --- lib/std/Target.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 458021db4a..5f5bbf5389 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -571,6 +571,7 @@ pub const Os = struct { pub fn requiresLibC(os: Os) bool { return switch (os.tag) { .freebsd, + .aix, .netbsd, .driverkit, .macos, @@ -593,7 +594,6 @@ pub const Os = struct { .ps3, .zos, .rtems, - .aix, .cuda, .nvcl, .amdhsa, From 9246c880313cd228e9c245242ca57315e00bb67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 19:39:50 +0200 Subject: [PATCH 04/14] std.Target: Clean up Arch.toElfMachine(). --- lib/std/Target.zig | 70 ++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 5f5bbf5389..a6621522f9 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1210,51 +1210,43 @@ pub const Cpu = struct { pub fn toElfMachine(arch: Arch) std.elf.EM { return switch (arch) { - .avr => .AVR, - .msp430 => .MSP430, + .amdgcn => .AMDGPU, .arc => .ARC, - .arm => .ARM, - .armeb => .ARM, + .arm, .armeb, .thumb, .thumbeb => .ARM, + .aarch64, .aarch64_be => .AARCH64, + .avr => .AVR, + .bpfel, .bpfeb => .BPF, + .csky => .CSKY, .hexagon => .HEXAGON, - .dxil => .NONE, - .m68k => .@"68K", - .mips => .MIPS, - .mipsel => .MIPS_RS3_LE, - .powerpc, .powerpcle => .PPC, - .riscv32 => .RISCV, - .sparc => .SPARC, - .thumb => .ARM, - .thumbeb => .ARM, - .x86 => .@"386", - .xcore => .XCORE, - .xtensa => .XTENSA, - .nvptx => .NONE, .kalimba => .CSR_KALIMBA, .lanai => .LANAI, - .wasm32 => .NONE, - .aarch64 => .AARCH64, - .aarch64_be => .AARCH64, - .mips64 => .MIPS, - .mips64el => .MIPS_RS3_LE, - .powerpc64 => .PPC64, - .powerpc64le => .PPC64, - .riscv64 => .RISCV, - .x86_64 => .X86_64, - .nvptx64 => .NONE, - .wasm64 => .NONE, - .amdgcn => .AMDGPU, - .bpfel => .BPF, - .bpfeb => .BPF, - .csky => .CSKY, - .sparc64 => .SPARCV9, + .loongarch32, .loongarch64 => .LOONGARCH, + .m68k => .@"68K", + .mips, .mips64 => .MIPS, + .mipsel, .mips64el => .MIPS_RS3_LE, + .msp430 => .MSP430, + .powerpc, .powerpcle => .PPC, + .powerpc64, .powerpc64le => .PPC64, + .riscv32, .riscv64 => .RISCV, .s390x => .S390, - .ve => .NONE, + .sparc => .SPARC, // TODO: Should be SPARC32PLUS when targeting 32-bit v9. + .sparc64 => .SPARCV9, .spu_2 => .SPU_2, - .spirv => .NONE, - .spirv32 => .NONE, - .spirv64 => .NONE, - .loongarch32 => .LOONGARCH, - .loongarch64 => .LOONGARCH, + .x86 => .@"386", + .x86_64 => .X86_64, + .xcore => .XCORE, + .xtensa => .XTENSA, + + .dxil, + .nvptx, + .nvptx64, + .spirv, + .spirv32, + .spirv64, + .ve, + .wasm32, + .wasm64, + => .NONE, }; } From 6c27cab5b32d634b414b0c416ccc11a0d89e83ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 19:57:09 +0200 Subject: [PATCH 05/14] std.Target: Fix Arch.toElfMachine() for arc. The arc tag means ARCv2. --- lib/std/Target.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index a6621522f9..946825b526 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1209,9 +1209,10 @@ pub const Cpu = struct { } pub fn toElfMachine(arch: Arch) std.elf.EM { + // TODO: Return IAMCU for elfiamcu OS. return switch (arch) { .amdgcn => .AMDGPU, - .arc => .ARC, + .arc => .ARC_COMPACT2, .arm, .armeb, .thumb, .thumbeb => .ARM, .aarch64, .aarch64_be => .AARCH64, .avr => .AVR, From 490b3281270b175d73bc146390e28ef6188e45a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 20:33:20 +0200 Subject: [PATCH 06/14] std.Target: Fix Arch.toElfMachine() for mips. EM_MIPS_RS3_LE is obsolete; all mips targets just use EM_MIPS. Also, fun fact: EM_MIPS_RS3_LE is actually big endian! --- lib/std/Target.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 946825b526..f2ce762e96 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1223,8 +1223,7 @@ pub const Cpu = struct { .lanai => .LANAI, .loongarch32, .loongarch64 => .LOONGARCH, .m68k => .@"68K", - .mips, .mips64 => .MIPS, - .mipsel, .mips64el => .MIPS_RS3_LE, + .mips, .mips64, .mipsel, .mips64el => .MIPS, .msp430 => .MSP430, .powerpc, .powerpcle => .PPC, .powerpc64, .powerpc64le => .PPC64, From 8516a6ab571238527e460423dac30671c6380880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 20:32:00 +0200 Subject: [PATCH 07/14] std.Target: Clean up Arch.toCoffMachine(). --- lib/std/Target.zig | 84 +++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index f2ce762e96..d10948f201 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1252,51 +1252,51 @@ pub const Cpu = struct { pub fn toCoffMachine(arch: Arch) std.coff.MachineType { return switch (arch) { - .avr => .Unknown, - .msp430 => .Unknown, - .arc => .Unknown, .arm => .ARM, - .armeb => .Unknown, - .dxil => .Unknown, - .hexagon => .Unknown, - .m68k => .Unknown, - .mips => .Unknown, - .mipsel => .Unknown, - .powerpc, .powerpcle => .POWERPC, - .riscv32 => .RISCV32, - .sparc => .Unknown, - .thumb => .Thumb, - .thumbeb => .Thumb, - .x86 => .I386, - .xcore => .Unknown, - .xtensa => .Unknown, - .nvptx => .Unknown, - .kalimba => .Unknown, - .lanai => .Unknown, - .wasm32 => .Unknown, - .aarch64 => .ARM64, - .aarch64_be => .ARM64, - .mips64 => .Unknown, - .mips64el => .Unknown, - .powerpc64 => .Unknown, - .powerpc64le => .Unknown, - .riscv64 => .RISCV64, - .x86_64 => .X64, - .nvptx64 => .Unknown, - .wasm64 => .Unknown, - .amdgcn => .Unknown, - .bpfel => .Unknown, - .bpfeb => .Unknown, - .csky => .Unknown, - .sparc64 => .Unknown, - .s390x => .Unknown, - .ve => .Unknown, - .spu_2 => .Unknown, - .spirv => .Unknown, - .spirv32 => .Unknown, - .spirv64 => .Unknown, + .thumb, .thumbeb => .Thumb, + .aarch64, .aarch64_be => .ARM64, .loongarch32 => .LOONGARCH32, .loongarch64 => .LOONGARCH64, + .powerpc, .powerpcle => .POWERPC, + .riscv32 => .RISCV32, + .riscv64 => .RISCV64, + .x86 => .I386, + .x86_64 => .X64, + + .amdgcn, + .arc, + .armeb, + .avr, + .bpfel, + .bpfeb, + .csky, + .dxil, + .hexagon, + .kalimba, + .lanai, + .m68k, + .mips, + .mipsel, + .mips64, + .mips64el, + .msp430, + .nvptx, + .nvptx64, + .powerpc64, + .powerpc64le, + .s390x, + .sparc, + .sparc64, + .spirv, + .spirv32, + .spirv64, + .spu_2, + .ve, + .wasm32, + .wasm64, + .xcore, + .xtensa, + => .Unknown, }; } From b24fc35eeb417e660f4fed017d45c534bd794469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 21:03:34 +0200 Subject: [PATCH 08/14] std.Target: Don't match big endian architectures in Arch.toCoffMachine(). All of these were mapping to types that are little endian. In fact, I can find no evidence that either Windows or UEFI have ever been used on big endian systems. --- lib/std/Target.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index d10948f201..4b680ee130 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1253,11 +1253,11 @@ pub const Cpu = struct { pub fn toCoffMachine(arch: Arch) std.coff.MachineType { return switch (arch) { .arm => .ARM, - .thumb, .thumbeb => .Thumb, - .aarch64, .aarch64_be => .ARM64, + .thumb => .Thumb, + .aarch64 => .ARM64, .loongarch32 => .LOONGARCH32, .loongarch64 => .LOONGARCH64, - .powerpc, .powerpcle => .POWERPC, + .powerpcle => .POWERPC, .riscv32 => .RISCV32, .riscv64 => .RISCV64, .x86 => .I386, @@ -1266,6 +1266,8 @@ pub const Cpu = struct { .amdgcn, .arc, .armeb, + .thumbeb, + .aarch64_be, .avr, .bpfel, .bpfeb, @@ -1282,6 +1284,7 @@ pub const Cpu = struct { .msp430, .nvptx, .nvptx64, + .powerpc, .powerpc64, .powerpc64le, .s390x, From 65affb1c225a034058fb2cbe32c289019b463ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 21:13:10 +0200 Subject: [PATCH 09/14] std.Target: Don't match PowerPC in Arch.toCoffMachine(). It's entirely unclear whether this should map to POWERPC or POWERPCFP, and as I can find no evidence of people producing PE files for PowerPC since Windows NT, let's just not make a likely-wrong guess. We can revisit this if the need ever actually arises. --- lib/std/Target.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 4b680ee130..79207fcf70 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1257,7 +1257,6 @@ pub const Cpu = struct { .aarch64 => .ARM64, .loongarch32 => .LOONGARCH32, .loongarch64 => .LOONGARCH64, - .powerpcle => .POWERPC, .riscv32 => .RISCV32, .riscv64 => .RISCV64, .x86 => .I386, @@ -1285,6 +1284,7 @@ pub const Cpu = struct { .nvptx, .nvptx64, .powerpc, + .powerpcle, .powerpc64, .powerpc64le, .s390x, From ac9ca7d30c03f3111b6bb0a0bacade97d1f6f6df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 20:53:37 +0200 Subject: [PATCH 10/14] std.coff: Remove MachineType.fromTargetCpuArch(). This does the same thing as std.Target.Cpu.Arch.toCoffMachine(). Just use that. --- lib/std/coff.zig | 17 ----------------- src/link/Coff.zig | 2 +- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/lib/std/coff.zig b/lib/std/coff.zig index bb7be2537d..818f980f6f 100644 --- a/lib/std/coff.zig +++ b/lib/std/coff.zig @@ -1061,23 +1061,6 @@ pub const MachineType = enum(u16) { _, - pub fn fromTargetCpuArch(arch: std.Target.Cpu.Arch) MachineType { - return switch (arch) { - .arm => .ARM, - .powerpc => .POWERPC, - .riscv32 => .RISCV32, - .thumb => .Thumb, - .x86 => .I386, - .aarch64 => .ARM64, - .riscv64 => .RISCV64, - .x86_64 => .X64, - .loongarch32 => .LOONGARCH32, - .loongarch64 => .LOONGARCH64, - // there's cases we don't (yet) handle - else => unreachable, - }; - } - pub fn toTargetCpuArch(machine_type: MachineType) ?std.Target.Cpu.Arch { return switch (machine_type) { .ARM => .arm, diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 73822dfec8..84fc4bb6ee 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -2259,7 +2259,7 @@ fn writeHeader(self: *Coff) !void { const timestamp = if (self.repro) 0 else std.time.timestamp(); const size_of_optional_header = @as(u16, @intCast(self.getOptionalHeaderSize() + self.getDataDirectoryHeadersSize())); var coff_header = coff.CoffHeader{ - .machine = coff.MachineType.fromTargetCpuArch(target.cpu.arch), + .machine = target.cpu.arch.toCoffMachine(), .number_of_sections = @as(u16, @intCast(self.sections.slice().len)), // TODO what if we prune a section .time_date_stamp = @as(u32, @truncate(@as(u64, @bitCast(timestamp)))), .pointer_to_symbol_table = self.strtab_offset orelse 0, From b01c595d4a0c4a643a5a276b3c7624449b73b301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 21:19:24 +0200 Subject: [PATCH 11/14] std.coff: Capitalize MachineType.{Unknown,Thumb} for consistency. --- lib/std/Target.zig | 4 ++-- lib/std/coff.zig | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 79207fcf70..ebb5b90d1d 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1253,7 +1253,7 @@ pub const Cpu = struct { pub fn toCoffMachine(arch: Arch) std.coff.MachineType { return switch (arch) { .arm => .ARM, - .thumb => .Thumb, + .thumb => .THUMB, .aarch64 => .ARM64, .loongarch32 => .LOONGARCH32, .loongarch64 => .LOONGARCH64, @@ -1299,7 +1299,7 @@ pub const Cpu = struct { .wasm64, .xcore, .xtensa, - => .Unknown, + => .UNKNOWN, }; } diff --git a/lib/std/coff.zig b/lib/std/coff.zig index 818f980f6f..ca05ce3cf9 100644 --- a/lib/std/coff.zig +++ b/lib/std/coff.zig @@ -983,7 +983,7 @@ pub const DebugInfoDefinition = struct { }; pub const MachineType = enum(u16) { - Unknown = 0x0, + UNKNOWN = 0x0, /// Alpha AXP, 32-bit address space ALPHA = 0x184, /// Alpha 64, 64-bit address space @@ -1053,7 +1053,7 @@ pub const MachineType = enum(u16) { /// Hitachi SH5 SH5 = 0x1a8, /// Thumb - Thumb = 0x1c2, + THUMB = 0x1c2, /// Infineon TRICORE = 0x520, /// MIPS little-endian WCE v2 @@ -1066,7 +1066,7 @@ pub const MachineType = enum(u16) { .ARM => .arm, .POWERPC => .powerpc, .RISCV32 => .riscv32, - .Thumb => .thumb, + .THUMB => .thumb, .I386 => .x86, .ARM64 => .aarch64, .RISCV64 => .riscv64, From 550438653d9d2b87edbd6f0d214b24d6fd03f706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 21:36:55 +0200 Subject: [PATCH 12/14] std.Target: Pull toCoffMachine()/toElfMachine() up from Arch to Target. This enables them to give more correct results. Contributes to #20771. --- lib/compiler/aro/backend/Object/Elf.zig | 2 +- lib/std/Target.zig | 190 ++++++++++++------------ src/link/Coff.zig | 2 +- src/link/Elf.zig | 2 +- 4 files changed, 98 insertions(+), 98 deletions(-) diff --git a/lib/compiler/aro/backend/Object/Elf.zig b/lib/compiler/aro/backend/Object/Elf.zig index a14830813f..2a303d348c 100644 --- a/lib/compiler/aro/backend/Object/Elf.zig +++ b/lib/compiler/aro/backend/Object/Elf.zig @@ -199,7 +199,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void { const elf_header = std.elf.Elf64_Ehdr{ .e_ident = .{ 0x7F, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .e_type = std.elf.ET.REL, // we only produce relocatables - .e_machine = elf.obj.target.cpu.arch.toElfMachine(), + .e_machine = elf.obj.target.toElfMachine(), .e_version = 1, .e_entry = 0, // linker will handle this .e_phoff = 0, // no program header diff --git a/lib/std/Target.zig b/lib/std/Target.zig index ebb5b90d1d..2234ca5af4 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -835,6 +835,101 @@ pub const ObjectFormat = enum { } }; +pub fn toElfMachine(target: Target) std.elf.EM { + // TODO: Return IAMCU for elfiamcu OS. + return switch (target.cpu.arch) { + .amdgcn => .AMDGPU, + .arc => .ARC_COMPACT2, + .arm, .armeb, .thumb, .thumbeb => .ARM, + .aarch64, .aarch64_be => .AARCH64, + .avr => .AVR, + .bpfel, .bpfeb => .BPF, + .csky => .CSKY, + .hexagon => .HEXAGON, + .kalimba => .CSR_KALIMBA, + .lanai => .LANAI, + .loongarch32, .loongarch64 => .LOONGARCH, + .m68k => .@"68K", + .mips, .mips64, .mipsel, .mips64el => .MIPS, + .msp430 => .MSP430, + .powerpc, .powerpcle => .PPC, + .powerpc64, .powerpc64le => .PPC64, + .riscv32, .riscv64 => .RISCV, + .s390x => .S390, + .sparc => .SPARC, // TODO: Should be SPARC32PLUS when targeting 32-bit v9. + .sparc64 => .SPARCV9, + .spu_2 => .SPU_2, + .x86 => .@"386", + .x86_64 => .X86_64, + .xcore => .XCORE, + .xtensa => .XTENSA, + + .dxil, + .nvptx, + .nvptx64, + .spirv, + .spirv32, + .spirv64, + .ve, + .wasm32, + .wasm64, + => .NONE, + }; +} + +pub fn toCoffMachine(target: Target) std.coff.MachineType { + return switch (target.cpu.arch) { + .arm => .ARM, + .thumb => .THUMB, + .aarch64 => .ARM64, + .loongarch32 => .LOONGARCH32, + .loongarch64 => .LOONGARCH64, + .riscv32 => .RISCV32, + .riscv64 => .RISCV64, + .x86 => .I386, + .x86_64 => .X64, + + .amdgcn, + .arc, + .armeb, + .thumbeb, + .aarch64_be, + .avr, + .bpfel, + .bpfeb, + .csky, + .dxil, + .hexagon, + .kalimba, + .lanai, + .m68k, + .mips, + .mipsel, + .mips64, + .mips64el, + .msp430, + .nvptx, + .nvptx64, + .powerpc, + .powerpcle, + .powerpc64, + .powerpc64le, + .s390x, + .sparc, + .sparc64, + .spirv, + .spirv32, + .spirv64, + .spu_2, + .ve, + .wasm32, + .wasm64, + .xcore, + .xtensa, + => .UNKNOWN, + }; +} + pub const SubSystem = enum { Console, Windows, @@ -1208,101 +1303,6 @@ pub const Cpu = struct { return error.UnknownCpuModel; } - pub fn toElfMachine(arch: Arch) std.elf.EM { - // TODO: Return IAMCU for elfiamcu OS. - return switch (arch) { - .amdgcn => .AMDGPU, - .arc => .ARC_COMPACT2, - .arm, .armeb, .thumb, .thumbeb => .ARM, - .aarch64, .aarch64_be => .AARCH64, - .avr => .AVR, - .bpfel, .bpfeb => .BPF, - .csky => .CSKY, - .hexagon => .HEXAGON, - .kalimba => .CSR_KALIMBA, - .lanai => .LANAI, - .loongarch32, .loongarch64 => .LOONGARCH, - .m68k => .@"68K", - .mips, .mips64, .mipsel, .mips64el => .MIPS, - .msp430 => .MSP430, - .powerpc, .powerpcle => .PPC, - .powerpc64, .powerpc64le => .PPC64, - .riscv32, .riscv64 => .RISCV, - .s390x => .S390, - .sparc => .SPARC, // TODO: Should be SPARC32PLUS when targeting 32-bit v9. - .sparc64 => .SPARCV9, - .spu_2 => .SPU_2, - .x86 => .@"386", - .x86_64 => .X86_64, - .xcore => .XCORE, - .xtensa => .XTENSA, - - .dxil, - .nvptx, - .nvptx64, - .spirv, - .spirv32, - .spirv64, - .ve, - .wasm32, - .wasm64, - => .NONE, - }; - } - - pub fn toCoffMachine(arch: Arch) std.coff.MachineType { - return switch (arch) { - .arm => .ARM, - .thumb => .THUMB, - .aarch64 => .ARM64, - .loongarch32 => .LOONGARCH32, - .loongarch64 => .LOONGARCH64, - .riscv32 => .RISCV32, - .riscv64 => .RISCV64, - .x86 => .I386, - .x86_64 => .X64, - - .amdgcn, - .arc, - .armeb, - .thumbeb, - .aarch64_be, - .avr, - .bpfel, - .bpfeb, - .csky, - .dxil, - .hexagon, - .kalimba, - .lanai, - .m68k, - .mips, - .mipsel, - .mips64, - .mips64el, - .msp430, - .nvptx, - .nvptx64, - .powerpc, - .powerpcle, - .powerpc64, - .powerpc64le, - .s390x, - .sparc, - .sparc64, - .spirv, - .spirv32, - .spirv64, - .spu_2, - .ve, - .wasm32, - .wasm64, - .xcore, - .xtensa, - => .UNKNOWN, - }; - } - pub fn endian(arch: Arch) std.builtin.Endian { return switch (arch) { .avr, diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 84fc4bb6ee..aaa9a9b5cd 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -2259,7 +2259,7 @@ fn writeHeader(self: *Coff) !void { const timestamp = if (self.repro) 0 else std.time.timestamp(); const size_of_optional_header = @as(u16, @intCast(self.getOptionalHeaderSize() + self.getDataDirectoryHeadersSize())); var coff_header = coff.CoffHeader{ - .machine = target.cpu.arch.toCoffMachine(), + .machine = target.toCoffMachine(), .number_of_sections = @as(u16, @intCast(self.sections.slice().len)), // TODO what if we prune a section .time_date_stamp = @as(u32, @truncate(@as(u64, @bitCast(timestamp)))), .pointer_to_symbol_table = self.strtab_offset orelse 0, diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 103c69202b..140b8a6978 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -2831,7 +2831,7 @@ pub fn writeElfHeader(self: *Elf) !void { mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(elf_type), endian); index += 2; - const machine = target.cpu.arch.toElfMachine(); + const machine = target.toElfMachine(); mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(machine), endian); index += 2; From 9b5c992d31cc92aa869fcf1f954db44fbdd0f07f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 21:40:02 +0200 Subject: [PATCH 13/14] std.Target: Return the correct value for elfiamcu in toElfMachine(). --- lib/std/Target.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 2234ca5af4..a5799c3898 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -836,7 +836,8 @@ pub const ObjectFormat = enum { }; pub fn toElfMachine(target: Target) std.elf.EM { - // TODO: Return IAMCU for elfiamcu OS. + if (target.os.tag == .elfiamcu) return .IAMCU; + return switch (target.cpu.arch) { .amdgcn => .AMDGPU, .arc => .ARC_COMPACT2, From 6db9ad7798a112fa54e72c890a5386d25de3bb67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 9 Aug 2024 21:40:21 +0200 Subject: [PATCH 14/14] std.Target: Return the correct value for 32-bit sparc v9 in toElfMachine(). --- lib/std/Target.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index a5799c3898..a231892e6e 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -857,7 +857,7 @@ pub fn toElfMachine(target: Target) std.elf.EM { .powerpc64, .powerpc64le => .PPC64, .riscv32, .riscv64 => .RISCV, .s390x => .S390, - .sparc => .SPARC, // TODO: Should be SPARC32PLUS when targeting 32-bit v9. + .sparc => if (Target.sparc.featureSetHas(target.cpu.features, .v9)) .SPARC32PLUS else .SPARC, .sparc64 => .SPARCV9, .spu_2 => .SPU_2, .x86 => .@"386",