2017-11-01 22:16:06 +01:00
|
|
|
//===- llvm-objcopy.cpp ---------------------------------------------------===//
|
2017-08-01 02:33:58 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
#include "llvm-objcopy.h"
|
|
|
|
#include "Object.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
|
|
|
#include "llvm/Object/ELFTypes.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
2018-04-24 07:43:32 +02:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/Casting.h"
|
2017-08-01 02:33:58 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2017-08-01 02:33:58 +02:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
2017-08-01 02:33:58 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2017-11-01 22:16:06 +01:00
|
|
|
#include <utility>
|
2017-08-01 02:33:58 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
using namespace ELF;
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum ID {
|
|
|
|
OBJCOPY_INVALID = 0, // This is not an option ID.
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
|
|
|
OBJCOPY_##ID,
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
2018-04-24 08:23:22 +02:00
|
|
|
static const opt::OptTable::Info ObjcopyInfoTable[] = {
|
2018-04-24 07:43:32 +02:00
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
|
|
|
{PREFIX, NAME, HELPTEXT, \
|
|
|
|
METAVAR, OBJCOPY_##ID, opt::Option::KIND##Class, \
|
|
|
|
PARAM, FLAGS, OBJCOPY_##GROUP, \
|
|
|
|
OBJCOPY_##ALIAS, ALIASARGS, VALUES},
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class ObjcopyOptTable : public opt::OptTable {
|
|
|
|
public:
|
|
|
|
ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
// The name this program was invoked as.
|
|
|
|
static StringRef ToolName;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2017-10-12 01:54:34 +02:00
|
|
|
LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
|
2017-08-01 02:33:58 +02:00
|
|
|
errs() << ToolName << ": " << Message << ".\n";
|
|
|
|
errs().flush();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
|
|
|
|
assert(EC);
|
|
|
|
errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
|
2017-08-01 02:33:58 +02:00
|
|
|
assert(E);
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(std::move(E), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
errs() << ToolName << ": '" << File << "': " << Buf;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
struct CopyConfig {
|
|
|
|
StringRef OutputFilename;
|
|
|
|
StringRef InputFilename;
|
|
|
|
StringRef OutputFormat;
|
|
|
|
StringRef InputFormat;
|
|
|
|
StringRef BinaryArch;
|
|
|
|
|
|
|
|
StringRef SplitDWO;
|
|
|
|
StringRef AddGnuDebugLink;
|
|
|
|
std::vector<StringRef> ToRemove;
|
|
|
|
std::vector<StringRef> Keep;
|
|
|
|
std::vector<StringRef> OnlyKeep;
|
|
|
|
std::vector<StringRef> AddSection;
|
2018-04-26 19:44:43 +02:00
|
|
|
std::vector<StringRef> LocalizeSymbol;
|
2018-04-24 07:43:32 +02:00
|
|
|
bool StripAll;
|
|
|
|
bool StripAllGNU;
|
|
|
|
bool StripDebug;
|
|
|
|
bool StripSections;
|
|
|
|
bool StripNonAlloc;
|
|
|
|
bool StripDWO;
|
|
|
|
bool ExtractDWO;
|
|
|
|
bool LocalizeHidden;
|
|
|
|
};
|
2017-10-11 20:09:18 +02:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
using SectionPred = std::function<bool(const SectionBase &Sec)>;
|
2018-04-26 19:44:43 +02:00
|
|
|
using SymbolPred = std::function<bool(const Symbol &Sym)>;
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2017-12-15 21:17:55 +01:00
|
|
|
bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
|
2017-11-03 19:58:41 +01:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
|
2017-11-03 19:58:41 +01:00
|
|
|
// We can't remove the section header string table.
|
2018-01-25 23:46:17 +01:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-03 19:58:41 +01:00
|
|
|
return false;
|
|
|
|
// Short of keeping the string table we want to keep everything that is a DWO
|
|
|
|
// section and remove everything else.
|
|
|
|
return !IsDWOSection(Sec);
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
|
|
|
|
StringRef File, ElfType OutputElfType) {
|
|
|
|
if (Config.OutputFormat == "binary") {
|
|
|
|
return llvm::make_unique<BinaryWriter>(File, Obj);
|
2018-01-25 23:46:17 +01:00
|
|
|
}
|
|
|
|
// Depending on the initial ELFT and OutputFormat we need a different Writer.
|
|
|
|
switch (OutputElfType) {
|
|
|
|
case ELFT_ELF32LE:
|
2018-04-24 07:43:32 +02:00
|
|
|
return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj,
|
|
|
|
!Config.StripSections);
|
2018-01-25 23:46:17 +01:00
|
|
|
case ELFT_ELF64LE:
|
2018-04-24 07:43:32 +02:00
|
|
|
return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj,
|
|
|
|
!Config.StripSections);
|
2018-01-25 23:46:17 +01:00
|
|
|
case ELFT_ELF32BE:
|
2018-04-24 07:43:32 +02:00
|
|
|
return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj,
|
|
|
|
!Config.StripSections);
|
2018-01-25 23:46:17 +01:00
|
|
|
case ELFT_ELF64BE:
|
2018-04-24 07:43:32 +02:00
|
|
|
return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj,
|
|
|
|
!Config.StripSections);
|
2018-01-25 23:46:17 +01:00
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid output format");
|
|
|
|
}
|
2018-01-25 22:03:38 +01:00
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
|
|
|
|
StringRef File, ElfType OutputElfType) {
|
2018-01-25 23:46:17 +01:00
|
|
|
auto DWOFile = Reader.create();
|
|
|
|
DWOFile->removeSections(
|
|
|
|
[&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
|
2018-04-24 07:43:32 +02:00
|
|
|
auto Writer = CreateWriter(Config, *DWOFile, File, OutputElfType);
|
2018-01-25 23:46:17 +01:00
|
|
|
Writer->finalize();
|
|
|
|
Writer->write();
|
2017-11-03 19:58:41 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 21:14:53 +01:00
|
|
|
// This function handles the high level operations of GNU objcopy including
|
|
|
|
// handling command line options. It's important to outline certain properties
|
|
|
|
// we expect to hold of the command line operations. Any operation that "keeps"
|
|
|
|
// should keep regardless of a remove. Additionally any removal should respect
|
|
|
|
// any previous removals. Lastly whether or not something is removed shouldn't
|
|
|
|
// depend a) on the order the options occur in or b) on some opaque priority
|
|
|
|
// system. The only priority is that keeps/copies overrule removes.
|
2018-04-24 07:43:32 +02:00
|
|
|
void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
|
|
|
|
ElfType OutputElfType) {
|
2018-01-25 22:03:38 +01:00
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (!Config.SplitDWO.empty()) {
|
|
|
|
SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
|
2018-01-25 23:46:17 +01:00
|
|
|
}
|
2017-11-03 19:58:41 +01:00
|
|
|
|
2018-04-26 19:44:43 +02:00
|
|
|
SymbolPred LocalizePred = [](const Symbol &) { return false; };
|
|
|
|
|
2018-01-05 20:19:09 +01:00
|
|
|
// Localize:
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.LocalizeHidden) {
|
2018-04-26 19:44:43 +02:00
|
|
|
LocalizePred = [](const Symbol &Sym) {
|
2018-01-05 20:19:09 +01:00
|
|
|
return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL;
|
2018-04-26 19:44:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config.LocalizeSymbol.empty()) {
|
|
|
|
LocalizePred = [LocalizePred, &Config](const Symbol &Sym) {
|
|
|
|
return LocalizePred(Sym) ||
|
|
|
|
std::find(std::begin(Config.LocalizeSymbol),
|
|
|
|
std::end(Config.LocalizeSymbol),
|
|
|
|
Sym.Name) != std::end(Config.LocalizeSymbol);
|
|
|
|
};
|
2018-01-05 20:19:09 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 19:44:43 +02:00
|
|
|
Obj.SymbolTable->localize(LocalizePred);
|
|
|
|
|
2017-10-11 20:09:18 +02:00
|
|
|
SectionPred RemovePred = [](const SectionBase &) { return false; };
|
|
|
|
|
2017-11-30 21:14:53 +01:00
|
|
|
// Removes:
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (!Config.ToRemove.empty()) {
|
|
|
|
RemovePred = [&Config](const SectionBase &Sec) {
|
|
|
|
return std::find(std::begin(Config.ToRemove), std::end(Config.ToRemove),
|
|
|
|
Sec.Name) != std::end(Config.ToRemove);
|
2017-10-11 20:09:18 +02:00
|
|
|
};
|
2017-10-10 20:47:09 +02:00
|
|
|
}
|
2017-10-11 20:09:18 +02:00
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.StripDWO || !Config.SplitDWO.empty())
|
2017-11-03 21:57:09 +01:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
2017-11-03 19:58:41 +01:00
|
|
|
return IsDWOSection(Sec) || RemovePred(Sec);
|
|
|
|
};
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.ExtractDWO)
|
2017-11-03 19:58:41 +01:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
2018-01-25 23:46:17 +01:00
|
|
|
return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
|
2017-11-03 19:58:41 +01:00
|
|
|
};
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.StripAllGNU)
|
2017-11-13 23:02:07 +01:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
|
|
|
if ((Sec.Flags & SHF_ALLOC) != 0)
|
|
|
|
return false;
|
2018-01-25 23:46:17 +01:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-13 23:02:07 +01:00
|
|
|
return false;
|
2017-12-15 21:17:55 +01:00
|
|
|
switch (Sec.Type) {
|
2017-11-13 23:02:07 +01:00
|
|
|
case SHT_SYMTAB:
|
|
|
|
case SHT_REL:
|
|
|
|
case SHT_RELA:
|
|
|
|
case SHT_STRTAB:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return Sec.Name.startswith(".debug");
|
|
|
|
};
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.StripSections) {
|
2017-10-11 20:09:18 +02:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.StripDebug) {
|
2017-11-13 23:13:08 +01:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || Sec.Name.startswith(".debug");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.StripNonAlloc)
|
2017-11-14 19:50:24 +01:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
2018-01-25 23:46:17 +01:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-14 19:50:24 +01:00
|
|
|
return false;
|
|
|
|
return (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (Config.StripAll)
|
2017-11-27 19:56:01 +01:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
2018-01-25 23:46:17 +01:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-27 19:56:01 +01:00
|
|
|
return false;
|
|
|
|
if (Sec.Name.startswith(".gnu.warning"))
|
|
|
|
return false;
|
|
|
|
return (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
|
2017-11-30 21:14:53 +01:00
|
|
|
// Explicit copies:
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (!Config.OnlyKeep.empty()) {
|
|
|
|
RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
|
2017-11-30 21:14:53 +01:00
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
2018-04-24 07:43:32 +02:00
|
|
|
if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
|
|
|
|
Sec.Name) != std::end(Config.OnlyKeep))
|
2017-11-30 21:14:53 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Allow all implicit removes.
|
2018-01-25 23:46:17 +01:00
|
|
|
if (RemovePred(Sec))
|
2017-11-30 21:14:53 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Keep special sections.
|
2018-01-25 23:46:17 +01:00
|
|
|
if (Obj.SectionNames == &Sec)
|
2017-11-30 21:14:53 +01:00
|
|
|
return false;
|
2018-01-25 23:46:17 +01:00
|
|
|
if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
|
2017-11-30 21:14:53 +01:00
|
|
|
return false;
|
2018-01-25 23:46:17 +01:00
|
|
|
|
2017-11-30 21:14:53 +01:00
|
|
|
// Remove everything else.
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (!Config.Keep.empty()) {
|
|
|
|
RemovePred = [Config, RemovePred](const SectionBase &Sec) {
|
2017-11-30 21:14:53 +01:00
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
2018-04-24 07:43:32 +02:00
|
|
|
if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
|
|
|
|
std::end(Config.Keep))
|
2017-11-30 21:14:53 +01:00
|
|
|
return false;
|
|
|
|
// Otherwise defer to RemovePred.
|
|
|
|
return RemovePred(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
Obj.removeSections(RemovePred);
|
2017-12-19 01:47:30 +01:00
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (!Config.AddSection.empty()) {
|
|
|
|
for (const auto &Flag : Config.AddSection) {
|
|
|
|
auto SecPair = Flag.split("=");
|
2017-12-19 01:47:30 +01:00
|
|
|
auto SecName = SecPair.first;
|
|
|
|
auto File = SecPair.second;
|
|
|
|
auto BufOrErr = MemoryBuffer::getFile(File);
|
|
|
|
if (!BufOrErr)
|
|
|
|
reportError(File, BufOrErr.getError());
|
|
|
|
auto Buf = std::move(*BufOrErr);
|
|
|
|
auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
|
|
|
|
auto BufSize = Buf->getBufferSize();
|
2018-01-25 23:46:17 +01:00
|
|
|
Obj.addSection<OwnedDataSection>(SecName,
|
|
|
|
ArrayRef<uint8_t>(BufPtr, BufSize));
|
2017-12-19 01:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
if (!Config.AddGnuDebugLink.empty()) {
|
|
|
|
Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
|
2018-01-25 23:15:14 +01:00
|
|
|
}
|
2018-01-25 23:46:17 +01:00
|
|
|
}
|
2018-01-25 23:15:14 +01:00
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
std::unique_ptr<Reader> CreateReader(StringRef InputFilename,
|
|
|
|
ElfType &OutputElfType) {
|
2018-01-25 23:46:17 +01:00
|
|
|
// Right now we can only read ELF files so there's only one reader;
|
2018-04-24 07:43:32 +02:00
|
|
|
auto Out = llvm::make_unique<ELFReader>(InputFilename);
|
2018-01-25 23:46:17 +01:00
|
|
|
// We need to set the default ElfType for output.
|
|
|
|
OutputElfType = Out->getElfType();
|
|
|
|
return std::move(Out);
|
2017-08-01 02:33:58 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
void ExecuteElfObjcopy(const CopyConfig &Config) {
|
|
|
|
ElfType OutputElfType;
|
|
|
|
auto Reader = CreateReader(Config.InputFilename, OutputElfType);
|
|
|
|
auto Obj = Reader->create();
|
|
|
|
auto Writer =
|
|
|
|
CreateWriter(Config, *Obj, Config.OutputFilename, OutputElfType);
|
|
|
|
HandleArgs(Config, *Obj, *Reader, OutputElfType);
|
|
|
|
Writer->finalize();
|
|
|
|
Writer->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseObjcopyOptions returns the config and sets the input arguments. If a
|
|
|
|
// help flag is set then ParseObjcopyOptions will print the help messege and
|
|
|
|
// exit.
|
|
|
|
CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
|
|
|
|
ObjcopyOptTable T;
|
|
|
|
unsigned MissingArgumentIndex, MissingArgumentCount;
|
|
|
|
llvm::opt::InputArgList InputArgs =
|
|
|
|
T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
|
|
|
|
|
|
|
|
if (InputArgs.size() == 0 || InputArgs.hasArg(OBJCOPY_help)) {
|
|
|
|
T.PrintHelp(outs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<const char *, 2> Positional;
|
|
|
|
|
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_UNKNOWN))
|
|
|
|
error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
|
|
|
|
|
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_INPUT))
|
|
|
|
Positional.push_back(Arg->getValue());
|
|
|
|
|
|
|
|
if (Positional.empty())
|
|
|
|
error("No input file specified");
|
|
|
|
|
|
|
|
if (Positional.size() > 2)
|
|
|
|
error("Too many positional arguments");
|
|
|
|
|
|
|
|
CopyConfig Config;
|
|
|
|
Config.InputFilename = Positional[0];
|
|
|
|
Config.OutputFilename = Positional[Positional.size() == 1 ? 0 : 1];
|
|
|
|
Config.InputFormat = InputArgs.getLastArgValue(OBJCOPY_input_target);
|
|
|
|
Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target);
|
|
|
|
Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture);
|
|
|
|
|
|
|
|
Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo);
|
|
|
|
Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink);
|
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_remove_section))
|
|
|
|
Config.ToRemove.push_back(Arg->getValue());
|
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_keep))
|
|
|
|
Config.Keep.push_back(Arg->getValue());
|
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_only_keep))
|
|
|
|
Config.OnlyKeep.push_back(Arg->getValue());
|
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_add_section))
|
|
|
|
Config.AddSection.push_back(Arg->getValue());
|
|
|
|
Config.StripAll = InputArgs.hasArg(OBJCOPY_strip_all);
|
|
|
|
Config.StripAllGNU = InputArgs.hasArg(OBJCOPY_strip_all_gnu);
|
|
|
|
Config.StripDebug = InputArgs.hasArg(OBJCOPY_strip_debug);
|
|
|
|
Config.StripDWO = InputArgs.hasArg(OBJCOPY_strip_dwo);
|
|
|
|
Config.StripSections = InputArgs.hasArg(OBJCOPY_strip_sections);
|
|
|
|
Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc);
|
|
|
|
Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
|
|
|
|
Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
|
2018-04-26 19:44:43 +02:00
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
|
|
|
|
Config.LocalizeSymbol.push_back(Arg->getValue());
|
2018-04-24 07:43:32 +02:00
|
|
|
|
|
|
|
return Config;
|
|
|
|
}
|
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-13 20:26:06 +02:00
|
|
|
InitLLVM X(argc, argv);
|
2017-08-01 02:33:58 +02:00
|
|
|
ToolName = argv[0];
|
2018-01-25 23:46:17 +01:00
|
|
|
|
2018-04-24 07:43:32 +02:00
|
|
|
CopyConfig Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
|
|
|
|
ExecuteElfObjcopy(Config);
|
2017-08-01 02:33:58 +02:00
|
|
|
}
|