LLVM  10.0.0svn
MachOUniversal.h
Go to the documentation of this file.
1 //===- MachOUniversal.h - Mach-O universal binaries -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares Mach-O fat/universal binaries.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
14 #define LLVM_OBJECT_MACHOUNIVERSAL_H
15 
16 #include "llvm/ADT/Triple.h"
19 #include "llvm/Object/Archive.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Object/MachO.h"
22 
23 namespace llvm {
24 class StringRef;
25 
26 namespace object {
27 
28 class MachOUniversalBinary : public Binary {
29  virtual void anchor();
30 
31  uint32_t Magic;
32  uint32_t NumberOfObjects;
33 public:
34  class ObjectForArch {
35  const MachOUniversalBinary *Parent;
36  /// Index of object in the universal binary.
37  uint32_t Index;
38  /// Descriptor of the object.
39  MachO::fat_arch Header;
40  MachO::fat_arch_64 Header64;
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 {
56  if (Parent->getMagic() == MachO::FAT_MAGIC)
57  return Header.cputype;
58  else // Parent->getMagic() == MachO::FAT_MAGIC_64
59  return Header64.cputype;
60  }
62  if (Parent->getMagic() == MachO::FAT_MAGIC)
63  return Header.cpusubtype;
64  else // Parent->getMagic() == MachO::FAT_MAGIC_64
65  return Header64.cpusubtype;
66  }
67  uint32_t getOffset() const {
68  if (Parent->getMagic() == MachO::FAT_MAGIC)
69  return Header.offset;
70  else // Parent->getMagic() == MachO::FAT_MAGIC_64
71  return Header64.offset;
72  }
73  uint32_t getSize() const {
74  if (Parent->getMagic() == MachO::FAT_MAGIC)
75  return Header.size;
76  else // Parent->getMagic() == MachO::FAT_MAGIC_64
77  return Header64.size;
78  }
79  uint32_t getAlign() const {
80  if (Parent->getMagic() == MachO::FAT_MAGIC)
81  return Header.align;
82  else // Parent->getMagic() == MachO::FAT_MAGIC_64
83  return Header64.align;
84  }
86  if (Parent->getMagic() == MachO::FAT_MAGIC)
87  return 0;
88  else // Parent->getMagic() == MachO::FAT_MAGIC_64
89  return Header64.reserved;
90  }
91  std::string getArchFlagName() const {
92  const char *McpuDefault, *ArchFlag;
93  if (Parent->getMagic() == MachO::FAT_MAGIC) {
94  Triple T =
96  &McpuDefault, &ArchFlag);
97  } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
98  Triple T =
100  Header64.cpusubtype,
101  &McpuDefault, &ArchFlag);
102  }
103  if (ArchFlag) {
104  std::string ArchFlagName(ArchFlag);
105  return ArchFlagName;
106  } else {
107  std::string ArchFlagName("");
108  return ArchFlagName;
109  }
110  }
111 
113 
115  };
116 
118  ObjectForArch Obj;
119  public:
120  object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
121  const ObjectForArch *operator->() const { return &Obj; }
122  const ObjectForArch &operator*() const { return Obj; }
123 
124  bool operator==(const object_iterator &Other) const {
125  return Obj == Other.Obj;
126  }
127  bool operator!=(const object_iterator &Other) const {
128  return !(*this == Other);
129  }
130 
131  object_iterator& operator++() { // Preincrement
132  Obj = Obj.getNext();
133  return *this;
134  }
135  };
136 
140 
142  return ObjectForArch(this, 0);
143  }
145  return ObjectForArch(nullptr, 0);
146  }
147 
149  return make_range(begin_objects(), end_objects());
150  }
151 
152  uint32_t getMagic() const { return Magic; }
153  uint32_t getNumberOfObjects() const { return NumberOfObjects; }
154 
155  // Cast methods.
156  static bool classof(Binary const *V) {
157  return V->isMachOUniversalBinary();
158  }
159 
161  getObjectForArch(StringRef ArchName) const;
162 };
163 
164 }
165 }
166 
167 #endif
uint32_t align
Definition: MachO.h:931
object_iterator end_objects() const
Expected< std::unique_ptr< Archive > > getAsArchive() const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index)
bool operator==(const ObjectForArch &Other) const
uint32_t cpusubtype
Definition: MachO.h:928
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
bool operator==(const object_iterator &Other) const
bool operator!=(const object_iterator &Other) const
Expected< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
MachOUniversalBinary(MemoryBufferRef Souce, Error &Err)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
iterator_range< object_iterator > objects() const
Expected< std::unique_ptr< MachOObjectFile > > getObjectForArch(StringRef ArchName) const
bool isMachOUniversalBinary() const
Definition: Binary.h:110
Triple getArchTriple(const char **McpuDefault=nullptr) const
object_iterator begin_objects() const
A range adaptor for a pair of iterators.
uint32_t cputype
Definition: MachO.h:927
uint32_t offset
Definition: MachO.h:929
static bool classof(Binary const *V)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48