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"
|
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
|
|
|
#include "llvm/Object/ELFTypes.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
|
|
|
#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"
|
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;
|
|
|
|
|
|
|
|
// 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-12 08:40:50 +08:00
|
|
|
static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
|
|
|
|
static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("[ <output> ]"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFormat("O", cl::desc("Set output format to one of the following:"
|
|
|
|
"\n\tbinary"));
|
|
|
|
static cl::list<std::string> ToRemove("remove-section",
|
|
|
|
cl::desc("Remove <section>"),
|
|
|
|
cl::value_desc("section"));
|
|
|
|
static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
|
|
|
|
cl::aliasopt(ToRemove));
|
|
|
|
static cl::opt<bool> StripAll(
|
|
|
|
"strip-all",
|
|
|
|
cl::desc(
|
|
|
|
"Removes non-allocated sections other than .gnu.warning* sections"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
StripAllGNU("strip-all-gnu",
|
|
|
|
cl::desc("Removes symbol, relocation, and debug information"));
|
|
|
|
static cl::list<std::string> Keep("keep", cl::desc("Keep <section>"),
|
|
|
|
cl::value_desc("section"));
|
|
|
|
static cl::list<std::string> OnlyKeep("only-keep",
|
|
|
|
cl::desc("Remove all but <section>"),
|
|
|
|
cl::value_desc("section"));
|
|
|
|
static cl::alias OnlyKeepA("j", cl::desc("Alias for only-keep"),
|
|
|
|
cl::aliasopt(OnlyKeep));
|
|
|
|
static cl::opt<bool> StripDebug("strip-debug",
|
|
|
|
cl::desc("Removes all debug information"));
|
|
|
|
static cl::opt<bool> StripSections("strip-sections",
|
|
|
|
cl::desc("Remove all section headers"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
StripNonAlloc("strip-non-alloc",
|
|
|
|
cl::desc("Remove all non-allocated sections"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
StripDWO("strip-dwo", cl::desc("Remove all DWARF .dwo sections from file"));
|
|
|
|
static cl::opt<bool> ExtractDWO(
|
|
|
|
"extract-dwo",
|
|
|
|
cl::desc("Remove all sections that are not DWARF .dwo sections from file"));
|
|
|
|
static cl::opt<std::string>
|
|
|
|
SplitDWO("split-dwo",
|
|
|
|
cl::desc("Equivalent to extract-dwo on the input file to "
|
|
|
|
"<dwo-file>, then strip-dwo on the input file"),
|
|
|
|
cl::value_desc("dwo-file"));
|
|
|
|
static cl::list<std::string> AddSection(
|
|
|
|
"add-section",
|
|
|
|
cl::desc("Make a section named <section> with the contents of <file>."),
|
|
|
|
cl::value_desc("section=file"));
|
|
|
|
static cl::opt<bool> LocalizeHidden(
|
|
|
|
"localize-hidden",
|
|
|
|
cl::desc(
|
|
|
|
"Mark all symbols that have hidden or internal visibility as local"));
|
|
|
|
static cl::opt<std::string>
|
|
|
|
AddGnuDebugLink("add-gnu-debuglink",
|
|
|
|
cl::desc("adds a .gnu_debuglink for <debug-file>"),
|
|
|
|
cl::value_desc("debug-file"));
|
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-12 08:40:50 +08:00
|
|
|
static ElfType OutputElfType;
|
|
|
|
|
|
|
|
std::unique_ptr<Writer> CreateWriter(Object &Obj, StringRef File) {
|
|
|
|
if (OutputFormat == "binary") {
|
|
|
|
return llvm::make_unique<BinaryWriter>(OutputFilename, Obj);
|
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-04-12 08:40:50 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
case ELFT_ELF64LE:
|
2018-04-12 08:40:50 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, !StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
case ELFT_ELF32BE:
|
2018-04-12 08:40:50 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, !StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
case ELFT_ELF64BE:
|
2018-04-12 08:40:50 +08:00
|
|
|
return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, !StripSections);
|
2018-01-26 06:46:17 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid output format");
|
|
|
|
}
|
2018-01-26 05:03:38 +08:00
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
void SplitDWOToFile(const Reader &Reader, StringRef File) {
|
2018-01-26 06:46:17 +08:00
|
|
|
auto DWOFile = Reader.create();
|
|
|
|
DWOFile->removeSections(
|
|
|
|
[&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); });
|
2018-04-12 08:40:50 +08:00
|
|
|
auto Writer = CreateWriter(*DWOFile, File);
|
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-12 08:40:50 +08:00
|
|
|
void HandleArgs(Object &Obj, const Reader &Reader) {
|
2018-01-26 05:03:38 +08:00
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
if (!SplitDWO.empty()) {
|
|
|
|
SplitDWOToFile(Reader, SplitDWO);
|
2018-01-26 06:46:17 +08:00
|
|
|
}
|
2017-11-04 02:58:41 +08:00
|
|
|
|
2018-01-06 03:19:09 +08:00
|
|
|
// Localize:
|
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
if (LocalizeHidden) {
|
2018-01-26 06:46:17 +08:00
|
|
|
Obj.SymbolTable->localize([](const Symbol &Sym) {
|
2018-01-06 03:19:09 +08:00
|
|
|
return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-12 08:40:50 +08:00
|
|
|
if (!ToRemove.empty()) {
|
|
|
|
RemovePred = [&](const SectionBase &Sec) {
|
|
|
|
return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
|
|
|
|
std::end(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-12 08:40:50 +08:00
|
|
|
if (StripDWO || !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-12 08:40:50 +08:00
|
|
|
if (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-12 08:40:50 +08:00
|
|
|
if (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-12 08:40:50 +08:00
|
|
|
if (StripSections) {
|
2017-10-12 02:09:18 +08:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
if (StripDebug) {
|
2017-11-14 06:13:08 +08:00
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || Sec.Name.startswith(".debug");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
if (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-12 08:40:50 +08:00
|
|
|
if (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-12 08:40:50 +08:00
|
|
|
if (!OnlyKeep.empty()) {
|
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
2017-12-01 04:14:53 +08:00
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
2018-04-12 08:40:50 +08:00
|
|
|
if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) !=
|
|
|
|
std::end(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-12 08:40:50 +08:00
|
|
|
if (!Keep.empty()) {
|
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
2017-12-01 04:14:53 +08:00
|
|
|
// Explicitly keep these sections regardless of previous removes.
|
2018-04-12 08:40:50 +08:00
|
|
|
if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) !=
|
|
|
|
std::end(Keep))
|
2017-12-01 04:14:53 +08:00
|
|
|
return false;
|
|
|
|
// Otherwise defer to RemovePred.
|
|
|
|
return RemovePred(Sec);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:17 +08:00
|
|
|
Obj.removeSections(RemovePred);
|
2017-12-19 08:47:30 +08:00
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
if (!AddSection.empty()) {
|
|
|
|
for (const auto &Flag : AddSection) {
|
|
|
|
auto SecPair = StringRef(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-12 08:40:50 +08:00
|
|
|
if (!AddGnuDebugLink.empty()) {
|
|
|
|
Obj.addSection<GnuDebugLinkSection>(StringRef(AddGnuDebugLink));
|
2018-01-26 06:15:14 +08:00
|
|
|
}
|
2018-01-26 06:46:17 +08:00
|
|
|
}
|
2018-01-26 06:15:14 +08:00
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
std::unique_ptr<Reader> CreateReader() {
|
2018-01-26 06:46:17 +08:00
|
|
|
// Right now we can only read ELF files so there's only one reader;
|
2018-04-12 08:40:50 +08:00
|
|
|
auto Out = llvm::make_unique<ELFReader>(StringRef(InputFilename));
|
2018-01-26 06:46:17 +08:00
|
|
|
// We need to set the default ElfType for output.
|
|
|
|
OutputElfType = Out->getElfType();
|
|
|
|
return std::move(Out);
|
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);
|
2018-04-12 08:40:50 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n");
|
2017-08-01 08:33:58 +08:00
|
|
|
ToolName = argv[0];
|
2018-04-12 08:40:50 +08:00
|
|
|
if (InputFilename.empty()) {
|
|
|
|
cl::PrintHelpMessage();
|
|
|
|
return 2;
|
|
|
|
}
|
2018-01-26 06:46:17 +08:00
|
|
|
|
2018-04-12 08:40:50 +08:00
|
|
|
auto Reader = CreateReader();
|
|
|
|
auto Obj = Reader->create();
|
|
|
|
StringRef Output =
|
|
|
|
OutputFilename.getNumOccurrences() ? OutputFilename : InputFilename;
|
|
|
|
auto Writer = CreateWriter(*Obj, Output);
|
|
|
|
HandleArgs(*Obj, *Reader);
|
|
|
|
Writer->finalize();
|
|
|
|
Writer->write();
|
2017-08-01 08:33:58 +08:00
|
|
|
}
|