llvm-project
107 строк · 3.4 Кб
1//===-- llvm/BinaryFormat/MachO.cpp - The MachO file format -----*- C++/-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/BinaryFormat/MachO.h"
10#include "llvm/TargetParser/ARMTargetParser.h"
11#include "llvm/TargetParser/Triple.h"
12
13using namespace llvm;
14
15static MachO::CPUSubTypeX86 getX86SubType(const Triple &T) {
16assert(T.isX86());
17if (T.isArch32Bit())
18return MachO::CPU_SUBTYPE_I386_ALL;
19
20assert(T.isArch64Bit());
21if (T.getArchName() == "x86_64h")
22return MachO::CPU_SUBTYPE_X86_64_H;
23return MachO::CPU_SUBTYPE_X86_64_ALL;
24}
25
26static MachO::CPUSubTypeARM getARMSubType(const Triple &T) {
27assert(T.isARM() || T.isThumb());
28StringRef Arch = T.getArchName();
29ARM::ArchKind AK = ARM::parseArch(Arch);
30switch (AK) {
31default:
32return MachO::CPU_SUBTYPE_ARM_V7;
33case ARM::ArchKind::ARMV4T:
34return MachO::CPU_SUBTYPE_ARM_V4T;
35case ARM::ArchKind::ARMV5T:
36case ARM::ArchKind::ARMV5TE:
37case ARM::ArchKind::ARMV5TEJ:
38return MachO::CPU_SUBTYPE_ARM_V5;
39case ARM::ArchKind::ARMV6:
40case ARM::ArchKind::ARMV6K:
41return MachO::CPU_SUBTYPE_ARM_V6;
42case ARM::ArchKind::ARMV7A:
43return MachO::CPU_SUBTYPE_ARM_V7;
44case ARM::ArchKind::ARMV7S:
45return MachO::CPU_SUBTYPE_ARM_V7S;
46case ARM::ArchKind::ARMV7K:
47return MachO::CPU_SUBTYPE_ARM_V7K;
48case ARM::ArchKind::ARMV6M:
49return MachO::CPU_SUBTYPE_ARM_V6M;
50case ARM::ArchKind::ARMV7M:
51return MachO::CPU_SUBTYPE_ARM_V7M;
52case ARM::ArchKind::ARMV7EM:
53return MachO::CPU_SUBTYPE_ARM_V7EM;
54}
55}
56
57static MachO::CPUSubTypeARM64 getARM64SubType(const Triple &T) {
58assert(T.isAArch64());
59if (T.isArch32Bit())
60return (MachO::CPUSubTypeARM64)MachO::CPU_SUBTYPE_ARM64_32_V8;
61if (T.isArm64e())
62return MachO::CPU_SUBTYPE_ARM64E;
63
64return MachO::CPU_SUBTYPE_ARM64_ALL;
65}
66
67static MachO::CPUSubTypePowerPC getPowerPCSubType(const Triple &T) {
68return MachO::CPU_SUBTYPE_POWERPC_ALL;
69}
70
71static Error unsupported(const char *Str, const Triple &T) {
72return createStringError(std::errc::invalid_argument,
73"Unsupported triple for mach-o cpu %s: %s", Str,
74T.str().c_str());
75}
76
77Expected<uint32_t> MachO::getCPUType(const Triple &T) {
78if (!T.isOSBinFormatMachO())
79return unsupported("type", T);
80if (T.isX86() && T.isArch32Bit())
81return MachO::CPU_TYPE_X86;
82if (T.isX86() && T.isArch64Bit())
83return MachO::CPU_TYPE_X86_64;
84if (T.isARM() || T.isThumb())
85return MachO::CPU_TYPE_ARM;
86if (T.isAArch64())
87return T.isArch32Bit() ? MachO::CPU_TYPE_ARM64_32 : MachO::CPU_TYPE_ARM64;
88if (T.getArch() == Triple::ppc)
89return MachO::CPU_TYPE_POWERPC;
90if (T.getArch() == Triple::ppc64)
91return MachO::CPU_TYPE_POWERPC64;
92return unsupported("type", T);
93}
94
95Expected<uint32_t> MachO::getCPUSubType(const Triple &T) {
96if (!T.isOSBinFormatMachO())
97return unsupported("subtype", T);
98if (T.isX86())
99return getX86SubType(T);
100if (T.isARM() || T.isThumb())
101return getARMSubType(T);
102if (T.isAArch64() || T.getArch() == Triple::aarch64_32)
103return getARM64SubType(T);
104if (T.getArch() == Triple::ppc || T.getArch() == Triple::ppc64)
105return getPowerPCSubType(T);
106return unsupported("subtype", T);
107}
108