authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-09 21:36:55+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-15 10:55:46+02:00
log550438653d9d2b87edbd6f0d214b24d6fd03f706
tree89e30399bcd9ba7c88e0051cca62d14c3027ffe4
parentb01c595d4a0c4a643a5a276b3c7624449b73b301
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.Target: Pull toCoffMachine()/toElfMachine() up from Arch to Target.

This enables them to give more correct results. Contributes to #20771.

4 files changed, 98 insertions(+), 98 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+95-95
...@@ -835,6 +835,101 @@ pub const ObjectFormat = enum {...@@ -835,6 +835,101 @@ pub const ObjectFormat = enum {
835 }835 }
836};836};
837837
838pub fn toElfMachine(target: Target) std.elf.EM {
839 // TODO: Return IAMCU for elfiamcu OS.
840 return switch (target.cpu.arch) {
841 .amdgcn => .AMDGPU,
842 .arc => .ARC_COMPACT2,
843 .arm, .armeb, .thumb, .thumbeb => .ARM,
844 .aarch64, .aarch64_be => .AARCH64,
845 .avr => .AVR,
846 .bpfel, .bpfeb => .BPF,
847 .csky => .CSKY,
848 .hexagon => .HEXAGON,
849 .kalimba => .CSR_KALIMBA,
850 .lanai => .LANAI,
851 .loongarch32, .loongarch64 => .LOONGARCH,
852 .m68k => .@"68K",
853 .mips, .mips64, .mipsel, .mips64el => .MIPS,
854 .msp430 => .MSP430,
855 .powerpc, .powerpcle => .PPC,
856 .powerpc64, .powerpc64le => .PPC64,
857 .riscv32, .riscv64 => .RISCV,
858 .s390x => .S390,
859 .sparc => .SPARC, // TODO: Should be SPARC32PLUS when targeting 32-bit v9.
860 .sparc64 => .SPARCV9,
861 .spu_2 => .SPU_2,
862 .x86 => .@"386",
863 .x86_64 => .X86_64,
864 .xcore => .XCORE,
865 .xtensa => .XTENSA,
866
867 .dxil,
868 .nvptx,
869 .nvptx64,
870 .spirv,
871 .spirv32,
872 .spirv64,
873 .ve,
874 .wasm32,
875 .wasm64,
876 => .NONE,
877 };
878}
879
880pub fn toCoffMachine(target: Target) std.coff.MachineType {
881 return switch (target.cpu.arch) {
882 .arm => .ARM,
883 .thumb => .THUMB,
884 .aarch64 => .ARM64,
885 .loongarch32 => .LOONGARCH32,
886 .loongarch64 => .LOONGARCH64,
887 .riscv32 => .RISCV32,
888 .riscv64 => .RISCV64,
889 .x86 => .I386,
890 .x86_64 => .X64,
891
892 .amdgcn,
893 .arc,
894 .armeb,
895 .thumbeb,
896 .aarch64_be,
897 .avr,
898 .bpfel,
899 .bpfeb,
900 .csky,
901 .dxil,
902 .hexagon,
903 .kalimba,
904 .lanai,
905 .m68k,
906 .mips,
907 .mipsel,
908 .mips64,
909 .mips64el,
910 .msp430,
911 .nvptx,
912 .nvptx64,
913 .powerpc,
914 .powerpcle,
915 .powerpc64,
916 .powerpc64le,
917 .s390x,
918 .sparc,
919 .sparc64,
920 .spirv,
921 .spirv32,
922 .spirv64,
923 .spu_2,
924 .ve,
925 .wasm32,
926 .wasm64,
927 .xcore,
928 .xtensa,
929 => .UNKNOWN,
930 };
931}
932
838pub const SubSystem = enum {933pub const SubSystem = enum {
839 Console,934 Console,
840 Windows,935 Windows,
...@@ -1208,101 +1303,6 @@ pub const Cpu = struct {...@@ -1208,101 +1303,6 @@ pub const Cpu = struct {
1208 return error.UnknownCpuModel;1303 return error.UnknownCpuModel;
1209 }1304 }
12101305
1211 pub fn toElfMachine(arch: Arch) std.elf.EM {
1212 // TODO: Return IAMCU for elfiamcu OS.
1213 return switch (arch) {
1214 .amdgcn => .AMDGPU,
1215 .arc => .ARC_COMPACT2,
1216 .arm, .armeb, .thumb, .thumbeb => .ARM,
1217 .aarch64, .aarch64_be => .AARCH64,
1218 .avr => .AVR,
1219 .bpfel, .bpfeb => .BPF,
1220 .csky => .CSKY,
1221 .hexagon => .HEXAGON,
1222 .kalimba => .CSR_KALIMBA,
1223 .lanai => .LANAI,
1224 .loongarch32, .loongarch64 => .LOONGARCH,
1225 .m68k => .@"68K",
1226 .mips, .mips64, .mipsel, .mips64el => .MIPS,
1227 .msp430 => .MSP430,
1228 .powerpc, .powerpcle => .PPC,
1229 .powerpc64, .powerpc64le => .PPC64,
1230 .riscv32, .riscv64 => .RISCV,
1231 .s390x => .S390,
1232 .sparc => .SPARC, // TODO: Should be SPARC32PLUS when targeting 32-bit v9.
1233 .sparc64 => .SPARCV9,
1234 .spu_2 => .SPU_2,
1235 .x86 => .@"386",
1236 .x86_64 => .X86_64,
1237 .xcore => .XCORE,
1238 .xtensa => .XTENSA,
1239
1240 .dxil,
1241 .nvptx,
1242 .nvptx64,
1243 .spirv,
1244 .spirv32,
1245 .spirv64,
1246 .ve,
1247 .wasm32,
1248 .wasm64,
1249 => .NONE,
1250 };
1251 }
1252
1253 pub fn toCoffMachine(arch: Arch) std.coff.MachineType {
1254 return switch (arch) {
1255 .arm => .ARM,
1256 .thumb => .THUMB,
1257 .aarch64 => .ARM64,
1258 .loongarch32 => .LOONGARCH32,
1259 .loongarch64 => .LOONGARCH64,
1260 .riscv32 => .RISCV32,
1261 .riscv64 => .RISCV64,
1262 .x86 => .I386,
1263 .x86_64 => .X64,
1264
1265 .amdgcn,
1266 .arc,
1267 .armeb,
1268 .thumbeb,
1269 .aarch64_be,
1270 .avr,
1271 .bpfel,
1272 .bpfeb,
1273 .csky,
1274 .dxil,
1275 .hexagon,
1276 .kalimba,
1277 .lanai,
1278 .m68k,
1279 .mips,
1280 .mipsel,
1281 .mips64,
1282 .mips64el,
1283 .msp430,
1284 .nvptx,
1285 .nvptx64,
1286 .powerpc,
1287 .powerpcle,
1288 .powerpc64,
1289 .powerpc64le,
1290 .s390x,
1291 .sparc,
1292 .sparc64,
1293 .spirv,
1294 .spirv32,
1295 .spirv64,
1296 .spu_2,
1297 .ve,
1298 .wasm32,
1299 .wasm64,
1300 .xcore,
1301 .xtensa,
1302 => .UNKNOWN,
1303 };
1304 }
1305
1306 pub fn endian(arch: Arch) std.builtin.Endian {1306 pub fn endian(arch: Arch) std.builtin.Endian {
1307 return switch (arch) {1307 return switch (arch) {
1308 .avr,1308 .avr,
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 = target.cpu.arch.toCoffMachine(),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