1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
//===- MachOUniversal.cpp - Mach-O universal binary -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the MachOUniversalBinary class.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace llvm;
using namespace object;
static Error
malformedError(Twine Msg) {
std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
return make_error<GenericBinaryError>(std::move(StringMsg),
object_error::parse_failed);
}
template<typename T>
static T getUniversalBinaryStruct(const char *Ptr) {
T Res;
memcpy(&Res, Ptr, sizeof(T));
// Universal binary headers have big-endian byte order.
if (sys::IsLittleEndianHost)
swapStruct(Res);
return Res;
}
MachOUniversalBinary::ObjectForArch::ObjectForArch(
const MachOUniversalBinary *Parent, uint32_t Index)
: Parent(Parent), Index(Index) {
// The iterators use Parent as a nullptr and an Index+1 == NumberOfObjects.
if (!Parent || Index >= Parent->getNumberOfObjects()) {
clear();
} else {
// Parse object header.
StringRef ParentData = Parent->getData();
if (Parent->getMagic() == MachO::FAT_MAGIC) {
const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
Index * sizeof(MachO::fat_arch);
Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
} else { // Parent->getMagic() == MachO::FAT_MAGIC_64
const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
Index * sizeof(MachO::fat_arch_64);
Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
}
}
}
Expected<std::unique_ptr<MachOObjectFile>>
MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
if (!Parent)
report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
"called when Parent is a nullptr");
StringRef ParentData = Parent->getData();
StringRef ObjectData;
uint32_t cputype;
if (Parent->getMagic() == MachO::FAT_MAGIC) {
ObjectData = ParentData.substr(Header.offset, Header.size);
cputype = Header.cputype;
} else { // Parent->getMagic() == MachO::FAT_MAGIC_64
ObjectData = ParentData.substr(Header64.offset, Header64.size);
cputype = Header64.cputype;
}
StringRef ObjectName = Parent->getFileName();
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
return ObjectFile::createMachOObjectFile(ObjBuffer, cputype, Index);
}
Expected<std::unique_ptr<IRObjectFile>>
MachOUniversalBinary::ObjectForArch::getAsIRObject(LLVMContext &Ctx) const {
if (!Parent)
report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsIRObject() "
"called when Parent is a nullptr");
StringRef ParentData = Parent->getData();
StringRef ObjectData;
if (Parent->getMagic() == MachO::FAT_MAGIC) {
ObjectData = ParentData.substr(Header.offset, Header.size);
} else { // Parent->getMagic() == MachO::FAT_MAGIC_64
ObjectData = ParentData.substr(Header64.offset, Header64.size);
}
StringRef ObjectName = Parent->getFileName();
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
return IRObjectFile::create(ObjBuffer, Ctx);
}
Expected<std::unique_ptr<Archive>>
MachOUniversalBinary::ObjectForArch::getAsArchive() const {
if (!Parent)
report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
"called when Parent is a nullptr");
StringRef ParentData = Parent->getData();
StringRef ObjectData;
if (Parent->getMagic() == MachO::FAT_MAGIC)
ObjectData = ParentData.substr(Header.offset, Header.size);
else // Parent->getMagic() == MachO::FAT_MAGIC_64
ObjectData = ParentData.substr(Header64.offset, Header64.size);
StringRef ObjectName = Parent->getFileName();
MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
return Archive::create(ObjBuffer);
}
void MachOUniversalBinary::anchor() { }
Expected<std::unique_ptr<MachOUniversalBinary>>
MachOUniversalBinary::create(MemoryBufferRef Source) {
Error Err = Error::success();
std::unique_ptr<MachOUniversalBinary> Ret(
new MachOUniversalBinary(Source, Err));
if (Err)
return std::move(Err);
return std::move(Ret);
}
MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
: Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
NumberOfObjects(0) {
ErrorAsOutParameter ErrAsOutParam(&Err);
if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
Err = make_error<GenericBinaryError>("File too small to be a Mach-O "
"universal file",
object_error::invalid_file_type);
return;
}
// Check for magic value and sufficient header size.
StringRef Buf = getData();
MachO::fat_header H =
getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
Magic = H.magic;
NumberOfObjects = H.nfat_arch;
if (NumberOfObjects == 0) {
Err = malformedError("contains zero architecture types");
return;
}
uint32_t MinSize = sizeof(MachO::fat_header);
if (Magic == MachO::FAT_MAGIC)
MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
else if (Magic == MachO::FAT_MAGIC_64)
MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
else {
Err = malformedError("bad magic number");
return;
}
if (Buf.size() < MinSize) {
Err = malformedError("fat_arch" +
Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") +
" structs would extend past the end of the file");
return;
}
for (uint32_t i = 0; i < NumberOfObjects; i++) {
ObjectForArch A(this, i);
uint64_t bigSize = A.getOffset();
bigSize += A.getSize();
if (bigSize > Buf.size()) {
Err = malformedError("offset plus size of cputype (" +
Twine(A.getCPUType()) + ") cpusubtype (" +
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
") extends past the end of the file");
return;
}
if (A.getAlign() > MaxSectionAlignment) {
Err = malformedError("align (2^" + Twine(A.getAlign()) +
") too large for cputype (" + Twine(A.getCPUType()) +
") cpusubtype (" +
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
") (maximum 2^" + Twine(MaxSectionAlignment) + ")");
return;
}
if(A.getOffset() % (1ull << A.getAlign()) != 0){
Err = malformedError("offset: " + Twine(A.getOffset()) +
" for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
return;
}
if (A.getOffset() < MinSize) {
Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
"cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
") offset " + Twine(A.getOffset()) + " overlaps universal headers");
return;
}
}
for (uint32_t i = 0; i < NumberOfObjects; i++) {
ObjectForArch A(this, i);
for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
ObjectForArch B(this, j);
if (A.getCPUType() == B.getCPUType() &&
(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
Err = malformedError("contains two of the same architecture (cputype "
"(" + Twine(A.getCPUType()) + ") cpusubtype (" +
Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
return;
}
if ((A.getOffset() >= B.getOffset() &&
A.getOffset() < B.getOffset() + B.getSize()) ||
(A.getOffset() + A.getSize() > B.getOffset() &&
A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
(A.getOffset() <= B.getOffset() &&
A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
"cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
") at offset " + Twine(A.getOffset()) + " with a size of " +
Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
+ ") at offset " + Twine(B.getOffset()) + " with a size of "
+ Twine(B.getSize()));
return;
}
}
}
Err = Error::success();
}
Expected<MachOUniversalBinary::ObjectForArch>
MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
return make_error<GenericBinaryError>("Unknown architecture "
"named: " +
ArchName,
object_error::arch_not_found);
for (const auto &Obj : objects())
if (Obj.getArchFlagName() == ArchName)
return Obj;
return make_error<GenericBinaryError>("fat file does not "
"contain " +
ArchName,
object_error::arch_not_found);
}
Expected<std::unique_ptr<MachOObjectFile>>
MachOUniversalBinary::getMachOObjectForArch(StringRef ArchName) const {
Expected<ObjectForArch> O = getObjectForArch(ArchName);
if (!O)
return O.takeError();
return O->getAsObjectFile();
}
Expected<std::unique_ptr<IRObjectFile>>
MachOUniversalBinary::getIRObjectForArch(StringRef ArchName,
LLVMContext &Ctx) const {
Expected<ObjectForArch> O = getObjectForArch(ArchName);
if (!O)
return O.takeError();
return O->getAsIRObject(Ctx);
}
Expected<std::unique_ptr<Archive>>
MachOUniversalBinary::getArchiveForArch(StringRef ArchName) const {
Expected<ObjectForArch> O = getObjectForArch(ArchName);
if (!O)
return O.takeError();
return O->getAsArchive();
}
|