2019-05-29 07:22:12 +08:00
|
|
|
//===-- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-08-06 03:06:55 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2019-05-29 07:22:12 +08:00
|
|
|
#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"
|
2019-06-22 05:59:01 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2019-05-29 07:22:12 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
|
|
|
#include "llvm/Support/WithColor.h"
|
2019-10-08 05:14:22 +08:00
|
|
|
#include "llvm/TextAPI/MachO/Architecture.h"
|
2019-05-29 07:22:12 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-06-08 04:47:58 +08:00
|
|
|
// LipoInfoTable below references LIPO_##PREFIX. OptionGroup has prefix nullptr.
|
|
|
|
const char *const *LIPO_nullptr = nullptr;
|
2019-05-29 07:22:12 +08:00
|
|
|
#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) {}
|
|
|
|
};
|
|
|
|
|
2019-06-08 04:47:58 +08:00
|
|
|
enum class LipoAction {
|
|
|
|
PrintArchs,
|
2019-07-23 08:42:03 +08:00
|
|
|
PrintInfo,
|
2019-06-08 04:47:58 +08:00
|
|
|
VerifyArch,
|
2019-06-22 05:59:01 +08:00
|
|
|
ThinArch,
|
2019-11-21 10:16:11 +08:00
|
|
|
ExtractArch,
|
2019-07-19 06:48:38 +08:00
|
|
|
CreateUniversal,
|
2019-07-30 02:46:34 +08:00
|
|
|
ReplaceArch,
|
|
|
|
};
|
|
|
|
|
2019-09-28 06:33:18 +08:00
|
|
|
struct InputFile {
|
|
|
|
Optional<StringRef> ArchType;
|
2019-07-30 02:46:34 +08:00
|
|
|
StringRef FileName;
|
2019-06-08 04:47:58 +08:00
|
|
|
};
|
|
|
|
|
2019-05-29 07:22:12 +08:00
|
|
|
struct Config {
|
2019-09-28 06:33:18 +08:00
|
|
|
SmallVector<InputFile, 1> InputFiles;
|
2019-05-29 07:22:12 +08:00
|
|
|
SmallVector<std::string, 1> VerifyArchList;
|
2019-09-28 06:33:18 +08:00
|
|
|
SmallVector<InputFile, 1> ReplacementFiles;
|
2019-08-06 03:06:55 +08:00
|
|
|
StringMap<const uint32_t> SegmentAlignments;
|
2019-11-21 10:16:11 +08:00
|
|
|
std::string ArchType;
|
2019-06-22 05:59:01 +08:00
|
|
|
std::string OutputFile;
|
2019-06-08 04:47:58 +08:00
|
|
|
LipoAction ActionToPerform;
|
2019-05-29 07:22:12 +08:00
|
|
|
};
|
|
|
|
|
2019-09-24 06:22:55 +08:00
|
|
|
// For compatibility with cctools lipo, a file's alignment is calculated as the
|
|
|
|
// minimum aligment of all segments. For object files, the file's alignment is
|
|
|
|
// the maximum alignment of its sections.
|
|
|
|
static uint32_t calculateFileAlignment(const MachOObjectFile &O) {
|
|
|
|
uint32_t P2CurrentAlignment;
|
|
|
|
uint32_t P2MinAlignment = MachOUniversalBinary::MaxSectionAlignment;
|
|
|
|
const bool Is64Bit = O.is64Bit();
|
|
|
|
|
|
|
|
for (const auto &LC : O.load_commands()) {
|
|
|
|
if (LC.C.cmd != (Is64Bit ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT))
|
|
|
|
continue;
|
|
|
|
if (O.getHeader().filetype == MachO::MH_OBJECT) {
|
|
|
|
unsigned NumberOfSections =
|
|
|
|
(Is64Bit ? O.getSegment64LoadCommand(LC).nsects
|
|
|
|
: O.getSegmentLoadCommand(LC).nsects);
|
|
|
|
P2CurrentAlignment = NumberOfSections ? 2 : P2MinAlignment;
|
|
|
|
for (unsigned SI = 0; SI < NumberOfSections; ++SI) {
|
|
|
|
P2CurrentAlignment = std::max(P2CurrentAlignment,
|
|
|
|
(Is64Bit ? O.getSection64(LC, SI).align
|
|
|
|
: O.getSection(LC, SI).align));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
P2CurrentAlignment =
|
|
|
|
countTrailingZeros(Is64Bit ? O.getSegment64LoadCommand(LC).vmaddr
|
|
|
|
: O.getSegmentLoadCommand(LC).vmaddr);
|
|
|
|
}
|
|
|
|
P2MinAlignment = std::min(P2MinAlignment, P2CurrentAlignment);
|
|
|
|
}
|
|
|
|
// return a value >= 4 byte aligned, and less than MachO MaxSectionAlignment
|
|
|
|
return std::max(
|
|
|
|
static_cast<uint32_t>(2),
|
|
|
|
std::min(P2MinAlignment, static_cast<uint32_t>(
|
|
|
|
MachOUniversalBinary::MaxSectionAlignment)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t calculateAlignment(const MachOObjectFile *ObjectFile) {
|
|
|
|
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 calculateFileAlignment(*ObjectFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Slice {
|
|
|
|
const Binary *B;
|
|
|
|
uint32_t CPUType;
|
|
|
|
uint32_t CPUSubType;
|
|
|
|
std::string ArchName;
|
|
|
|
|
|
|
|
// P2Alignment field stores slice alignment values from universal
|
|
|
|
// binaries. This is also needed to order the slices so the total
|
2019-07-19 06:48:38 +08:00
|
|
|
// file size can be calculated before creating the output buffer.
|
2019-09-24 06:22:55 +08:00
|
|
|
uint32_t P2Alignment;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Slice(const MachOObjectFile *O, uint32_t Align)
|
|
|
|
: B(O), CPUType(O->getHeader().cputype),
|
|
|
|
CPUSubType(O->getHeader().cpusubtype),
|
2020-01-29 03:23:46 +08:00
|
|
|
ArchName(std::string(O->getArchTriple().getArchName())),
|
|
|
|
P2Alignment(Align) {}
|
2019-09-24 06:22:55 +08:00
|
|
|
|
|
|
|
explicit Slice(const MachOObjectFile *O) : Slice(O, calculateAlignment(O)){};
|
|
|
|
|
|
|
|
explicit Slice(const Archive *A) : B(A) {
|
|
|
|
Error Err = Error::success();
|
|
|
|
std::unique_ptr<MachOObjectFile> FO = nullptr;
|
|
|
|
for (const Archive::Child &Child : A->children(Err)) {
|
|
|
|
Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
|
|
|
|
if (!ChildOrErr)
|
|
|
|
reportError(A->getFileName(), ChildOrErr.takeError());
|
|
|
|
Binary *Bin = ChildOrErr.get().get();
|
|
|
|
if (Bin->isMachOUniversalBinary())
|
|
|
|
reportError(("archive member " + Bin->getFileName() +
|
|
|
|
" is a fat file (not allowed in an archive)")
|
|
|
|
.str());
|
|
|
|
if (!Bin->isMachO())
|
|
|
|
reportError(("archive member " + Bin->getFileName() +
|
|
|
|
" is not a MachO file (not allowed in an archive)"));
|
|
|
|
MachOObjectFile *O = cast<MachOObjectFile>(Bin);
|
|
|
|
if (FO &&
|
|
|
|
std::tie(FO->getHeader().cputype, FO->getHeader().cpusubtype) !=
|
|
|
|
std::tie(O->getHeader().cputype, O->getHeader().cpusubtype)) {
|
|
|
|
reportError(("archive member " + O->getFileName() + " cputype (" +
|
|
|
|
Twine(O->getHeader().cputype) + ") and cpusubtype(" +
|
|
|
|
Twine(O->getHeader().cpusubtype) +
|
|
|
|
") does not match previous archive members cputype (" +
|
|
|
|
Twine(FO->getHeader().cputype) + ") and cpusubtype(" +
|
|
|
|
Twine(FO->getHeader().cpusubtype) +
|
|
|
|
") (all members must match) " + FO->getFileName())
|
|
|
|
.str());
|
|
|
|
}
|
|
|
|
if (!FO) {
|
|
|
|
ChildOrErr.get().release();
|
|
|
|
FO.reset(O);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Err)
|
|
|
|
reportError(A->getFileName(), std::move(Err));
|
|
|
|
if (!FO)
|
|
|
|
reportError(("empty archive with no architecture specification: " +
|
|
|
|
A->getFileName() + " (can't determine architecture for it)")
|
|
|
|
.str());
|
|
|
|
CPUType = FO->getHeader().cputype;
|
|
|
|
CPUSubType = FO->getHeader().cpusubtype;
|
2020-01-29 03:23:46 +08:00
|
|
|
ArchName = std::string(FO->getArchTriple().getArchName());
|
2019-09-24 06:22:55 +08:00
|
|
|
// Replicate the behavior of cctools lipo.
|
|
|
|
P2Alignment = FO->is64Bit() ? 3 : 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setP2Alignment(uint32_t Align) { P2Alignment = Align; }
|
|
|
|
|
|
|
|
const Binary *getBinary() const { return B; }
|
|
|
|
|
|
|
|
uint32_t getCPUType() const { return CPUType; }
|
|
|
|
|
|
|
|
uint32_t getCPUSubType() const { return CPUSubType; }
|
|
|
|
|
|
|
|
uint32_t getP2Alignment() const { return P2Alignment; }
|
|
|
|
|
|
|
|
uint64_t getCPUID() const {
|
|
|
|
return static_cast<uint64_t>(CPUType) << 32 | CPUSubType;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getArchString() const {
|
|
|
|
if (!ArchName.empty())
|
|
|
|
return ArchName;
|
|
|
|
return ("unknown(" + Twine(CPUType) + "," +
|
|
|
|
Twine(CPUSubType & ~MachO::CPU_SUBTYPE_MASK) + ")")
|
|
|
|
.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator<(const Slice &Lhs, const Slice &Rhs) {
|
|
|
|
if (Lhs.CPUType == Rhs.CPUType)
|
|
|
|
return Lhs.CPUSubType < Rhs.CPUSubType;
|
|
|
|
// force arm64-family to follow after all other slices for
|
|
|
|
// compatibility with cctools lipo
|
|
|
|
if (Lhs.CPUType == MachO::CPU_TYPE_ARM64)
|
|
|
|
return false;
|
|
|
|
if (Rhs.CPUType == MachO::CPU_TYPE_ARM64)
|
|
|
|
return true;
|
|
|
|
// Sort by alignment to minimize file size
|
|
|
|
return Lhs.P2Alignment < Rhs.P2Alignment;
|
|
|
|
}
|
2019-07-19 06:48:38 +08:00
|
|
|
};
|
|
|
|
|
2019-05-29 07:22:12 +08:00
|
|
|
} // end namespace
|
|
|
|
|
2019-06-22 05:59:01 +08:00
|
|
|
static void validateArchitectureName(StringRef ArchitectureName) {
|
2019-07-04 08:17:02 +08:00
|
|
|
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());
|
|
|
|
}
|
2019-06-22 05:59:01 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 07:22:12 +08:00
|
|
|
static Config parseLipoOptions(ArrayRef<const char *> ArgsArr) {
|
|
|
|
Config C;
|
|
|
|
LipoOptTable T;
|
|
|
|
unsigned MissingArgumentIndex, MissingArgumentCount;
|
2019-07-19 06:48:38 +08:00
|
|
|
opt::InputArgList InputArgs =
|
2019-05-29 07:22:12 +08:00
|
|
|
T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
|
|
|
|
|
2019-06-22 05:59:01 +08:00
|
|
|
if (MissingArgumentCount)
|
|
|
|
reportError("missing argument to " +
|
|
|
|
StringRef(InputArgs.getArgString(MissingArgumentIndex)) +
|
|
|
|
" option");
|
|
|
|
|
2019-05-29 07:22:12 +08:00
|
|
|
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))
|
2019-09-28 06:33:18 +08:00
|
|
|
C.InputFiles.push_back({None, Arg->getValue()});
|
|
|
|
for (auto Arg : InputArgs.filtered(LIPO_arch)) {
|
|
|
|
validateArchitectureName(Arg->getValue(0));
|
|
|
|
if (!Arg->getValue(1))
|
|
|
|
reportError(
|
|
|
|
"arch is missing an argument: expects -arch arch_type file_name");
|
|
|
|
C.InputFiles.push_back({StringRef(Arg->getValue(0)), Arg->getValue(1)});
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:22:12 +08:00
|
|
|
if (C.InputFiles.empty())
|
|
|
|
reportError("at least one input file should be specified");
|
|
|
|
|
2019-06-22 05:59:01 +08:00
|
|
|
if (InputArgs.hasArg(LIPO_output))
|
2020-01-29 03:23:46 +08:00
|
|
|
C.OutputFile = std::string(InputArgs.getLastArgValue(LIPO_output));
|
2019-06-22 05:59:01 +08:00
|
|
|
|
2019-08-06 03:06:55 +08:00
|
|
|
for (auto Segalign : InputArgs.filtered(LIPO_segalign)) {
|
|
|
|
if (!Segalign->getValue(1))
|
|
|
|
reportError("segalign is missing an argument: expects -segalign "
|
|
|
|
"arch_type alignment_value");
|
|
|
|
|
|
|
|
validateArchitectureName(Segalign->getValue(0));
|
|
|
|
|
|
|
|
uint32_t AlignmentValue;
|
|
|
|
if (!to_integer<uint32_t>(Segalign->getValue(1), AlignmentValue, 16))
|
|
|
|
reportError("argument to -segalign <arch_type> " +
|
|
|
|
Twine(Segalign->getValue(1)) +
|
|
|
|
" (hex) is not a proper hexadecimal number");
|
|
|
|
if (!isPowerOf2_32(AlignmentValue))
|
|
|
|
reportError("argument to -segalign <arch_type> " +
|
|
|
|
Twine(Segalign->getValue(1)) +
|
|
|
|
" (hex) must be a non-zero power of two");
|
|
|
|
if (Log2_32(AlignmentValue) > MachOUniversalBinary::MaxSectionAlignment)
|
|
|
|
reportError(
|
|
|
|
"argument to -segalign <arch_type> " + Twine(Segalign->getValue(1)) +
|
|
|
|
" (hex) must be less than or equal to the maximum section align 2^" +
|
|
|
|
Twine(MachOUniversalBinary::MaxSectionAlignment));
|
|
|
|
auto Entry = C.SegmentAlignments.try_emplace(Segalign->getValue(0),
|
|
|
|
Log2_32(AlignmentValue));
|
|
|
|
if (!Entry.second)
|
|
|
|
reportError("-segalign " + Twine(Segalign->getValue(0)) +
|
|
|
|
" <alignment_value> specified multiple times: " +
|
|
|
|
Twine(1 << Entry.first->second) + ", " +
|
|
|
|
Twine(AlignmentValue));
|
|
|
|
}
|
|
|
|
|
2019-06-08 04:47:58 +08:00
|
|
|
SmallVector<opt::Arg *, 1> ActionArgs(InputArgs.filtered(LIPO_action_group));
|
|
|
|
if (ActionArgs.empty())
|
|
|
|
reportError("at least one action should be specified");
|
2019-07-30 02:46:34 +08:00
|
|
|
// errors if multiple actions specified other than replace
|
|
|
|
// multiple replace flags may be specified, as long as they are not mixed with
|
|
|
|
// other action flags
|
|
|
|
auto ReplacementArgsRange = InputArgs.filtered(LIPO_replace);
|
|
|
|
if (ActionArgs.size() > 1 &&
|
|
|
|
ActionArgs.size() !=
|
|
|
|
static_cast<size_t>(std::distance(ReplacementArgsRange.begin(),
|
|
|
|
ReplacementArgsRange.end()))) {
|
2019-06-08 04:47:58 +08:00
|
|
|
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:
|
2019-05-29 07:22:12 +08:00
|
|
|
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");
|
2019-06-08 04:47:58 +08:00
|
|
|
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;
|
|
|
|
|
2019-07-23 08:42:03 +08:00
|
|
|
case LIPO_info:
|
|
|
|
C.ActionToPerform = LipoAction::PrintInfo;
|
|
|
|
return C;
|
|
|
|
|
2019-06-22 05:59:01 +08:00
|
|
|
case LIPO_thin:
|
|
|
|
if (C.InputFiles.size() > 1)
|
|
|
|
reportError("thin expects a single input file");
|
|
|
|
if (C.OutputFile.empty())
|
|
|
|
reportError("thin expects a single output file");
|
2019-11-21 10:16:11 +08:00
|
|
|
C.ArchType = ActionArgs[0]->getValue();
|
|
|
|
validateArchitectureName(C.ArchType);
|
2019-06-22 05:59:01 +08:00
|
|
|
C.ActionToPerform = LipoAction::ThinArch;
|
|
|
|
return C;
|
|
|
|
|
2019-11-21 10:16:11 +08:00
|
|
|
case LIPO_extract:
|
|
|
|
if (C.InputFiles.size() > 1)
|
|
|
|
reportError("extract expects a single input file");
|
|
|
|
if (C.OutputFile.empty())
|
|
|
|
reportError("extract expects a single output file");
|
|
|
|
C.ArchType = ActionArgs[0]->getValue();
|
|
|
|
validateArchitectureName(C.ArchType);
|
|
|
|
C.ActionToPerform = LipoAction::ExtractArch;
|
|
|
|
return C;
|
|
|
|
|
2019-07-19 06:48:38 +08:00
|
|
|
case LIPO_create:
|
|
|
|
if (C.OutputFile.empty())
|
|
|
|
reportError("create expects a single output file to be specified");
|
|
|
|
C.ActionToPerform = LipoAction::CreateUniversal;
|
|
|
|
return C;
|
|
|
|
|
2019-07-30 02:46:34 +08:00
|
|
|
case LIPO_replace:
|
|
|
|
for (auto Action : ActionArgs) {
|
|
|
|
if (!Action->getValue(1))
|
|
|
|
reportError(
|
|
|
|
"replace is missing an argument: expects -replace arch_type "
|
|
|
|
"file_name");
|
2019-09-28 06:33:18 +08:00
|
|
|
validateArchitectureName(Action->getValue(0));
|
|
|
|
C.ReplacementFiles.push_back(
|
|
|
|
{StringRef(Action->getValue(0)), Action->getValue(1)});
|
2019-07-30 02:46:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (C.OutputFile.empty())
|
|
|
|
reportError("replace expects a single output file to be specified");
|
|
|
|
if (C.InputFiles.size() > 1)
|
|
|
|
reportError("replace expects a single input file");
|
|
|
|
C.ActionToPerform = LipoAction::ReplaceArch;
|
|
|
|
return C;
|
|
|
|
|
2019-06-08 04:47:58 +08:00
|
|
|
default:
|
|
|
|
reportError("llvm-lipo action unspecified");
|
2019-05-29 07:22:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static SmallVector<OwningBinary<Binary>, 1>
|
2019-09-28 06:33:18 +08:00
|
|
|
readInputBinaries(ArrayRef<InputFile> InputFiles) {
|
2019-05-29 07:22:12 +08:00
|
|
|
SmallVector<OwningBinary<Binary>, 1> InputBinaries;
|
2019-09-28 06:33:18 +08:00
|
|
|
for (const InputFile &IF : InputFiles) {
|
|
|
|
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(IF.FileName);
|
2019-05-29 07:22:12 +08:00
|
|
|
if (!BinaryOrErr)
|
2019-09-28 06:33:18 +08:00
|
|
|
reportError(IF.FileName, BinaryOrErr.takeError());
|
|
|
|
const Binary *B = BinaryOrErr->getBinary();
|
|
|
|
if (!B->isArchive() && !B->isMachO() && !B->isMachOUniversalBinary())
|
|
|
|
reportError("File " + IF.FileName + " has unsupported binary format");
|
|
|
|
if (IF.ArchType && (B->isMachO() || B->isArchive())) {
|
2019-10-08 05:14:22 +08:00
|
|
|
const auto S = B->isMachO() ? Slice(cast<MachOObjectFile>(B))
|
|
|
|
: Slice(cast<Archive>(B));
|
2019-10-12 14:14:02 +08:00
|
|
|
const auto SpecifiedCPUType = MachO::getCPUTypeFromArchitecture(
|
|
|
|
MachO::getArchitectureFromName(
|
|
|
|
Triple(*IF.ArchType).getArchName()))
|
|
|
|
.first;
|
2019-10-08 05:14:22 +08:00
|
|
|
// For compatibility with cctools' lipo the comparison is relaxed just to
|
|
|
|
// checking cputypes.
|
|
|
|
if (S.getCPUType() != SpecifiedCPUType)
|
2019-09-28 06:33:18 +08:00
|
|
|
reportError("specified architecture: " + *IF.ArchType +
|
|
|
|
" for file: " + B->getFileName() +
|
2019-10-08 05:14:22 +08:00
|
|
|
" does not match the file's architecture (" +
|
|
|
|
S.getArchString() + ")");
|
2019-09-28 06:33:18 +08:00
|
|
|
}
|
2019-05-29 07:22:12 +08:00
|
|
|
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)
|
2019-06-22 05:59:01 +08:00
|
|
|
validateArchitectureName(Arch);
|
2019-05-29 07:22:12 +08:00
|
|
|
|
|
|
|
if (auto UO =
|
|
|
|
dyn_cast<MachOUniversalBinary>(InputBinaries.front().getBinary())) {
|
|
|
|
for (StringRef Arch : VerifyArchList) {
|
2019-09-24 06:22:55 +08:00
|
|
|
Expected<MachOUniversalBinary::ObjectForArch> Obj =
|
|
|
|
UO->getObjectForArch(Arch);
|
|
|
|
if (!Obj)
|
2019-05-29 07:22:12 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-23 08:42:03 +08:00
|
|
|
static void printBinaryArchs(const Binary *Binary, raw_ostream &OS) {
|
2019-07-19 06:48:38 +08:00
|
|
|
// Prints trailing space for compatibility with cctools lipo.
|
2019-07-23 08:42:03 +08:00
|
|
|
if (auto UO = dyn_cast<MachOUniversalBinary>(Binary)) {
|
2019-07-19 06:48:38 +08:00
|
|
|
for (const auto &O : UO->objects()) {
|
2019-09-24 06:22:55 +08:00
|
|
|
Expected<std::unique_ptr<MachOObjectFile>> MachOObjOrError =
|
2019-07-19 06:48:38 +08:00
|
|
|
O.getAsObjectFile();
|
2019-09-24 06:22:55 +08:00
|
|
|
if (MachOObjOrError) {
|
|
|
|
OS << Slice(MachOObjOrError->get()).getArchString() << " ";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Expected<std::unique_ptr<Archive>> ArchiveOrError = O.getAsArchive();
|
|
|
|
if (ArchiveOrError) {
|
|
|
|
consumeError(MachOObjOrError.takeError());
|
|
|
|
OS << Slice(ArchiveOrError->get()).getArchString() << " ";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
consumeError(ArchiveOrError.takeError());
|
|
|
|
reportError(Binary->getFileName(), MachOObjOrError.takeError());
|
2019-06-08 04:47:58 +08:00
|
|
|
}
|
2019-07-23 08:42:03 +08:00
|
|
|
OS << "\n";
|
|
|
|
return;
|
2019-06-08 04:47:58 +08:00
|
|
|
}
|
2019-09-24 06:22:55 +08:00
|
|
|
OS << Slice(cast<MachOObjectFile>(Binary)).getArchString() << " \n";
|
2019-07-23 08:42:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN
|
|
|
|
static void printArchs(ArrayRef<OwningBinary<Binary>> InputBinaries) {
|
|
|
|
assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
|
|
|
|
printBinaryArchs(InputBinaries.front().getBinary(), outs());
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2019-06-08 04:47:58 +08:00
|
|
|
|
2019-07-23 08:42:03 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN
|
|
|
|
static void printInfo(ArrayRef<OwningBinary<Binary>> InputBinaries) {
|
|
|
|
// Group universal and thin files together for compatibility with cctools lipo
|
|
|
|
for (auto &IB : InputBinaries) {
|
|
|
|
const Binary *Binary = IB.getBinary();
|
|
|
|
if (Binary->isMachOUniversalBinary()) {
|
|
|
|
outs() << "Architectures in the fat file: " << Binary->getFileName()
|
|
|
|
<< " are: ";
|
|
|
|
printBinaryArchs(Binary, outs());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &IB : InputBinaries) {
|
|
|
|
const Binary *Binary = IB.getBinary();
|
|
|
|
if (!Binary->isMachOUniversalBinary()) {
|
|
|
|
assert(Binary->isMachO() && "expected MachO binary");
|
|
|
|
outs() << "Non-fat file: " << Binary->getFileName()
|
|
|
|
<< " is architecture: ";
|
|
|
|
printBinaryArchs(Binary, outs());
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 04:47:58 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-06-22 05:59:01 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN
|
2019-11-21 10:16:11 +08:00
|
|
|
static void thinSlice(ArrayRef<OwningBinary<Binary>> InputBinaries,
|
|
|
|
StringRef ArchType, StringRef OutputFileName) {
|
|
|
|
assert(!ArchType.empty() && "The architecture type should be non-empty");
|
2019-06-22 05:59:01 +08:00
|
|
|
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 =
|
2019-11-21 10:16:11 +08:00
|
|
|
UO->getMachOObjectForArch(ArchType);
|
|
|
|
Expected<std::unique_ptr<Archive>> Ar = UO->getArchiveForArch(ArchType);
|
2019-09-24 06:22:55 +08:00
|
|
|
if (!Obj && !Ar)
|
2019-06-22 05:59:01 +08:00
|
|
|
reportError("fat input file " + UO->getFileName() +
|
2019-11-21 10:16:11 +08:00
|
|
|
" does not contain the specified architecture " + ArchType +
|
2019-06-22 05:59:01 +08:00
|
|
|
" to thin it to");
|
2019-09-24 06:22:55 +08:00
|
|
|
Binary *B = Obj ? static_cast<Binary *>(Obj->get())
|
|
|
|
: static_cast<Binary *>(Ar->get());
|
2019-06-22 05:59:01 +08:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> OutFileOrError =
|
|
|
|
FileOutputBuffer::create(OutputFileName,
|
2019-09-24 06:22:55 +08:00
|
|
|
B->getMemoryBufferRef().getBufferSize(),
|
2019-06-22 05:59:01 +08:00
|
|
|
sys::fs::can_execute(UO->getFileName())
|
|
|
|
? FileOutputBuffer::F_executable
|
|
|
|
: 0);
|
|
|
|
if (!OutFileOrError)
|
|
|
|
reportError(OutputFileName, OutFileOrError.takeError());
|
2019-09-24 06:22:55 +08:00
|
|
|
std::copy(B->getMemoryBufferRef().getBufferStart(),
|
|
|
|
B->getMemoryBufferRef().getBufferEnd(),
|
2019-06-22 05:59:01 +08:00
|
|
|
OutFileOrError.get()->getBufferStart());
|
|
|
|
if (Error E = OutFileOrError.get()->commit())
|
|
|
|
reportError(OutputFileName, std::move(E));
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-10-12 14:14:02 +08:00
|
|
|
static void checkArchDuplicates(ArrayRef<Slice> Slices) {
|
2019-09-24 06:22:55 +08:00
|
|
|
DenseMap<uint64_t, const Binary *> CPUIds;
|
2019-07-19 06:48:38 +08:00
|
|
|
for (const auto &S : Slices) {
|
2019-09-24 06:22:55 +08:00
|
|
|
auto Entry = CPUIds.try_emplace(S.getCPUID(), S.getBinary());
|
2019-07-19 06:48:38 +08:00
|
|
|
if (!Entry.second)
|
|
|
|
reportError(Entry.first->second->getFileName() + " and " +
|
2019-09-24 06:22:55 +08:00
|
|
|
S.getBinary()->getFileName() +
|
|
|
|
" have the same architecture " + S.getArchString() +
|
2019-07-19 06:48:38 +08:00
|
|
|
" and therefore cannot be in the same universal binary");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 03:06:55 +08:00
|
|
|
template <typename Range>
|
2019-09-24 06:22:55 +08:00
|
|
|
static void updateAlignments(Range &Slices,
|
|
|
|
const StringMap<const uint32_t> &Alignments) {
|
2019-08-06 03:06:55 +08:00
|
|
|
for (auto &Slice : Slices) {
|
2019-09-24 06:22:55 +08:00
|
|
|
auto Alignment = Alignments.find(Slice.getArchString());
|
2019-08-06 03:06:55 +08:00
|
|
|
if (Alignment != Alignments.end())
|
2019-09-24 06:22:55 +08:00
|
|
|
Slice.setP2Alignment(Alignment->second);
|
2019-08-06 03:06:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkUnusedAlignments(ArrayRef<Slice> Slices,
|
|
|
|
const StringMap<const uint32_t> &Alignments) {
|
|
|
|
auto HasArch = [&](StringRef Arch) {
|
|
|
|
return llvm::find_if(Slices, [Arch](Slice S) {
|
2019-09-24 06:22:55 +08:00
|
|
|
return S.getArchString() == Arch;
|
2019-08-06 03:06:55 +08:00
|
|
|
}) != Slices.end();
|
|
|
|
};
|
|
|
|
for (StringRef Arch : Alignments.keys())
|
|
|
|
if (!HasArch(Arch))
|
|
|
|
reportError("-segalign " + Arch +
|
|
|
|
" <value> specified but resulting fat file does not contain "
|
|
|
|
"that architecture ");
|
|
|
|
}
|
|
|
|
|
2019-07-19 06:48:38 +08:00
|
|
|
// Updates vector ExtractedObjects with the MachOObjectFiles extracted from
|
|
|
|
// Universal Binary files to transfer ownership.
|
|
|
|
static SmallVector<Slice, 2> buildSlices(
|
|
|
|
ArrayRef<OwningBinary<Binary>> InputBinaries,
|
2019-08-06 03:06:55 +08:00
|
|
|
const StringMap<const uint32_t> &Alignments,
|
2019-07-19 06:48:38 +08:00
|
|
|
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()));
|
2019-09-24 06:22:55 +08:00
|
|
|
Slices.emplace_back(ExtractedObjects.back().get(), O.getAlign());
|
2019-07-19 06:48:38 +08:00
|
|
|
}
|
|
|
|
} else if (auto O = dyn_cast<MachOObjectFile>(InputBinary)) {
|
2019-09-24 06:22:55 +08:00
|
|
|
Slices.emplace_back(O);
|
|
|
|
} else if (auto A = dyn_cast<Archive>(InputBinary)) {
|
|
|
|
Slices.emplace_back(A);
|
2019-07-19 06:48:38 +08:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unexpected binary format");
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 06:22:55 +08:00
|
|
|
updateAlignments(Slices, Alignments);
|
2019-07-19 06:48:38 +08:00
|
|
|
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);
|
|
|
|
|
2019-09-24 06:22:55 +08:00
|
|
|
for (const auto &S : Slices) {
|
|
|
|
Offset = alignTo(Offset, 1ull << S.getP2Alignment());
|
2019-07-19 06:48:38 +08:00
|
|
|
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 " +
|
2019-09-24 06:22:55 +08:00
|
|
|
Twine(Offset) + " for " + S.getBinary()->getFileName() +
|
|
|
|
" for architecture " + S.getArchString() + "exceeds that.");
|
2019-07-19 06:48:38 +08:00
|
|
|
|
|
|
|
MachO::fat_arch FatArch;
|
2019-09-24 06:22:55 +08:00
|
|
|
FatArch.cputype = S.getCPUType();
|
|
|
|
FatArch.cpusubtype = S.getCPUSubType();
|
2019-07-19 06:48:38 +08:00
|
|
|
FatArch.offset = Offset;
|
2019-09-24 06:22:55 +08:00
|
|
|
FatArch.size = S.getBinary()->getMemoryBufferRef().getBufferSize();
|
|
|
|
FatArch.align = S.getP2Alignment();
|
2019-07-19 06:48:38 +08:00
|
|
|
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();
|
|
|
|
|
2019-09-24 06:22:55 +08:00
|
|
|
stable_sort(Slices);
|
2019-07-19 06:48:38 +08:00
|
|
|
SmallVector<MachO::fat_arch, 2> FatArchList = buildFatArchList(Slices);
|
|
|
|
|
|
|
|
const bool IsExecutable = any_of(Slices, [](Slice S) {
|
2019-09-24 06:22:55 +08:00
|
|
|
return sys::fs::can_execute(S.getBinary()->getFileName());
|
2019-07-19 06:48:38 +08:00
|
|
|
});
|
|
|
|
const uint64_t OutputFileSize =
|
2019-10-16 04:10:34 +08:00
|
|
|
static_cast<uint64_t>(FatArchList.back().offset) +
|
|
|
|
FatArchList.back().size;
|
2019-07-19 06:48:38 +08:00
|
|
|
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) {
|
2019-09-24 06:22:55 +08:00
|
|
|
MemoryBufferRef BufferRef = Slices[Index].getBinary()->getMemoryBufferRef();
|
2019-07-19 06:48:38 +08:00
|
|
|
std::copy(BufferRef.getBufferStart(), BufferRef.getBufferEnd(),
|
|
|
|
OutFile->getBufferStart() + FatArchList[Index].offset);
|
|
|
|
}
|
|
|
|
|
2019-07-23 08:42:03 +08:00
|
|
|
// FatArchs written after Slices in order to reduce the number of swaps for
|
|
|
|
// the LittleEndian case
|
2019-07-19 06:48:38 +08:00
|
|
|
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,
|
2019-08-06 03:06:55 +08:00
|
|
|
const StringMap<const uint32_t> &Alignments,
|
2019-07-19 06:48:38 +08:00
|
|
|
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;
|
2019-08-06 03:06:55 +08:00
|
|
|
SmallVector<Slice, 1> Slices =
|
|
|
|
buildSlices(InputBinaries, Alignments, ExtractedObjects);
|
2019-07-19 06:48:38 +08:00
|
|
|
checkArchDuplicates(Slices);
|
2019-08-06 03:06:55 +08:00
|
|
|
checkUnusedAlignments(Slices, Alignments);
|
2019-07-19 06:48:38 +08:00
|
|
|
createUniversalBinary(Slices, OutputFileName);
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:16:11 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN
|
|
|
|
static void extractSlice(ArrayRef<OwningBinary<Binary>> InputBinaries,
|
|
|
|
const StringMap<const uint32_t> &Alignments,
|
|
|
|
StringRef ArchType, StringRef OutputFileName) {
|
|
|
|
assert(!ArchType.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 -extract option is specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<std::unique_ptr<MachOObjectFile>, 2> ExtractedObjects;
|
|
|
|
SmallVector<Slice, 2> Slices =
|
|
|
|
buildSlices(InputBinaries, Alignments, ExtractedObjects);
|
|
|
|
erase_if(Slices, [ArchType](const Slice &S) {
|
|
|
|
return ArchType != S.getArchString();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Slices.empty())
|
|
|
|
reportError(
|
|
|
|
"fat input file " + InputBinaries.front().getBinary()->getFileName() +
|
|
|
|
" does not contain the specified architecture " + ArchType);
|
|
|
|
createUniversalBinary(Slices, OutputFileName);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-07-30 02:46:34 +08:00
|
|
|
static StringMap<Slice>
|
|
|
|
buildReplacementSlices(ArrayRef<OwningBinary<Binary>> ReplacementBinaries,
|
2019-09-28 06:33:18 +08:00
|
|
|
const StringMap<const uint32_t> &Alignments) {
|
2019-07-30 02:46:34 +08:00
|
|
|
StringMap<Slice> Slices;
|
|
|
|
// populates StringMap of slices to replace with; error checks for mismatched
|
|
|
|
// replace flag args, fat files, and duplicate arch_types
|
2019-09-28 06:33:18 +08:00
|
|
|
for (const auto &OB : ReplacementBinaries) {
|
|
|
|
const Binary *ReplacementBinary = OB.getBinary();
|
2019-07-30 02:46:34 +08:00
|
|
|
auto O = dyn_cast<MachOObjectFile>(ReplacementBinary);
|
|
|
|
if (!O)
|
|
|
|
reportError("replacement file: " + ReplacementBinary->getFileName() +
|
|
|
|
" is a fat file (must be a thin file)");
|
2019-09-28 06:33:18 +08:00
|
|
|
Slice S(O);
|
|
|
|
auto Entry = Slices.try_emplace(S.getArchString(), S);
|
2019-07-30 02:46:34 +08:00
|
|
|
if (!Entry.second)
|
2019-09-28 06:33:18 +08:00
|
|
|
reportError("-replace " + S.getArchString() +
|
2019-07-30 02:46:34 +08:00
|
|
|
" <file_name> specified multiple times: " +
|
2019-09-24 06:22:55 +08:00
|
|
|
Entry.first->second.getBinary()->getFileName() + ", " +
|
2019-07-30 02:46:34 +08:00
|
|
|
O->getFileName());
|
|
|
|
}
|
2019-08-06 03:06:55 +08:00
|
|
|
auto SlicesMapRange = map_range(
|
|
|
|
Slices, [](StringMapEntry<Slice> &E) -> Slice & { return E.getValue(); });
|
2019-09-24 06:22:55 +08:00
|
|
|
updateAlignments(SlicesMapRange, Alignments);
|
2019-07-30 02:46:34 +08:00
|
|
|
return Slices;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN
|
|
|
|
static void replaceSlices(ArrayRef<OwningBinary<Binary>> InputBinaries,
|
2019-08-06 03:06:55 +08:00
|
|
|
const StringMap<const uint32_t> &Alignments,
|
2019-07-30 02:46:34 +08:00
|
|
|
StringRef OutputFileName,
|
2019-09-28 06:33:18 +08:00
|
|
|
ArrayRef<InputFile> ReplacementFiles) {
|
2019-07-30 02:46:34 +08:00
|
|
|
assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
|
|
|
|
assert(!OutputFileName.empty() && "Replace expects a single output file");
|
|
|
|
|
|
|
|
if (InputBinaries.front().getBinary()->isMachO())
|
|
|
|
reportError("input file " +
|
|
|
|
InputBinaries.front().getBinary()->getFileName() +
|
|
|
|
" must be a fat file when the -replace option is specified");
|
|
|
|
|
|
|
|
SmallVector<OwningBinary<Binary>, 1> ReplacementBinaries =
|
|
|
|
readInputBinaries(ReplacementFiles);
|
|
|
|
|
|
|
|
StringMap<Slice> ReplacementSlices =
|
2019-09-28 06:33:18 +08:00
|
|
|
buildReplacementSlices(ReplacementBinaries, Alignments);
|
2019-07-30 02:46:34 +08:00
|
|
|
SmallVector<std::unique_ptr<MachOObjectFile>, 2> ExtractedObjects;
|
2019-08-06 03:06:55 +08:00
|
|
|
SmallVector<Slice, 2> Slices =
|
|
|
|
buildSlices(InputBinaries, Alignments, ExtractedObjects);
|
2019-07-30 02:46:34 +08:00
|
|
|
|
|
|
|
for (auto &Slice : Slices) {
|
2019-09-24 06:22:55 +08:00
|
|
|
auto It = ReplacementSlices.find(Slice.getArchString());
|
2019-07-30 02:46:34 +08:00
|
|
|
if (It != ReplacementSlices.end()) {
|
|
|
|
Slice = It->second;
|
|
|
|
ReplacementSlices.erase(It); // only keep remaining replacing arch_types
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReplacementSlices.empty())
|
|
|
|
reportError("-replace " + ReplacementSlices.begin()->first() +
|
|
|
|
" <file_name> specified but fat file: " +
|
|
|
|
InputBinaries.front().getBinary()->getFileName() +
|
|
|
|
" does not contain that architecture");
|
2019-08-06 03:06:55 +08:00
|
|
|
|
|
|
|
checkUnusedAlignments(Slices, Alignments);
|
2019-07-30 02:46:34 +08:00
|
|
|
createUniversalBinary(Slices, OutputFileName);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:22:12 +08:00
|
|
|
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);
|
2019-06-08 04:47:58 +08:00
|
|
|
|
|
|
|
switch (C.ActionToPerform) {
|
|
|
|
case LipoAction::VerifyArch:
|
2019-05-29 07:22:12 +08:00
|
|
|
verifyArch(InputBinaries, C.VerifyArchList);
|
2019-06-08 04:47:58 +08:00
|
|
|
break;
|
|
|
|
case LipoAction::PrintArchs:
|
|
|
|
printArchs(InputBinaries);
|
|
|
|
break;
|
2019-07-23 08:42:03 +08:00
|
|
|
case LipoAction::PrintInfo:
|
|
|
|
printInfo(InputBinaries);
|
|
|
|
break;
|
2019-06-22 05:59:01 +08:00
|
|
|
case LipoAction::ThinArch:
|
2019-11-21 10:16:11 +08:00
|
|
|
thinSlice(InputBinaries, C.ArchType, C.OutputFile);
|
|
|
|
break;
|
|
|
|
case LipoAction::ExtractArch:
|
|
|
|
extractSlice(InputBinaries, C.SegmentAlignments, C.ArchType, C.OutputFile);
|
2019-06-22 05:59:01 +08:00
|
|
|
break;
|
2019-07-19 06:48:38 +08:00
|
|
|
case LipoAction::CreateUniversal:
|
2019-08-06 03:06:55 +08:00
|
|
|
createUniversalBinary(InputBinaries, C.SegmentAlignments, C.OutputFile);
|
2019-07-19 06:48:38 +08:00
|
|
|
break;
|
2019-07-30 02:46:34 +08:00
|
|
|
case LipoAction::ReplaceArch:
|
2019-08-06 03:06:55 +08:00
|
|
|
replaceSlices(InputBinaries, C.SegmentAlignments, C.OutputFile,
|
2019-09-28 06:33:18 +08:00
|
|
|
C.ReplacementFiles);
|
2019-07-30 02:46:34 +08:00
|
|
|
break;
|
2019-06-08 04:47:58 +08:00
|
|
|
}
|
2019-05-29 07:22:12 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|