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/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"
30 class MachOUniversalBinary : public Binary {
31 virtual void anchor();
33 uint32_t NumberOfObjects;
36 const MachOUniversalBinary *Parent;
37 /// \brief Index of object in the universal binary.
39 /// \brief Descriptor of the object.
40 MachO::fat_arch Header;
43 ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
50 bool operator==(const ObjectForArch &Other) const {
51 return (Parent == Other.Parent) && (Index == Other.Index);
54 ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
55 uint32_t getCPUType() const { return Header.cputype; }
56 std::string getArchTypeName() const {
57 return Triple::getArchTypeName(MachOObjectFile::getArch(Header.cputype));
60 ErrorOr<std::unique_ptr<ObjectFile>> getAsObjectFile() const;
62 std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
65 class object_iterator {
68 object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
69 const ObjectForArch* operator->() const {
73 bool operator==(const object_iterator &Other) const {
74 return Obj == Other.Obj;
76 bool operator!=(const object_iterator &Other) const {
77 return !(*this == Other);
80 object_iterator& operator++() { // Preincrement
86 MachOUniversalBinary(std::unique_ptr<MemoryBuffer> Source,
88 static ErrorOr<MachOUniversalBinary *>
89 create(std::unique_ptr<MemoryBuffer> Source);
91 object_iterator begin_objects() const {
92 return ObjectForArch(this, 0);
94 object_iterator end_objects() const {
95 return ObjectForArch(nullptr, 0);
98 uint32_t getNumberOfObjects() const { return NumberOfObjects; }
101 static inline bool classof(Binary const *V) {
102 return V->isMachOUniversalBinary();
105 ErrorOr<std::unique_ptr<ObjectFile>>
106 getObjectForArch(Triple::ArchType Arch) const;