1 //===- MachOUniversal.h - Mach-O universal binaries -------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file declares Mach-O fat/universal binaries. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_OBJECT_MACHOUNIVERSAL_H 15 #define LLVM_OBJECT_MACHOUNIVERSAL_H 16 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/Object/Binary.h" 20 #include "llvm/Object/Archive.h" 21 #include "llvm/Object/MachO.h" 22 #include "llvm/Support/ErrorOr.h" 23 #include "llvm/Support/MachO.h" 24 25 namespace llvm { 26 namespace object { 27 28 class ObjectFile; 29 30 class MachOUniversalBinary : public Binary { 31 virtual void anchor(); 32 33 uint32_t NumberOfObjects; 34 public: 35 class ObjectForArch { 36 const MachOUniversalBinary *Parent; 37 /// \brief Index of object in the universal binary. 38 uint32_t Index; 39 /// \brief Descriptor of the object. 40 MachO::fat_arch Header; 41 42 public: 43 ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index); 44 clear()45 void clear() { 46 Parent = nullptr; 47 Index = 0; 48 } 49 50 bool operator==(const ObjectForArch &Other) const { 51 return (Parent == Other.Parent) && (Index == Other.Index); 52 } 53 getNext()54 ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); } getCPUType()55 uint32_t getCPUType() const { return Header.cputype; } getArchTypeName()56 std::string getArchTypeName() const { 57 Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype); 58 return T.getArchName(); 59 } 60 61 ErrorOr<std::unique_ptr<ObjectFile>> getAsObjectFile() const; 62 63 std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const; 64 }; 65 66 class object_iterator { 67 ObjectForArch Obj; 68 public: object_iterator(const ObjectForArch & Obj)69 object_iterator(const ObjectForArch &Obj) : Obj(Obj) {} 70 const ObjectForArch* operator->() const { 71 return &Obj; 72 } 73 74 bool operator==(const object_iterator &Other) const { 75 return Obj == Other.Obj; 76 } 77 bool operator!=(const object_iterator &Other) const { 78 return !(*this == Other); 79 } 80 81 object_iterator& operator++() { // Preincrement 82 Obj = Obj.getNext(); 83 return *this; 84 } 85 }; 86 87 MachOUniversalBinary(std::unique_ptr<MemoryBuffer> Source, 88 std::error_code &ec); 89 static ErrorOr<MachOUniversalBinary *> 90 create(std::unique_ptr<MemoryBuffer> Source); 91 begin_objects()92 object_iterator begin_objects() const { 93 return ObjectForArch(this, 0); 94 } end_objects()95 object_iterator end_objects() const { 96 return ObjectForArch(nullptr, 0); 97 } 98 getNumberOfObjects()99 uint32_t getNumberOfObjects() const { return NumberOfObjects; } 100 101 // Cast methods. classof(Binary const * V)102 static inline bool classof(Binary const *V) { 103 return V->isMachOUniversalBinary(); 104 } 105 106 ErrorOr<std::unique_ptr<ObjectFile>> 107 getObjectForArch(Triple::ArchType Arch) const; 108 }; 109 110 } 111 } 112 113 #endif 114