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) {
10601060 WCEMIPSV2 = 0x169,
10611061
10621062 _,
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 }
10801063};
10811064
10821065pub const CoffError = error{
lib/std/elf.zig-38
......@@ -1646,44 +1646,6 @@ pub const EM = enum(u16) {
16461646 FRV = 0x5441,
16471647
16481648 _,
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 }
16871649};
16881650
16891651pub 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
11001100 error.MalformedObject,
11011101 error.MalformedArchive,
11021102 error.MismatchedEflags,
1103 error.InvalidCpuArch,
1103 error.InvalidMachineType,
11041104 => continue, // already reported
11051105 else => |e| try self.reportParseError(
11061106 obj.path,
......@@ -1187,7 +1187,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
11871187
11881188 for (system_libs.items) |lib| {
11891189 self.parseLibrary(lib, false) catch |err| switch (err) {
1190 error.MalformedObject, error.MalformedArchive, error.InvalidCpuArch => continue, // already reported
1190 error.MalformedObject, error.MalformedArchive, error.InvalidMachineType => continue, // already reported
11911191 else => |e| try self.reportParseError(
11921192 lib.path,
11931193 "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
12131213 error.MalformedObject,
12141214 error.MalformedArchive,
12151215 error.MismatchedEflags,
1216 error.InvalidCpuArch,
1216 error.InvalidMachineType,
12171217 => continue, // already reported
12181218 else => |e| try self.reportParseError(
12191219 obj.path,
......@@ -1642,7 +1642,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16421642pub const ParseError = error{
16431643 MalformedObject,
16441644 MalformedArchive,
1645 InvalidCpuArch,
1645 InvalidMachineType,
16461646 MismatchedEflags,
16471647 OutOfMemory,
16481648 Overflow,
......@@ -1813,7 +1813,7 @@ fn parseLdScript(self: *Elf, lib: SystemLib) ParseError!void {
18131813 .needed = scr_obj.needed,
18141814 .path = full_path,
18151815 }, false) catch |err| switch (err) {
1816 error.MalformedObject, error.MalformedArchive, error.InvalidCpuArch => continue, // already reported
1816 error.MalformedObject, error.MalformedArchive, error.InvalidMachineType => continue, // already reported
18171817 else => |e| try self.reportParseError(
18181818 full_path,
18191819 "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
105105 defer allocator.free(header_buffer);
106106 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;
109 if (target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {
108 const em = elf_file.base.comp.root_mod.resolved_target.result.toElfMachine();
109 if (em != self.header.?.e_machine) {
110110 try elf_file.reportParseError2(
111111 self.index,
112 "invalid cpu architecture: {s}",
113 .{@tagName(self.header.?.e_machine.toTargetCpuArch().?)},
112 "invalid ELF machine type: {s}",
113 .{@tagName(self.header.?.e_machine)},
114114 );
115 return error.InvalidCpuArch;
115 return error.InvalidMachineType;
116116 }
117117 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 {
5656 defer gpa.free(header_buffer);
5757 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;
60 if (target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {
59 const em = elf_file.base.comp.root_mod.resolved_target.result.toElfMachine();
60 if (em != self.header.?.e_machine) {
6161 try elf_file.reportParseError2(
6262 self.index,
63 "invalid cpu architecture: {s}",
64 .{@tagName(self.header.?.e_machine.toTargetCpuArch().?)},
63 "invalid ELF machine type: {s}",
64 .{@tagName(self.header.?.e_machine)},
6565 );
66 return error.InvalidCpuArch;
66 return error.InvalidMachineType;
6767 }
6868
6969 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
2121 parsePositional(elf_file, obj.path) catch |err| switch (err) {
2222 error.MalformedObject,
2323 error.MalformedArchive,
24 error.InvalidCpuArch,
24 error.InvalidMachineType,
2525 error.MismatchedEflags,
2626 => continue, // already reported
2727 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
178178 elf_file.parsePositional(obj.path, obj.must_link) catch |err| switch (err) {
179179 error.MalformedObject,
180180 error.MalformedArchive,
181 error.InvalidCpuArch,
181 error.InvalidMachineType,
182182 error.MismatchedEflags,
183183 => continue, // already reported
184184 else => |e| try elf_file.reportParseError(
src/link/MachO.zig+1-1
......@@ -921,7 +921,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {
921921 error.MalformedObject,
922922 error.MalformedDylib,
923923 error.MalformedTbd,
924 error.InvalidCpuArch,
924 error.InvalidMachineType,
925925 error.InvalidTarget,
926926 => {}, // already reported
927927 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 {
7575 macho.CPU_TYPE_X86_64 => .x86_64,
7676 else => |x| {
7777 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});
78 return error.InvalidCpuArch;
78 return error.InvalidMachineType;
7979 },
8080 };
8181 if (macho_file.getTarget().cpu.arch != this_cpu_arch) {
8282 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});
83 return error.InvalidCpuArch;
83 return error.InvalidMachineType;
8484 }
8585
8686 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 {
9191 macho.CPU_TYPE_X86_64 => .x86_64,
9292 else => |x| {
9393 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});
94 return error.InvalidCpuArch;
94 return error.InvalidMachineType;
9595 },
9696 };
9797 if (cpu_arch != this_cpu_arch) {
9898 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});
99 return error.InvalidCpuArch;
99 return error.InvalidMachineType;
100100 }
101101
102102 const lc_buffer = try gpa.alloc(u8, self.header.?.sizeofcmds);
......@@ -1648,12 +1648,12 @@ pub fn parseAr(self: *Object, macho_file: *MachO) !void {
16481648 macho.CPU_TYPE_X86_64 => .x86_64,
16491649 else => |x| {
16501650 try macho_file.reportParseError2(self.index, "unknown cpu architecture: {d}", .{x});
1651 return error.InvalidCpuArch;
1651 return error.InvalidMachineType;
16521652 },
16531653 };
16541654 if (macho_file.getTarget().cpu.arch != this_cpu_arch) {
16551655 try macho_file.reportParseError2(self.index, "invalid cpu architecture: {s}", .{@tagName(this_cpu_arch)});
1656 return error.InvalidCpuArch;
1656 return error.InvalidMachineType;
16571657 }
16581658
16591659 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 {
232232
233233 for (macho_file.objects.items) |index| {
234234 macho_file.getFile(index).?.parseAr(macho_file) catch |err| switch (err) {
235 error.InvalidCpuArch => {}, // already reported
235 error.InvalidMachineType => {}, // already reported
236236 else => |e| try macho_file.reportParseError2(index, "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}),
237237 };
238238 }
test/link/elf.zig+1-1
......@@ -2257,7 +2257,7 @@ fn testMismatchedCpuArchitectureError(b: *Build, opts: Options) *Step {
22572257 exe.linkLibC();
22582258
22592259 expectLinkErrors(exe, test_step, .{ .exact = &.{
2260 "invalid cpu architecture: aarch64",
2260 "invalid ELF machine type: AARCH64",
22612261 "note: while parsing /?/a.o",
22622262 } });
22632263