authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-15 22:13:31-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-15 22:13:31-07:00
log05c7968920c6a74debe407c2ea8c23cd60d89611
treef03fef3e85a85dc347843cacfdcabf7bffe71b0b
parentb917d778c6e8fd24c420a18ecf3f55aab3f93798
parent6db9ad7798a112fa54e72c890a5386d25de3bb67
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21020 from alexrp/target-fixes

`std.Target`: Assorted corrections, plus cleanup around ELF/COFF machine types

5 files changed, 134 insertions(+), 133 deletions(-)

lib/compiler/aro/backend/Object/Elf.zig+1-1
...@@ -199,7 +199,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {...@@ -199,7 +199,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
199 const elf_header = std.elf.Elf64_Ehdr{199 const elf_header = std.elf.Elf64_Ehdr{
200 .e_ident = .{ 0x7F, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },200 .e_ident = .{ 0x7F, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
201 .e_type = std.elf.ET.REL, // we only produce relocatables201 .e_type = std.elf.ET.REL, // we only produce relocatables
202 .e_machine = elf.obj.target.cpu.arch.toElfMachine(),202 .e_machine = elf.obj.target.toElfMachine(),
203 .e_version = 1,203 .e_version = 1,
204 .e_entry = 0, // linker will handle this204 .e_entry = 0, // linker will handle this
205 .e_phoff = 0, // no program header205 .e_phoff = 0, // no program header
lib/std/Target.zig+128-110
...@@ -75,7 +75,13 @@ pub const Os = struct {...@@ -75,7 +75,13 @@ pub const Os = struct {
7575
76 pub inline fn isDarwin(tag: Tag) bool {76 pub inline fn isDarwin(tag: Tag) bool {
77 return switch (tag) {77 return switch (tag) {
78 .ios, .macos, .watchos, .tvos, .visionos => true,78 .driverkit,
79 .ios,
80 .macos,
81 .tvos,
82 .visionos,
83 .watchos,
84 => true,
79 else => false,85 else => false,
80 };86 };
81 }87 }
...@@ -116,7 +122,13 @@ pub const Os = struct {...@@ -116,7 +122,13 @@ pub const Os = struct {
116 pub fn dynamicLibSuffix(tag: Tag) [:0]const u8 {122 pub fn dynamicLibSuffix(tag: Tag) [:0]const u8 {
117 return switch (tag) {123 return switch (tag) {
118 .windows, .uefi => ".dll",124 .windows, .uefi => ".dll",
119 .ios, .macos, .watchos, .tvos, .visionos => ".dylib",125 .driverkit,
126 .ios,
127 .macos,
128 .tvos,
129 .visionos,
130 .watchos,
131 => ".dylib",
120 else => ".so",132 else => ".so",
121 };133 };
122 }134 }
...@@ -132,7 +144,7 @@ pub const Os = struct {...@@ -132,7 +144,7 @@ pub const Os = struct {
132 }144 }
133145
134 pub inline fn isGnuLibC(tag: Os.Tag, abi: Abi) bool {146 pub inline fn isGnuLibC(tag: Os.Tag, abi: Abi) bool {
135 return tag == .linux and abi.isGnu();147 return (tag == .hurd or tag == .linux) and abi.isGnu();
136 }148 }
137149
138 pub fn defaultVersionRange(tag: Tag, arch: Cpu.Arch) Os {150 pub fn defaultVersionRange(tag: Tag, arch: Cpu.Arch) Os {
...@@ -163,7 +175,6 @@ pub const Os = struct {...@@ -163,7 +175,6 @@ pub const Os = struct {
163 .hermit,175 .hermit,
164 .hurd,176 .hurd,
165 .emscripten,177 .emscripten,
166 .driverkit,
167 .shadermodel,178 .shadermodel,
168 .uefi,179 .uefi,
169 .opencl, // TODO: OpenCL versions180 .opencl, // TODO: OpenCL versions
...@@ -175,6 +186,7 @@ pub const Os = struct {...@@ -175,6 +186,7 @@ pub const Os = struct {
175 .other,186 .other,
176 => .none,187 => .none,
177188
189 .driverkit,
178 .freebsd,190 .freebsd,
179 .macos,191 .macos,
180 .ios,192 .ios,
...@@ -392,7 +404,6 @@ pub const Os = struct {...@@ -392,7 +404,6 @@ pub const Os = struct {
392 .hermit,404 .hermit,
393 .hurd,405 .hurd,
394 .emscripten,406 .emscripten,
395 .driverkit,
396 .shadermodel,407 .shadermodel,
397 .uefi,408 .uefi,
398 .opencl, // TODO: OpenCL versions409 .opencl, // TODO: OpenCL versions
...@@ -410,6 +421,12 @@ pub const Os = struct {...@@ -410,6 +421,12 @@ pub const Os = struct {
410 .max = .{ .major = 14, .minor = 0, .patch = 0 },421 .max = .{ .major = 14, .minor = 0, .patch = 0 },
411 },422 },
412 },423 },
424 .driverkit => .{
425 .semver = .{
426 .min = .{ .major = 19, .minor = 0, .patch = 0 },
427 .max = .{ .major = 24, .minor = 0, .patch = 0 },
428 },
429 },
413 .macos => switch (arch) {430 .macos => switch (arch) {
414 .aarch64 => VersionRange{431 .aarch64 => VersionRange{
415 .semver = .{432 .semver = .{
...@@ -554,7 +571,9 @@ pub const Os = struct {...@@ -554,7 +571,9 @@ pub const Os = struct {
554 pub fn requiresLibC(os: Os) bool {571 pub fn requiresLibC(os: Os) bool {
555 return switch (os.tag) {572 return switch (os.tag) {
556 .freebsd,573 .freebsd,
574 .aix,
557 .netbsd,575 .netbsd,
576 .driverkit,
558 .macos,577 .macos,
559 .ios,578 .ios,
560 .tvos,579 .tvos,
...@@ -575,7 +594,6 @@ pub const Os = struct {...@@ -575,7 +594,6 @@ pub const Os = struct {
575 .ps3,594 .ps3,
576 .zos,595 .zos,
577 .rtems,596 .rtems,
578 .aix,
579 .cuda,597 .cuda,
580 .nvcl,598 .nvcl,
581 .amdhsa,599 .amdhsa,
...@@ -589,7 +607,6 @@ pub const Os = struct {...@@ -589,7 +607,6 @@ pub const Os = struct {
589 .hurd,607 .hurd,
590 .wasi,608 .wasi,
591 .emscripten,609 .emscripten,
592 .driverkit,
593 .shadermodel,610 .shadermodel,
594 .uefi,611 .uefi,
595 .opencl,612 .opencl,
...@@ -818,6 +835,102 @@ pub const ObjectFormat = enum {...@@ -818,6 +835,102 @@ pub const ObjectFormat = enum {
818 }835 }
819};836};
820837
838pub fn toElfMachine(target: Target) std.elf.EM {
839 if (target.os.tag == .elfiamcu) return .IAMCU;
840
841 return switch (target.cpu.arch) {
842 .amdgcn => .AMDGPU,
843 .arc => .ARC_COMPACT2,
844 .arm, .armeb, .thumb, .thumbeb => .ARM,
845 .aarch64, .aarch64_be => .AARCH64,
846 .avr => .AVR,
847 .bpfel, .bpfeb => .BPF,
848 .csky => .CSKY,
849 .hexagon => .HEXAGON,
850 .kalimba => .CSR_KALIMBA,
851 .lanai => .LANAI,
852 .loongarch32, .loongarch64 => .LOONGARCH,
853 .m68k => .@"68K",
854 .mips, .mips64, .mipsel, .mips64el => .MIPS,
855 .msp430 => .MSP430,
856 .powerpc, .powerpcle => .PPC,
857 .powerpc64, .powerpc64le => .PPC64,
858 .riscv32, .riscv64 => .RISCV,
859 .s390x => .S390,
860 .sparc => if (Target.sparc.featureSetHas(target.cpu.features, .v9)) .SPARC32PLUS else .SPARC,
861 .sparc64 => .SPARCV9,
862 .spu_2 => .SPU_2,
863 .x86 => .@"386",
864 .x86_64 => .X86_64,
865 .xcore => .XCORE,
866 .xtensa => .XTENSA,
867
868 .dxil,
869 .nvptx,
870 .nvptx64,
871 .spirv,
872 .spirv32,
873 .spirv64,
874 .ve,
875 .wasm32,
876 .wasm64,
877 => .NONE,
878 };
879}
880
881pub fn toCoffMachine(target: Target) std.coff.MachineType {
882 return switch (target.cpu.arch) {
883 .arm => .ARM,
884 .thumb => .THUMB,
885 .aarch64 => .ARM64,
886 .loongarch32 => .LOONGARCH32,
887 .loongarch64 => .LOONGARCH64,
888 .riscv32 => .RISCV32,
889 .riscv64 => .RISCV64,
890 .x86 => .I386,
891 .x86_64 => .X64,
892
893 .amdgcn,
894 .arc,
895 .armeb,
896 .thumbeb,
897 .aarch64_be,
898 .avr,
899 .bpfel,
900 .bpfeb,
901 .csky,
902 .dxil,
903 .hexagon,
904 .kalimba,
905 .lanai,
906 .m68k,
907 .mips,
908 .mipsel,
909 .mips64,
910 .mips64el,
911 .msp430,
912 .nvptx,
913 .nvptx64,
914 .powerpc,
915 .powerpcle,
916 .powerpc64,
917 .powerpc64le,
918 .s390x,
919 .sparc,
920 .sparc64,
921 .spirv,
922 .spirv32,
923 .spirv64,
924 .spu_2,
925 .ve,
926 .wasm32,
927 .wasm64,
928 .xcore,
929 .xtensa,
930 => .UNKNOWN,
931 };
932}
933
821pub const SubSystem = enum {934pub const SubSystem = enum {
822 Console,935 Console,
823 Windows,936 Windows,
...@@ -1191,106 +1304,6 @@ pub const Cpu = struct {...@@ -1191,106 +1304,6 @@ pub const Cpu = struct {
1191 return error.UnknownCpuModel;1304 return error.UnknownCpuModel;
1192 }1305 }
11931306
1194 pub fn toElfMachine(arch: Arch) std.elf.EM {
1195 return switch (arch) {
1196 .avr => .AVR,
1197 .msp430 => .MSP430,
1198 .arc => .ARC,
1199 .arm => .ARM,
1200 .armeb => .ARM,
1201 .hexagon => .HEXAGON,
1202 .dxil => .NONE,
1203 .m68k => .@"68K",
1204 .mips => .MIPS,
1205 .mipsel => .MIPS_RS3_LE,
1206 .powerpc, .powerpcle => .PPC,
1207 .riscv32 => .RISCV,
1208 .sparc => .SPARC,
1209 .thumb => .ARM,
1210 .thumbeb => .ARM,
1211 .x86 => .@"386",
1212 .xcore => .XCORE,
1213 .xtensa => .XTENSA,
1214 .nvptx => .NONE,
1215 .kalimba => .CSR_KALIMBA,
1216 .lanai => .LANAI,
1217 .wasm32 => .NONE,
1218 .aarch64 => .AARCH64,
1219 .aarch64_be => .AARCH64,
1220 .mips64 => .MIPS,
1221 .mips64el => .MIPS_RS3_LE,
1222 .powerpc64 => .PPC64,
1223 .powerpc64le => .PPC64,
1224 .riscv64 => .RISCV,
1225 .x86_64 => .X86_64,
1226 .nvptx64 => .NONE,
1227 .wasm64 => .NONE,
1228 .amdgcn => .AMDGPU,
1229 .bpfel => .BPF,
1230 .bpfeb => .BPF,
1231 .csky => .CSKY,
1232 .sparc64 => .SPARCV9,
1233 .s390x => .S390,
1234 .ve => .NONE,
1235 .spu_2 => .SPU_2,
1236 .spirv => .NONE,
1237 .spirv32 => .NONE,
1238 .spirv64 => .NONE,
1239 .loongarch32 => .LOONGARCH,
1240 .loongarch64 => .LOONGARCH,
1241 };
1242 }
1243
1244 pub fn toCoffMachine(arch: Arch) std.coff.MachineType {
1245 return switch (arch) {
1246 .avr => .Unknown,
1247 .msp430 => .Unknown,
1248 .arc => .Unknown,
1249 .arm => .ARM,
1250 .armeb => .Unknown,
1251 .dxil => .Unknown,
1252 .hexagon => .Unknown,
1253 .m68k => .Unknown,
1254 .mips => .Unknown,
1255 .mipsel => .Unknown,
1256 .powerpc, .powerpcle => .POWERPC,
1257 .riscv32 => .RISCV32,
1258 .sparc => .Unknown,
1259 .thumb => .Thumb,
1260 .thumbeb => .Thumb,
1261 .x86 => .I386,
1262 .xcore => .Unknown,
1263 .xtensa => .Unknown,
1264 .nvptx => .Unknown,
1265 .kalimba => .Unknown,
1266 .lanai => .Unknown,
1267 .wasm32 => .Unknown,
1268 .aarch64 => .ARM64,
1269 .aarch64_be => .ARM64,
1270 .mips64 => .Unknown,
1271 .mips64el => .Unknown,
1272 .powerpc64 => .Unknown,
1273 .powerpc64le => .Unknown,
1274 .riscv64 => .RISCV64,
1275 .x86_64 => .X64,
1276 .nvptx64 => .Unknown,
1277 .wasm64 => .Unknown,
1278 .amdgcn => .Unknown,
1279 .bpfel => .Unknown,
1280 .bpfeb => .Unknown,
1281 .csky => .Unknown,
1282 .sparc64 => .Unknown,
1283 .s390x => .Unknown,
1284 .ve => .Unknown,
1285 .spu_2 => .Unknown,
1286 .spirv => .Unknown,
1287 .spirv32 => .Unknown,
1288 .spirv64 => .Unknown,
1289 .loongarch32 => .LOONGARCH32,
1290 .loongarch64 => .LOONGARCH64,
1291 };
1292 }
1293
1294 pub fn endian(arch: Arch) std.builtin.Endian {1307 pub fn endian(arch: Arch) std.builtin.Endian {
1295 return switch (arch) {1308 return switch (arch) {
1296 .avr,1309 .avr,
...@@ -1802,6 +1815,7 @@ pub const DynamicLinker = struct {...@@ -1802,6 +1815,7 @@ pub const DynamicLinker = struct {
1802 => none,1815 => none,
1803 },1816 },
18041817
1818 .driverkit,
1805 .ios,1819 .ios,
1806 .tvos,1820 .tvos,
1807 .watchos,1821 .watchos,
...@@ -1846,7 +1860,6 @@ pub const DynamicLinker = struct {...@@ -1846,7 +1860,6 @@ pub const DynamicLinker = struct {
1846 .amdpal,1860 .amdpal,
1847 .hermit,1861 .hermit,
1848 .hurd,1862 .hurd,
1849 .driverkit,
1850 .shadermodel,1863 .shadermodel,
1851 => none,1864 => none,
1852 };1865 };
...@@ -2261,7 +2274,13 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {...@@ -2261,7 +2274,13 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
2261 },2274 },
2262 },2275 },
22632276
2264 .macos, .ios, .tvos, .watchos, .visionos => switch (c_type) {2277 .driverkit,
2278 .ios,
2279 .macos,
2280 .tvos,
2281 .visionos,
2282 .watchos,
2283 => switch (c_type) {
2265 .char => return 8,2284 .char => return 8,
2266 .short, .ushort => return 16,2285 .short, .ushort => return 16,
2267 .int, .uint, .float => return 32,2286 .int, .uint, .float => return 32,
...@@ -2334,7 +2353,6 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {...@@ -2334,7 +2353,6 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
2334 .hermit,2353 .hermit,
2335 .hurd,2354 .hurd,
2336 .opengl,2355 .opengl,
2337 .driverkit,
2338 .shadermodel,2356 .shadermodel,
2339 => @panic("TODO specify the C integer and float type sizes for this OS"),2357 => @panic("TODO specify the C integer and float type sizes for this OS"),
2340 }2358 }
lib/std/coff.zig+3-20
...@@ -983,7 +983,7 @@ pub const DebugInfoDefinition = struct {...@@ -983,7 +983,7 @@ pub const DebugInfoDefinition = struct {
983};983};
984984
985pub const MachineType = enum(u16) {985pub const MachineType = enum(u16) {
986 Unknown = 0x0,986 UNKNOWN = 0x0,
987 /// Alpha AXP, 32-bit address space987 /// Alpha AXP, 32-bit address space
988 ALPHA = 0x184,988 ALPHA = 0x184,
989 /// Alpha 64, 64-bit address space989 /// Alpha 64, 64-bit address space
...@@ -1053,7 +1053,7 @@ pub const MachineType = enum(u16) {...@@ -1053,7 +1053,7 @@ pub const MachineType = enum(u16) {
1053 /// Hitachi SH51053 /// Hitachi SH5
1054 SH5 = 0x1a8,1054 SH5 = 0x1a8,
1055 /// Thumb1055 /// Thumb
1056 Thumb = 0x1c2,1056 THUMB = 0x1c2,
1057 /// Infineon1057 /// Infineon
1058 TRICORE = 0x520,1058 TRICORE = 0x520,
1059 /// MIPS little-endian WCE v21059 /// MIPS little-endian WCE v2
...@@ -1061,29 +1061,12 @@ pub const MachineType = enum(u16) {...@@ -1061,29 +1061,12 @@ pub const MachineType = enum(u16) {
10611061
1062 _,1062 _,
10631063
1064 pub fn fromTargetCpuArch(arch: std.Target.Cpu.Arch) MachineType {
1065 return switch (arch) {
1066 .arm => .ARM,
1067 .powerpc => .POWERPC,
1068 .riscv32 => .RISCV32,
1069 .thumb => .Thumb,
1070 .x86 => .I386,
1071 .aarch64 => .ARM64,
1072 .riscv64 => .RISCV64,
1073 .x86_64 => .X64,
1074 .loongarch32 => .LOONGARCH32,
1075 .loongarch64 => .LOONGARCH64,
1076 // there's cases we don't (yet) handle
1077 else => unreachable,
1078 };
1079 }
1080
1081 pub fn toTargetCpuArch(machine_type: MachineType) ?std.Target.Cpu.Arch {1064 pub fn toTargetCpuArch(machine_type: MachineType) ?std.Target.Cpu.Arch {
1082 return switch (machine_type) {1065 return switch (machine_type) {
1083 .ARM => .arm,1066 .ARM => .arm,
1084 .POWERPC => .powerpc,1067 .POWERPC => .powerpc,
1085 .RISCV32 => .riscv32,1068 .RISCV32 => .riscv32,
1086 .Thumb => .thumb,1069 .THUMB => .thumb,
1087 .I386 => .x86,1070 .I386 => .x86,
1088 .ARM64 => .aarch64,1071 .ARM64 => .aarch64,
1089 .RISCV64 => .riscv64,1072 .RISCV64 => .riscv64,
src/link/Coff.zig+1-1
...@@ -2259,7 +2259,7 @@ fn writeHeader(self: *Coff) !void {...@@ -2259,7 +2259,7 @@ fn writeHeader(self: *Coff) !void {
2259 const timestamp = if (self.repro) 0 else std.time.timestamp();2259 const timestamp = if (self.repro) 0 else std.time.timestamp();
2260 const size_of_optional_header = @as(u16, @intCast(self.getOptionalHeaderSize() + self.getDataDirectoryHeadersSize()));2260 const size_of_optional_header = @as(u16, @intCast(self.getOptionalHeaderSize() + self.getDataDirectoryHeadersSize()));
2261 var coff_header = coff.CoffHeader{2261 var coff_header = coff.CoffHeader{
2262 .machine = coff.MachineType.fromTargetCpuArch(target.cpu.arch),2262 .machine = target.toCoffMachine(),
2263 .number_of_sections = @as(u16, @intCast(self.sections.slice().len)), // TODO what if we prune a section2263 .number_of_sections = @as(u16, @intCast(self.sections.slice().len)), // TODO what if we prune a section
2264 .time_date_stamp = @as(u32, @truncate(@as(u64, @bitCast(timestamp)))),2264 .time_date_stamp = @as(u32, @truncate(@as(u64, @bitCast(timestamp)))),
2265 .pointer_to_symbol_table = self.strtab_offset orelse 0,2265 .pointer_to_symbol_table = self.strtab_offset orelse 0,
src/link/Elf.zig+1-1
...@@ -2831,7 +2831,7 @@ pub fn writeElfHeader(self: *Elf) !void {...@@ -2831,7 +2831,7 @@ pub fn writeElfHeader(self: *Elf) !void {
2831 mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(elf_type), endian);2831 mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(elf_type), endian);
2832 index += 2;2832 index += 2;
28332833
2834 const machine = target.cpu.arch.toElfMachine();2834 const machine = target.toElfMachine();
2835 mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(machine), endian);2835 mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(machine), endian);
2836 index += 2;2836 index += 2;
28372837