MachOUniversal.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MachOUniversal.h - Mach-O universal binaries -------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares Mach-O fat/universal binaries.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
  18. #define LLVM_OBJECT_MACHOUNIVERSAL_H
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/ADT/iterator_range.h"
  21. #include "llvm/BinaryFormat/MachO.h"
  22. #include "llvm/Object/Archive.h"
  23. #include "llvm/Object/Binary.h"
  24. #include "llvm/Object/MachO.h"
  25. namespace llvm {
  26. class StringRef;
  27. class LLVMContext;
  28. namespace object {
  29. class IRObjectFile;
  30. class MachOUniversalBinary : public Binary {
  31. virtual void anchor();
  32. uint32_t Magic;
  33. uint32_t NumberOfObjects;
  34. public:
  35. static constexpr uint32_t MaxSectionAlignment = 15; /* 2**15 or 0x8000 */
  36. class ObjectForArch {
  37. const MachOUniversalBinary *Parent;
  38. /// Index of object in the universal binary.
  39. uint32_t Index;
  40. /// Descriptor of the object.
  41. MachO::fat_arch Header;
  42. MachO::fat_arch_64 Header64;
  43. public:
  44. ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
  45. void clear() {
  46. Parent = nullptr;
  47. Index = 0;
  48. }
  49. bool operator==(const ObjectForArch &Other) const {
  50. return (Parent == Other.Parent) && (Index == Other.Index);
  51. }
  52. ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
  53. uint32_t getCPUType() const {
  54. if (Parent->getMagic() == MachO::FAT_MAGIC)
  55. return Header.cputype;
  56. else // Parent->getMagic() == MachO::FAT_MAGIC_64
  57. return Header64.cputype;
  58. }
  59. uint32_t getCPUSubType() const {
  60. if (Parent->getMagic() == MachO::FAT_MAGIC)
  61. return Header.cpusubtype;
  62. else // Parent->getMagic() == MachO::FAT_MAGIC_64
  63. return Header64.cpusubtype;
  64. }
  65. uint64_t getOffset() const {
  66. if (Parent->getMagic() == MachO::FAT_MAGIC)
  67. return Header.offset;
  68. else // Parent->getMagic() == MachO::FAT_MAGIC_64
  69. return Header64.offset;
  70. }
  71. uint64_t getSize() const {
  72. if (Parent->getMagic() == MachO::FAT_MAGIC)
  73. return Header.size;
  74. else // Parent->getMagic() == MachO::FAT_MAGIC_64
  75. return Header64.size;
  76. }
  77. uint32_t getAlign() const {
  78. if (Parent->getMagic() == MachO::FAT_MAGIC)
  79. return Header.align;
  80. else // Parent->getMagic() == MachO::FAT_MAGIC_64
  81. return Header64.align;
  82. }
  83. uint32_t getReserved() const {
  84. if (Parent->getMagic() == MachO::FAT_MAGIC)
  85. return 0;
  86. else // Parent->getMagic() == MachO::FAT_MAGIC_64
  87. return Header64.reserved;
  88. }
  89. Triple getTriple() const {
  90. return MachOObjectFile::getArchTriple(getCPUType(), getCPUSubType());
  91. }
  92. std::string getArchFlagName() const {
  93. const char *McpuDefault, *ArchFlag;
  94. MachOObjectFile::getArchTriple(getCPUType(), getCPUSubType(),
  95. &McpuDefault, &ArchFlag);
  96. return ArchFlag ? ArchFlag : std::string();
  97. }
  98. Expected<std::unique_ptr<MachOObjectFile>> getAsObjectFile() const;
  99. Expected<std::unique_ptr<IRObjectFile>>
  100. getAsIRObject(LLVMContext &Ctx) const;
  101. Expected<std::unique_ptr<Archive>> getAsArchive() const;
  102. };
  103. class object_iterator {
  104. ObjectForArch Obj;
  105. public:
  106. object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
  107. const ObjectForArch *operator->() const { return &Obj; }
  108. const ObjectForArch &operator*() const { return Obj; }
  109. bool operator==(const object_iterator &Other) const {
  110. return Obj == Other.Obj;
  111. }
  112. bool operator!=(const object_iterator &Other) const {
  113. return !(*this == Other);
  114. }
  115. object_iterator& operator++() { // Preincrement
  116. Obj = Obj.getNext();
  117. return *this;
  118. }
  119. };
  120. MachOUniversalBinary(MemoryBufferRef Souce, Error &Err);
  121. static Expected<std::unique_ptr<MachOUniversalBinary>>
  122. create(MemoryBufferRef Source);
  123. object_iterator begin_objects() const {
  124. return ObjectForArch(this, 0);
  125. }
  126. object_iterator end_objects() const {
  127. return ObjectForArch(nullptr, 0);
  128. }
  129. iterator_range<object_iterator> objects() const {
  130. return make_range(begin_objects(), end_objects());
  131. }
  132. uint32_t getMagic() const { return Magic; }
  133. uint32_t getNumberOfObjects() const { return NumberOfObjects; }
  134. // Cast methods.
  135. static bool classof(Binary const *V) {
  136. return V->isMachOUniversalBinary();
  137. }
  138. Expected<ObjectForArch>
  139. getObjectForArch(StringRef ArchName) const;
  140. Expected<std::unique_ptr<MachOObjectFile>>
  141. getMachOObjectForArch(StringRef ArchName) const;
  142. Expected<std::unique_ptr<IRObjectFile>>
  143. getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const;
  144. Expected<std::unique_ptr<Archive>>
  145. getArchiveForArch(StringRef ArchName) const;
  146. };
  147. }
  148. }
  149. #endif
  150. #ifdef __GNUC__
  151. #pragma GCC diagnostic pop
  152. #endif