Imported Upstream version 5.18.0.167

Former-commit-id: 289509151e0fee68a1b591a20c9f109c3c789d3a
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-10-20 08:25:10 +00:00
parent e19d552987
commit b084638f15
28489 changed files with 184 additions and 3866856 deletions

View File

@ -1,13 +0,0 @@
set(LLVM_LINK_COMPONENTS
Object
Support
MC
)
add_llvm_tool(llvm-objcopy
llvm-objcopy.cpp
Object.cpp
)
if(LLVM_INSTALL_BINUTILS_SYMLINKS)
add_llvm_tool_symlink(objcopy llvm-objcopy)
endif()

View File

@ -1,21 +0,0 @@
;===- ./tools/llvm-objcopy/LLVMBuild.txt -----------------------*- Conf -*--===;
;
; The LLVM Compiler Infrastructure
;
; This file is distributed under the University of Illinois Open Source
; License. See LICENSE.TXT for details.
;
;===------------------------------------------------------------------------===;
;
; This is an LLVMBuild description file for the components in this subdirectory.
;
; For more information on the LLVMBuild system, please see:
;
; http://llvm.org/docs/LLVMBuild.html
;
;===------------------------------------------------------------------------===;
[component_0]
type = Tool
name = llvm-objcopy
parent = Tools
required_libraries = Object Support MC

File diff suppressed because it is too large Load Diff

View File

@ -1,433 +0,0 @@
//===- Object.h -------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
#define LLVM_TOOLS_OBJCOPY_OBJECT_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/ELFObjectFile.h"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <set>
#include <vector>
namespace llvm {
class FileOutputBuffer;
class SectionBase;
class Segment;
class SectionTableRef {
private:
ArrayRef<std::unique_ptr<SectionBase>> Sections;
public:
SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs)
: Sections(Secs) {}
SectionTableRef(const SectionTableRef &) = default;
SectionBase *getSection(uint16_t Index, Twine ErrMsg);
template <class T>
T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
};
class SectionBase {
public:
StringRef Name;
Segment *ParentSegment = nullptr;
uint64_t HeaderOffset;
uint64_t OriginalOffset;
uint32_t Index;
uint64_t Addr = 0;
uint64_t Align = 1;
uint32_t EntrySize = 0;
uint64_t Flags = 0;
uint64_t Info = 0;
uint64_t Link = ELF::SHN_UNDEF;
uint64_t NameIndex = 0;
uint64_t Offset = 0;
uint64_t Size = 0;
uint64_t Type = ELF::SHT_NULL;
virtual ~SectionBase() = default;
virtual void initialize(SectionTableRef SecTable);
virtual void finalize();
virtual void removeSectionReferences(const SectionBase *Sec);
template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
virtual void writeSection(FileOutputBuffer &Out) const = 0;
};
class Segment {
private:
struct SectionCompare {
bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
// Some sections might have the same address if one of them is empty. To
// fix this we can use the lexicographic ordering on ->Addr and the
// address of the actully stored section.
if (Lhs->OriginalOffset == Rhs->OriginalOffset)
return Lhs < Rhs;
return Lhs->OriginalOffset < Rhs->OriginalOffset;
}
};
std::set<const SectionBase *, SectionCompare> Sections;
ArrayRef<uint8_t> Contents;
public:
uint64_t Align;
uint64_t FileSize;
uint32_t Flags;
uint32_t Index;
uint64_t MemSize;
uint64_t Offset;
uint64_t PAddr;
uint64_t Type;
uint64_t VAddr;
uint64_t OriginalOffset;
Segment *ParentSegment = nullptr;
Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
const SectionBase *firstSection() const {
if (!Sections.empty())
return *Sections.begin();
return nullptr;
}
void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
void writeSegment(FileOutputBuffer &Out) const;
};
class Section : public SectionBase {
private:
ArrayRef<uint8_t> Contents;
public:
Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
void writeSection(FileOutputBuffer &Out) const override;
};
class OwnedDataSection : public SectionBase {
private:
std::vector<uint8_t> Data;
public:
OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
: Data(std::begin(Data), std::end(Data)) {
Name = SecName;
Type = ELF::SHT_PROGBITS;
Size = Data.size();
}
void writeSection(FileOutputBuffer &Out) const override;
};
// There are two types of string tables that can exist, dynamic and not dynamic.
// In the dynamic case the string table is allocated. Changing a dynamic string
// table would mean altering virtual addresses and thus the memory image. So
// dynamic string tables should not have an interface to modify them or
// reconstruct them. This type lets us reconstruct a string table. To avoid
// this class being used for dynamic string tables (which has happened) the
// classof method checks that the particular instance is not allocated. This
// then agrees with the makeSection method used to construct most sections.
class StringTableSection : public SectionBase {
private:
StringTableBuilder StrTabBuilder;
public:
StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
Type = ELF::SHT_STRTAB;
}
void addString(StringRef Name);
uint32_t findIndex(StringRef Name) const;
void finalize() override;
void writeSection(FileOutputBuffer &Out) const override;
static bool classof(const SectionBase *S) {
if (S->Flags & ELF::SHF_ALLOC)
return false;
return S->Type == ELF::SHT_STRTAB;
}
};
// Symbols have a st_shndx field that normally stores an index but occasionally
// stores a different special value. This enum keeps track of what the st_shndx
// field means. Most of the values are just copies of the special SHN_* values.
// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
enum SymbolShndxType {
SYMBOL_SIMPLE_INDEX = 0,
SYMBOL_ABS = ELF::SHN_ABS,
SYMBOL_COMMON = ELF::SHN_COMMON,
SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
};
struct Symbol {
uint8_t Binding;
SectionBase *DefinedIn = nullptr;
SymbolShndxType ShndxType;
uint32_t Index;
StringRef Name;
uint32_t NameIndex;
uint64_t Size;
uint8_t Type;
uint64_t Value;
uint8_t Visibility;
uint16_t getShndx() const;
};
class SymbolTableSection : public SectionBase {
protected:
std::vector<std::unique_ptr<Symbol>> Symbols;
StringTableSection *SymbolNames = nullptr;
using SymPtr = std::unique_ptr<Symbol>;
public:
void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
uint16_t Shndx, uint64_t Sz);
void addSymbolNames();
const SectionBase *getStrTab() const { return SymbolNames; }
const Symbol *getSymbolByIndex(uint32_t Index) const;
void removeSectionReferences(const SectionBase *Sec) override;
void initialize(SectionTableRef SecTable) override;
void finalize() override;
static bool classof(const SectionBase *S) {
return S->Type == ELF::SHT_SYMTAB;
}
};
// Only writeSection depends on the ELF type so we implement it in a subclass.
template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
void writeSection(FileOutputBuffer &Out) const override;
};
struct Relocation {
const Symbol *RelocSymbol = nullptr;
uint64_t Offset;
uint64_t Addend;
uint32_t Type;
};
// All relocation sections denote relocations to apply to another section.
// However, some relocation sections use a dynamic symbol table and others use
// a regular symbol table. Because the types of the two symbol tables differ in
// our system (because they should behave differently) we can't uniformly
// represent all relocations with the same base class if we expose an interface
// that mentions the symbol table type. So we split the two base types into two
// different classes, one which handles the section the relocation is applied to
// and another which handles the symbol table type. The symbol table type is
// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
class RelocationSectionBase : public SectionBase {
protected:
SectionBase *SecToApplyRel = nullptr;
public:
const SectionBase *getSection() const { return SecToApplyRel; }
void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
static bool classof(const SectionBase *S) {
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
}
};
// Takes the symbol table type to use as a parameter so that we can deduplicate
// that code between the two symbol table types.
template <class SymTabType>
class RelocSectionWithSymtabBase : public RelocationSectionBase {
private:
SymTabType *Symbols = nullptr;
protected:
RelocSectionWithSymtabBase() = default;
public:
void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
void removeSectionReferences(const SectionBase *Sec) override;
void initialize(SectionTableRef SecTable) override;
void finalize() override;
};
template <class ELFT>
class RelocationSection
: public RelocSectionWithSymtabBase<SymbolTableSection> {
private:
using Elf_Rel = typename ELFT::Rel;
using Elf_Rela = typename ELFT::Rela;
std::vector<Relocation> Relocations;
template <class T> void writeRel(T *Buf) const;
public:
void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
void writeSection(FileOutputBuffer &Out) const override;
static bool classof(const SectionBase *S) {
if (S->Flags & ELF::SHF_ALLOC)
return false;
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
}
};
class SectionWithStrTab : public Section {
private:
const SectionBase *StrTab = nullptr;
public:
SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
void removeSectionReferences(const SectionBase *Sec) override;
void initialize(SectionTableRef SecTable) override;
void finalize() override;
static bool classof(const SectionBase *S);
};
class DynamicSymbolTableSection : public SectionWithStrTab {
public:
DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
static bool classof(const SectionBase *S) {
return S->Type == ELF::SHT_DYNSYM;
}
};
class DynamicSection : public SectionWithStrTab {
public:
DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
static bool classof(const SectionBase *S) {
return S->Type == ELF::SHT_DYNAMIC;
}
};
class DynamicRelocationSection
: public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
private:
ArrayRef<uint8_t> Contents;
public:
DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
void writeSection(FileOutputBuffer &Out) const override;
static bool classof(const SectionBase *S) {
if (!(S->Flags & ELF::SHF_ALLOC))
return false;
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
}
};
template <class ELFT> class Object {
private:
using SecPtr = std::unique_ptr<SectionBase>;
using SegPtr = std::unique_ptr<Segment>;
using Elf_Shdr = typename ELFT::Shdr;
using Elf_Ehdr = typename ELFT::Ehdr;
using Elf_Phdr = typename ELFT::Phdr;
void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
SymbolTableSection *SymTab, SectionTableRef SecTable);
SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
const Elf_Shdr &Shdr);
void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
protected:
StringTableSection *SectionNames = nullptr;
SymbolTableSection *SymbolTable = nullptr;
std::vector<SecPtr> Sections;
std::vector<SegPtr> Segments;
void writeHeader(FileOutputBuffer &Out) const;
void writeProgramHeaders(FileOutputBuffer &Out) const;
void writeSectionData(FileOutputBuffer &Out) const;
void writeSectionHeaders(FileOutputBuffer &Out) const;
public:
uint8_t Ident[16];
uint64_t Entry;
uint64_t SHOffset;
uint32_t Type;
uint32_t Machine;
uint32_t Version;
uint32_t Flags;
bool WriteSectionHeaders = true;
Object(const object::ELFObjectFile<ELFT> &Obj);
virtual ~Object() = default;
const SymbolTableSection *getSymTab() const { return SymbolTable; }
const SectionBase *getSectionHeaderStrTab() const { return SectionNames; }
void removeSections(std::function<bool(const SectionBase &)> ToRemove);
void addSection(StringRef SecName, ArrayRef<uint8_t> Data);
virtual size_t totalSize() const = 0;
virtual void finalize() = 0;
virtual void write(FileOutputBuffer &Out) const = 0;
};
template <class ELFT> class ELFObject : public Object<ELFT> {
private:
using SecPtr = std::unique_ptr<SectionBase>;
using SegPtr = std::unique_ptr<Segment>;
using Elf_Shdr = typename ELFT::Shdr;
using Elf_Ehdr = typename ELFT::Ehdr;
using Elf_Phdr = typename ELFT::Phdr;
void sortSections();
void assignOffsets();
public:
ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
void finalize() override;
size_t totalSize() const override;
void write(FileOutputBuffer &Out) const override;
};
template <class ELFT> class BinaryObject : public Object<ELFT> {
private:
using SecPtr = std::unique_ptr<SectionBase>;
using SegPtr = std::unique_ptr<Segment>;
uint64_t TotalSize;
public:
BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
void finalize() override;
size_t totalSize() const override;
void write(FileOutputBuffer &Out) const override;
};
} // end namespace llvm
#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H

View File

@ -1,345 +0,0 @@
//===- llvm-objcopy.cpp ---------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm-objcopy.h"
#include "Object.h"
#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"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <functional>
#include <iterator>
#include <memory>
#include <string>
#include <system_error>
#include <utility>
using namespace llvm;
using namespace object;
using namespace ELF;
// The name this program was invoked as.
static StringRef ToolName;
namespace llvm {
LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
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);
}
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();
errs() << ToolName << ": '" << File << "': " << Buf;
exit(1);
}
} // 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>"),
cl::init("-"));
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"));
using SectionPred = std::function<bool(const SectionBase &Sec)>;
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) {
std::unique_ptr<FileOutputBuffer> Buffer;
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
FileOutputBuffer::create(File, Obj.totalSize(),
FileOutputBuffer::F_executable);
handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) {
error("failed to open " + OutputFilename);
});
Buffer = std::move(*BufferOrErr);
Obj.write(*Buffer);
if (auto E = Buffer->commit())
reportError(File, errorToErrorCode(std::move(E)));
}
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);
}
// 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.
template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) {
std::unique_ptr<Object<ELFT>> Obj;
if (!OutputFormat.empty() && OutputFormat != "binary")
error("invalid output format '" + OutputFormat + "'");
if (!OutputFormat.empty() && OutputFormat == "binary")
Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile);
else
Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile);
if (!SplitDWO.empty())
SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue());
SectionPred RemovePred = [](const SectionBase &) { return false; };
// Removes:
if (!ToRemove.empty()) {
RemovePred = [&](const SectionBase &Sec) {
return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
std::end(ToRemove);
};
}
if (StripDWO || !SplitDWO.empty())
RemovePred = [RemovePred](const SectionBase &Sec) {
return IsDWOSection(Sec) || RemovePred(Sec);
};
if (ExtractDWO)
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec);
};
if (StripAllGNU)
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
if (RemovePred(Sec))
return true;
if ((Sec.Flags & SHF_ALLOC) != 0)
return false;
if (&Sec == Obj->getSectionHeaderStrTab())
return false;
switch (Sec.Type) {
case SHT_SYMTAB:
case SHT_REL:
case SHT_RELA:
case SHT_STRTAB:
return true;
}
return Sec.Name.startswith(".debug");
};
if (StripSections) {
RemovePred = [RemovePred](const SectionBase &Sec) {
return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0;
};
Obj->WriteSectionHeaders = false;
}
if (StripDebug) {
RemovePred = [RemovePred](const SectionBase &Sec) {
return RemovePred(Sec) || Sec.Name.startswith(".debug");
};
}
if (StripNonAlloc)
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
if (RemovePred(Sec))
return true;
if (&Sec == Obj->getSectionHeaderStrTab())
return false;
return (Sec.Flags & SHF_ALLOC) == 0;
};
if (StripAll)
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
if (RemovePred(Sec))
return true;
if (&Sec == Obj->getSectionHeaderStrTab())
return false;
if (Sec.Name.startswith(".gnu.warning"))
return false;
return (Sec.Flags & SHF_ALLOC) == 0;
};
// Explicit copies:
if (!OnlyKeep.empty()) {
RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
// Explicitly keep these sections regardless of previous removes.
if (std::find(std::begin(OnlyKeep), std::end(OnlyKeep), Sec.Name) !=
std::end(OnlyKeep))
return false;
// Allow all implicit removes.
if (RemovePred(Sec)) {
return true;
}
// Keep special sections.
if (Obj->getSectionHeaderStrTab() == &Sec) {
return false;
}
if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) {
return false;
}
// Remove everything else.
return true;
};
}
if (!Keep.empty()) {
RemovePred = [RemovePred](const SectionBase &Sec) {
// Explicitly keep these sections regardless of previous removes.
if (std::find(std::begin(Keep), std::end(Keep), Sec.Name) !=
std::end(Keep))
return false;
// Otherwise defer to RemovePred.
return RemovePred(Sec);
};
}
Obj->removeSections(RemovePred);
if (!AddSection.empty()) {
for (const auto &Flag : AddSection) {
auto SecPair = StringRef(Flag).split("=");
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();
Obj->addSection(SecName, ArrayRef<uint8_t>(BufPtr, BufSize));
}
}
Obj->finalize();
WriteObjectFile(*Obj, OutputFilename.getValue());
}
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 (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) {
CopyBinary(*o);
return 0;
}
if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) {
CopyBinary(*o);
return 0;
}
if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) {
CopyBinary(*o);
return 0;
}
if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) {
CopyBinary(*o);
return 0;
}
reportError(InputFilename, object_error::invalid_file_type);
}

View File

@ -1,37 +0,0 @@
//===- llvm-objcopy.h -------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_OBJCOPY_OBJCOPY_H
#define LLVM_TOOLS_OBJCOPY_OBJCOPY_H
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
namespace llvm {
LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message);
// This is taken from llvm-readobj.
// [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38)
template <class T> T unwrapOrError(Expected<T> EO) {
if (EO)
return *EO;
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(EO.takeError(), OS, "");
OS.flush();
error(Buf);
}
} // end namespace llvm
#endif // LLVM_TOOLS_OBJCOPY_OBJCOPY_H