[MachO] Add cpu(sub)type tests and improve error handling

Add checks for triples that don't use mach-o, and unit tests for
everything.
This commit is contained in:
Francis Visoiu Mistrih 2020-02-24 10:42:21 -08:00
parent 00570c2f18
commit 7b0a5683fa
2 changed files with 88 additions and 4 deletions

View File

@ -68,7 +68,15 @@ static MachO::CPUSubTypePowerPC getPowerPCSubType(const Triple &T) {
return MachO::CPU_SUBTYPE_POWERPC_ALL;
}
static Error unsupported(const char *Str, const Triple &T) {
return createStringError(std::errc::invalid_argument,
"Unsupported triple for mach-o cpu %s: %s", Str,
T.str().c_str());
}
Expected<uint32_t> MachO::getCPUType(const Triple &T) {
if (!T.isOSBinFormatMachO())
return unsupported("type", T);
if (T.isX86() && T.isArch32Bit())
return MachO::CPU_TYPE_X86;
if (T.isX86() && T.isArch64Bit())
@ -83,11 +91,12 @@ Expected<uint32_t> MachO::getCPUType(const Triple &T) {
return MachO::CPU_TYPE_POWERPC;
if (T.getArch() == Triple::ppc64)
return MachO::CPU_TYPE_POWERPC64;
return createStringError(std::errc::invalid_argument,
"Unsupported triple for mach-o cpu type.");
return unsupported("type", T);
}
Expected<uint32_t> MachO::getCPUSubType(const Triple &T) {
if (!T.isOSBinFormatMachO())
return unsupported("subtype", T);
if (T.isX86())
return getX86SubType(T);
if (T.isARM() || T.isThumb())
@ -96,6 +105,5 @@ Expected<uint32_t> MachO::getCPUSubType(const Triple &T) {
return getARM64SubType(T);
if (T.getArch() == Triple::ppc || T.getArch() == Triple::ppc64)
return getPowerPCSubType(T);
return createStringError(std::errc::invalid_argument,
"Unsupported triple for mach-o cpu subtype.");
return unsupported("subtype", T);
}

View File

@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/ADT/Triple.h"
#include "gtest/gtest.h"
using namespace llvm;
@ -44,3 +45,78 @@ TEST(MachOTest, UnalignedLC) {
Current += LC->load_command_data.cmdsize;
}
}
TEST(MachOTest, CPUType) {
#define CHECK_CPUTYPE(StrTriple, ExpectedCPUType) \
ASSERT_EQ((MachO::CPUType)cantFail(MachO::getCPUType(Triple(StrTriple))), \
(ExpectedCPUType))
CHECK_CPUTYPE("x86_64-apple-darwin", MachO::CPU_TYPE_X86_64);
CHECK_CPUTYPE("x86_64h-apple-darwin", MachO::CPU_TYPE_X86_64);
CHECK_CPUTYPE("i386-apple-darwin", MachO::CPU_TYPE_X86);
CHECK_CPUTYPE("armv7-apple-darwin", MachO::CPU_TYPE_ARM);
CHECK_CPUTYPE("thumbv7-apple-darwin", MachO::CPU_TYPE_ARM);
CHECK_CPUTYPE("arm64-apple-darwin", MachO::CPU_TYPE_ARM64);
CHECK_CPUTYPE("arm64e-apple-darwin", MachO::CPU_TYPE_ARM64);
CHECK_CPUTYPE("arm64_32-apple-darwin", MachO::CPU_TYPE_ARM64_32);
CHECK_CPUTYPE("powerpc-apple-darwin", MachO::CPU_TYPE_POWERPC);
CHECK_CPUTYPE("powerpc64-apple-darwin", MachO::CPU_TYPE_POWERPC64);
{
// Not a mach-o.
Expected<uint32_t> Type = MachO::getCPUType(Triple("x86_64-linux-unknown"));
ASSERT_EQ(toString(Type.takeError()),
"Unsupported triple for mach-o cpu type: x86_64-linux-unknown");
}
{
// Not a valid mach-o architecture.
Expected<uint32_t> Type = MachO::getCPUType(Triple("mips-apple-darwin"));
ASSERT_EQ(toString(Type.takeError()),
"Unsupported triple for mach-o cpu type: mips-apple-darwin");
}
#undef CHECK_CPUTYPE
}
TEST(MachOTest, CPUSubType) {
#define CHECK_CPUSUBTYPE(StrTriple, ExpectedCPUSubType) \
ASSERT_EQ(cantFail(MachO::getCPUSubType(Triple(StrTriple))), \
((uint32_t)ExpectedCPUSubType))
CHECK_CPUSUBTYPE("x86_64-apple-darwin", MachO::CPU_SUBTYPE_X86_64_ALL);
CHECK_CPUSUBTYPE("x86_64h-apple-darwin", MachO::CPU_SUBTYPE_X86_64_H);
CHECK_CPUSUBTYPE("i386-apple-darwin", MachO::CPU_SUBTYPE_I386_ALL);
CHECK_CPUSUBTYPE("arm-apple-darwin", MachO::CPU_SUBTYPE_ARM_V7); // Default
CHECK_CPUSUBTYPE("armv4t-apple-darwin", MachO::CPU_SUBTYPE_ARM_V4T);
CHECK_CPUSUBTYPE("armv5t-apple-darwin", MachO::CPU_SUBTYPE_ARM_V5);
CHECK_CPUSUBTYPE("armv5te-apple-darwin", MachO::CPU_SUBTYPE_ARM_V5);
CHECK_CPUSUBTYPE("armv5tej-apple-darwin", MachO::CPU_SUBTYPE_ARM_V5);
CHECK_CPUSUBTYPE("armv6-apple-darwin", MachO::CPU_SUBTYPE_ARM_V6);
CHECK_CPUSUBTYPE("armv6k-apple-darwin", MachO::CPU_SUBTYPE_ARM_V6);
CHECK_CPUSUBTYPE("armv7a-apple-darwin", MachO::CPU_SUBTYPE_ARM_V7);
CHECK_CPUSUBTYPE("armv7s-apple-darwin", MachO::CPU_SUBTYPE_ARM_V7S);
CHECK_CPUSUBTYPE("armv7k-apple-darwin", MachO::CPU_SUBTYPE_ARM_V7K);
CHECK_CPUSUBTYPE("armv6m-apple-darwin", MachO::CPU_SUBTYPE_ARM_V6M);
CHECK_CPUSUBTYPE("armv7m-apple-darwin", MachO::CPU_SUBTYPE_ARM_V7M);
CHECK_CPUSUBTYPE("armv7em-apple-darwin", MachO::CPU_SUBTYPE_ARM_V7EM);
CHECK_CPUSUBTYPE("thumbv7-apple-darwin", MachO::CPU_SUBTYPE_ARM_V7);
CHECK_CPUSUBTYPE("thumbv6-apple-darwin", MachO::CPU_SUBTYPE_ARM_V6);
CHECK_CPUSUBTYPE("arm64-apple-darwin", MachO::CPU_SUBTYPE_ARM64_ALL);
CHECK_CPUSUBTYPE("arm64e-apple-darwin", MachO::CPU_SUBTYPE_ARM64E);
CHECK_CPUSUBTYPE("arm64_32-apple-darwin", MachO::CPU_SUBTYPE_ARM64_32_V8);
CHECK_CPUSUBTYPE("powerpc-apple-darwin", MachO::CPU_SUBTYPE_POWERPC_ALL);
CHECK_CPUSUBTYPE("powerpc64-apple-darwin", MachO::CPU_SUBTYPE_POWERPC_ALL);
{
// Not a mach-o.
Expected<uint32_t> Type =
MachO::getCPUSubType(Triple("x86_64-linux-unknown"));
ASSERT_EQ(
toString(Type.takeError()),
"Unsupported triple for mach-o cpu subtype: x86_64-linux-unknown");
}
{
// Not a valid mach-o architecture.
Expected<uint32_t> Type = MachO::getCPUSubType(Triple("mips-apple-darwin"));
ASSERT_EQ(toString(Type.takeError()),
"Unsupported triple for mach-o cpu subtype: mips-apple-darwin");
}
#undef CHECK_CPUSUBTYPE
}