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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
//===-- llvm-lipo.cpp - a tool for manipulating universal binaries --------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// A utility for creating / splitting / inspecting universal binaries.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Triple.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/WithColor.h"
using namespace llvm;
using namespace llvm::object;
static const StringRef ToolName = "llvm-lipo";
LLVM_ATTRIBUTE_NORETURN static void reportError(Twine Message) {
WithColor::error(errs(), ToolName) << Message << "\n";
errs().flush();
exit(EXIT_FAILURE);
}
LLVM_ATTRIBUTE_NORETURN static void reportError(StringRef File, Error E) {
assert(E);
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(std::move(E), OS);
OS.flush();
WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf;
exit(EXIT_FAILURE);
}
namespace {
enum LipoID {
LIPO_INVALID = 0, // This is not an option ID.
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
LIPO_##ID,
#include "LipoOpts.inc"
#undef OPTION
};
// LipoInfoTable below references LIPO_##PREFIX. OptionGroup has prefix nullptr.
const char *const *LIPO_nullptr = nullptr;
#define PREFIX(NAME, VALUE) const char *const LIPO_##NAME[] = VALUE;
#include "LipoOpts.inc"
#undef PREFIX
static const opt::OptTable::Info LipoInfoTable[] = {
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
{LIPO_##PREFIX, NAME, HELPTEXT, \
METAVAR, LIPO_##ID, opt::Option::KIND##Class, \
PARAM, FLAGS, LIPO_##GROUP, \
LIPO_##ALIAS, ALIASARGS, VALUES},
#include "LipoOpts.inc"
#undef OPTION
};
class LipoOptTable : public opt::OptTable {
public:
LipoOptTable() : OptTable(LipoInfoTable) {}
};
enum class LipoAction {
PrintArchs,
VerifyArch,
ThinArch,
CreateUniversal,
};
struct Config {
SmallVector<std::string, 1> InputFiles;
SmallVector<std::string, 1> VerifyArchList;
std::string ThinArchType;
std::string OutputFile;
LipoAction ActionToPerform;
};
struct Slice {
const MachOObjectFile *ObjectFile;
// Requires Alignment field to store slice alignment values from universal
// binaries. Also needed to order the slices using compareSlices, so the total
// file size can be calculated before creating the output buffer.
uint32_t Alignment;
};
} // end namespace
static void validateArchitectureName(StringRef ArchitectureName) {
if (!MachOObjectFile::isValidArch(ArchitectureName)) {
std::string Buf;
raw_string_ostream OS(Buf);
OS << "Invalid architecture: " << ArchitectureName
<< "\nValid architecture names are:";
for (auto arch : MachOObjectFile::getValidArchs())
OS << " " << arch;
reportError(OS.str());
}
}
static Config parseLipoOptions(ArrayRef<const char *> ArgsArr) {
Config C;
LipoOptTable T;
unsigned MissingArgumentIndex, MissingArgumentCount;
opt::InputArgList InputArgs =
T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
if (MissingArgumentCount)
reportError("missing argument to " +
StringRef(InputArgs.getArgString(MissingArgumentIndex)) +
" option");
if (InputArgs.size() == 0) {
// PrintHelp does not accept Twine.
T.PrintHelp(errs(), "llvm-lipo input[s] option[s]", "llvm-lipo");
exit(EXIT_FAILURE);
}
if (InputArgs.hasArg(LIPO_help)) {
// PrintHelp does not accept Twine.
T.PrintHelp(outs(), "llvm-lipo input[s] option[s]", "llvm-lipo");
exit(EXIT_SUCCESS);
}
if (InputArgs.hasArg(LIPO_version)) {
outs() << ToolName + "\n";
cl::PrintVersionMessage();
exit(EXIT_SUCCESS);
}
for (auto Arg : InputArgs.filtered(LIPO_UNKNOWN))
reportError("unknown argument '" + Arg->getAsString(InputArgs) + "'");
for (auto Arg : InputArgs.filtered(LIPO_INPUT))
C.InputFiles.push_back(Arg->getValue());
if (C.InputFiles.empty())
reportError("at least one input file should be specified");
if (InputArgs.hasArg(LIPO_output))
C.OutputFile = InputArgs.getLastArgValue(LIPO_output);
SmallVector<opt::Arg *, 1> ActionArgs(InputArgs.filtered(LIPO_action_group));
if (ActionArgs.empty())
reportError("at least one action should be specified");
if (ActionArgs.size() > 1) {
std::string Buf;
raw_string_ostream OS(Buf);
OS << "only one of the following actions can be specified:";
for (auto Arg : ActionArgs)
OS << " " << Arg->getSpelling();
reportError(OS.str());
}
switch (ActionArgs[0]->getOption().getID()) {
case LIPO_verify_arch:
for (auto A : InputArgs.getAllArgValues(LIPO_verify_arch))
C.VerifyArchList.push_back(A);
if (C.VerifyArchList.empty())
reportError(
"verify_arch requires at least one architecture to be specified");
if (C.InputFiles.size() > 1)
reportError("verify_arch expects a single input file");
C.ActionToPerform = LipoAction::VerifyArch;
return C;
case LIPO_archs:
if (C.InputFiles.size() > 1)
reportError("archs expects a single input file");
C.ActionToPerform = LipoAction::PrintArchs;
return C;
case LIPO_thin:
if (C.InputFiles.size() > 1)
reportError("thin expects a single input file");
C.ThinArchType = ActionArgs[0]->getValue();
validateArchitectureName(C.ThinArchType);
if (C.OutputFile.empty())
reportError("thin expects a single output file");
C.ActionToPerform = LipoAction::ThinArch;
return C;
case LIPO_create:
if (C.OutputFile.empty())
reportError("create expects a single output file to be specified");
C.ActionToPerform = LipoAction::CreateUniversal;
return C;
default:
reportError("llvm-lipo action unspecified");
}
}
static SmallVector<OwningBinary<Binary>, 1>
readInputBinaries(ArrayRef<std::string> InputFiles) {
SmallVector<OwningBinary<Binary>, 1> InputBinaries;
for (StringRef InputFile : InputFiles) {
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFile);
if (!BinaryOrErr)
reportError(InputFile, BinaryOrErr.takeError());
// TODO: Add compatibility for archive files
if (BinaryOrErr->getBinary()->isArchive())
reportError("File " + InputFile +
" is an archive file and is not yet supported.");
if (!BinaryOrErr->getBinary()->isMachO() &&
!BinaryOrErr->getBinary()->isMachOUniversalBinary())
reportError("File " + InputFile + " has unsupported binary format");
InputBinaries.push_back(std::move(*BinaryOrErr));
}
return InputBinaries;
}
LLVM_ATTRIBUTE_NORETURN
static void verifyArch(ArrayRef<OwningBinary<Binary>> InputBinaries,
ArrayRef<std::string> VerifyArchList) {
assert(!VerifyArchList.empty() &&
"The list of architectures should be non-empty");
assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
for (StringRef Arch : VerifyArchList)
validateArchitectureName(Arch);
if (auto UO =
dyn_cast<MachOUniversalBinary>(InputBinaries.front().getBinary())) {
for (StringRef Arch : VerifyArchList) {
Expected<std::unique_ptr<MachOObjectFile>> Obj =
UO->getObjectForArch(Arch);
if (!Obj)
exit(EXIT_FAILURE);
}
} else if (auto O =
dyn_cast<MachOObjectFile>(InputBinaries.front().getBinary())) {
const Triple::ArchType ObjectArch = O->getArch();
for (StringRef Arch : VerifyArchList)
if (ObjectArch != Triple(Arch).getArch())
exit(EXIT_FAILURE);
} else {
llvm_unreachable("Unexpected binary format");
}
exit(EXIT_SUCCESS);
}
// Returns a string of the given Object file's architecture type
// Unknown architectures formatted unknown(CPUType,CPUSubType) for compatibility
// with cctools lipo
static std::string getArchString(const MachOObjectFile &ObjectFile) {
const Triple T = ObjectFile.getArchTriple();
const StringRef ObjectArch = T.getArchName();
if (!ObjectArch.empty())
return ObjectArch;
return ("unknown(" + Twine(ObjectFile.getHeader().cputype) + "," +
Twine(ObjectFile.getHeader().cpusubtype & ~MachO::CPU_SUBTYPE_MASK) +
")")
.str();
}
LLVM_ATTRIBUTE_NORETURN
static void printArchs(ArrayRef<OwningBinary<Binary>> InputBinaries) {
// Prints trailing space for compatibility with cctools lipo.
assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
const Binary *InputBinary = InputBinaries.front().getBinary();
if (auto UO = dyn_cast<MachOUniversalBinary>(InputBinary)) {
for (const auto &O : UO->objects()) {
Expected<std::unique_ptr<MachOObjectFile>> BinaryOrError =
O.getAsObjectFile();
if (!BinaryOrError)
reportError(InputBinary->getFileName(), BinaryOrError.takeError());
outs() << getArchString(*BinaryOrError.get().get()) << " ";
}
} else if (auto O = dyn_cast<MachOObjectFile>(InputBinary)) {
outs() << getArchString(*O) << " ";
} else {
llvm_unreachable("Unexpected binary format");
}
outs() << "\n";
exit(EXIT_SUCCESS);
}
LLVM_ATTRIBUTE_NORETURN
static void extractSlice(ArrayRef<OwningBinary<Binary>> InputBinaries,
StringRef ThinArchType, StringRef OutputFileName) {
assert(!ThinArchType.empty() && "The architecture type should be non-empty");
assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
assert(!OutputFileName.empty() && "Thin expects a single output file");
if (InputBinaries.front().getBinary()->isMachO()) {
reportError("input file " +
InputBinaries.front().getBinary()->getFileName() +
" must be a fat file when the -thin option is specified");
exit(EXIT_FAILURE);
}
auto *UO = cast<MachOUniversalBinary>(InputBinaries.front().getBinary());
Expected<std::unique_ptr<MachOObjectFile>> Obj =
UO->getObjectForArch(ThinArchType);
if (!Obj)
reportError("fat input file " + UO->getFileName() +
" does not contain the specified architecture " + ThinArchType +
" to thin it to");
Expected<std::unique_ptr<FileOutputBuffer>> OutFileOrError =
FileOutputBuffer::create(OutputFileName,
Obj.get()->getMemoryBufferRef().getBufferSize(),
sys::fs::can_execute(UO->getFileName())
? FileOutputBuffer::F_executable
: 0);
if (!OutFileOrError)
reportError(OutputFileName, OutFileOrError.takeError());
std::copy(Obj.get()->getMemoryBufferRef().getBufferStart(),
Obj.get()->getMemoryBufferRef().getBufferEnd(),
OutFileOrError.get()->getBufferStart());
if (Error E = OutFileOrError.get()->commit())
reportError(OutputFileName, std::move(E));
exit(EXIT_SUCCESS);
}
static void checkArchDuplicates(const ArrayRef<Slice> &Slices) {
DenseMap<uint64_t, const MachOObjectFile *> CPUIds;
auto CPUIDForSlice = [](const Slice &S) {
return static_cast<uint64_t>(S.ObjectFile->getHeader().cputype) << 32 |
S.ObjectFile->getHeader().cpusubtype;
};
for (const auto &S : Slices) {
auto Entry = CPUIds.try_emplace(CPUIDForSlice(S), S.ObjectFile);
if (!Entry.second)
reportError(Entry.first->second->getFileName() + " and " +
S.ObjectFile->getFileName() + " have the same architecture " +
getArchString(*S.ObjectFile) +
" and therefore cannot be in the same universal binary");
}
}
static uint32_t calculateAlignment(const MachOObjectFile *ObjectFile) {
// TODO: Implement getAlign() and remove hard coding
// Will be implemented in a follow-up.
switch (ObjectFile->getHeader().cputype) {
case MachO::CPU_TYPE_I386:
case MachO::CPU_TYPE_X86_64:
case MachO::CPU_TYPE_POWERPC:
case MachO::CPU_TYPE_POWERPC64:
return 12; // log2 value of page size(4k) for x86 and PPC
case MachO::CPU_TYPE_ARM:
case MachO::CPU_TYPE_ARM64:
case MachO::CPU_TYPE_ARM64_32:
return 14; // log2 value of page size(16k) for Darwin ARM
default:
return 12;
}
}
// This function replicates ordering from cctools lipo for consistency
static bool compareSlices(const Slice &Lhs, const Slice &Rhs) {
if (Lhs.ObjectFile->getHeader().cputype ==
Rhs.ObjectFile->getHeader().cputype)
return Lhs.ObjectFile->getHeader().cpusubtype <
Rhs.ObjectFile->getHeader().cpusubtype;
// force arm64-family to follow after all other slices for compatibility
// with cctools lipo
if (Lhs.ObjectFile->getHeader().cputype == MachO::CPU_TYPE_ARM64)
return false;
if (Rhs.ObjectFile->getHeader().cputype == MachO::CPU_TYPE_ARM64)
return true;
// Sort by alignment to minimize file size
return Lhs.Alignment < Rhs.Alignment;
}
// Updates vector ExtractedObjects with the MachOObjectFiles extracted from
// Universal Binary files to transfer ownership.
static SmallVector<Slice, 2> buildSlices(
ArrayRef<OwningBinary<Binary>> InputBinaries,
SmallVectorImpl<std::unique_ptr<MachOObjectFile>> &ExtractedObjects) {
SmallVector<Slice, 2> Slices;
for (auto &IB : InputBinaries) {
const Binary *InputBinary = IB.getBinary();
if (auto UO = dyn_cast<MachOUniversalBinary>(InputBinary)) {
for (const auto &O : UO->objects()) {
Expected<std::unique_ptr<MachOObjectFile>> BinaryOrError =
O.getAsObjectFile();
if (!BinaryOrError)
reportError(InputBinary->getFileName(), BinaryOrError.takeError());
ExtractedObjects.push_back(std::move(BinaryOrError.get()));
Slices.push_back(Slice{ExtractedObjects.back().get(), O.getAlign()});
}
} else if (auto O = dyn_cast<MachOObjectFile>(InputBinary)) {
Slices.push_back(Slice{O, calculateAlignment(O)});
} else {
llvm_unreachable("Unexpected binary format");
}
}
return Slices;
}
static SmallVector<MachO::fat_arch, 2>
buildFatArchList(ArrayRef<Slice> Slices) {
SmallVector<MachO::fat_arch, 2> FatArchList;
uint64_t Offset =
sizeof(MachO::fat_header) + Slices.size() * sizeof(MachO::fat_arch);
for (size_t Index = 0, Size = Slices.size(); Index < Size; ++Index) {
Offset = alignTo(Offset, 1 << Slices[Index].Alignment);
const MachOObjectFile *ObjectFile = Slices[Index].ObjectFile;
if (Offset > UINT32_MAX)
reportError("fat file too large to be created because the offset "
"field in struct fat_arch is only 32-bits and the offset " +
Twine(Offset) + " for " + ObjectFile->getFileName() +
" for architecture " + getArchString(*ObjectFile) +
"exceeds that.");
MachO::fat_arch FatArch;
FatArch.cputype = ObjectFile->getHeader().cputype;
FatArch.cpusubtype = ObjectFile->getHeader().cpusubtype;
FatArch.offset = Offset;
FatArch.size = ObjectFile->getMemoryBufferRef().getBufferSize();
FatArch.align = Slices[Index].Alignment;
Offset += FatArch.size;
FatArchList.push_back(FatArch);
}
return FatArchList;
}
static void createUniversalBinary(SmallVectorImpl<Slice> &Slices,
StringRef OutputFileName) {
MachO::fat_header FatHeader;
FatHeader.magic = MachO::FAT_MAGIC;
FatHeader.nfat_arch = Slices.size();
stable_sort(Slices, compareSlices);
SmallVector<MachO::fat_arch, 2> FatArchList = buildFatArchList(Slices);
const bool IsExecutable = any_of(Slices, [](Slice S) {
return sys::fs::can_execute(S.ObjectFile->getFileName());
});
const uint64_t OutputFileSize =
FatArchList.back().offset + FatArchList.back().size;
Expected<std::unique_ptr<FileOutputBuffer>> OutFileOrError =
FileOutputBuffer::create(OutputFileName, OutputFileSize,
IsExecutable ? FileOutputBuffer::F_executable
: 0);
if (!OutFileOrError)
reportError(OutputFileName, OutFileOrError.takeError());
std::unique_ptr<FileOutputBuffer> OutFile = std::move(OutFileOrError.get());
std::memset(OutFile->getBufferStart(), 0, OutputFileSize);
if (sys::IsLittleEndianHost)
MachO::swapStruct(FatHeader);
std::memcpy(OutFile->getBufferStart(), &FatHeader, sizeof(MachO::fat_header));
for (size_t Index = 0, Size = Slices.size(); Index < Size; ++Index) {
MemoryBufferRef BufferRef = Slices[Index].ObjectFile->getMemoryBufferRef();
std::copy(BufferRef.getBufferStart(), BufferRef.getBufferEnd(),
OutFile->getBufferStart() + FatArchList[Index].offset);
}
// FatArchs written after Slices in order reduce the number of swaps for the
// LittleEndian case
if (sys::IsLittleEndianHost)
for (MachO::fat_arch &FA : FatArchList)
MachO::swapStruct(FA);
std::memcpy(OutFile->getBufferStart() + sizeof(MachO::fat_header),
FatArchList.begin(),
sizeof(MachO::fat_arch) * FatArchList.size());
if (Error E = OutFile->commit())
reportError(OutputFileName, std::move(E));
}
LLVM_ATTRIBUTE_NORETURN
static void createUniversalBinary(ArrayRef<OwningBinary<Binary>> InputBinaries,
StringRef OutputFileName) {
assert(InputBinaries.size() >= 1 && "Incorrect number of input binaries");
assert(!OutputFileName.empty() && "Create expects a single output file");
SmallVector<std::unique_ptr<MachOObjectFile>, 1> ExtractedObjects;
SmallVector<Slice, 1> Slices = buildSlices(InputBinaries, ExtractedObjects);
checkArchDuplicates(Slices);
createUniversalBinary(Slices, OutputFileName);
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv) {
InitLLVM X(argc, argv);
Config C = parseLipoOptions(makeArrayRef(argv + 1, argc));
SmallVector<OwningBinary<Binary>, 1> InputBinaries =
readInputBinaries(C.InputFiles);
switch (C.ActionToPerform) {
case LipoAction::VerifyArch:
verifyArch(InputBinaries, C.VerifyArchList);
break;
case LipoAction::PrintArchs:
printArchs(InputBinaries);
break;
case LipoAction::ThinArch:
extractSlice(InputBinaries, C.ThinArchType, C.OutputFile);
break;
case LipoAction::CreateUniversal:
createUniversalBinary(InputBinaries, C.OutputFile);
break;
}
return EXIT_SUCCESS;
}
|