#include "ObjcopyOptions.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/ObjCopy/COFF/COFFConfig.h"
#include "llvm/ObjCopy/COFF/COFFObjcopy.h"
#include "llvm/ObjCopy/CommonConfig.h"
#include "llvm/ObjCopy/ELF/ELFConfig.h"
#include "llvm/ObjCopy/ELF/ELFObjcopy.h"
#include "llvm/ObjCopy/MachO/MachOConfig.h"
#include "llvm/ObjCopy/MachO/MachOObjcopy.h"
#include "llvm/ObjCopy/ObjCopy.h"
#include "llvm/ObjCopy/wasm/WasmConfig.h"
#include "llvm/ObjCopy/wasm/WasmObjcopy.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ArchiveWriter.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/ELFTypes.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/Wasm.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/SmallVectorMemoryBuffer.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <memory>
#include <string>
#include <system_error>
#include <utility>
using namespace llvm;
using namespace llvm::objcopy;
using namespace llvm::object;
static StringRef ToolName;
static ErrorSuccess reportWarning(Error E) {
assert(E);
WithColor::warning(errs(), ToolName) << toString(std::move(E)) << '\n';
return Error::success();
}
static Expected<DriverConfig> getDriverConfig(ArrayRef<const char *> Args) {
StringRef Stem = sys::path::stem(ToolName);
auto Is = [=](StringRef Tool) {
auto I = Stem.rfind_insensitive(Tool);
return I != StringRef::npos &&
(I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
};
if (Is("bitcode-strip") || Is("bitcode_strip"))
return parseBitcodeStripOptions(Args, reportWarning);
else if (Is("strip"))
return parseStripOptions(Args, reportWarning);
else if (Is("install-name-tool") || Is("install_name_tool"))
return parseInstallNameToolOptions(Args);
else
return parseObjcopyOptions(Args, reportWarning);
}
static Error executeObjcopyOnIHex(ConfigManager &ConfigMgr, MemoryBuffer &In,
raw_ostream &Out) {
Expected<const ELFConfig &> ELFConfig = ConfigMgr.getELFConfig();
if (!ELFConfig)
return ELFConfig.takeError();
return elf::executeObjcopyOnIHex(ConfigMgr.getCommonConfig(), *ELFConfig, In,
Out);
}
static Error executeObjcopyOnRawBinary(ConfigManager &ConfigMgr,
MemoryBuffer &In, raw_ostream &Out) {
const CommonConfig &Config = ConfigMgr.getCommonConfig();
switch (Config.OutputFormat) {
case FileFormat::ELF:
case FileFormat::Binary:
case FileFormat::IHex:
case FileFormat::Unspecified:
Expected<const ELFConfig &> ELFConfig = ConfigMgr.getELFConfig();
if (!ELFConfig)
return ELFConfig.takeError();
return elf::executeObjcopyOnRawBinary(Config, *ELFConfig, In, Out);
}
llvm_unreachable("unsupported output format");
}
static Error executeObjcopy(ConfigManager &ConfigMgr) {
CommonConfig &Config = ConfigMgr.Common;
Expected<FilePermissionsApplier> PermsApplierOrErr =
FilePermissionsApplier::create(Config.InputFilename);
if (!PermsApplierOrErr)
return PermsApplierOrErr.takeError();
std::function<Error(raw_ostream & OutFile)> ObjcopyFunc;
OwningBinary<llvm::object::Binary> BinaryHolder;
std::unique_ptr<MemoryBuffer> MemoryBufferHolder;
if (Config.InputFormat == FileFormat::Binary ||
Config.InputFormat == FileFormat::IHex) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
MemoryBuffer::getFileOrSTDIN(Config.InputFilename);
if (!BufOrErr)
return createFileError(Config.InputFilename, BufOrErr.getError());
MemoryBufferHolder = std::move(*BufOrErr);
if (Config.InputFormat == FileFormat::Binary)
ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
return executeObjcopyOnRawBinary(ConfigMgr, *MemoryBufferHolder,
OutFile);
};
else
ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
return executeObjcopyOnIHex(ConfigMgr, *MemoryBufferHolder, OutFile);
};
} else {
Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
createBinary(Config.InputFilename);
if (!BinaryOrErr)
return createFileError(Config.InputFilename, BinaryOrErr.takeError());
BinaryHolder = std::move(*BinaryOrErr);
if (Archive *Ar = dyn_cast<Archive>(BinaryHolder.getBinary())) {
if (Error E = executeObjcopyOnArchive(ConfigMgr, *Ar))
return E;
} else {
ObjcopyFunc = [&](raw_ostream &OutFile) -> Error {
return executeObjcopyOnBinary(ConfigMgr, *BinaryHolder.getBinary(),
OutFile);
};
}
}
if (ObjcopyFunc) {
if (Config.SplitDWO.empty()) {
if (Error E = writeToOutput(Config.OutputFilename, ObjcopyFunc))
return E;
} else {
Config.ExtractDWO = true;
Config.StripDWO = false;
if (Error E = writeToOutput(Config.SplitDWO, ObjcopyFunc))
return E;
Config.ExtractDWO = false;
Config.StripDWO = true;
if (Error E = writeToOutput(Config.OutputFilename, ObjcopyFunc))
return E;
}
}
if (Error E =
PermsApplierOrErr->apply(Config.OutputFilename, Config.PreserveDates))
return E;
if (!Config.SplitDWO.empty())
if (Error E =
PermsApplierOrErr->apply(Config.SplitDWO, Config.PreserveDates,
static_cast<sys::fs::perms>(0666)))
return E;
return Error::success();
}
int llvm_objcopy_main(int argc, char **argv) {
InitLLVM X(argc, argv);
ToolName = argv[0];
SmallVector<const char *, 20> NewArgv(argv, argv + argc);
BumpPtrAllocator A;
StringSaver Saver(A);
cl::ExpandResponseFiles(Saver,
Triple(sys::getProcessTriple()).isOSWindows()
? cl::TokenizeWindowsCommandLine
: cl::TokenizeGNUCommandLine,
NewArgv);
auto Args = makeArrayRef(NewArgv).drop_front();
Expected<DriverConfig> DriverConfig = getDriverConfig(Args);
if (!DriverConfig) {
logAllUnhandledErrors(DriverConfig.takeError(),
WithColor::error(errs(), ToolName));
return 1;
}
for (ConfigManager &ConfigMgr : DriverConfig->CopyConfigs) {
if (Error E = executeObjcopy(ConfigMgr)) {
logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName));
return 1;
}
}
return 0;
}