Change the output of llvm-nm and llvm-size for Mach-O universal files (aka
[oota-llvm.git] / include / llvm / Object / MachOUniversal.h
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
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
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));
58     }
59
60     std::error_code getAsObjectFile(std::unique_ptr<ObjectFile> &Result) const;
61
62     std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
63   };
64
65   class object_iterator {
66     ObjectForArch Obj;
67   public:
68     object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
69     const ObjectForArch* operator->() const {
70       return &Obj;
71     }
72
73     bool operator==(const object_iterator &Other) const {
74       return Obj == Other.Obj;
75     }
76     bool operator!=(const object_iterator &Other) const {
77       return !(*this == Other);
78     }
79
80     object_iterator& operator++() {  // Preincrement
81       Obj = Obj.getNext();
82       return *this;
83     }
84   };
85
86   MachOUniversalBinary(MemoryBuffer *Source, std::error_code &ec);
87   static ErrorOr<MachOUniversalBinary*> create(MemoryBuffer *Source);
88
89   object_iterator begin_objects() const {
90     return ObjectForArch(this, 0);
91   }
92   object_iterator end_objects() const {
93     return ObjectForArch(nullptr, 0);
94   }
95
96   uint32_t getNumberOfObjects() const { return NumberOfObjects; }
97
98   // Cast methods.
99   static inline bool classof(Binary const *V) {
100     return V->isMachOUniversalBinary();
101   }
102
103   std::error_code getObjectForArch(Triple::ArchType Arch,
104                                    std::unique_ptr<ObjectFile> &Result) const;
105 };
106
107 }
108 }
109
110 #endif