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"
|
|
|
|
#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"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-08-01 02:33:58 +02:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.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;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
|
|
|
|
static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
|
2017-08-01 02:33:58 +02:00
|
|
|
cl::init("-"));
|
2017-11-01 22:16:06 +01:00
|
|
|
static cl::opt<std::string>
|
2017-08-04 23:09:26 +02:00
|
|
|
OutputFormat("O", cl::desc("set output format to one of the following:"
|
|
|
|
"\n\tbinary"));
|
2017-11-01 22:16:06 +01:00
|
|
|
static cl::list<std::string> ToRemove("remove-section",
|
|
|
|
cl::desc("Remove a specific section"));
|
|
|
|
static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
|
|
|
|
cl::aliasopt(ToRemove));
|
|
|
|
static cl::opt<bool> StripSections("strip-sections",
|
|
|
|
cl::desc("Remove all section headers"));
|
2017-11-03 19:58:41 +01:00
|
|
|
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"));
|
2017-10-11 20:09:18 +02:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
using SectionPred = std::function<bool(const SectionBase &Sec)>;
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2017-11-03 19:58:41 +01:00
|
|
|
bool IsDWOSection(const SectionBase &Sec) {
|
|
|
|
return Sec.Name.endswith(".dwo");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) {
|
|
|
|
// We can't remove the section header string table.
|
|
|
|
if (&Sec == Obj.getSectionHeaderStrTab())
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) {
|
2017-08-01 02:33:58 +02:00
|
|
|
std::unique_ptr<FileOutputBuffer> Buffer;
|
2017-11-08 02:05:44 +01:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
2017-11-03 19:58:41 +01:00
|
|
|
FileOutputBuffer::create(File, Obj.totalSize(),
|
|
|
|
FileOutputBuffer::F_executable);
|
2017-11-08 22:15:21 +01:00
|
|
|
handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) {
|
2017-11-03 19:58:41 +01:00
|
|
|
error("failed to open " + OutputFilename);
|
2017-11-08 22:15:21 +01:00
|
|
|
});
|
|
|
|
Buffer = std::move(*BufferOrErr);
|
|
|
|
|
2017-11-03 19:58:41 +01:00
|
|
|
Obj.write(*Buffer);
|
2017-11-08 02:50:29 +01:00
|
|
|
if (auto E = Buffer->commit())
|
|
|
|
reportError(File, errorToErrorCode(std::move(E)));
|
2017-11-03 19:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) {
|
|
|
|
// Construct a second output file for the DWO sections.
|
|
|
|
ELFObject<ELFT> DWOFile(ObjFile);
|
|
|
|
|
|
|
|
DWOFile.removeSections([&](const SectionBase &Sec) {
|
|
|
|
return OnlyKeepDWOPred<ELFT>(DWOFile, Sec);
|
|
|
|
});
|
|
|
|
DWOFile.finalize();
|
|
|
|
WriteObjectFile(DWOFile, File);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
|
2017-08-04 23:09:26 +02:00
|
|
|
std::unique_ptr<Object<ELF64LE>> Obj;
|
2017-11-03 19:58:41 +01:00
|
|
|
|
2017-08-04 23:09:26 +02:00
|
|
|
if (!OutputFormat.empty() && OutputFormat != "binary")
|
|
|
|
error("invalid output format '" + OutputFormat + "'");
|
|
|
|
if (!OutputFormat.empty() && OutputFormat == "binary")
|
|
|
|
Obj = llvm::make_unique<BinaryObject<ELF64LE>>(ObjFile);
|
|
|
|
else
|
|
|
|
Obj = llvm::make_unique<ELFObject<ELF64LE>>(ObjFile);
|
2017-10-11 20:09:18 +02:00
|
|
|
|
2017-11-03 19:58:41 +01:00
|
|
|
if (!SplitDWO.empty())
|
|
|
|
SplitDWOToFile<ELF64LE>(ObjFile, SplitDWO.getValue());
|
|
|
|
|
2017-10-11 20:09:18 +02:00
|
|
|
SectionPred RemovePred = [](const SectionBase &) { return false; };
|
|
|
|
|
2017-10-10 20:47:09 +02:00
|
|
|
if (!ToRemove.empty()) {
|
2017-10-11 20:09:18 +02:00
|
|
|
RemovePred = [&](const SectionBase &Sec) {
|
2017-10-11 01:02:43 +02:00
|
|
|
return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
|
|
|
|
std::end(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
|
|
|
|
2017-11-03 19:58:41 +01:00
|
|
|
if (StripDWO || !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);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ExtractDWO)
|
|
|
|
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
|
|
|
|
return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec);
|
|
|
|
};
|
|
|
|
|
2017-10-11 20:09:18 +02:00
|
|
|
if (StripSections) {
|
|
|
|
RemovePred = [RemovePred](const SectionBase &Sec) {
|
|
|
|
return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
|
|
|
|
};
|
|
|
|
Obj->WriteSectionHeaders = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Obj->removeSections(RemovePred);
|
2017-08-04 23:09:26 +02:00
|
|
|
Obj->finalize();
|
2017-11-03 19:58:41 +01:00
|
|
|
WriteObjectFile(*Obj, OutputFilename.getValue());
|
2017-08-01 02:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm objcopy utility\n");
|
|
|
|
ToolName = argv[0];
|
|
|
|
if (InputFilename.empty()) {
|
|
|
|
cl::PrintHelpMessage();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename);
|
|
|
|
if (!BinaryOrErr)
|
|
|
|
reportError(InputFilename, BinaryOrErr.takeError());
|
|
|
|
Binary &Binary = *BinaryOrErr.get().getBinary();
|
|
|
|
if (ELFObjectFile<ELF64LE> *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) {
|
|
|
|
CopyBinary(*o);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
reportError(InputFilename, object_error::invalid_file_type);
|
|
|
|
}
|