LLVM 22.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
18#include "llvm/Object/Binary.h"
19#include "llvm/Object/MachO.h"
22
23namespace llvm {
24class StringRef;
25class LLVMContext;
26
27namespace object {
28class Archive;
29class IRObjectFile;
30
32 virtual void anchor();
33
34 uint32_t Magic;
35 uint32_t NumberOfObjects;
36public:
37 static constexpr uint32_t MaxSectionAlignment = 15; /* 2**15 or 0x8000 */
38
40 const MachOUniversalBinary *Parent;
41 /// Index of object in the universal binary.
43 /// Descriptor of the object.
44 MachO::fat_arch Header;
45 MachO::fat_arch_64 Header64;
46
47 public:
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); }
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 }
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 }
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 {
97 return MachOObjectFile::getArchTriple(getCPUType(), getCPUSubType());
98 }
99 std::string getArchFlagName() const {
100 const char *McpuDefault, *ArchFlag;
101 MachOObjectFile::getArchTriple(getCPUType(), getCPUSubType(),
102 &McpuDefault, &ArchFlag);
103 return ArchFlag ? ArchFlag : std::string();
104 }
105
108 getAsIRObject(LLVMContext &Ctx) const;
109
110 LLVM_ABI Expected<std::unique_ptr<Archive>> getAsArchive() const;
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
135 create(MemoryBufferRef Source);
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#endif
#define LLVM_ABI
Definition: Compiler.h:213
uint32_t Index
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1328
static unsigned getCPUType(const MachOObjectFile &O)
static unsigned getCPUSubType(const MachOObjectFile &O)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
A range adaptor for a pair of iterators.
bool operator==(const ObjectForArch &Other) const
bool operator==(const object_iterator &Other) const
bool operator!=(const object_iterator &Other) const
object_iterator begin_objects() const
object_iterator end_objects() const
iterator_range< object_iterator > objects() const
static bool classof(Binary const *V)
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.