LLVM API Documentation
00001 //===- MachOUniversal.h - Mach-O universal binaries -------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares Mach-O fat/universal binaries. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_OBJECT_MACHOUNIVERSAL_H 00015 #define LLVM_OBJECT_MACHOUNIVERSAL_H 00016 00017 #include "llvm/ADT/StringRef.h" 00018 #include "llvm/ADT/Triple.h" 00019 #include "llvm/Object/Binary.h" 00020 #include "llvm/Object/Archive.h" 00021 #include "llvm/Object/MachO.h" 00022 #include "llvm/Support/ErrorOr.h" 00023 #include "llvm/Support/MachO.h" 00024 00025 namespace llvm { 00026 namespace object { 00027 00028 class ObjectFile; 00029 00030 class MachOUniversalBinary : public Binary { 00031 virtual void anchor(); 00032 00033 uint32_t NumberOfObjects; 00034 public: 00035 class ObjectForArch { 00036 const MachOUniversalBinary *Parent; 00037 /// \brief Index of object in the universal binary. 00038 uint32_t Index; 00039 /// \brief Descriptor of the object. 00040 MachO::fat_arch Header; 00041 00042 public: 00043 ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index); 00044 00045 void clear() { 00046 Parent = nullptr; 00047 Index = 0; 00048 } 00049 00050 bool operator==(const ObjectForArch &Other) const { 00051 return (Parent == Other.Parent) && (Index == Other.Index); 00052 } 00053 00054 ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); } 00055 uint32_t getCPUType() const { return Header.cputype; } 00056 std::string getArchTypeName() const { 00057 Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype); 00058 return T.getArchName(); 00059 } 00060 00061 ErrorOr<std::unique_ptr<ObjectFile>> getAsObjectFile() const; 00062 00063 std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const; 00064 }; 00065 00066 class object_iterator { 00067 ObjectForArch Obj; 00068 public: 00069 object_iterator(const ObjectForArch &Obj) : Obj(Obj) {} 00070 const ObjectForArch* operator->() const { 00071 return &Obj; 00072 } 00073 00074 bool operator==(const object_iterator &Other) const { 00075 return Obj == Other.Obj; 00076 } 00077 bool operator!=(const object_iterator &Other) const { 00078 return !(*this == Other); 00079 } 00080 00081 object_iterator& operator++() { // Preincrement 00082 Obj = Obj.getNext(); 00083 return *this; 00084 } 00085 }; 00086 00087 MachOUniversalBinary(MemoryBufferRef Souce, std::error_code &EC); 00088 static ErrorOr<std::unique_ptr<MachOUniversalBinary>> 00089 create(MemoryBufferRef Source); 00090 00091 object_iterator begin_objects() const { 00092 return ObjectForArch(this, 0); 00093 } 00094 object_iterator end_objects() const { 00095 return ObjectForArch(nullptr, 0); 00096 } 00097 00098 uint32_t getNumberOfObjects() const { return NumberOfObjects; } 00099 00100 // Cast methods. 00101 static inline bool classof(Binary const *V) { 00102 return V->isMachOUniversalBinary(); 00103 } 00104 00105 ErrorOr<std::unique_ptr<ObjectFile>> 00106 getObjectForArch(Triple::ArchType Arch) const; 00107 }; 00108 00109 } 00110 } 00111 00112 #endif