LLVM: lib/Object/MachOUniversal.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
20
21using namespace llvm;
22using namespace object;
23
26 std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
28 object_error::parse_failed);
29}
30
31template
33 T Res;
34 memcpy(&Res, Ptr, sizeof(T));
35
37 swapStruct(Res);
38 return Res;
39}
40
43 : Parent(Parent), Index(Index) {
44
45 if (!Parent || Index >= Parent->getNumberOfObjects()) {
46 clear();
47 } else {
48
49 StringRef ParentData = Parent->getData();
50 if (Parent->getMagic() == MachO::FAT_MAGIC) {
51 const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
52 Index * sizeof(MachO::fat_arch);
53 Header = getUniversalBinaryStructMachO::fat\_arch(HeaderPos);
54 } else {
55 const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
56 Index * sizeof(MachO::fat_arch_64);
57 Header64 = getUniversalBinaryStructMachO::fat\_arch\_64(HeaderPos);
58 }
59 }
60}
61
64 if (!Parent)
65 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
66 "called when Parent is a nullptr");
67
68 StringRef ParentData = Parent->getData();
72 ObjectData = ParentData.substr(Header.offset, Header.size);
73 cputype = Header.cputype;
74 } else {
75 ObjectData = ParentData.substr(Header64.offset, Header64.size);
76 cputype = Header64.cputype;
77 }
78 StringRef ObjectName = Parent->getFileName();
81}
82
85 if (!Parent)
86 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsIRObject() "
87 "called when Parent is a nullptr");
88
89 StringRef ParentData = Parent->getData();
92 ObjectData = ParentData.substr(Header.offset, Header.size);
93 } else {
94 ObjectData = ParentData.substr(Header64.offset, Header64.size);
95 }
96 StringRef ObjectName = Parent->getFileName();
98
100}
101
104 if (!Parent)
105 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
106 "called when Parent is a nullptr");
107
108 StringRef ParentData = Parent->getData();
111 ObjectData = ParentData.substr(Header.offset, Header.size);
112 else
113 ObjectData = ParentData.substr(Header64.offset, Header64.size);
114 StringRef ObjectName = Parent->getFileName();
117}
118
119void MachOUniversalBinary::anchor() { }
120
124 std::unique_ptr Ret(
126 if (Err)
127 return std::move(Err);
128 return std::move(Ret);
129}
130
133 NumberOfObjects(0) {
137 "universal file",
139 return;
140 }
141
145 Magic = H.magic;
146 NumberOfObjects = H.nfat_arch;
147 if (NumberOfObjects == 0) {
148 Err = malformedError("contains zero architecture types");
149 return;
150 }
156 else {
158 return;
159 }
160 if (Buf.size() < MinSize) {
163 " structs would extend past the end of the file");
164 return;
165 }
166 for (uint32_t i = 0; i < NumberOfObjects; i++) {
168 uint64_t bigSize = A.getOffset();
169 bigSize += A.getSize();
170 if (bigSize > Buf.size()) {
171 Err = malformedError("offset plus size of cputype (" +
172 Twine(A.getCPUType()) + ") cpusubtype (" +
174 ") extends past the end of the file");
175 return;
176 }
177
180 ") too large for cputype (" + Twine(A.getCPUType()) +
181 ") cpusubtype (" +
184 return;
185 }
186 if(A.getOffset() % (1ull << A.getAlign()) != 0){
188 " for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
190 ") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
191 return;
192 }
193 if (A.getOffset() < MinSize) {
196 ") offset " + Twine(A.getOffset()) + " overlaps universal headers");
197 return;
198 }
199 }
200 for (uint32_t i = 0; i < NumberOfObjects; i++) {
202 for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
204 if (A.getCPUType() == B.getCPUType() &&
207 Err = malformedError("contains two of the same architecture (cputype "
208 "(" + Twine(A.getCPUType()) + ") cpusubtype (" +
210 return;
211 }
212 if ((A.getOffset() >= B.getOffset() &&
213 A.getOffset() < B.getOffset() + B.getSize()) ||
214 (A.getOffset() + A.getSize() > B.getOffset() &&
215 A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
216 (A.getOffset() <= B.getOffset() &&
217 A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
220 ") at offset " + Twine(A.getOffset()) + " with a size of " +
221 Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
223 + ") at offset " + Twine(B.getOffset()) + " with a size of "
225 return;
226 }
227 }
228 }
230}
231
236 "named: " +
237 ArchName,
239 for (const auto &Obj : objects())
240 if (Obj.getArchFlagName() == ArchName)
241 return Obj;
243 "contain " +
244 ArchName,
246}
247
251 if (!O)
252 return O.takeError();
253 return O->getAsObjectFile();
254}
255
260 if (!O)
261 return O.takeError();
262 return O->getAsIRObject(Ctx);
263}
264
268 if (!O)
269 return O.takeError();
270 return O->getAsArchive();
271}
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static Error malformedError(Twine Msg)
Definition MachOUniversal.cpp:25
static T getUniversalBinaryStruct(const char *Ptr)
Definition MachOUniversal.cpp:32
static Error malformedError(Twine Msg)
Helper for Errors used as out-parameters.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
This is an important class for using LLVM in a threaded context.
StringRef - Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr size_t size() const
size - Get the string size.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
StringRef getData() const
Binary(unsigned int Type, MemoryBufferRef Source)
@ ID_MachOUniversalBinary
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
LLVM_ABI Expected< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
Definition MachOUniversal.cpp:63
LLVM_ABI ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index)
Definition MachOUniversal.cpp:41
LLVM_ABI Expected< std::unique_ptr< IRObjectFile > > getAsIRObject(LLVMContext &Ctx) const
Definition MachOUniversal.cpp:84
LLVM_ABI Expected< std::unique_ptr< Archive > > getAsArchive() const
Definition MachOUniversal.cpp:103
Expected< ObjectForArch > getObjectForArch(StringRef ArchName) const
Definition MachOUniversal.cpp:233
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
Definition MachOUniversal.cpp:122
Expected< std::unique_ptr< IRObjectFile > > getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const
Definition MachOUniversal.cpp:257
Expected< std::unique_ptr< MachOObjectFile > > getMachOObjectForArch(StringRef ArchName) const
Definition MachOUniversal.cpp:249
Expected< std::unique_ptr< Archive > > getArchiveForArch(StringRef ArchName) const
Definition MachOUniversal.cpp:266
MachOUniversalBinary(MemoryBufferRef Souce, Error &Err)
Definition MachOUniversal.cpp:131
iterator_range< object_iterator > objects() const
static constexpr uint32_t MaxSectionAlignment
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0, size_t MachOFilesetEntryOffset=0)
Create a MachOObjectFile instance from a given buffer.
constexpr bool IsLittleEndianHost
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.