1 //===- MachOUniversal.h - Mach-O universal binaries -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares Mach-O fat/universal binaries.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
15 #define LLVM_OBJECT_MACHOUNIVERSAL_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/Binary.h"
22 #include "llvm/Object/MachO.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/MachO.h"
29 class MachOUniversalBinary : public Binary {
30 virtual void anchor();
32 uint32_t NumberOfObjects;
35 const MachOUniversalBinary *Parent;
36 /// \brief Index of object in the universal binary.
38 /// \brief Descriptor of the object.
39 MachO::fat_arch Header;
42 ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
49 bool operator==(const ObjectForArch &Other) const {
50 return (Parent == Other.Parent) && (Index == Other.Index);
53 ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
54 uint32_t getCPUType() const { return Header.cputype; }
55 uint32_t getCPUSubType() const { return Header.cpusubtype; }
56 uint32_t getOffset() const { return Header.offset; }
57 uint32_t getSize() const { return Header.size; }
58 uint32_t getAlign() const { return Header.align; }
59 std::string getArchTypeName() const {
60 Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype);
61 return T.getArchName();
64 ErrorOr<std::unique_ptr<MachOObjectFile>> getAsObjectFile() const;
66 ErrorOr<std::unique_ptr<Archive>> getAsArchive() const;
69 class object_iterator {
72 object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
73 const ObjectForArch *operator->() const { return &Obj; }
74 const ObjectForArch &operator*() const { return Obj; }
76 bool operator==(const object_iterator &Other) const {
77 return Obj == Other.Obj;
79 bool operator!=(const object_iterator &Other) const {
80 return !(*this == Other);
83 object_iterator& operator++() { // Preincrement
89 MachOUniversalBinary(MemoryBufferRef Souce, std::error_code &EC);
90 static ErrorOr<std::unique_ptr<MachOUniversalBinary>>
91 create(MemoryBufferRef Source);
93 object_iterator begin_objects() const {
94 return ObjectForArch(this, 0);
96 object_iterator end_objects() const {
97 return ObjectForArch(nullptr, 0);
100 iterator_range<object_iterator> objects() const {
101 return make_range(begin_objects(), end_objects());
104 uint32_t getNumberOfObjects() const { return NumberOfObjects; }
107 static inline bool classof(Binary const *V) {
108 return V->isMachOUniversalBinary();
111 ErrorOr<std::unique_ptr<MachOObjectFile>>
112 getObjectForArch(StringRef ArchName) const;