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"
|
2018-10-12 06:33:50 +08:00
|
|
|
#include "CopyConfig.h"
|
2017-08-01 08:33:58 +08:00
|
|
|
#include "Object.h"
|
2018-10-12 06:33:50 +08:00
|
|
|
|
2018-08-02 00:23:22 +08:00
|
|
|
#include "llvm/ADT/BitmaskEnum.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-08-02 00:23:22 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2018-09-07 16:10:22 +08:00
|
|
|
#include "llvm/MC/MCTargetOptions.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"
|
2018-09-07 16:10:22 +08:00
|
|
|
#include "llvm/Support/Compression.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#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"
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
#include "llvm/Support/Memory.h"
|
2018-05-08 03:32:09 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-08-17 02:29:40 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2018-08-10 06:52:03 +08:00
|
|
|
#include "llvm/Support/WithColor.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;
|
2018-07-18 08:10:51 +08:00
|
|
|
using namespace llvm::objcopy;
|
2017-08-01 08:33:58 +08:00
|
|
|
using namespace object;
|
|
|
|
using namespace ELF;
|
|
|
|
|
2017-11-02 05:16:06 +08:00
|
|
|
using SectionPred = std::function<bool(const SectionBase &Sec)>;
|
2017-08-01 08:33:58 +08:00
|
|
|
|
2018-07-18 08:10:51 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
|
|
|
|
// The name this program was invoked as.
|
|
|
|
StringRef ToolName;
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
|
2018-08-10 06:52:03 +08:00
|
|
|
WithColor::error(errs(), ToolName) << Message << ".\n";
|
2018-07-18 08:10:51 +08:00
|
|
|
errs().flush();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
|
|
|
|
assert(EC);
|
2018-08-10 06:52:03 +08:00
|
|
|
WithColor::error(errs(), ToolName)
|
|
|
|
<< "'" << File << "': " << EC.message() << ".\n";
|
2018-07-18 08:10:51 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
|
|
|
|
assert(E);
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(std::move(E), OS, "");
|
|
|
|
OS.flush();
|
2018-08-10 06:52:03 +08:00
|
|
|
WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf;
|
2018-07-18 08:10:51 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace objcopy
|
2018-07-17 06:17:05 +08:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2018-08-14 05:30:27 +08:00
|
|
|
static bool isDebugSection(const SectionBase &Sec) {
|
2018-09-07 16:10:22 +08:00
|
|
|
return StringRef(Sec.Name).startswith(".debug") ||
|
|
|
|
StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index";
|
2018-07-28 06:51:36 +08:00
|
|
|
}
|
|
|
|
|
2018-08-14 05:30:27 +08:00
|
|
|
static bool isDWOSection(const SectionBase &Sec) {
|
2018-09-07 16:10:22 +08:00
|
|
|
return StringRef(Sec.Name).endswith(".dwo");
|
2018-07-17 06:17:05 +08:00
|
|
|
}
|
2017-11-04 02:58:41 +08:00
|
|
|
|
2018-08-14 05:30:27 +08:00
|
|
|
static 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.
|
2018-08-14 05:30:27 +08:00
|
|
|
return !isDWOSection(Sec);
|
2017-11-04 02:58:41 +08:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
static ElfType getOutputElfType(const Binary &Bin) {
|
|
|
|
// Infer output ELF type from the input ELF object
|
|
|
|
if (isa<ELFObjectFile<ELF32LE>>(Bin))
|
|
|
|
return ELFT_ELF32LE;
|
|
|
|
if (isa<ELFObjectFile<ELF64LE>>(Bin))
|
|
|
|
return ELFT_ELF64LE;
|
|
|
|
if (isa<ELFObjectFile<ELF32BE>>(Bin))
|
|
|
|
return ELFT_ELF32BE;
|
|
|
|
if (isa<ELFObjectFile<ELF64BE>>(Bin))
|
|
|
|
return ELFT_ELF64BE;
|
|
|
|
llvm_unreachable("Invalid ELFType");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ElfType getOutputElfType(const MachineInfo &MI) {
|
|
|
|
// Infer output ELF type from the binary arch specified
|
|
|
|
if (MI.Is64Bit)
|
|
|
|
return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
|
|
|
|
else
|
|
|
|
return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
|
|
|
|
}
|
|
|
|
|
2018-08-14 05:30:27 +08:00
|
|
|
static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
|
2018-07-17 06:17:05 +08:00
|
|
|
Object &Obj, 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-08-14 05:30:27 +08:00
|
|
|
static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
|
2018-07-17 06:17:05 +08:00
|
|
|
StringRef File, ElfType OutputElfType) {
|
2018-01-26 06:46:17 +08:00
|
|
|
auto DWOFile = Reader.create();
|
|
|
|
DWOFile->removeSections(
|
2018-08-14 05:30:27 +08:00
|
|
|
[&](const SectionBase &Sec) { return onlyKeepDWOPred(*DWOFile, Sec); });
|
2018-07-07 01:51:03 +08:00
|
|
|
FileBuffer FB(File);
|
2018-08-14 05:30:27 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-10 01:05:21 +08:00
|
|
|
static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
|
|
|
|
Object &Obj) {
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
|
|
|
if (Sec.Name == SecName) {
|
|
|
|
if (Sec.OriginalData.size() == 0)
|
|
|
|
return make_error<StringError>("Can't dump section \"" + SecName +
|
|
|
|
"\": it has no contents",
|
|
|
|
object_error::parse_failed);
|
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
|
|
|
FileOutputBuffer::create(Filename, Sec.OriginalData.size());
|
|
|
|
if (!BufferOrErr)
|
|
|
|
return BufferOrErr.takeError();
|
|
|
|
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
|
|
|
|
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
|
|
|
|
Buf->getBufferStart());
|
|
|
|
if (Error E = Buf->commit())
|
|
|
|
return E;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return make_error<StringError>("Section not found",
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
2018-09-07 16:10:22 +08:00
|
|
|
static bool isCompressed(const SectionBase &Section) {
|
|
|
|
const char *Magic = "ZLIB";
|
|
|
|
return StringRef(Section.Name).startswith(".zdebug") ||
|
|
|
|
(Section.OriginalData.size() > strlen(Magic) &&
|
|
|
|
!strncmp(reinterpret_cast<const char *>(Section.OriginalData.data()),
|
|
|
|
Magic, strlen(Magic))) ||
|
|
|
|
(Section.Flags & ELF::SHF_COMPRESSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCompressable(const SectionBase &Section) {
|
|
|
|
return !isCompressed(Section) && isDebugSection(Section) &&
|
|
|
|
Section.Name != ".gdb_index";
|
|
|
|
}
|
|
|
|
|
2018-10-01 18:29:41 +08:00
|
|
|
static void replaceDebugSections(
|
|
|
|
const CopyConfig &Config, Object &Obj, SectionPred &RemovePred,
|
|
|
|
function_ref<bool(const SectionBase &)> shouldReplace,
|
|
|
|
function_ref<SectionBase *(const SectionBase *)> addSection) {
|
|
|
|
SmallVector<SectionBase *, 13> ToReplace;
|
2018-09-07 16:10:22 +08:00
|
|
|
SmallVector<RelocationSection *, 13> RelocationSections;
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
|
|
|
if (RelocationSection *R = dyn_cast<RelocationSection>(&Sec)) {
|
2018-10-01 18:29:41 +08:00
|
|
|
if (shouldReplace(*R->getSection()))
|
2018-09-07 16:10:22 +08:00
|
|
|
RelocationSections.push_back(R);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-01 18:29:41 +08:00
|
|
|
if (shouldReplace(Sec))
|
|
|
|
ToReplace.push_back(&Sec);
|
2018-09-07 16:10:22 +08:00
|
|
|
}
|
|
|
|
|
2018-10-01 18:29:41 +08:00
|
|
|
for (SectionBase *S : ToReplace) {
|
|
|
|
SectionBase *NewSection = addSection(S);
|
2018-09-07 16:10:22 +08:00
|
|
|
|
|
|
|
for (RelocationSection *RS : RelocationSections) {
|
|
|
|
if (RS->getSection() == S)
|
2018-10-01 18:29:41 +08:00
|
|
|
RS->setSection(NewSection);
|
2018-09-07 16:10:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 18:29:41 +08:00
|
|
|
RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
|
|
|
|
return shouldReplace(Sec) || RemovePred(Sec);
|
2018-09-07 16:10:22 +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-08-14 05:30:27 +08:00
|
|
|
static void handleArgs(const CopyConfig &Config, Object &Obj,
|
2018-07-17 06:17:05 +08:00
|
|
|
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()) {
|
2018-08-14 05:30:27 +08:00
|
|
|
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;
|
|
|
|
|
2018-08-18 06:34:48 +08:00
|
|
|
// Note: these two globalize flags have very similar names but different
|
|
|
|
// meanings:
|
|
|
|
//
|
|
|
|
// --globalize-symbol: promote a symbol to global
|
|
|
|
// --keep-global-symbol: all symbols except for these should be made local
|
|
|
|
//
|
|
|
|
// If --globalize-symbol is specified for a given symbol, it will be
|
|
|
|
// global in the output file even if it is not included via
|
|
|
|
// --keep-global-symbol. Because of that, make sure to check
|
|
|
|
// --globalize-symbol second.
|
|
|
|
if (!Config.SymbolsToKeepGlobal.empty() &&
|
|
|
|
!is_contained(Config.SymbolsToKeepGlobal, Sym.Name))
|
|
|
|
Sym.Binding = STB_LOCAL;
|
|
|
|
|
2018-05-23 02:24:07 +08:00
|
|
|
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-08-10 01:49:04 +08:00
|
|
|
|
|
|
|
if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
|
|
|
|
Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
|
2018-05-23 02:24:07 +08:00
|
|
|
});
|
|
|
|
|
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
|
2018-10-11 05:16:57 +08:00
|
|
|
// symbols are still 'needed' and which are not.
|
2018-05-25 19:01:25 +08:00
|
|
|
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) {
|
2018-08-21 08:13:52 +08:00
|
|
|
return is_contained(Config.ToRemove, Sec.Name);
|
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) {
|
2018-08-14 05:30:27 +08:00
|
|
|
return isDWOSection(Sec) || RemovePred(Sec);
|
2017-11-04 02:58:41 +08:00
|
|
|
};
|
|
|
|
|
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-08-14 05:30:27 +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;
|
|
|
|
}
|
2018-08-14 05:30:27 +08:00
|
|
|
return isDebugSection(Sec);
|
2017-11-14 06:02:07 +08:00
|
|
|
};
|
|
|
|
|
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) {
|
2018-08-14 05:30:27 +08:00
|
|
|
return RemovePred(Sec) || isDebugSection(Sec);
|
2017-11-14 06:13:08 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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;
|
2018-09-07 16:10:22 +08:00
|
|
|
if (StringRef(Sec.Name).startswith(".gnu.warning"))
|
2017-11-28 02:56:01 +08:00
|
|
|
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-08-21 08:13:52 +08:00
|
|
|
if (is_contained(Config.OnlyKeep, Sec.Name))
|
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-07-27 04:05:31 +08:00
|
|
|
if (Obj.SymbolTable == &Sec ||
|
|
|
|
(Obj.SymbolTable && 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-08-21 08:13:52 +08:00
|
|
|
if (is_contained(Config.Keep, Sec.Name))
|
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) &&
|
2018-07-27 04:05:31 +08:00
|
|
|
Obj.SymbolTable && !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-09-07 16:10:22 +08:00
|
|
|
if (Config.CompressionType != DebugCompressionType::None)
|
2018-10-01 18:29:41 +08:00
|
|
|
replaceDebugSections(Config, Obj, RemovePred, isCompressable,
|
|
|
|
[&Config, &Obj](const SectionBase *S) {
|
|
|
|
return &Obj.addSection<CompressedSection>(
|
|
|
|
*S, Config.CompressionType);
|
|
|
|
});
|
|
|
|
else if (Config.DecompressDebugSections)
|
|
|
|
replaceDebugSections(
|
|
|
|
Config, Obj, RemovePred,
|
|
|
|
[](const SectionBase &S) { return isa<CompressedSection>(&S); },
|
|
|
|
[&Obj](const SectionBase *S) {
|
|
|
|
auto CS = cast<CompressedSection>(S);
|
|
|
|
return &Obj.addSection<DecompressedSection>(*CS);
|
|
|
|
});
|
2018-09-07 16:10:22 +08:00
|
|
|
|
2018-01-26 06:46:17 +08:00
|
|
|
Obj.removeSections(RemovePred);
|
2017-12-19 08:47:30 +08:00
|
|
|
|
2018-07-21 03:54:24 +08:00
|
|
|
if (!Config.SectionsToRename.empty()) {
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
|
|
|
const auto Iter = Config.SectionsToRename.find(Sec.Name);
|
2018-08-02 00:23:22 +08:00
|
|
|
if (Iter != Config.SectionsToRename.end()) {
|
|
|
|
const SectionRename &SR = Iter->second;
|
|
|
|
Sec.Name = SR.NewName;
|
|
|
|
if (SR.NewFlags.hasValue()) {
|
|
|
|
// Preserve some flags which should not be dropped when setting flags.
|
|
|
|
// Also, preserve anything OS/processor dependant.
|
|
|
|
const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE |
|
|
|
|
ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
|
|
|
|
ELF::SHF_MASKOS | ELF::SHF_MASKPROC |
|
|
|
|
ELF::SHF_TLS | ELF::SHF_INFO_LINK;
|
|
|
|
Sec.Flags = (Sec.Flags & PreserveMask) |
|
|
|
|
(SR.NewFlags.getValue() & ~PreserveMask);
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 03:54:24 +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-08-10 01:05:21 +08:00
|
|
|
if (!Config.DumpSection.empty()) {
|
|
|
|
for (const auto &Flag : Config.DumpSection) {
|
|
|
|
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
|
|
|
|
StringRef SecName = SecPair.first;
|
|
|
|
StringRef File = SecPair.second;
|
|
|
|
if (Error E = dumpSectionToFile(SecName, File, Obj))
|
|
|
|
reportError(Config.InputFilename, std::move(E));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
static void executeElfObjcopyOnBinary(const CopyConfig &Config, Reader &Reader,
|
|
|
|
Buffer &Out, ElfType OutputElfType) {
|
2018-07-07 01:51:03 +08:00
|
|
|
std::unique_ptr<Object> Obj = Reader.create();
|
2017-08-01 08:33:58 +08:00
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
handleArgs(Config, *Obj, Reader, OutputElfType);
|
2018-07-07 01:51:03 +08:00
|
|
|
|
|
|
|
std::unique_ptr<Writer> Writer =
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
createWriter(Config, *Obj, Out, OutputElfType);
|
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.
|
2018-07-17 06:17:05 +08:00
|
|
|
static Error deepWriteArchive(StringRef ArcName,
|
|
|
|
ArrayRef<NewArchiveMember> NewMembers,
|
|
|
|
bool WriteSymtab, object::Archive::Kind Kind,
|
|
|
|
bool Deterministic, bool Thin) {
|
2018-07-07 01:51:03 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-08-14 05:30:27 +08:00
|
|
|
static void executeElfObjcopyOnArchive(const CopyConfig &Config,
|
2018-08-03 02:16:52 +08:00
|
|
|
const Archive &Ar) {
|
2018-07-07 01:51:03 +08:00
|
|
|
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());
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
Binary *Bin = ChildOrErr->get();
|
|
|
|
|
2018-07-07 01:51:03 +08:00
|
|
|
Expected<StringRef> ChildNameOrErr = Child.getName();
|
|
|
|
if (!ChildNameOrErr)
|
|
|
|
reportError(Ar.getFileName(), ChildNameOrErr.takeError());
|
|
|
|
|
|
|
|
MemBuffer MB(ChildNameOrErr.get());
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
ELFReader Reader(Bin);
|
|
|
|
executeElfObjcopyOnBinary(Config, Reader, MB, getOutputElfType(*Bin));
|
2018-07-07 01:51:03 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-08-17 02:29:40 +08:00
|
|
|
static void restoreDateOnFile(StringRef Filename,
|
|
|
|
const sys::fs::file_status &Stat) {
|
|
|
|
int FD;
|
|
|
|
|
2018-08-30 07:21:56 +08:00
|
|
|
if (auto EC =
|
|
|
|
sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
|
2018-08-17 02:29:40 +08:00
|
|
|
reportError(Filename, EC);
|
|
|
|
|
|
|
|
if (auto EC = sys::fs::setLastAccessAndModificationTime(
|
|
|
|
FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
|
|
|
|
reportError(Filename, EC);
|
|
|
|
|
|
|
|
if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
|
|
|
|
reportError(Filename, EC);
|
|
|
|
}
|
|
|
|
|
2018-08-14 05:30:27 +08:00
|
|
|
static void executeElfObjcopy(const CopyConfig &Config) {
|
2018-08-17 02:29:40 +08:00
|
|
|
sys::fs::file_status Stat;
|
|
|
|
if (Config.PreserveDates)
|
|
|
|
if (auto EC = sys::fs::status(Config.InputFilename, Stat))
|
|
|
|
reportError(Config.InputFilename, EC);
|
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
if (Config.InputFormat == "binary") {
|
|
|
|
auto BufOrErr = MemoryBuffer::getFile(Config.InputFilename);
|
|
|
|
if (!BufOrErr)
|
|
|
|
reportError(Config.InputFilename, BufOrErr.getError());
|
2018-07-07 01:51:03 +08:00
|
|
|
|
2018-08-17 02:29:40 +08:00
|
|
|
FileBuffer FB(Config.OutputFilename);
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-18 02:51:11 +08:00
|
|
|
BinaryReader Reader(Config.BinaryArch, BufOrErr->get());
|
|
|
|
executeElfObjcopyOnBinary(Config, Reader, FB,
|
|
|
|
getOutputElfType(Config.BinaryArch));
|
|
|
|
} else {
|
|
|
|
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())) {
|
|
|
|
executeElfObjcopyOnArchive(Config, *Ar);
|
|
|
|
} else {
|
|
|
|
FileBuffer FB(Config.OutputFilename);
|
|
|
|
Binary *Bin = BinaryOrErr.get().getBinary();
|
|
|
|
ELFReader Reader(Bin);
|
|
|
|
executeElfObjcopyOnBinary(Config, Reader, FB, getOutputElfType(*Bin));
|
|
|
|
}
|
2018-08-17 02:29:40 +08:00
|
|
|
}
|
2018-07-07 01:51:03 +08:00
|
|
|
|
2018-08-17 02:29:40 +08:00
|
|
|
if (Config.PreserveDates) {
|
|
|
|
restoreDateOnFile(Config.OutputFilename, Stat);
|
|
|
|
if (!Config.SplitDWO.empty())
|
|
|
|
restoreDateOnFile(Config.SplitDWO, Stat);
|
|
|
|
}
|
2018-07-07 01:51:03 +08:00
|
|
|
}
|
|
|
|
|
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-09-05 21:10:03 +08:00
|
|
|
DriverConfig DriverConfig;
|
2018-05-08 03:32:09 +08:00
|
|
|
if (sys::path::stem(ToolName).endswith_lower("strip"))
|
2018-09-05 21:10:03 +08:00
|
|
|
DriverConfig = parseStripOptions(makeArrayRef(argv + 1, argc));
|
2018-05-08 03:32:09 +08:00
|
|
|
else
|
2018-09-05 21:10:03 +08:00
|
|
|
DriverConfig = parseObjcopyOptions(makeArrayRef(argv + 1, argc));
|
|
|
|
for (const CopyConfig &CopyConfig : DriverConfig.CopyConfigs)
|
|
|
|
executeElfObjcopy(CopyConfig);
|
2017-08-01 08:33:58 +08:00
|
|
|
}
|