LLVM  14.0.0git
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 class LLVMContext;
26 
27 namespace object {
28 class IRObjectFile;
29 
30 class MachOUniversalBinary : public Binary {
31  virtual void anchor();
32 
33  uint32_t Magic;
34  uint32_t NumberOfObjects;
35 public:
36  static constexpr uint32_t MaxSectionAlignment = 15; /* 2**15 or 0x8000 */
37 
38  class ObjectForArch {
39  const MachOUniversalBinary *Parent;
40  /// Index of object in the universal binary.
41  uint32_t Index;
42  /// Descriptor of the object.
43  MachO::fat_arch Header;
44  MachO::fat_arch_64 Header64;
45 
46  public:
47  ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
48 
49  void clear() {
50  Parent = nullptr;
51  Index = 0;
52  }
53 
54  bool operator==(const ObjectForArch &Other) const {
55  return (Parent == Other.Parent) && (Index == Other.Index);
56  }
57 
58  ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
59  uint32_t getCPUType() const {
60  if (Parent->getMagic() == MachO::FAT_MAGIC)
61  return Header.cputype;
62  else // Parent->getMagic() == MachO::FAT_MAGIC_64
63  return Header64.cputype;
64  }
66  if (Parent->getMagic() == MachO::FAT_MAGIC)
67  return Header.cpusubtype;
68  else // Parent->getMagic() == MachO::FAT_MAGIC_64
69  return Header64.cpusubtype;
70  }
71  uint64_t getOffset() const {
72  if (Parent->getMagic() == MachO::FAT_MAGIC)
73  return Header.offset;
74  else // Parent->getMagic() == MachO::FAT_MAGIC_64
75  return Header64.offset;
76  }
77  uint64_t getSize() const {
78  if (Parent->getMagic() == MachO::FAT_MAGIC)
79  return Header.size;
80  else // Parent->getMagic() == MachO::FAT_MAGIC_64
81  return Header64.size;
82  }
83  uint32_t getAlign() const {
84  if (Parent->getMagic() == MachO::FAT_MAGIC)
85  return Header.align;
86  else // Parent->getMagic() == MachO::FAT_MAGIC_64
87  return Header64.align;
88  }
90  if (Parent->getMagic() == MachO::FAT_MAGIC)
91  return 0;
92  else // Parent->getMagic() == MachO::FAT_MAGIC_64
93  return Header64.reserved;
94  }
95  Triple getTriple() const {
97  }
98  std::string getArchFlagName() const {
99  const char *McpuDefault, *ArchFlag;
101  &McpuDefault, &ArchFlag);
102  return ArchFlag ? ArchFlag : std::string();
103  }
104 
107  getAsIRObject(LLVMContext &Ctx) const;
108 
110  };
111 
113  ObjectForArch Obj;
114  public:
115  object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
116  const ObjectForArch *operator->() const { return &Obj; }
117  const ObjectForArch &operator*() const { return Obj; }
118 
119  bool operator==(const object_iterator &Other) const {
120  return Obj == Other.Obj;
121  }
122  bool operator!=(const object_iterator &Other) const {
123  return !(*this == Other);
124  }
125 
126  object_iterator& operator++() { // Preincrement
127  Obj = Obj.getNext();
128  return *this;
129  }
130  };
131 
135 
137  return ObjectForArch(this, 0);
138  }
140  return ObjectForArch(nullptr, 0);
141  }
142 
144  return make_range(begin_objects(), end_objects());
145  }
146 
147  uint32_t getMagic() const { return Magic; }
148  uint32_t getNumberOfObjects() const { return NumberOfObjects; }
149 
150  // Cast methods.
151  static bool classof(Binary const *V) {
152  return V->isMachOUniversalBinary();
153  }
154 
156  getObjectForArch(StringRef ArchName) const;
157 
159  getMachOObjectForArch(StringRef ArchName) const;
160 
162  getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const;
163 
165  getArchiveForArch(StringRef ArchName) const;
166 };
167 
168 }
169 }
170 
171 #endif
llvm::MachO::fat_arch_64::offset
uint64_t offset
Definition: MachO.h:951
llvm::MachO::fat_arch_64::reserved
uint32_t reserved
Definition: MachO.h:954
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
llvm::MachO::fat_arch_64::align
uint32_t align
Definition: MachO.h:953
llvm::MachO::fat_arch::align
uint32_t align
Definition: MachO.h:945
llvm::MachO::fat_arch_64::size
uint64_t size
Definition: MachO.h:952
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::object::MachOUniversalBinary::getNumberOfObjects
uint32_t getNumberOfObjects() const
Definition: MachOUniversal.h:148
llvm::object::MachOUniversalBinary::object_iterator::operator*
const ObjectForArch & operator*() const
Definition: MachOUniversal.h:117
llvm::object::MachOUniversalBinary::ObjectForArch::getAlign
uint32_t getAlign() const
Definition: MachOUniversal.h:83
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::MachOUniversalBinary::getArchiveForArch
Expected< std::unique_ptr< Archive > > getArchiveForArch(StringRef ArchName) const
Definition: MachOUniversal.cpp:267
llvm::object::MachOUniversalBinary::MachOUniversalBinary
MachOUniversalBinary(MemoryBufferRef Souce, Error &Err)
Definition: MachOUniversal.cpp:132
llvm::object::MachOUniversalBinary::object_iterator::operator++
object_iterator & operator++()
Definition: MachOUniversal.h:126
MachO.h
llvm::MachO::fat_arch::size
uint32_t size
Definition: MachO.h:944
llvm::object::MachOUniversalBinary::ObjectForArch::getNext
ObjectForArch getNext() const
Definition: MachOUniversal.h:58
llvm::object::MachOUniversalBinary::ObjectForArch
Definition: MachOUniversal.h:38
llvm::object::MachOUniversalBinary::ObjectForArch::getAsObjectFile
Expected< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
Definition: MachOUniversal.cpp:64
llvm::MachO::fat_arch
Definition: MachO.h:940
llvm::MachO::fat_arch_64::cputype
uint32_t cputype
Definition: MachO.h:949
llvm::object::MachOUniversalBinary::ObjectForArch::getReserved
uint32_t getReserved() const
Definition: MachOUniversal.h:89
llvm::object::MachOUniversalBinary::ObjectForArch::getArchFlagName
std::string getArchFlagName() const
Definition: MachOUniversal.h:98
llvm::object::MachOUniversalBinary::getMachOObjectForArch
Expected< std::unique_ptr< MachOObjectFile > > getMachOObjectForArch(StringRef ArchName) const
Definition: MachOUniversal.cpp:250
llvm::object::MachOUniversalBinary::getObjectForArch
Expected< ObjectForArch > getObjectForArch(StringRef ArchName) const
Definition: MachOUniversal.cpp:234
llvm::MachO::FAT_MAGIC
@ FAT_MAGIC
Definition: MachO.h:34
llvm::object::MachOUniversalBinary::ObjectForArch::getSize
uint64_t getSize() const
Definition: MachOUniversal.h:77
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
uint64_t
llvm::object::MachOUniversalBinary::ObjectForArch::getAsIRObject
Expected< std::unique_ptr< IRObjectFile > > getAsIRObject(LLVMContext &Ctx) const
Definition: MachOUniversal.cpp:85
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::MachO::fat_arch::offset
uint32_t offset
Definition: MachO.h:943
Archive.h
llvm::object::MachOObjectFile::getArchTriple
Triple getArchTriple(const char **McpuDefault=nullptr) const
llvm::object::MachOUniversalBinary::MaxSectionAlignment
static constexpr uint32_t MaxSectionAlignment
Definition: MachOUniversal.h:36
iterator_range.h
llvm::object::MachOUniversalBinary::objects
iterator_range< object_iterator > objects() const
Definition: MachOUniversal.h:143
llvm::object::MachOUniversalBinary::ObjectForArch::ObjectForArch
ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index)
Definition: MachOUniversal.cpp:42
llvm::MachO::fat_arch_64::cpusubtype
uint32_t cpusubtype
Definition: MachO.h:950
Triple.h
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::object::MachOUniversalBinary::object_iterator::object_iterator
object_iterator(const ObjectForArch &Obj)
Definition: MachOUniversal.h:115
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::object::Binary
Definition: Binary.h:32
llvm::object::MachOUniversalBinary::getIRObjectForArch
Expected< std::unique_ptr< IRObjectFile > > getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const
Definition: MachOUniversal.cpp:258
uint32_t
llvm::object::MachOUniversalBinary::object_iterator
Definition: MachOUniversal.h:112
llvm::object::MachOUniversalBinary::ObjectForArch::clear
void clear()
Definition: MachOUniversal.h:49
llvm::object::MachOUniversalBinary::object_iterator::operator->
const ObjectForArch * operator->() const
Definition: MachOUniversal.h:116
llvm::object::MachOUniversalBinary::ObjectForArch::getTriple
Triple getTriple() const
Definition: MachOUniversal.h:95
llvm::object::MachOUniversalBinary::create
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
Definition: MachOUniversal.cpp:123
llvm::object::MachOUniversalBinary::ObjectForArch::getAsArchive
Expected< std::unique_ptr< Archive > > getAsArchive() const
Definition: MachOUniversal.cpp:104
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::object::MachOUniversalBinary::begin_objects
object_iterator begin_objects() const
Definition: MachOUniversal.h:136
llvm::object::MachOUniversalBinary::classof
static bool classof(Binary const *V)
Definition: MachOUniversal.h:151
llvm::object::MachOUniversalBinary::ObjectForArch::getCPUType
uint32_t getCPUType() const
Definition: MachOUniversal.h:59
Binary.h
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::object::Binary::isMachOUniversalBinary
bool isMachOUniversalBinary() const
Definition: Binary.h:114
llvm::MachO::fat_arch_64
Definition: MachO.h:948
llvm::object::MachOUniversalBinary::end_objects
object_iterator end_objects() const
Definition: MachOUniversal.h:139
llvm::object::MachOUniversalBinary::ObjectForArch::operator==
bool operator==(const ObjectForArch &Other) const
Definition: MachOUniversal.h:54
MachO.h
llvm::MachO::fat_arch::cpusubtype
uint32_t cpusubtype
Definition: MachO.h:942
llvm::object::MachOUniversalBinary::object_iterator::operator==
bool operator==(const object_iterator &Other) const
Definition: MachOUniversal.h:119
llvm::object::MachOUniversalBinary::ObjectForArch::getOffset
uint64_t getOffset() const
Definition: MachOUniversal.h:71
llvm::object::MachOUniversalBinary::getMagic
uint32_t getMagic() const
Definition: MachOUniversal.h:147
llvm::object::MachOUniversalBinary::object_iterator::operator!=
bool operator!=(const object_iterator &Other) const
Definition: MachOUniversal.h:122
llvm::MachO::fat_arch::cputype
uint32_t cputype
Definition: MachO.h:941
llvm::object::MachOUniversalBinary
Definition: MachOUniversal.h:30
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1198
llvm::object::MachOUniversalBinary::ObjectForArch::getCPUSubType
uint32_t getCPUSubType() const
Definition: MachOUniversal.h:65