authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-19 13:25:08+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-23 19:56:24+02:00
loga69f55a7cc3980f4d4dfcce6cb9d21a597975c7f
treea3ec23392b88a7d9e9a2fa9cb99f21d5c2cc6b9f
parent3fb6e46f6e4231b9569193a15a4357a2ae11fb0f
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.{coff,elf}: Remove the {MachineType,EM}.toTargetCpuArch() functions.

These are fundamentally incapable of producing accurate information for reasons I've laid out in #20771. Since our only use of these functions is to check that object files have the correct machine type, and since #21020 made `std.Target.to{Coff,Elf}Machine()` more accurate, just switch these checks over to that and compare the machine type tags instead. Closes #20771.

5 files changed, 9 insertions(+), 64 deletions(-)

lib/std/coff.zig-17
...@@ -1060,23 +1060,6 @@ pub const MachineType = enum(u16) {...@@ -1060,23 +1060,6 @@ pub const MachineType = enum(u16) {
1060 WCEMIPSV2 = 0x169,1060 WCEMIPSV2 = 0x169,
10611061
1062 _,1062 _,
1063
1064 pub fn toTargetCpuArch(machine_type: MachineType) ?std.Target.Cpu.Arch {
1065 return switch (machine_type) {
1066 .ARM => .arm,
1067 .POWERPC => .powerpc,
1068 .RISCV32 => .riscv32,
1069 .THUMB => .thumb,
1070 .I386 => .x86,
1071 .ARM64 => .aarch64,
1072 .RISCV64 => .riscv64,
1073 .X64 => .x86_64,
1074 .LOONGARCH32 => .loongarch32,
1075 .LOONGARCH64 => .loongarch64,
1076 // there's cases we don't (yet) handle
1077 else => null,
1078 };
1079 }
1080};1063};
10811064
1082pub const CoffError = error{1065pub const CoffError = error{
lib/std/elf.zig-38
...@@ -1646,44 +1646,6 @@ pub const EM = enum(u16) {...@@ -1646,44 +1646,6 @@ pub const EM = enum(u16) {
1646 FRV = 0x5441,1646 FRV = 0x5441,
16471647
1648 _,1648 _,
1649
1650 pub fn toTargetCpuArch(em: EM) ?std.Target.Cpu.Arch {
1651 return switch (em) {
1652 .AVR => .avr,
1653 .MSP430 => .msp430,
1654 .ARC => .arc,
1655 .ARM => .arm,
1656 .HEXAGON => .hexagon,
1657 .@"68K" => .m68k,
1658 .MIPS => .mips,
1659 .MIPS_RS3_LE => .mipsel,
1660 .PPC => .powerpc,
1661 .SPARC => .sparc,
1662 .@"386" => .x86,
1663 .XCORE => .xcore,
1664 .CSR_KALIMBA => .kalimba,
1665 .LANAI => .lanai,
1666 .AARCH64 => .aarch64,
1667 .PPC64 => .powerpc64,
1668 .RISCV => .riscv64,
1669 .X86_64 => .x86_64,
1670 .BPF => .bpfel,
1671 .SPARCV9 => .sparc64,
1672 .S390 => .s390x,
1673 .SPU_2 => .spu_2,
1674 // FIXME:
1675 // No support for .loongarch32 yet so it is safe to assume we are on .loongarch64.
1676 //
1677 // However, when e_machine is .LOONGARCH, we should check
1678 // ei_class's value to decide the CPU architecture.
1679 // - ELFCLASS32 => .loongarch32
1680 // - ELFCLASS64 => .loongarch64
1681 .LOONGARCH => .loongarch64,
1682 // there's many cases we don't (yet) handle, or will never have a
1683 // zig target cpu arch equivalent (such as null).
1684 else => null,
1685 };
1686 }
1687};1649};
16881650
1689pub const GRP_COMDAT = 1;1651pub const GRP_COMDAT = 1;
src/link/Elf/Object.zig+4-4
...@@ -105,12 +105,12 @@ fn parseCommon(self: *Object, allocator: Allocator, handle: std.fs.File, elf_fil...@@ -105,12 +105,12 @@ fn parseCommon(self: *Object, allocator: Allocator, handle: std.fs.File, elf_fil
105 defer allocator.free(header_buffer);105 defer allocator.free(header_buffer);
106 self.header = @as(*align(1) const elf.Elf64_Ehdr, @ptrCast(header_buffer)).*;106 self.header = @as(*align(1) const elf.Elf64_Ehdr, @ptrCast(header_buffer)).*;
107107
108 const target = elf_file.base.comp.root_mod.resolved_target.result;108 const em = elf_file.base.comp.root_mod.resolved_target.result.toElfMachine();
109 if (target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {109 if (em != self.header.?.e_machine) {
110 try elf_file.reportParseError2(110 try elf_file.reportParseError2(
111 self.index,111 self.index,
112 "invalid cpu architecture: {s}",112 "invalid ELF machine type: {s}",
113 .{@tagName(self.header.?.e_machine.toTargetCpuArch().?)},113 .{@tagName(self.header.?.e_machine)},
114 );114 );
115 return error.InvalidCpuArch;115 return error.InvalidCpuArch;
116 }116 }
src/link/Elf/SharedObject.zig+4-4
...@@ -56,12 +56,12 @@ pub fn parse(self: *SharedObject, elf_file: *Elf, handle: std.fs.File) !void {...@@ -56,12 +56,12 @@ pub fn parse(self: *SharedObject, elf_file: *Elf, handle: std.fs.File) !void {
56 defer gpa.free(header_buffer);56 defer gpa.free(header_buffer);
57 self.header = @as(*align(1) const elf.Elf64_Ehdr, @ptrCast(header_buffer)).*;57 self.header = @as(*align(1) const elf.Elf64_Ehdr, @ptrCast(header_buffer)).*;
5858
59 const target = elf_file.base.comp.root_mod.resolved_target.result;59 const em = elf_file.base.comp.root_mod.resolved_target.result.toElfMachine();
60 if (target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {60 if (em != self.header.?.e_machine) {
61 try elf_file.reportParseError2(61 try elf_file.reportParseError2(
62 self.index,62 self.index,
63 "invalid cpu architecture: {s}",63 "invalid ELF machine type: {s}",
64 .{@tagName(self.header.?.e_machine.toTargetCpuArch().?)},64 .{@tagName(self.header.?.e_machine)},
65 );65 );
66 return error.InvalidCpuArch;66 return error.InvalidCpuArch;
67 }67 }
test/link/elf.zig+1-1
...@@ -2257,7 +2257,7 @@ fn testMismatchedCpuArchitectureError(b: *Build, opts: Options) *Step {...@@ -2257,7 +2257,7 @@ fn testMismatchedCpuArchitectureError(b: *Build, opts: Options) *Step {
2257 exe.linkLibC();2257 exe.linkLibC();
22582258
2259 expectLinkErrors(exe, test_step, .{ .exact = &.{2259 expectLinkErrors(exe, test_step, .{ .exact = &.{
2260 "invalid cpu architecture: aarch64",2260 "invalid ELF machine type: AARCH64",
2261 "note: while parsing /?/a.o",2261 "note: while parsing /?/a.o",
2262 } });2262 } });
22632263