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 {
199199 const elf_header = std.elf.Elf64_Ehdr{
200200 .e_ident = .{ 0x7F, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
201201 .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(),
203203 .e_version = 1,
204204 .e_entry = 0, // linker will handle this
205205 .e_phoff = 0, // no program header
lib/std/Target.zig+128-110
......@@ -75,7 +75,13 @@ pub const Os = struct {
7575
7676 pub inline fn isDarwin(tag: Tag) bool {
7777 return switch (tag) {
78 .ios, .macos, .watchos, .tvos, .visionos => true,
78 .driverkit,
79 .ios,
80 .macos,
81 .tvos,
82 .visionos,
83 .watchos,
84 => true,
7985 else => false,
8086 };
8187 }
......@@ -116,7 +122,13 @@ pub const Os = struct {
116122 pub fn dynamicLibSuffix(tag: Tag) [:0]const u8 {
117123 return switch (tag) {
118124 .windows, .uefi => ".dll",
119 .ios, .macos, .watchos, .tvos, .visionos => ".dylib",
125 .driverkit,
126 .ios,
127 .macos,
128 .tvos,
129 .visionos,
130 .watchos,
131 => ".dylib",
120132 else => ".so",
121133 };
122134 }
......@@ -132,7 +144,7 @@ pub const Os = struct {
132144 }
133145
134146 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();
136148 }
137149
138150 pub fn defaultVersionRange(tag: Tag, arch: Cpu.Arch) Os {
......@@ -163,7 +175,6 @@ pub const Os = struct {
163175 .hermit,
164176 .hurd,
165177 .emscripten,
166 .driverkit,
167178 .shadermodel,
168179 .uefi,
169180 .opencl, // TODO: OpenCL versions
......@@ -175,6 +186,7 @@ pub const Os = struct {
175186 .other,
176187 => .none,
177188
189 .driverkit,
178190 .freebsd,
179191 .macos,
180192 .ios,
......@@ -392,7 +404,6 @@ pub const Os = struct {
392404 .hermit,
393405 .hurd,
394406 .emscripten,
395 .driverkit,
396407 .shadermodel,
397408 .uefi,
398409 .opencl, // TODO: OpenCL versions
......@@ -410,6 +421,12 @@ pub const Os = struct {
410421 .max = .{ .major = 14, .minor = 0, .patch = 0 },
411422 },
412423 },
424 .driverkit => .{
425 .semver = .{
426 .min = .{ .major = 19, .minor = 0, .patch = 0 },
427 .max = .{ .major = 24, .minor = 0, .patch = 0 },
428 },
429 },
413430 .macos => switch (arch) {
414431 .aarch64 => VersionRange{
415432 .semver = .{
......@@ -554,7 +571,9 @@ pub const Os = struct {
554571 pub fn requiresLibC(os: Os) bool {
555572 return switch (os.tag) {
556573 .freebsd,
574 .aix,
557575 .netbsd,
576 .driverkit,
558577 .macos,
559578 .ios,
560579 .tvos,
......@@ -575,7 +594,6 @@ pub const Os = struct {
575594 .ps3,
576595 .zos,
577596 .rtems,
578 .aix,
579597 .cuda,
580598 .nvcl,
581599 .amdhsa,
......@@ -589,7 +607,6 @@ pub const Os = struct {
589607 .hurd,
590608 .wasi,
591609 .emscripten,
592 .driverkit,
593610 .shadermodel,
594611 .uefi,
595612 .opencl,
......@@ -818,6 +835,102 @@ pub const ObjectFormat = enum {
818835 }
819836};
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
821934pub const SubSystem = enum {
822935 Console,
823936 Windows,
......@@ -1191,106 +1304,6 @@ pub const Cpu = struct {
11911304 return error.UnknownCpuModel;
11921305 }
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
12941307 pub fn endian(arch: Arch) std.builtin.Endian {
12951308 return switch (arch) {
12961309 .avr,
......@@ -1802,6 +1815,7 @@ pub const DynamicLinker = struct {
18021815 => none,
18031816 },
18041817
1818 .driverkit,
18051819 .ios,
18061820 .tvos,
18071821 .watchos,
......@@ -1846,7 +1860,6 @@ pub const DynamicLinker = struct {
18461860 .amdpal,
18471861 .hermit,
18481862 .hurd,
1849 .driverkit,
18501863 .shadermodel,
18511864 => none,
18521865 };
......@@ -2261,7 +2274,13 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
22612274 },
22622275 },
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) {
22652284 .char => return 8,
22662285 .short, .ushort => return 16,
22672286 .int, .uint, .float => return 32,
......@@ -2334,7 +2353,6 @@ pub fn cTypeBitSize(target: Target, c_type: CType) u16 {
23342353 .hermit,
23352354 .hurd,
23362355 .opengl,
2337 .driverkit,
23382356 .shadermodel,
23392357 => @panic("TODO specify the C integer and float type sizes for this OS"),
23402358 }
lib/std/coff.zig+3-20
......@@ -983,7 +983,7 @@ pub const DebugInfoDefinition = struct {
983983};
984984
985985pub const MachineType = enum(u16) {
986 Unknown = 0x0,
986 UNKNOWN = 0x0,
987987 /// Alpha AXP, 32-bit address space
988988 ALPHA = 0x184,
989989 /// Alpha 64, 64-bit address space
......@@ -1053,7 +1053,7 @@ pub const MachineType = enum(u16) {
10531053 /// Hitachi SH5
10541054 SH5 = 0x1a8,
10551055 /// Thumb
1056 Thumb = 0x1c2,
1056 THUMB = 0x1c2,
10571057 /// Infineon
10581058 TRICORE = 0x520,
10591059 /// MIPS little-endian WCE v2
......@@ -1061,29 +1061,12 @@ pub const MachineType = enum(u16) {
10611061
10621062 _,
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
10811064 pub fn toTargetCpuArch(machine_type: MachineType) ?std.Target.Cpu.Arch {
10821065 return switch (machine_type) {
10831066 .ARM => .arm,
10841067 .POWERPC => .powerpc,
10851068 .RISCV32 => .riscv32,
1086 .Thumb => .thumb,
1069 .THUMB => .thumb,
10871070 .I386 => .x86,
10881071 .ARM64 => .aarch64,
10891072 .RISCV64 => .riscv64,
src/link/Coff.zig+1-1
......@@ -2259,7 +2259,7 @@ fn writeHeader(self: *Coff) !void {
22592259 const timestamp = if (self.repro) 0 else std.time.timestamp();
22602260 const size_of_optional_header = @as(u16, @intCast(self.getOptionalHeaderSize() + self.getDataDirectoryHeadersSize()));
22612261 var coff_header = coff.CoffHeader{
2262 .machine = coff.MachineType.fromTargetCpuArch(target.cpu.arch),
2262 .machine = target.toCoffMachine(),
22632263 .number_of_sections = @as(u16, @intCast(self.sections.slice().len)), // TODO what if we prune a section
22642264 .time_date_stamp = @as(u32, @truncate(@as(u64, @bitCast(timestamp)))),
22652265 .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 {
28312831 mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(elf_type), endian);
28322832 index += 2;
28332833
2834 const machine = target.cpu.arch.toElfMachine();
2834 const machine = target.toElfMachine();
28352835 mem.writeInt(u16, hdr_buf[index..][0..2], @intFromEnum(machine), endian);
28362836 index += 2;
28372837