MachO.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "llvm/BinaryFormat/MachO.h"
  9. #include "llvm/ADT/Triple.h"
  10. #include "llvm/Support/ARMTargetParser.h"
  11. using namespace llvm;
  12. static MachO::CPUSubTypeX86 getX86SubType(const Triple &T) {
  13. assert(T.isX86());
  14. if (T.isArch32Bit())
  15. return MachO::CPU_SUBTYPE_I386_ALL;
  16. assert(T.isArch64Bit());
  17. if (T.getArchName() == "x86_64h")
  18. return MachO::CPU_SUBTYPE_X86_64_H;
  19. return MachO::CPU_SUBTYPE_X86_64_ALL;
  20. }
  21. static MachO::CPUSubTypeARM getARMSubType(const Triple &T) {
  22. assert(T.isARM() || T.isThumb());
  23. StringRef Arch = T.getArchName();
  24. ARM::ArchKind AK = ARM::parseArch(Arch);
  25. switch (AK) {
  26. default:
  27. return MachO::CPU_SUBTYPE_ARM_V7;
  28. case ARM::ArchKind::ARMV4T:
  29. return MachO::CPU_SUBTYPE_ARM_V4T;
  30. case ARM::ArchKind::ARMV5T:
  31. case ARM::ArchKind::ARMV5TE:
  32. case ARM::ArchKind::ARMV5TEJ:
  33. return MachO::CPU_SUBTYPE_ARM_V5;
  34. case ARM::ArchKind::ARMV6:
  35. case ARM::ArchKind::ARMV6K:
  36. return MachO::CPU_SUBTYPE_ARM_V6;
  37. case ARM::ArchKind::ARMV7A:
  38. return MachO::CPU_SUBTYPE_ARM_V7;
  39. case ARM::ArchKind::ARMV7S:
  40. return MachO::CPU_SUBTYPE_ARM_V7S;
  41. case ARM::ArchKind::ARMV7K:
  42. return MachO::CPU_SUBTYPE_ARM_V7K;
  43. case ARM::ArchKind::ARMV6M:
  44. return MachO::CPU_SUBTYPE_ARM_V6M;
  45. case ARM::ArchKind::ARMV7M:
  46. return MachO::CPU_SUBTYPE_ARM_V7M;
  47. case ARM::ArchKind::ARMV7EM:
  48. return MachO::CPU_SUBTYPE_ARM_V7EM;
  49. }
  50. }
  51. static MachO::CPUSubTypeARM64 getARM64SubType(const Triple &T) {
  52. assert(T.isAArch64());
  53. if (T.isArch32Bit())
  54. return (MachO::CPUSubTypeARM64)MachO::CPU_SUBTYPE_ARM64_32_V8;
  55. if (T.isArm64e())
  56. return MachO::CPU_SUBTYPE_ARM64E;
  57. return MachO::CPU_SUBTYPE_ARM64_ALL;
  58. }
  59. static MachO::CPUSubTypePowerPC getPowerPCSubType(const Triple &T) {
  60. return MachO::CPU_SUBTYPE_POWERPC_ALL;
  61. }
  62. static Error unsupported(const char *Str, const Triple &T) {
  63. return createStringError(std::errc::invalid_argument,
  64. "Unsupported triple for mach-o cpu %s: %s", Str,
  65. T.str().c_str());
  66. }
  67. Expected<uint32_t> MachO::getCPUType(const Triple &T) {
  68. if (!T.isOSBinFormatMachO())
  69. return unsupported("type", T);
  70. if (T.isX86() && T.isArch32Bit())
  71. return MachO::CPU_TYPE_X86;
  72. if (T.isX86() && T.isArch64Bit())
  73. return MachO::CPU_TYPE_X86_64;
  74. if (T.isARM() || T.isThumb())
  75. return MachO::CPU_TYPE_ARM;
  76. if (T.isAArch64())
  77. return T.isArch32Bit() ? MachO::CPU_TYPE_ARM64_32 : MachO::CPU_TYPE_ARM64;
  78. if (T.getArch() == Triple::ppc)
  79. return MachO::CPU_TYPE_POWERPC;
  80. if (T.getArch() == Triple::ppc64)
  81. return MachO::CPU_TYPE_POWERPC64;
  82. return unsupported("type", T);
  83. }
  84. Expected<uint32_t> MachO::getCPUSubType(const Triple &T) {
  85. if (!T.isOSBinFormatMachO())
  86. return unsupported("subtype", T);
  87. if (T.isX86())
  88. return getX86SubType(T);
  89. if (T.isARM() || T.isThumb())
  90. return getARMSubType(T);
  91. if (T.isAArch64() || T.getArch() == Triple::aarch64_32)
  92. return getARM64SubType(T);
  93. if (T.getArch() == Triple::ppc || T.getArch() == Triple::ppc64)
  94. return getPowerPCSubType(T);
  95. return unsupported("subtype", T);
  96. }