authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-25 14:49:53-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-25 14:49:53-07:00
log849c31a6cc3d1e554f97c2ccf7aaa886070cfadd
treec71047e77eac9215fcef554c9f583ad2a8bd2542
parent7d54c62c8a55240bbe144ab03c78573a344598ce
parentfb6f5a30b2d6334d0f1415446849d39fe00d3af0
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21177 from alexrp/elf-coff-conv

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

11 files changed, 26 insertions(+), 81 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.zig+5-5
...@@ -1100,7 +1100,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1100,7 +1100,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1100 error.MalformedObject,1100 error.MalformedObject,
1101 error.MalformedArchive,1101 error.MalformedArchive,
1102 error.MismatchedEflags,1102 error.MismatchedEflags,
1103 error.InvalidCpuArch,1103 error.InvalidMachineType,
1104 => continue, // already reported1104 => continue, // already reported
1105 else => |e| try self.reportParseError(1105 else => |e| try self.reportParseError(
1106 obj.path,1106 obj.path,
...@@ -1187,7 +1187,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1187,7 +1187,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
11871187
1188 for (system_libs.items) |lib| {1188 for (system_libs.items) |lib| {
1189 self.parseLibrary(lib, false) catch |err| switch (err) {1189 self.parseLibrary(lib, false) catch |err| switch (err) {
1190 error.MalformedObject, error.MalformedArchive, error.InvalidCpuArch => continue, // already reported1190 error.MalformedObject, error.MalformedArchive, error.InvalidMachineType => continue, // already reported
1191 else => |e| try self.reportParseError(1191 else => |e| try self.reportParseError(
1192 lib.path,1192 lib.path,
1193 "unexpected error: parsing library failed with error {s}",1193 "unexpected error: parsing library failed with error {s}",
...@@ -1213,7 +1213,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1213,7 +1213,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1213 error.MalformedObject,1213 error.MalformedObject,
1214 error.MalformedArchive,1214 error.MalformedArchive,
1215 error.MismatchedEflags,1215 error.MismatchedEflags,
1216 error.InvalidCpuArch,1216 error.InvalidMachineType,
1217 => continue, // already reported1217 => continue, // already reported
1218 else => |e| try self.reportParseError(1218 else => |e| try self.reportParseError(
1219 obj.path,1219 obj.path,
...@@ -1642,7 +1642,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {...@@ -1642,7 +1642,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
1642pub const ParseError = error{1642pub const ParseError = error{
1643 MalformedObject,1643 MalformedObject,
1644 MalformedArchive,1644 MalformedArchive,
1645 InvalidCpuArch,1645 InvalidMachineType,
1646 MismatchedEflags,1646 MismatchedEflags,
1647 OutOfMemory,1647 OutOfMemory,
1648 Overflow,1648 Overflow,
...@@ -1813,7 +1813,7 @@ fn parseLdScript(self: *Elf, lib: SystemLib) ParseError!void {...@@ -1813,7 +1813,7 @@ fn parseLdScript(self: *Elf, lib: SystemLib) ParseError!void {
1813 .needed = scr_obj.needed,1813 .needed = scr_obj.needed,
1814 .path = full_path,1814 .path = full_path,
1815 }, false) catch |err| switch (err) {1815 }, false) catch |err| switch (err) {
1816 error.MalformedObject, error.MalformedArchive, error.InvalidCpuArch => continue, // already reported1816 error.MalformedObject, error.MalformedArchive, error.InvalidMachineType => continue, // already reported
1817 else => |e| try self.reportParseError(1817 else => |e| try self.reportParseError(
1818 full_path,1818 full_path,
1819 "unexpected error: parsing library failed with error {s}",1819 "unexpected error: parsing library failed with error {s}",
src/link/Elf/Object.zig+5-5
...@@ -105,14 +105,14 @@ fn parseCommon(self: *Object, allocator: Allocator, handle: std.fs.File, elf_fil...@@ -105,14 +105,14 @@ 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.InvalidMachineType;
116 }116 }
117 try elf_file.validateEFlags(self.index, self.header.?.e_flags);117 try elf_file.validateEFlags(self.index, self.header.?.e_flags);
118118
src/link/Elf/SharedObject.zig+5-5
...@@ -56,14 +56,14 @@ pub fn parse(self: *SharedObject, elf_file: *Elf, handle: std.fs.File) !void {...@@ -56,14 +56,14 @@ 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.InvalidMachineType;
67 }67 }
6868
69 const shoff = std.math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow;69 const shoff = std.math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow;
src/link/Elf/relocatable.zig+2-2
...@@ -21,7 +21,7 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co...@@ -21,7 +21,7 @@ pub fn flushStaticLib(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]co
21 parsePositional(elf_file, obj.path) catch |err| switch (err) {21 parsePositional(elf_file, obj.path) catch |err| switch (err) {
22 error.MalformedObject,22 error.MalformedObject,
23 error.MalformedArchive,23 error.MalformedArchive,
24 error.InvalidCpuArch,24 error.InvalidMachineType,
25 error.MismatchedEflags,25 error.MismatchedEflags,
26 => continue, // already reported26 => continue, // already reported
27 error.UnknownFileType => try elf_file.reportParseError(obj.path, "unknown file type for an object file", .{}),27 error.UnknownFileType => try elf_file.reportParseError(obj.path, "unknown file type for an object file", .{}),
...@@ -178,7 +178,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const...@@ -178,7 +178,7 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
178 elf_file.parsePositional(obj.path, obj.must_link) catch |err| switch (err) {178 elf_file.parsePositional(obj.path, obj.must_link) catch |err| switch (err) {
179 error.MalformedObject,179 error.MalformedObject,
180 error.MalformedArchive,180 error.MalformedArchive,
181 error.InvalidCpuArch,181 error.InvalidMachineType,
182 error.MismatchedEflags,182 error.MismatchedEflags,
183 => continue, // already reported183 => continue, // already reported
184 else => |e| try elf_file.reportParseError(184 else => |e| try elf_file.reportParseError(
src/link/MachO.zig+1-1
...@@ -921,7 +921,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {...@@ -921,7 +921,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {
921 error.MalformedObject,921 error.MalformedObject,
922 error.MalformedDylib,922 error.MalformedDylib,
923 error.MalformedTbd,923 error.MalformedTbd,
924 error.InvalidCpuArch,924 error.InvalidMachineType,
925 error.InvalidTarget,925 error.InvalidTarget,
926 => {}, // already reported926 => {}, // already reported
927 else => |e| self.reportParseError2(file.getIndex(), "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}) catch {},927 else => |e| self.reportParseError2(file.getIndex(), "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}) catch {},
src/link/MachO/Dylib.zig+2-2
...@@ -75,12 +75,12 @@ fn parseBinary(self: *Dylib, macho_file: *MachO) !void {...@@ -75,12 +75,12 @@ fn parseBinary(self: *Dylib, macho_file: *MachO) !void {
75 macho.CPU_TYPE_X86_64 => .x86_64,75 macho.CPU_TYPE_X86_64 => .x86_64,
76 else => |x| {76 else => |x| {
77 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});77 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});
78 return error.InvalidCpuArch;78 return error.InvalidMachineType;
79 },79 },
80 };80 };
81 if (macho_file.getTarget().cpu.arch != this_cpu_arch) {81 if (macho_file.getTarget().cpu.arch != this_cpu_arch) {
82 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});82 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});
83 return error.InvalidCpuArch;83 return error.InvalidMachineType;
84 }84 }
8585
86 const lc_buffer = try gpa.alloc(u8, header.sizeofcmds);86 const lc_buffer = try gpa.alloc(u8, header.sizeofcmds);
src/link/MachO/Object.zig+4-4
...@@ -91,12 +91,12 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {...@@ -91,12 +91,12 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
91 macho.CPU_TYPE_X86_64 => .x86_64,91 macho.CPU_TYPE_X86_64 => .x86_64,
92 else => |x| {92 else => |x| {
93 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});93 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});
94 return error.InvalidCpuArch;94 return error.InvalidMachineType;
95 },95 },
96 };96 };
97 if (cpu_arch != this_cpu_arch) {97 if (cpu_arch != this_cpu_arch) {
98 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});98 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});
99 return error.InvalidCpuArch;99 return error.InvalidMachineType;
100 }100 }
101101
102 const lc_buffer = try gpa.alloc(u8, self.header.?.sizeofcmds);102 const lc_buffer = try gpa.alloc(u8, self.header.?.sizeofcmds);
...@@ -1648,12 +1648,12 @@ pub fn parseAr(self: *Object, macho_file: *MachO) !void {...@@ -1648,12 +1648,12 @@ pub fn parseAr(self: *Object, macho_file: *MachO) !void {
1648 macho.CPU_TYPE_X86_64 => .x86_64,1648 macho.CPU_TYPE_X86_64 => .x86_64,
1649 else => |x| {1649 else => |x| {
1650 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});1650 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});
1651 return error.InvalidCpuArch;1651 return error.InvalidMachineType;
1652 },1652 },
1653 };1653 };
1654 if (macho_file.getTarget().cpu.arch != this_cpu_arch) {1654 if (macho_file.getTarget().cpu.arch != this_cpu_arch) {
1655 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});1655 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});
1656 return error.InvalidCpuArch;1656 return error.InvalidMachineType;
1657 }1657 }
16581658
1659 const lc_buffer = try gpa.alloc(u8, self.header.?.sizeofcmds);1659 const lc_buffer = try gpa.alloc(u8, self.header.?.sizeofcmds);
src/link/MachO/relocatable.zig+1-1
...@@ -232,7 +232,7 @@ fn parseInputFilesAr(macho_file: *MachO) !void {...@@ -232,7 +232,7 @@ fn parseInputFilesAr(macho_file: *MachO) !void {
232232
233 for (macho_file.objects.items) |index| {233 for (macho_file.objects.items) |index| {
234 macho_file.getFile(index).?.parseAr(macho_file) catch |err| switch (err) {234 macho_file.getFile(index).?.parseAr(macho_file) catch |err| switch (err) {
235 error.InvalidCpuArch => {}, // already reported235 error.InvalidMachineType => {}, // already reported
236 else => |e| try macho_file.reportParseError2(index, "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}),236 else => |e| try macho_file.reportParseError2(index, "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}),
237 };237 };
238 }238 }
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