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 Module;
26 class LLVMContext;
27 
28 namespace object {
29 class IRObjectFile;
30 
31 class MachOUniversalBinary : public Binary {
32  virtual void anchor();
33 
34  uint32_t Magic;
35  uint32_t NumberOfObjects;
36 public:
37  static constexpr uint32_t MaxSectionAlignment = 15; /* 2**15 or 0x8000 */
38 
39  class ObjectForArch {
40  const MachOUniversalBinary *Parent;
41  /// Index of object in the universal binary.
42  uint32_t Index;
43  /// Descriptor of the object.
44  MachO::fat_arch Header;
45  MachO::fat_arch_64 Header64;
46 
47  public:
48  ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
49 
50  void clear() {
51  Parent = nullptr;
52  Index = 0;
53  }
54 
55  bool operator==(const ObjectForArch &Other) const {
56  return (Parent == Other.Parent) && (Index == Other.Index);
57  }
58 
59  ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
60  uint32_t getCPUType() const {
61  if (Parent->getMagic() == MachO::FAT_MAGIC)
62  return Header.cputype;
63  else // Parent->getMagic() == MachO::FAT_MAGIC_64
64  return Header64.cputype;
65  }
67  if (Parent->getMagic() == MachO::FAT_MAGIC)
68  return Header.cpusubtype;
69  else // Parent->getMagic() == MachO::FAT_MAGIC_64
70  return Header64.cpusubtype;
71  }
72  uint64_t getOffset() const {
73  if (Parent->getMagic() == MachO::FAT_MAGIC)
74  return Header.offset;
75  else // Parent->getMagic() == MachO::FAT_MAGIC_64
76  return Header64.offset;
77  }
78  uint64_t getSize() const {
79  if (Parent->getMagic() == MachO::FAT_MAGIC)
80  return Header.size;
81  else // Parent->getMagic() == MachO::FAT_MAGIC_64
82  return Header64.size;
83  }
84  uint32_t getAlign() const {
85  if (Parent->getMagic() == MachO::FAT_MAGIC)
86  return Header.align;
87  else // Parent->getMagic() == MachO::FAT_MAGIC_64
88  return Header64.align;
89  }
91  if (Parent->getMagic() == MachO::FAT_MAGIC)
92  return 0;
93  else // Parent->getMagic() == MachO::FAT_MAGIC_64
94  return Header64.reserved;
95  }
96  Triple getTriple() const {
98  }
99  std::string getArchFlagName() const {
100  const char *McpuDefault, *ArchFlag;
102  &McpuDefault, &ArchFlag);
103  return ArchFlag ? ArchFlag : std::string();
104  }
105 
108  getAsIRObject(LLVMContext &Ctx) const;
109 
111  };
112 
114  ObjectForArch Obj;
115  public:
116  object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
117  const ObjectForArch *operator->() const { return &Obj; }
118  const ObjectForArch &operator*() const { return Obj; }
119 
120  bool operator==(const object_iterator &Other) const {
121  return Obj == Other.Obj;
122  }
123  bool operator!=(const object_iterator &Other) const {
124  return !(*this == Other);
125  }
126 
127  object_iterator& operator++() { // Preincrement
128  Obj = Obj.getNext();
129  return *this;
130  }
131  };
132 
136 
138  return ObjectForArch(this, 0);
139  }
141  return ObjectForArch(nullptr, 0);
142  }
143 
145  return make_range(begin_objects(), end_objects());
146  }
147 
148  uint32_t getMagic() const { return Magic; }
149  uint32_t getNumberOfObjects() const { return NumberOfObjects; }
150 
151  // Cast methods.
152  static bool classof(Binary const *V) {
153  return V->isMachOUniversalBinary();
154  }
155 
157  getObjectForArch(StringRef ArchName) const;
158 
160  getMachOObjectForArch(StringRef ArchName) const;
161 
163  getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const;
164 
166  getArchiveForArch(StringRef ArchName) const;
167 };
168 
169 }
170 }
171 
172 #endif
llvm::MachO::fat_arch_64::offset
uint64_t offset
Definition: MachO.h:950
llvm::MachO::fat_arch_64::reserved
uint32_t reserved
Definition: MachO.h:953
llvm
---------------------— PointerInfo ------------------------------------—
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:952
llvm::MachO::fat_arch::align
uint32_t align
Definition: MachO.h:944
llvm::MachO::fat_arch_64::size
uint64_t size
Definition: MachO.h:951
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::object::MachOUniversalBinary::getNumberOfObjects
uint32_t getNumberOfObjects() const
Definition: MachOUniversal.h:149
llvm::object::MachOUniversalBinary::object_iterator::operator*
const ObjectForArch & operator*() const
Definition: MachOUniversal.h:118
llvm::object::MachOUniversalBinary::ObjectForArch::getAlign
uint32_t getAlign() const
Definition: MachOUniversal.h:84
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:127
MachO.h
llvm::MachO::fat_arch::size
uint32_t size
Definition: MachO.h:943
llvm::object::MachOUniversalBinary::ObjectForArch::getNext
ObjectForArch getNext() const
Definition: MachOUniversal.h:59
llvm::object::MachOUniversalBinary::ObjectForArch
Definition: MachOUniversal.h:39
llvm::object::MachOUniversalBinary::ObjectForArch::getAsObjectFile
Expected< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
Definition: MachOUniversal.cpp:64
llvm::MachO::fat_arch
Definition: MachO.h:939
llvm::MachO::fat_arch_64::cputype
uint32_t cputype
Definition: MachO.h:948
llvm::object::MachOUniversalBinary::ObjectForArch::getReserved
uint32_t getReserved() const
Definition: MachOUniversal.h:90
llvm::object::MachOUniversalBinary::ObjectForArch::getArchFlagName
std::string getArchFlagName() const
Definition: MachOUniversal.h:99
llvm::MachO::FAT_MAGIC
@ FAT_MAGIC
Definition: MachO.h:34
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::object::MachOUniversalBinary::ObjectForArch::getSize
uint64_t getSize() const
Definition: MachOUniversal.h:78
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
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:68
llvm::MachO::fat_arch::offset
uint32_t offset
Definition: MachO.h:942
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:37
iterator_range.h
llvm::object::MachOUniversalBinary::objects
iterator_range< object_iterator > objects() const
Definition: MachOUniversal.h:144
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:949
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:116
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
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
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::object::MachOUniversalBinary::object_iterator
Definition: MachOUniversal.h:113
llvm::object::MachOUniversalBinary::ObjectForArch::clear
void clear()
Definition: MachOUniversal.h:50
llvm::object::MachOUniversalBinary::object_iterator::operator->
const ObjectForArch * operator->() const
Definition: MachOUniversal.h:117
llvm::object::MachOUniversalBinary::ObjectForArch::getTriple
Triple getTriple() const
Definition: MachOUniversal.h:96
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:137
llvm::object::MachOUniversalBinary::classof
static bool classof(Binary const *V)
Definition: MachOUniversal.h:152
llvm::object::MachOUniversalBinary::ObjectForArch::getCPUType
uint32_t getCPUType() const
Definition: MachOUniversal.h:60
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:947
llvm::object::MachOUniversalBinary::end_objects
object_iterator end_objects() const
Definition: MachOUniversal.h:140
llvm::object::MachOUniversalBinary::ObjectForArch::operator==
bool operator==(const ObjectForArch &Other) const
Definition: MachOUniversal.h:55
MachO.h
llvm::MachO::fat_arch::cpusubtype
uint32_t cpusubtype
Definition: MachO.h:941
llvm::object::MachOUniversalBinary::object_iterator::operator==
bool operator==(const object_iterator &Other) const
Definition: MachOUniversal.h:120
llvm::object::MachOUniversalBinary::ObjectForArch::getOffset
uint64_t getOffset() const
Definition: MachOUniversal.h:72
llvm::object::MachOUniversalBinary::getMagic
uint32_t getMagic() const
Definition: MachOUniversal.h:148
llvm::object::MachOUniversalBinary::object_iterator::operator!=
bool operator!=(const object_iterator &Other) const
Definition: MachOUniversal.h:123
llvm::MachO::fat_arch::cputype
uint32_t cputype
Definition: MachO.h:940
llvm::object::MachOUniversalBinary
Definition: MachOUniversal.h:31
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172
llvm::object::MachOUniversalBinary::ObjectForArch::getCPUSubType
uint32_t getCPUSubType() const
Definition: MachOUniversal.h:66