2017-11-02 05:16:06 +08:00
|
|
|
//===- llvm-objcopy.cpp ---------------------------------------------------===//
|
2017-08-01 08:33:58 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-02 05:16:06 +08:00
|
|
|
|
2017-08-01 08:33:58 +08:00
|
|
|
#include "llvm-objcopy.h"
|
|
|
|
#include "Object.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2018-07-07 01:51:03 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
|
|
|
#include "llvm/Object/ELFTypes.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
2018-04-24 13:43:32 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2017-08-01 08:33:58 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2017-08-01 08:33:58 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2018-04-14 02:26:06 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2018-05-08 03:32:09 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
2017-08-01 08:33:58 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2017-11-02 05:16:06 +08:00
|
|
|
#include <utility>
|
2017-08-01 08:33:58 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
using namespace ELF;
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-05-08 03:32:09 +08:00
|
|
|
enum ObjcopyID {
|
2018-04-24 13:43:32 +08:00
|
|
|
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,
|
2018-05-08 03:32:09 +08:00
|
|
|
#include "ObjcopyOpts.inc"
|
2018-04-24 13:43:32 +08:00
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2018-05-24 04:39:52 +08:00
|
|
|
#define PREFIX(NAME, VALUE) const char *const OBJCOPY_##NAME[] = VALUE;
|
2018-05-08 03:32:09 +08:00
|
|
|
#include "ObjcopyOpts.inc"
|
2018-04-24 13:43:32 +08:00
|
|
|
#undef PREFIX
|
|
|
|
|
2018-04-24 14:23:22 +08:00
|
|
|
static const opt::OptTable::Info ObjcopyInfoTable[] = {
|
2018-04-24 13:43:32 +08:00
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
2018-05-24 04:39:52 +08:00
|
|
|
{OBJCOPY_##PREFIX, \
|
|
|
|
NAME, \
|
|
|
|
HELPTEXT, \
|
|
|
|
METAVAR, \
|
|
|
|
OBJCOPY_##ID, \
|
|
|
|
opt::Option::KIND##Class, \
|
|
|
|
PARAM, \
|
|
|
|
FLAGS, \
|
|
|
|
OBJCOPY_##GROUP, \
|
|
|
|
OBJCOPY_##ALIAS, \
|
|
|
|
ALIASARGS, \
|
|
|
|
VALUES},
|
2018-05-08 03:32:09 +08:00
|
|
|
#include "ObjcopyOpts.inc"
|
2018-04-24 13:43:32 +08:00
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class ObjcopyOptTable : public opt::OptTable {
|
|
|
|
public:
|
|
|
|
ObjcopyOptTable() : OptTable(ObjcopyInfoTable, true) {}
|
|
|
|
};
|
|
|
|
|
2018-05-08 03:32:09 +08:00
|
|
|
enum StripID {
|
|
|
|
STRIP_INVALID = 0, // This is not an option ID.
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
|
|
|
STRIP_##ID,
|
|
|
|
#include "StripOpts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2018-05-24 04:39:52 +08:00
|
|
|
#define PREFIX(NAME, VALUE) const char *const STRIP_##NAME[] = VALUE;
|
|
|
|
#include "StripOpts.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
2018-05-08 03:32:09 +08:00
|
|
|
static const opt::OptTable::Info StripInfoTable[] = {
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
2018-05-24 04:39:52 +08:00
|
|
|
{STRIP_##PREFIX, NAME, HELPTEXT, \
|
|
|
|
METAVAR, STRIP_##ID, opt::Option::KIND##Class, \
|
|
|
|
PARAM, FLAGS, STRIP_##GROUP, \
|
|
|
|
STRIP_##ALIAS, ALIASARGS, VALUES},
|
2018-05-08 03:32:09 +08:00
|
|
|
#include "StripOpts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class StripOptTable : public opt::OptTable {
|
|
|
|
public:
|
|
|
|
StripOptTable() : OptTable(StripInfoTable, true) {}
|
|
|
|
};
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
} // namespace
|
|
|
|
|
2017-08-01 08:33:58 +08:00
|
|
|
// The name this program was invoked as.
|
|
|
|
static StringRef ToolName;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2017-10-12 07:54:34 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
|
2017-08-01 08:33:58 +08: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-02 05:16:06 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
|
2017-08-01 08:33:58 +08: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-02 05:16:06 +08:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2018-04-24 13:43:32 +08: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-27 02:28:17 +08:00
|
|
|
std::vector<StringRef> SymbolsToLocalize;
|
2018-04-28 03:09:44 +08:00
|
|
|
std::vector<StringRef> SymbolsToGlobalize;
|
2018-04-28 03:16:27 +08:00
|
|
|
std::vector<StringRef> SymbolsToWeaken;
|
2018-05-10 05:36:54 +08:00
|
|
|
std::vector<StringRef> SymbolsToRemove;
|
2018-05-15 22:09:37 +08:00
|
|
|
std::vector<StringRef> SymbolsToKeep;
|
2018-04-27 02:28:17 +08:00
|
|
|
StringMap<StringRef> SymbolsToRename;
|
2018-05-08 03:32:09 +08:00
|
|
|
bool StripAll = false;
|
|
|
|
bool StripAllGNU = false;
|
|
|
|
bool StripDebug = false;
|
|
|
|
bool StripSections = false;
|
|
|
|
bool StripNonAlloc = false;
|
|
|
|
bool StripDWO = false;
|
2018-05-25 19:01:25 +08:00
|
|
|
bool StripUnneeded = false;
|
2018-05-08 03:32:09 +08:00
|
|
|
bool ExtractDWO = false;
|
|
|
|
bool LocalizeHidden = false;
|
|
|
|
bool Weaken = false;
|
|
|
|
bool DiscardAll = false;
|
2018-05-16 04:53:53 +08:00
|
|
|
bool OnlyKeepDebug = false;
|
2018-05-26 16:10:37 +08:00
|
|
|
bool KeepFileSymbols = false;
|
2018-04-24 13:43:32 +08:00
|
|
|
};
|
2017-10-12 02:09:18 +08:00
|
|
|
|
2017-11-02 05:16:06 +08:00
|
|
|
using SectionPred = std::function<bool(const SectionBase &Sec)>;
|
2017-08-01 08:33:58 +08:00
|
|
|
|
2017-12-16 04:17:55 +08:00
|
|
|
bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); }
|
2017-11-04 02:58:41 +08:00
|
|
|
|
2018-01-26 06:46:17 +08:00
|
|
|
bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
|
2017-11-04 02:58:41 +08:00
|
|
|
// We can't remove the section header string table.
|
2018-01-26 06:46:17 +08:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-04 02:58:41 +08: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 13:43:32 +08:00
|
|
|
std::unique_ptr<Writer> CreateWriter(const CopyConfig &Config, Object &Obj,
|
2018-07-07 01:51:03 +08:00
|
|
|
Buffer &Buf, ElfType OutputElfType) {
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.OutputFormat == "binary") {
|
2018-07-07 01:51:03 +08:00
|
|
|
return llvm::make_unique<BinaryWriter>(Obj, Buf);
|
2018-01-26 06:46:17 +08:00
|
|
|
}
|
|
|
|
// Depending on the initial ELFT and OutputFormat we need a different Writer.
|
|
|
|
switch (OutputElfType) {
|
|
|
|
case ELFT_ELF32LE:
|
2018-07-07 01:51:03 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf,
|
2018-04-24 13:43:32 +08:00
|
|
|
!Config.StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
case ELFT_ELF64LE:
|
2018-07-07 01:51:03 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf,
|
2018-04-24 13:43:32 +08:00
|
|
|
!Config.StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
case ELFT_ELF32BE:
|
2018-07-07 01:51:03 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf,
|
2018-04-24 13:43:32 +08:00
|
|
|
!Config.StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
case ELFT_ELF64BE:
|
2018-07-07 01:51:03 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf,
|
2018-04-24 13:43:32 +08:00
|
|
|
!Config.StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid output format");
|
|
|
|
}
|
2018-01-26 05:03:38 +08:00
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
void SplitDWOToFile(const CopyConfig &Config, const Reader &Reader,
|
|
|
|
StringRef File, ElfType OutputElfType) {
|
2018-01-26 06:46:17 +08:00
|
|
|
auto DWOFile = Reader.create();
|
|
|
|
DWOFile->removeSections(
|
|
|
|
[&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
|
2018-07-07 01:51:03 +08:00
|
|
|
FileBuffer FB(File);
|
|
|
|
auto Writer = CreateWriter(Config, *DWOFile, FB, OutputElfType);
|
2018-01-26 06:46:17 +08:00
|
|
|
Writer->finalize();
|
|
|
|
Writer->write();
|
2017-11-04 02:58:41 +08:00
|
|
|
}
|
|
|
|
|
2017-12-01 04:14:53 +08: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 13:43:32 +08:00
|
|
|
void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
|
|
|
|
ElfType OutputElfType) {
|
2018-01-26 05:03:38 +08:00
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (!Config.SplitDWO.empty()) {
|
|
|
|
SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
|
2018-01-26 06:46:17 +08:00
|
|
|
}
|
2018-05-24 04:39:52 +08:00
|
|
|
|
|
|
|
// TODO: update or remove symbols only if there is an option that affects
|
|
|
|
// them.
|
2018-05-23 02:24:07 +08:00
|
|
|
if (Obj.SymbolTable) {
|
|
|
|
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
|
|
|
|
if ((Config.LocalizeHidden &&
|
|
|
|
(Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
|
|
|
|
(!Config.SymbolsToLocalize.empty() &&
|
|
|
|
is_contained(Config.SymbolsToLocalize, Sym.Name)))
|
|
|
|
Sym.Binding = STB_LOCAL;
|
|
|
|
|
|
|
|
if (!Config.SymbolsToGlobalize.empty() &&
|
|
|
|
is_contained(Config.SymbolsToGlobalize, Sym.Name))
|
|
|
|
Sym.Binding = STB_GLOBAL;
|
|
|
|
|
|
|
|
if (!Config.SymbolsToWeaken.empty() &&
|
|
|
|
is_contained(Config.SymbolsToWeaken, Sym.Name) &&
|
|
|
|
Sym.Binding == STB_GLOBAL)
|
|
|
|
Sym.Binding = STB_WEAK;
|
|
|
|
|
|
|
|
if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
|
|
|
|
Sym.getShndx() != SHN_UNDEF)
|
|
|
|
Sym.Binding = STB_WEAK;
|
|
|
|
|
|
|
|
const auto I = Config.SymbolsToRename.find(Sym.Name);
|
|
|
|
if (I != Config.SymbolsToRename.end())
|
|
|
|
Sym.Name = I->getValue();
|
|
|
|
});
|
|
|
|
|
2018-05-25 19:01:25 +08:00
|
|
|
// The purpose of this loop is to mark symbols referenced by sections
|
|
|
|
// (like GroupSection or RelocationSection). This way, we know which
|
|
|
|
// symbols are still 'needed' and wich are not.
|
|
|
|
if (Config.StripUnneeded) {
|
|
|
|
for (auto &Section : Obj.sections())
|
|
|
|
Section.markSymbols();
|
|
|
|
}
|
|
|
|
|
2018-05-23 02:24:07 +08:00
|
|
|
Obj.removeSymbols([&](const Symbol &Sym) {
|
2018-05-26 16:10:37 +08:00
|
|
|
if ((!Config.SymbolsToKeep.empty() &&
|
|
|
|
is_contained(Config.SymbolsToKeep, Sym.Name)) ||
|
|
|
|
(Config.KeepFileSymbols && Sym.Type == STT_FILE))
|
2018-05-23 02:24:07 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Config.DiscardAll && Sym.Binding == STB_LOCAL &&
|
|
|
|
Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE &&
|
|
|
|
Sym.Type != STT_SECTION)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Config.StripAll || Config.StripAllGNU)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Config.SymbolsToRemove.empty() &&
|
|
|
|
is_contained(Config.SymbolsToRemove, Sym.Name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:19:46 +08:00
|
|
|
if (Config.StripUnneeded && !Sym.Referenced &&
|
2018-05-25 19:01:25 +08:00
|
|
|
(Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
|
|
|
|
Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
|
|
|
|
return true;
|
|
|
|
|
2018-05-23 02:24:07 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2017-11-04 02:58:41 +08:00
|
|
|
|
2017-10-12 02:09:18 +08:00
|
|
|
SectionPred RemovePred = [](const SectionBase &) { return false; };
|
|
|
|
|
2017-12-01 04:14:53 +08:00
|
|
|
// Removes:
|
2018-04-24 13:43:32 +08: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-12 02:09:18 +08:00
|
|
|
};
|
2017-10-11 02:47:09 +08:00
|
|
|
}
|
2017-10-12 02:09:18 +08:00
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.StripDWO || !Config.SplitDWO.empty())
|
2017-11-04 04:57:09 +08:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
2017-11-04 02:58:41 +08:00
|
|
|
return IsDWOSection(Sec) || RemovePred(Sec);
|
|
|
|
};
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.ExtractDWO)
|
2017-11-04 02:58:41 +08:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
2018-01-26 06:46:17 +08:00
|
|
|
return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
|
2017-11-04 02:58:41 +08:00
|
|
|
};
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.StripAllGNU)
|
2017-11-14 06:02:07 +08:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
|
|
|
if ((Sec.Flags & SHF_ALLOC) != 0)
|
|
|
|
return false;
|
2018-01-26 06:46:17 +08:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-14 06:02:07 +08:00
|
|
|
return false;
|
2017-12-16 04:17:55 +08:00
|
|
|
switch (Sec.Type) {
|
2017-11-14 06:02:07 +08:00
|
|
|
case SHT_SYMTAB:
|
|
|
|
case SHT_REL:
|
|
|
|
case SHT_RELA:
|
|
|
|
case SHT_STRTAB:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return Sec.Name.startswith(".debug");
|
|
|
|
};
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.StripSections) {
|
2017-10-12 02:09:18 +08:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.StripDebug) {
|
2017-11-14 06:13:08 +08:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || Sec.Name.startswith(".debug");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.StripNonAlloc)
|
2017-11-15 02:50:24 +08:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
2018-01-26 06:46:17 +08:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-15 02:50:24 +08:00
|
|
|
return false;
|
|
|
|
return (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (Config.StripAll)
|
2017-11-28 02:56:01 +08:00
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
if (RemovePred(Sec))
|
|
|
|
return true;
|
2018-01-26 06:46:17 +08:00
|
|
|
if (&Sec == Obj.SectionNames)
|
2017-11-28 02:56:01 +08:00
|
|
|
return false;
|
|
|
|
if (Sec.Name.startswith(".gnu.warning"))
|
|
|
|
return false;
|
|
|
|
return (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
|
2017-12-01 04:14:53 +08:00
|
|
|
// Explicit copies:
|
2018-04-24 13:43:32 +08:00
|
|
|
if (!Config.OnlyKeep.empty()) {
|
|
|
|
RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
|
2017-12-01 04:14:53 +08:00
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
2018-04-24 13:43:32 +08:00
|
|
|
if (std::find(std::begin(Config.OnlyKeep), std::end(Config.OnlyKeep),
|
|
|
|
Sec.Name) != std::end(Config.OnlyKeep))
|
2017-12-01 04:14:53 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Allow all implicit removes.
|
2018-01-26 06:46:17 +08:00
|
|
|
if (RemovePred(Sec))
|
2017-12-01 04:14:53 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Keep special sections.
|
2018-01-26 06:46:17 +08:00
|
|
|
if (Obj.SectionNames == &Sec)
|
2017-12-01 04:14:53 +08:00
|
|
|
return false;
|
2018-01-26 06:46:17 +08:00
|
|
|
if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
|
2017-12-01 04:14:53 +08:00
|
|
|
return false;
|
2018-01-26 06:46:17 +08:00
|
|
|
|
2017-12-01 04:14:53 +08:00
|
|
|
// Remove everything else.
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (!Config.Keep.empty()) {
|
|
|
|
RemovePred = [Config, RemovePred](const SectionBase &Sec) {
|
2017-12-01 04:14:53 +08:00
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
2018-04-24 13:43:32 +08:00
|
|
|
if (std::find(std::begin(Config.Keep), std::end(Config.Keep), Sec.Name) !=
|
|
|
|
std::end(Config.Keep))
|
2017-12-01 04:14:53 +08:00
|
|
|
return false;
|
|
|
|
// Otherwise defer to RemovePred.
|
|
|
|
return RemovePred(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-23 02:24:07 +08:00
|
|
|
// This has to be the last predicate assignment.
|
|
|
|
// If the option --keep-symbol has been specified
|
2018-05-24 04:39:52 +08:00
|
|
|
// and at least one of those symbols is present
|
2018-05-23 02:24:07 +08:00
|
|
|
// (equivalently, the updated symbol table is not empty)
|
|
|
|
// the symbol table and the string table should not be removed.
|
2018-05-26 16:10:37 +08:00
|
|
|
if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
|
|
|
|
!Obj.SymbolTable->empty()) {
|
2018-05-23 02:24:07 +08:00
|
|
|
RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
|
|
|
|
if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
|
|
|
|
return false;
|
|
|
|
return RemovePred(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:17 +08:00
|
|
|
Obj.removeSections(RemovePred);
|
2017-12-19 08:47:30 +08:00
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
if (!Config.AddSection.empty()) {
|
|
|
|
for (const auto &Flag : Config.AddSection) {
|
|
|
|
auto SecPair = Flag.split("=");
|
2017-12-19 08:47:30 +08: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-26 06:46:17 +08:00
|
|
|
Obj.addSection<OwnedDataSection>(SecName,
|
|
|
|
ArrayRef<uint8_t>(BufPtr, BufSize));
|
2017-12-19 08:47:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 02:28:17 +08:00
|
|
|
if (!Config.AddGnuDebugLink.empty())
|
2018-04-24 13:43:32 +08:00
|
|
|
Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink);
|
2018-01-26 06:46:17 +08:00
|
|
|
}
|
2018-01-26 06:15:14 +08:00
|
|
|
|
2018-07-07 01:51:03 +08:00
|
|
|
void ExecuteElfObjcopyOnBinary(const CopyConfig &Config, Binary &Binary,
|
|
|
|
Buffer &Out) {
|
|
|
|
ELFReader Reader(&Binary);
|
|
|
|
std::unique_ptr<Object> Obj = Reader.create();
|
2017-08-01 08:33:58 +08:00
|
|
|
|
2018-07-07 01:51:03 +08:00
|
|
|
HandleArgs(Config, *Obj, Reader, Reader.getElfType());
|
|
|
|
|
|
|
|
std::unique_ptr<Writer> Writer =
|
|
|
|
CreateWriter(Config, *Obj, Out, Reader.getElfType());
|
2018-04-24 13:43:32 +08:00
|
|
|
Writer->finalize();
|
|
|
|
Writer->write();
|
|
|
|
}
|
|
|
|
|
2018-07-07 01:51:03 +08:00
|
|
|
// For regular archives this function simply calls llvm::writeArchive,
|
|
|
|
// For thin archives it writes the archive file itself as well as its members.
|
|
|
|
Error deepWriteArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
|
|
|
|
bool WriteSymtab, object::Archive::Kind Kind,
|
|
|
|
bool Deterministic, bool Thin) {
|
|
|
|
Error E =
|
|
|
|
writeArchive(ArcName, NewMembers, WriteSymtab, Kind, Deterministic, Thin);
|
|
|
|
if (!Thin || E)
|
|
|
|
return E;
|
|
|
|
for (const NewArchiveMember &Member : NewMembers) {
|
|
|
|
// Internally, FileBuffer will use the buffer created by
|
|
|
|
// FileOutputBuffer::create, for regular files (that is the case for
|
|
|
|
// deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer.
|
|
|
|
// OnDiskBuffer uses a temporary file and then renames it. So in reality
|
|
|
|
// there is no inefficiency / duplicated in-memory buffers in this case. For
|
|
|
|
// now in-memory buffers can not be completely avoided since
|
|
|
|
// NewArchiveMember still requires them even though writeArchive does not
|
|
|
|
// write them on disk.
|
|
|
|
FileBuffer FB(Member.MemberName);
|
|
|
|
FB.allocate(Member.Buf->getBufferSize());
|
|
|
|
std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
|
|
|
|
FB.getBufferStart());
|
|
|
|
if (auto E = FB.commit())
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecuteElfObjcopyOnArchive(const CopyConfig &Config, const Archive &Ar) {
|
|
|
|
std::vector<NewArchiveMember> NewArchiveMembers;
|
|
|
|
Error Err = Error::success();
|
|
|
|
for (const Archive::Child &Child : Ar.children(Err)) {
|
|
|
|
Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
|
|
|
|
if (!ChildOrErr)
|
|
|
|
reportError(Ar.getFileName(), ChildOrErr.takeError());
|
|
|
|
Expected<StringRef> ChildNameOrErr = Child.getName();
|
|
|
|
if (!ChildNameOrErr)
|
|
|
|
reportError(Ar.getFileName(), ChildNameOrErr.takeError());
|
|
|
|
|
|
|
|
MemBuffer MB(ChildNameOrErr.get());
|
|
|
|
ExecuteElfObjcopyOnBinary(Config, **ChildOrErr, MB);
|
|
|
|
|
|
|
|
Expected<NewArchiveMember> Member =
|
|
|
|
NewArchiveMember::getOldMember(Child, true);
|
|
|
|
if (!Member)
|
|
|
|
reportError(Ar.getFileName(), Member.takeError());
|
|
|
|
Member->Buf = MB.releaseMemoryBuffer();
|
|
|
|
Member->MemberName = Member->Buf->getBufferIdentifier();
|
|
|
|
NewArchiveMembers.push_back(std::move(*Member));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Err)
|
|
|
|
reportError(Config.InputFilename, std::move(Err));
|
|
|
|
if (Error E =
|
|
|
|
deepWriteArchive(Config.OutputFilename, NewArchiveMembers,
|
|
|
|
Ar.hasSymbolTable(), Ar.kind(), true, Ar.isThin()))
|
|
|
|
reportError(Config.OutputFilename, std::move(E));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecuteElfObjcopy(const CopyConfig &Config) {
|
|
|
|
Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
|
|
|
|
createBinary(Config.InputFilename);
|
|
|
|
if (!BinaryOrErr)
|
|
|
|
reportError(Config.InputFilename, BinaryOrErr.takeError());
|
|
|
|
|
|
|
|
if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary()))
|
|
|
|
return ExecuteElfObjcopyOnArchive(Config, *Ar);
|
|
|
|
|
|
|
|
FileBuffer FB(Config.OutputFilename);
|
|
|
|
ExecuteElfObjcopyOnBinary(Config, *BinaryOrErr.get().getBinary(), FB);
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
// 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);
|
2018-05-18 12:18:41 +08:00
|
|
|
|
2018-05-09 01:12:54 +08:00
|
|
|
if (InputArgs.size() == 0) {
|
|
|
|
T.PrintHelp(errs(), "llvm-objcopy <input> [ <output> ]", "objcopy tool");
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-05-18 12:18:41 +08:00
|
|
|
|
2018-05-09 01:12:54 +08:00
|
|
|
if (InputArgs.hasArg(OBJCOPY_help)) {
|
2018-04-24 13:43:32 +08:00
|
|
|
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);
|
2018-04-27 02:28:17 +08:00
|
|
|
|
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_redefine_symbol)) {
|
|
|
|
if (!StringRef(Arg->getValue()).contains('='))
|
|
|
|
error("Bad format for --redefine-sym");
|
|
|
|
auto Old2New = StringRef(Arg->getValue()).split('=');
|
|
|
|
if (!Config.SymbolsToRename.insert(Old2New).second)
|
|
|
|
error("Multiple redefinition of symbol " + Old2New.first);
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:43:32 +08:00
|
|
|
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);
|
2018-05-25 19:01:25 +08:00
|
|
|
Config.StripUnneeded = InputArgs.hasArg(OBJCOPY_strip_unneeded);
|
2018-04-24 13:43:32 +08:00
|
|
|
Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo);
|
|
|
|
Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden);
|
2018-05-03 04:14:49 +08:00
|
|
|
Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken);
|
2018-05-03 04:19:22 +08:00
|
|
|
Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all);
|
2018-05-16 04:53:53 +08:00
|
|
|
Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
|
2018-05-26 16:10:37 +08:00
|
|
|
Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
|
2018-04-27 01:44:43 +08:00
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol))
|
2018-04-27 02:28:17 +08:00
|
|
|
Config.SymbolsToLocalize.push_back(Arg->getValue());
|
2018-04-28 03:09:44 +08:00
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))
|
|
|
|
Config.SymbolsToGlobalize.push_back(Arg->getValue());
|
2018-04-28 03:16:27 +08:00
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_weaken_symbol))
|
|
|
|
Config.SymbolsToWeaken.push_back(Arg->getValue());
|
2018-05-10 05:36:54 +08:00
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_strip_symbol))
|
|
|
|
Config.SymbolsToRemove.push_back(Arg->getValue());
|
2018-05-15 22:09:37 +08:00
|
|
|
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
|
|
|
|
Config.SymbolsToKeep.push_back(Arg->getValue());
|
2018-04-24 13:43:32 +08:00
|
|
|
|
|
|
|
return Config;
|
|
|
|
}
|
|
|
|
|
2018-05-08 03:32:09 +08:00
|
|
|
// ParseStripOptions returns the config and sets the input arguments. If a
|
|
|
|
// help flag is set then ParseStripOptions will print the help messege and
|
|
|
|
// exit.
|
|
|
|
CopyConfig ParseStripOptions(ArrayRef<const char *> ArgsArr) {
|
|
|
|
StripOptTable T;
|
|
|
|
unsigned MissingArgumentIndex, MissingArgumentCount;
|
|
|
|
llvm::opt::InputArgList InputArgs =
|
|
|
|
T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
|
|
|
|
|
2018-05-09 01:12:54 +08:00
|
|
|
if (InputArgs.size() == 0) {
|
|
|
|
T.PrintHelp(errs(), "llvm-strip <input> [ <output> ]", "strip tool");
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-05-18 12:18:41 +08:00
|
|
|
|
2018-05-09 01:12:54 +08:00
|
|
|
if (InputArgs.hasArg(STRIP_help)) {
|
2018-05-08 03:32:09 +08:00
|
|
|
T.PrintHelp(outs(), "llvm-strip <input> [ <output> ]", "strip tool");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<const char *, 2> Positional;
|
|
|
|
for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
|
|
|
|
error("unknown argument '" + Arg->getAsString(InputArgs) + "'");
|
|
|
|
for (auto Arg : InputArgs.filtered(STRIP_INPUT))
|
|
|
|
Positional.push_back(Arg->getValue());
|
|
|
|
|
|
|
|
if (Positional.empty())
|
|
|
|
error("No input file specified");
|
|
|
|
|
|
|
|
if (Positional.size() > 2)
|
|
|
|
error("Support for multiple input files is not implemented yet");
|
|
|
|
|
|
|
|
CopyConfig Config;
|
|
|
|
Config.InputFilename = Positional[0];
|
2018-06-01 04:42:13 +08:00
|
|
|
Config.OutputFilename =
|
|
|
|
InputArgs.getLastArgValue(STRIP_output, Positional[0]);
|
2018-05-08 03:32:09 +08:00
|
|
|
|
|
|
|
Config.StripDebug = InputArgs.hasArg(STRIP_strip_debug);
|
2018-07-07 01:51:03 +08:00
|
|
|
|
2018-06-07 05:23:19 +08:00
|
|
|
Config.DiscardAll = InputArgs.hasArg(STRIP_discard_all);
|
2018-06-07 18:05:25 +08:00
|
|
|
Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded);
|
2018-07-13 01:42:17 +08:00
|
|
|
Config.StripAll = InputArgs.hasArg(STRIP_strip_all);
|
2018-06-07 18:05:25 +08:00
|
|
|
|
|
|
|
if (!Config.StripDebug && !Config.StripUnneeded && !Config.DiscardAll)
|
2018-05-08 03:32:09 +08:00
|
|
|
Config.StripAll = true;
|
2018-05-18 12:18:41 +08:00
|
|
|
|
2018-05-11 13:27:06 +08:00
|
|
|
for (auto Arg : InputArgs.filtered(STRIP_remove_section))
|
|
|
|
Config.ToRemove.push_back(Arg->getValue());
|
2018-05-24 04:39:52 +08:00
|
|
|
|
2018-05-24 03:44:19 +08:00
|
|
|
for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
|
|
|
|
Config.SymbolsToKeep.push_back(Arg->getValue());
|
2018-05-11 13:27:06 +08:00
|
|
|
|
2018-05-08 03:32:09 +08:00
|
|
|
return Config;
|
|
|
|
}
|
|
|
|
|
2017-08-01 08:33:58 +08:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-14 02:26:06 +08:00
|
|
|
InitLLVM X(argc, argv);
|
2017-08-01 08:33:58 +08:00
|
|
|
ToolName = argv[0];
|
2018-05-08 03:32:09 +08:00
|
|
|
CopyConfig Config;
|
|
|
|
if (sys::path::stem(ToolName).endswith_lower("strip"))
|
|
|
|
Config = ParseStripOptions(makeArrayRef(argv + 1, argc));
|
|
|
|
else
|
|
|
|
Config = ParseObjcopyOptions(makeArrayRef(argv + 1, argc));
|
2018-04-24 13:43:32 +08:00
|
|
|
ExecuteElfObjcopy(Config);
|
2017-08-01 08:33:58 +08:00
|
|
|
}
|