diff --git a/llvm/tools/llvm-objcopy/CopyConfig.h b/llvm/tools/llvm-objcopy/CopyConfig.h --- a/llvm/tools/llvm-objcopy/CopyConfig.h +++ b/llvm/tools/llvm-objcopy/CopyConfig.h @@ -29,23 +29,34 @@ enum class FileFormat { Unspecified, ELF, + MachO, Binary, IHex, }; // This type keeps track of the machine info for various architectures. This -// lets us map architecture names to ELF types and the e_machine value of the -// ELF file. +// lets us map architecture names to format-specific values. struct MachineInfo { - MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle) - : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {} + MachineInfo(uint16_t EM, uint8_t ABI, uint32_t MachOCPUType, + uint32_t MachOCPUSubType, bool Is64, bool IsLittle) + : EMachine(EM), OSABI(ABI), MachOCPUType(MachOCPUType), + MachOCPUSubType(MachOCPUSubType), Is64Bit(Is64), + IsLittleEndian(IsLittle) {} // Alternative constructor that defaults to NONE for OSABI. - MachineInfo(uint16_t EM, bool Is64, bool IsLittle) - : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {} + MachineInfo(uint16_t EM, uint32_t MachOCPUType, uint32_t MachOSubCPUType, + bool Is64, bool IsLittle) + : MachineInfo(EM, ELF::ELFOSABI_NONE, MachOCPUType, MachOSubCPUType, Is64, + IsLittle) {} // Default constructor for unset fields. - MachineInfo() : MachineInfo(0, 0, false, false) {} + MachineInfo() : MachineInfo(0, 0, 0, 0, false, false) {} + + // ELF values. uint16_t EMachine; uint8_t OSABI; + // MachO values. + uint32_t MachOCPUType; + uint32_t MachOCPUSubType; + // Common values. bool Is64Bit; bool IsLittleEndian; }; diff --git a/llvm/tools/llvm-objcopy/CopyConfig.cpp b/llvm/tools/llvm-objcopy/CopyConfig.cpp --- a/llvm/tools/llvm-objcopy/CopyConfig.cpp +++ b/llvm/tools/llvm-objcopy/CopyConfig.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" +#include "llvm/BinaryFormat/MachO.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/CommandLine.h" @@ -256,18 +257,20 @@ } static const StringMap ArchMap{ - // Name, {EMachine, 64bit, LittleEndian} - {"aarch64", {ELF::EM_AARCH64, true, true}}, - {"arm", {ELF::EM_ARM, false, true}}, - {"i386", {ELF::EM_386, false, true}}, - {"i386:x86-64", {ELF::EM_X86_64, true, true}}, - {"mips", {ELF::EM_MIPS, false, false}}, - {"powerpc:common64", {ELF::EM_PPC64, true, true}}, - {"riscv:rv32", {ELF::EM_RISCV, false, true}}, - {"riscv:rv64", {ELF::EM_RISCV, true, true}}, - {"sparc", {ELF::EM_SPARC, false, false}}, - {"sparcel", {ELF::EM_SPARC, false, true}}, - {"x86-64", {ELF::EM_X86_64, true, true}}, + // Name, {EMachine, MachOCPUType, MachOCPUSubType, 64bit, LittleEndian} + {"aarch64", {ELF::EM_AARCH64, 0, 0, true, true}}, + {"arm", {ELF::EM_ARM, 0, 0, false, true}}, + {"i386", {ELF::EM_386, 0, 0, false, true}}, + {"i386:x86-64", {ELF::EM_X86_64, 0, 0, true, true}}, + {"mips", {ELF::EM_MIPS, 0, 0, false, false}}, + {"powerpc:common64", {ELF::EM_PPC64, 0, 0, true, true}}, + {"riscv:rv32", {ELF::EM_RISCV, 0, 0, false, true}}, + {"riscv:rv64", {ELF::EM_RISCV, 0, 0, true, true}}, + {"sparc", {ELF::EM_SPARC, 0, 0, false, false}}, + {"sparcel", {ELF::EM_SPARC, 0, 0, false, true}}, + {"x86-64", + {ELF::EM_X86_64, MachO::CPU_TYPE_X86_64, MachO::CPU_SUBTYPE_X86_64_ALL, + true, true}}, }; static Expected getMachineInfo(StringRef Arch) { @@ -285,37 +288,39 @@ // FIXME: consolidate with the bfd parsing used by lld. static const StringMap TargetMap{ - // Name, {EMachine, 64bit, LittleEndian} + // Name, {EMachine, MachOCPUType, MachOCPUSubType, 64bit, LittleEndian} // x86 - {"elf32-i386", {ELF::EM_386, false, true}}, - {"elf32-x86-64", {ELF::EM_X86_64, false, true}}, - {"elf64-x86-64", {ELF::EM_X86_64, true, true}}, + {"elf32-i386", {ELF::EM_386, 0, 0, false, true}}, + {"elf32-x86-64", {ELF::EM_X86_64, 0, 0, false, true}}, + {"elf64-x86-64", {ELF::EM_X86_64, 0, 0, true, true}}, + {"mach-o-x86-64", + {0, MachO::CPU_TYPE_X86_64, MachO::CPU_SUBTYPE_X86_64_ALL, true, true}}, // Intel MCU - {"elf32-iamcu", {ELF::EM_IAMCU, false, true}}, + {"elf32-iamcu", {ELF::EM_IAMCU, 0, 0, false, true}}, // ARM - {"elf32-littlearm", {ELF::EM_ARM, false, true}}, + {"elf32-littlearm", {ELF::EM_ARM, 0, 0, false, true}}, // ARM AArch64 - {"elf64-aarch64", {ELF::EM_AARCH64, true, true}}, - {"elf64-littleaarch64", {ELF::EM_AARCH64, true, true}}, + {"elf64-aarch64", {ELF::EM_AARCH64, 0, 0, true, true}}, + {"elf64-littleaarch64", {ELF::EM_AARCH64, 0, 0, true, true}}, // RISC-V - {"elf32-littleriscv", {ELF::EM_RISCV, false, true}}, - {"elf64-littleriscv", {ELF::EM_RISCV, true, true}}, + {"elf32-littleriscv", {ELF::EM_RISCV, 0, 0, false, true}}, + {"elf64-littleriscv", {ELF::EM_RISCV, 0, 0, true, true}}, // PowerPC - {"elf32-powerpc", {ELF::EM_PPC, false, false}}, - {"elf32-powerpcle", {ELF::EM_PPC, false, true}}, - {"elf64-powerpc", {ELF::EM_PPC64, true, false}}, - {"elf64-powerpcle", {ELF::EM_PPC64, true, true}}, + {"elf32-powerpc", {ELF::EM_PPC, 0, 0, false, false}}, + {"elf32-powerpcle", {ELF::EM_PPC, 0, 0, false, true}}, + {"elf64-powerpc", {ELF::EM_PPC64, 0, 0, true, false}}, + {"elf64-powerpcle", {ELF::EM_PPC64, 0, 0, true, true}}, // MIPS - {"elf32-bigmips", {ELF::EM_MIPS, false, false}}, - {"elf32-ntradbigmips", {ELF::EM_MIPS, false, false}}, - {"elf32-ntradlittlemips", {ELF::EM_MIPS, false, true}}, - {"elf32-tradbigmips", {ELF::EM_MIPS, false, false}}, - {"elf32-tradlittlemips", {ELF::EM_MIPS, false, true}}, - {"elf64-tradbigmips", {ELF::EM_MIPS, true, false}}, - {"elf64-tradlittlemips", {ELF::EM_MIPS, true, true}}, + {"elf32-bigmips", {ELF::EM_MIPS, 0, 0, false, false}}, + {"elf32-ntradbigmips", {ELF::EM_MIPS, 0, 0, false, false}}, + {"elf32-ntradlittlemips", {ELF::EM_MIPS, 0, 0, false, true}}, + {"elf32-tradbigmips", {ELF::EM_MIPS, 0, 0, false, false}}, + {"elf32-tradlittlemips", {ELF::EM_MIPS, 0, 0, false, true}}, + {"elf64-tradbigmips", {ELF::EM_MIPS, 0, 0, true, false}}, + {"elf64-tradlittlemips", {ELF::EM_MIPS, 0, 0, true, true}}, // SPARC - {"elf32-sparc", {ELF::EM_SPARC, false, false}}, - {"elf32-sparcel", {ELF::EM_SPARC, false, true}}, + {"elf32-sparc", {ELF::EM_SPARC, 0, 0, false, false}}, + {"elf32-sparcel", {ELF::EM_SPARC, 0, 0, false, true}}, }; static Expected @@ -334,6 +339,8 @@ FileFormat Format; if (TargetName.startswith("elf")) Format = FileFormat::ELF; + else if (TargetName.startswith("mach-o")) + Format = FileFormat::MachO; else // This should never happen because `TargetName` is valid (it certainly // exists in the TargetMap).